当前位置:网站首页>Calculate the inverse source of the matrix (using the adjoint matrix, a 3x3 matrix)
Calculate the inverse source of the matrix (using the adjoint matrix, a 3x3 matrix)
2022-07-30 07:50:00 【The sunset dyed the ramp】
目的:最近写C++代码,Encounter finding the inverse of a matrix.EigenThe library has methods to compute.But because of the re-derivation process,A specific way of inverting is required.So I realized the inverse of the matrix myself.具体代码如下,The formula part will be added later,更新.
//Update formula section
维基百科的定义:
在线性代数中,The adjoint matrix of a square matrix(英语:adjugate matrix)is a concept similar to an inverse matrix.如果矩阵可逆,那么它的逆矩阵和它的伴随矩阵之间只差一个系数.然而,伴随矩阵对不可逆的矩阵也有定义,并且不需要用到除法.
假设 A \bm{A} AThe adjoint matrix of a d j ( A ) adj(\bm{A}) adj(A)或者 A ∗ \bm{A}^* A∗
Because in projects it is usually required 3 × 3 3\times 3 3×3的矩阵,它可以定义为:
A = [ a 11 a 12 a 13 a 21 a 22 a 23 a 31 a 32 a 33 ] \bm{A}=\begin{bmatrix} a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \\ a_{31} & a_{32} & a_{33} \end{bmatrix} A=⎣⎡a11a21a31a12a22a32a13a23a33⎦⎤
Its adjoint matrix is :
a d j ( A ) = [ + ∣ a 22 a 23 a 32 a 33 ∣ − ∣ a 12 a 13 a 32 a 33 ∣ + ∣ a 12 a 13 a 22 a 23 ∣ − ∣ a 21 a 23 a 31 a 33 ∣ + ∣ a 11 a 13 a 31 a 33 ∣ − ∣ a 11 a 13 a 21 a 23 ∣ + ∣ a 21 a 22 a 31 a 32 ∣ − ∣ a 11 a 12 a 31 a 32 ∣ + ∣ a 11 a 12 a 21 a 22 ∣ ] adj(\bm{A})=\begin{bmatrix} +\begin{vmatrix} a_{22} & a_{23} \\ a_{32} & a_{33} \end{vmatrix} & -\begin{vmatrix} a_{12} & a_{13} \\ a_{32} & a_{33} \end{vmatrix} & +\begin{vmatrix} a_{12} & a_{13} \\ a_{22} & a_{23} \end{vmatrix} \\ \\ -\begin{vmatrix} a_{21} & a_{23} \\ a_{31} & a_{33} \end{vmatrix} & +\begin{vmatrix} a_{11} & a_{13} \\ a_{31} & a_{33} \end{vmatrix} & -\begin{vmatrix} a_{11} & a_{13} \\ a_{21} & a_{23} \end{vmatrix} \\ \\ +\begin{vmatrix} a_{21} & a_{22} \\ a_{31} & a_{32} \end{vmatrix} & -\begin{vmatrix} a_{11} & a_{12} \\ a_{31} & a_{32} \end{vmatrix} & +\begin{vmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{vmatrix} \end{bmatrix} adj(A)=⎣⎡+∣∣a22a32a23a33∣∣−∣∣a21a31a23a33∣∣+∣∣a21a31a22a32∣∣−∣∣a12a32a13a33∣∣+∣∣a11a31a13a33∣∣−∣∣a11a31a12a32∣∣+∣∣a12a22a13a23∣∣−∣∣a11a21a13a23∣∣+∣∣a11a21a12a22∣∣⎦⎤
其中
∣ a i m a i n a j m a j n ∣ = d e t [ a i m a i n a j m a j n ] = d e t ∣ a i m a i n a j m a j n ∣ \begin{vmatrix} a_{im} & a_{in} \\ a_{jm} & a_{jn} \end{vmatrix} =det \begin{bmatrix} a_{im} & a_{in} \\ a_{jm} & a_{jn} \end{bmatrix}=det \begin{vmatrix} a_{im} & a_{in} \\ a_{jm} & a_{jn} \end{vmatrix} ∣∣aimajmainajn∣∣=det[aimajmainajn]=det∣∣aimajmainajn∣∣
从上面公式可以看出,The adjoint matrix is the transpose of the cofactor matrix.
The companion matrix can be used to compute the inverse of the matrix:
A − 1 = a d j ( A ) d e t ( A ) = A ∗ ∣ A ∣ \bm{A}^{-1}=\cfrac{adj(\bm{A})}{det(\bm{A})}=\cfrac{\bm{A}^*}{|\bm{A}|} A−1=det(A)adj(A)=∣A∣A∗
其中 A ∗ \bm{A}^* A∗为伴随矩阵; ∣ A ∣ |\bm{A}| ∣A∣is the determinant of the matrix.
The determinant calculation can refer to mineblog
https://blog.csdn.net/weixin_43851636/article/details/125999210?spm=1001.2014.3001.5502
The following is the calculation of the inverse of the matrix by the adjoint matrixC++代码,它和EigenThe inversion in the library is the same.
c++代码
#include<Eigen/Eigen>
#include<iostream>
#include<vector>
#include<string>
/* m=[n1, n2, n3] */
Eigen::Matrix3f ConstructMatrix3fFromVectors(Eigen::Vector3f& n1, Eigen::Vector3f& n2, Eigen::Vector3f& n3)
{
Eigen::Matrix3f m;
m.block<1, 3>(0, 0) = n1;
m.block<1, 3>(1, 0) = n2;
m.block<1, 3>(2, 0) = n3;
return m;
}
Eigen::Matrix3f ConstructAdjugateMatrix3f(Eigen::Matrix3f& m)
{
Eigen::Matrix3f adju_m;
adju_m(0, 0) = m(1, 1)*m(2, 2) - m(2, 1)*m(1, 2);
adju_m(0, 1) = -(m(0, 1)*m(2, 2) - m(2, 1)*m(0, 2));
adju_m(0, 2) = m(0, 1)*m(1, 2) - m(1, 1)*m(0, 2);
adju_m(1, 0) = -(m(1, 0)*m(2, 2) - m(2, 0)*m(1, 2));
adju_m(1, 1) = m(0, 0)*m(2, 2) - m(2, 0)*m(0, 2);
adju_m(1, 2) = -(m(0, 0)*m(1, 2) - m(1, 0)*m(0, 2));
adju_m(2, 0) = m(1, 0)*m(2, 1) - m(2, 0)*m(1, 1);
adju_m(2, 1) = -(m(0, 0)*m(2, 1) - m(2, 0)*m(0, 1));
adju_m(2, 2) = m(0, 0)*m(1, 1) - m(1, 0)*m(0, 1);
return adju_m;
}
Eigen::Vector3f ComputeKVector(Eigen::Matrix3f& adjm, Eigen::Vector3f& b)
{
Eigen::Vector3f k;
k = adjm*b;
return k;
}
//test adjugate matrix
int main(int argc, char** argv)
{
Eigen::Vector3f n1 = Eigen::Vector3f(1, 2, 3);
Eigen::Vector3f n2 = Eigen::Vector3f(2, 1, 3);
Eigen::Vector3f n3 = Eigen::Vector3f(2, 2, 1);
Eigen::Matrix3f m = ConstructMatrix3fFromVectors(n1, n2, n3);
Eigen::Matrix3f m_i = m.inverse();
std::cerr << "m_i: \n" << m_i << std::endl;
Eigen::Matrix3f adju_m = ConstructAdjugateMatrix3f(m);
float det = m.determinant();
Eigen::Matrix3f m_i_my = adju_m/det;
std::cerr << "m_i_my: \n" << m_i_my << std::endl;
std::cerr<<"end test..."<<std::endl;
return 0;
}
运行的结果如下:它和eigenCalculate the same.
Paste the hyperlink to Wikipedia as :
https://zh.m.wikipedia.org/zh-sg/%E4%BC%B4%E9%9A%8F%E7%9F%A9%E9%98%B5
边栏推荐
猜你喜欢

空间顶点到直线的距离计算及其源码

AI可通过X光片识别种族,但没人知道为什么

What happens when @Bean and @Component are used on the same class?

PXE高效批量网络装机

STL源码剖析:临时对象的代码测试和理解

How to understand plucker coordinates (geometric understanding)

Huawei released "ten inventions", including computing, intelligent driving and other new fields

prometheus-Federation机制配置

图解关系数据库设计思想,这也太形象了

export , export default, import complete usage
随机推荐
export , export default, import complete usage
Software Testing Terminology - Scenario Testing
The Geometric Meaning of Vector Cross Product and the Calculation of Modulus
你被MySQL 中的反斜杠 \\坑过吗?
VR机器人教你如何正确打乒乓球
Network Protocol 03 - Routing and NAT
Redis6的数据类型
When does MySQL use table locks and when does it use row locks?
No, the Log4j vulnerability hasn't been fully fixed yet?
DHCP原理与配置
Test Development Engineer Growth Diary 018 - Record of Required Questions for Test Interview (Continuous Update)
Table with tens of millions of data, how to query the fastest?
Ali two sides: Sentinel vs Hystrix comparison, how to choose?
MYSQL-GROUP BY 用法 全网最精,通熟易懂的话解释
STL source code analysis: conceptual understanding of iterators, and code testing.
使用 Grafana 的 Redis Data Source 插件监控 Redis
测试开发工程师成长日记007 - Bug的优先级定义及填写规范
DHCP principle and configuration
debian 问题
多线程基础(多线程内存,安全,通信,线程池和阻塞队列)