Function on array

Hello Julia’s users,
I have a little question: If I have a Mathematical function of 3 variações, let
f(x) = x(1) + 3 sin(x(2)) + 1/x(3)
And a array, let a = [1,2,3] of length 3.
How can I availble this function along this vector, i.e., calculate f with x(1) = a[1], x(2) = a[2] and x(3) = a[3], or f(1,2,3) = 1 + sin(2) + 1/3?

Do you need something more than standard array indexing?

julia> f(x1, x2, x3) = x1 + 3sin(x2) + 1/x3
f (generic function with 1 method)

julia> f(x) = f(x...)
f (generic function with 2 methods)

julia> f([1,2,3])
4.061225613810378

If a have n variables?

Can you post a complete example of what you are trying to do? It’s hard to help when we don’t know exactly what you want.

1 Like

@stillyslalom 's answer has what you need: the “splat operator” .... It can also be used as follows:

julia> f(x1, x2, x3) = x1 + 3sin(x2) + 1/x3
f (generic function with 1 method)

julia> f([1,2,3]...)
4.061225613810378

My problem is the following: I have a matrix of m lines and n columns. Let A this matriz, A = rand(m,n). Consider the function f(x) a function of n variables, x = (x[1],…x[n]) where n is given by the user. Let the function f(x) = sum((x[i] - 1) for i in 1:n).
The question is: how can I eval each line o A in this function? Recall that the line i of A is a vector of length n. For example, how can I calculate f(A[i,:])?

My problem is the following: I have a matrix of m lines and n columns. Let A this matriz, A = rand(m,n). Consider the function f(x) a function of n variables, x = (x[1],…x[n]) where n is given by the user. Let the function f(x) = sum((x[i] - 1) for i in 1:n).
The question is: how can I eval each line o A in this function? Recall that the line i of A is a vector of length n. For example, how can I calculate f(A[i,:])?

As far as I can tell, your question literally answers itself :slight_smile: All I had to do was copy in the functions exactly as you wrote them:

julia> f(x) = sum((x[i] - 1) for i in 1:n)
f (generic function with 1 method)

julia> m = 10
10

julia> n = 5
5

julia> A = rand(m,n)
10×5 Array{Float64,2}:
 0.277094  0.7306     0.28146     0.458967  0.691436 
 0.477905  0.355686   0.00673167  0.957138  0.351736 
 0.43662   0.369704   0.702524    0.246111  0.235479 
 0.597162  0.71314    0.571849    0.516778  0.992487 
 0.619666  0.11355    0.113864    0.88801   0.493072 
 0.654798  0.0283057  0.80974     0.134625  0.779239 
 0.580662  0.226542   0.858494    0.52336   0.82212  
 0.934111  0.851859   0.57078     0.125764  0.707969 
 0.106628  0.319482   0.19381     0.633272  0.0843583
 0.372935  0.248298   0.103394    0.957219  0.532761 

julia> i = 1
1

julia> f(A[i,:])
-2.5604425755948945

If you want to compute f(A[i, :]) for each row i, then you can use another comprehension:

julia> [f(A[i,:]) for i in 1:size(A, 1)]
10-element Array{Float64,1}:
 -2.5604425755948945
 -2.8508035815576784
 -3.0095627401690637
 -1.6085832396218775
 -2.77183885186226  
 -2.5932921630133396
 -1.9888216065245665
 -1.8095174340528288
 -3.6624498607396623
 -2.7853927632472653
1 Like

You’re an extremely kind person.
Solved my problem.
I was manipulating a function in the variables x and y not as x [1] and x [2].
A hug!