Element-wise addition operator

37 views
Skip to first unread message

Zizheng Tai

unread,
Aug 29, 2016, 10:16:20 PM8/29/16
to Scala Breeze
Hi everyone,

I'm trying out Breeze and notice that both + and :+ can be used for element-wise addition of vectors. The :+ operator seems more consistent with other element-wise operators (:*, :/, etc.) though. Also, / and :/ seem to have the same effect, too. Is there any difference between them? Which one should I prefer?

Thanks,
Zizheng

David Hall

unread,
Aug 29, 2016, 10:22:41 PM8/29/16
to scala-...@googlegroups.com
Hi Zizheng,

The operators without ":" are "shaped", while the ones with ":" are elementwise. It happens that addition of vectors corresponds to elementwise addition, which is why they're the same. (This is usually true, of course...) 

If I knew what I knew now, I would probably not allow / to mean elementwise division (except by scalar). I may still do that after a suitable deprecation cycle. 

I personally usually use + over :+, and similarly with other operators when they have the same meaning. The main reason for this is operator precedence. All operators starting with : (and not ending in =) have the same precedence in scala, which is highly inconvenient in this case, though I can't fault them for choosing a relatively simple rule.

Note that in master and snapshots, the :+ style operators are deprecated because of the confusing precedence. We're switching to +:+, etc, which are a bit more verbose, but don't have precedence problems.

--
You received this message because you are subscribed to the Google Groups "Scala Breeze" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-breeze+unsubscribe@googlegroups.com.
To post to this group, send email to scala-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scala-breeze/23c05286-8556-4174-b783-fb9315a171a1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages