# Trying to define imaginary error function

**URL:** <https://root-forum.cern.ch/t/trying-to-define-imaginary-error-function/50032>\
**Category:** Newbie\
**Tags:** root\
**Created:** [May 19, 2022, 2:18pm UTC](https://root-forum.cern.ch/t/trying-to-define-imaginary-error-function/50032 "2022-05-19T14:18:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![analyce](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/analyce/32/29734_2.png) [@analyce](https://root-forum.cern.ch/u/analyce)\
**Post date:** [May 19, 2022, 2:18pm UTC](https://root-forum.cern.ch/t/trying-to-define-imaginary-error-function/50032/1 "2022-05-19T14:18:54Z")

</div>

I need to use the imaginary error function to fit some data. ROOT does not have a predefined erfi(), so I tried using the definition  
erfi(z) = -i \* erf(i\*z)  
I found an error function in RooMath that will take complex z, but it uses the std::complex class. I’m using TF1 to define this function, but it will not take the std::complex variable, instead it will only use the TComplex version of i. RooMath won’t take TComplex so I can’t get the erfi() to work. Is there a way to implement this?

`std::complex j = 1i;  
TF1 \*erfi = new TF1(“erfi”,"-[0]\*RooMath::erf([0]\*x)",0,10);  
erfi-\>SetParameters(0,j);  
’

_ROOT Version:_ Not Provided  
_Platform:_ Not Provided  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 19, 2022, 2:43pm UTC](https://root-forum.cern.ch/t/trying-to-define-imaginary-error-function/50032/2 "2022-05-19T14:43:34Z")

</div>

@moneta can help you.

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [May 19, 2022, 4:50pm UTC](https://root-forum.cern.ch/t/trying-to-define-imaginary-error-function/50032/3 "2022-05-19T16:50:10Z")

</div>

Hi,  
You should be able to use std::complex inside the TFormula string , but then the expression should evaluate to a real number, otherwise you can construct a TF1. You can use either the imaginary part, the real , the module or the phase, depending on your problem, or you need to define two different TF1 classes, (actually TF2) for one of the case. Example:

```auto
typedef std::complex<double> complex_t;
TF2 * erfReal = new TF2("erfR", "RooMath::erf(complex_t(x,y)).real()",0,10,0,10);
TF2 * erfImag = new TF2("erfI", "RooMath::erf(complex_t(x,y)).imag()",0,10,0,10);

```

Lorenzo

---

<div class="post-metadata">

**Author:** ![analyce](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/analyce/32/29734_2.png) [@analyce](https://root-forum.cern.ch/u/analyce)\
**Post date:** [May 19, 2022, 6:05pm UTC](https://root-forum.cern.ch/t/trying-to-define-imaginary-error-function/50032/5 "2022-05-19T18:05:34Z")

</div>

I tried using the TF2 functions you provided, but it’s now giving me this error:

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/7/e/7ea9837d6493c37ae99b8603ac7de856f582689a.png)

I also tried passing in a std::complex variable through the FixParameter and got this error:

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/8/5/85e3f80c2a03abdb9e256372211337a2569095ec.png)

---

<div class="post-metadata">

**Author:** ![joa](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/dec6dc/32.png) [@joa](https://root-forum.cern.ch/u/joa)\
**Post date:** [May 19, 2022, 7:04pm UTC](https://root-forum.cern.ch/t/trying-to-define-imaginary-error-function/50032/6 "2022-05-19T19:04:09Z")

</div>

Hi,

I’m somewhat intrigued by your problem as there are no complex observables… what are you actually fitting?

Cheers

Joa

---

<div class="post-metadata">

**Author:** ![analyce](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/analyce/32/29734_2.png) [@analyce](https://root-forum.cern.ch/u/analyce)\
**Post date:** [May 19, 2022, 7:36pm UTC](https://root-forum.cern.ch/t/trying-to-define-imaginary-error-function/50032/7 "2022-05-19T19:36:07Z")

</div>

I’m just fitting some data that has to do with the momentum of kaons coming out of particle collisions. Part of the fit I’m doing requires an imaginary error function erfi(). I know erfi(x) is real if x is real, but ROOT doesn’t have a built in erfi(), so I’m trying to construct it from the regular error function and running into some problems.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [May 19, 2022, 7:57pm UTC](https://root-forum.cern.ch/t/trying-to-define-imaginary-error-function/50032/8 "2022-05-19T19:57:16Z")

</div>

You should be able to get the [Dawson function from GSL](https://www.gnu.org/software/gsl/doc/html/specfunc.html#dawson-function) ([which can then be used to calculate erfi](https://en.wikipedia.org/wiki/Error_function#Imaginary_error_function)).  
Try:

```auto
root [0] #include "gsl/gsl_sf.h"
root [1] std::cout << gsl_sf_dawson(1.) << std::endl;

```

---

<div class="post-metadata">

**Author:** ![analyce](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/analyce/32/29734_2.png) [@analyce](https://root-forum.cern.ch/u/analyce)\
**Post date:** [May 19, 2022, 8:49pm UTC](https://root-forum.cern.ch/t/trying-to-define-imaginary-error-function/50032/9 "2022-05-19T20:49:56Z")

</div>

I think this worked! Thank you so much!!

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [May 20, 2022, 8:54am UTC](https://root-forum.cern.ch/t/trying-to-define-imaginary-error-function/50032/10 "2022-05-20T08:54:01Z")

</div>

Hi,  
The template signature is not supported for the TFormula expression, for this you need to define before a typedef (such as complex\_t) to use inside TFormula.  
Then the TF1 parameters needs to be real numbers not complex ones. You would need to define two parameters to describe a complex one.

Lorenzo
