Structural deformable models
Functions
dmutil Namespace Reference

Functions

template<class T >
DMatrix< T >::EMT convert (const DMatrix< T > &dmat)
 
template<class T >
DMatrix< T > convert (const typename DMatrix< T >::EMT &mat)
 
template<class T >
DMatrix< T > & invert (DMatrix< T > &dmat)
 
template<class T >
DMatrix< T > inverse (const DMatrix< T > &dmat)
 
template<class T >
bool SVD (const DMatrix< T > &dmat, DMatrix< T > &mU, DMatrix< T > &mS, DMatrix< T > &mV)
 
template<class T >
DMatrix< T > pseudoInv (const DMatrix< T > &dmat)
 
template<class T >
DMatrix< T > makeDiag (const DMatrix< T > &diag)
 
template<class T >
DMatrix< T > makeIdentity (dword sxy, const T &diag=1)
 
template<class T >
DMatrix< T > makeUpper (dword sxy, bool filldiag=true, const T &val=1)
 
template<class T >
DMatrix< T > makeLower (dword sxy, bool filldiag=true, const T &val=1)
 
template<class T >
DMatrix< T > makeRotMat2D (const float &angle)
 
template<class T >
DMatrix< T > horizCat (const DMatrix< T > &lm, const DMatrix< T > &rm)
 
template<class T >
DMatrix< T > vertCat (const DMatrix< T > &um, const DMatrix< T > &lm)
 
template<class T >
DMatrix< T > sum (const DMatrix< T > &mat)
 
template<class T >
DMatrix< T > & sqr (DMatrix< T > &mat)
 
template<class T >
DMatrix< T > & sqrt (DMatrix< T > &mat)
 
template<class T >
DMatrix< T > avg (const DMatrix< T > &mat)
 
template<class T >
DMatrix< T > var (const DMatrix< T > &mat)
 
template<class T >
DMatrix< T > stdev (const DMatrix< T > &mat)
 
template<class T >
DMatrix< T > expand (const DMatrix< T > &mat, dword mx, dword my)
 
template<class T >
DMatrix< T > & threshold (DMatrix< T > &mat, const T &th, const T &repl=T(0))
 
template<class T >
DMatrix< T > & abs (DMatrix< T > &mat)
 

Function Documentation

template<class T >
DMatrix<T>& dmutil::abs ( DMatrix< T > &  mat)

Definition at line 132 of file DMatrixUtil.h.

References DMatrix< T >::getData().

Referenced by Model::addNoise(), MStructure::buildMasterModel(), Brain::doCommand(), Model::getQualityOfFit(), VVector< float, 4 >::majorComponent(), ImageWindow::onKeyPress(), ImageWindow::onRightMouseClick(), and Brain::triggerTest().

133  {
134  for(typename DMatrix<T>::iterator mi = mat.getData().begin();
135  mi != mat.getData().end(); mi++)
136  if(*mi<T(0)) *mi = -*mi;
137  return mat;
138  }
std::vector< T >::iterator iterator
Definition: DMatrix.h:16
std::vector< T > & getData()
Definition: DMatrix.h:48
template<class T >
DMatrix<T> dmutil::avg ( const DMatrix< T > &  mat)

Definition at line 90 of file DMatrixUtil.h.

References DMatrix< T >::sizeY(), and sum().

Referenced by MStructure::addExpectation(), SubStructure::analyseTF(), Sensor::calcMinMax(), EMDGauss::setAvg(), MStructure::showStats(), var(), and VVector< float, 4 >::var().

91  {
92  DMatrix<T> res(sum(mat));
93  return res/=T(mat.sizeY());
94  }
DMatrix< T > sum(const DMatrix< T > &mat)
Definition: DMatrixUtil.h:59
dword sizeY() const
Definition: DMatrix.h:43
template<class T >
DMatrix<T>::EMT dmutil::convert ( const DMatrix< T > &  dmat)

Definition at line 17 of file DMatrixLinAlg.h.

References DMatrix< T >::getData(), DMatrix< T >::sizeX(), and DMatrix< T >::sizeY().

