Xmipp  v3.23.11-Nereus
spe.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Carlos Oscar Sanchez Sorzano coss@cnb.csic.es (2013)
4  * Alejandro Gomez Rodriguez (2013)
5  *
6  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21  * 02111-1307 USA
22  *
23  * All comments concerning this program package may be sent to the
24  * e-mail address 'xmipp@cnb.csic.es'
25  ***************************************************************************/
26 
27 #include "spe.h"
28 #include <data/numerical_tools.h>
29 
30 void SPE::setSpecificParameters(bool global, int k)
31 {
32  this->global = true;
33  this->k = 1;
34 }
35 
37 {
38  double lambda = 1; // lambda
39  int s = 100; // number of updates per iteration
40  int max_iter = 20000 + round(0.04 * MAT_YSIZE(*X) * MAT_YSIZE(*X)); // number of iterations
41  double tol = 1e-5;
42  int n = MAT_YSIZE(*X);
44  Matrix1D<double> D, Rt, W;
46  Matrix1D<int> ind1,ind2;
47 
48  ind1.resize(s);
49  ind2.resize(s);
50  Rt.initZeros(VEC_XSIZE(ind1));
51  D.initZeros(VEC_XSIZE(ind1));
52  W.initZeros(VEC_XSIZE(ind1));
53 
55 
56  // Compute distance all vs. all
57  computeDistance(*X, R, distance, true);
58  R /= R.computeMax() / sqrt(2);
59 
60  if (global)
61  max_iter *= 3;
62  int ind1_0=0, ind1_F=s;
63  int ind2_F=std::min(2*s,n-1);
64  for(int nn=0;nn<max_iter;nn++)
65  {
66  // Compute random points
67  randomPermutation(n,J);
68 
69  // Get the ind1 and ind2 indexes vectors
70  if(n>s){
71  memcpy(&VEC_ELEM(ind1, 0),&A1D_ELEM(J,ind1_0),ind1_F*sizeof(int));
72  memcpy(&VEC_ELEM(ind2, 0),&A1D_ELEM(J,ind1_F),ind1_F*sizeof(int));
73  }
74  else{
75  memcpy(&VEC_ELEM(ind1, 0),&A1D_ELEM(J,0),ind2_F*sizeof(int));
76  memcpy(&VEC_ELEM(ind2, 0),&A1D_ELEM(J,ind1_F),ind2_F*sizeof(int));
77  }
78 
79  // Compute distances between points in embedded space
80  computeRandomPointsDistance(Y,D,ind1,ind2,distance,true);
81 
82  // Get corresponding distances in real space
83  for (int l=0;l<VEC_XSIZE(ind1);l++)
84  VEC_ELEM(Rt,l)= R(VEC_ELEM(ind1,l),VEC_ELEM(ind2,l));
85 
86  // Compute (Rt-D) / (D + tol)
88  VEC_ELEM(W,i) = (VEC_ELEM(Rt,i)-VEC_ELEM(D,i))/(VEC_ELEM(D,i)+tol);
89 
90  // Get the index to update locations
91  for(int ii=0;ii<s;ii++)
92  {
93  int i1 = VEC_ELEM(ind1,ii);
94  int i2 = VEC_ELEM(ind2,ii);
95 
96  // Update locations
97  double factor=lambda * 1/2;
98  for (size_t j = 0; j< outputDim; ++j){
99  double aux= MAT_ELEM(Y, i1, j) + factor*VEC_ELEM(W,ii)*(MAT_ELEM(Y, i1,j)-MAT_ELEM(Y, i2,j));
100  MAT_ELEM(Y, i2, j) += factor*VEC_ELEM(W,ii)*(MAT_ELEM(Y,i2,j)-MAT_ELEM(Y,i1,j));
101  MAT_ELEM(Y,i1,j) = aux;
102  }
103  }
104  // Update lambda
105  lambda = lambda - (lambda / max_iter);
106  }
107 }
#define MAT_YSIZE(m)
Definition: matrix2d.h:124
void min(Image< double > &op1, const Image< double > &op2)
#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
void computeDistance(const Matrix2D< double > &X, Matrix2D< double > &distance, DimRedDistance2 f, bool computeSqrt)
void sqrt(Image< double > &op)
Matrix2D< double > * X
Pointer to input data.
Definition: dimred_tools.h:141
#define A1D_ELEM(v, i)
void computeRandomPointsDistance(const Matrix2D< double > &X, Matrix1D< double > &distance, Matrix1D< int > ind1, Matrix1D< int > ind2, DimRedDistance2 f, bool computeSqrt)
void reduceDimensionality()
Reduce dimensionality.
Definition: spe.cpp:36
#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 randomPermutation(int N, MultidimArray< int > &result)
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
double * lambda
void resize(size_t Xdim, bool copy=true)
Definition: matrix1d.h:410
void initZeros()
Definition: matrix1d.h:592
#define j
bool global
Definition: spe.h:40
DimRedDistance2 distance
Distance function.
Definition: dimred_tools.h:150
int round(double x)
Definition: ap.cpp:7245
void initRandom(size_t Ydim, size_t Xdim, double op1, double op2, RandomMode mode=RND_UNIFORM)
Definition: matrix2d.cpp:1108
T computeMax() const
Definition: matrix2d.cpp:1236
void setSpecificParameters(bool global=true, int k=12)
Set specific parameters.
Definition: spe.cpp:30
int * n