'float'对象没有属性'sin' [英] 'Float' object has no attribute 'sin'

查看:100
本文介绍了'float'对象没有属性'sin'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我要打印矩阵时,它会显示一条错误消息,指出浮点"对象没有属性"sin".

When I go to print my matrix, it prints an error that says 'Float' object has no attribute 'sin'.

在Python中进行编码时,我尝试使用sym.matrix以符号形式打印矩阵,但我在数字上定义了矩阵.当我去打印矩阵时,它会显示一个错误,提示"Float"对象没有属性"sin".看来由于某种原因,我的sin函数无法正确读取或输出.

While coding in Python, I am attempting to print a matrix in symbolic form using sym.matrix but I am defining my matrix numerically. When I go to print my matrix, it prints an error that says 'Float' object has no attribute 'sin'. It appears that for some reason, my sin function is not being read or outputted properly.


import numpy as np
from scipy.integrate import quad
from numpy import sin, cos, pi

N = 5 

L = 1

r_e = 1.4

mu = 916

def Phi_V(x,n,r):
    return (2/L)**(1/2) * sin(n*np.pi*x/L +n*np.pi/2) * 4.7 * (1-np.exp(-x))**2 * (2/L)**(1/2) * sin(r*np.pi*x/L +r*np.pi/2)
def V_Func(n,r,j):
    return quad(Phi_V, -L/2, L/2, args = (n,r))[0] + (j*j+1)/(2*mu*r_e**2)

V = sym.Matrix(N,N, lambda n,r: V_Func(n + 1 ,r + 1,10))

V

我收到此错误消息:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-38-8624393320b4> in <module>()
     16     return quad(Phi_V, -L/2, L/2, args = (n,r))[0] + (j*j+1)/(2*mu*r_e**2)
     17 
---> 18 V = sym.Matrix(N,N, lambda n,r: V_Func(n + 1 ,r + 1,10))
     19 
     20 V

/anaconda3/lib/python3.7/site-packages/sympy/matrices/dense.py in __new__(cls, *args, **kwargs)
    418 class MutableDenseMatrix(DenseMatrix, MatrixBase):
    419     def __new__(cls, *args, **kwargs):
--> 420         return cls._new(*args, **kwargs)
    421 
    422     @classmethod

/anaconda3/lib/python3.7/site-packages/sympy/matrices/dense.py in _new(cls, *args, **kwargs)
    430             rows, cols, flat_list = args
    431         else:
--> 432             rows, cols, flat_list = cls._handle_creation_inputs(*args, **kwargs)
    433             flat_list = list(flat_list) # create a shallow copy
    434         self = object.__new__(cls)

/anaconda3/lib/python3.7/site-packages/sympy/matrices/matrices.py in _handle_creation_inputs(cls, *args, **kwargs)
   2111                     flat_list.extend(
   2112                         [cls._sympify(op(cls._sympify(i), cls._sympify(j)))
-> 2113                          for j in range(cols)])
   2114 
   2115             # Matrix(2, 2, [1, 2, 3, 4])

/anaconda3/lib/python3.7/site-packages/sympy/matrices/matrices.py in <listcomp>(.0)
   2111                     flat_list.extend(
   2112                         [cls._sympify(op(cls._sympify(i), cls._sympify(j)))
-> 2113                          for j in range(cols)])
   2114 
   2115             # Matrix(2, 2, [1, 2, 3, 4])

<ipython-input-38-8624393320b4> in <lambda>(n, r)
     16     return quad(Phi_V, -L/2, L/2, args = (n,r))[0] + (j*j+1)/(2*mu*r_e**2)
     17 
---> 18 V = sym.Matrix(N,N, lambda n,r: V_Func(n + 1 ,r + 1,10))
     19 
     20 V

<ipython-input-38-8624393320b4> in V_Func(n, r, j)
     14     return (2/L)**(1/2) * sin(n*np.pi*x/L +n*np.pi/2) * 4.7 * (1-np.exp(-x))**2 * (2/L)**(1/2) * sin(r*np.pi*x/L +r*np.pi/2)
     15 def V_Func(n,r,j):
---> 16     return quad(Phi_V, -L/2, L/2, args = (n,r))[0] + (j*j+1)/(2*mu*r_e**2)
     17 
     18 V = sym.Matrix(N,N, lambda n,r: V_Func(n + 1 ,r + 1,10))

