Xmipp  v3.23.11-Nereus
ltsa.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Sergio Calvo Gonzalez sergiocg90@gmail.com (2013)
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18  * 02111-1307 USA
19  *
20  * All comments concerning this program package may be sent to the
21  * e-mail address 'xmipp@cnb.csic.es'
22  ***************************************************************************/
23 
24 #include "ltsa.h"
25 
27 {
28  this->k = k;
29 }
30 
32 {
33  weightVector.initZeros(MAT_XSIZE(A));
34  for (size_t i = 0; i < MAT_XSIZE(A); ++i)
35  {
36  double diagelem = MAT_ELEM(A, i, i);
37  for (size_t j = 0; j < MAT_XSIZE(A); ++j)
38  if (((i != j) && (MAT_ELEM(A, j, j) > diagelem)) || ((MAT_ELEM(A, j, j) == diagelem) && (j < i)))
39  VEC_ELEM(weightVector,i)++;
40  }
41 }
42 
44 {
45  size_t outputDim = MAT_XSIZE(B) - 1;
46  for (size_t index = 0; index < outputDim; ++index)
47  FOR_ALL_ELEMENTS_IN_MATRIX1D(weightVector)
48  if (VEC_ELEM(weightVector,i) == index)
49  {
50  for (size_t j = 0; j < MAT_YSIZE(A); ++j)
51  MAT_ELEM(B, j, index + 1) = MAT_ELEM(A, j, i);
52  break;
53  }
54 }
55 
57 {
59 
60  size_t n = MAT_YSIZE(*X);
63  kNearestNeighbours(*X, k, ni, D);
64  Matrix2D<double> Xi(MAT_XSIZE(ni), MAT_XSIZE(*X)), W, Vi, Vi2, Si, Gi;
65 
66  B.initIdentity(n);
67  Matrix1D<int> weightVector;
68  for (size_t iLoop = 0; iLoop < n; ++iLoop)
69  {
70  extractNearestNeighbours(*X, ni, iLoop, Xi);
72 
73  matrixOperation_AAt(Xi, W); // W=X*X^t
74  schur(W, Vi, Si); // W=Vi*Si*Vi^t
75 
76  computeWeightsVector(Si, weightVector);
77 
78  Vi2.resizeNoCopy(MAT_YSIZE(Vi), outputDim + 1);
79  Vi2.setConstantCol(0, 1/sqrt(k)); //Vi2(0,:)=1/sqrt(k)
80  getLessWeightNColumns(Vi, weightVector, Vi2);
81 
82  matrixOperation_AAt(Vi2, Gi); // Gi=Vi2*Vi2^t
83  matrixOperation_IminusA(Gi); // Gi=I-Gi
84 
85  // Compute partial B with correlation matrix Gi
87  MAT_ELEM(B, MAT_ELEM(ni,iLoop,i), MAT_ELEM(ni,iLoop,j)) += MAT_ELEM(Gi, i, j);
88  MAT_ELEM(B, iLoop, iLoop)-=1;
89  }
90 }
91 
93 {
96 
97  Matrix1D<double> DEigs;
98  eigsBetween(B, 1, outputDim, DEigs, Y);
99 }
#define FOR_ALL_ELEMENTS_IN_MATRIX2D(m)
Definition: matrix2d.h:104
#define MAT_YSIZE(m)
Definition: matrix2d.h:124
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
void subtractColumnMeans(Matrix2D< double > &A)
Definition: matrix2d.cpp:230
void eigsBetween(const Matrix2D< double > &A, size_t I1, size_t I2, Matrix1D< double > &D, Matrix2D< double > &P)
Definition: matrix2d.cpp:324
void setConstantCol(size_t j, T v)
Definition: matrix2d.h:1028
Matrix2D< double > Y
Output data.
Definition: dimred_tools.h:147
void kNearestNeighbours(const Matrix2D< double > &X, int K, Matrix2D< int > &idx, Matrix2D< double > &distance, DimRedDistance2 f, bool computeSqrt)
void sqrt(Image< double > &op)
Matrix2D< double > * X
Pointer to input data.
Definition: dimred_tools.h:141
void extractNearestNeighbours(const Matrix2D< double > &X, Matrix2D< int > &idx, int i, Matrix2D< double > &Xi)
void matrixOperation_IminusA(Matrix2D< double > &A)
Definition: matrix2d.cpp:533
#define i
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
void getLessWeightNColumns(const Matrix2D< double > &A, const Matrix1D< int > &weightVector, Matrix2D< double > &B)
Definition: ltsa.cpp:43
void resizeNoCopy(int Ydim, int Xdim)
Definition: matrix2d.h:534
void matrixOperation_AAt(const Matrix2D< double > &A, Matrix2D< double > &C)
Definition: matrix2d.cpp:449
size_t outputDim
Output dim.
Definition: dimred_tools.h:144
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
#define FOR_ALL_ELEMENTS_IN_MATRIX1D(v)
Definition: matrix1d.h:72
viol index
void computeWeightsVector(const Matrix2D< double > &A, Matrix1D< int > &weightVector)
Definition: ltsa.cpp:31
void initZeros()
Definition: matrix1d.h:592
virtual void reduceDimensionality()
Reduce dimensionality.
Definition: ltsa.cpp:92
int ni
void schur(const Matrix2D< double > &M, Matrix2D< double > &O, Matrix2D< double > &T)
Definition: matrix2d.cpp:251
#define j
#define MAT_XSIZE(m)
Definition: matrix2d.h:120
void computeAlignmentMatrix(Matrix2D< double > &B)
Common part.
Definition: ltsa.cpp:56
int k
Definition: ltsa.h:39
void setSpecificParameters(int k=12)
Set specific parameters.
Definition: ltsa.cpp:26
int * n
void initIdentity()
Definition: matrix2d.h:673