# Function on array

**URL:** <https://discourse.julialang.org/t/function-on-array/22323>\
**Category:** General Usage\
**Created:** [March 25, 2019, 8:59pm UTC](https://discourse.julialang.org/t/function-on-array/22323 "2019-03-25T20:59:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)\
**Post date:** [March 25, 2019, 8:59pm UTC](https://discourse.julialang.org/t/function-on-array/22323/1 "2019-03-25T20:59:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [March 25, 2019, 9:15pm UTC](https://discourse.julialang.org/t/function-on-array/22323/2 "2019-03-25T21:15:29Z")

</div>

Do you need something more than standard array indexing?

```julia
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

```

---

<div class="post-metadata">

**Author:** ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)\
**Post date:** [March 25, 2019, 9:27pm UTC](https://discourse.julialang.org/t/function-on-array/22323/3 "2019-03-25T21:27:24Z")

</div>

If a have n variables?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 25, 2019, 9:34pm UTC](https://discourse.julialang.org/t/function-on-array/22323/4 "2019-03-25T21:34:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tkluck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkluck/32/15769_2.png) [@tkluck](https://discourse.julialang.org/u/tkluck)\
**Post date:** [March 25, 2019, 9:43pm UTC](https://discourse.julialang.org/t/function-on-array/22323/5 "2019-03-25T21:43:15Z")

</div>

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

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

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

```

---

<div class="post-metadata">

**Author:** ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)\
**Post date:** [March 25, 2019, 11:53pm UTC](https://discourse.julialang.org/t/function-on-array/22323/6 "2019-03-25T23:53:48Z")

</div>

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,:])?

---

<div class="post-metadata">

**Author:** ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)\
**Post date:** [March 25, 2019, 11:54pm UTC](https://discourse.julialang.org/t/function-on-array/22323/7 "2019-03-25T23:54:05Z")

</div>

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,:])?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 25, 2019, 11:59pm UTC](https://discourse.julialang.org/t/function-on-array/22323/8 "2019-03-25T23:59:30Z")

</div>

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

```julia
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
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

```

---

<div class="post-metadata">

**Author:** ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)\
**Post date:** [March 26, 2019, 12:17am UTC](https://discourse.julialang.org/t/function-on-array/22323/9 "2019-03-26T00:17:31Z")

</div>

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!
