mahmoud fathy - 1 year ago 88

R Question

I am getting an equation with this form

`exp(az) = 1 + cz`

which I wanna solve for z,

where c is a complex number, so expectedly z is complex also.

I cannot figure out how to solve an equation involving complex number in R.

I hope anybody can help me

Answer Source

Wolfram Alpha can get a solution in terms of the *Lambert W* function (which it calls the `ProductLog`

function):

The `emdbook`

package (among others) has an implementation of the Lambert W function. (In order to make this work I had to fix a bug, so you can't use the CRAN implementation. Instead, install the latest version from Github: `library(devtools); install_github("bbolker/emdbook")`

...

```
library(emdbook)
sfun <- function(a,c) {
w <- lambertW(-a/c*exp(-a/c))
-(c*w+a)/(a*c)
}
```

Example

```
a <- 2+1i; c <- 1+1i
(z <- sfun(a,c))
## [1] -0.1686391-0.2337278i
```

Check answer:

```
(exp(a*z)-(1+c*z))
## [1] 0+5.551115e-17i
```

This is zero to within expected numeric tolerance ...