differences between element wise summation and element wise products

Viewed 172

I am really new to Julia, and so I am going to ask a very very elementary question. I understand the logic of the dot (.) to make element-wise operation however, I am perplexed by this very simple piece of code

array_try_op = [1, 2, 3, 4]
array_sum2 = array_try_op .+ 2
array_sum2_wrong = array_try_op + 2
array_prod2 = array_try_op .* 2
array_prod2_wrong = array_try_op * 2

Indeed, when I try to perform array_sum2_wrong = array_try_op + 2 I correctly get an error message, but on the contrary array_prod2_wrong = array_try_op * 2 does not return any error message, and actually returns the same result of array_prod2 = array_try_op .* 2

Why this is the case?

1 Answers

Because scaling by a scalar is a well-defined (and required!) operation on vector spaces, which Arrays are treated as. On the other hand, addition between a vector and a scalar does not really make sense, unless you want broadcasting behaviour -- which the dot indicates. Of course, scaling and broadcasted multiplication with a scalar coincide.

Related