1

I was getting this error:

> float() argument must be a string or a number

So, why does this happen?(I tried commands like np.asarray() but it keeps failing).

mp.mpc(cmath.rect(a,b)))

Dadep
  • 2,796
  • 5
  • 27
  • 40
  • What happens if you use np.array() instead of np.asarray()? It may have something to do with how numpy vectorizes computations, just a guess though. –  Jun 04 '17 at 01:07
  • It happens the same erros. I think commas must influence in it some way. But they both return the same values when i ask for any specific position like array[1] (or at least visually)) –  Jun 04 '17 at 01:11
  • I can't reproduce the error using the `a_list` shown in your question. Could you edit your question to include the full traceback, rather than just the last line? – ali_m Jun 04 '17 at 12:03
  • I changed my mind, I guess the error is in the format of the numbers inside the list. But I still don't know the solution –  Jun 04 '17 at 13:15
  • I have a similar problem but since you did not provide smallest example that reproduces the error I cannot figure out what to do in my case. – Valentyn Nov 29 '21 at 19:39

1 Answers1

1

The items in raizes are actually mpmath.mpc instances rather than native Python complex floats. numpy doesn't know how to deal with mpmath types, hence the TypeError.

You didn't mention mpmath at all in your original question. The problem would still have been easy to diagnose if you had posted the full traceback, rather than cutting off the most important part at the end:

In [10]: np.roots(Q)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-f3a270c7e8c0> in <module>()
----> 1 np.roots(Q)

/home/alistair/.venvs/mpmath/lib/python3.6/site-packages/numpy/lib/polynomial.py in roots(p)
    220     # casting: if incoming array isn't floating point, make it floating point.
    221     if not issubclass(p.dtype.type, (NX.floating, NX.complexfloating)):
--> 222         p = p.astype(float)
    223
    224     N = len(p)

TypeError: float() argument must be a string or a number, not 'mpc'

Whenever you ask for help with debugging on this site, please always post the whole traceback rather than just (part of) the last line - it contains a lot of information that can be helpful for diagnosing the problem.


The solution is simple enough - just don't convert the native Python complex floats returned by cmath.rect to mpmath.mpc complex floats:

raizes = []
for i in range(2*n):
   a, f = cmath.polar(l[i])
   if((f>np.pi/2) or (f<-np.pi/2)):
        raizes.append(cmath.rect(a*r,f))

Q = np.poly(raizes)

print(np.roots(Q))

# [-0.35372430 +1.08865146e+00j -0.92606224 +6.72823602e-01j
#  -0.35372430 -1.08865146e+00j -1.14467588 -9.11902316e-16j
#  -0.92606224 -6.72823602e-01j]
ali_m
  • 71,714
  • 23
  • 223
  • 298
  • Is there a simple way to reduce the decimal numbers to only two? –  Jun 04 '17 at 17:11
  • Do you just want to display only the first two decimal places and retain the full precision, or do you want to round the values in the array in a way that affects subsequent calculations? – ali_m Jun 04 '17 at 17:16
  • I would like to round them –  Jun 04 '17 at 17:20
  • Then use [`numpy.round(values, decimals=2)`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.round_.html) – ali_m Jun 04 '17 at 17:22
  • It didn't work very well. I got lot of zeros or nines after the two decimals and some number at the end. Could I cut them off? –  Jun 04 '17 at 17:33
  • My guess is that you're using the rounded values in subsequent calculations, in which case the final result is still likely to end up with digits after the second decimal place due to floating point rounding errors. The easiest solution would be to only round the final result. – ali_m Jun 04 '17 at 17:58