by sampo · 2y ago · 0 comments
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.
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).