# Numerical derivative of an array

**URL:** <https://root-forum.cern.ch/t/numerical-derivative-of-an-array/57754>\
**Category:** Newbie\
**Created:** [January 6, 2024, 10:00am UTC](https://root-forum.cern.ch/t/numerical-derivative-of-an-array/57754 "2024-01-06T10:00:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![samuelb91](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/samuelb91/32/35418_2.png) [@samuelb91](https://root-forum.cern.ch/u/samuelb91)\
**Post date:** [January 6, 2024, 10:00am UTC](https://root-forum.cern.ch/t/numerical-derivative-of-an-array/57754/1 "2024-01-06T10:00:56Z")

</div>

I have an array of doubles I’d like to compute the second derivative of. I’ve poked around on the forums/documentation for a little while, and it seems like ROOT doesn’t support numerical differentiation of an array in ways that other platforms do (Matlab for example). Is this true, and if so, are there any suggestions for a work around? Thanks in advance.

_ROOT Version:_ 6.16.00  
_Platform:_ Debian  
_Compiler:_ g++

* * *

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [January 6, 2024, 6:30pm UTC](https://root-forum.cern.ch/t/numerical-derivative-of-an-array/57754/2 "2024-01-06T18:30:28Z")

</div>

Hello Samuel,

Indeed I don’t think we provide anything out of the box in ROOT, but I am sure several examples online can be used in C++ or Python. Once this is done, the result will be directly usable in ROOT!

Cheers,  
Danilo

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [January 8, 2024, 8:56am UTC](https://root-forum.cern.ch/t/numerical-derivative-of-an-array/57754/3 "2024-01-08T08:56:09Z")

</div>

I typically use the central finite difference method:

> **[Finite difference](https://en.wikipedia.org/wiki/Finite_difference)**
>
> A finite difference is a mathematical expression of the form f (x + b) − f (x + a). If a finite difference is divided by b − a, one gets a difference quotient. The approximation of derivatives by finite differences plays a central role in finite difference methods for the numerical solution of differential equations, especially boundary value problems.
> The difference operator, commonly denoted 
>   
>     
>       
> Δ
>       
>     
> {\\displaystyle \\Delta }
>   
> is the operator that maps a fu...

```auto

double arr[N];
//Initialize here your array "arr"
double d1[N]{};
double d2[N]{};
const double step = 1.; // bin width
for(size_t i=1; i<N-1;++i) {
    d1[i] = (arr[i+1] - arr[i-1])/(2.*step);
    d2[i] = (arr[i+1] + arr[i-1] - 2*arr[i])/step;
}

```

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [July 16, 2026, 9:11am UTC](https://root-forum.cern.ch/t/numerical-derivative-of-an-array/57754/4 "2026-07-16T09:11:05Z")

</div>

There is now TMath::Gradient and TMath::Laplacian to do that (there’s a bug though in the second)
