我试图从 MATLAB 中重现 Python 中的结果。但是,我似乎无法正确处理。这是正确的 MATLAB 代码:
nx = 5;
ny = 7;
x = linspace(0, 1, nx); dx = x(2) - x(1);
y = linspace(0, 1, ny); dy = y(2) - y(1);
onex = ones(nx, 1);
oney = ones(ny, 1);
Dx = spdiags([onex -2*onex onex], [-1 0 1], nx, nx);
Dy = spdiags([oney -2*oney oney], [-1 0 1], ny, ny);
Ix = eye(nx); Iy = eye(ny);
L = kron(Iy, Dx);
size(L) % 35 35
现在,这是 Python 代码:
nx = 5
ny = 7
x = linspace(0, 1, nx); dx = x[1] - x[0]
y = linspace(0, 1, ny); dy = y[1] - y[0]
onex = ones(nx)
oney = ones(ny)
Dx = sparse.dia_matrix( ([onex, -2*onex, onex], [-1,0,1] ), shape=(nx,nx))
Dy = sparse.dia_matrix( ([oney, -2*oney, oney], [-1,0,1] ), shape=(ny,ny))
Ix = eye(nx)
Iy = eye(ny)
L = kron(Iy, Dx)
L.shape # (7, 7)
据我所知,在定义 L 之前,一切都是正确的。根据kron(Iy, Dx)
MATLAB(应该是 kronecker 乘积)应该产生一个 35X35 矩阵,但 Python 认为它应该是一个 7X7 矩阵。在更简单的计算中,两者都给出了正确的答案:
Python:
kron(array(([1,2],[2,3])), [1,2])
array([[1, 2, 2, 4],
[2, 4, 3, 6]])
MATLAB
kron([1 2; 2 3], [1 2])
ans = 1 2 2 4
2 4 3 6
为什么我会得到不同的结果?
谢谢!