Purpose: I recently wrote C++ code and encountered finding the inverse of a matrix. The Eigen library has method compute. But because of the re-derivation process, a specific way of inversion is required. So I realized the inverse of the matrix myself. The specific code is as follows, and the formula part will be supplemented and updated later.
// Update the formula section
Wikipedia's definition:
In linear algebra, the adjoint matrix (English: adjust matrix) of a square matrix is a concept similar to the inverse matrix. If a matrix is invertible, then its inverse differs from its adjoint by only one coefficient. However, adjoint matrices are also defined for matrices that are not invertible and do not require division.
Suppose A \bm{A}The adjoint matrix of A is written as adj ( A ) adj(\bm{A})a d j ( A ) orA ∗ \bm{A}^*A∗ Because it usually takes 3 × 3 3\times 3
in the project3×3 squares, can be defined as:
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 companion 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∣
∣=d e t[aimajmainajn]=d e t∣
∣aimajmainajn∣
∣
It can be seen from the above formula that the adjoint matrix is the transpose of the cofactor matrix.
The adjoint matrix can be used to calculate the inverse of the matrix:
A − 1 = adj ( A ) det ( A ) = A ∗ ∣ A ∣ \bm{A}^{-1}=\cfrac{adj(\bm{A}) }{det(\bm{A})}=\cfrac{\bm{A}^*}{|\bm{A}|}A−1=d e t ( A )adj(A)=∣A∣A∗
where A ∗ \bm{A}^*A∗ is the accompanying matrix;∣ A ∣ |\bm{A}|∣ A ∣ is the determinant of the matrix.
The determinant calculation can refer to my blog
https://blog.csdn.net/weixin_43851636/article/details/125999210?spm=1001.2014.3001.5502
Below is the C++ code to compute the inverse of a matrix via an adjoint matrix, which is the same as the inversion in the Eigen library.
c++ code
#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;
}
The result of the operation is as follows: it is the same as the eigen calculation.
The hyperlink pasted on Wikipedia is:
https://zh.m.wikipedia.org/zh-sg/%E4%BC%B4%E9%9A%8F%E7%9F%A9%E9%98%B5