Ping Zhang
Ping Zhang

Reputation: 53

How can I calculate exponents about negative values in python?

a = np.array([-40, -20, -30])

I want to calculate exponents about a.

My code is a**0.5

but I got the result.

array([nan, nan, nan])

I want to get results of complex numbers.

Upvotes: 2

Views: 114

Answers (2)

hpaulj
hpaulj

Reputation: 231385

In [322]: a = np.array([-40, -20, -30])
In [323]: a**.5
<ipython-input-323-8f2154f01f45>:1: RuntimeWarning: invalid value encountered in power
  a**.5
Out[323]: array([nan, nan, nan])

But if the array is complex dtype, it's happy to produce a complex result:

In [325]: a = np.array([-40, -20, -30], dtype=complex)
In [326]: a**.5
Out[326]: array([0.+6.32455532j, 0.+4.47213595j, 0.+5.47722558j])

Upvotes: 3

Nima S
Nima S

Reputation: 203

import numpy as np
np.lib.scimath.sqrt(my_array)

Upvotes: 3

Related Questions