Xmipp  v3.23.11-Nereus
gplvm.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Francisco Sanz Encinas franciscosanz89@gmail.com (2013)
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 #include "gplvm.h"
27 #include <data/numerical_tools.h>
28 
29 void GPLVM::setSpecificParameters(double sigma)
30 {
31  this->sigma=sigma;
32 }
33 
35 {
39 
40  double aux=1.0/(2*sigma*sigma);
43  {
44  double aux2=0;
45  for (int k=0; k<MAT_XSIZE(Y); k++)
46  aux2+=MAT_ELEM(Y,i,k)*MAT_ELEM(Y,j,k);
47  MAT_ELEM(K,i,j)=exp((2*aux2-VEC_ELEM(sumY2,i)-VEC_ELEM(sumY2,j))*aux);
48  }
49 
51  tmp= K.inv() * tmp;
52 
53  double d=MAT_YSIZE(*X);
54  double n=MAT_XSIZE(*X);
55  return(-(d*n/2) * log( 2 * M_PI) - (n/2) * log(K.det()+2.2e-308) - 0.5 * tmp.trace());
56 }
57 
58 double gplvmObjectiveFuntion(double *p, void *prm)
59 {
60  auto *gpvlm=(GPLVM *)prm;
61  Matrix2D<double> &Y=gpvlm->Y;
62  memcpy(&MAT_ELEM(Y,0,0),&(p[1]),MAT_XSIZE(Y)*MAT_YSIZE(Y)*sizeof(double));
63  double c=gpvlm->objectiveFunction();
64  return c;
65 }
66 
68 {
69  // Compute the first guess
71 
72  // Refine
74  steps.initConstant(1);
75  memcpy(&VEC_ELEM(pY,0),&MAT_ELEM(Y,0,0),VEC_XSIZE(pY)*sizeof(double));
76  double goal;
77  int iter;
78  powellOptimizer(pY,1,VEC_XSIZE(pY),&gplvmObjectiveFuntion,this,0.01,goal,iter,steps,true);
79  memcpy(&MAT_ELEM(Y,0,0),&VEC_ELEM(pY,0),VEC_XSIZE(pY)*sizeof(double));
80 }
Definition: gplvm.h:39
#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
Matrix2D< double > Y
Output data.
Definition: dimred_tools.h:147
#define VEC_XSIZE(m)
Definition: matrix1d.h:77
doublereal * c
Matrix2D< double > * X
Pointer to input data.
Definition: dimred_tools.h:141
T trace() const
Definition: matrix2d.cpp:1304
void inv(Matrix2D< T > &result) const
Definition: matrix2d.cpp:663
void powellOptimizer(Matrix1D< double > &p, int i0, int n, double(*f)(double *x, void *), void *prm, double ftol, double &fret, int &iter, const Matrix1D< double > &steps, bool show)
glob_prnt iter
void reduceDimensionality()
Reduce dimensionality.
Definition: gplvm.cpp:67
#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
doublereal * d
void resizeNoCopy(int Ydim, int Xdim)
Definition: matrix2d.h:534
void matrixOperation_AAt(const Matrix2D< double > &A, Matrix2D< double > &C)
Definition: matrix2d.cpp:449
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
void setSpecificParameters(double sigma=1.0)
Set specific parameters.
Definition: gplvm.cpp:29
void log(Image< double > &op)
double sigma
Definition: gplvm.h:42
void reduceDimensionality()
Reduce dimensionality.
Definition: pca.cpp:28
Matrix2D< double > tmp
Definition: gplvm.h:53
T det() const
Definition: matrix2d.cpp:38
void initZeros()
Definition: matrix1d.h:592
#define j
double steps
double objectiveFunction()
Objective function.
Definition: gplvm.cpp:34
Matrix2D< double > K
Definition: gplvm.h:53
#define MAT_XSIZE(m)
Definition: matrix2d.h:120
ProgClassifyCL2D * prm
Matrix1D< double > sumY2
Definition: gplvm.h:54
void initConstant(T val)
Definition: matrix1d.cpp:83
int * n
double gplvmObjectiveFuntion(double *p, void *prm)
Definition: gplvm.cpp:58