37 | | /*! |
38 | | * Perfroms a rank-1 update by outer product of vectors: \f$V = V + w v v'\f$. |
39 | | * @param v Vector forming the outer product to be added |
40 | | * @param w weight of updating; can be negative |
41 | | |
42 | | BLAS-2b operation. |
43 | | */ |
44 | | virtual void opupdt ( const vec &v, double w ) = 0; |
45 | | |
46 | | /*! \brief Conversion to full matrix. |
47 | | */ |
48 | | virtual mat to_mat() const = 0; |
49 | | |
50 | | /*! \brief Inplace symmetric multiplication by a SQUARE matrix \f$C\f$, i.e. \f$V = C*V*C'\f$ |
51 | | @param C multiplying matrix, |
52 | | */ |
53 | | virtual void mult_sym ( const mat &C ) = 0; |
54 | | |
55 | | /*! \brief Inplace symmetric multiplication by a SQUARE transpose of matrix \f$C\f$, i.e. \f$V = C'*V*C\f$ |
56 | | @param C multiplying matrix, |
57 | | */ |
58 | | virtual void mult_sym_t ( const mat &C ) = 0; |
59 | | |
60 | | /*! |
61 | | \brief Logarithm of a determinant. |
62 | | |
63 | | */ |
64 | | virtual double logdet() const = 0; |
65 | | |
66 | | /*! |
67 | | \brief Multiplies square root of \f$V\f$ by vector \f$x\f$. |
68 | | |
69 | | Used e.g. in generating normal samples. |
70 | | */ |
71 | | virtual vec sqrt_mult ( const vec &v ) const = 0; |
72 | | |
73 | | /*! |
74 | | \brief Evaluates quadratic form \f$x= v'*V*v\f$; |
75 | | |
76 | | */ |
77 | | virtual double qform ( const vec &v ) const = 0; |
78 | | |
79 | | /*! |
80 | | \brief Evaluates quadratic form \f$x= v'*inv(V)*v\f$; |
81 | | |
82 | | */ |
83 | | virtual double invqform ( const vec &v ) const = 0; |
| 37 | /*! |
| 38 | * Perfroms a rank-1 update by outer product of vectors: \f$V = V + w v v'\f$. |
| 39 | * @param v Vector forming the outer product to be added |
| 40 | * @param w weight of updating; can be negative |
| 41 | |
| 42 | BLAS-2b operation. |
| 43 | */ |
| 44 | virtual void opupdt ( const vec &v, double w ) = 0; |
| 45 | |
| 46 | /*! \brief Conversion to full matrix. |
| 47 | */ |
| 48 | virtual mat to_mat() const = 0; |
| 49 | |
| 50 | /*! \brief Inplace symmetric multiplication by a SQUARE matrix \f$C\f$, i.e. \f$V = C*V*C'\f$ |
| 51 | @param C multiplying matrix, |
| 52 | */ |
| 53 | virtual void mult_sym ( const mat &C ) = 0; |
| 54 | |
| 55 | /*! \brief Inplace symmetric multiplication by a SQUARE transpose of matrix \f$C\f$, i.e. \f$V = C'*V*C\f$ |
| 56 | @param C multiplying matrix, |
| 57 | */ |
| 58 | virtual void mult_sym_t ( const mat &C ) = 0; |
| 59 | |
| 60 | /*! |
| 61 | \brief Logarithm of a determinant. |
| 62 | |
| 63 | */ |
| 64 | virtual double logdet() const = 0; |
| 65 | |
| 66 | /*! |
| 67 | \brief Multiplies square root of \f$V\f$ by vector \f$x\f$. |
| 68 | |
| 69 | Used e.g. in generating normal samples. |
| 70 | */ |
| 71 | virtual vec sqrt_mult ( const vec &v ) const = 0; |
| 72 | |
| 73 | /*! |
| 74 | \brief Evaluates quadratic form \f$x= v'*V*v\f$; |
| 75 | |
| 76 | */ |
| 77 | virtual double qform ( const vec &v ) const = 0; |
| 78 | |
| 79 | /*! |
| 80 | \brief Evaluates quadratic form \f$x= v'*inv(V)*v\f$; |
| 81 | |
| 82 | */ |
| 83 | virtual double invqform ( const vec &v ) const = 0; |
122 | | void opupdt ( const vec &v, double w ); |
123 | | mat to_mat() const; |
124 | | void mult_sym ( const mat &C ); |
125 | | void mult_sym_t ( const mat &C ); |
126 | | //! store result of \c mult_sym in external matrix \f$U\f$ |
127 | | void mult_sym ( const mat &C, fsqmat &U ) const; |
128 | | //! store result of \c mult_sym_t in external matrix \f$U\f$ |
129 | | void mult_sym_t ( const mat &C, fsqmat &U ) const; |
130 | | void clear(); |
131 | | |
132 | | //! Default initialization |
133 | | fsqmat() {}; // mat will be initialized OK |
134 | | //! Default initialization with proper size |
135 | | fsqmat ( const int dim0 ); // mat will be initialized OK |
136 | | //! Constructor |
137 | | fsqmat ( const mat &M ); |
138 | | |
139 | | /*! |
140 | | Some templates require this constructor to compile, but |
141 | | it shouldn't actually be called. |
142 | | */ |
143 | | fsqmat ( const fsqmat &M, const ivec &perm ) { |
144 | | bdm_error ( "not implemented" ); |
145 | | } |
146 | | |
147 | | //! Constructor |
148 | | fsqmat ( const vec &d ) : sqmat ( d.length() ) { |
149 | | M = diag ( d ); |
150 | | }; |
151 | | |
152 | | //! Destructor for future use; |
153 | | virtual ~fsqmat() {}; |
154 | | |
155 | | |
156 | | /*! \brief Matrix inversion preserving the chosen form. |
157 | | |
158 | | @param Inv a space where the inverse is stored. |
159 | | |
160 | | */ |
161 | | void inv ( fsqmat &Inv ) const; |
162 | | |
163 | | double logdet() const { |
164 | | return log ( det ( M ) ); |
165 | | }; |
166 | | double qform ( const vec &v ) const { |
167 | | return ( v* ( M*v ) ); |
168 | | }; |
169 | | double invqform ( const vec &v ) const { |
170 | | return ( v* ( itpp::inv ( M ) *v ) ); |
171 | | }; |
172 | | vec sqrt_mult ( const vec &v ) const { |
173 | | mat Ch = chol ( M ); |
174 | | return Ch*v; |
175 | | }; |
176 | | |
177 | | //! Add another matrix in fsq form with weight w |
178 | | void add ( const fsqmat &fsq2, double w = 1.0 ) { |
179 | | M += fsq2.M; |
180 | | }; |
181 | | |
182 | | //! Access functions |
183 | | void setD ( const vec &nD ) { |
184 | | M = diag ( nD ); |
185 | | } |
186 | | //! Access functions |
187 | | vec getD () { |
188 | | return diag ( M ); |
189 | | } |
190 | | //! Access functions |
191 | | void setD ( const vec &nD, int i ) { |
192 | | for ( int j = i; j < nD.length(); j++ ) { |
193 | | M ( j, j ) = nD ( j - i ); //Fixme can be more general |
194 | | } |
195 | | } |
196 | | |
197 | | |
198 | | //! add another fsqmat matrix |
199 | | fsqmat& operator += ( const fsqmat &A ) { |
200 | | M += A.M; |
201 | | return *this; |
202 | | }; |
203 | | //! subtrack another fsqmat matrix |
204 | | fsqmat& operator -= ( const fsqmat &A ) { |
205 | | M -= A.M; |
206 | | return *this; |
207 | | }; |
208 | | //! multiply by a scalar |
209 | | fsqmat& operator *= ( double x ) { |
210 | | M *= x; |
211 | | return *this; |
212 | | }; |
213 | | |
214 | | //! cast to normal mat |
215 | | operator mat&() { |
216 | | return M; |
217 | | }; |
| 122 | void opupdt ( const vec &v, double w ); |
| 123 | mat to_mat() const; |
| 124 | void mult_sym ( const mat &C ); |
| 125 | void mult_sym_t ( const mat &C ); |
| 126 | //! store result of \c mult_sym in external matrix \f$U\f$ |
| 127 | void mult_sym ( const mat &C, fsqmat &U ) const; |
| 128 | //! store result of \c mult_sym_t in external matrix \f$U\f$ |
| 129 | void mult_sym_t ( const mat &C, fsqmat &U ) const; |
| 130 | void clear(); |
| 131 | |
| 132 | //! Default initialization |
| 133 | fsqmat() {}; // mat will be initialized OK |
| 134 | //! Default initialization with proper size |
| 135 | fsqmat ( const int dim0 ); // mat will be initialized OK |
| 136 | //! Constructor |
| 137 | fsqmat ( const mat &M ); |
| 138 | |
| 139 | /*! |
| 140 | Some templates require this constructor to compile, but |
| 141 | it shouldn't actually be called. |
| 142 | */ |
| 143 | fsqmat ( const fsqmat &M, const ivec &perm ) { |
| 144 | bdm_error ( "not implemented" ); |
| 145 | } |
| 146 | |
| 147 | //! Constructor |
| 148 | fsqmat ( const vec &d ) : sqmat ( d.length() ) { |
| 149 | M = diag ( d ); |
| 150 | }; |
| 151 | |
| 152 | //! Destructor for future use; |
| 153 | virtual ~fsqmat() {}; |
| 154 | |
| 155 | |
| 156 | /*! \brief Matrix inversion preserving the chosen form. |
| 157 | |
| 158 | @param Inv a space where the inverse is stored. |
| 159 | |
| 160 | */ |
| 161 | void inv ( fsqmat &Inv ) const; |
| 162 | |
| 163 | double logdet() const { |
| 164 | return log ( det ( M ) ); |
| 165 | }; |
| 166 | double qform ( const vec &v ) const { |
| 167 | return ( v* ( M*v ) ); |
| 168 | }; |
| 169 | double invqform ( const vec &v ) const { |
| 170 | return ( v* ( itpp::inv ( M ) *v ) ); |
| 171 | }; |
| 172 | vec sqrt_mult ( const vec &v ) const { |
| 173 | mat Ch = chol ( M ); |
| 174 | return Ch*v; |
| 175 | }; |
| 176 | |
| 177 | //! Add another matrix in fsq form with weight w |
| 178 | void add ( const fsqmat &fsq2, double w = 1.0 ) { |
| 179 | M += fsq2.M; |
| 180 | }; |
| 181 | |
| 182 | //! Access functions |
| 183 | void setD ( const vec &nD ) { |
| 184 | M = diag ( nD ); |
| 185 | } |
| 186 | //! Access functions |
| 187 | vec getD () { |
| 188 | return diag ( M ); |
| 189 | } |
| 190 | //! Access functions |
| 191 | void setD ( const vec &nD, int i ) { |
| 192 | for ( int j = i; j < nD.length(); j++ ) { |
| 193 | M ( j, j ) = nD ( j - i ); //Fixme can be more general |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | |
| 198 | //! add another fsqmat matrix |
| 199 | fsqmat& operator += ( const fsqmat &A ) { |
| 200 | M += A.M; |
| 201 | return *this; |
| 202 | }; |
| 203 | //! subtrack another fsqmat matrix |
| 204 | fsqmat& operator -= ( const fsqmat &A ) { |
| 205 | M -= A.M; |
| 206 | return *this; |
| 207 | }; |
| 208 | //! multiply by a scalar |
| 209 | fsqmat& operator *= ( double x ) { |
| 210 | M *= x; |
| 211 | return *this; |
| 212 | }; |
| 213 | |
| 214 | //! cast to normal mat |
| 215 | operator mat&() { |
| 216 | return M; |
| 217 | }; |
237 | | //! Construct by copy of L and D. |
238 | | ldmat ( const mat &L, const vec &D ); |
239 | | //! Construct by decomposition of full matrix V. |
240 | | ldmat ( const mat &V ); |
241 | | //! Construct by restructuring of V0 accordint to permutation vector perm. |
242 | | ldmat ( const ldmat &V0, const ivec &perm ) : sqmat ( V0.rows() ) { |
243 | | ldform ( V0.L.get_cols ( perm ), V0.D ); |
244 | | }; |
245 | | //! Construct diagonal matrix with diagonal D0 |
246 | | ldmat ( vec D0 ); |
247 | | //!Default constructor |
248 | | ldmat (); |
249 | | //! Default initialization with proper size |
250 | | ldmat ( const int dim0 ); |
251 | | |
252 | | //! Destructor for future use; |
253 | | virtual ~ldmat() {}; |
254 | | |
255 | | // Reimplementation of compulsory operatios |
256 | | |
257 | | void opupdt ( const vec &v, double w ); |
258 | | mat to_mat() const; |
259 | | void mult_sym ( const mat &C ); |
260 | | void mult_sym_t ( const mat &C ); |
261 | | //! Add another matrix in LD form with weight w |
262 | | void add ( const ldmat &ld2, double w = 1.0 ); |
263 | | double logdet() const; |
264 | | double qform ( const vec &v ) const; |
265 | | double invqform ( const vec &v ) const; |
266 | | void clear(); |
267 | | vec sqrt_mult ( const vec &v ) const; |
268 | | |
269 | | |
270 | | /*! \brief Matrix inversion preserving the chosen form. |
271 | | @param Inv a space where the inverse is stored. |
272 | | */ |
273 | | void inv ( ldmat &Inv ) const; |
274 | | |
275 | | /*! \brief Symmetric multiplication of \f$U\f$ by a general matrix \f$C\f$, result of which is stored in the current class. |
276 | | @param C matrix to multiply with |
277 | | @param U a space where the inverse is stored. |
278 | | */ |
279 | | void mult_sym ( const mat &C, ldmat &U ) const; |
280 | | |
281 | | /*! \brief Symmetric multiplication of \f$U\f$ by a transpose of a general matrix \f$C\f$, result of which is stored in the current class. |
282 | | @param C matrix to multiply with |
283 | | @param U a space where the inverse is stored. |
284 | | */ |
285 | | void mult_sym_t ( const mat &C, ldmat &U ) const; |
286 | | |
287 | | |
288 | | /*! \brief Transforms general \f$A'D0 A\f$ into pure \f$L'DL\f$ |
289 | | |
290 | | The new decomposition fullfills: \f$A'*diag(D)*A = self.L'*diag(self.D)*self.L\f$ |
291 | | @param A general matrix |
292 | | @param D0 general vector |
293 | | */ |
294 | | void ldform ( const mat &A, const vec &D0 ); |
295 | | |
296 | | //! Access functions |
297 | | void setD ( const vec &nD ) { |
298 | | D = nD; |
299 | | } |
300 | | //! Access functions |
301 | | void setD ( const vec &nD, int i ) { |
302 | | D.replace_mid ( i, nD ); //Fixme can be more general |
303 | | } |
304 | | //! Access functions |
305 | | void setL ( const vec &nL ) { |
306 | | L = nL; |
307 | | } |
| 237 | //! Construct by copy of L and D. |
| 238 | ldmat ( const mat &L, const vec &D ); |
| 239 | //! Construct by decomposition of full matrix V. |
| 240 | ldmat ( const mat &V ); |
| 241 | //! Construct by restructuring of V0 accordint to permutation vector perm. |
| 242 | ldmat ( const ldmat &V0, const ivec &perm ) : sqmat ( V0.rows() ) { |
| 243 | ldform ( V0.L.get_cols ( perm ), V0.D ); |
| 244 | }; |
| 245 | //! Construct diagonal matrix with diagonal D0 |
| 246 | ldmat ( vec D0 ); |
| 247 | //!Default constructor |
| 248 | ldmat (); |
| 249 | //! Default initialization with proper size |
| 250 | ldmat ( const int dim0 ); |
| 251 | |
| 252 | //! Destructor for future use; |
| 253 | virtual ~ldmat() {}; |
| 254 | |
| 255 | // Reimplementation of compulsory operatios |
| 256 | |
| 257 | void opupdt ( const vec &v, double w ); |
| 258 | mat to_mat() const; |
| 259 | void mult_sym ( const mat &C ); |
| 260 | void mult_sym_t ( const mat &C ); |
| 261 | //! Add another matrix in LD form with weight w |
| 262 | void add ( const ldmat &ld2, double w = 1.0 ); |
| 263 | double logdet() const; |
| 264 | double qform ( const vec &v ) const; |
| 265 | double invqform ( const vec &v ) const; |
| 266 | void clear(); |
| 267 | vec sqrt_mult ( const vec &v ) const; |
| 268 | |
| 269 | |
| 270 | /*! \brief Matrix inversion preserving the chosen form. |
| 271 | @param Inv a space where the inverse is stored. |
| 272 | */ |
| 273 | void inv ( ldmat &Inv ) const; |
| 274 | |
| 275 | /*! \brief Symmetric multiplication of \f$U\f$ by a general matrix \f$C\f$, result of which is stored in the current class. |
| 276 | @param C matrix to multiply with |
| 277 | @param U a space where the inverse is stored. |
| 278 | */ |
| 279 | void mult_sym ( const mat &C, ldmat &U ) const; |
| 280 | |
| 281 | /*! \brief Symmetric multiplication of \f$U\f$ by a transpose of a general matrix \f$C\f$, result of which is stored in the current class. |
| 282 | @param C matrix to multiply with |
| 283 | @param U a space where the inverse is stored. |
| 284 | */ |
| 285 | void mult_sym_t ( const mat &C, ldmat &U ) const; |
| 286 | |
| 287 | |
| 288 | /*! \brief Transforms general \f$A'D0 A\f$ into pure \f$L'DL\f$ |
| 289 | |
| 290 | The new decomposition fullfills: \f$A'*diag(D)*A = self.L'*diag(self.D)*self.L\f$ |
| 291 | @param A general matrix |
| 292 | @param D0 general vector |
| 293 | */ |
| 294 | void ldform ( const mat &A, const vec &D0 ); |
| 295 | |
| 296 | //! Access functions |
| 297 | void setD ( const vec &nD ) { |
| 298 | D = nD; |
| 299 | } |
| 300 | //! Access functions |
| 301 | void setD ( const vec &nD, int i ) { |
| 302 | D.replace_mid ( i, nD ); //Fixme can be more general |
| 303 | } |
| 304 | //! Access functions |
| 305 | void setL ( const vec &nL ) { |
| 306 | L = nL; |
| 307 | } |