18  {
19  Eigen::Map<const typename DMatrix<T>::EMT> mat(&*dmat.getData().begin(), (int)dmat.sizeY(), (int)dmat.sizeX());
20  //typename DMatrix<T>::const_iterator dat = dmat.getData().begin();
21  //for(int k=dmat.size(); k>0; k--, dat++)
22  // mat << *dat;
23  //linalg::Matrix eth(mat);
24  // TODO: translate this code from LinAlg to Eigen
25  //for(register int i=eth.q_row_lwb(); i<=eth.q_row_upb(); i++)
26  // for(register int j=eth.q_col_lwb(); j<=eth.q_col_upb(); j++)
27  // eth(i,j) = dmat.at(j-1,i-1);
28 
29  return mat;
30  }
dword sizeX() const
Definition: DMatrix.h:42
dword sizeY() const
Definition: DMatrix.h:43
std::vector< T > & getData()
Definition: DMatrix.h:48
template<class T >
DMatrix<T> dmutil::convert ( const typename DMatrix< T >::EMT &  mat)

Definition at line 33 of file DMatrixLinAlg.h.

34  {
35  typename DMatrix<T>::EMT cmat(mat);
36  typename DMatrix<T>::EMT eth(cmat);
37  DMatrix<T> dmat(mat.cols(), mat.rows(), &mat(0));
38  // (eth.q_col_upb()-eth.q_col_lwb()+1, // TODO
39  // eth.q_row_upb()-eth.q_row_lwb()+1);
40  //typename DMatrix<T>::iterator dat = dmat.getData().begin();
41  //for(register int i=eth.q_row_lwb(); i<=eth.q_row_upb(); i++)
42  // for(register int j=eth.q_col_lwb(); j<=eth.q_col_upb(); j++)
43  // dmat.at(j-1,i-1) = eth(i,j);
44  return dmat;
45  }
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > EMT
Definition: DMatrix.h:20
template<class T >
DMatrix<T> dmutil::expand ( const DMatrix< T > &  mat,
dword  mx,
dword  my 
)

Definition at line 113 of file DMatrixUtil.h.

References DMatrix< T >::setRange(), DMatrix< T >::sizeX(), and DMatrix< T >::sizeY().

Referenced by SubStructure::analyseTF().

114  {
115  DMatrix<T> res(mat.sizeX()*mx, mat.sizeY()*my);
116  for(dword y=0; y<my; y++)
117  for(dword x=0; x<mx; x++)
118  res.setRange(mat.sizeX()*x, mat.sizeY()*y, mat);
119  return res;
120  }
dword sizeX() const
Definition: DMatrix.h:42
unsigned long dword
Definition: simpletypes.h:6
MT & setRange(dword ox, dword oy, const MT &mat)
Definition: DMatrix.h:185
dword sizeY() const
Definition: DMatrix.h:43
template<class T >
DMatrix<T> dmutil::horizCat ( const DMatrix< T > &  lm,
const DMatrix< T > &  rm 
)

Definition at line 39 of file DMatrixUtil.h.

References DMatrix< T >::setRange(), DMatrix< T >::sizeX(), and DMatrix< T >::sizeY().

40  {
41  assert(lm.sizeY() == rm.sizeY());
42  DMatrix<T> res(lm.sizeX()+rm.sizeX(), lm.sizeY());
43  res.setRange(0,0, lm);
44  res.setRange(lm.sizeX(),0, rm);
45  return res;
46  }
dword sizeX() const
Definition: DMatrix.h:42
MT & setRange(dword ox, dword oy, const MT &mat)
Definition: DMatrix.h:185
dword sizeY() const
Definition: DMatrix.h:43
template<class T >
DMatrix<T> dmutil::inverse ( const DMatrix< T > &  dmat)

Definition at line 67 of file DMatrixLinAlg.h.

References invert().

Referenced by SubStructure::analyseTF(), MStructure::getExpMap(), and EMDXformer::setTFMat().

68  {
69  DMatrix<T> imat(dmat);
70  return invert(imat);
71  }
DMatrix< T > & invert(DMatrix< T > &dmat)
Definition: DMatrixLinAlg.h:48
template<class T >
DMatrix<T>& dmutil::invert ( DMatrix< T > &  dmat)

Definition at line 48 of file DMatrixLinAlg.h.

References DMatrix< T >::empty().

Referenced by inverse().

49  {
50  bool m_Verbose = false;
51  if(!dmat.empty()) {
52  try {
53  //if(!m_Verbose) freopen("/dev/null","a+",stderr);
54  typename DMatrix<T>::EMT mat = convert<T>(dmat);
55  //dmat = convert<T>(linalg::inverse(mat)); // TODO
56  dmat = convert<T>(mat.inverse());
57  } catch(void*)
58  {
59  //if(!m_Verbose) freopen("/dev/stdout","a+",stderr);
60  }
61  //if(!m_Verbose) freopen("/dev/stdout","a+",stderr);
62  }
63  return dmat;
64  }
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > EMT
Definition: DMatrix.h:20
bool empty() const
Definition: DMatrix.h:50
template<class T >
DMatrix<T> dmutil::makeDiag ( const DMatrix< T > &  diag)

Definition at line 6 of file DMatrixUtil.h.

References DMatrix< T >::at(), DMatrix< T >::getData(), and DMatrix< T >::size().

7  {
8  dword sxy = diag.size();
9  DMatrix<T> mat(sxy, sxy, T(0));
10  typename DMatrix<T>::const_iterator di = diag.getData().begin();
11  for(dword i = 0; i < sxy; i++, di++) mat.at(i,i) = *di;
12  return mat;
13  }
unsigned long dword
Definition: simpletypes.h:6
std::vector< T >::const_iterator const_iterator
Definition: DMatrix.h:15
dword size() const
Definition: DMatrix.h:44
std::vector< T > & getData()
Definition: DMatrix.h:48
template<class T >
DMatrix<T> dmutil::makeIdentity ( dword  sxy,
const T &  diag = 1 
)

Definition at line 16 of file DMatrixUtil.h.

16  {
17  return DMatrix<T>(sxy, sxy, T(0)).setDiag(diag);
18  }
template<class T >
DMatrix<T> dmutil::makeLower ( dword  sxy,
bool  filldiag = true,
const T &  val = 1 
)

Definition at line 26 of file DMatrixUtil.h.

26  {
27  return DMatrix<T>(sxy, sxy).setLower(val);
28  }
template<class T >
DMatrix<T> dmutil::makeRotMat2D ( const float &  angle)

Definition at line 31 of file DMatrixUtil.h.

References DMatrix< T >::at().

31  {
32  DMatrix<T> mat(2,2);
33  mat.at(0,0) = (T)cos(angle); mat.at(1,0) = (T)-sin(angle);
34  mat.at(0,1) = (T)sin(angle); mat.at(1,1) = (T)cos(angle);
35  return mat;
36  }
template<class T >
DMatrix<T> dmutil::makeUpper ( dword  sxy,
bool  filldiag = true,
const T &  val = 1 
)

Definition at line 21 of file DMatrixUtil.h.

21  {
22  return DMatrix<T>(sxy, sxy).setUpper(1, val);
23  }
template<class T >
DMatrix<T> dmutil::pseudoInv ( const DMatrix< T > &  dmat)

Definition at line 131 of file DMatrixLinAlg.h.

References DMatrix< T >::at(), DMLA_TAU, DMatrix< T >::mulRight(), DMatrix< T >::sizeX(), DMatrix< T >::sizeY(), SVD(), and DMatrix< T >::transpose().

132  {
133  DMatrix<T> U,S,V;
134  if(dmutil::SVD(dmat, U,S,V)) {
135  //create an inverted S and skip the nearly-zero elements
136  //on the diagonal
137  dword sxy = std::min(S.sizeX(),S.sizeY());
138  for(dword i=0; i<sxy; i++)
139  if(S.at(i,i)>T(DMLA_TAU)) S.at(i,i) = T(1)/S.at(i,i);
140  else S.at(i,i) = 0;
141  S.transpose();
142  U.transpose();
143  DMatrix<T> P = V.mulRight(S.mulRight(U)); // actually V * S(+)*Ut
144  return P;
145  }
146  return dmat;
147  }
#define DMLA_TAU
Definition: DMatrixLinAlg.h:12
MT & transpose()
Definition: DMatrix.h:260
dword sizeX() const
Definition: DMatrix.h:42
bool SVD(const DMatrix< T > &dmat, DMatrix< T > &mU, DMatrix< T > &mS, DMatrix< T > &mV)
Definition: DMatrixLinAlg.h:74
unsigned long dword
Definition: simpletypes.h:6
dword sizeY() const
Definition: DMatrix.h:43
T & at(dword x, dword y)
Definition: DMatrix.h:46
MT mulRight(const MT &rhs) const
Definition: DMatrix.h:149
template<class T >
DMatrix<T>& dmutil::sqr ( DMatrix< T > &  mat)

