| 1 | /* |
| 2 | * Definition of Lorene class Matrice |
| 3 | * |
| 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 1999-2001 Philippe Grandclement |
| 8 | * |
| 9 | * This file is part of LORENE. |
| 10 | * |
| 11 | * LORENE is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * LORENE is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with LORENE; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | |
| 28 | #ifndef __MATRICE_H_ |
| 29 | #define __MATRICE_H_ |
| 30 | |
| 31 | /* |
| 32 | * $Id: matrice.h,v 1.14 2014/10/13 08:52:35 j_novak Exp $ |
| 33 | * $Log: matrice.h,v $ |
| 34 | * Revision 1.14 2014/10/13 08:52:35 j_novak |
| 35 | * Lorene classes and functions now belong to the namespace Lorene. |
| 36 | * |
| 37 | * Revision 1.13 2014/10/06 15:09:40 j_novak |
| 38 | * Modified #include directives to use c++ syntax. |
| 39 | * |
| 40 | * Revision 1.12 2005/10/24 09:22:21 p_grandclement |
| 41 | * addition of annule_hard for matrices |
| 42 | * |
| 43 | * Revision 1.11 2005/09/16 12:28:16 j_novak |
| 44 | * New method del_deriv() and shorter version for set(i,j). |
| 45 | * |
| 46 | * Revision 1.10 2005/01/25 12:47:32 j_novak |
| 47 | * Added some member arithmetic and operator=(Tbl). |
| 48 | * |
| 49 | * Revision 1.9 2004/12/29 12:27:35 j_novak |
| 50 | * permute is now a Itbl* which array is sent directly to the LAPACK routines. |
| 51 | * It is now possible to solve a general system (i.e. even if the Matrice |
| 52 | * is not in a banded form). |
| 53 | * |
| 54 | * Revision 1.8 2004/08/24 09:14:40 p_grandclement |
| 55 | * Addition of some new operators, like Poisson in 2d... It now requieres the |
| 56 | * GSL library to work. |
| 57 | * |
| 58 | * Also, the way a variable change is stored by a Param_elliptic is changed and |
| 59 | * no longer uses Change_var but rather 2 Scalars. The codes using that feature |
| 60 | * will requiere some modification. (It should concern only the ones about monopoles) |
| 61 | * |
| 62 | * Revision 1.7 2004/03/22 13:12:42 j_novak |
| 63 | * Modification of comments to use doxygen instead of doc++ |
| 64 | * |
| 65 | * Revision 1.6 2002/09/24 10:49:41 e_gourgoulhon |
| 66 | * |
| 67 | * Modif commentaires. |
| 68 | * |
| 69 | * Revision 1.5 2002/09/24 08:34:12 e_gourgoulhon |
| 70 | * |
| 71 | * Added member function transpose() |
| 72 | * and matrix multiplication |
| 73 | * |
| 74 | * Revision 1.4 2002/09/13 09:17:33 j_novak |
| 75 | * Modif. commentaires |
| 76 | * |
| 77 | * Revision 1.3 2002/06/17 14:05:17 j_novak |
| 78 | * friend functions are now also declared outside the class definition |
| 79 | * |
| 80 | * Revision 1.2 2002/01/03 13:18:40 j_novak |
| 81 | * Optimization: the members set(i,j) and operator(i,j) of class Matrice are |
| 82 | * now defined inline. Matrice is a friend class of Tbl. |
| 83 | * |
| 84 | * Revision 1.1.1.1 2001/11/20 15:19:27 e_gourgoulhon |
| 85 | * LORENE |
| 86 | * |
| 87 | * Revision 2.12 1999/11/30 17:45:28 phil |
| 88 | * changement de prototypage |
| 89 | * |
| 90 | * Revision 2.11 1999/10/25 08:02:11 eric |
| 91 | * Changements commentaires. |
| 92 | * |
| 93 | * Revision 2.10 1999/10/12 16:04:06 phil |
| 94 | * Doc |
| 95 | * |
| 96 | * Revision 2.9 1999/10/12 15:59:09 phil |
| 97 | * Documentation |
| 98 | * |
| 99 | * Revision 2.8 1999/10/12 09:42:07 phil |
| 100 | * retour |
| 101 | * |
| 102 | * Revision 2.7 1999/10/12 09:39:38 phil |
| 103 | * passage en const |
| 104 | * |
| 105 | * Revision 2.6 1999/10/11 09:35:40 phil |
| 106 | * changement prototypage arithmetique |
| 107 | * |
| 108 | * Revision 2.5 1999/10/05 17:02:32 phil |
| 109 | * ajout de determinant et val_propre |
| 110 | * |
| 111 | * Revision 2.4 1999/09/20 11:25:25 phil |
| 112 | * passage en Doc++ |
| 113 | * |
| 114 | * Revision 2.3 1999/09/20 11:18:53 phil |
| 115 | * *** empty log message *** |
| 116 | * |
| 117 | * Revision 2.2 1999/04/13 13:56:11 phil |
| 118 | * suppression de proto.h |
| 119 | * |
| 120 | * Revision 2.1 1999/04/07 14:53:38 phil |
| 121 | * Changement de prototypage |
| 122 | * |
| 123 | * Revision 2.0 1999/04/07 14:03:59 phil |
| 124 | * *** empty log message *** |
| 125 | * |
| 126 | * |
| 127 | * $Header: /cvsroot/Lorene/C++/Include/matrice.h,v 1.14 2014/10/13 08:52:35 j_novak Exp $ |
| 128 | * |
| 129 | */ |
| 130 | |
| 131 | //fichiers includes |
| 132 | #include <cstdio> |
| 133 | |
| 134 | #include "type_parite.h" |
| 135 | #include "tbl.h" |
| 136 | #include "itbl.h" |
| 137 | |
| 138 | namespace Lorene { |
| 139 | /** |
| 140 | * Matrix handling. |
| 141 | * The matrix can be stored in the usual way in \c std, in a band-way by |
| 142 | * \c band and on a LU-decomposition by the two arrays \c lu and |
| 143 | * \c permute. All the storage conventions are those af \b LAPACK which is |
| 144 | * used to make the LU-decomposition, the inversion and to compute the |
| 145 | * eigenvalues of the matrix. All those representations are redondant, that means |
| 146 | * that doing the LU-decomposition, for example, does NOT destroy |
| 147 | * previously calculated type of storage. |
| 148 | * |
| 149 | * \ingroup (util) |
| 150 | */ |
| 151 | |
| 152 | class Matrice { |
| 153 | //Elements |
| 154 | private: |
| 155 | |
| 156 | int etat ; ///< logical state \c (ETATZERO, ETATQCQ or ETATNONDEF) |
| 157 | |
| 158 | Tbl* std ; ///< Pointer on the array of the standard representation. |
| 159 | |
| 160 | |
| 161 | mutable int ku ; ///< Number of upper-diagonals in the band representation. |
| 162 | mutable int kl ; ///< Number of lower-diagonals in the band representation. |
| 163 | |
| 164 | /** |
| 165 | * Pointer on the array of the band representation of a square matrix. |
| 166 | * To be precise, \f$ A(i, j)\f$ is stored in \c band |
| 167 | * \f$[ku+1+i-j, j]\f$ for \f$\mathrm {max}(1, j-ku) \leq i \leq |
| 168 | * \mathrm{min} (n, j+kl)\f$, \e n being the size of the matrix. |
| 169 | */ |
| 170 | mutable Tbl* band ; |
| 171 | |
| 172 | |
| 173 | mutable Tbl* lu ; ///< Pointer on the first array of the LU-representation. |
| 174 | mutable Itbl* permute ; ///< Pointer on the second array of the LU-representation. |
| 175 | |
| 176 | // Constructeurs destructeurs |
| 177 | public: |
| 178 | /** |
| 179 | * Standard constructor. |
| 180 | * All the representations are set to \c ETATNONDEF. |
| 181 | * @param size1 [input] number of lines. |
| 182 | * @param size2 [input] number of columns. |
| 183 | */ |
| 184 | Matrice (int size1, int size2 ) ; |
| 185 | |
| 186 | Matrice (const Matrice& ) ; ///< Constructor by copy. |
| 187 | |
| 188 | /** |
| 189 | * Constructor from a \c Tbl. |
| 190 | * @param tab [input] 2-dimension or 1-dimension array |
| 191 | * |
| 192 | * If \c tab is a 1-dimension \c Tbl, a single-column matrix is created, |
| 193 | * otherwise \c *std is simply constructed by a \c Tbl copy of \c tab. |
| 194 | * |
| 195 | */ |
| 196 | Matrice (const Tbl& tab) ; |
| 197 | |
| 198 | ~Matrice() ; ///< Destructor |
| 199 | |
| 200 | //Gestion memoire |
| 201 | /** |
| 202 | * Logical destructor : dellocates the memory of the various used |
| 203 | * representations. |
| 204 | * |
| 205 | */ |
| 206 | private: |
| 207 | void del_t() ; |
| 208 | void del_deriv() ; ///< Deletes the (mutable) derived members: band, lu, permute |
| 209 | |
| 210 | // manipulation des etats |
| 211 | public: |
| 212 | /// Returns the logical state. |
| 213 | int get_etat() const { return etat ; }; |
| 214 | |
| 215 | /** |
| 216 | * Sets the logical state to \c ETATQCQ (ordinary state). |
| 217 | * The state of \c *std is now \c ETATQCQ and the one of all the |
| 218 | * other representations is \c ETATNONDEF. |
| 219 | */ |
| 220 | void set_etat_qcq() ; |
| 221 | |
| 222 | /** |
| 223 | * Sets the logical state to \c ETATZERO (zero). |
| 224 | * The state of \c *std is now \c ETATZERO and the one of all the |
| 225 | * other representations is \c ETATNONDEF. |
| 226 | */ |
| 227 | void set_etat_zero() ; |
| 228 | |
| 229 | /** |
| 230 | * Sets the logical state to \c ETATNONDEF (undefined state). |
| 231 | * The state of of all the representations is now \c ETATNONDEF. |
| 232 | */ |
| 233 | void set_etat_nondef() ; |
| 234 | |
| 235 | /** |
| 236 | * Sets the logical state to \c ETATQCQ (undefined state). |
| 237 | * And sets all the components to zero |
| 238 | */ |
| 239 | void annule_hard() ; |
| 240 | |
| 241 | |
| 242 | public: |
| 243 | /** |
| 244 | * Returns the dimension of the matrix. |
| 245 | * @param i [input] if \e i =0 returns the number of lines and if \e i =2 |
| 246 | * returns the number of columns. |
| 247 | */ |
| 248 | int get_dim(int i) const ; |
| 249 | |
| 250 | /// Returns the array of matrix elements |
| 251 | Tbl get_array() const {return *std; } ; |
| 252 | |
| 253 | // affectation |
| 254 | public: |
| 255 | /** |
| 256 | * Sets all the element of \c *std to \e x. |
| 257 | * The other representations are set to \c ETATNONDEF. |
| 258 | */ |
| 259 | void operator=(double x) ; |
| 260 | |
| 261 | void operator=(const Matrice& ) ; ///< Assignement to another \c Matrice. |
| 262 | void operator=(const Tbl& ) ; ///< Assignement to a \c Tbl. |
| 263 | |
| 264 | //Impression |
| 265 | friend ostream& operator<<(ostream& , const Matrice& ) ; ///< Display |
| 266 | |
| 267 | // extraction d'un element : |
| 268 | public: |
| 269 | /** |
| 270 | * Read/write of a particuliar element. |
| 271 | * This is done in \c *std and all the other representations are no |
| 272 | * longer valid. |
| 273 | * @param j [input] line coordinate. |
| 274 | * @param i [input] column coordinate. |
| 275 | * |
| 276 | */ |
| 277 | double& set(int j, int i) { |
| 278 | assert (etat == ETATQCQ) ; |
| 279 | assert ((i>=0) && (i<std->dim.dim[0])) ; |
| 280 | assert( (j>=0) && (j<std->dim.dim[1]) ) ; |
| 281 | if ( (band != 0x0) || (lu != 0x0) ) del_deriv() ; |
| 282 | return std->t[std->dim.dim[0] * j + i] ; |
| 283 | } ; |
| 284 | |
| 285 | /** |
| 286 | * Read-only of a particuliar element. |
| 287 | * @param j [input] line coordinate. |
| 288 | * @param i [input] column coordinate. |
| 289 | */ |
| 290 | double operator()(int j , int i) const { |
| 291 | assert(etat != ETATNONDEF) ; |
| 292 | assert( (i>=0) && (i<std->dim.dim[0]) ) ; |
| 293 | assert( (j>=0) && (j<std->dim.dim[1]) ) ; |
| 294 | if (etat == ETATZERO) { |
| 295 | double zero = 0. ; |
| 296 | return zero ; |
| 297 | } |
| 298 | else return std->t[std->dim.dim[0] * j + i] ; |
| 299 | }; |
| 300 | |
| 301 | // Passage matrice a bande |
| 302 | /** |
| 303 | * Calculate the band storage of \c *std. |
| 304 | * Please note that this function does NOT check if \c *std |
| 305 | * represents a real band-matrix. |
| 306 | * @param up [input] number of upper-diagonals. |
| 307 | * @param low [input] number of lower-diagonals. |
| 308 | */ |
| 309 | void set_band (int up, int low) const ; |
| 310 | |
| 311 | // Decomposition LU |
| 312 | /** |
| 313 | * Calculate the LU-representation, assuming the band-storage has been |
| 314 | * done. The calculus is done using \b LAPACK. |
| 315 | */ |
| 316 | void set_lu () const ; |
| 317 | |
| 318 | // Inversion de la matrice |
| 319 | /** |
| 320 | * Solves the linear system represented by the matrix. |
| 321 | * The calculus assumes the the LU-decomposition has been done and is |
| 322 | * conducted using \b LAPACK. |
| 323 | * @param sec_membre [input] the right-hand side of the system. |
| 324 | */ |
| 325 | Tbl inverse (const Tbl& sec_membre) const ; |
| 326 | |
| 327 | // Les valeurs propres : |
| 328 | /** |
| 329 | * Returns the eigenvalues of the matrix, calculated using \b LAPACK. |
| 330 | * @return contains the real and the imaginary parts of the |
| 331 | * eigenvalues. The real parts are in \c Tbl \e (0, *) and |
| 332 | * the imaginary parts in \c Tbl \e (1, *). |
| 333 | */ |
| 334 | Tbl val_propre() const ; |
| 335 | /** |
| 336 | * Returns the eigenvectors of the matrix, calculated using \b LAPACK. |
| 337 | * |
| 338 | */ |
| 339 | Matrice vect_propre() const ; |
| 340 | |
| 341 | /** |
| 342 | * Computes the determinant of the matrix, using \b LAPACK and the |
| 343 | * standard decomposition. |
| 344 | */ |
| 345 | double determinant() const ; |
| 346 | |
| 347 | /** |
| 348 | * Computes the transpose matrix |
| 349 | */ |
| 350 | Matrice transpose() const ; |
| 351 | |
| 352 | |
| 353 | // Member arithmetics |
| 354 | // ------------------ |
| 355 | public: |
| 356 | /// Addition of a \c Matrice to \c this |
| 357 | void operator+=(const Matrice &) ; |
| 358 | void operator+=(double) ; ///< Addition of a \c double to \c this |
| 359 | /// Subtraction of a \c Matrice to \c this |
| 360 | void operator-=(const Matrice &) ; |
| 361 | void operator-=(double) ; ///< Subtraction of a \c double to \c this |
| 362 | void operator*=(double) ; ///< Multiplication of \c this by a \c double |
| 363 | void operator/=(double) ; ///< Division of \c this by a \c double |
| 364 | |
| 365 | // Operateurs amis |
| 366 | friend Matrice operator+ (const Matrice&, const Matrice& ) ; |
| 367 | friend Matrice operator- (const Matrice&, const Matrice& ) ; |
| 368 | friend Matrice operator* (const Matrice&, double ) ; |
| 369 | friend Matrice operator* (double, const Matrice& ) ; |
| 370 | friend Matrice operator* (const Matrice&, const Matrice& ) ; |
| 371 | friend Matrice operator/ (const Matrice&, double ) ; |
| 372 | } ; |
| 373 | ostream& operator<<(ostream& , const Matrice& ) ; |
| 374 | |
| 375 | /** |
| 376 | * \defgroup mat_mat Matrice arithmetics |
| 377 | * \ingroup (util) |
| 378 | * @{ |
| 379 | */ |
| 380 | Matrice operator+ (const Matrice&, const Matrice& ) ; ///< \c Matrice + \c Matrice |
| 381 | Matrice operator- (const Matrice&, const Matrice& ) ; ///< \c Matrice - \c Matrice |
| 382 | Matrice operator* (const Matrice&, double ) ;///< \c Matrice * \c double |
| 383 | Matrice operator* (double, const Matrice& ) ;///< \c double * \c Matrice |
| 384 | Matrice operator* (const Matrice&, const Matrice& ) ; ///< Matrix product |
| 385 | Matrice operator/ (const Matrice&, double ) ; ///< \c Matrice / \c double |
| 386 | |
| 387 | /**@} */ |
| 388 | |
| 389 | } |
| 390 | #endif |