Errors while converting np.array to decimal in Python

半腔热情 提交于 2019-12-12 02:14:09

问题


This is a continuation of the question asked here: Exponential of large negative numbers. The reason I want to use decimal is that np.exp(-large_numbers) return 0.0. Using Eric's answer to this question: https://stackoverflow.com/a/7771210/6943476, I tried converting my np.array to decimal but I still get the data-type
numpy.ndarray.

Coding ability: Beginner

My final goal is to have a function return a damped sinusoid:

def waveform(mass_sm, amplitude, F, Q, phi, Time):
    w = F/mass_sm

    print type(Time)
    Ntime = np.array(Time,dtype=np.dtype(decimal.Decimal))
    priny type(Ntime)

    return mass_sm * amplitude * np.sin(w * Ntime + phi) * np.exp(-w * Ntime/ (2 * Q))

It tried to debug the code by breaking down the return function into the sine, and then the exponential:

def waveform(mass_sm, amplitude, F, Q, phi, Time):
    w = F/mass_sm

    print type(Time)
    Ntime = np.array(Time,dtype=np.dtype(decimal.Decimal))
    priny type(Ntime)

    return mass_sm * amplitude * np.sin(w * Ntime + phi)

Output:

<type 'numpy.ndarray'>
<type 'numpy.ndarray'>
AttributeError: 'float' object has no attribute 'sin'

For the exponential part, I get the (expected) error that:

AttributeError: 'float' object has no attribute 'exp'

I have also tried to convert the elements of my array to decimal by doing this:

    Ntime =  [decimal.Decimal(i) for i in Time]

This time, getting this error for sine:

TypeError: can't multiply sequence by non-int of type 'float'

and this error for exp:

TypeError: bad operand type for unary -: 'list'

来源:https://stackoverflow.com/questions/43100725/errors-while-converting-np-array-to-decimal-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!