4 ms·
> does it support slicing and broadcasting Slicing: yes. Broadcasting: I think only so, that you can operate on arrays by scalars, the scalar gets broadcasted
by sampo 3y ago
> does it support slicing and broadcasting
Slicing: yes.
Broadcasting: I think only so, that you can operate on arrays by scalars, the scalar gets broadcasted.
- v9v 3y agonot sure if it's different from what you are saying, but the elemental specifier[0] makes functions broadcastable, such functions operate on each element of its input separately. [0] https://fortranwiki.org/fortran/show/elemental https://fortranwiki.org/fortran/show/elemental
- medo-bear 3y agoI think broadcasting in numpy is over rated. Its rules are complicated and ironically non-mathematical. Why should this be obvious? 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 :)
- enriquto 3y ago> Why should this be obvious? 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.
- medo-bear 3y ago> This is very natural, and consistent with standard mathematical notation. 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.
- enriquto 3y agoI don't understand what you mean. Precisely the Bourbaki camp would be the only people who would avoid the broadcasting notation that is commonly used in math.
- medo-bear 3y ago> Thus you write 7 instead of x↦7 I meant this. Very few people look at a number 7 and think of it as a constant function
- enriquto 3y agoMy point exactly. That's what makes broadcasting natural!
- medo-bear 3y agoOur positions diverge because you take the extra-formal specification in mathematics as basis, and then say, "nah thats too formal", broadcasting is more natural. On the other hand I take elementary to undergrad mathematics as basis and say that rules about adding (using a standard '+' symbol) a scalar to a vector in numpy via broadcasting is confusing.
- ayhanfuat 3y agoJulia requires you to explicitly specify if you want to use broadcasting (i.e. `[2, 4, 8] + 3` fails so you need to do `[2, 4, 8] .+ 3`). When I first came across this I thought it was unnecessary but the more I use it the more I like it because I think it improved my mental model of broadcasting. With numpy it was mostly trial and error.
- enriquto 3y ago> 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).
- ivanpribec 3y agoFor "broadcasting" an array along a particular axis, you'll need to use the function "spread(source,dim,ncopies)". But mixed array/scalar expressions are indeed supported.