📄 core.inl
字号:
Vector<T> dummy(m.Columns());
for(int i=0; i<m.Rows(); i++)
{
for(int j=0; j<m.Columns(); j++)
{
dummy.ElemNC(j) = m.ElemNC(i,j);
}
QuickSort(dummy);
for(int j=0; j<m.Columns(); j++)
{
m.ElemNC(i,j) = dummy.ElemNC(j);
}
}
}
else
{
cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
Utility::RunTimeError("dimension should be either 1 or 2 for matrices!");
}
return m;
}
template <class T>
Matrix<T>& SortI(Matrix<T>& m)
{
return SortI(m,1);
}
template <class T>
Vector<T> Abs(Vector<T>& m)
{
Vector<T> dummy(m.Numel());
for(int i=0; i<m.Numel(); i++)
{
dummy.ElemNC(i) = abs(m.ElemNC(i));
}
return dummy;
}
template <class T>
Vector<T>& AbsI(Vector<T>& m)
{
for(int i=0; i<m.Numel(); i++)
{
m.ElemNC(i) = abs(m.ElemNC(i));
}
return m;
}
template <class T>
Matrix<T> Abs(Matrix<T>& m)
{
Matrix<T> dummy(m.Rows(), m.Columns());
for(int i=0; i<m.Numel(); i++)
{
dummy.ElemNC(i) = abs(m.ElemNC(i));
}
return dummy;
}
template <class T>
Matrix<T>& AbsI(Matrix<T>& m)
{
for(int i=0; i<m.Numel(); i++)
{
m.ElemNC(i) = abs(m.ElemNC(i));
}
return m;
}
template <class T>
void MeshGrid(Vector<T>& x, Vector<T>& y, Matrix<T>& X, Matrix<T>& Y)
{
X = Matrix<T>(y.Length(), x.Length());
Y = Matrix<T>(y.Length(), x.Length());
for(int i=0; i<y.Length(); i++)
{
for(int j=0; j<x.Length(); j++)
{
X.ElemNC(i,j) = x(j);
Y.ElemNC(i,j) = y(i);
}
}
}
template <class T>
Vector<T> UnitScale(Vector<T>& m)
{
T max = Maximum(m));
T min = Minimum(m));
return (m - min)/(max-min);
}
template <class T>
Matrix<T> UnitScale(Matrix<T>& m)
{
T max = Maximum(m(":"));
T min = Minimum(m(":"));
return (m - min)/(max-min);
}
template <class T>
Vector<T>& UnitScaleI(Vector<T>& m)
{
T max = Maximum(m);
T min = Minimum(m);
m -= min;
m /= (max-min);
return m;
}
template <class T>
Matrix<T>& UnitScaleI(Matrix<T>& m)
{
T max = Maximum(m(":"));
T min = Minimum(m(":"));
m -= min;
m /= (max-min);
return m;
}
template <class T>
Vector<T> ImageScale(Vector<T>& m)
{
T max = Maximum(m));
T min = Minimum(m));
return (m - min)*255/(max-min);
}
template <class T>
Matrix<T> ImageScale(Matrix<T>& m)
{
T max = Maximum(m(":"));
T min = Minimum(m(":"));
return (m - min)*255/(max-min);
}
template <class T>
Vector<T>& ImageScaleI(Vector<T>& m)
{
T max = Maximum(m);
T min = Minimum(m);
m -= min;
m *= 255;
m /= (max-min);
return m;
}
template <class T>
Matrix<T>& ImageScaleI(Matrix<T>& m)
{
T max = Maximum(m(":"));
T min = Minimum(m(":"));
m -= min;
m *= 255;
m /= (max-min);
return m;
}
template <class T>
Vector<int> Sign(Vector<T>& m)
{
Vector<int> temp(m.Length());
for(int i=0; i<m.Length(); i++)
{
if(m.ElemNC(i) > 0)
{
temp.ElemNC(i) = 1;
}
else if(m.ElemNC(i) < 0)
{
temp.ElemNC(i) = -1;
}
else
{
temp.ElemNC(i) = 0;
}
}
return temp;
}
template <class T>
Matrix<int> Sign(Matrix<T>& m)
{
Matrix<int> temp(m.Rows(), m.Columns());
for(int i=0; i<m.Length(); i++)
{
if(m.ElemNC(i) > 0)
{
temp.ElemNC(i) = 1;
}
else if(m.ElemNC(i) < 0)
{
temp.ElemNC(i) = -1;
}
else
{
temp.ElemNC(i) = 0;
}
}
return temp;
}
template <class T>
Vector<T> Gradient(Vector<T>& m)
{
Vector<T> temp(m.Length());
for(int i=1; i<m.Length()-1; i++)
{
temp.ElemNC(i) = (m.ElemNC(i+1)-m.ElemNC(i-1))/2;
}
temp.ElemNC(0) = m.ElemNC(1)-m.ElemNC(0);
temp.ElemNC(m.Length()-1) = m.ElemNC(m.Length()-1)-m.ElemNC(m.Length()-2);
return temp;
}
template <class T>
void Gradient(Matrix<T>& m, Matrix<T>& X, Matrix<T>& Y)
{
X = Matrix<T>(m.Rows(), m.Columns());
Y = Matrix<T>(m.Rows(), m.Columns());
for(int j=1; j<m.Columns()-1; j++)
{
for(int i=0; i<m.Rows(); i++)
{
X.ElemNC(i,j) = (m.ElemNC(i,j+1)-m.ElemNC(i,j-1))/2;
}
}
for(int i=0; i<m.Rows(); i++)
{
X.ElemNC(i,0) = m.ElemNC(i,1)-m.ElemNC(i,0);
X.ElemNC(i,m.Columns()-1) = m.ElemNC(i,m.Columns()-1)-m.ElemNC(i,m.Columns()-2);
}
for(int j=0; j<m.Columns(); j++)
{
for(int i=1; i<m.Rows()-1; i++)
{
Y.ElemNC(i,j) = (m.ElemNC(i+1,j)-m.ElemNC(i-1,j))/2;
}
}
for(int j=0; j<m.Columns(); j++)
{
Y.ElemNC(0,j) = m.ElemNC(1,j)-m.ElemNC(0,j);
Y.ElemNC(m.Rows()-1,j) = m.ElemNC(m.Rows()-1,j)-m.ElemNC(m.Rows()-2,j);
}
}
template <class T>
Matrix<T> Divergence(Matrix<T>& u, Matrix<T>& v)
{
if(u.Rows() != v.Rows() || u.Columns() != v.Columns())
{
cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
Utility::RunTimeError("Matrix sizes are not the same!");
}
Matrix<T> temp(u.Rows(), u.Columns(), 0);
for(int j=1; j<u.Columns()-1; j++)
{
for(int i=0; i<u.Rows(); i++)
{
temp.ElemNC(i,j) += (u.ElemNC(i,j+1)-u.ElemNC(i,j-1))/2;
}
}
for(int i=0; i<u.Rows(); i++)
{
temp.ElemNC(i,0) += u.ElemNC(i,1)-u.ElemNC(i,0);
temp.ElemNC(i,u.Columns()-1) += u.ElemNC(i,u.Columns()-1)-u.ElemNC(i,u.Columns()-2);
}
for(int j=0; j<v.Columns(); j++)
{
for(int i=1; i<v.Rows()-1; i++)
{
temp.ElemNC(i,j) += (v.ElemNC(i+1,j)-v.ElemNC(i-1,j))/2;
}
}
for(int j=0; j<v.Columns(); j++)
{
temp.ElemNC(0,j) += v.ElemNC(1,j)-v.ElemNC(0,j);
temp.ElemNC(v.Rows()-1,j) += v.ElemNC(v.Rows()-1,j)-v.ElemNC(v.Rows()-2,j);
}
return temp;
}
template <class T>
Vector<T> Del2(Vector<T>& m)
{
Vector<T> temp(m.Length());
if(m.Length()>3)
{
for(int i=1; i<m.Length()-1; i++)
{
temp.ElemNC(i) = (m.ElemNC(i+1) - 2*m.ElemNC(i) + m.ElemNC(i-1))/4;
}
temp.ElemNC(0) = 2*temp.ElemNC(1)-temp.ElemNC(2);
temp.ElemNC(m.Length()-1) = 2*temp.ElemNC(m.Length()-2)-temp.ElemNC(m.Length()-3);
}
else if(m.Length()==3)
{
temp.ElemNC(1) = (m.ElemNC(2) - 2*m.ElemNC(1) + m.ElemNC(0))/4;
temp.ElemNC(0) = temp.ElemNC(1);
temp.ElemNC(2) = temp.ElemNC(1);
}
else
{
temp.ElemNC(0) = 0;
temp.ElemNC(m.Length()-1) = 0;
}
return temp;
}
template <class T>
Matrix<T> Del2(Matrix<T>& m)
{
Matrix<T> temp(m.Rows(), m.Columns(),0);
Vector<T> temp2(m.Columns());
for(int i=0; i<m.Rows(); i++)
{
if(m.Columns()>3)
{
for(int j=1; j<m.Columns()-1; j++)
{
temp2.ElemNC(j) = (m.ElemNC(i,j+1) - 2*m.ElemNC(i,j) + m.ElemNC(i,j-1))/4;
}
temp2.ElemNC(0) = 2*temp2.ElemNC(1)-temp2.ElemNC(2);
temp2.ElemNC(m.Columns()-1) = 2*temp2.ElemNC(m.Columns()-2)-temp2.ElemNC(m.Columns()-3);
for(int j=0; j<m.Columns(); j++)
{
temp.ElemNC(i,j) += temp2.ElemNC(j);
}
}
else if(m.Columns()==3)
{
T dummy = (m.ElemNC(i,2) - 2*m.ElemNC(i,1) + m.ElemNC(i,0))/4;
temp.ElemNC(i,0) += dummy;
temp.ElemNC(i,1) += dummy;
temp.ElemNC(i,2) += dummy;
}
}
Vector<T> temp3(m.Rows());
for(int j=0; j<m.Columns(); j++)
{
if(m.Rows()>3)
{
for(int i=1; i<m.Rows()-1; i++)
{
temp3.ElemNC(i) = (m.ElemNC(i+1,j) - 2*m.ElemNC(i,j) + m.ElemNC(i-1,j))/4;
}
temp3.ElemNC(0) = 2*temp3.ElemNC(1)-temp3.ElemNC(2);
temp3.ElemNC(m.Rows()-1) = 2*temp3.ElemNC(m.Rows()-2)-temp3.ElemNC(m.Rows()-3);
for(int i=0; i<m.Rows(); i++)
{
temp.ElemNC(i,j) += temp3.ElemNC(i);
}
}
else if(m.Rows()==3)
{
T dummy = (m.ElemNC(2,j) - 2*m.ElemNC(1,j) + m.ElemNC(0,j))/4;
temp.ElemNC(0,j) += dummy;
temp.ElemNC(1,j) += dummy;
temp.ElemNC(2,j) += dummy;
}
}
return temp;
}
template <class T>
Vector<T> CircShift(Vector<T>& m, int shift)
{
Vector<T> temp(m.Length());
for(int i=0; i<m.Length(); i++)
{
int ind = (i-shift) % m.Length();
ind = ind >=0 ? ind : ind+m.Length();
temp.ElemNC(i) = m.ElemNC(ind);
}
return temp;
}
template <class T>
Matrix<T> CircShift(Matrix<T>& m, int rshift, int cshift)
{
Matrix<T> temp(m.Rows(), m.Columns());
for(int i=0; i<m.Rows(); i++)
{
int rind = (i-rshift) % m.Rows();
rind = rind >=0 ? rind : rind+m.Rows();
for(int j=0; j<m.Columns(); j++)
{
int cind = (j-cshift) % m.Columns();
cind = cind >=0 ? cind : cind+m.Columns();
temp.ElemNC(i,j) = m.ElemNC(rind,cind);
}
}
return temp;
}
template <class T>
Vector<T> FFTShift(Vector<T>& m)
{
return CircShift(m, m.Length()/2);
}
template <class T>
Matrix<T> FFTShift(Matrix<T>& m)
{
return CircShift(m, -m.Rows()/2, -m.Columns()/2);
}
template <class T>
Vector<T> IFFTShift(Vector<T>& m)
{
return CircShift(m, m.Length()/2);
}
template <class T>
Matrix<T> IFFTShift(Matrix<T>& m)
{
return CircShift(m, -m.Rows()/2, -m.Columns()/2);
}
template <class T>
Vector<int> Floor(Vector<T>& m)
{
Vector<int> temp(m.Length());
for(int i=0; i<m.Length(); i++)
{
temp.ElemNC(i) = (int)floor(m.ElemNC(i));
}
return temp;
}
template <class T>
Matrix<int> Floor(Matrix<T>& m)
{
Matrix<int> temp(m.Length());
for(int i=0; i<m.Length(); i++)
{
temp.ElemNC(i) = (int)floor(m.ElemNC(i));
}
return temp;
}
template <class T>
Vector<int> Ceil(Vector<T>& m)
{
Vector<int> temp(m.Length());
for(int i=0; i<m.Length(); i++)
{
temp.ElemNC(i) = (int)ceil(m.ElemNC(i));
}
return temp;
}
template <class T>
Matrix<int> Ceil(Matrix<T>& m)
{
Matrix<int> temp(m.Length());
for(int i=0; i<m.Length(); i++)
{
temp.ElemNC(i) = (int)ceil(m.ElemNC(i));
}
return temp;
}
template <class T>
Vector<int> Fix(Vector<T>& m)
{
Vector<int> temp(m.Length());
for(int i=0; i<m.Length(); i++)
{
temp.ElemNC(i) = (m.ElemNC(i) >= 0) ? (int)floor(m.ElemNC(i)) : (int)ceil(m.ElemNC(i));
}
return temp;
}
template <class T>
Matrix<int> Fix(Matrix<T>& m)
{
Matrix<int> temp(m.Length());
for(int i=0; i<m.Length(); i++)
{
temp.ElemNC(i) = (m.ElemNC(i) >= 0) ? (int)floor(m.ElemNC(i)) : (int)ceil(m.ElemNC(i));
}
return temp;
}
template <class T>
Vector<int> Round(Vector<T>& m)
{
Vector<int> temp(m.Length());
for(int i=0; i<m.Length(); i++)
{
temp.ElemNC(i) = (int)floor(m.ElemNC(i)+0.5);
}
return temp;
}
template <class T>
Matrix<int> Round(Matrix<T>& m)
{
Matrix<int> temp(m.Length());
for(int i=0; i<m.Length(); i++)
{
temp.ElemNC(i) = (int)floor(m.ElemNC(i)+0.5);
}
return temp;
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -