1. 简介

三对角线矩阵(Tridiagonal Matrix),结构如公式(1)所示:

aixi−1+bixi+cixx+1=di(1)

其中a1=0,cn=0。写成矩阵形式如(2):

⎡⎣⎢⎢⎢⎢⎢⎢⎢⎢⎢b1a20c1b2a3c2b3⋱⋱⋱cn−1an0bn⎤⎦⎥⎥⎥⎥⎥⎥⎥⎥⎥⎡⎣⎢⎢⎢⎢⎢⎢⎢x1x2x3⋮xn⎤⎦⎥⎥⎥⎥⎥⎥⎥=⎡⎣⎢⎢⎢⎢⎢⎢⎢d1d2d3⋮dn⎤⎦⎥⎥⎥⎥⎥⎥⎥(2)

常用的解法为Thomas algorithm,又称为The Tridiagonal matrix algorithm(TDMA). 它是一种高斯消元法的解法。分为两个阶段:向前消元(Forward Elimination)和回代(Back Substitution)。

  • 向前消元(Forward Elimination):

    c′i=⎧⎩⎨⎪⎪⎪⎪⎪⎪⎪⎪cibicibi−aic′i−1;i=1;i=2,3,…,n−1(3)
    d′i=⎧⎩⎨⎪⎪⎪⎪⎪⎪⎪⎪dibidi−aid′i−1bi−aic′i−1;i=1;i=2,3,…,n.(4)
  • 回代(Back Substitution):

    xn=d′nxi=d′i−c′ixi+1;i=n−1,n−2,…,1.(5)

2.代码

  • 维基百科提供的C语言版本:
void solve_tridiagonal_in_place_destructive(float * restrict const x, const size_t X, const float * restrict const a, const float * restrict const b, float * restrict const c)
{/*solves Ax = v where A is a tridiagonal matrix consisting of vectors a, b, cx - initially contains the input vector v, and returns the solution x. indexed from 0 to X - 1 inclusiveX - number of equations (length of vector x)a - subdiagonal (means it is the diagonal below the main diagonal), indexed from 1 to X - 1 inclusiveb - the main diagonal, indexed from 0 to X - 1 inclusivec - superdiagonal (means it is the diagonal above the main diagonal), indexed from 0 to X - 2 inclusiveNote: contents of input vector c will be modified, making this a one-time-use function (scratch space can be allocated instead for this purpose to make it reusable)Note 2: We don't check for diagonal dominance, etc.; this is not guaranteed stable*//* index variable is an unsigned integer of same size as pointer */size_t ix;c[0] = c[0] / b[0];x[0] = x[0] / b[0];/* loop from 1 to X - 1 inclusive, performing the forward sweep */for (ix = 1; ix < X; ix++) {const float m = 1.0f / (b[ix] - a[ix] * c[ix - 1]);c[ix] = c[ix] * m;x[ix] = (x[ix] - a[ix] * x[ix - 1]) * m;}/* loop from X - 2 to 0 inclusive (safely testing loop condition for an unsigned integer), to perform the back substitution */for (ix = X - 1; ix-- > 0; )x[ix] = x[ix] - c[ix] * x[ix + 1];
}
  • 本人基于Opencv的版本:
bool caltridiagonalMatrices( cv::Mat_<double> &input_a, cv::Mat_<double> &input_b, cv::Mat_<double> &input_c,cv::Mat_<double> &input_d,cv::Mat_<double> &output_x )
{/*solves Ax = v where A is a tridiagonal matrix consisting of vectors input_a, input_b, input_c, and v is a vector consisting of input_d.input_a - subdiagonal (means it is the diagonal below the main diagonal), indexed from 1 to X - 1 inclusiveinput_b - the main diagonal, indexed from 0 to X - 1 inclusiveinput_c - superdiagonal (means it is the diagonal above the main diagonal), indexed from 0 to X - 2 inclusiveinput_d - the input vector v, indexed from 0 to X - 1 inclusiveoutput_x - returns the solution x. indexed from 0 to X - 1 inclusive*//* the size of input_a is 1*n or n*1 */int rows = input_a.rows;int cols = input_a.cols;if ( ( rows == 1 && cols > rows ) || (cols == 1 && rows > cols ) ){const int count = ( rows > cols ? rows : cols ) - 1;output_x = cv::Mat_<double>::zeros(rows, cols);cv::Mat_<double> cCopy, dCopy;input_c.copyTo(cCopy);input_d.copyTo(dCopy);if ( input_b(0) != 0 ){cCopy(0) /= input_b(0);dCopy(0) /= input_b(0);}else{return false;}for ( int i=1; i < count; i++ ){double temp = input_b(i) - input_a(i) * cCopy(i-1);if ( temp == 0.0 ){return false;}cCopy(i) /= temp;dCopy(i) = ( dCopy(i) - dCopy(i-1)*input_a(i) ) / temp;}output_x(count) = dCopy(count);for ( int i=count-2; i > 0; i-- ){output_x(i) = dCopy(i) - cCopy(i)*output_x(i+1);}return true;}else{return false;}
}

参考文献:https://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm

转载于:https://www.cnblogs.com/hehehaha/p/6332241.html

Opencv 三对角线矩阵(Tridiagonal Matrix)解法之(Thomas Algorithm)相关推荐

  1. 数值计算方法--线性方程组的数值解法(3) 追赶法(Thomas),选择主元(Pivoting),求逆(Inversion)

    追赶法 追赶法适用于三对角矩阵,形如(XX000XXX000XXX000XXX000XX)\begin{pmatrix}X&X&0&0&0\\X &X& ...

  2. CUDA Samples: matrix multiplication(C = A * B)

    以下CUDA sample是分别用C++和CUDA实现的两矩阵相乘运算code即C= A*B,CUDA中包含了两种核函数的实现方法,第一种方法来自于CUDA Samples\v8.0\0_Simple ...

  3. opencv运动目标跟踪预测_浅谈多目标跟踪中的相机运动

    ©PaperWeekly 原创 · 作者|黄飘 学校|华中科技大学硕士生 研究方向|多目标跟踪 之前的文章中我介绍了 Kalman 滤波器,这个算法被广泛用于多目标跟踪任务中的行人运动模型.然而实际场 ...

  4. Python+OpenCV:对极几何(Epipolar Geometry)

    Python+OpenCV:对极几何(Epipolar Geometry) 理论 When we take an image using pin-hole camera, we loose an im ...

  5. 线性代数学习笔记7-4:马尔可夫矩阵、矩阵幂的稳态问题

    马尔可夫矩阵Markov Matrix 马尔可夫矩阵与概率的思想有关,马尔可夫矩阵满足 每个元素aij≥0a_{ij}\geq 0aij​≥0 每列元素的总和为1 有时也定义马尔可夫矩阵的每行元素总和 ...

  6. opencv历史BUG

    2020.9.20 报错: cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-j8nxabm_ ...

  7. python opencv 将一张图片无缝合成到另一张图片中

    原文地址:Seamless Cloning using OpenCV (Python , C++) 无缝合成(Seamless Cloning)是 opencv3 的新特性. 利用这个新特性,我们可以 ...

  8. 基于opencV的动态背景下运动目标检测及跟踪(修改版)

    基于openCV的动态背景下的运动目标检测 from: http://www.mianfeiwendang.com/doc/89c6692a222a84b2ced0d502/1 摘要:介绍在动态背景下 ...

  9. OpenCV注视估计Gaze Estimation的实例(附完整代码)

    OpenCV注视估计Gaze Estimation的实例 OpenCV注视估计Gaze Estimation的实例 OpenCV注视估计Gaze Estimation的实例 #include < ...

最新文章

  1. Torch not compiled with CUDA enabled
  2. CentOS 6和CentOS 7管理系统服务的区别
  3. Android--SoundPool
  4. html滚动菜单置顶,javascript改变position值实现菜单滚动至顶部后固定
  5. c语言 activemq,activemq概念介绍
  6. android中内存泄露,Android中的内存泄露
  7. 【NOIp 2015】【DFS】斗地主
  8. 中微CMS32 Keil环境搭建
  9. ubuntu18.04纯命令行安装chrome
  10. Android equal和==的区别
  11. SAP License:如何取消物料帐的激活
  12. 关于一些初级ACM竞赛题目的分析和题解(四)。
  13. Wannacry蠕虫勒索软件“永恒之蓝”3种修复方案
  14. 数学分析教程(第三版)读后感
  15. Google的愚人节
  16. 扫地机器人扫水泥地板有用吗_拖地机器人好用吗—拖地机器人的优点介绍
  17. 免费AI数据标注工具-音频标注软件
  18. OS和Linux笔记
  19. Ubuntu 下查看DNS地址
  20. AGM FPGA使用答疑

热门文章

  1. 超简单 CameraX 人脸识别效果封装
  2. 帮我写一份老年人服务的商业计划书
  3. 计算机网络OSI模型
  4. 《理论生态学》——第九周
  5. java ssm高校田径运动会成绩管理系统
  6. python邮件正文表格怎么编辑_如何使用python将excel文件的内容复制粘贴到电子邮件正文中...
  7. 虚幻5:或许将会颠覆可视化行业
  8. Java方法中的要点
  9. 2012年11月 R语言TIOBE榜单(排名28)
  10. 计算机硬件技术基础 ---第一章