Assessment of combination with vectorized function in Julia

Julia uses a dot vector function for elementary manipulation ..

Running f.(x)means f(x[1]), f(x[2])... are executed sequentially

However, suppose I have a function that takes two arguments, for example g(x,y)

I wish g(x[1],y[1]), g(x[2],y[1]), g(x[3],y[1]), ..., g(x[1],y[2]), g(x[2],y[2]), g(x[3],y[2]), ...

Is there any way to evaluate all combinations of xand y?

+6
source share
2 answers

Matt's answer is good, but I would like to provide an alternative using an understanding of the array:

julia> x = 1:5
       y = 10:10:50
       [i + j for i in x, j in y]
5Γ—5 Array{Int64,2}:
 11  21  31  41  51
 12  22  32  42  52
 13  23  33  43  53
 14  24  34  44  54
 15  25  35  45  55

, , broadcast reshape.

+5

, y , x. . broadcast . "" .

, x y x y :

julia> x = 1:5
       y = 10:10:50
       (+).(x, reshape(y, 1, length(y)))
5Γ—5 Array{Int64,2}:
 11  21  31  41  51
 12  22  32  42  52
 13  23  33  43  53
 14  24  34  44  54
 15  25  35  45  55

, ; x y , .

+5
source

Source: https://habr.com/ru/post/1015801/


All Articles