# Automatic Differentiation Built Into LFortran

**URL:** <https://fortran-lang.discourse.group/t/automatic-differentiation-built-into-lfortran/3198>\
**Category:** Language enhancement\
**Created:** [April 10, 2022, 9:02am UTC](https://fortran-lang.discourse.group/t/automatic-differentiation-built-into-lfortran/3198 "2022-04-10T09:02:43Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![kimala](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/kimala/32/2707_2.png) [@kimala](https://fortran-lang.discourse.group/u/kimala)\
**Post date:** [December 29, 2023, 5:36pm UTC](https://fortran-lang.discourse.group/t/automatic-differentiation-built-into-lfortran/3198/21 "2023-12-29T17:36:07Z")

</div>

I know this is a newbie question, but in my field (oceanography) lots of people says that Python and other languages are better because you can use AD instead of finite differences, and yet I cannot find papers or resources that explain how to compute a derivative in an automatic way without using an actual function but rather an array of points. Does someone have some resources about that?

---

<div class="post-metadata">

**Author:** ![oscardssmith](https://avatars.discourse-cdn.com/v4/letter/o/b9e5f3/32.png) [@oscardssmith](https://fortran-lang.discourse.group/u/oscardssmith)\
**Post date:** [December 29, 2023, 7:34pm UTC](https://fortran-lang.discourse.group/t/automatic-differentiation-built-into-lfortran/3198/22 "2023-12-29T19:34:35Z")

</div>

ad is a source to source translation. it doesn’t just use the list of numbers. The difference between AD and symbolic differentiation is that symbolic differentiation requires flattening program execution, while AD works with more dynamic program flow by following the flow that the primal execution.

---

<div class="post-metadata">

**Author:** ![gardhor](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/gardhor/32/88_2.png) [@gardhor](https://fortran-lang.discourse.group/u/gardhor)\
**Post date:** [December 29, 2023, 7:36pm UTC](https://fortran-lang.discourse.group/t/automatic-differentiation-built-into-lfortran/3198/23 "2023-12-29T19:36:59Z")

</div>

- You can look to the this website: [www.Autodiff.org - Community Portal on Automatic Differentiation](https://www.autodiff.org/?module=Introduction)  
Although I’m not sure if it is up to date
- Wikipedia explains very well the automatic differentiation schemes: [Automatic differentiation - Wikipedia](https://en.wikipedia.org/wiki/Automatic_differentiation)  
The forward mode is easy to understand.

You have AD schemes in many languages, see the autodiff web page

---

<div class="post-metadata">

**Author:** ![sblionel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/sblionel/32/853_2.png) [@sblionel](https://fortran-lang.discourse.group/u/sblionel)\
**Post date:** [December 29, 2023, 7:49pm UTC](https://fortran-lang.discourse.group/t/automatic-differentiation-built-into-lfortran/3198/24 "2023-12-29T19:49:38Z")

</div>

> [@general\_rishkin](#):
>
> I am so glad it is also under J3 consideration.

The Github @certik linked to collects suggestions for J3 to consider in the future - an entry there does not mean J3 is actively discussing it, but it’s a handy place to flesh things out before formal consideration.

---

<div class="post-metadata">

**Author:** ![rwmsu](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@rwmsu](https://fortran-lang.discourse.group/u/rwmsu)\
**Post date:** [December 29, 2023, 10:52pm UTC](https://fortran-lang.discourse.group/t/automatic-differentiation-built-into-lfortran/3198/25 "2023-12-29T22:52:53Z")

</div>

The following ArXiv survey paper has an informative discussion on the various forms of AD as it pertains to Machine Learning.

> **[Automatic differentiation in machine learning: a survey](https://arxiv.org/abs/1502.05767)**
>
> Derivatives, mostly in the form of gradients and Hessians, are ubiquitous in machine learning. Automatic differentiation (AD), also called algorithmic differentiation or simply "autodiff", is a family of techniques similar to but more general than...

---

<div class="post-metadata">

**Author:** ![aerosayan](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@aerosayan](https://fortran-lang.discourse.group/u/aerosayan)\
**Post date:** [December 29, 2023, 11:13pm UTC](https://fortran-lang.discourse.group/t/automatic-differentiation-built-into-lfortran/3198/26 "2023-12-29T23:13:29Z")

</div>

> [@general\_rishkin](#):
>
> to have automatic differentiation built into the LFortran compiler as a first class citizen

That is a great idea, though I believe it’s somewhat easier to develop advanced features from currently available features in the language, instead of directly baking them into the compiler.

For Autodiff specifically, OOP based operator overloading would be enough for the users to implement it.

Though it is little bit harder to reuse old code without modifying them to use the new Autodiff derived types, so I would like to gently point out that having generic types would’ve helped here.

Although using Fypp preprocessor, we can achieve the same result as using generic types, so it’s not much of a problem.

---

<div class="post-metadata">

**Author:** ![rwmsu](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@rwmsu](https://fortran-lang.discourse.group/u/rwmsu)\
**Post date:** [December 29, 2023, 11:39pm UTC](https://fortran-lang.discourse.group/t/automatic-differentiation-built-into-lfortran/3198/27 "2023-12-29T23:39:17Z")

</div>

And for those who toil in the CFD vineyard and are looking for a Fortran specific approach for computing flux Jacobians using AD, I can also recommend the following arXiv pre-print and the subsequent Journal of Computational Physics article.

> **[Automatic Differentiation using Operator Overloading (ADOO) for implicit...](https://arxiv.org/abs/1904.02136v1)**
>
> Implicit time integration schemes are widely used in computational fluid dynamics numerical codes to speed-up computations. Indeed, implicit schemes usually allow for less stringent time-step stability constraints than their explicit counterpart. The...

See also JCP, Vol 399, (2019) 108942

[https://www.sciencedirect.com/science/article/abs/pii/S0021999119306473](https://www.sciencedirect.com/science/article/abs/pii/S0021999119306473)

---

<div class="post-metadata">

**Author:** ![gardhor](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/gardhor/32/88_2.png) [@gardhor](https://fortran-lang.discourse.group/u/gardhor)\
**Post date:** [April 9, 2024, 10:24pm UTC](https://fortran-lang.discourse.group/t/automatic-differentiation-built-into-lfortran/3198/28 "2024-04-09T22:24:29Z")

</div>

@fedebenelli , I’ve made an update of my automatic differentiation library [AD\_dnSVM](https://github.com/lauvergn/AD_dnSVM) and now it is possible to compute g(x,y) from f(x,y).

```fortran
f = TWO * X**2 * Y
g = deriv(f,ider=1)**2 * X/Y

```

where, X,Y,f and g are extended dual numbers (with higher derivatives).

[Previous page](https://fortran-lang.discourse.group/t/automatic-differentiation-built-into-lfortran/3198.md?page=1)
