我目前正试图征服一项涉及从等式中插入点(基本上是极值和x截距)的大量任务。在Python中执行此操作存在多个问题,一般而言。如果我是正确的,坐标的数量必须是一个完美的正方形(对于V = np.polynomial.polynomial.polyvander2d(xcoo,ycoo,[s,s])
,其中s是(间接)输出方程的度数,V是Vandermonde矩阵。但即使符合上述条件,我收到以下错误:
回溯(最近一次呼叫最后一次):文件"",第1行,in 多变量([1,0,-1],[3,2,0],0,0,0)文件 " /Users/user/Desktop/Proj/Interpolation.py" ;,第512行,多变量 c = np.linalg.solve(V,zcoo) File" /Users/user/anaconda/lib/python3.5/site-packages/numpy/linalg/linalg.py", 第384行,在解决 r = gufunc(a,b,signature = signature,extobj = extobj) TypeError:没有匹配指定签名和转换的循环 找到了ufunc solve1
我真的不知道这意味着什么。 即使我得到了c的值,我也需要将它们乘以符号形式的Vandermonde矩阵的方程,并以某种方式从数组中提取出方程式,因为我需要在之后操作表达式。
这是我的代码:
def multivariate(coef,exp,z,coords,t):
if t == 0:
y = Symbol('y')
a = function(coef, exp) #Equation of function using the coef and exp (i.e. function([5,4,3],[3,2,0]) outputs 5*x**3 + 4*x**2 + 3
b = function(coef, exp)
b = b.subs(x, y)
print(b)
func = a + b
r = f(coef, exp, 0) #Used to find y-intercept
z = func - r
print("z =",z)
root = []
crit3d = []
crit2d = rootsofd(coef, exp)
n = 0
g = []
k = []
while n < len(crit2d):
h = [crit2d[n]]
h.append(crit2d[n])
q = z.subs(x,h[0])
q = q.subs(y,h[1])
crit3d.append(q)
h.append(q)
g.append(h)
k.extend(g)
n = n + 1
print(k) #coords of critical values
roots2d = [roots(fvalue(coef,exp))]
roots3d = []
o = []
roots3d.extend(roots2d)
o.extend([0])
o = [o]
roots3d.extend(o)
print(roots3d)
roots3d2 = roots3d
roots3d = list(itertools.product(*roots3d))
rev3d = list(itertools.product(*reversed(roots3d2)))
roots3d.extend(rev3d)
print(roots3d)
n = 0
w = []
g = []
while n < len(roots3d):
m = list(roots3d[n])
q = z.subs(x,m[0])
q = q.subs(y,m[1])
root.append(q)
m.append(q)
g.append(m)
w.extend(g)
n = n + 1
print(root)
print(w) #coords of roots
coords = w
coords.extend(k)
print(coords) #Full x,y,z coordinates
xcoo = [el[0] for el in coords]
ycoo = [el[1] for el in coords]
zcoo = [el[2] for el in coords]
print("xcoo =", xcoo)
print("ycoo=", ycoo)
print("zcoo=", zcoo)
s = len(xcoo)
s = (s)**.5
if s - (s-1) >= .5:
s = round(s) - 1
else:
s = round(s)
V = np.polynomial.polynomial.polyvander2d(xcoo, ycoo, [s,s])
c = np.linalg.solve(V,zcoo) #This is essentially where the error occurs
poly(s+1,c)
p = np.polynomial.polynomial.polyvander2d(x,y,[s,s])
p = p*c #This will also become an issue
r = 0
o = []
while s**2 > r:
o.append(p.item(r))
r = r + 1
p = Poly(o)
我为长代码道歉;我需要展示所有内容才能完全理解这个场景。另外,我真的不知道错误的来源,比抱歉更安全。
任何建议都会对我有所帮助,因为我处于紧急状态。
编辑:
当我multivariate([5,4,-.2], [3,2,0], 0, 0 ,0)
时,我得到以下长矩阵:
[[ 1. 0. 0. 0. 0. -0.7236068
-0. -0. -0. -0. 0.5236068 0. 0.
0. 0. -0.37888544 -0. -0. -0. -0.
0.27416408 0. 0. 0. 0. ]
[ 1. 0. 0. 0. 0. -0.7236068
-0. -0. -0. -0. 0.5236068 0. 0.
0. 0. -0.37888544 -0. -0. -0. -0.
0.27416408 0. 0. 0. 0. ]
[ 1. 0. 0. 0. 0. -0.2763932
-0. -0. -0. -0. 0.0763932 0. 0.
0. 0. -0.02111456 -0. -0. -0. -0.
0.00583592 0. 0. 0. 0. ]
[ 1. 0. 0. 0. 0. -0.7236068
-0. -0. -0. -0. 0.5236068 0. 0.
0. 0. -0.37888544 -0. -0. -0. -0.
0.27416408 0. 0. 0. 0. ]
[ 1. 0. 0. 0. 0. -0.2763932
-0. -0. -0. -0. 0.0763932 0. 0.
0. 0. -0.02111456 -0. -0. -0. -0.
0.00583592 0. 0. 0. 0. ]
[ 1. 0. 0. 0. 0. 0.2 0.
0. 0. 0. 0.04 0. 0. 0.
0. 0.008 0. 0. 0. 0. 0.0016
0. 0. 0. 0. ]
[ 1. 0. 0. 0. 0. -0.7236068
-0. -0. -0. -0. 0.5236068 0. 0.
0. 0. -0.37888544 -0. -0. -0. -0.
0.27416408 0. 0. 0. 0. ]
[ 1. 0. 0. 0. 0. -0.2763932
-0. -0. -0. -0. 0.0763932 0. 0.
0. 0. -0.02111456 -0. -0. -0. -0.
0.00583592 0. 0. 0. 0. ]
[ 1. 0. 0. 0. 0. 0.2 0.
0. 0. 0. 0.04 0. 0. 0.
0. 0.008 0. 0. 0. 0. 0.0016
0. 0. 0. 0. ]
[ 1. -0.7236068 0.5236068 -0.37888544 0.27416408 0. -0.
0. -0. 0. 0. -0. 0. -0.
0. 0. -0. 0. -0. 0. 0.
-0. 0. -0. 0. ]
[ 1. 0. 0. 0. 0. -0.7236068
-0. -0. -0. -0. 0.5236068 0. 0.
0. 0. -0.37888544 -0. -0. -0. -0.
0.27416408 0. 0. 0. 0. ]
[ 1. 0. 0. 0. 0. -0.2763932
-0. -0. -0. -0. 0.0763932 0. 0.
0. 0. -0.02111456 -0. -0. -0. -0.
0.00583592 0. 0. 0. 0. ]
[ 1. 0. 0. 0. 0. 0.2 0.
0. 0. 0. 0.04 0. 0. 0.
0. 0.008 0. 0. 0. 0. 0.0016
0. 0. 0. 0. ]
[ 1. -0.7236068 0.5236068 -0.37888544 0.27416408 0. -0.
0. -0. 0. 0. -0. 0. -0.
0. 0. -0. 0. -0. 0. 0.
-0. 0. -0. 0. ]
[ 1. -0.2763932 0.0763932 -0.02111456 0.00583592 0. -0.
0. -0. 0. 0. -0. 0. -0.
0. 0. -0. 0. -0. 0. 0.
-0. 0. -0. 0. ]
[ 1. 0. 0. 0. 0. -0.7236068
-0. -0. -0. -0. 0.5236068 0. 0.
0. 0. -0.37888544 -0. -0. -0. -0.
0.27416408 0. 0. 0. 0. ]
[ 1. 0. 0. 0. 0. -0.2763932
-0. -0. -0. -0. 0.0763932 0. 0.
0. 0. -0.02111456 -0. -0. -0. -0.
0.00583592 0. 0. 0. 0. ]
[ 1. 0. 0. 0. 0. 0.2 0.
0. 0. 0. 0.04 0. 0. 0.
0. 0.008 0. 0. 0. 0. 0.0016
0. 0. 0. 0. ]
[ 1. -0.7236068 0.5236068 -0.37888544 0.27416408 0. -0.
0. -0. 0. 0. -0. 0. -0.
0. 0. -0. 0. -0. 0. 0.
-0. 0. -0. 0. ]
[ 1. -0.2763932 0.0763932 -0.02111456 0.00583592 0. -0.
0. -0. 0. 0. -0. 0. -0.
0. 0. -0. 0. -0. 0. 0.
-0. 0. -0. 0. ]
[ 1. 0.2 0.04 0.008 0.0016 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. ]
[ 1. -0.53333333 0.28444444 -0.1517037 0.08090864 -0.53333333
0.28444444 -0.1517037 0.08090864 -0.04315128 0.28444444 -0.1517037
0.08090864 -0.04315128 0.02301401 -0.1517037 0.08090864 -0.04315128
0.02301401 -0.01227414 0.08090864 -0.04315128 0.02301401 -0.01227414
0.00654621]
[ 1. -0.53333333 0.28444444 -0.1517037 0.08090864 -0.53333333
0.28444444 -0.1517037 0.08090864 -0.04315128 0.28444444 -0.1517037
0.08090864 -0.04315128 0.02301401 -0.1517037 0.08090864 -0.04315128
0.02301401 -0.01227414 0.08090864 -0.04315128 0.02301401 -0.01227414
0.00654621]
[ 1. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. ]]
当我做multivariate([1,0,-1],[3,2,0],0,0,0)
时,其中n是坐标,其中n是一个完美的正方形,我得到以下矩阵(这是上述错误的起源):
[[ 1. 0. 1. 0.]
[ 1. 0. 1. 0.]
[ 1. 1. 0. 0.]
[ 1. 0. 0. 0.]]