I would like to call a function with all combination of values from several arrays, and get the corresponding array of values. Is a convenient general way to do that? Broadcasting comes first to my mind, but I wouldn’t call it convenient for this case, e.g.:
First it is very verbose and repetitive, and second it is error-prone especially if some of those arrays have the same size. I failed to find a better way, is there any?
Oh, really! I didn’t even look at that method thinking that Iterators are “single-dimensional” only. I guess that will work fine for me.
If only it could also transparently be applied to cases like f.(scalar, scalar, arr, arr, scalar, a=b, c=d)…
Also nice suggestion, thank you - didn’t know about reshape with colon.
However, one needs to be very careful with transpose in Julia: at first I used a and b' for two-dimensional broadcast as you say, and surprisingly got wrong results. After digging in it turned out that transpose is not the same transpose I got used to in other languages, but also performs conjugation. Yes, my arrays were complex in that case, but after that I’m certainly not going to use transpose for broadcasting. Broadcasting syntax should not depend on the array element type, otherwise you can easily get errors like this when some eltype changes.
And anyway, transpose solves only a single simple case of multidimensional broadcasting, it is not of much help for > 2 arrays, or when they are > 1 dimensional (see 1st post for example of both). And it doesn’t work at all with non-numeric types.
Be aware, though, that transpose does not do complex conjugation, while 'does (' is the same as adjoint). Both of them work recursively on their elements, which is important to be aware of, if the elements of your array can themselves be transposed.
Ah, ok. Actually approach to transpose in julia feels like an inconvenience for me, and very simple stuff like “swap rows and columns in e.g. a matplotlib plot” cannot be written as ax' like in Python (ax.T there of course), but only as permutedims(ax, 2, 1). However after reading the discussion I see that the majority is happy with the current state, so it will stay like this.
Are you sure? You should be able to use ' or transpose in most cases. Are you talking about higher-dimensional arrays? You would have the same problem in Matlab, btw. And in Matlab ' also complex conjugates.
Wow, that caught me by surprise. To be honest, I personally think that should work. The reason it’s not is that there is no method for transpose(::Symbol). Might be worth opening a discussion thread on that topic specifically.
In numpy transpose arr.T just swaps rows and cols of an array, without any regards to its internal structure (eltype), and without modifying elements of course, unlike arr' in julia.
And yes, there is not transpose(::Symbol), as well as no transpose(::anything else but for numerics). And as I understood the corresponding discussion, it is by design.
OK, didn’t know that. Anyway, I think it’s strange that transpose doesn’t work on non-numeric matrices. I would ask about it specifically if I were you, unless you’ve seen somewhere that it is definitely intensional.
Anyway, note that for matrices, you don’t need to specify dimension to swap, since there are only two:
Cool, didn’t know about that as well (permutedims without explicit dims)!
And as for transpose, it is definitely intensional:
permutedims(m::AbstractMatrix) is now short for permutedims(m, (2,1)) , and is now a more convenient way of making a “shallow transpose” of a 2D array. This is the recommended approach for manipulating arrays of data, rather than the recursively defined, linear-algebra function transpose . Similarly, permutedims(v::AbstractVector) will create a row matrix (#24839).