3 ms·
Broadcasting in Julia (Illustrated)
- npalli 3y agoCan someone explain how this compares to numpy which also can broadcast. Broadcasting over user defined functions seems to not be possible in numpy. Plus the syntax is clunky (in numpy)
- adgjlsfhk1 3y agoThe two major differences are that broadcasting in julia is 1. caller based. As a user, I can ask for a broadcasted version of any function (not only specific functions which implement broadcasting) 2. composable. The expression `sqrt.(exp.(x).+1)` in julia will generate a single loop over `x` compared to the numpy version which will make 3 passes over the memory.
- npalli 3y agoThanks, seem very much in line with broadcasting being first class in the language unlike Python where the whole scientific ecosystem is grafted on.
- Bostonian 3y agoModern Fortran also has broadcasting. The intrinsic functions such as `exp` take either scalars or arrays and return a scalar or array of the same shape. Users can also define such `elemental` functions.
- ChrisRackauckas 3y agoThat's the most basic feature of broadcasting, but not quite what Julia's broadcasting is all about. There's also the case of things like f(x,y) = cos(x)*sin(y) g(x,y) = tan(x)*inv(y) x, y = (rand(4), rand(4)) z = rand(4) z .= f.(x,y') .+ g.(x',y) to create create one loop that creates whole matrix of results and writes in-place into z. Is there a syntax for that in modern Fortran? I know that it has intrinsic functions which accepts vectors but its this fusion of multiple operations mixed with how it handles differently-sized objects that really defines the braodcasting operation.
- ChrisRackauckas 3y agoLooking at https://fortranwiki.org/fortran/show/elemental https://fortranwiki.org/fortran/show/elemental, it doesn't discuss how to fuse elementals with out elemental operations, their behavior on different dimensional inputs, or how to make any operation elemental (for example the use case demonstrated in the blog post of `string.([:hi,:bye])` where a built-in operation for turning something into a strong is transformed into an array operation on-demand by the caller).
- sundarurfriend 3y agohttps://julialang.org/blog/2017/01/moredots/ https://julialang.org/blog/2017/01/moredots/ is a great post for anyone who needs more explanation of this fusion of multiple operations.