?? 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 + -