当前位置:网站首页>QR分解求矩阵逆--c工程实现
QR分解求矩阵逆--c工程实现
2022-07-17 08:19:00 【Jack Ju】
一、逆上三角矩阵算法
问题
我有代码来计算下三角矩阵的逆。如何通过稍微改变它来从下面的代码 计算上三角矩阵的逆?
function L = L_inv(A)
[n,n] = size(A);
L = zeros(n);
for i=1:n
L(i,i) = 1/A(i,i);
for j=i+1:n
L(j,i)=-A(j, i:j-1)*L(i:j-1,i)/A(j,j);
end
end
end

笔者根据上述图片写的代码:
在这里插入代码片
/**
* Function:Solution for Matrix Inverse
* Date:2022-07-16 18:00:00
* Last Modified:Juchunyu
*/
#include <stdio.h>
#include <math.h>
`int main(){
double m_in_[3][3]={
1,2,3,
0,2,3,
0,0,5,
};
int length = 3;
double m_in[3][3]={0};
//tranpose
for(int i=0;i<length;i++){
for(int j=0;j<length;j++){
m_in[j][i] = m_in_[i][j];
}
}
double m_inverse_[3][3]={0};
for(int j=0;j<length;j++){
//求解对角线
m_inverse_[j][j] = 1/m_in[j][j];
for(int i=j+1;i<length;i++){
double temp = 0;
for(int k=j;k<=i-1;k++){
temp+=m_in[i][k]*m_inverse_[k][j];
}
m_inverse_[i][j] = -temp/m_in[i][i];
}
}
double m_inverse[3][3]={0};
//tranpose
for(int i=0;i<length;i++){
for(int j=0;j<length;j++){
m_inverse[j][i] = m_inverse_[i][j];
}
}
printf("shangsanjiao分解:\n");
//printf
for(int i=0;i<length;++i)
{
for(int j=0;j<length;++j)
{
printf("%2.4f ",m_inverse[i][j]);
//
}
printf("\n");
}
}`
二、QR分解





笔者根据上述的QR分解的数值计算公式写了如下的代码,可以求出Q和R矩阵。
/** * Function:Solution for Matrix Inverse * Date:2022-07-16 22:00:00 * Last Modified:Juchunyu */
#include <stdio.h>
#include <math.h>
#define SIZE 3
int main(){
//double init[3][3]={1,1,1,1,1,0,1,0,0};
double init[3][3]={
1,2,3,4,5,6,7,8,9};
double Q[3][3]={
0};
double R[3][3]={
0};
double v[3]={
0};
int length = 3;
for(int k=0;k< length;k++){
/*R(K-1,K)*/
if(k>=1){
for(int i=0;i<k;i++){
for(int j=0;j<length;j++){
R[i][k] += Q[j][i]*init[j][k];
}
}
}
/*V*/
for(int j=0;j<length;j++){
if(k<1){
v[j] = init[j][0];
} else {
v[j] = init[j][k];
for(int g=0;g<k;g++){
v[j] -= R[g][k]*Q[j][g];
}
}
}
printf("Jcy v:\n");
printf("v=\n");
for(int i=0;i<SIZE;++i)
{
printf("%2.4f ",v[i]);
}
/*R(K,K)*/
for(int i=0;i<length;i++){
R[k][k] += v[i]*v[i];
}
R[k][k] = sqrt(R[k][k]);
/*Q**/
for(int i=0;i<length;i++){
Q[i][k] = v[i]/R[k][k];
}
printf("Jcy QR分解:\n");
printf("Q=\n");
for(int i=0;i<SIZE;++i)
{
for(int j=0;j<SIZE;++j)
{
printf("%2.4f ",Q[i][j]);
//
}
printf("\n");
}
printf("Jcy QR分解:\n");
printf("R=\n");
for(int i=0;i<SIZE;++i)
{
for(int j=0;j<SIZE;++j)
{
printf("%2.4f ",R[i][j]);
//
}
printf("\n");
}
//return;
//Q[k-1][k] =
}
/* printf("Jcy QR分解:\n"); printf("Q=\n"); for(int i=0;i<SIZE;++i) { for(int j=0;j<SIZE;++j) { printf("%2.4f ",Q[i][j]); // } printf("\n"); } printf("Jcy QR分解:\n"); printf("R=\n"); for(int i=0;i<SIZE;++i) { for(int j=0;j<SIZE;++j) { printf("%2.4f ",R[i][j]); // } printf("\n"); } */
}
边栏推荐
- 凸面镜面3D玻璃轮廓扫描
- 深度学习第二周Neural Network Basics习题整理
- JS study note 04-05 - modification of constructor and creation using factory method
- Solution to sudo PIP install gevent installation failure
- JS学习笔记14-15:JS数组及数组字母量
- The latest generation of Internet: Web 3.0
- Eureka self protection
- Filesourcestrategy, datasourcestrategy and datasourcev2strategy in spark
- WebGL的CPU负载问题-WebGL对比
- 深度学习第四周Key Concepts on Deep Neural Networks习题整理
猜你喜欢

Linear regression + basic optimization of deep learning

Redis6新数据类型——HyperLogLog

把HBuilderX的主题变成vscode

WPF 3D application building (Foundation)

创建静态库的基本步骤

60. Initial knowledge of wsgiref handwritten web framework +jinja2 module

Visual studio 2022 (vs 2022) cannot read memory

事件循环、宏任务、微任务

1. Flask Foundation

Redis常用数据类型——哈希(Hash)和有序集合 Zset(sorted set)
随机推荐
Microservices and microservice architecture
Finishing of key concepts on deep neural networks exercises in the fourth week of in-depth learning
Basic steps for creating a static library
1. Decision tree
JS learning notes 14-15: JS array and array letter quantity
Baidu Apollo
Redis common data types - hash and ordered set Zset (sorted set)
Yyds dry inventory cross origin cross domain request
instanceof的另类写法示例说明
力扣1669合并两个链表笔记
Use of OpenCV polar transformation function warppolar
力扣455分发饼干笔记
STM32CUBEIDE(9)----USART通过DMA收发
JS learning notes 09-12: prototype objects, foreach+tostring and recycle bin
JS study note 04-05 - modification of constructor and creation using factory method
什么是内存溢出
半透明双层玻璃侧厚
Use torch NN builds the simplest neural network framework
How to position the circle of friends? Three core steps to build a circle of friends selling popular products
ROS常用工具包命令