简体   繁体   English

类型错误:无法将表达式转换为浮点数,使用符号 x,多项式插值

[英]TypeError: can't convert expression to float, with symbolic x, polynomial interpolation

I'm trying to run the following code but an error appears at the line with S[i][0] .我正在尝试运行以下代码,但在S[i][0]行中出现错误。

TypeError: can't convert expression to float类型错误:无法将表达式转换为浮点数

I'm am sure that my variables D[][] , h[] , age[] , A[] , B[] and n are ok (they are floats).我确信我的变量D[][]h[]age[]A[]B[]n (它们是浮点数)。 I think it has something to do with the symbolic figure x .我认为这与象征性的数字x

At the end, S is supposed to be a (7,1) vector of polynomes.最后, S应该是多项式的 (7,1) 向量。

import numpy as np
import sympy as sp
age = np.arange(15,55,5)  
N = np.array([0, 7.442, 26.703, 41.635, 49.785, 50.209, 50.226])
n = len(N)

h = np.zeros(n)
for i in range(n) :
    h[i] = age[i+1] - age[i]

mu = np.zeros(n)
lamda = np.zeros(n)
F = np.zeros((n,1))
A = np.zeros(n)
B = np.zeros(n)

for i in range(n) :
    mu[i] = h[i-1]/(h[i]+h[i-1])
    lamda[i] = h[i]/(h[i]+h[i-1])
    F[i][0] = (6*(N[i]*h[i-1] - h[i]*N[i-1]))/((h[i]+h[i-1])*h[i]*h[i-1])   

M = 2*np.eye(n,n) + 0.5*np.diag(np.ones((n-1)),1)+ 0.5*np.diag(np.ones((n-1)),1).T

D = np.dot(np.linalg.inv(M),F)

for i in range(n-1) :
    A[i] = (N[i+1] - N[i])/h[i] - (h[i]*(D[i+1][0] - D[i][0]))/6
    B[i] = N[i] - (((h[i])**2)*D[i][0])/6

def s(D, h, age, A, B, n) :
    S = np.zeros((n,1))
    x = sp.Symbol("x")
    for i in range(n) :
        S[i][0] = (D[i+1][0]*((x - age[i])**3))/(6*h[i]) - (D[i][0]*((x - age[i+1])**3))/(6*h[i]) + (A[i])*(x - age[i]) + B[i]
    return sp.simplify(S)


S = s(D, h, age, A, B, n)

The problem is that while most of your variables are floats, x is symbolic, which causes all of those calculations in the end to be a symbolic object.问题是,虽然你的大部分变量都是浮点数,但x是符号的,这导致所有这些计算最终都是一个符号对象。 Such objects are handled at python-level with classes, while numpy requires numbers, so it tries to apply float() to it.这些对象在 python 级别用类处理,而 numpy 需要数字,因此它尝试将float()应用于它。 For example,例如,

>> float(sp.Add(1, 1))
2.0

>> float(s.Add(1 + x))
Traceback (most recent call last):
  File "<pyshell#85>", line 1, in <module>
    float(sp.Add(1, x))
  File "C:\Users\ip507\AppData\Local\Programs\Python\Python36\lib\site-packages\sympy\core\expr.py", line 239, in __float__
    raise TypeError("can't convert expression to float")
TypeError: can't convert expression to float

Since you don't intend to use an calcuations with the "array-like" S object, it can just be a list, where you collect the expressions.由于您不打算对“类似数组”的S对象使用计算,因此它可以只是一个列表,您可以在其中收集表达式。

def s(D, h, age, A, B, n):
    x = sp.Symbol("x")
    S = []
    for i in range(n):
        S.append((D[i+1][0]*((x - age[i])**3))/(6*h[i]) - (D[i][0]*((x - age[i+1])**3))/(6*h[i]) + (A[i])*(x - age[i]) + B[i])
    return S

Having said that, you have an error which causes an IndexError .话虽如此,您有一个错误导致IndexError D has a shape of (7, 1), but you call D[i+1] . D的形状为 (7, 1),但您调用D[i+1] It's something you'll need to address since I don't know your intentions.这是你需要解决的问题,因为我不知道你的意图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:无法将表达式转换为浮点型 - TypeError: can't convert expression to float TypeError:无法将表达式转换为使用Sympy浮动 - TypeError: can't convert expression to float with Sympy sympy TypeError:无法将表达式转换为浮点数 - sympy TypeError: can't convert expression to float Sympy:TypeError:无法将表达式转换为浮点型 - Sympy: TypeError: can't convert expression to float TypeError:无法舍入符号表达式 - TypeError: can't round symbolic expression Python:sympy TypeError:无法将表达式转换为浮点数 - Python : sympy TypeError: can't convert expression to float TypeError:无法将表达式转换为浮点数 - 定积分 sympy - TypeError: can't convert expression to float - definite integral sympy odeint -(TypeError: can't convert expression to float ) - 将表达式导入 function 以执行 odeint - odeint -(TypeError: can't convert expression to float ) - Importing expression into a function to perform odeint 非线性方程求解Sympy Python用于液压 - 需要解决TypeError(“无法将表达式转换为浮点数”) - Non-linear equation solving Sympy Python for hydraulics - Need resolve TypeError(“can't convert expression to float”) 如何做自然日志的派生类型错误:无法将表达式转换为浮点数 - How to do a derivative of a natural log getting TypeError: can't convert expression to float
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM