VBA idea 常用快捷键 Kotlin listview grails variant threejs jackson 河南普通话考试报名 ppt视频教程下载 excel太长的文字隐藏 小程序下拉刷新样式 matlab读入图片 excel加减混合求和 kubernetes入门 mysql数据库 python中文手册 python正则匹配数字 java8函数式接口 java开发环境配置 怎么安装java环境 java字符串匹配 一键刷入recovery 圣剑世界 rndis驱动下载 bat脚本 js格式化时间 ad19 图片放大软件 特战英雄辅助 steam怎么卸载 tomcat修改端口 1667 cad视口旋转 cad特性匹配 php递归 正当防卫4存档 quickchm grep正则表达式 omg小北
当前位置: 首页 > 学习教程  > 编程语言

矩阵类(+,*,转置,求逆)

2021/1/28 23:24:35 文章标签:

Matrix.h文件 #pragma once #include <iostream> #include<math.h> #include<iomanip> #include<string.h> using namespace std;class Matrix { public:Matrix();//无参数的构造函数Matrix(int a, int b);//有参数的构造函数Matrix(const Matrix &am…

Matrix.h文件

#pragma once
#include <iostream>
#include<math.h>
#include<iomanip>
#include<string.h>
using namespace std;

class Matrix
{
public:
	Matrix();//无参数的构造函数
	Matrix(int a, int b);//有参数的构造函数
	Matrix(const Matrix & one);//拷贝构造函数
	~Matrix();//析构函数
	void Input();//输入
	void operator=(const Matrix &m);//重载=
	friend Matrix operator+(const Matrix &a, const Matrix & b);//重载+
	friend Matrix operator*(const Matrix & a,const Matrix & b);//重载*
	Matrix Transpose();//转置
	Matrix Inverse();//求逆
	void Show();//输出
public:
	int row;//行
	int col;//列
	double **array;//动态数组
};

Matrix.cpp

#include "Matrix.h"


Matrix::Matrix()
{
	cout << "矩阵的行:";
	cin >> row;
	cout << "矩阵的列:";
	cin >> col;
	array = new double*[row];
	for (int i = 0; i < row; i++)//col列
	{
		array[i] = new double[col];
	}
	Input();
}

Matrix::Matrix(int a, int b)
{
	row = a;
	col = b;
	array = new double*[row];
	for (int i = 0; i < row; i++)//每行col列
	{
		array[i] = new double[col];
	}

	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			array[i][j] = 0;
		}
	}
}

Matrix::Matrix(const Matrix & one)//复制构造函数
{
	row = one.row;
	col = one.col;
	array = new double*[row];
	for (int i = 0; i < row; i++)//col列
	{
		array[i] = new double[col];
	}

	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			array[i][j] = one.array[i][j];
		}
	}
}

Matrix::~Matrix()
{
	for (int i = 0; i < row; i++)
		delete[]array[i];
	delete[]array;
}

void Matrix::Input()
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			cin >> array[i][j];
		}
	}
}

void Matrix::operator=(const Matrix & one)
{
	row = one.row;
	col = one.col;
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			array[i][j] = one.array[i][j];
		}
	}
}

Matrix operator+(const Matrix & a, const Matrix & b)
{
	if (a.row == b.row&& a.col == b.col)
	{
		Matrix c(a.row, b.col);
		for (int i = 0; i < a.row; i++)
		{
			for (int j = 0; j < a.col; j++)
			{
				c.array[i][j] = a.array[i][j] +
				b.array[i][j];
			}
		}
		return c;
	}
	else
		cout << "矩阵无法相加" << endl;
}
	
Matrix operator*(const Matrix & a, const Matrix & b)
{
	if (a.row == b.col || a.col == b.row)
	{
		if (a.col == b.row)
		{
			//相乘后的矩阵
			Matrix c(a.row, b.col);
			for (int i = 0; i < a.row; i++)
			{
				for (int z = 0; z < b.col; z++)
				{
					for (int j = 0; j < a.col; j++)
					{
						c.array[i][z] = c.array[i][z] + a.array[i][j] * b.array[j][z];
					}
				}
			}
			return c;
		}
		else if (a.row == b.col)
		{
			//相乘后的矩阵
			Matrix c(a.row, b.col);
			for (int i = 0; i < b.row; i++)
			{
				for (int z = 0; z < a.col; z++)
				{
					for (int j = 0; j < b.col; j++)
					{
						c.array[i][z] = c.array[i][z] + b.array[i][j] * a.array[j][z];
					}
				}
			}
			return c;
		}
	}
	else
		cout << "两矩阵无法相乘" << endl;
}

Matrix Matrix::Transpose()
{
	//转置后的矩阵
	Matrix b(col, row);
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
			b.array[j][i] = array[i][j];
	}
	return b;
}

