当前位置:网站首页>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
边栏推荐
- Selenium01
- 05-Theos
- 软件测试术语 - 场景测试
- Test Development Engineer Growth Diary 007 - Bug Priority Definition and Filling Specifications
- The CTO said I was not advised to use SELECT *, why is that?
- STL source code analysis: conceptual understanding of iterators, and code testing.
- prometheus-federation-tls加密
- 阿里二面:列出 Api 接口优化的几个技巧
- Pioneer in Distributed Systems - Leslie Lambert
- Distance calculation from space vertex to straight line and its source code
猜你喜欢

分布式系统中的开创者—莱斯利·兰伯特

How to understand plucker coordinates (geometric understanding)

作为测试leader,考察求职者的几个方面

DNS域名解析服务

MySQL主从复制配置搭建,一步到位

Camera coordinate system, world coordinate system, pixel coordinate system conversion, and Fov conversion of OPENGLDEFocal Length and Opengl

Distance calculation from space vertex to straight line and its source code

Advanced multi-threading (lock strategy, spin+CAS, Synchronized, JUC, semaphore)

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

阿里二面:列出 Api 接口优化的几个技巧
随机推荐
Camera coordinate system, world coordinate system, pixel coordinate system conversion, and Fov conversion of OPENGLDEFocal Length and Opengl
《心智社会》—马文·明斯基
Test Development Engineer Growth Diary 003 - Interface Automation Framework Construction
Advanced multi-threading (lock strategy, spin+CAS, Synchronized, JUC, semaphore)
测试开发工程师成长日记017 - bug的生命周期
阿里二面:Redis有几种集群方案?我答了4种
Proof of distance calculation from space vertex to plane and its source code
Test Development Engineer Growth Diary 018 - Record of Required Questions for Test Interview (Continuous Update)
STL源码剖析:迭代器的概念理解,以及代码测试。
多线程进阶(CountDownLatch,死锁,线程安全集合类)
export , export default, import complete usage
阿里二面:列出 Api 接口优化的几个技巧
openstack删除计算节点
Data types of Redis6
iptables命令
PXE efficient mass network capacity
让百度地图生成器里的“标注”内容展开--解决方案
Test Development Engineer Growth Diary 015 - Top 20 Test Interview Questions
空间顶点到平面的距离计算的证明及其源码
Detailed explanation of numpy multidimensional array ndarray