這個類資料型別是double,包含了常用的矩陣計算,多數方法經過實踐驗證,也難免有不足之處,如有發現歡迎指出。
#include #include #include #include using namespace std;
#ifndef _in_opt_
#define _in_opt_
#endif
#ifndef _out_
#define _out_
#endif
typedef unsigned index_t;
class matrix
else
m_ptr = null;
};matrix(index_t r, index_t c, double val ) :m_row(r), m_col(c)// 賦初值val
else
m_ptr = null;
};matrix(index_t n) :m_row(n), m_col(n)//方陣構造
else
m_ptr = null;
};matrix(const matrix &rhs)//拷貝構造
index_t col()const
matrix getrow(index_t index); // 返回第index 行,索引從0 算起
matrix getcol(index_t index); // 返回第index 列
matrix cov(_in_opt_ bool flag = true); //協方差陣 或者樣本方差
double det(); //行列式
matrix solveab(matrix &obj); // b是行向量或者列向量
matrix diag(); //返回對角線元素
//matrix asigndiag(); //對角線元素
matrix t()const; //轉置
void sort(bool);//true為從小到大
matrix adjoint();
matrix inverse();
void qr(_out_ matrix&, _out_ matrix&)const;
matrix eig_val(_in_opt_ index_t _iters = 1000);
matrix eig_vect(_in_opt_ index_t _iters = 1000);
double norm1();//1範數
double norm2();//2範數
double mean();// 矩陣均值
double*operator(index_t i)//注意this加括號, (*this)[i][j]
void zeromean(_in_opt_ bool flag = true);//預設引數為true計算列
void normalize(_in_opt_ bool flag = true);//預設引數為true計算列
matrix exponent(double x);//每個元素x次冪
matrix eye();//對角陣
void maxlimit(double max,double set=0);//對角陣
};/*
類方法的實現
*/matrix matrix::mtanh() // 對應元素 tanh()
}}matrix matrix::eye()//對角陣}}
return *this;
}void matrix::zeromean(_in_opt_ bool flag)
mean[j] /= m_row;
}for (index_t j = 0; j < m_col; j++)
}deletemean;
}else //計算行均值
mean[i] /= m_col;
}for (index_t i = 0; i < m_row; i++)
}deletemean;
}}void matrix::normalize(_in_opt_ bool flag)
mean[j] /= m_row;
}for (index_t j = 0; j < m_col; j++)
}///計算標準差
for (index_t j = 0; j < m_col; j++)
mean[j] = sqrt(mean[j] / m_row); // 開方
}for (index_t j = 0; j < m_col; j++)
}deletemean;
}else //計算行均值
mean[i] /= m_col;
}for (index_t i = 0; i < m_row; i++)
}///計算標準差
for (index_t i = 0; i< m_row; i++)
mean[i] = sqrt(mean[i] / m_col); // 開方
}for (index_t i = 0; i < m_row; i++)
}deletemean;
}}double matrix::det()}/
istream& operator>>(istream &is, matrix &obj)
return is;
}ostream& operator<<(ostream &out, matrix &obj)
out << endl;
}return out;
}ofstream& operator<<(ofstream &out, matrix &obj)//列印逆矩陣到檔案
out << endl;
}return out;
}matrix& operator<<(matrix &obj, const double val)
matrix& operator,(matrix &obj, const double val)
*(obj.m_ptr + obj.m_curindex) = val;
++obj.m_curindex;
return obj;
}matrix operator+(const matrix& lm, const matrix& rm)
matrix ret(lm.m_row, lm.m_col);
for (index_t i = 0; im_ptr[j])
}else
sum = -sum / m_ptr[m * m_col + m];
midsum += sum * sum;
ret.m_ptr[m * ret.m_col + count] = sum;
}midsum = sqrt(midsum);
for (index_t i = 0; i < ret.m_row; ++i)
}*this = matcopy;//恢復原始矩陣;
return ret;
}matrix matrix::cov(bool flag)
double *mean = new double[m_col]; //均值向量
for (index_t j = 0; jmatrix ret(m_col);
for (index_t j = 0; jmean[j] /= m_row;
}index_t i, k, j;
for (i = 0; iif (flag == true)
else}}
for (i = 0; i}
return ret;}/*
* 返回代數余子式
*/double calcalgebraiccofactor( matrix& srcmat, index_t ai, index_t aj)
}int flag = (ai + aj) % 2 == 0 ? 1 : -1;
return flag * temmat.det();}/*
* 返回伴隨陣
*/matrix matrix::adjoint()
matrix adjointmat(m_row);
for (index_t ai = 0; ai < m_row; ai++)
}return adjointmat;
}matrix matrix::inverse()
return adjoint()/detofmat;
}
C 過載矩陣類運算子
過載 運算子 矩陣 includeusing namespace std templateclass matrix templatematrix matrix int n,int m templatematrix matrix const matrix mat templatematrix matr...
CMatrix類 矩陣類 C
pragma once class matrix 返回行 intcol const 返回列 void setsize int row,int col 調整陣列的大小,原有資料不變 未測試 double operator int row,int col 獲取矩陣元素 double operator i...
C 矩陣運算庫推薦
最近在幾個地方都看到有人問c 下用什麼矩陣運算庫比較好,順便做了個調查,做一些相關的推薦吧。主要針對稠密矩陣,有時間會再寫乙個稀疏矩陣的推薦。許可證 mpl 2.0 目前使用比較廣的c 矩陣運算庫之一,是在c 下使用matlab方式操作矩陣很好的選擇,許多matlab的矩陣操作函式都可以找到對應,這...