我有以下形式的多项式:
p(y) = A + By + Cy^2 ... + Dy^n
在这里,每个系数A,B,..,D
是矩阵(因此p(y)
也是矩阵)。假设我在n+1
点处插值多项式。我现在应该能够解决此系统。我正在Numpy中尝试这样做。我现在有以下代码:
a = np.vander([0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1,1.2]) #polynomial degree is 12, a -> (12x12)
b = np.random.rand(12,60,60) #p(x) is a 60x60 matrix that I have evaluated at 12 points
x = np.linalg.solve(a,b)
我收到以下错误:
ValueError: solve: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (m,m),(m,n)->(m,n) (size 60 is different from 12)
如何在Numpy中解决该系统以获得x
?有一般的数学技巧吗?
答案 0 :(得分:1)
基本上,您只需要执行3600个12d多项式回归并将这些系数组成矩阵即可。例如,组件p(y)[0,0]
就是:
p(y)[0, 0] = A[0, 0] + B[0, 0] * y + C[0, 0] * y**2 ... + D[0, 0] * y**n
问题是np.linalg.solve
只能采用一维系数。但是,由于矩阵元素都是独立的(y
是标量),因此可以ravel
进行使用,并且可以使用(m,m),(m,n**2) -> (m,n**2)
的形式进行计算,然后重塑为矩阵。因此,尝试:
a = np.vander([0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1,1.2]) #polynomial degree is 12, a -> (12x12)
b = np.random.rand(12,60,60) #p(x) is a 60x60 matrix that I have evaluated at 12 points
s = b.shape
x = np.linalg.solve(a, b.reshape(s[0], -1))
x = x.reshape(s)