我在MATLAB中找到了thomas算法或TDMA的實現。三對角矩陣算法(TDMA)又名托馬斯算法,在NumPy數組中使用Python
function x = TDMAsolver(a,b,c,d)
%a, b, c are the column vectors for the compressed tridiagonal matrix, d is the right vector
n = length(b); % n is the number of rows
% Modify the first-row coefficients
c(1) = c(1)/b(1); % Division by zero risk.
d(1) = d(1)/b(1); % Division by zero would imply a singular matrix.
for i = 2:n-1
temp = b(i) - a(i) * c(i-1);
c(i) = c(i)/temp;
d(i) = (d(i) - a(i) * d(i-1))/temp;
end
d(n) = (d(n) - a(n) * d(n-1))/(b(n) - a(n) * c(n-1));
% Now back substitute.
x(n) = d(n);
for i = n-1:-1:1
x(i) = d(i) - c(i) * x(i + 1);
end
end
我需要它在python中使用numpy數組,這裏我第一次嘗試在python中的算法。
import numpy
aa = (0.,8.,9.,3.,4.)
bb = (4.,5.,9.,4.,7.)
cc = (9.,4.,5.,7.,0.)
dd = (8.,4.,5.,9.,6.)
ary = numpy.array
a = ary(aa)
b = ary(bb)
c = ary(cc)
d = ary(dd)
n = len(b)## n is the number of rows
## Modify the first-row coefficients
c[0] = c[0]/ b[0] ## risk of Division by zero.
d[0] = d[0]/ b[0]
for i in range(1,n,1):
temp = b[i] - a[i] * c[i-1]
c[i] = c[i]/temp
d[i] = (d[i] - a[i] * d[i-1])/temp
d[-1] = (d[-1] - a[-1] * d[-2])/(b[-1] - a[-1] * c[-2])
## Now back substitute.
x = numpy.zeros(5)
x[-1] = d[-1]
for i in range(-2, -n-1, -1):
x[i] = d[i] - c[i] * x[i + 1]
他們給出了不同的結果,所以我做錯了什麼?
Python的'範圍()'函數不包括最後一個索引,所以在這種情況下,'for'循環將停止在n-1 。 – voithos 2012-01-04 20:01:54
@voithos:它不包含'n',但'n-1' *是最後一個索引,因爲Python基於零的索引,而Matlab是基於一個的。 – 2012-01-04 20:05:07
啊,好點。我認爲一個關鍵的區別(我沒有意識到)是Python的'range()'沒有經過指定的末尾索引(正如你所說,這是由於Python的索引從0開始),而Matlab的'x: X'語法**不會**通過結束索引。 – voithos 2012-01-04 20:23:31