Xmipp  v3.23.11-Nereus
numerical_tools.h
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Carlos Oscar S. Sorzano (coss@cnb.csic.es)
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 /* Variable and prototype definitions for the Numerical Core */
27 /*****************************************************************************/
28 #ifndef CORE__NUMERICAL_TOOLS_HH
29 #define CORE__NUMERICAL_TOOLS_HH
30 
31 #include "core/multidim_array.h"
32 
35 
41 void randomPermutation(int N, MultidimArray<int>& result);
42 
83  int i0, int n,
84  double(*f)(double* , void *),
85  void *prm,
86  double ftol,
87  double& fret,
88  int& iter,
90  bool show = false);
91 
106  double xstep;
107  double xmax;
108  double ixstep;
109 public:
115  void initialize(double xmax, int N, bool normalize=true);
116 
118  inline double getValue(double x) const {
119  x=fabs(x);
120  if (x>xmax) return 0;
121  else
122  {
123  auto iaux=(int)round(x*ixstep);
124  return DIRECT_A1D_ELEM(v,iaux);
125  }
126  }
127 };
128 
139  Matrix1D< double >& result);
140 
149  const Matrix1D< double >& b,
150  Matrix1D< double >& result);
151 
163  const Matrix2D< double >& H, double& val,
165 
176  const Matrix2D< double >& A, const Matrix1D< double >& b,
177  const Matrix2D< double >& Aeq, const Matrix1D< double >& beq,
179  Matrix1D< double >& x);
180 
181 
191 void leastSquare(const Matrix2D< double >& C, const Matrix1D< double >& d,
192  const Matrix2D< double >& A, const Matrix1D< double >& b,
193  const Matrix2D< double >& Aeq, const Matrix1D< double >& beq,
195  Matrix1D< double >& x);
196 
208  const Matrix1D< double >& d, double lambda,
210 
214 template<typename T>
215 void solve(const Matrix2D<T>& A, const Matrix1D<T>& b, Matrix1D<T>& result)
216 {
217  if (MAT_XSIZE(A) == 0)
218  REPORT_ERROR(ERR_MATRIX_EMPTY, "Solve: Matrix is empty");
219 
220  if (MAT_XSIZE(A) != MAT_YSIZE(A))
221  REPORT_ERROR(ERR_MATRIX_SIZE, "Solve: Matrix is not squared");
222 
223  if (MAT_XSIZE(A) != b.size())
224  REPORT_ERROR(ERR_MATRIX_SIZE, "Solve: Different sizes of Matrix and Vector");
225 
226  if (b.isRow())
227  REPORT_ERROR(ERR_MATRIX_DIM, "Solve: Not correct vector shape");
228 
229  // Perform LU decomposition and then solve
230  Matrix1D< int > indx;
231  T d;
232  Matrix2D<T> LU;
233  ludcmp(A, LU, indx, d);
234  result = b;
235  lubksb(LU, indx, result);
236 }
237 
241 template<typename T>
242 void solveBySVD(const Matrix2D< T >& A, const Matrix1D< T >& b,
243  Matrix1D< double >& result, double tolerance);
244 
248 template<typename T>
249 void solve(const Matrix2D<T>& A, const Matrix2D<T>& b, Matrix2D<T>& result);
250 
251 // Differential Evolution Solver Class
252 // Based on algorithms developed by Dr. Rainer Storn & Kenneth Price
253 // Written By: Lester E. Godwin
254 // PushCorp, Inc.
255 // Dallas, Texas
256 // 972-840-0208 x102
257 // godwin@pushcorp.com
258 // Created: 6/8/98
259 // Last Modified: 6/8/98
260 // Revision: 1.0
261 
262 constexpr int stBest1Exp = 0;
263 constexpr int stRand1Exp = 1;
264 constexpr int stRandToBest1Exp = 2;
265 constexpr int stBest2Exp = 3;
266 constexpr int stRand2Exp = 4;
267 constexpr int stBest1Bin = 5;
268 constexpr int stRand1Bin = 6;
269 constexpr int stRandToBest1Bin = 7;
270 constexpr int stBest2Bin = 8;
271 constexpr int stRand2Bin = 9;
272 
273 class DESolver;
274 
275 typedef void(DESolver::*StrategyFunction)(int);
276 
312 class DESolver
313 {
314 public:
316  DESolver(int dim, int popSize);
317 
318  DESolver(const DESolver &)=delete; // Do not use the default copy constructor
319  DESolver& operator=(const DESolver &)=delete; // Do not use the default copy assignment
320 
322  virtual ~DESolver(void);
323 
325  void Setup(double min[],
326  double max[],
327  int deStrategy,
328  double diffScale,
329  double crossoverProb);
330 
334  virtual bool Solve(int maxGenerations);
335 
341  virtual double EnergyFunction(double testSolution[], bool& bAtSolution) = 0;
342 
344  int Dimension() const
345  {
346  return nDim;
347  }
348 
350  int Population() const
351  {
352  return nPop;
353  }
354 
356  double Energy() const
357  {
358  return bestEnergy;
359  }
360 
362  double* Solution(void)
363  {
364  return bestSolution;
365  }
366 
368  int Generations() const
369  {
370  return generations;
371  }
372 
373 protected:
374  void SelectSamples(int candidate,
375  int* r1,
376  int* r2 = nullptr,
377  int* r3 = nullptr,
378  int* r4 = nullptr,
379  int* r5 = nullptr);
380 
381  int nDim;
382  int nPop;
384 
385  int strategy;
387  double scale;
388  double probability;
389 
390  double trialEnergy;
391  double bestEnergy;
392 
393  double* trialSolution;
394  double* bestSolution;
395  double* popEnergy;
396  double* population;
397 private:
398  void Best1Exp(int candidate);
399  void Rand1Exp(int candidate);
400  void RandToBest1Exp(int candidate);
401  void Best2Exp(int candidate);
402  void Rand2Exp(int candidate);
403  void Best1Bin(int candidate);
404  void Rand1Bin(int candidate);
405  void RandToBest1Bin(int candidate);
406  void Best2Bin(int candidate);
407  void Rand2Bin(int candidate);
408 };
409 
415 double checkRandomness(const std::string &sequence);
416 
426 double ZernikeSphericalHarmonics(int l1, int n, int l2, int m, double xr, double yr, double zr, double r);
427 
428 #ifdef NEVERDEFINED
429 
439 double ALegendreSphericalHarmonics(int l, int m, double xr, double yr, double zr, double rr);
440 #endif
441 
446 void spherical_index2lnm(int idx, int &l1, int &n, int &l2, int &m, int maxl1);
447 
452 int spherical_lnm2index(int l1, int n, int l2, int m, int maxl1);
453 #endif
#define MAT_YSIZE(m)
Definition: matrix2d.h:124
double probability
double Energy() const
Call these functions after Solve() to get results.
void min(Image< double > &op1, const Image< double > &op2)
double * population
double * bestSolution
constexpr int stBest1Exp
int spherical_lnm2index(int l1, int n, int l2, int m, int maxl1)
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
size_t size() const
Definition: matrix1d.h:508
Problem with matrix size.
Definition: xmipp_error.h:152
doublereal * c
double * bl
doublereal * grad
constexpr int stBest2Exp
The matrix is empty.
Definition: xmipp_error.h:151
constexpr int stBest1Bin
constexpr int stRand2Bin
glob_prnt iter
void powellOptimizer(Matrix1D< double > &p, int i0, int n, double(*f)(double *, void *), void *prm, double ftol, double &fret, int &iter, const Matrix1D< double > &steps, bool show=false)
StrategyFunction calcTrialSolution
doublereal * x
int Generations() const
Return the number of generations.
doublereal * d
void randomPermutation(int N, MultidimArray< int > &result)
double trialEnergy
double * bu
void evaluateQuadratic(const Matrix1D< double > &x, const Matrix1D< double > &c, const Matrix2D< double > &H, double &val, Matrix1D< double > &grad)
#define DIRECT_A1D_ELEM(v, i)
doublereal * b
void quadraticProgramming(const Matrix2D< double > &C, const Matrix1D< double > &d, const Matrix2D< double > &A, const Matrix1D< double > &b, const Matrix2D< double > &Aeq, const Matrix1D< double > &beq, Matrix1D< double > &bl, Matrix1D< double > &bu, Matrix1D< double > &x)
void lubksb(const Matrix2D< T > &LU, Matrix1D< int > &indx, Matrix1D< T > &b)
Definition: matrix2d.cpp:596
double * lambda
int isRow() const
Definition: matrix1d.h:520
Problem with matrix dimensions.
Definition: xmipp_error.h:150
double * f
constexpr int stBest2Bin
quaternion_type< T > normalize(quaternion_type< T > q)
Definition: point.cpp:278
void max(Image< double > &op1, const Image< double > &op2)
constexpr int stRand2Exp
void(DESolver::* StrategyFunction)(int)
double getValue(double x) const
void spherical_index2lnm(int idx, int &l1, int &n, int &l2, int &m, int maxl1)
double * trialSolution
double bestEnergy
double steps
int m
void ludcmp(const Matrix2D< T > &A, Matrix2D< T > &LU, Matrix1D< int > &indx, T &d)
Definition: matrix2d.cpp:586
constexpr int stRand1Bin
void regularizedLeastSquare(const Matrix2D< double > &A, const Matrix1D< double > &d, double lambda, const Matrix2D< double > &G, Matrix1D< double > &x)
constexpr int stRandToBest1Exp
int Population() const
Return population.
int round(double x)
Definition: ap.cpp:7245
int Dimension() const
Return dimension.
double scale
#define MAT_XSIZE(m)
Definition: matrix2d.h:120
float r3
constexpr int stRandToBest1Bin
void initialize(double xmax, int N, bool normalize=true)
double * popEnergy
void solve(const Matrix2D< T > &A, const Matrix1D< T > &b, Matrix1D< T > &result)
float r2
ProgClassifyCL2D * prm
double solveNonNegative(const Matrix2D< double > &A, const Matrix1D< double > &b, Matrix1D< double > &result)
constexpr int stRand1Exp
double ZernikeSphericalHarmonics(int l1, int n, int l2, int m, double xr, double yr, double zr, double r)
void solveViaCholesky(const Matrix2D< double > &A, const Matrix1D< double > &b, Matrix1D< double > &result)
double * Solution(void)
Return best solution.
int * n
void solveBySVD(const Matrix2D< T > &A, const Matrix1D< T > &b, Matrix1D< double > &result, double tolerance)
void leastSquare(const Matrix2D< double > &C, const Matrix1D< double > &d, const Matrix2D< double > &A, const Matrix1D< double > &b, const Matrix2D< double > &Aeq, const Matrix1D< double > &beq, Matrix1D< double > &bl, Matrix1D< double > &bu, Matrix1D< double > &x)
float r1
double checkRandomness(const std::string &sequence)