00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef TOON_INCLUDE_LAPACK_CHOLESKY_H
00032 #define TOON_INCLUDE_LAPACK_CHOLESKY_H
00033
00034 #include <TooN/TooN.h>
00035
00036 #include <TooN/lapack.h>
00037
00038 #include <assert.h>
00039
00040 namespace TooN {
00041
00042
00071 template <int Size, typename Precision=DefaultPrecision>
00072 class Lapack_Cholesky {
00073 public:
00074
00075 Lapack_Cholesky(){}
00076
00077 template<class P2, class B2>
00078 Lapack_Cholesky(const Matrix<Size, Size, P2, B2>& m)
00079 : my_cholesky(m), my_cholesky_lapack(m) {
00080 SizeMismatch<Size,Size>::test(m.num_rows(), m.num_cols());
00081 do_compute();
00082 }
00083
00085 Lapack_Cholesky(int size) : my_cholesky(size,size), my_cholesky_lapack(size,size) {}
00086
00087 template<class P2, class B2> void compute(const Matrix<Size, Size, P2, B2>& m){
00088 SizeMismatch<Size,Size>::test(m.num_rows(), m.num_cols());
00089 SizeMismatch<Size,Size>::test(m.num_rows(), my_cholesky.num_rows());
00090 my_cholesky_lapack=m;
00091 do_compute();
00092 }
00093
00094
00095
00096 void do_compute(){
00097 int N = my_cholesky.num_rows();
00098 int info;
00099 potrf_("L", &N, my_cholesky_lapack.my_data, &N, &info);
00100 for (int i=0;i<N;i++) {
00101 int j;
00102 for (j=0;j<=i;j++) {
00103 my_cholesky[i][j]=my_cholesky_lapack[j][i];
00104 }
00105
00106
00107 for (;j<N;j++) {
00108 my_cholesky[i][j]=0;
00109 }
00110 }
00111 assert(info >= 0);
00112 if (info > 0) {
00113 my_rank = info-1;
00114 }
00115 }
00116
00117 int rank() const { return my_rank; }
00118
00119 template <int Size2, typename P2, typename B2>
00120 Vector<Size, Precision> backsub (const Vector<Size2, P2, B2>& v) const {
00121 SizeMismatch<Size,Size2>::test(my_cholesky.num_cols(), v.size());
00122
00123 Vector<Size> result(v);
00124 int N=my_cholesky.num_rows();
00125 int NRHS=1;
00126 int info;
00127 potrs_("L", &N, &NRHS, my_cholesky_lapack.my_data, &N, result.my_data, &N, &info);
00128 assert(info==0);
00129 return result;
00130 }
00131
00132 template <int Size2, int Cols2, typename P2, typename B2>
00133 Matrix<Size, Cols2, Precision, ColMajor> backsub (const Matrix<Size2, Cols2, P2, B2>& m) const {
00134 SizeMismatch<Size,Size2>::test(my_cholesky.num_cols(), m.num_rows());
00135
00136 Matrix<Size, Cols2, Precision, ColMajor> result(m);
00137 int N=my_cholesky.num_rows();
00138 int NRHS=m.num_cols();
00139 int info;
00140 potrs_("L", &N, &NRHS, my_cholesky_lapack.my_data, &N, result.my_data, &N, &info);
00141 assert(info==0);
00142 return result;
00143 }
00144
00145 template <int Size2, typename P2, typename B2>
00146 Precision mahalanobis(const Vector<Size2, P2, B2>& v) const {
00147 return v * backsub(v);
00148 }
00149
00150 Matrix<Size,Size,Precision> get_L() const {
00151 return my_cholesky;
00152 }
00153
00154 Precision determinant() const {
00155 Precision det = my_cholesky[0][0];
00156 for (int i=1; i<my_cholesky.num_rows(); i++)
00157 det *= my_cholesky[i][i];
00158 return det*det;
00159 }
00160
00161 Matrix<> get_inverse() const {
00162 Matrix<Size> M(my_cholesky.num_rows(),my_cholesky.num_rows());
00163 M=my_cholesky_lapack;
00164 int N = my_cholesky.num_rows();
00165 int info;
00166 potri_("L", &N, M.my_data, &N, &info);
00167 assert(info == 0);
00168 for (int i=1;i<N;i++) {
00169 for (int j=0;j<i;j++) {
00170 M[i][j]=M[j][i];
00171 }
00172 }
00173 return M;
00174 }
00175
00176 private:
00177 Matrix<Size,Size,Precision> my_cholesky_lapack;
00178 Matrix<Size,Size,Precision> my_cholesky;
00179 int my_rank;
00180 };
00181
00182
00183 }
00184
00185 #endif