📄 matrix.h
字号:
#ifndef _MATRIX_H
#define _MATRIX_H
#include<iostream>
#include<cstdlib>
using namespace std;
//矩阵类的定义
template<class T> class Mtx
{
private:
int nrows; //行数
int ncols; //列数
T** ets; //矩阵元素
public:
//Mtx(int n, int m, T**); //构造函数(n乘m)
Mtx(int n, int m, T d = 0); //所有元素等于d
//Mtx(const Mtx &); //拷贝构造函数
~Mtx(); //析构函数
//Mtx& operator=(const Mtx&); //重载=
//Mtx& operator+=(const Mtx&); //重载+=
//Mtx& operator-=(const Mtx&); //重载-=
//Vtr operator*(const Vtr&) const; //矩阵-向量乘法
//T* operator[](int i) const{ return ets[i]; }
//下标,i行j列元素是[i][j]
T& operator()(int i, int j){ return ets[i][j]; }
T& operator()(int i, int j) const { return ets[i][j]; }
//下标,i行j列的元素是(i,j)
//template<class S> friend Mtx<S> operator+(const Mtx<S>&); //一元+
//template<class S> friend Mtx<S> operator+(const Mtx<S>&, const Mtx<S>&); //二元+
//template<class S> friend Mtx<S> operator-(const Mtx<S>&); //一元-
//template<class S> friend Mtx<S> operator-(const Mtx<S>&, const Mtx<S>&); //二元-
//template<class S> friend ostream& operator<<(ostream&, const Mtx<S>&); //输出运算符
//template<class S> friend Mtx<S> turn(const Mtx<S>&); //矩阵转置
};
inline void error(char* v) // 辅助函数
{
cout << v << "\n ...program exited\n\n\n\n";
system("pause");
exit(1); // 包括<stdlib.h>
}
template<class T> ostream& operator<<(ostream& s, const Mtx<T>& t)
{
s << "\n nrows = " << t.nrows << "\tncols = " << t.ncols << endl;
for(int i = 0; i < t.nrows; i++)
{
for(int j = 0; j < t.ncols; j++)
s << t(i, j) << "\t";
s << "\n\n";
}
return s;
}
/*
template<class T> Mtx<T>::Mtx(int n, int m, T** dbp)
{
nrows = n;
ncols = m;
ets = new T* [nrows];
for(int i = 0; i < nrows; i++)
{
ets[i] = new T [ncols];
for(int j = 0; j < ncols; j++) ets[i][j] = dbp[i][j];
}
}
*/
template<class T> Mtx<T>::Mtx(int n, int m, T a)
{
ets = new T* [nrows = n];
ncols = m;
for(int i = 0; i < nrows; i++)
{
ets[i] = new T [ncols];
for(int j = 0; j < ncols; j++) ets[i][j] = a;
}
}
/*
template<class T> Mtx<T>::Mtx(const Mtx<T>& mat)
{
ets = new T* [nrows = mat.nrows];
ncols = mat.ncols;
ets = new T* [nrows];
for(int i = 0; i < nrows; i++)
{
ets[i] = new T [ncols];
for(int j = 0; j < ncols; j++) ets[i][j] = mat[i][j];
}
}
*/
template<class T> inline Mtx<T>::~Mtx()
{
for(int i = 0; i < nrows; i++) delete[] ets[i];
delete[] ets;
}
/*
template<class T> Mtx<T>& Mtx<T>::operator=(const Mtx<T>& mat)
{
if(this != &mat)
{
if(nrows != mat.nrows || ncols != mat.ncols)
error("bad natrix sizes");
for(int i = 0;i < nrows; i++)
for(int j = 0; j < ncols; j++)
ets[i][j] = mat[i][j];
}
return *this;
}
template<class T> Mtx<T>& Mtx<T>::operator+=(const Mtx<T>& mat)
{
if(nrows != mat.nrows || ncols != mat.ncols)
error("bad natrix sizes");
for(int i = 0;i < nrows; i++)
for(int j = 0; j < ncols; j++)
ets[i][j] += mat[i][j];
return *this;
}
template<class T> Mtx<T>& Mtx<T>::operator-=(const Mtx<T>& mat)
{
if(nrows != mat.nrows || ncols != mat.ncols)
error("bad natrix sizes");
for(int i = 0;i < nrows; i++)
for(int j = 0; j < ncols; j++)
ets[i][j] -= mat[i][j];
return *this;
}
template<class T> inline Mtx<T> operator+(const Mtx<T>& mat)
{
return mat;
}
template<class T> inline Mtx<T> operator-(const Mtx<T>& mat)
{
return Mtx<T>(mat.nrows, mat.ncols) - mat;
}
template<class T> Mtx<T> operator+(const Mtx<T>& m1, const Mtx<T>& m2)
{
if(m1.nrows != m2.nrows || m1.ncols != m2.ncols)
error("bad matrix sizes");
Mtx<T> sum = m1;
sum += m2;
return sum;
}
template<class T> Mtx<T> operator-(const Mtx<T>& m1, const Mtx<T>& m2)
{
if(m1.nrows != m2.nrows || m1.ncols != m2.ncols)
error("bad matrix sizes");
Mtx<T> sum = m1;
sum -= m2;
return sum;
}
Vtr Mtx::operator*(const Vtr& v) const
{
if(ncols != v.size())
error("matix and vector sizes do not match");
Vtr tm(nrows);
for(int i = 0; i < nrows; i++)
for(int j = 0; j < ncols; j++)
tm[i] += ets[i][j]*v[j];
return tm;
}
template<class T> Mtx<T> turn(const Mtx<T>& mat)
{
Mtx<T> m1(mat.ncols, mat.nrows);
for(int i = 0; i < m1.nrows; i++)
for(int j = 0; j < m1.ncols; j++)
m1.ets[i][j] = mat.ets[j][i];
return m1;
}
*/
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -