0
votes

I'm trying to simulate a simple pendulum using pylot. For that, I created the class Pendulum and, in one of the methods, I keep getting the same error.

def __init__(self, L = 1, M = 1, G = 9.8, origin = (0, 0), init = [60, 0]):
    self.init_state = np.array(init, dtype = 'float')
    self.params = (L, M, G)
    self.origin = origin
    self.time = 0
    self.state = self.init_state * (np.pi/180.)

def dstate_dt(self, state, t):
    """compute the derivative of the given state"""
    (L, M, G) = self.params

    dydx = np.zero_lie(state)
    dydx[0] = state[1]
    dydx[1] = -(G/L)*np.sin(state[0])

    return dydx

The error code reads:

File "C:\Users\Lucas\Anaconda3\envs\Spyder\lib\site-packages\numpy\core\fromnumeric.py", line 47, in _wrapit result = getattr(asarray(obj), method)(*args, **kwds)

TypeError: only integer scalar arrays can be converted to a scalar index

Going into the refered line, in a folder called 'fromnumeric.pi', the given function reads:

def _wrapit(obj, method, *args, **kwds):
try:
    wrap = obj.__array_wrap__
except AttributeError:
    wrap = None
result = getattr(asarray(obj), method)(*args, **kwds)
if wrap:
    if not isinstance(result, mu.ndarray):
        result = asarray(result)
    result = wrap(result)
return result

What triggered this error and how can I fix it? The full traceback is this:

File "C:\Users\Lucas\Desktop\Estudos\Python\Simple Pendulum.py", line 86, in animate(0)

File "C:\Users\Lucas\Desktop\Estudos\Python\Simple Pendulum.py", line 80, in animate line.set_data(pendulum.position())

File "C:\Users\Lucas\Desktop\Estudos\Python\Simple Pendulum.py", line 27, in position x = np.cumsum(self.origin[0], L*np.sin(self.state[0]))

File "<array_function internals>", line 6, in cumsum

File "C:\Users\Lucas\Anaconda3\envs\Spyder\lib\site-packages\numpy\core\fromnumeric.py", line 2423, in cumsum return _wrapfunc(a, 'cumsum', axis=axis, dtype=dtype, out=out)

File "C:\Users\Lucas\Anaconda3\envs\Spyder\lib\site-packages\numpy\core\fromnumeric.py", line 58, in _wrapfunc return _wrapit(obj, method, *args, **kwds)

File "C:\Users\Lucas\Anaconda3\envs\Spyder\lib\site-packages\numpy\core\fromnumeric.py", line 47, in _wrapit result = getattr(asarray(obj), method)(*args, **kwds)

TypeError: only integer scalar arrays can be converted to a scalar index

1
There's a typo: np.zero_lie should be np.zeros_like - Darina
Full traceback please. - hpaulj
There it is, the full traceback. Also, even if it is mispelled here (and it is), I checked the actual code and there is no mispelling. - lucas belasque

1 Answers

0
votes

The traceback shows that the error occurs in this line (from your code)

File "C:\Users\Lucas\Desktop\Estudos\Python\Simple Pendulum.py", line 27, in 
position x = np.cumsum(self.origin[0], L*np.sin(self.state[0]))

I don't see that use of cumsum in your code sample! The posted methods don't appear at all in the traceback.

You pass cumsum two arguments

self.origin[0]
L*np.sin(self.state[0]

But if you take time to check the docs for cumsum you'll see:

numpy.cumsum(a, axis=None, dtype=None, out=None)[source]

It takes one array argument a. The second argument is axis, which can only be a scalar, a single number. That's the error is complaining about!

I can't suggest a fix because I don't know what you intend with the cumsum call.