多元插值矩阵方程的Python求解

2024-10-03 13:22:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在努力克服一项艰巨的任务,这项任务涉及从方程中插值点(基本上是极值和x截距)。在Python中执行此操作存在多个问题。如果我是正确的,坐标的数目必须是一个完全平方(对于V = np.polynomial.polynomial.polyvander2d(xcoo,ycoo,[s,s])),其中s是(间接)输出的方程的度数,V是Vandermonde矩阵。但即使满足上述条件,我也会得到以下错误:

Traceback (most recent call last): File "", line 1, in multivariate([1,0,-1],[3,2,0],0,0,0) File "/Users/user/Desktop/Proj/Interpolation.py", line 512, in multivariate c = np.linalg.solve(V,zcoo) File "/Users/user/anaconda/lib/python3.5/site-packages/numpy/linalg/linalg.py", line 384, in solve r = gufunc(a, b, signature=signature, extobj=extobj) TypeError: No loop matching the specified signature and casting was found for ufunc solve1

我真的不知道这意味着什么。 即使我得到了c的值,我也需要用符号形式的范德蒙矩阵的方程乘以它们,然后从数组中提取出这个方程,因为我需要在之后操作表达式。在

这是我的代码:

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)时,我得到了以下长矩阵:

^{pr2}$

当我做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.]]

Tags: innpcoordselmultivariatesubsprintappend