# Evaluate multi-variable anonymous function for one variable

**URL:** <https://discourse.julialang.org/t/evaluate-multi-variable-anonymous-function-for-one-variable/11397>\
**Category:** General Usage\
**Created:** [June 3, 2018, 8:35pm UTC](https://discourse.julialang.org/t/evaluate-multi-variable-anonymous-function-for-one-variable/11397 "2018-06-03T20:35:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leblouba\_Moussa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leblouba_moussa/32/11570_2.png) [@Leblouba\_Moussa](https://discourse.julialang.org/u/Leblouba_Moussa)\
**Post date:** [June 3, 2018, 8:35pm UTC](https://discourse.julialang.org/t/evaluate-multi-variable-anonymous-function-for-one-variable/11397/1 "2018-06-03T20:35:32Z")

</div>

Hi Julians,

I have an anonymous function f, of several variables x1, x2,…, xn, say for example:  
f1 = (x1, x2, x3) = 5x1 + x2^2 + x3^0.25  
I would like to create a new function, say g, with g = f(x1,value2, value3), with value2 and value3 are real numbers and x1 should remain as a variable. For example, if value2= 0.1 and value3= 3.0, I should be able to obtain g(x1) = 5x1 + 1.326074. It is similar to substitution in Maple or Matlab.  
How would you solve this problem.

Many thanks

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [June 3, 2018, 8:59pm UTC](https://discourse.julialang.org/t/evaluate-multi-variable-anonymous-function-for-one-variable/11397/2 "2018-06-03T20:59:35Z")

</div>

You do it, as you would in Matlab, with a closure:

```julia
julia> f1 = (x1, x2, x3) -> 5x1 + x2^2 + x3^0.25
(::#3) (generic function with 1 method)

julia> const value1,value2= 4,5 # use const here for performace
(4, 5)
                                                                                                                                                                     
julia> g = x1 -> f1(x1, value1, value2)                                                                                                                                  
(::#7) (generic function with 1 method)                                                                                                                                  

julia> g(5)                                                                                                                                                              
42.49534878122122                                                                                                                                                        

```

---

<div class="post-metadata">

**Author:** ![Leblouba\_Moussa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leblouba_moussa/32/11570_2.png) [@Leblouba\_Moussa](https://discourse.julialang.org/u/Leblouba_Moussa)\
**Post date:** [June 3, 2018, 10:12pm UTC](https://discourse.julialang.org/t/evaluate-multi-variable-anonymous-function-for-one-variable/11397/3 "2018-06-03T22:12:43Z")

</div>

Thank you @mauro3 for the prompt answer. I’m aware of what you said, however, I’m looking for a way to generate the function g (of a single variable) based on an input. g could be a function of x1 or x2, or xi. We don’t know until we know what are the inputs. Say for example we pay an array [value1, x2, value2], in this case, the array has numerical values for x1 and x3, then g would be a function of x2 only. How can we solve this type of problem?

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [June 4, 2018, 5:25am UTC](https://discourse.julialang.org/t/evaluate-multi-variable-anonymous-function-for-one-variable/11397/4 "2018-06-04T05:25:30Z")

</div>

I don’t really understand what you want. Maybe you need to combine above with an `if` and generate a different closure depending on what inputs you got? Send a code snippet of what you got (please quote you code: [PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530)).

---

<div class="post-metadata">

**Author:** ![Leblouba\_Moussa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leblouba_moussa/32/11570_2.png) [@Leblouba\_Moussa](https://discourse.julialang.org/u/Leblouba_Moussa)\
**Post date:** [June 4, 2018, 4:13pm UTC](https://discourse.julialang.org/t/evaluate-multi-variable-anonymous-function-for-one-variable/11397/5 "2018-06-04T16:13:44Z")

</div>

I don’t have a code yet; the code is an implementation of a modified Point Estimate Method. Given a function f(x1,x2,…,xn) we want to fix all variables except xi, then evaluate the function at selected points. We repeat this process by leaving one variable and fixing the rest of variables each time. The function is input by the user, for example in the form of an anonymous function.  
The process needs to be automatic, given any function f.  
**Example** :  
f(x1, x2, x3) = x1^2 + x1\*cos(x2) - 5x3

_step 1_: leave x1 and fix x2 and x3 at 1.5 and 0.8  
g1(x1) = f(x1, 1.5, 0.8)  
_step 2_: leave x2 and fix x1 and x3 at 0.75 and 0.8  
g2(x2) = f(0.75, x2, 0.8)  
_step 3_: leave x3 and fix x1 and x2 at 0.75 and 1.5  
g3(x3) = f(0.75, 1.5, x3)

Hope this clarifies the problem.  
Thank you.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [June 4, 2018, 6:47pm UTC](https://discourse.julialang.org/t/evaluate-multi-variable-anonymous-function-for-one-variable/11397/6 "2018-06-04T18:47:28Z")

</div>

Read the link I posted (again) 😉

---

<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:** [June 4, 2018, 7:22pm UTC](https://discourse.julialang.org/t/evaluate-multi-variable-anonymous-function-for-one-variable/11397/7 "2018-06-04T19:22:40Z")

</div>

As @mauro3 suggested, all you need to do is create a new anonymous function for each of your `g_i`, where each anonymous function takes only one argument (`x_i`) and stores the appropriate values of the other arguments to `f`. A function with some of its arguments already bound to values is a [closure](https://en.wikipedia.org/wiki/Closure_(computer_programming)) and we usually write them in Julia with anonymous functions.

For example, given `f(x, y)`, we can make `g1` an anonymous function that only takes `x` and holds a default value for `y`:

```julia
julia> f(x, y) = x + y
f (generic function with 1 method)

julia> g1 = x -> f(x, 2.0) # y = 2.0
(::#1) (generic function with 1 method)

julia> g2 = y -> f(1.5, y) # x = 1.5
(::#3) (generic function with 1 method)

julia> g1(1.5) == g2(2.0) == f(1.5, 2.0)
true

```

To automate this across all the inputs to some function `f`, we just need to build up those anonymous functions in a loop.

Here’s one way to do it. I’m using the helpful `setindex` function from `StaticArrays.jl` to make this a bit more efficient:

```julia
julia> using StaticArrays: setindex

julia> function make_closures(f, default_values...)
         [xi -> f(setindex(default_values, xi, i)...) for i in 1:length(default_values)]
       end
make_closures (generic function with 1 method)

julia> gs = make_closures(f, 0.75, 1.5, 0.8)
3-element Array{##11#13{Int64,#f,Tuple{Float64,Float64,Float64}},1}:
 #11
 #11
 #11

julia> gs[1](1.0) == f(1.0, 1.5, 0.8)
true

julia> gs[2](1.0) == f(0.75, 1.0, 0.8)
true

julia> gs[3](1.0) == f(0.75, 1.5, 1.0)
true

```

A very nice feature of Julia is that you can do this with essentially no performance penalty. Using BenchmarkTools.jl:

```julia
julia> using BenchmarkTools

julia> @btime $(gs[1])(0.75)
  14.931 ns (0 allocations: 0 bytes)

```

Also, as @mauro3 suggested, it will be easier for us to read your code and help you if you [quote it properly](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530)

---

<div class="post-metadata">

**Author:** ![Leblouba\_Moussa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leblouba_moussa/32/11570_2.png) [@Leblouba\_Moussa](https://discourse.julialang.org/u/Leblouba_Moussa)\
**Post date:** [June 4, 2018, 8:03pm UTC](https://discourse.julialang.org/t/evaluate-multi-variable-anonymous-function-for-one-variable/11397/8 "2018-06-04T20:03:39Z")

</div>

Thank you very much @rdeits and @mauro3 for your kind help, very much appreciated.  
I have the code written in Matlab and I’m planning to write it in Julia. I will share the full code with all Julians in this forum. Here is the paper that details the Point Estimate Method I’m implementing: [System Reliability Assessment by Method of Moments](https://doi.org/10.1061/(ASCE)0733-9445(2003)129:10(1341)).
