# Jacobian of a multi-inputs function

**URL:** <https://discourse.julialang.org/t/jacobian-of-a-multi-inputs-function/33787>\
**Category:** New to Julia\
**Created:** [January 25, 2020, 7:26pm UTC](https://discourse.julialang.org/t/jacobian-of-a-multi-inputs-function/33787 "2020-01-25T19:26:34Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![AhmedAlreweny](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@AhmedAlreweny](https://discourse.julialang.org/u/AhmedAlreweny)\
**Post date:** [January 25, 2020, 7:26pm UTC](https://discourse.julialang.org/t/jacobian-of-a-multi-inputs-function/33787/1 "2020-01-25T19:26:34Z")

</div>

Hello,  
I have a function which represents the mapping F(x,p), where x is a vector and p is a scalar parameter.  
The function (mapping) output is a vector. I would like to compute the the jacobian (in this case the directional derivative as I have a scalar parameter p) of the function F wrt the parameter p. How can I go around implementing that. I am using

```julia
global n
n = x #vector
 FiniteDifferences.jacobian(central_fdm(3, 1), F, p)[1]

```

to pass the x vector to the function F, which is not ideal. Is there a way to pass the value of x while taking the derivative wrt to p other than the global variables?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 25, 2020, 8:14pm UTC](https://discourse.julialang.org/t/jacobian-of-a-multi-inputs-function/33787/2 "2020-01-25T20:14:21Z")

</div>

Use a closure, i.e. `p -> F(x,p)`

---

<div class="post-metadata">

**Author:** ![AhmedAlreweny](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@AhmedAlreweny](https://discourse.julialang.org/u/AhmedAlreweny)\
**Post date:** [January 30, 2020, 2:35pm UTC](https://discourse.julialang.org/t/jacobian-of-a-multi-inputs-function/33787/3 "2020-01-30T14:35:49Z")

</div>

Calculating the jacobian this way is very expensive. I recently came across your ‘new’ package `FiniteDiff`. I was wondering how could I calculate the Jacobian with the new package if I have such a mapping. I tried something like

```julia
FiniteDiff.finite_difference_jacobian!(p-> Derdp(x,p), p)

```

but it didn’t work out.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 30, 2020, 2:42pm UTC](https://discourse.julialang.org/t/jacobian-of-a-multi-inputs-function/33787/4 "2020-01-30T14:42:38Z")

</div>

Only use the mutating function if you are mutating. `FiniteDiff.finite_difference_jacobian(p-> Derdp(x,p), p)` would work.

---

<div class="post-metadata">

**Author:** ![AhmedAlreweny](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@AhmedAlreweny](https://discourse.julialang.org/u/AhmedAlreweny)\
**Post date:** [January 30, 2020, 2:47pm UTC](https://discourse.julialang.org/t/jacobian-of-a-multi-inputs-function/33787/5 "2020-01-30T14:47:56Z")

</div>

> [@ChrisRackauckas](#):
>
> `FiniteDiff.finite_difference_jacobian(p-> Derdp(x,p), p)` would work.

This is actually what I tried, sorry the above quote was wrong. however, it gives the following error:

```julia
ERROR: MethodError: no method matching finite_difference_jacobian(::getfield(Main, Symbol("##53#54")){Int64}, ::Float64)

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 30, 2020, 3:50pm UTC](https://discourse.julialang.org/t/jacobian-of-a-multi-inputs-function/33787/6 "2020-01-30T15:50:52Z")

</div>

Why is `p` a Float64? You need to take the Jacobian with respect to a vector.

---

<div class="post-metadata">

**Author:** ![AhmedAlreweny](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@AhmedAlreweny](https://discourse.julialang.org/u/AhmedAlreweny)\
**Post date:** [January 30, 2020, 4:07pm UTC](https://discourse.julialang.org/t/jacobian-of-a-multi-inputs-function/33787/7 "2020-01-30T16:07:09Z")

</div>

p is supposed to be a vector of parameters. I started with a simple case where I have only one parameter, so the jacobian \<–\> directional derivative. Maybe I should pass it as a vector of a single element?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 30, 2020, 4:24pm UTC](https://discourse.julialang.org/t/jacobian-of-a-multi-inputs-function/33787/8 "2020-01-30T16:24:28Z")

</div>

Yes, it’s telling you that the function doesn’t handle scalars.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [January 30, 2020, 4:24pm UTC](https://discourse.julialang.org/t/jacobian-of-a-multi-inputs-function/33787/9 "2020-01-30T16:24:50Z")

</div>

Yes. Unlike in Matlab, Julia, cares about scalar vs array vs matrix. A 1 element vector is not the same as a scalar.

---

<div class="post-metadata">

**Author:** ![AhmedAlreweny](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@AhmedAlreweny](https://discourse.julialang.org/u/AhmedAlreweny)\
**Post date:** [January 31, 2020, 8:48am UTC](https://discourse.julialang.org/t/jacobian-of-a-multi-inputs-function/33787/10 "2020-01-31T08:48:16Z")

</div>

Thanks! It is working now much much faster than before. I appreciate your help.
