# Complex-valued functions with real arguments - ForwardDiff, JuliaDiff

**URL:** <https://discourse.julialang.org/t/complex-valued-functions-with-real-arguments-forwarddiff-juliadiff/27386>\
**Category:** Numerics\
**Created:** [August 10, 2019, 10:43am UTC](https://discourse.julialang.org/t/complex-valued-functions-with-real-arguments-forwarddiff-juliadiff/27386 "2019-08-10T10:43:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcvb97](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@marcvb97](https://discourse.julialang.org/u/marcvb97)\
**Post date:** [August 10, 2019, 10:43am UTC](https://discourse.julialang.org/t/complex-valued-functions-with-real-arguments-forwarddiff-juliadiff/27386/1 "2019-08-10T10:43:43Z")

</div>

When executing the following code to apply ForwardDiff to a complex-valued function with real argument:

```
using ForwardDiff
f2(t::Real) = real( (cos(t) + 1im*sin(t))^(1/2) )
t = pi/2
ForwardDiff.derivative(f2,t)

```

I get the following error:

```
ERROR: StackOverflowError:
Stacktrace:
 [1] Type at ./complex.jl:12 [inlined] (repeats 2 times)
 [2] float at ./complex.jl:1002 [inlined]
 [3] 
_cpow(::Complex{ForwardDiff.Dual{ForwardDiff.Tag{typeof(f2),Float64},Float64,1}}, ::ForwardDiff.Dual{ForwardDiff.Tag{typeof(f2),Float64},Float64,1}) at ./complex.jl:770 (repeats 80000 times)

```

I wonder what the constraints are when using ForwardDiff to this type of functions.  
All help is appreciated.

---

<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:** [August 10, 2019, 12:59pm UTC](https://discourse.julialang.org/t/complex-valued-functions-with-real-arguments-forwarddiff-juliadiff/27386/2 "2019-08-10T12:59:08Z")

</div>

ForwardDiff needs a lot more than just opening up type signatures to work with complex numbers, so I wouldn’t expect this to just work any time soon. You can split to real and imaginary parts and diff that. We are making essentially a ForwardDiff 2.0 (with Jarrett’s blessing 🙂 ) to address this, along with the lack of multivariate rules (use BLAS!) and post-hoc rule definition (via ChainRules).

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 10, 2019, 1:55pm UTC](https://discourse.julialang.org/t/complex-valued-functions-with-real-arguments-forwarddiff-juliadiff/27386/3 "2019-08-10T13:55:39Z")

</div>

> [@ChrisRackauckas](#):
>
> making essentially a ForwardDiff 2.0 (with Jarrett’s blessing 🙂 ) to address this

Can you please link the repository? Or is it just a new, breaking release of ForwardDiff?

---

<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:** [August 10, 2019, 2:11pm UTC](https://discourse.julialang.org/t/complex-valued-functions-with-real-arguments-forwarddiff-juliadiff/27386/4 "2019-08-10T14:11:38Z")

</div>

> [@Tamas\_Papp](#):
>
> Can you please link the repository? Or is it just a new, breaking release of ForwardDiff?

It’s a new repo. I hope we can open it up by next week with some preliminary results. It’s quite cool: it’s struct of arrays and matrix backed with Cassette generating the connection to ChainRules.jl

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [August 10, 2019, 3:11pm UTC](https://discourse.julialang.org/t/complex-valued-functions-with-real-arguments-forwarddiff-juliadiff/27386/5 "2019-08-10T15:11:00Z")

</div>

Also, you will be able to do this type of automatic differentiation with [Grassmann.jl](https://github.com/chakravala/Grassmann.jl) after I finish implementing the [Leibniz.jl](https://github.com/chakravala/Leibniz.jl) package for working with the symmetric differentials. Complex number algebra is simply a subalgera of the geometric algebra in `Grassmann` and by extending it with the symmetric `Leibniz` algebra the automatic differentiation is automatically built-in, as described in my paper.

```nohighlight
julia> using Grassmann, basis"2"
(⟨++⟩, v, v₁, v₂, v₁₂)

julia> f2(t) = real(sqrt(cos(t)+vectorspace(t)(I)*sin(t)))
f2 (generic function with 1 method)

julia> f2((pi/2)v)
0.7070714177261098v⃖

```

All of the methods needed for your AD example are already implemented in `Grassmann` variables so now I only need to finish implementing the extended support for the `Leibniz` indices and then you will be able to also tack on the derivatives also to get the forward mode AD.

```nohighlight
julia> @basis tangent(ℝ^2)
(⟨++₁⟩, v, v₁, v₂, ∂₁, v₁₂, ∂₁v₁, ∂₁v₂, ∂₁v₁₂)

```

You will be able to work with it using this extended `tangent` basis… which is still a work in progress. In the near future I will resume work on this aspect when I get around to it.

---

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [September 28, 2020, 10:30pm UTC](https://discourse.julialang.org/t/complex-valued-functions-with-real-arguments-forwarddiff-juliadiff/27386/6 "2020-09-28T22:30:51Z")

</div>

Hello,

> [@ChrisRackauckas](#):
>
> ForwardDiff needs a lot more than just opening up type signatures to work with complex numbers, so I wouldn’t expect this to just work any time soon. You can split to real and imaginary parts and diff that. We are making essentially a ForwardDiff 2.0 (with Jarrett’s blessing 🙂 ) to address this, along with the lack of multivariate rules (use BLAS!) and post-hoc rule definition (via ChainRules).

@ChrisRackauckas, is there any news about your package for automatic differentiation with complex numbers?

---

<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:** [September 28, 2020, 10:41pm UTC](https://discourse.julialang.org/t/complex-valued-functions-with-real-arguments-forwarddiff-juliadiff/27386/7 "2020-09-28T22:41:30Z")

</div>

[https://github.com/YingboMa/ForwardDiff2.jl](https://github.com/YingboMa/ForwardDiff2.jl), it does a lot but Cassette is being abandoned. ForwardDiff got (limited) support for Complex though, so that should be fine.