/anaconda3/lib/python3.7/site-packages/scipy/integrate/quadpack.py in quad(func, a, b, args, full_output, epsabs, epsrel, limit, points, weight, wvar, wopts, maxp1, limlst)
    339     if weight is None:
    340         retval = _quad(func, a, b, args, full_output, epsabs, epsrel, limit,
--> 341                        points)
    342     else:
    343         retval = _quad_weight(func, a, b, args, full_output, epsabs, epsrel,

/anaconda3/lib/python3.7/site-packages/scipy/integrate/quadpack.py in _quad(func, a, b, args, full_output, epsabs, epsrel, limit, points)
    446     if points is None:
    447         if infbounds == 0:
--> 448             return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
    449         else:
    450             return _quadpack._qagie(func,bound,infbounds,args,full_output,epsabs,epsrel,limit)

<ipython-input-38-8624393320b4> in Phi_V(x, n, r)
     12 
     13 def Phi_V(x,n,r):
---> 14     return (2/L)**(1/2) * sin(n*np.pi*x/L +n*np.pi/2) * 4.7 * (1-np.exp(-x))**2 * (2/L)**(1/2) * sin(r*np.pi*x/L +r*np.pi/2)
     15 def V_Func(n,r,j):
     16     return quad(Phi_V, -L/2, L/2, args = (n,r))[0] + (j*j+1)/(2*mu*r_e**2)

AttributeError: 'Float' object has no attribute 'sin'

推荐答案

如果从混合对象(例如数字,字符串或符号)中创建一个numpy数组,则会得到一个对象dtype数组.

If you make a numpy array from mixed objects, such as numbers, strings or symbols, you get an object dtype array.

np.sin(arr)是通过调用[x.sin() for x in arr]执行的,即它将任务委托给每个元素的sin方法.大多数对象(包括浮点数)都没有这种方法,因此会失败.其他numpy功能(例如expsqrt)也是如此.可以委派一些东西,例如加法.

np.sin(arr) on such an array is performed by calling [x.sin() for x in arr], i.e. it delegates the task to the sin method of each element. Most objects, including floats, don't have such a method, so this fails. The same applies to other numpy functions such as exp and sqrt. Somethings delegate just fine, such as addition.

使用sympy的唯一指示是sym.Matrix调用.我不明白您打算怎么做.您期望什么样的符号形式? sympy不会到达" scipy/numpy代码并将其转换为符号.充其量,它将尝试通过将符号传递给numpy代码来评估代码,从而导致此错误.

The only indication of your using sympy is the sym.Matrix call. I don't understand what you are trying to do with that. What kind of symbolic form are you expecting? sympy isn't going to 'reach' into the scipy/numpy code and convert it to symbols. At best it will try to evaluate the code with by passing symbols to the numpy code, resulting in this error.

将sympy和numpy混合起来很棘手,而且常常不是错的.

Mixing sympy and numpy is tricky, and more often than not wrong.

为什么sympy lambdify函数无法识别numpy sum函数和乘法函数

是尝试同时使用sympy和numpy的另一个示例.我的回答漫长而又令人费解,但是我以那种方式离开了,以使人们感觉到同时使用这两个软件包是多么棘手.

is another example of trying to use sympy and numpy together. My answer is long and and convoluted, but I left it that way to give a sense of how tricky it is to use the two packages together.

isympy会话中:

In [8]: Matrix                                                                  
Out[8]: sympy.matrices.dense.MutableDenseMatrix

In [9]: Matrix(3,3, lambda n,r: n+r)                                            
Out[9]: 
⎡0  1  2⎤
⎢       ⎥
⎢1  2  3⎥
⎢       ⎥
⎣2  3  4⎦

Matrix通过将索引传递给函数来创建一个sympy对象.我们可以通过以下方式更清楚地看到这一点:

Matrix makes a sympy object by passing the indices to the function. We see this more clearly with:

In [10]: def foo(n,r): 
    ...:     print(n,r, type(n), type(r)) 
    ...:     return n+r 
    ...:                                                                        

In [11]: Matrix(3,3,foo)                                                        
0 0 <class 'sympy.core.numbers.Zero'> <class 'sympy.core.numbers.Zero'>
0 1 <class 'sympy.core.numbers.Zero'> <class 'sympy.core.numbers.One'>
...
Out[11]: 
⎡0  1  2⎤
⎢       ⎥
⎢1  2  3⎥
⎢       ⎥
⎣2  3  4⎦

如果我们尝试在此功能中使用np.sin:

If we try to use np.sin in this function:

In [15]: def foo(n,r): 
    ...:     return np.sin(r) 
    ...:                                                                        

In [16]: Matrix(3,3,foo)                                                        
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
AttributeError: 'Zero' object has no attribute 'sin'
...

如上所述,尝试将非数字值用作np.sin参数会导致委托给sin方法时出错.

As I explained above, trying to use nonumeric values as np.sin arguments results in the error in delegation to a sin method.

您的代码通过将值传递到scipy.quad进一步使事情复杂化,该值又将积分变量xnr传递给Phi_V.

Your code further complicates things by passing the values through the scipy.quad, which in turn passes the integration variable x plus n and r to Phi_V.

仍在isympy会话中,

In [26]: L = 1 
    ...:  
    ...: r_e = 1.4 
    ...:  
    ...: mu = 916                                                               

使用@Ian建议的功能,并使用sinsympy版本,等等:

Using the function that @Ian suggests, using the sympy versions of sin, etc:

In [27]: def foo(n,r): 
    ...:     return ((2/L)**(1/2)) * sin(n * pi * (x/L) + n * pi/2) * 4.7 * (1 -
    ...:  exp(-x))**2 * (2/L)**(1/2) * sin(r * pi * x/L + r * pi/2) 
    ...:                                                                        

In [28]: foo(1,2)                                                               
Out[28]: 
              2                    
     ⎛     -x⎞                     
-9.4⋅⎝1 - ℯ  ⎠ ⋅sin(2⋅π⋅x)⋅cos(π⋅x)

In [29]: Matrix(3,3,foo)                                                        
Out[29]: 
⎡0                   0                                    0                 ⎤
⎢                                                                           ⎥
⎢                     2                                2                    ⎥
⎢            ⎛     -x⎞     2                  ⎛     -x⎞                     ⎥
⎢0       9.4⋅⎝1 - ℯ  ⎠ ⋅cos (π⋅x)        -9.4⋅⎝1 - ℯ  ⎠ ⋅sin(2⋅π⋅x)⋅cos(π⋅x)⎥
⎢                                                                           ⎥
⎢                 2                                       2                 ⎥
⎢        ⎛     -x⎞                               ⎛     -x⎞     2            ⎥
⎣0  -9.4⋅⎝1 - ℯ  ⎠ ⋅sin(2⋅π⋅x)⋅cos(π⋅x)      9.4⋅⎝1 - ℯ  ⎠ ⋅sin (2⋅π⋅x)     ⎦

但这不会显示有关quad集成的任何内容.也不能将其用作quad中的目标函数.

But this doesn't display anything about the quad integration. Nor can it be used as the objective function in quad.

这篇关于'float'对象没有属性'sin'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