Xmipp  v3.23.11-Nereus
eq_system_solver.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: David Strelak (davidstrelak@gmail.com)
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 "eq_system_solver.h"
28 
31  Matrix1D<float>& shiftYt, int verbosity, int iterations);
34  Matrix1D<double>& shiftYt, int verbosity, int iterations);
35 template<typename T>
37  Matrix1D<T>& bYt, Matrix2D<T>& At, Matrix1D<T>& shiftXt,
38  Matrix1D<T>& shiftYt, int verbosity, int iterations) {
42  helper.bs.resize(2);
43  std::vector<Matrix1D<double>> shifts(2);
47  Matrix1D<double> &shiftX = shifts[0];
48  Matrix1D<double> &shiftY = shifts[1];
49 
50  typeCast(bXt, bX);
51  typeCast(bYt, bY);
52  typeCast(shiftXt, shiftX);
53  typeCast(shiftYt, shiftY);
54 
55  helper.w.resizeNoCopy(VEC_XSIZE(bX));
56  helper.w.initConstant(1);
57 
58  int it = 0;
59  double mean;
60  double varbX;
61  double varbY;
62  bX.computeMeanAndStddev(mean, varbX);
63  varbX *= varbX;
64  bY.computeMeanAndStddev(mean, varbY);
65  varbY *= varbY;
66  if (verbosity > 1)
67  std::cout << "Solving equation system ...\n";
68  do {
69  // Solve the equation system
70  helper.bs[0] = bX;
71  helper.bs[1] = bY;
72  typeCast(At, helper.A);
73  weightedLeastSquares(helper, shifts); // we have updated A, is that OK?
74 
75  // Compute residuals
76  ex = bX - helper.A * shiftX;
77  ey = bY - helper.A * shiftY;
78 
79  // Compute R2
80  double stddeveX;
81  ex.computeMeanAndStddev(mean, stddeveX);
82  double stddeveY;
83  ey.computeMeanAndStddev(mean, stddeveY);
84  double R2x = 1 - (stddeveX * stddeveX) / varbX;
85  double R2y = 1 - (stddeveY * stddeveY) / varbY;
86  if (verbosity > 1)
87  std::cout << "Iteration " << it << " R2x=" << R2x << " R2y=" << R2y
88  << std::endl;
89 
90  // Identify outliers
91  double oldWeightSum = helper.w.sum();
93  if (fabs(VEC_ELEM(ex, i)) > 3 * stddeveX
94  || fabs(VEC_ELEM(ey, i)) > 3 * stddeveY)
95  VEC_ELEM(helper.w, i) = 0.0;
96  }
97  double newWeightSum = helper.w.sum();
98  if ((newWeightSum == oldWeightSum) && (verbosity > 1)){
99  std::cout << "No outlier found\n\n";
100  break;
101  } else if (verbosity > 1)
102  std::cout << "Found " << (int) (oldWeightSum - newWeightSum)
103  << " outliers\n\n";
104 
105  it++;
106  } while (it < iterations);
107 
108  typeCast(shiftX, shiftXt);
109  typeCast(shiftY, shiftYt);
110 }
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define VEC_XSIZE(m)
Definition: matrix1d.h:77
std::vector< Matrix1D< double > > bs
#define i
#define FOR_ALL_ELEMENTS_IN_MATRIX1D(v)
Definition: matrix1d.h:72
static void solve(Matrix1D< T > &bXt, Matrix1D< T > &bYt, Matrix2D< T > &At, Matrix1D< T > &shiftXt, Matrix1D< T > &shiftYt, int verbosity, int iterations)
double sum(bool average=false) const
Definition: matrix1d.cpp:652
void typeCast(const Matrix1D< T1 > &v1, Matrix1D< T2 > &v2)
Definition: matrix1d.h:1227
void resizeNoCopy(int Xdim)
Definition: matrix1d.h:458
void computeMeanAndStddev(double &mean, double &stddev) const
Definition: matrix1d.cpp:588
void weightedLeastSquares(WeightedLeastSquaresHelper &h, Matrix1D< double > &result)
void initConstant(T val)
Definition: matrix1d.cpp:83