Search code examples
pythonnumpyvalueerror

What to use instead of * and / in Python to avoid ValueError?


I'm having a similar issue as to this post:

Why does this simple numpy multiply operation raise an "invalid number of arguments" error?

I have this incredibly confusing equation in my code:

(m[0]*np.power(q[0])*q[0]*r[0]*(m[0]*R)*np.power(q[0]))/(R*((m[0]*R)*np.power(q[0])+m*np.power(q[0]))**2) - a[0]*N/(b[0]+N)

and when I run it, it returns the error:

ValueError: invalid number of arguments

I assumed it could have something to do with the ** I was using to define the exponents (q[0] is an exponent), so I substituted those to np.power() to no success. Also, I changed the / that I am using to define the fractions to *(.....)**(-1), but that didn't work either. At this point, I'm assuming the issue is with the * I'm using for multiplications. But how can I write multiplications and divisions in this long expression without raising a position error?

Thank you in advance for the help!


Solution

  • You have to add another argument of what power you want.
    For example, power means add 2 For example, cube means add 3

    np.power(x1, 2)
    np.power(x1, 3)
    

    If you just want to squre with np.power(), then I have already edited the code for you:

    (m[0]*np.power(q[0], 2)*q[0]*r[0]*(m[0]*R)*np.power(q[0], 2))/(R*((m[0]*R)*np.power(q[0], 2)+m*np.power(q[0], 2))**2) - a[0]*N/(b[0]+N)