123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- /**
- * @file AlgebraTools.cpp
- * @brief AlgebraTools Class
- * @author Michael Koch
- * @date 08/19/2008
- */
- #include "AlgebraTools.h"
- using namespace OBJREC;
- using namespace std;
- using namespace NICE;
- //calculate mean
- // refactor-nice.pl: check this substitution
- // old: void AlgebraTools::calculateMean(const Matrix &data, Vector &mean)
- void AlgebraTools::calculateMean(const NICE::Matrix &data, NICE::Vector &mean)
- {
- uint features = data.rows();
- uint dimension = data.cols();
- mean = Vector(dimension);
- for (uint k = 0;k < mean.size();k++)
- {
- mean[k] = 0.0;
- }
- for (uint r = 0;r < features;r++)
- {
- for (uint c = 0;c < dimension;c++)
- {
- // refactor-nice.pl: check this substitution
- // old: if (isnan(data[r][c]) || isinf(data[r][c]))
- if(isnan(data(r, c)) ||(isnan(data(r, c))))
- {
- //strange data
- }
- else
- {
- // refactor-nice.pl: check this substitution
- // old: mean[c] = mean[c] + data[r][c];
- mean[c] = mean[c] + data(r, c);
- }
- }
- }
- for (uint r = 0;r < mean.size();r++)
- {
- if (features > 0)
- {
- mean[r] = mean[r] / (double)features;
- }
- }
- // cout<<"mean-calc"<<mean<<endl;
- }
- //calculate mean
- void AlgebraTools::calculateMean(const NICE::Matrix &data, NICE::Vector &mean, NICE::Vector &sigma)
- {
- uint features = data.rows();
- uint dimension = data.cols();
- mean = Vector(dimension);
- sigma = Vector(dimension);
- for (uint k = 0;k < mean.size();k++)
- {
- mean[k] = 0.0;
- sigma[k] = 0.0;
- }
- for (uint r = 0;r < features;r++)
- {
- for (uint c = 0;c < dimension;c++)
- {
- // refactor-nice.pl: check this substitution
- // old: if (isnan(data[r][c]) || isinf(data[r][c]))
- if(isnan(data(r, c)) ||(isnan(data(r, c))))
- {
- //strange data
- }
- else
- {
- // refactor-nice.pl: check this substitution
- // old: mean[c] = mean[c] + data[r][c];
- mean[c] = mean[c] + data(r, c);
- }
- }
- }
- for (uint k = 0;k < mean.size();k++)
- {
- if (features > 0)
- {
- mean[k] = mean[k] / (double)features;
- }
- }
- // cout<<"mean-calc"<<mean<<endl;
- //calc sigma
- for (uint r = 0;r < features;r++)
- {
- for (uint c = 0;c < dimension;c++)
- {
- // refactor-nice.pl: check this substitution
- // old: sigma[c] = sigma[c] + (mean[c] - data[r][c]) * (mean[c] - data[r][c]);
- sigma[c] = sigma[c] + (mean[c] -data(r, c)) * (mean[c] -data(r, c));
- }
- }
- for (uint k = 0;k < sigma.size();k++)
- {
- if (features > 0)
- {
- sigma[k] = sigma[k] / features;
- sigma[k] = sqrt(sigma[k]);
- }
- }
- }
- // refactor-nice.pl: check this substitution
- // old: double AlgebraTools::trace(const Matrix &M)
- double AlgebraTools::trace(const NICE::Matrix &M)
- {
- if (M.cols() != M.rows())
- {
- throw("non quadratic Matrix");
- }
- else
- {
- uint n = M.rows();
- double sum = 0.0;
- for (uint i = 0;i < n;i++)
- {
- // refactor-nice.pl: check this substitution
- // old: sum += M[i][i];
- sum +=M(i, i);
- }
- return sum;
- }
- }
- double AlgebraTools::matrixRowColNorm(const NICE::Matrix &M)
- {
- //max row norm
- double maxr = 0.0, maxc = 0.0;
- for (uint r = 0;r < M.rows();r++)
- {
- double sum = 0.0;
- for (uint c = 0;c < M.cols();c++)
- {
- sum +=fabs(M(r, c));
- }
- if (r == 0)
- {
- maxr = sum;
- }
- else
- {
- if (sum > maxr)
- {
- maxr = sum;
- }
- }
- }
- //max col norm
- for (uint c = 0;c < M.cols();c++)
- {
- double sum = 0.0;
- for (uint r = 0;r < M.rows();r++)
- {
- sum +=fabs(M(r, c));
- }
- if (c == 0)
- {
- maxc = sum;
- }
- else
- {
- if (sum > maxc)
- {
- maxc = sum;
- }
- }
- }
- if (maxr > maxc)
- {
- return maxr;
- }
- else
- {
- return maxc;
- }
- }
- void AlgebraTools::orthogonalize(Matrix &M, bool iterative)
- {
- #if 1
- fprintf (stderr, "AlgebraTools::orthogonalize: FIX THIS CODE !\n");
- exit(-1);
- #else
- //iterative
- if (iterative)
- {
- NICE::Vector onevec = Vector(M.rows());
- for (uint k = 0;k < onevec.size();k++)
- {
- onevec[k] = 1.0;
- }
- NICE::Matrix One = diag(onevec);
- NICE::Matrix Mt = M.transpose();
- NICE::Matrix error;
- error.multiply (M, Mt);
- error -= One;
- double errornorm = matrixRowColNorm(error);
- double deltaerror = 2.0;
- //reduceddim
- while (deltaerror > 1.0e-03)
- {
- double norm = matrixRowColNorm(M);
- if (norm > 0.0)
- {
- // refactor: M = (1.0 / norm) * M;
- M *= (1.0 / norm);
- }
- Mt = M.transpose();
- M = 1.5 * M - 0.5 * ((M * Mt) * M);
- Mt = M.transpose();
- //convergencetest
- error = (M * (Mt) - One);
- deltaerror = errornorm;
- errornorm = matrixRowColNorm(error);
- deltaerror = fabs(deltaerror - errornorm);
- }
- }
- else
- {
- //noniteratve
- // refactor-nice.pl: check this substitution
- // old: Matrix D, eigenvectors;
- NICE::Matrix D, eigenvectors;
- // refactor-nice.pl: check this substitution
- // old: Vector eigenvalue;
- NICE::Vector eigenvalue;
- // refactor-nice.pl: check this substitution
- // old: Matrix MMt = M*!M;
- NICE::Matrix MMt;
- MMt.multiply ( M, M.transpose() );
- Eigenvalue(MMt, eigenvalue, eigenvectors);
- D = diag(eigenvalue);
- for (int d = 0;d < D.rows();d++)
- {
- // refactor-nice.pl: check this substitution
- // old: if (D[d][d] > 0.0)
- if(D(d, d) > 0.0)
- {
- // refactor-nice.pl: check this substitution
- // old: D[d][d] = 1.0 / sqrt(D[d][d]);
- D(d, d) = 1.0 /D(d, d));
- }
- }
- // refactor-nice.pl: check this substitution
- // old: Matrix Et = !eigenvectors;
- NICE::Matrix Et = !eigenvectors;
- // refactor-nice.pl: check this substitution
- // old: Matrix MMtinvsqrt = eigenvectors * D * Et;
- NICE::Matrix MMtinvsqrt = eigenvectors * D * Et;
- M = MMtinvsqrt * M;
- }
- #endif
- }
- // refactor-nice.pl: check this substitution
- // old: Vector AlgebraTools::meanOfMatrix(const Matrix &M)
- NICE::Vector AlgebraTools::meanOfMatrix(const NICE::Matrix &M)
- {
- #if 1
- fprintf (stderr, "AlgebraTools:: FIX THIS CODE !\n");
- exit(-1);
- #else
- // refactor-nice.pl: check this substitution
- // old: Vector meanvec = Vector(M.cols());
- NICE::Vector meanvec = Vector(M.cols());
- //initialize meanvec
- for (uint k = 0;k < meanvec.size();k++)
- {
- meanvec[k] = 0.0;
- }
- //calculate mean
- for (int f = 0;f < M.rows();f++)
- {
- meanvec = M[f] + meanvec;
- }
- if (M.rows() > 0)
- {
- meanvec = 1.0 / M.rows() * meanvec;
- }
- return meanvec;
- #endif
- }
- // refactor-nice.pl: check this substitution
- // old: double AlgebraTools::meanOfVector(const Vector &vec)
- double AlgebraTools::meanOfVector(const NICE::Vector &vec)
- {
- #if 1
- fprintf (stderr, "AlgebraTools:: FIX THIS CODE !\n");
- exit(-1);
- #else
- double mean = 0.0;
- for (uint k = 0;k < vec.size();k++)
- {
- mean += vec[k];
- }
- if (vec.size() > 0)
- {
- mean /= vec.size();
- }
- return mean;
- #endif
- }
- // refactor-nice.pl: check this substitution
- // old: Matrix AlgebraTools::vectorToMatrix(const Vector &vec)
- NICE::Matrix AlgebraTools::vectorToMatrix(const NICE::Vector &vec)
- {
- if (vec.size() > 0)
- {
- // refactor-nice.pl: check this substitution
- // old: Matrix D = Matrix(vec.size(), 1, 0);
- NICE::Matrix D = Matrix(vec.size(), 1, 0);
- for (uint k = 0;k < vec.size();k++)
- {
- // refactor-nice.pl: check this substitution
- // old: D[k][0] = vec[k];
- D(k, 0) = vec[k];
- }
- return D;
- }
- else
- {
- return Matrix();
- }
- }
- // refactor-nice.pl: check this substitution
- // old: Vector AlgebraTools::matrixToVector(const Matrix &mat)
- NICE::Vector AlgebraTools::matrixToVector(const NICE::Matrix &mat)
- {
- if (mat.rows() < 1 && mat.cols() < 1)
- {
- fprintf(stderr, "Matrix has no entries!");
- }
- if (mat.rows() > mat.cols())
- {
- // refactor-nice.pl: check this substitution
- // old: Vector tmp = Vector(mat.rows());
- NICE::Vector tmp (mat.rows());
- tmp.set(0.0);
- for (uint i = 0;i < mat.rows();i++)
- {
- // refactor-nice.pl: check this substitution
- // old: tmp[i] = mat[i][0];
- tmp[i] =mat(i, 0);
- }
- return tmp;
- }
- else
- {
- // refactor-nice.pl: check this substitution
- // old: Vector tmp = Vector(mat.cols());
- NICE::Vector tmp = Vector(mat.cols());
- for (uint i = 0;i < mat.cols();i++)
- {
- // refactor-nice.pl: check this substitution
- // old: tmp[i] = mat[0][i];
- tmp[i] =mat(0, i);
- }
- return tmp;
- }
- }
- // refactor-nice.pl: check this substitution
- // old: Matrix AlgebraTools::diag(const Vector &diagonalelements)
- NICE::Matrix AlgebraTools::diag(const NICE::Vector &diagonalelements)
- {
- if (diagonalelements.size() > 0)
- {
- // refactor-nice.pl: check this substitution
- // old: Matrix D = Matrix(diagonalelements.size(), diagonalelements.size(), 0);
- NICE::Matrix D (diagonalelements.size(), diagonalelements.size());
- D.set(0.0);
- for (uint k = 0;k < diagonalelements.size();k++)
- {
- // refactor-nice.pl: check this substitution
- // old: D[k][k] = diagonalelements[k];
- D(k, k) = diagonalelements[k];
- }
- return D;
- }
- else
- {
- return Matrix();
- }
- }
|