Matrix Matrix::Inverse()
{
	int i, j, n, test;
	Matrix a(row, col);
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			a.array[i][j] = array[i][j];
		}
	}
	//单位矩阵
	Matrix b(row, col);
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			if (i == j)
				b.array[i][j] = 1;
			else
				b.array[i][j] = 0;
		}
	}

	//求逆--求逆矩阵所做的所有步骤,单位矩阵都做一遍
	int r = 0, c = 0;
	double nu, num;
	n = row;
	//原始的求逆矩阵有一整行0,或一整列0.后面都不用做了。
	for (i = 0; i < n; i++)//有一行全为零,矩阵不进行满秩和求逆步骤
	{
		r = 0;
		for (int j = 0; j < n; j++)
		{
			if (a.array[i][j] == 0)
			{
				r = r + 1;
			}
		}
		if (r == n)
			break;
	}
	for (j = 0; j < n; j++)//有一列全为零,矩阵不进行满秩和求逆步骤
	{
		c = 0;
		for (i = 0; i < n; i++)
		{
			if (a.array[i][j] == 0)
			{
				c = c + 1;
			}
		}
		if (c == n)
			break;
	}

	//先判断是否满秩,满秩则求逆
	if (r != n && c != n)//没有一行或一列都为0的情况
	{
		i = 0;
		j = 0;
		//初等行变换,变成阶梯矩阵
		for (test = 0; test < n; test++)//test表示主元所在的行,列。
		{
			r = test, c = test;
			//找每一列从第test行往下第一个不为0的当主元
			for (; a.array[r][c] == 0; r++)
			{
				if (r == n - 1)//防止指针出界
					break;
			}
			for (j = 0, c = 0; j < n; j++)//使主元移动到主对角线上
			{
				num = a.array[r][c];
				nu = a.array[test][j];
				a.array[test][j] = num;
				a.array[r][c] = nu;

				num = b.array[r][c];
				nu = b.array[test][j];
				b.array[test][j] = num;
				b.array[r][c] = nu;

				c++;
			}

			//主元所在列的数,主元以下的都要变成0
			for (i = test; i < n; i++)
			{
				j = test;
				if (i == j)//主元往下
					i++;
				if (i == n)//防止指针出界
					break;
				num = a.array[i][j] / a.array[test][j];

				for (j = 0; j < n; j++)
				{
					a.array[i][j] = a.array[i][j] - a.array[test][j] * num;

					b.array[i][j] = b.array[i][j] - b.array[test][j] * num;
				}
			}
		}

		//看是否满秩
		for (i = 0; i < n; i++)//有一行全为零,矩阵不满秩
		{
			r = 0;
			for (int j = 0; j < n; j++)
			{
				if (a.array[i][j] == 0)
				{
					r = r + 1;
				}
			}
			if (r == n)
				break;
		}
		for (j = 0; j < n; j++)//有一列全为零,矩阵不满秩
		{
			c = 0;
			for (i = 0; i < n; i++)
			{
				if (a.array[i][j] == 0)
				{
					c = c + 1;
				}
			}
			if (c == n)
				break;
		}

		//如果满秩,则可以进行求逆。(此时求逆阵,单位阵已经经过了初等行变换)
		if (r != n && c != n)//没有一行或一列都为0的情况
		{
			for (i = 0, j = 0, test = 0; test < n; test++)//test表示主元所在的行,列。
			{
				i = test, j = test;
				r = test, c = test;
				num = a.array[i][j];
				for (j = 0; j < n; j++)//所在行除以主元本身,主元变成1
				{
					a.array[i][j] = a.array[i][j] / num;

					b.array[i][j] = b.array[i][j] / num;
				}
				for (i = 0; i < test; i++)//主元所在列的数,主元以上的变成0
				{
					j = test;
					num = a.array[i][j];
					for (j = 0; j < n; j++)
					{
						a.array[i][j] = a.array[i][j] - a.array[test][j] * num;

						b.array[i][j] = b.array[i][j] - b.array[test][j] * num;
					}
				}
			}
			return b;
		}
		else
		{
			cout << "矩阵不可逆" << endl;
		}
	}
	else
	{
		cout << "矩阵不可逆" << endl;
	}
}

void Matrix::Show()
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			if (fabs(array[i][j]) < 10e-10)
			{
				array[i][j] = 0;

			}
			cout << fixed << setprecision(4) << array[i][j] << ",";
		}
		cout << endl;
	}
}


本文链接: http://www.dtmao.cc/news_show_650205.shtml

附件下载

相关教程

    暂无相关的数据...

共有条评论 网友评论

验证码: 看不清楚?