Definition at line 72 of file DMatrixUtil.h.

References DMatrix< T >::getData().

Referenced by var().

73  {
74  for(typename DMatrix<T>::iterator mi = mat.getData().begin();
75  mi != mat.getData().end(); mi++)
76  *mi = *mi**mi;
77  return mat;
78  }
std::vector< T >::iterator iterator
Definition: DMatrix.h:16
std::vector< T > & getData()
Definition: DMatrix.h:48
template<class T >
DMatrix<T>& dmutil::sqrt ( DMatrix< T > &  mat)
template<class T >
DMatrix<T> dmutil::stdev ( const DMatrix< T > &  mat)

Definition at line 106 of file DMatrixUtil.h.

References sqrt(), and var().

Referenced by MStructure::addExpectation(), SubStructure::analyseTF(), EMDGauss::EMDGauss(), EMDGauss::setAvg(), EMDGauss::setStdev(), EMDXformer::setStdev(), MStructure::showStats(), and SmoothIntensitySensor::updateScale().

107  {
108  DMatrix<T> sd(var(mat));
109  return dmutil::sqrt(sd);
110  }
DMatrix< T > var(const DMatrix< T > &mat)
Definition: DMatrixUtil.h:97
DMatrix< T > & sqrt(DMatrix< T > &mat)
Definition: DMatrixUtil.h:81
template<class T >
DMatrix<T> dmutil::sum ( const DMatrix< T > &  mat)

Definition at line 59 of file DMatrixUtil.h.

References DMatrix< T >::getData(), DMatrix< T >::sizeX(), and DMatrix< T >::sizeY().

Referenced by avg(), VVector< float, 4 >::avg(), create2DGaussian(), and Edge::toNode().

60  {
61  DMatrix<T> res(mat.sizeX(), 1);
62  typename DMatrix<T>::const_iterator mi = mat.getData().begin();
63  for(dword y=0; y<mat.sizeY(); y++) {
64  typename DMatrix<T>::iterator ri = res.getData().begin();
65  for(dword x=0; x<mat.sizeX(); x++, ri++, mi++)
66  *ri += *mi;
67  }
68  return res;
69  }
dword sizeX() const
Definition: DMatrix.h:42
unsigned long dword
Definition: simpletypes.h:6
std::vector< T >::const_iterator const_iterator
Definition: DMatrix.h:15
std::vector< T >::iterator iterator
Definition: DMatrix.h:16
dword sizeY() const
Definition: DMatrix.h:43
std::vector< T > & getData()
Definition: DMatrix.h:48
template<class T >
bool dmutil::SVD ( const DMatrix< T > &  dmat,
DMatrix< T > &  mU,
DMatrix< T > &  mS,
DMatrix< T > &  mV 
)

Definition at line 74 of file DMatrixLinAlg.h.

References DMatrix< T >::at(), DMatrix< T >::getRange(), DMatrix< T >::resize(), DMatrix< T >::setCol(), DMatrix< T >::sizeX(), and DMatrix< T >::sizeY().

Referenced by SubStructure::analyseTF(), and pseudoInv().

