#include "MATRIX.h"
Mtx::Mtx(int m1, int n1) {
if (m1 <= 0 || n1 <= 0)
{
cout << "错误:二维数组的维数不得为0或负数!\n";
system("pause");
exit(0);
}
m = m1;
n = n1;
M = new double*[m1];
for (int i = 0; i < m1; ++i)
M[i] = new double[n1];
}
Mtx::Mtx(const Mtx& M1) {
setMtx(M1.M, M1.m, M1.n);
}
Mtx::Mtx(double **M1, int m1, int n1) {
setMtx(M1, m1, n1);
}
Mtx::~Mtx() {
for (int i = 0; i < m; ++i)
delete[] M[i];
delete[] M;
}
void Mtx::setMtx(double **M1, int m1, int n1) {
if (m1 <= 0 || n1 <= 0)
{
cout << "错误:二维数组的维数不得为0或负数!\n";
system("pause");
exit(0);
}
m = m1;
n = n1;
M = new double*[m1];
for (int i = 0; i < m1; ++i)
{
M[i] = new double[n1];
for (int j = 0; j < n1; ++j)
M[i][j] = M1[i][j];
}
}
void swap(double &a, double &b) {
double c;
c = a;
a = b;
b = c;
}
Mtx Mtx::inv() {
double Mmax, **M1;
int i, j, k, *Mrow, *Mcol;
Mtx M2(m, n);
M1 = new double*[m];
for (i = 0; i < m; ++i)
{
M1[i] = new double[n];
for (j = 0; j < n; ++j)
M1[i][j] = M[i][j];
}
Mrow = new int[m];
Mcol = new int[n];
if (m != n)
{
cout << "错误:矩阵不是方阵,无法求逆!\n";
cout << "当前对象的维数是: " << m << " * " << n << endl;
system("pause");
exit(0);
}
for (k = 0; k < m; ++k)
{
Mmax = 0.;
//找到右下角(m-k)*(m-k)矩阵的主元,及其所在行列
for (i = k; i<m; ++i)
for (j = k; j < n; ++j)
if (fabs(M1[i][j]) > Mmax)
{
//cout << i << j << endl;
Mmax = fabs(M1[i][j]);
Mrow[k] = i;
Mcol[k] = j;
}
if (fabs(Mmax) < 1.E-7)
{
cout << "错误:矩阵奇异,无法求逆!\n";
cout << "当前对象的秩是: " << k << endl;
system("pause");
exit(0);
}
//将当前的主元变换到k*k位置
//cout << Mrow[k] << Mcol[k] << endl;
if (Mrow[k] != k)
for (j = 0; j < n; ++j)
swap(M1[k][j], M1[Mrow[k]][j]);
if (Mcol[k] != k)
for (i = 0; i < m; ++i)
swap(M1[i][k], M1[i][Mcol[k]]);
//将k*k元素,即主元变成1,再将k列其它元素全部化为零,记此变化为左乘Pk:Pk*M1
M1[k][k] = 1 / M1[k][k];
for (i = 0; i < m; ++i)
if (i != k)
M1[i][k] = -M1[i][k] * M1[k][k];
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
if (i != k&&j != k)
M1[i][j] += M1[i][k] * M1[k][j];
for (j = 0; j < n; ++j)
if (j != k)
M1[k][j] *= M1[k][k];
}
//当k执行到最后一次的时候,M1变成的由Pk组成的矩阵,再对其进行从后往前的列行交换返回到原本的情况,就是M1的逆矩阵
for (k = m - 1; k >= 0; --k)
{
if (Mcol[k] != k)
for (j = 0; j < n; ++j)
swap(M1[k][j], M1[Mcol[k]][j]);
if (Mrow[k] != k)
for (i = 0; i < m; ++i)
swap(M1[i][k], M1[i][Mrow[k]]);
}
M2 = M1;
for (int i = 0; i < m; ++i)
delete[] M1[i];
delete[] M1;
delete[] Mrow;
delete[] Mcol;
return M2;
}
Mtx Mtx::tran() {
Mtx M2(n, m);
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
M2.M[j][i] = M[i][j];
return M2;
}
Mtx& Mtx::operator=(const Mtx& M1) {
setMtx(M1.M, M1.m, M1.n);
return *this;
}
Mtx& Mtx::operator=(double **M1) {
if (m == 0 || n == 0)
cout << "错误:矩阵维数未初始化!\n";
setMtx(M1, m, n);
return *this;
}
Mtx operator+(const Mtx& M1, const Mtx& M2) {
if (M1.m != M2.m || M1.n != M2.n)
{
cout << "错误:维数不一致!\n";
cout << "当前对象的维数是: " << M1.m << " * " << M1.n << endl;
cout << "另一个对象的维数是: " << M2.m << " * " << M2.n << endl;
system("pause");
exit(0);
}
Mtx M3(M1.m, M1.n);
for (int i = 0; i < M1.m; ++i)
for (int j = 0; j < M1.n; ++j)
M3.M[i][j] = M1.M[i][j] + M2.M[i][j];
return M3;
}
Mtx& Mtx::operator+=(const Mtx& M1) {
if (m != M1.m || n != M1.n)
{
cout << "错误:维数不一致!\n";
cout << "当前对象的维数是: " << m << " * " << n << endl;
cout << "另一个对象的维数是: " << M1.m << " * " << M1.n << endl;
system("pause");
exit(0);
}
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
M[i][j] += M1.M[i][j];
return *this;
}
Mtx operator-(const Mtx& M1, const Mtx& M2) {
if (M1.m != M2.m || M1.n != M2.n)
{
cout << "错误:维数不一致!\n";
cout << "当前对象的维数是: " << M1.m << " * " << M1.n << endl;
cout << "另一个对象的维数是: " << M2.m << " * " << M2.n << endl;
system("pause");
exit(0);
}
Mtx M3(M1.m, M1.n);
for (int i = 0; i < M1.m; ++i)
for (int j = 0; j < M1.n; ++j)
M3.M[i][j] = M1.M[i][j] - M2.M[i][j];
return M3;
}
Mtx& Mtx::operator-=(const Mtx& M1) {
if (m != M1.m || n != M1.n)
{
cout << "错误:维数不一致!\n";
cout << "当前对象的维数是: " << m << " * " << n << endl;
cout << "另一个对象的维数是: " << M1.m << " * " << M1.n << endl;
system("pause");
exit(0);
}
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
M[i][j] -= M1.M[i][j];
return *this;
}
Mtx operator*(const Mtx& M1, const Mtx& M2) {
if (M1.n != M2.m)
{
cout << "错误:矩阵乘法的维数不匹配!\n";
cout << "当前对象的维数是: " << M1.m << " * " << M1.n << endl;
cout << "另一个对象的维数是: " << M2.m << " * " << M2.n << endl;
system("pause");
exit(0);
}
Mtx M3(M1.m, M2.n);
for (int i = 0; i < M1.m; ++i)
for (int j = 0; j < M2.n; ++j)
{
M3.M[i][j] = 0.;
for (int k = 0; k < M1.n; k++)
M3.M[i][j] += M1.M[i][k] * M2.M[k][j];
}
return M3;
}
double& Mtx::operator[](int mn) {
int m1 = mn / n;
int n1 = mn % n;
return M[m1][n1];
}
const double& Mtx::operator[](int mn) const {
int m1 = mn / n;
int n1 = mn % n;
return M[m1][n1];
}
ostream& operator<< (ostream& os, const Mtx& M1) {
for (int i = 0; i<M1.m; ++i)
{
for (int j = 0; j<M1.n; ++j)
cout << setw(8) << M1.M[i][j] << " ";
cout << endl;
}
cout << endl;
return os;
}
istream& operator<< (istream& is, Mtx& M1) {
for (int i = 0; i<M1.m; i++)
for (int j = 0; j<M1.n; j++)
cin >> M1.M[i][j];
cout << endl;
return is;
}