python生成三对角矩阵_追赶法求解三对角矩阵(C/C++/python/matlab/Fortran 90)

追赶法求解过程

import numpy as np

## Tri Diagonal Matrix Algorithm(a.k.a Thomas algorithm) solver

def TDMAsolver(a, b, c, d):

'''

TDMA solver, a b c d can be NumPy array type or Python list type.

refer to http://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm

and to http://www.cfd-online.com/Wiki/Tridiagonal_matrix_algorithm_-_TDMA_(Thomas_algorithm)

'''

nf = len(d) # number of equations

ac, bc, cc, dc = map(np.array, (a, b, c, d)) # copy arrays

for it in range(1, nf):

mc = ac[it-1]/bc[it-1]

bc[it] = bc[it] - mc*cc[it-1]

dc[it] = dc[it] - mc*dc[it-1]

xc = bc

xc[-1] = dc[-1]/bc[-1]

for il in range(nf-2, -1, -1):

xc[il] = (dc[il]-cc[il]*xc[il+1])/bc[il]

return xc

例如:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值