AnalysisSystemForRadionucli.../genenalfunc.h
2024-06-04 15:25:02 +08:00

112 lines
3.2 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef GENENALFUNC
#define GENENALFUNC
#include "gamma_alg_global.h"
arma::mat QVec2ToMat(QVector<QVector<double> > vec2);
arma::umat IsInf(arma::mat d);
arma::umat IsFinite(arma::mat d);
// 矩阵索引 相当于 Matlab 的 mat(mat) 操作;
template<typename T, typename T2>
T IdxMat(const T dataMat, const T2 idxMat)
{
//bool Ok = true;
arma::uword data_size = dataMat.n_elem;
T retMat;
if(dataMat.is_vec())
{
retMat.set_size(idxMat.size());
} else {
retMat.set_size(size(idxMat));
}
for(arma::uword i=0; i<retMat.size(); i++)
{
if(idxMat(i) < data_size)
{
retMat(i) = dataMat(idxMat(i));
}
else { retMat(i) = 0; }
}
//bOk = &Ok;
return retMat;
}
template<typename T>
arma::mat MatCols(const arma::mat dataMat, const T idxMat)
{
arma::mat retMat(dataMat.n_rows, idxMat.size());
int cols = dataMat.n_cols;
for(int i=0; i<idxMat.size(); i++)
{
if(idxMat(i) < cols)
{
retMat.col(i) = dataMat.col(idxMat(i));
}
}
return retMat;
}
template<typename T>
arma::mat MatRows(const arma::mat dataMat, const T idxMat)
{
arma::mat retMat(idxMat.size(), dataMat.n_cols);
int rows = dataMat.n_rows;
for(int i=0; i<idxMat.size(); i++)
{
if(idxMat(i) < rows)
{
retMat.row(i) = dataMat.row(idxMat(i));
}
}
return retMat;
}
arma::umat OrUMat(arma::umat a, arma::umat b);
arma::umat AndUMat(arma::umat a, arma::umat b);
// 找出矩阵A中所有非零值的行下标存在向量I中和列下标存在向量J中
void find(arma::uvec& I, arma::uvec& J, arma::vec &V, arma::mat A);
/* 矩阵部分赋值
* @param dataMat: 被赋值的矩阵
* @param idxMat: 下标索引矩阵
* @param valMat: 存储值的矩阵该矩阵的值将被赋给dataMat矩阵中相应的元素
*/
bool MatAssign(arma::mat& dataMat, const arma::umat idxMat, const arma::mat valMat);
bool MatAssign(arma::mat& dataMat, const arma::umat idxMat, double val);
bool MatAssign(arma::mat& dataMat, int start, int end, const arma::mat valMat);
/* 可代替 Matlab 的 start:end) 或 start:end
* 生成一个数据从 start 递增递增量为1到 end 且大于等于 start 小于等于 end 的行向量 */
arma::urowvec RangeVec(int start, int end, int span = 1);
arma::rowvec RangeVec2(double start, double end, double span = 1);
// 对矩阵依据指定列按行排序相当于Matlab的 sortrowsmatcol),但功能比较少
arma::uvec sortrows(arma::mat& retMat, arma::mat dataMat, int col = 0);
/* T 使用 rowvec 或 colvec
* 可代替 Matlab 的 [start: span: end] 操作符 和 [start: end] 操作符,注意传参顺序 */
arma::mat MySpan(arma::mat myData, int start = 0, int end = -1, int span = 1);
arma::mat UMatToMat(arma::umat oriMat);
double min(double d1, double d2);
double min(double d1, double d2, double d3);
double max(double d1, double d2);
double max(double d1, double d2, double d3);
arma::mat max(double val, arma::mat dataMat);
arma::mat min(double val, arma::mat dataMat);
// 把矩阵中非数值到元素置为1
arma::mat NaNto1(arma::mat dataMat);
arma::rowvec partVec(arma::urowvec col, arma::rowvec idx);
int sign(double d);
#endif // GENENALFUNC