Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
LeastSquares.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 #ifndef __LeastSquares_hh__
27 #define __LeastSquares_hh__
28 
29 #include <Base/Math/Matrix.hh>
30 #include <Base/Math/Vector.hh>
31 #include "SVD.hh"
32 
33 
34 #define D_LS_MATRIXES 0x00000001
35 #define D_LS_RED_MATRIXES 0x00000002
36 #define D_LS_SVD 0x00000004
37 
38 namespace BIAS {
39 
40  /** @class LeastSquaresBase
41  @ingroup g_mathalgo
42  @brief Base class for linear least squares solvers.
43  @author MIP
44  */
45  class BIASMathAlgo_EXPORT LeastSquaresBase : public Debug
46  {
47  public:
48 
49  LeastSquaresBase() : _ATA(), _AT(), _Weight(), _ATb() {};
50 
51  virtual ~LeastSquaresBase() {};
52 
53  /** @brief Initialize least squares solver.
54  @param[in] SolutionSize Number of parameters
55  @param[in] ReduceToATA Reduce minimization of |Ax-b| to solution of
56  (A'A)x = A'b
57  */
58  int Init(unsigned SolutionSize, bool ReduceToATA = true);
59 
60  /** @brief Solve linear equation system A*x = b using linear least squares,
61  i.e. minimize |A*x - b|^2.
62  @return Returns 0 in case of success, <0 otherwise.
63  */
64  virtual int Solve(Matrix<double> &A, Vector<double> &b, Vector<double> &x);
65 
66  /** @brief Solve linear equation system A*x = 0 using linear least squares,
67  i.e. minimize |A*x|^2, subject to |x| = 1.
68  @return Returns 0 in case of success, <0 otherwise.
69  */
70  virtual int Solve(Matrix<double> &A, Vector<double> &x);
71 
72  /** @brief Solve linear equation system A*x = b using weighted linear least
73  squares, i.e. minimize |W*(A*x - b)|^2 with diagonal matrix W
74  composed of given weights.
75  @return Returns 0 in case of success, <0 otherwise.
76  */
77  virtual int WeightedSolve(Matrix<double> &A, Vector<double> &b,
78  Vector<double> &weights, Vector<double> &x);
79 
80  /** @brief Solve linear equation system A*x = 0 using weighted linear least
81  squares, i.e. minimize |W*(A*x - b)|^2 with diagonal matrix W
82  composed of given weights, subject to |x| = 1.
83  @return Returns 0 in case of success, <0 otherwise.
84  */
85  virtual int WeightedSolve(Matrix<double> &A, Vector<double> &weights,
86  Vector<double> &x);
87 
88  protected:
89 
91  unsigned _SolutionSize;
92  Matrix<double> _ATA, _AT, _Weight;
94 
95  }; // class LeastSquaresBase
96 
97 
98  /** @class LeastSquaresLapack
99  @ingroup g_mathalgo
100  @brief Linear least squares solver based on Lapack routines.
101  @author MIP
102  */
103  class BIASMathAlgo_EXPORT LeastSquaresLapack : public LeastSquaresBase
104  {
105  public:
106 
108 
109  virtual ~LeastSquaresLapack() {};
110 
111  virtual int Solve(Matrix<double> &A, Vector<double> &b, Vector<double> &x);
112 
113  virtual int Solve(Matrix<double> &A, Vector<double> &x);
114 
115  virtual int WeightedSolve(Matrix<double> &A, Vector<double> &b,
116  Vector<double> &weights, Vector<double> &x);
117 
118  virtual int WeightedSolve(Matrix<double> &A, Vector<double> &weights,
119  Vector<double> &x);
120 
121  }; // class LeastSquaresLapack
122 
123 
124  /** @class LeastSquaresSVD
125  @ingroup g_mathalgo
126  @brief Linear least squares solver based on singular value decomposition.
127  @author MIP
128  */
129  class BIASMathAlgo_EXPORT LeastSquaresSVD : public LeastSquaresBase
130  {
131  public:
132 
133  LeastSquaresSVD() : _svd(), _VT(), _S() {};
134 
135  virtual ~LeastSquaresSVD() {};
136 
137  virtual int Solve(Matrix<double> &A, Vector<double> &b, Vector<double> &x);
138 
139  virtual int Solve(Matrix<double> &A, Vector<double> &x);
140 
141  virtual int WeightedSolve(Matrix<double> &A, Vector<double> &b,
142  Vector<double> &weights, Vector<double> &x);
143 
144  virtual int WeightedSolve(Matrix<double> &A, Vector<double> &weights,
145  Vector<double> &x);
146 
147  protected:
148 
152 
153  }; // class LeastSquaresSVD
154 
155 } // namespace
156 
157 #endif // __LeastSquares_hh__
Base class for linear least squares solvers.
Definition: LeastSquares.hh:45
computes and holds the singular value decomposition of a rectangular (not necessarily quadratic) Matr...
Definition: SVD.hh:92
Vector< double > _ATb
Definition: LeastSquares.hh:93
Matrix< double > _Weight
Definition: LeastSquares.hh:92
Matrix< double > _VT
Vector< double > _S
Linear least squares solver based on Lapack routines.
Linear least squares solver based on singular value decomposition.