# Evaluate function of several variables on cartesian product?

**URL:** <https://discourse.julialang.org/t/evaluate-function-of-several-variables-on-cartesian-product/1572>\
**Category:** General Usage\
**Created:** [January 18, 2017, 4:55pm UTC](https://discourse.julialang.org/t/evaluate-function-of-several-variables-on-cartesian-product/1572 "2017-01-18T16:55:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [January 18, 2017, 4:55pm UTC](https://discourse.julialang.org/t/evaluate-function-of-several-variables-on-cartesian-product/1572/1 "2017-01-18T16:55:35Z")

</div>

The title is not very good but what I want to is the following:

```
H(x->f(x),x) #returns a vector 
H((x,y)->f(x,y),x,y) #returns a matrix of dimension (length(x),length(y))
H((x,y,z)->f(x,y,z),x,y,z) #returns a 3D matrix of dimension (length(x),length(y),length(z))
etc...

```

Is there a generic function `H(f,vars....)` to do that ? x,y,z being vectors.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 18, 2017, 5:05pm UTC](https://discourse.julialang.org/t/evaluate-function-of-several-variables-on-cartesian-product/1572/2 "2017-01-18T17:05:34Z")

</div>

You can use broadcasting: `f.(x)` to get a vector, `f.(x, y.')` to get a matrix, `f.(x, y.', reshape(z, 1, 1, length(z))` to get a 3d array, and so on. Or a comprehension: `[f(x,y,z) for x in x, y in y, z in z]` will return a 3d array, for example.

---

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [January 19, 2017, 9:55am UTC](https://discourse.julialang.org/t/evaluate-function-of-several-variables-on-cartesian-product/1572/3 "2017-01-19T09:55:08Z")

</div>

Ok thanks, I’ll try to write my own then.

---

<div class="post-metadata">

**Author:** ![novatena](https://avatars.discourse-cdn.com/v4/letter/n/c68b51/32.png) [@novatena](https://discourse.julialang.org/u/novatena)\
**Post date:** [January 19, 2017, 3:20pm UTC](https://discourse.julialang.org/t/evaluate-function-of-several-variables-on-cartesian-product/1572/4 "2017-01-19T15:20:21Z")

</div>

You can use the function `product` defined in **Iterators** packet. It returns an array of tuples which represents the cartesian products of input iterators.

You can write a function like this:

```julia
    function myproduct(f, c...)
        pro = Iterators.product(c...)
        reshape(map(x->f(x...), pro), map(length, pro.xss))
    end

```

Example:

```julia
julia> a = 1:3;

julia> b = 1:3;

julia> myproduct(-,a,b)
3×3 Array{Int64,2}:
 0 -1 -2
 1 0 -1
 2 1 0

```

---

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [January 20, 2017, 10:23am UTC](https://discourse.julialang.org/t/evaluate-function-of-several-variables-on-cartesian-product/1572/5 "2017-01-20T10:23:44Z")

</div>

That’s elegant, but a bit slow (about 100x slower than the optimal), at least on 0.50.