76  {
77  using namespace Eigen;
78  bool m_Verbose = false;
79  try {
80  //if(!m_Verbose) freopen("/dev/null","a+",stderr);
81  // zeropad matrix to make enough rows
82  // (rank doesn't matter for the algorithm)
83  int d = dmat.sizeX() - dmat.sizeY();
84  if(d<0) d=0;
85  typename DMatrix<T>::EMT mat =
86  convert<T>(DMatrix<T>(dmat.sizeX(), dmat.sizeY()+d,0.0f)
87  .setRange(0,0,dmat));
88  JacobiSVD<typename DMatrix<T>::EMT> svd( mat, ComputeThinU | ComputeThinV);
89  //linalg::SVD svd(mat); // TODO
90  //if(m_Verbose) cout << "condition number of matrix A " <<
91  // svd.q_cond_number() << endl;
92  //if(m_Verbose) cout << svd.q_U().q_nrows() << " rows" << endl;
93 
94  DMatrix<T> U = dmutil::convert<T>(svd.matrixU());
95  DMatrix<T> V = dmutil::convert<T>(svd.matrixV());
96  VectorXf sig(svd.singularValues());
97  DMatrix<T> S;
98  S.resize(V.sizeY(),U.sizeX(),T(0));
99  dword sxy = std::min(S.sizeX(),S.sizeY());
100  std::vector< std::pair<T,dword> > sdiag(sxy);
101  for(dword i=0; i<sxy; i++) {
102  S.at(i,i) = sig(i);
103  sdiag[i].first = sig(i);
104  sdiag[i].second = i;
105  }
106  mS = S; mU = U; mV = V;
107  //sort by diagonal of S
108  sort(sdiag.begin(), sdiag.end(), std::greater< std::pair<float,T> >());
109  DMatrix<T> colu(1,U.sizeY());
110  DMatrix<T> colv(1,V.sizeY());
111  for(dword i=0; i<sxy; i++) {
112  dword permi = sdiag[i].second;
113  if(permi != i) {
114  U.getRange(permi,0,colu);
115  mU.setCol(i, colu);
116  V.getRange(permi,0,colv);
117  mV.setCol(i, colv);
118  mS.at(i,i) = S.at(permi,permi);
119  }
120  }
121  } catch(void*)
122  {
123  //if(!m_Verbose) freopen("/dev/stdout","a+",stderr);
124  return false;
125  }
126  //if(!m_Verbose) freopen("/dev/stdout","a+",stderr);
127  return true;
128  }
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > EMT
Definition: DMatrix.h:20
dword sizeX() const
Definition: DMatrix.h:42
MT & resize(dword _sx, dword _sy, const T &inival=T())
Definition: DMatrix.h:30
unsigned long dword
Definition: simpletypes.h:6
MT & setCol(dword col, MT &vec)
Definition: DMatrix.h:163
dword sizeY() const
Definition: DMatrix.h:43
T & at(dword x, dword y)
Definition: DMatrix.h:46
MT & getRange(dword ox, dword oy, MT &mat) const
Definition: DMatrix.h:197
template<class T >
DMatrix<T>& dmutil::threshold ( DMatrix< T > &  mat,
const T &  th,
const T &  repl = T(0) 
)

Definition at line 123 of file DMatrixUtil.h.

References DMatrix< T >::getData().

124  {
125  for(typename DMatrix<T>::iterator mi = mat.getData().begin();
126  mi != mat.getData().end(); mi++)
127  if(*mi<th) *mi = repl;
128  return mat;
129  }
std::vector< T >::iterator iterator
Definition: DMatrix.h:16
std::vector< T > & getData()
Definition: DMatrix.h:48
template<class T >
DMatrix<T> dmutil::var ( const DMatrix< T > &  mat)

Definition at line 97 of file DMatrixUtil.h.

References avg(), and sqr().

Referenced by stdev(), and VVector< float, 4 >::stdev().

98  {
99  DMatrix<T> e(avg(mat));
100  DMatrix<T> x2(mat); sqr(x2);
101  DMatrix<T> e2(avg(x2));
102  return e2-=sqr(e);
103  }
DMatrix< T > & sqr(DMatrix< T > &mat)
Definition: DMatrixUtil.h:72
DMatrix< T > avg(const DMatrix< T > &mat)
Definition: DMatrixUtil.h:90
template<class T >
DMatrix<T> dmutil::vertCat ( const DMatrix< T > &  um,
const DMatrix< T > &  lm 
)

Definition at line 49 of file DMatrixUtil.h.

References DMatrix< T >::setRange(), DMatrix< T >::sizeX(), and DMatrix< T >::sizeY().

50  {
51  assert(um.sizeX() == lm.sizeX());
52  DMatrix<T> res(um.sizeX(), lm.sizeY() + um.sizeY());
53  res.setRange(0,0, um);
54  res.setRange(0,um.sizeY(), lm);
55  return res;
56  }
dword sizeX() const
Definition: DMatrix.h:42
MT & setRange(dword ox, dword oy, const MT &mat)
Definition: DMatrix.h:185
dword sizeY() const
Definition: DMatrix.h:43