Opencv 三对角线矩阵(Tridiagonal Matrix)解法之(Thomas Algorithm)
1. 简介
三对角线矩阵(Tridiagonal Matrix),结构如公式(1)所示:
其中a1=0,cn=0。写成矩阵形式如(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)相关推荐
- 数值计算方法--线性方程组的数值解法(3) 追赶法(Thomas),选择主元(Pivoting),求逆(Inversion)
追赶法 追赶法适用于三对角矩阵,形如(XX000XXX000XXX000XXX000XX)\begin{pmatrix}X&X&0&0&0\\X &X& ...
- CUDA Samples: matrix multiplication(C = A * B)
以下CUDA sample是分别用C++和CUDA实现的两矩阵相乘运算code即C= A*B,CUDA中包含了两种核函数的实现方法,第一种方法来自于CUDA Samples\v8.0\0_Simple ...
- opencv运动目标跟踪预测_浅谈多目标跟踪中的相机运动
©PaperWeekly 原创 · 作者|黄飘 学校|华中科技大学硕士生 研究方向|多目标跟踪 之前的文章中我介绍了 Kalman 滤波器,这个算法被广泛用于多目标跟踪任务中的行人运动模型.然而实际场 ...
- Python+OpenCV:对极几何(Epipolar Geometry)
Python+OpenCV:对极几何(Epipolar Geometry) 理论 When we take an image using pin-hole camera, we loose an im ...
- 线性代数学习笔记7-4:马尔可夫矩阵、矩阵幂的稳态问题
马尔可夫矩阵Markov Matrix 马尔可夫矩阵与概率的思想有关,马尔可夫矩阵满足 每个元素aij≥0a_{ij}\geq 0aij≥0 每列元素的总和为1 有时也定义马尔可夫矩阵的每行元素总和 ...
- opencv历史BUG
2020.9.20 报错: cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-j8nxabm_ ...
- python opencv 将一张图片无缝合成到另一张图片中
原文地址:Seamless Cloning using OpenCV (Python , C++) 无缝合成(Seamless Cloning)是 opencv3 的新特性. 利用这个新特性,我们可以 ...
- 基于opencV的动态背景下运动目标检测及跟踪(修改版)
基于openCV的动态背景下的运动目标检测 from: http://www.mianfeiwendang.com/doc/89c6692a222a84b2ced0d502/1 摘要:介绍在动态背景下 ...
- OpenCV注视估计Gaze Estimation的实例(附完整代码)
OpenCV注视估计Gaze Estimation的实例 OpenCV注视估计Gaze Estimation的实例 OpenCV注视估计Gaze Estimation的实例 #include < ...
最新文章
- Torch not compiled with CUDA enabled
- CentOS 6和CentOS 7管理系统服务的区别
- Android--SoundPool
- html滚动菜单置顶,javascript改变position值实现菜单滚动至顶部后固定
- c语言 activemq,activemq概念介绍
- android中内存泄露,Android中的内存泄露
- 【NOIp 2015】【DFS】斗地主
- 中微CMS32 Keil环境搭建
- ubuntu18.04纯命令行安装chrome
- Android equal和==的区别
- SAP License:如何取消物料帐的激活
- 关于一些初级ACM竞赛题目的分析和题解(四)。
- Wannacry蠕虫勒索软件“永恒之蓝”3种修复方案
- 数学分析教程(第三版)读后感
- Google的愚人节
- 扫地机器人扫水泥地板有用吗_拖地机器人好用吗—拖地机器人的优点介绍
- 免费AI数据标注工具-音频标注软件
- OS和Linux笔记
- Ubuntu 下查看DNS地址
- AGM FPGA使用答疑