gpt4 book ai didi

c++ - 矩阵分解算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:31:16 24 4
gpt4 key购买 nike

我有以下算法,它的工作非常奇怪,为了清楚起见,这里是代码

#include <iostream>

using namespace std;

#define maxn 1000
#define n 3

double sum=0;
double sum1=0;
double a[maxn][maxn];
double l[maxn][maxn];
double u[maxn][maxn];

void read(){
for(int i=1;i=n;i++){
for(int j=1;j<=n;j++){
cin>>a[i][j];
}
}
}

void decomposition(){
for(int i=1;i<=n;i++)
l[i][1]=a[i][1];

for(int j=1;j<=n;j++)
u[1][j]=a[1][j]/l[1][1];

for(int j=2;j<=n;j++){
for(int i=j;i<=n;i++){
for(int k=1;k<j;k++){
sum+=l[i][k]*u[k][j];
}

l[i][j]=a[i][j]-sum;
}

u[j][j]=1;
for(int i=j+1;i<=n;i++){
for(int k=1;k<j;k++){
sum1+=l[j][k]*u[k][i];
}
u[j][i]=(a[j][i]-sum1)/l[j][j];
}
}
}

void print(){
cout<<" L matrix "<<endl;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cout<<l[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
cout<<" U matrix "<<endl;

for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cout<<u[i][j]<<" ";
}
cout<<endl;
}
}

int main(){
cout<<"enter the matrix "<<endl;
read();
cout<<endl;
decomposition();
cout<<"print twwo matrix "<<endl;
print();

return 0;
}

但是当我输入矩阵时,比如我想分解这个矩阵

 3 -1 2
1 2 3
2 -2 -1

程序不显示输出,只是需要再次输入一些输入,我在这里的代码中看不到需要输入更多矩阵或数据,所以有什么问题吗?

最佳答案

不确定这是否是问题所在,但在 read 中(这不是函数的好名字,顺便说一句)你有:

for(int i=1;i=n;i++){

这分配 i成为n , 应该是 i==n或者更有可能 i<=n , 它也始终为真,因此执行永远不会停止。

关于c++ - 矩阵分解算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8112146/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com