如何将以下 MATLAB代码转换为 Python?这是我的解决方案,但它不会产生相同的结果。例如,f
在 MATLAB 代码中似乎总是正值,但在我的 Python 代码中,f
也得到负值。
任何想法如何修复程序?
大多数情况下,我关心这些:
MATLAB:
for k = 1 : nx
j = k+2;
Python:
for k in range(1,nx+1):
j = k+2
MATLAB:
[V,D] = eig(A, B);
DD = diag(D);
keep_idxs = find( ~isinf(DD) );
D = diag( DD(keep_idxs) );
V = V(:, keep_idxs);
[lambda, idx] = min(diag(D));
f = V(:,idx);
Python:
w,vr = scipy.linalg.decomp.eig(A,B)
w = w.real
vr = vr.real
w = w[2:-1-2]
lambda_ = w.min()
idx = w.argmin()
f = vr[:,idx]
MATLAB:
f = f(3:end-2);
[nf, nf_idx] = max(abs(f)); % L_infty norm
n2 = f(nf_idx); % normalize sign away, too
f = f ./ n2;
Python:
f = f[2:-1-1]
nf = max(np.absolute(f))
nf_idx = np.absolute(f).argmax()
nf_idx = np.ma.argmax(f)
n2 = f[nf_idx]
f = f/n2
MATLAB:
xx = -kappa:h:kappa;
Python:
xx = np.arange(-kappa, kappa+h, h)
那些是等价的吗?如果是,那么为什么它们不产生完全相同的结果呢?