Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

broadcasting .+ versus "algebraic" + #1953

Closed
StefanKarpinski opened this issue Jan 9, 2013 · 8 comments
Closed

broadcasting .+ versus "algebraic" + #1953

StefanKarpinski opened this issue Jan 9, 2013 · 8 comments

Comments

@StefanKarpinski
Copy link
Member

https://groups.google.com/d/topic/julia-dev/CXUBlOPov3k/discussion
https://groups.google.com/d/topic/julia-dev/E47eV6B8w78/discussion

I'm opening this issue even though I now actually think we don't need separate .+ and + operators. The reason why is here:

broadcast distribute

Basically, matrix multiplication distributes correctly over broadcasting addition if it is even meaningful for it to do so. There are, however, cases where "distributing" doesn't make sense:

  • matrix left-multiplication can't sensibly distribute over addition of a matrix and a row vector
  • matrix right-multiplication can't sensibly distribute over addition of a matrix and a column vector.

From this I tend to conclude that it's reasonable to just have + broadcast singleton dimensions instead of introducing a new .+ operator for broadcasting addition.

cc: @toivoh

@JeffBezanson
Copy link
Member

Ok.

@ViralBShah
Copy link
Member

Also see #1019

@nolta
Copy link
Member

nolta commented Jan 9, 2013

Broadcasting + is a little too magical for my tastes. Personally, i'd rather A+u throws an error.

@StefanKarpinski
Copy link
Member Author

Would it be acceptable to have .+ be broadcasting and have + require compatible shapes?

@nolta
Copy link
Member

nolta commented Jan 9, 2013

Sure. I thought we had come up with a nice solution in the first email thread (e.g., https://groups.google.com/d/msg/julia-dev/CXUBlOPov3k/cyUZjzQIJW0J).

@toivoh
Copy link
Contributor

toivoh commented Jan 9, 2013

@StefanKarpinski: How about the case of adding a matrix and a scalar:

M*v + s*v    = (M + s*eye)*v   = (M + s)*v
M.*v .+ s.*v = (M + s*ones).*v = (M .+ s).*v

where M, v, and s is a matrix, vector, and scalar (Number or Array{T,0}), respectively.
There, matrix multiplication does not distribute correctly over broadcasting addition.

@toivoh
Copy link
Contributor

toivoh commented Jan 10, 2013

Would it be acceptable to have .+ be broadcasting and have + require compatible shapes?

I'm for this. I think that this was the original conclusion, with + being pretty conservative about shapes, probably not even adding trailing singleton dimensions.

Also, there was the prospect to have

matrix + scalar = matrix + one(matrix)*scalar  # for square matrices

i.e. to multiply the scalar with an appropriate identity matrix before adding. Starting out with a conservative +, that could of course be added afterwards.

@JeffBezanson
Copy link
Member

Broadcasting .+ now implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants