Giter VIP home page Giter VIP logo

Comments (4)

mikmoore avatar mikmoore commented on May 25, 2024 2

I don't think ' should be broadcast via @.. It gets used too often for reshaping, such as in @. x + x'. A decent amount of code would break (and some of it would break quietly) if this interaction were changed.

If one wants it to broadcast, one can use adjoint directly a la @. x + adjoint(x).

But to others' suggestions, it could be worth documenting this behavior. What other operators are skipped by @.? So far, I've noticed ', :, _[_...]. To get these to broadcast via @., one needs to use adjoint, Colon(), getindex/setindex! directly. We also don't broadcast any flavor of parenthetical (i.e., () when not a call, [], {}), obviously.

That's what I could glean from the source, but the metaprograming is a little sophisticated for me so I'm quite certain I've missed things.

I notice that one can use (:).(x,y) but not @. (:)(x,y). This is probably a bug an unavoidable consequence of x:y and (:)(x,y) being AST-equivalent and the desire to be able to write things like @. x[begin+1:end] - x[begin:end-1] without the @. doing weird things to indexing ranges. Although : is mostly used with scalars for indexing so this may not be a problem. Can someone provide additional commentary or background on why : is excluded?

from julia.

mbauman avatar mbauman commented on May 25, 2024 1

Colons used to have their own special Expr(:(:), ...) syntax form and thus wasn't considered a called function initially. Its special-casing in dottable was added when its syntactic special-casing was removed. But you're probably right — as a function with only scalar methods defined in Base, I don't think you'd ever see a difference (outside of things that were previously errors or perhaps some package methods that stretch the generic definition to its limit).

And that's where it's quite different from '.

But where it's similar is that you can't syntactically "add dots" to the infix 1:2 or 3:4:5; x.:y means something very different. Similarly, .' doesn't work at all (and used to mean something very different).

Edit: I started this comment not sure how much intentionality was behind this restriction, but after writing that last paragraph I'm convinced there's a good rhyme and reason to it all — it's about syntactically "adding dots."

from julia.

knuesel avatar knuesel commented on May 25, 2024

A grep for "@\.[^#]*'" in my local files also found two instances (here and here) in Makie:

z = @. sin(x) * cos(x')

and

zs = @. √(xs^2 + ys'^2)

Maybe a better fix would be to document the current behavior as a special case...

from julia.

knuesel avatar knuesel commented on May 25, 2024

Regarding why @. is not allowed to broadcast : I can imagine an index type that

  1. supports : to make valid index ranges and
  2. behaves as non-scalar under broadcasting.

Then @. would interfere with indexing in something like @. x[a:b] * y. Is that why : is special cased?

This is theoretical though. In practice I can't come up with an example where broadcasting : causes problems. To take @mikmoore 's example:

julia> x = 1:3;

julia> @. x[begin+1:end] * x[begin:end-1]
2-element Vector{Int64}:
 2
 6

This still works after allowing @. to broadcast the : operator:

julia> Broadcast.dottable(x::Symbol) = (!Base.isoperator(x) || first(string(x)) != '.' || x === :..)

julia> @. x[begin+1:end] * x[begin:end-1]
2-element Vector{Int64}:
 2
 6

and now we can do the following (which doesn't work without the dottable redefinition):

julia> @. 1:x
3-element Vector{UnitRange{Int64}}:
 1:1
 1:2
 1:3

from julia.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.