namespace ewalena
0.2.15
ewalena is not an acronym
|
00001 // ------------------------------------------------------------------- 00002 // @author Toby D. Young 00003 // @version $Id: solver_quadratic.h 987 2012-11-26 15:16:27Z oneliefleft $ 00004 // 00005 // Copyright 2012 namespace ewalena authors. All rights reserved. 00006 // 00007 // Redistribution and use in source and binary forms, with or without 00008 // modification, are permitted provided that the following conditions 00009 // are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above 00015 // copyright notice, this list of conditions and the following 00016 // disclaimer in the documentation and/or other materials provided 00017 // with the distribution. 00018 // 00019 // THIS SOFTWARE IS PROVIDED BY THE NAMEPSACE EWALENA AUTHORS ``AS 00020 // IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00021 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00022 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00023 // NAMESPACE EWALENA AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 00024 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00025 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00026 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00027 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 00028 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00029 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 00030 // OF THE POSSIBILITY OF SUCH DAMAGE. 00031 // 00032 // The views and conclusions contained in the software and 00033 // documentation are those of the authors and should not be 00034 // interpreted as representing official policies, either expressed or 00035 // implied, of the namespace ewalena authors. 00036 // ------------------------------------------------------------------- 00037 00038 #ifndef __ewalena_solver_quadratic_h 00039 #define __ewalena_solver_quadratic_h 00040 00041 #ifdef EWALENA_HAVE_CONFIG_H 00042 #include <ewalena/base/config.h> 00043 #endif 00044 00045 #include <ewalena/lac/solver_base.h> 00046 00047 #include <algorithm> 00048 00049 namespace ewalena 00050 { 00051 00074 template <typename ValueType = double> 00075 class SolverQuadratic 00076 : 00077 public SolverBase<ValueType> 00078 { 00079 00080 public: 00081 00087 SolverQuadratic (SolverControl<ValueType> &solver_control = 0); 00088 00092 virtual void solve (const Matrix<ValueType> &A, 00093 Vector<ValueType> &lambda, 00094 VectorBasis<ValueType> &x); 00095 00096 protected: 00097 00103 ValueType trace (const Matrix<ValueType> &matrix); 00104 00110 ValueType det (const Matrix<ValueType> &matrix); 00111 00112 00113 }; /* SolverJacobi */ 00114 00115 /*-------------- Inline and Other Functions -----------------------*/ 00116 00117 template <typename ValueType> 00118 inline 00119 ValueType 00120 SolverQuadratic<ValueType>::trace (const Matrix<ValueType> &matrix) 00121 { 00122 assert (matrix.n_cols () == matrix.n_rows ()); 00123 00124 ValueType trace = ValueType (0); 00125 00126 for (unsigned int i=0; i<matrix.n_rows (); ++i) 00127 trace += matrix(i,i); 00128 00129 return trace; 00130 } 00131 00132 00133 template <typename ValueType> 00134 inline 00135 ValueType 00136 SolverQuadratic<ValueType>::det (const Matrix<ValueType> &matrix) 00137 { 00138 assert (matrix.n_cols () == matrix.n_rows ()); 00139 00140 switch (matrix.n_rows ()) 00141 { 00142 case 2: 00143 return 00144 matrix(1,1) * matrix(2,2) - 00145 matrix(1,2) * matrix(2,1); 00146 break; 00147 00148 case 3: 00149 return 00150 matrix(1,1) * matrix(2,2) * matrix(3,3) + 00151 matrix(2,1) * matrix(3,2) * matrix(1,3) + 00152 matrix(3,1) * matrix(1,2) * matrix(2,3) - 00153 matrix(3,1) * matrix(2,2) * matrix(1,3) - 00154 matrix(2,1) * matrix(1,2) * matrix(3,3) - 00155 matrix(1,1) * matrix(3,2) * matrix(2,3); 00156 break; 00157 00158 default: 00159 assert (false); 00160 } 00161 00162 } 00163 00164 } /* namespace ewalena */ 00165 00166 #endif /* __ewalena_solver_quadratic_h */