396 lines
8.1 KiB
C++
396 lines
8.1 KiB
C++
#include "genenalfunc.h"
|
|
#include <QFile>
|
|
#include <QCoreApplication>
|
|
using namespace arma;
|
|
|
|
void PrintMat2(QString name, mat& dataMat)
|
|
{
|
|
QFile file("C:/Result_Debug3/" + name);
|
|
if (file.open(QIODevice::WriteOnly))
|
|
{
|
|
QTextStream out(&file);
|
|
if (dataMat.is_vec())
|
|
{
|
|
for (int i = 0; i < dataMat.size(); ++i)
|
|
{
|
|
out << QString::number(dataMat(i), 'g', 10) << "\r\n";
|
|
}
|
|
}
|
|
else {
|
|
for (uword row = 0; row < dataMat.n_rows; ++row)
|
|
{
|
|
for (uword col = 0; col < dataMat.n_cols; ++col)
|
|
{
|
|
out << QString::number(dataMat(row, col), 'g', 10) << "\t";
|
|
}
|
|
out << "\r\n";
|
|
}
|
|
}
|
|
file.close();
|
|
}
|
|
}
|
|
|
|
mat QVec2ToMat(QVector< QVector<double> > vec2)
|
|
{
|
|
mat retMat;
|
|
if(vec2.size() > 0)
|
|
{
|
|
retMat.set_size(vec2[0].size(), vec2.size());
|
|
for(int i=0; i<vec2.size(); ++i)
|
|
{
|
|
for(int j=0; j<vec2[i].size(); ++j)
|
|
{
|
|
retMat(j, i) = vec2[i][j];
|
|
}
|
|
}
|
|
}
|
|
return retMat;
|
|
}
|
|
|
|
bool MatAssign(mat &dataMat, const umat idxMat, const mat valMat)
|
|
{
|
|
if(idxMat.size() != valMat.size())
|
|
{
|
|
qDebug() << "The size of valMat must be same as the size of inxMat";
|
|
return false;
|
|
}
|
|
bool bRet = true;
|
|
uword data_size = dataMat.size();
|
|
for(uword i=0; i<idxMat.size(); ++i)
|
|
{
|
|
if(idxMat(i) < data_size)
|
|
{
|
|
dataMat(idxMat(i)) = valMat(i);
|
|
}
|
|
else {
|
|
bRet = false;
|
|
}
|
|
}
|
|
return bRet;
|
|
}
|
|
|
|
bool MatAssign(mat& dataMat, const umat idxMat, double val)
|
|
{
|
|
bool bRet = true;
|
|
uword data_size = dataMat.size();
|
|
for(uword i=0; i<idxMat.size(); ++i)
|
|
{
|
|
if(idxMat(i) < data_size)
|
|
{
|
|
dataMat(idxMat(i)) = val;
|
|
}
|
|
else {
|
|
bRet = false;
|
|
}
|
|
}
|
|
return bRet;
|
|
}
|
|
|
|
bool MatAssign(mat& dataMat, int start, int end, const mat valMat)
|
|
{
|
|
if(end - start + 1 != valMat.size())
|
|
{
|
|
qDebug() << "The size of valMat must be same as the size of inxMat";
|
|
return false;
|
|
}
|
|
bool bRet = true;
|
|
int data_size = dataMat.size();
|
|
if(start < 0) start = 0;
|
|
if(end >= data_size) end = data_size - 1;
|
|
for(int i=start; i<=end; ++i)
|
|
{
|
|
dataMat(i) = valMat(i);
|
|
}
|
|
return bRet;
|
|
}
|
|
|
|
urowvec RangeVec(int start, int end, int span)
|
|
{
|
|
urowvec retData;
|
|
if(span > 0 && start > end) return retData;
|
|
if(span < 0 && start < end) return retData;
|
|
|
|
int size = floor( (end - start) / span ) + 1;
|
|
retData.set_size(size);
|
|
int temp = start;
|
|
if(span > 0)
|
|
{
|
|
for(uword i = 0; temp<=end; temp+=span, ++i)
|
|
{
|
|
retData(i) = temp;
|
|
}
|
|
} else {
|
|
for(uword i = 0; temp>=end; temp+=span, ++i)
|
|
{
|
|
retData(i) = temp;
|
|
}
|
|
}
|
|
return retData;
|
|
}
|
|
|
|
rowvec RangeVec2(double start, double end, double span)
|
|
{
|
|
rowvec retData;
|
|
if(span > 0 && start > end) return retData;
|
|
if(span < 0 && start < end) return retData;
|
|
|
|
int size = floor( (end - start) / span) + 1;
|
|
retData.set_size(size);
|
|
|
|
double temp = start;
|
|
if(span > 0)
|
|
{
|
|
for(uword i = 0; temp<=end; temp+=span, ++i)
|
|
{
|
|
retData(i) = temp;
|
|
}
|
|
} else {
|
|
for(uword i = 0; temp>=end; temp+=span, ++i)
|
|
{
|
|
retData(i) = temp;
|
|
}
|
|
}
|
|
return retData;
|
|
}
|
|
|
|
uvec sortrows(mat& retMat, mat dataMat, int col)
|
|
{
|
|
uvec idx;
|
|
if(col >= dataMat.n_cols || col < 0) col = 0;
|
|
uword n_row = dataMat.n_rows;
|
|
|
|
idx.set_size(n_row);
|
|
for(uword i=0; i<n_row; ++i) idx(i) = i;
|
|
|
|
retMat.set_size( size(dataMat) );
|
|
uword min, temp;
|
|
for(uword i=0; i<n_row; ++i)
|
|
{
|
|
min = i;
|
|
for(uword j=i+1; j<n_row; ++j)
|
|
{
|
|
if(dataMat(j, col) < dataMat(min, col)) min = j;
|
|
}
|
|
retMat.row(i) = dataMat.row(min);
|
|
if(min != i)
|
|
{
|
|
dataMat.row(min) = dataMat.row(i);
|
|
temp = idx(i);
|
|
idx(i) = idx(min);
|
|
idx(min) = temp;
|
|
}
|
|
}
|
|
return idx;
|
|
}
|
|
|
|
mat MySpan(mat myData, int start, int end, int span)
|
|
{
|
|
mat retData;
|
|
int data_size = myData.size() - 1;
|
|
if(data_size >= 0)
|
|
{
|
|
if(start > data_size) start = data_size;
|
|
if(start < 0) start = 0;
|
|
if(end > data_size) end = data_size;
|
|
if(end < 0) end = 0;
|
|
span = abs(span);
|
|
if(myData.is_colvec())
|
|
{
|
|
retData.set_size(abs(end-start)/span+1, 1);
|
|
}
|
|
else { retData.set_size(1, abs(end-start)/span+1); }
|
|
|
|
if(start <= end)
|
|
{
|
|
for(int i=start, j=0; i<=end; i+=span)
|
|
{
|
|
retData(j++) = myData(i);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for(int i=start, j=0; i>=end; i-=span)
|
|
{
|
|
retData(j++) = myData(i);
|
|
}
|
|
}
|
|
}
|
|
return retData;
|
|
}
|
|
|
|
double min(double d1, double d2)
|
|
{
|
|
return d1 <= d2 ? d1 : d2;
|
|
}
|
|
|
|
double min(double d1, double d2, double d3)
|
|
{
|
|
double minVal = d1 <= d2 ? d1 : d2;
|
|
if(minVal > d3) minVal = d3;
|
|
return minVal;
|
|
}
|
|
|
|
double max(double d1, double d2)
|
|
{
|
|
return d1 >= d2 ? d1 : d2;
|
|
}
|
|
|
|
double max(double d1, double d2, double d3)
|
|
{
|
|
double maxVal = d1 >= d2 ? d1 : d2;
|
|
if(maxVal < d3) maxVal = d3;
|
|
return maxVal;
|
|
}
|
|
|
|
mat max(double val, mat dataMat)
|
|
{
|
|
mat retMat = dataMat;
|
|
for(uword row=0; row<dataMat.n_rows; ++row)
|
|
{
|
|
for(uword col=0; col<dataMat.n_cols; ++col)
|
|
{
|
|
if(dataMat(row, col) < val)
|
|
{
|
|
retMat(row, col) = val;
|
|
}
|
|
}
|
|
}
|
|
return retMat;
|
|
}
|
|
|
|
mat min(double val, mat dataMat)
|
|
{
|
|
mat retMat = dataMat;
|
|
for(uword row=0; row<dataMat.n_rows; ++row)
|
|
{
|
|
for(uword col=0; col<dataMat.n_cols; ++col)
|
|
{
|
|
if(dataMat(row, col) > val)
|
|
{
|
|
retMat(row, col) = val;
|
|
}
|
|
}
|
|
}
|
|
return retMat;
|
|
}
|
|
|
|
mat NaNto1(mat dataMat) // @cao
|
|
{
|
|
mat retMat = dataMat;
|
|
for(uword row=0; row<dataMat.n_rows; ++row)
|
|
{
|
|
for(uword col=0; col<dataMat.n_cols; ++col)
|
|
{
|
|
if(qIsNaN(dataMat(row, col)))
|
|
{
|
|
dataMat(row, col) = 1;
|
|
}
|
|
}
|
|
}
|
|
return retMat;
|
|
}
|
|
|
|
// col is original colvec, idx is position index by logic(the value is 1);MATLAB A(B)
|
|
rowvec partVec(urowvec row, rowvec idx)
|
|
{
|
|
QVector<double> pos;
|
|
for(size_t i=0; i!=idx.size(); ++i)
|
|
{
|
|
if(idx(i))
|
|
{
|
|
pos.append(i);
|
|
}
|
|
}
|
|
rowvec ret(pos.size());
|
|
for(size_t i = 0; i!= pos.size(); ++i)
|
|
{
|
|
ret(i) = row(pos[i]);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
mat UMatToMat(umat oriMat)
|
|
{
|
|
mat retMat(size(oriMat));
|
|
for(uword i=0; i<retMat.size(); ++i)
|
|
{
|
|
retMat(i) = oriMat(i);
|
|
}
|
|
return retMat;
|
|
}
|
|
|
|
void find(uvec &I, uvec &J, vec& V, mat A)
|
|
{
|
|
uword s = 0;
|
|
for(uword j=0; j<A.n_cols; ++j)
|
|
{
|
|
for(uword i=0; i<A.n_rows; ++i)
|
|
{
|
|
if(A(i,j) != 0)
|
|
{
|
|
I.resize(s+1);
|
|
J.resize(s+1);
|
|
V.resize(s+1);
|
|
I(s) = i;
|
|
J(s) = j;
|
|
V(s) = A(i,j);
|
|
s++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
umat IsInf(mat d)
|
|
{
|
|
umat retMat( size(d) );
|
|
for(uword i=0; i<d.size(); ++i)
|
|
{
|
|
if( is_finite( d(i) ) ) retMat(i) = 0u;
|
|
else retMat(i) = 1u;
|
|
}
|
|
return retMat;
|
|
}
|
|
|
|
umat IsFinite(mat d)
|
|
{
|
|
umat retMat( size(d) );
|
|
for(uword i=0; i<d.size(); ++i)
|
|
{
|
|
if( is_finite( d(i) ) ) retMat(i) = 1u;
|
|
else retMat(i) = 0u;
|
|
}
|
|
return retMat;
|
|
}
|
|
|
|
umat OrUMat(umat a, umat b)
|
|
{
|
|
umat retMat;
|
|
if(size(a) != size(b)) return retMat;
|
|
retMat.set_size(size(a));
|
|
for(uword i=0; i<retMat.size(); ++i)
|
|
{
|
|
retMat(i) = a(i) | b(i);
|
|
}
|
|
return retMat;
|
|
}
|
|
|
|
umat AndUMat(umat a, umat b)
|
|
{
|
|
umat retMat;
|
|
if(size(a) != size(b)) return retMat;
|
|
retMat.set_size(size(a));
|
|
for(uword i=0; i<retMat.size(); ++i)
|
|
{
|
|
retMat(i) = a(i) & b(i);
|
|
}
|
|
return retMat;
|
|
}
|
|
|
|
int sign(double d)
|
|
{
|
|
int iRet = 0;
|
|
if(d < 0) iRet = -1;
|
|
else if(d > 0) iRet = 1;
|
|
return iRet;
|
|
}
|