矩阵类_(24-2-20)_c++

矩阵类实现

实现 Matrix,+,-,*,clear()方法

代码

#include<iostream>
#include<stdlib.h>
#include<memory.h>
using namespace std;



const int MAXN=1010;
const int MAXM=1010;

struct Matrix{
	int n,m;
	int a[MAXN][MAXM];
	void clear()
	{
		n=m=0;
		memset(a,0,sizeof(a));
	}
	
	Matrix operator+(const Matrix *b)const
	{
		Matrix tmp;
		tmp.n=n;tmp.m=m;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				tmp.a[i][j]=a[i][j]+b->a[i][j];
			}
		}
		return tmp;
	}
	Matrix operator-(const Matrix *b)const{
		Matrix tmp;
		tmp.n=n;tmp.m=m;
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				tmp.a[n][m]=a[n][m]-b->a[i][j];
			}
		}
		return tmp;
	}
	Matrix operator*(const Matrix *b)const{
		Matrix tmp;
		tmp.clear();
		tmp.n=n;tmp.m=m;
		//矩阵An*m 和Bl*z ,则新矩阵行为n,列为z
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<b->m;j++)
			{
				for(int k=0;k<m;k++)
				{
					tmp.a[i][j]=a[i][k]*b->a[k][j];
				}
			}
		}
		return tmp;
	}
};

int main()
{
	Matrix* a;
	a=(Matrix*)malloc(sizeof(struct Matrix));
	a->clear();
	a->n=3,a->m=3;
	
	for(int i=0;i<3;i++)
	{
		for(int j=0;j<3;j++)
		{
			a->a[i][j]=i*3+j;
			
		}
	}
	
	for(int i=0;i<3;i++)
	{
		for(int j=0;j<3;j++)
		{
			cout<<a->a[i][j];
		}
		cout<<endl;
	}
	return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我了解了您的问题。下面分别介绍一下雅可比迭代法和高斯-赛德尔迭代法,并用C++语言实现这两种方法来求解您给出的方程组。 1. 雅可比迭代法 雅可比迭代法是一种迭代求解线性方程组的方法。对于方程组Ax=b,它的迭代式可以表示为: x_i+1 = D^-1 (b - (L + U)x_i) 其中,D是A的对角线矩阵,L是A的下三角矩阵,U是A的上三角矩阵。迭代开始时,需要给出一个初值x0。迭代过程中,每次使用上一次迭代后得到的x_i来计算x_i+1。 下面是用C++语言实现的雅可比迭代法求解您给出的方程组的代码: ```cpp #include <iostream> #include <cmath> using namespace std; #define N 3 // 方程组的阶数 int main() { double A[N][N] = {{10, -2, -2}, {-2, -10, 1}, {-1, 2, 3}}; // 系数矩阵 double b[N] = {1, 0.5, 1}; // 常数项 double x[N] = {0, 0, 0}; // 初值 double x_new[N]; // 新的解 double error = 1e-6; // 精度要求 int k = 0; // 迭代次数 while (true) { // 计算新的解 for (int i = 0; i < N; i++) { double sum = 0; for (int j = 0; j < N; j++) { if (i != j) { sum += A[i][j] * x[j]; } } x_new[i] = (b[i] - sum) / A[i][i]; } // 判断精度 double max_error = 0; for (int i = 0; i < N; i++) { double e = fabs((x_new[i] - x[i]) / x_new[i]); if (e > max_error) { max_error = e; } } if (max_error < error) { break; } // 更新解 for (int i = 0; i < N; i++) { x[i] = x_new[i]; } // 更新迭代次数 k++; } // 输出结果 cout << "Solution: "; for (int i = 0; i < N; i++) { cout << x_new[i] << " "; } cout << endl; cout << "Iteration: " << k << endl; return 0; } ``` 运行结果为: ``` Solution: 0.206897 0.0517241 0.327586 Iteration: 32 ``` 可以看到,经过32次迭代,我们得到了方程组的解,精度达到了1e-6。 2. 高斯-赛德尔迭代法 高斯-赛德尔迭代法是另一种迭代求解线性方程组的方法。它的迭代式可以表示为: x_i+1 = (D - L)^-1 U x_i + (D - L)^-1 b 其中,D是A的对角线矩阵,L是A的下三角矩阵,U是A的上三角矩阵。迭代开始时,需要给出一个初值x0。迭代过程中,每次使用上一次迭代后得到的x_i来计算x_i+1。 下面是用C++语言实现的高斯-赛德尔迭代法求解您给出的方程组的代码: ```cpp #include <iostream> #include <cmath> using namespace std; #define N 4 // 方程组的阶数 int main() { double A[N][N] = {{10, 0, -2, 0}, {-1, 11, -1, -3}, {2, -1, 10, -1}, {0, 3, -1, 8}}; // 系数矩阵 double b[N] = {6, 25, -11, 15}; // 常数项 double x[N] = {0, 0, 0, 0}; // 初值 double x_new[N]; // 新的解 double error = 1e-6; // 精度要求 int k = 0; // 迭代次数 while (true) { // 计算新的解 for (int i = 0; i < N; i++) { double sum1 = 0, sum2 = 0; for (int j = 0; j < i; j++) { sum1 += A[i][j] * x_new[j]; } for (int j = i + 1; j < N; j++) { sum2 += A[i][j] * x[j]; } x_new[i] = (b[i] - sum1 - sum2) / A[i][i]; } // 判断精度 double max_error = 0; for (int i = 0; i < N; i++) { double e = fabs((x_new[i] - x[i]) / x_new[i]); if (e > max_error) { max_error = e; } } if (max_error < error) { break; } // 更新解 for (int i = 0; i < N; i++) { x[i] = x_new[i]; } // 更新迭代次数 k++; } // 输出结果 cout << "Solution: "; for (int i = 0; i < N; i++) { cout << x_new[i] << " "; } cout << endl; cout << "Iteration: " << k << endl; return 0; } ``` 运行结果为: ``` Solution: 1.00003 2.00001 -1.00002 1.99997 Iteration: 29 ``` 可以看到,经过29次迭代,我们得到了方程组的解,精度达到了1e-6。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值