Slicing: yes.
Broadcasting: I think only so, that you can operate on arrays by scalars, the scalar gets broadcasted.
Slicing: yes.
Broadcasting: I think only so, that you can operate on arrays by scalars, the scalar gets broadcasted.
a = 5
b = np.array([1, 2, 3])
a + b
>>> array([6, 7, 8])
Looking through the documentation you get the feeling you are learning a whole new language whose syntax could change under your feet. They would have done better to use something like APL :)This is very natural, and consistent with standard mathematical notation. It's an abuse of notation so common that is barely ever mentioned in elementary mathematics. For example, when you work with functions f:R→R, like f:x↦3x², it is common to denote constants by their value. Thus you write 7 instead of x↦7. Then you can write simply f+7, where f is a function and 7 is a value, and everybody understands what you mean. No need to write ridiculously correct stuff like f+(x↦7) .
Broadcasting with arrays is very natural if you interpret an array as a function of its indices. It is exactly the same thing as above!
I second that we should be using APL anyways.
Which mathematical notation? One of the first things a student of linear algebra gets taught is that you cant add scalars to vectors. The example you gave is not what is taught in most math classes, and seems weird to everyone unless you come from Bourbaki camp.
> Broadcasting with arrays is very natural if you interpret an array as a function of its indices. It is exactly the same thing as above!
But an array is just a contiguous data structure, not a function of its indicies.
I meant this. Very few people look at a number 7 and think of it as a constant function
When you do signal processing, you pretty much interpret 1d arrays as functions of time, 2d arrays as functions on the plane, etc. In that case, it is very natural.
If I have an array "A" that represents an image, I can change the britghtness and contrast of the image by doing an operation like "B = α*A + β". This notation requires broadcasting because β is a constant, not an array (or, equivalently, a function).
But you dont write it like this in linear algebra. You write
B = αA + b
where b is just a vector. Or at least you would make β bold to distinguish it from a scalar B = α*A + β
means just B(t) = α*A(t) + β
for all relevant t. This broadcasting is used everywhere in signal and image processing, and it would be extremely unnatural if your language forced you to write some monstrosity that modified the constant β so that it has the same type as A.What exactly does this mean?
Since you can sum scalars to functions, it is natural to sum scalars to vectors.
B = aA + b
is not well defined. [1 2] [3 5]
2 [ ] + 1 = [ ]
[3 4] [7 9] [1 2] [1 2]
2 [ ] + 1 = 3 [ ]
[3 4] [3 4]
or, better yet, nothing. Mathematically that operation is not defined.