📄 tmatrix.h
字号:
copy(location,location + rowSize,Matrix[i].begin());
location += rowSize;
}
//#endif //_enable
}
//---------------------------------------------------------------------------
template<class type>void
matrix<type>::SaveToFile(const char* filename,ios_base::openmode mode)
{
ofstream matrixOut(filename,mode);
for(int i = 0;i < rowCount ;i++){
copy(Matrix[i].begin(),Matrix[i].end(),ostream_iterator<Cell,char>(matrixOut,"\t"));
matrixOut<<"\n";
}
}
//---------------------------------------------------------------------------
template<class type> void
matrix<type>::SetRows( int rIndex,vector<type>& RowVector)
{
if( rIndex >= rowCount || rIndex < 0){
CERR("The row can be set!Any key to Quit.");
}
Matrix[rIndex] = RowVector;
}
//---------------------------------------------------------------------------
template<class type> void
matrix<type>::SetCols( int cIndex, vector<type>& ColVector)
{
if( cIndex >= colCount || cIndex < 0){
CERR("The columns can be set!Any key to Quit.");
}
//Ensure access ColVector by subscript is valid:
if(ColVector.capacity() < colSize )
ColVector.resize(colSize);
for(int i = 0;i < colSize ;i++){
Matrix[i][cIndex] = ColVector[i];
}
}
//---------------------------------------------------------------------------
template<class type> const vector<type>
matrix<type>::ReadCols( int ColIndex) const
{
//Ensure ColIndex is less then colCount:
if( ColIndex >= colCount || ColIndex < 0){
CERR("This column doesn't exit!ReadCols() failed.Any key to Quit.");
}
vector<type> vt(colSize);
for(int i = 0;i < colSize ;i++){
vt[i] = Matrix[i][ColIndex];
}
return vt;
}
//---------------------------------------------------------------------------
template<class type> const vector<type>
matrix<type>::ReadRows( int RowIndex) const
{
//Ensure RowIndex is less than rowCount:
if( RowIndex >= rowCount || RowIndex < 0 ){
CERR("This row doesn't exit!Any key to Quit.");
}
return Matrix[RowIndex];
}
//---------------------------------------------------------------------------
template<class type> void
matrix<type>::SwapRows( int rowIndex1, int rowIndex2)
{
//Ensure rowIndex is less than rowCount:
if( rowIndex1 >= rowCount || rowIndex2 >= rowCount || rowIndex1 < 0 || rowIndex2 < 0 ){
CERR("One of rows doesn't exit!Any key to quit!");
}
swap(Matrix[rowIndex1],Matrix[rowIndex2]);
}
//---------------------------------------------------------------------------
template<class type> void
matrix<type>::SwapCols( int colIndex1, int colIndex2)
{
//Ensure colIndex is less than colCount:
if( colIndex1 >= colCount || colIndex2 >= colCount || colIndex1 < 0 || colIndex2 < 0 ){
CERR("One of columns doesn't exit!Any key to quit.");
}
vector<type> vctr1(colSize),vctr2(colSize);
//Store to vectors:vctr1,vctr2
for(int i = 0;i < colSize ;i++){
vctr1[i] = Matrix[i][colIndex1];
vctr2[i] = Matrix[i][colIndex2];
}
//swap:
swap(vctr1,vctr2);
//restore to matrix:
for(int i = 0;i < colSize ;i++){
Matrix[i][colIndex1] = vctr1[i];
Matrix[i][colIndex2] = vctr2[i];
}
}
//---------------------------------------------------------------------------
template<class type> void
matrix<type>::AddRow(const vector<type>& Vector)
{
vector<type> vt(Vector);
//Ensure vt is as large as the matrix needed:
if(vt.capacity() != rowSize )
vt.resize(rowSize,0);
//add Vector to the end:
Matrix.resize( ++rowCount,vt);
totalElem = rowCount * rowSize;
}
//---------------------------------------------------------------------------
template<class type> void
matrix<type>::AddCol(const vector<type>& Vector)
{
vector<type> vt(Vector);
//Ensure vt is as large as the matrix needed:
if(vt.capacity() != colSize )
vt.resize(colSize,0);
for(int i = 0;i < rowCount ;i++){
Matrix[i].resize(rowSize + 1);
Matrix[i][rowSize] = vt[i];
}
rowSize++;
totalElem = rowCount * rowSize;
}
//---------------------------------------------------------------------------
template<class type> void
matrix<type>::resize(const int RowCount, const int ColCount,const Cell& cell)
{
//If either of RowCount or ColCount is a invalid number such as an oppositive
// number or Zero,rowCount and colCount don't changed and this funtion do
//nothing at all:
rowCount = ( RowCount > 0 && ColCount > 0 ) ? RowCount : rowCount;
colCount = ( RowCount > 0 && ColCount > 0 ) ? ColCount : colCount;
if(RowCount <= 0 || ColCount <= 0 ){ }
else{
Matrix.resize(rowCount);
for(int i = 0;i < rowCount;i++){
Matrix[i].resize(rowSize,cell);
}
}
}
//---------------------------------------------------------------------------
template<class type>const int
inline matrix<type>::RowCount() const
{
return rowCount;
}
template<class type>const int
inline matrix<type>::ColCount() const
{
return colCount;
}
//---------------------------------------------------------------------------
//Get associate matrix:
template<class type> matrix<type>&
matrix<type>::associate(){
//transpoesing (*this):
(*this) = ~(*this);
//Conjugating (*this):
for(int i = 0;i<rowCount;i++)
for(int j = 0;j< colCount;j++){
this->SetCells(i,j,conj(this->ReadCells(i,j)));
}
return *this;
}
//---------------------------------------------------------------------------
//Matrix appling
//Rows apply:
template<class type> void
matrix<type>::ApplyRows(const int RowIndex,type fun(type&))
{
if( RowIndex >= rowCount || RowIndex < 0){
CERR("This row doesn't exit!Any key to Quit.");
}
transform(Matrix[RowIndex].begin(),Matrix[RowIndex].end(),
Matrix[RowIndex].begin(),ptr_fun(fun));
}
//Columns apply:
template<class type> void
matrix<type>::ApplyCols(const int ColIndex,type fun(type&))
{
if( ColIndex >= colCount || ColIndex < 0 ){
CERR("This column doesn't exit!Any key to Quit.");
}
vector<type> vt(rowCount);
for(int i = 0;i < rowCount ;i++){
vt[i] = Matrix[i][ColIndex];
}
transform(vt.begin(),vt.end(),vt.begin(),ptr_fun(fun));
for(int i = 0;i < rowCount ;i++){
Matrix[i][ColIndex] = vt[i];
}
}
//---------------------------------------------------------------------------
template<class type> void
matrix<type>::ShrinkToIdentityMatrix()
{
//Shrink matrix to the dimension of the smaller of rowCount and colCount:
rowCount = (rowCount < colCount) ? rowCount : colCount;
for(int i = 0;i < rowCount ;i++){
fill(Matrix[i].begin(),Matrix[i].end(),0);
Matrix[i][i] = 1;
}
}
//---------------------------------------------------------------------------
//Convert matrix to complex double type:
template<class type> matrix<complex_double_type>
matrix<type>::c_complex_double()
{
matrix<complex_double_type> MCD(rowCount,colCount);
complex<double> cm;
for(int i = 0;i< rowCount ;i++)
for(int j = 0;j< colCount ;j++){
cm = Matrix[i][j];
MCD.SetCells(i,j,cm);
}
return MCD;
}
//---------------------------------------------------------------------------
//Convert matrix to double type:
template<class type> matrix<double>
matrix<type>::c_double()
{
matrix<double> MD(rowCount,colCount);
double d;
for(int i = 0;i< rowCount ;i++)
for(int j = 0;j< colCount ;j++){
d = Matrix[i][j];
MD.SetCells(i,j,d);
}
return MD;
}
//---------------------------------------------------------------------------
template<class type> void
matrix<type>::adjust(const int& RowCount,const int& ColCount){
//Terminated if the condition is ture:
if( RowCount * ColCount != totalElem && RowCount < 0)
CERR("Invalid row count or column count!Any key to quit");
matrix<type> M(*this); //store the old matrix
this->resize(RowCount,ColCount);
int m = 0,n = 0;
for(int i = 0;i< M.RowCount() ;i++){
if( m >= rowCount)
break;
for(int j = 0;j< M.ColCount();j++){
if( n >= colCount){
m++;
n = 0;
}
if( m >= rowCount) //for safty.
break;
Matrix[m][n++] = M[i][j];
}
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Member Uniary operators:
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//Get norm value of complex:
double myNorm(complex<double>& C){
return C.real()*C.real() + C.imag()*C.imag();
}
//'!' operator:
template<class type> matrix<complex_double_type>
matrix<type>::operator!()
{
if(rowCount != colCount){
CERR("Sigular matrix!Any key to quit!");
}
int s; // swap line id:
matrix<complex_double_type> Mt(this->c_complex_double()); //Assign *this to complex<double> Mt;
matrix<complex_double_type> I(rowCount,colCount); //Identity Matrix:
I.ShrinkToIdentityMatrix();
complex<double> k; //store complex double value:
vector<double> vt; //store complex norm value:
vector<complex_double_type> vi(colCount); //middle variable
vector<complex_double_type> vj(colCount); //middle variable:
for(int i = 0;i < Mt.ColCount();i++){
vi = Mt.ReadCols(i);
vt.resize(Mt.RowCount() - i); //Reduce the size of vt
//transform the norm values of elements in vi to vt:
transform(vi.begin() + i,vi.end(),vt.begin(),ptr_fun(myNorm));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -