| 1 |
/* |
| 2 |
* Methods of class Matrice |
| 3 |
* |
| 4 |
* (see file matrice.h for documentation) |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
/* |
| 9 |
* Copyright (c) 1999-2001 Philippe Grandclement |
| 10 |
* |
| 11 |
* This file is part of LORENE. |
| 12 |
* |
| 13 |
* LORENE is free software; you can redistribute it and/or modify |
| 14 |
* it under the terms of the GNU General Public License as published by |
| 15 |
* the Free Software Foundation; either version 2 of the License, or |
| 16 |
* (at your option) any later version. |
| 17 |
* |
| 18 |
* LORENE is distributed in the hope that it will be useful, |
| 19 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
* GNU General Public License for more details. |
| 22 |
* |
| 23 |
* You should have received a copy of the GNU General Public License |
| 24 |
* along with LORENE; if not, write to the Free Software |
| 25 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 26 |
* |
| 27 |
*/ |
| 28 |
|
| 29 |
|
| 30 |
char matrice_C[] = "$Header: /cvsroot/Lorene/C++/Source/Matrice/matrice.C,v 1.19 2014/10/13 08:53:07 j_novak Exp $" ; |
| 31 |
|
| 32 |
/* |
| 33 |
* $Id: matrice.C,v 1.19 2014/10/13 08:53:07 j_novak Exp $ |
| 34 |
* $Log: matrice.C,v $ |
| 35 |
* Revision 1.19 2014/10/13 08:53:07 j_novak |
| 36 |
* Lorene classes and functions now belong to the namespace Lorene. |
| 37 |
* |
| 38 |
* Revision 1.18 2014/10/06 15:13:14 j_novak |
| 39 |
* Modified #include directives to use c++ syntax. |
| 40 |
* |
| 41 |
* Revision 1.17 2008/08/19 06:42:00 j_novak |
| 42 |
* Minor modifications to avoid warnings with gcc 4.3. Most of them concern |
| 43 |
* cast-type operations, and constant strings that must be defined as const char* |
| 44 |
* |
| 45 |
* Revision 1.16 2006/06/05 09:47:42 j_novak |
| 46 |
* Initialisation of the member band to zero, in order not to have messages from |
| 47 |
* the memory debugger. |
| 48 |
* |
| 49 |
* Revision 1.15 2005/11/24 14:07:29 j_novak |
| 50 |
* Minor speed enhancement for annule_hard(). |
| 51 |
* |
| 52 |
* Revision 1.14 2005/10/24 12:42:32 p_grandclement |
| 53 |
* correction of annule_hard |
| 54 |
* |
| 55 |
* Revision 1.13 2005/10/24 09:22:24 p_grandclement |
| 56 |
* addition of annule_hard for matrices |
| 57 |
* |
| 58 |
* Revision 1.12 2005/09/16 12:29:02 j_novak |
| 59 |
* New method del_deriv() and reorganization of band, lu, permute handling. |
| 60 |
* |
| 61 |
* Revision 1.11 2005/01/25 12:47:34 j_novak |
| 62 |
* Added some member arithmetic and operator=(Tbl). |
| 63 |
* |
| 64 |
* Revision 1.10 2004/12/29 12:27:36 j_novak |
| 65 |
* permute is now a Itbl* which array is sent directly to the LAPACK routines. |
| 66 |
* It is now possible to solve a general system (i.e. even if the Matrice |
| 67 |
* is not in a banded form). |
| 68 |
* |
| 69 |
* Revision 1.9 2004/10/05 15:44:19 j_novak |
| 70 |
* Minor speed enhancements. |
| 71 |
* |
| 72 |
* Revision 1.8 2004/08/24 09:14:43 p_grandclement |
| 73 |
* Addition of some new operators, like Poisson in 2d... It now requieres the |
| 74 |
* GSL library to work. |
| 75 |
* |
| 76 |
* Also, the way a variable change is stored by a Param_elliptic is changed and |
| 77 |
* no longer uses Change_var but rather 2 Scalars. The codes using that feature |
| 78 |
* will requiere some modification. (It should concern only the ones about monopoles) |
| 79 |
* |
| 80 |
* Revision 1.7 2003/12/19 16:21:44 j_novak |
| 81 |
* Shadow hunt |
| 82 |
* |
| 83 |
* Revision 1.6 2002/10/16 14:36:42 j_novak |
| 84 |
* Reorganization of #include instructions of standard C++, in order to |
| 85 |
* use experimental version 3 of gcc. |
| 86 |
* |
| 87 |
* Revision 1.5 2002/09/24 10:51:16 e_gourgoulhon |
| 88 |
* |
| 89 |
* The case of a 1D Tbl in the constructor from Tbl is now taken into account |
| 90 |
* (resulting in a single-column matrix). |
| 91 |
* |
| 92 |
* Revision 1.4 2002/09/24 08:36:44 e_gourgoulhon |
| 93 |
* |
| 94 |
* Corrected error in output (operator<<) : permutted number of rows and columns |
| 95 |
* |
| 96 |
* Added matrix multiplication |
| 97 |
* Added function transpose() |
| 98 |
* |
| 99 |
* Revision 1.3 2002/09/09 13:00:39 e_gourgoulhon |
| 100 |
* Modification of declaration of Fortran 77 prototypes for |
| 101 |
* a better portability (in particular on IBM AIX systems): |
| 102 |
* All Fortran subroutine names are now written F77_* and are |
| 103 |
* defined in the new file C++/Include/proto_f77.h. |
| 104 |
* |
| 105 |
* Revision 1.2 2002/01/03 13:18:41 j_novak |
| 106 |
* Optimization: the members set(i,j) and operator(i,j) of class Matrice are |
| 107 |
* now defined inline. Matrice is a friend class of Tbl. |
| 108 |
* |
| 109 |
* Revision 1.1.1.1 2001/11/20 15:19:28 e_gourgoulhon |
| 110 |
* LORENE |
| 111 |
* |
| 112 |
* Revision 2.9 1999/12/24 10:19:16 eric |
| 113 |
* Suppression des definitions de nbl et nbc lignes 149 et 150. |
| 114 |
* |
| 115 |
* Revision 2.8 1999/11/30 17:45:16 phil |
| 116 |
* changerment prototypage |
| 117 |
* |
| 118 |
* Revision 2.7 1999/10/12 15:49:16 phil |
| 119 |
* apres set band, lu et permute ne sont plus a jour ... |
| 120 |
* |
| 121 |
* Revision 2.6 1999/10/12 09:42:17 phil |
| 122 |
* retrour versian anterieure |
| 123 |
* |
| 124 |
* Revision 2.5 1999/10/12 09:39:07 phil |
| 125 |
* passage en const |
| 126 |
* |
| 127 |
* Revision 2.4 1999/10/11 09:35:07 phil |
| 128 |
* ajout de determinant et val_propre + modif de operator= (const Matrice&) |
| 129 |
* |
| 130 |
* Revision 2.3 1999/10/05 17:02:46 phil |
| 131 |
* ajout de determinant et val_propre |
| 132 |
* |
| 133 |
* Revision 2.2 1999/04/13 13:57:23 phil |
| 134 |
* ajout proto.h |
| 135 |
* |
| 136 |
* Revision 2.1 1999/04/07 14:18:51 phil |
| 137 |
* optimisation egalite |
| 138 |
* |
| 139 |
* Revision 2.0 1999/04/07 14:10:05 phil |
| 140 |
* *** empty log message *** |
| 141 |
* |
| 142 |
* |
| 143 |
* $Header: /cvsroot/Lorene/C++/Source/Matrice/matrice.C,v 1.19 2014/10/13 08:53:07 j_novak Exp $ |
| 144 |
* |
| 145 |
*/ |
| 146 |
|
| 147 |
|
| 148 |
//fichiers includes |
| 149 |
#include <cstdlib> |
| 150 |
#include "matrice.h" |
| 151 |
#include "proto_f77.h" |
| 152 |
|
| 153 |
//Destructeur logique |
| 154 |
|
| 155 |
namespace Lorene { |
| 156 |
void Matrice::del_t() { |
| 157 |
if (std != 0x0) delete std ; |
| 158 |
std = 0x0 ; |
| 159 |
del_deriv() ; |
| 160 |
} |
| 161 |
|
| 162 |
//Destructeur des quantites derivees |
| 163 |
|
| 164 |
void Matrice::del_deriv() { |
| 165 |
if (band != 0x0) delete band ; |
| 166 |
if (lu != 0x0) delete lu ; |
| 167 |
if (permute != 0x0) delete permute ; |
| 168 |
band = 0x0 ; |
| 169 |
lu = 0x0 ; |
| 170 |
permute = 0x0 ; |
| 171 |
} |
| 172 |
|
| 173 |
//Manipulation des etats |
| 174 |
|
| 175 |
void Matrice::set_etat_qcq() { |
| 176 |
std->set_etat_qcq() ; |
| 177 |
del_deriv() ; |
| 178 |
etat = ETATQCQ ; |
| 179 |
} |
| 180 |
|
| 181 |
void Matrice::set_etat_zero() { |
| 182 |
std->set_etat_zero() ; |
| 183 |
del_deriv() ; |
| 184 |
etat = ETATZERO ; |
| 185 |
} |
| 186 |
|
| 187 |
void Matrice::set_etat_nondef() { |
| 188 |
if (std != 0x0) std->set_etat_nondef() ; |
| 189 |
del_deriv() ; |
| 190 |
etat = ETATNONDEF ; |
| 191 |
} |
| 192 |
|
| 193 |
void Matrice::annule_hard() { |
| 194 |
std->set_etat_qcq() ; |
| 195 |
del_deriv() ; |
| 196 |
etat = ETATQCQ ; |
| 197 |
|
| 198 |
for (int i=0 ; i<std->get_taille() ; i++) |
| 199 |
std->t[i] = 0 ; |
| 200 |
} |
| 201 |
|
| 202 |
// Constructeurs |
| 203 |
Matrice::Matrice (int i, int j) { |
| 204 |
etat = ETATNONDEF ; |
| 205 |
std = new Tbl(i, j) ; |
| 206 |
kl = 0 ; |
| 207 |
ku = 0 ; |
| 208 |
band = 0x0 ; |
| 209 |
lu = 0x0 ; |
| 210 |
permute = 0x0 ; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
Matrice::Matrice (const Matrice & source) { |
| 215 |
etat = source.etat ; |
| 216 |
kl = source.kl ; |
| 217 |
ku = source.ku ; |
| 218 |
std = new Tbl(*source.std) ; |
| 219 |
if (source.band != 0x0) band = new Tbl(*source.band) ; |
| 220 |
else band = 0x0 ; |
| 221 |
if (source.lu != 0x0) lu = new Tbl(*source.lu) ; |
| 222 |
else lu = 0x0 ; |
| 223 |
if (source.permute != 0x0) permute = new Itbl(*source.permute) ; |
| 224 |
else permute = 0x0 ; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
Matrice::Matrice (const Tbl & source) { |
| 229 |
etat = source.get_etat() ; |
| 230 |
kl = 0 ; |
| 231 |
ku = 0 ; |
| 232 |
if (source.get_ndim() == 1) { // column vector |
| 233 |
int n = source.get_taille() ; |
| 234 |
std = new Tbl(n,1) ; |
| 235 |
if (source.get_etat() == ETATZERO) { |
| 236 |
std->set_etat_zero() ; |
| 237 |
} |
| 238 |
else { |
| 239 |
assert( source.get_etat() == ETATQCQ ) ; |
| 240 |
std->set_etat_qcq() ; |
| 241 |
for (int i=0; i<n; i++) { |
| 242 |
std->t[i] = source.t[i] ; |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
else { // 2D Tbl |
| 247 |
std = new Tbl(source) ; |
| 248 |
} |
| 249 |
band = 0x0 ; |
| 250 |
lu = 0x0 ; |
| 251 |
permute = 0x0 ; |
| 252 |
} |
| 253 |
|
| 254 |
// destructeur |
| 255 |
Matrice::~Matrice() { |
| 256 |
del_t() ; |
| 257 |
} |
| 258 |
|
| 259 |
// Extraction des dimensions |
| 260 |
int Matrice::get_dim(int i) const { |
| 261 |
return std->get_dim(i) ; |
| 262 |
} |
| 263 |
|
| 264 |
// affectation |
| 265 |
void Matrice::operator= (double x) { |
| 266 |
if (x == 0 ) set_etat_zero() ; |
| 267 |
else { |
| 268 |
set_etat_qcq() ; |
| 269 |
*std = x ; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
void Matrice::operator= (const Matrice &source) { |
| 274 |
|
| 275 |
assert (std->get_dim(0) == source.std->get_dim(0)) ; |
| 276 |
assert (std->get_dim(1) == source.std->get_dim(1)) ; |
| 277 |
|
| 278 |
switch (source.etat) { |
| 279 |
case ETATNONDEF : |
| 280 |
set_etat_nondef() ; |
| 281 |
break ; |
| 282 |
case ETATZERO : |
| 283 |
set_etat_zero() ; |
| 284 |
break ; |
| 285 |
case ETATQCQ : |
| 286 |
set_etat_qcq() ; |
| 287 |
del_t() ; |
| 288 |
|
| 289 |
if (source.std != 0x0) |
| 290 |
std = new Tbl(*source.std) ; |
| 291 |
|
| 292 |
if (source.band != 0x0) { |
| 293 |
band = new Tbl(*source.band) ; |
| 294 |
ku = source.ku ; |
| 295 |
kl = source.kl ; |
| 296 |
} |
| 297 |
|
| 298 |
if (source.lu != 0x0) { |
| 299 |
lu = new Tbl(*source.lu) ; |
| 300 |
permute = new Itbl(*source.permute) ; |
| 301 |
} |
| 302 |
break ; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
void Matrice::operator= (const Tbl &source) { |
| 307 |
|
| 308 |
assert (std->get_dim(0) == source.get_dim(0)) ; |
| 309 |
assert (std->get_dim(1) == source.get_dim(1)) ; |
| 310 |
|
| 311 |
switch (source.etat) { |
| 312 |
case ETATNONDEF : |
| 313 |
set_etat_nondef() ; |
| 314 |
break ; |
| 315 |
case ETATZERO : |
| 316 |
set_etat_zero() ; |
| 317 |
break ; |
| 318 |
case ETATQCQ : |
| 319 |
set_etat_qcq() ; |
| 320 |
del_t() ; |
| 321 |
|
| 322 |
assert (source.t != 0x0) ; |
| 323 |
std = new Tbl(source) ; |
| 324 |
break ; |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
//Impression |
| 330 |
ostream& operator<< (ostream& flux, const Matrice & source) { |
| 331 |
switch (source.std->get_etat()) { |
| 332 |
case ETATZERO : |
| 333 |
flux << "Null matrix. " << endl ; |
| 334 |
break ; |
| 335 |
case ETATNONDEF : |
| 336 |
flux << "Undefined matrix. " << endl ; |
| 337 |
break ; |
| 338 |
case ETATQCQ : |
| 339 |
int nbl = source.std->get_dim(1) ; |
| 340 |
int nbc = source.std->get_dim(0) ; |
| 341 |
flux << "Matrix " << nbl << " * " << nbc << endl ; |
| 342 |
for (int i=0 ; i<nbl ; i++) { |
| 343 |
for (int j=0 ; j<nbc ; j++) |
| 344 |
flux << (*source.std)(i, j) << " " ; |
| 345 |
flux << endl ; |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
flux << endl ; |
| 350 |
|
| 351 |
if ((source.band != 0x0) && (source.band->get_etat() != ETATNONDEF)) { |
| 352 |
flux << "Matrix : " << source.ku << " upper diags. and " |
| 353 |
<< source.kl << " lower diags." << endl ; |
| 354 |
} |
| 355 |
// else flux << "Diagonalisation non faite." << endl ; |
| 356 |
|
| 357 |
if ((source.lu != 0x0) && (source.lu->get_etat() != ETATNONDEF)) |
| 358 |
flux << "LU factorization done." << endl ; |
| 359 |
|
| 360 |
return flux ; |
| 361 |
} |
| 362 |
|
| 363 |
// Passage matrice a bande : stockage LAPACK |
| 364 |
void Matrice::set_band (int u, int l) const { |
| 365 |
if (band != 0x0) return ; |
| 366 |
else { |
| 367 |
int n = std->get_dim(0) ; |
| 368 |
assert (n == std->get_dim(1)) ; |
| 369 |
|
| 370 |
ku = u ; kl = l ; |
| 371 |
int ldab = 2*l+u+1 ; |
| 372 |
band = new Tbl(ldab*n) ; |
| 373 |
|
| 374 |
band->annule_hard() ; |
| 375 |
|
| 376 |
for (int i=0 ; i<u ; i++) |
| 377 |
for (int j=u-i ; j<n ; j++) |
| 378 |
band->set(j*ldab+i+l) = (*this)(j-u+i, j) ; |
| 379 |
|
| 380 |
for (int j=0 ; j<n ; j++) |
| 381 |
band->set(j*ldab+u+l) = (*this)(j, j) ; |
| 382 |
|
| 383 |
for (int i=u+1 ; i<u+l+1 ; i++) |
| 384 |
for (int j=0 ; j<n-i+u ; j++) |
| 385 |
band->set(j*ldab+i+l) = (*this) (i+j-u, j) ; |
| 386 |
|
| 387 |
} |
| 388 |
return ; |
| 389 |
} |
| 390 |
|
| 391 |
//Decomposition UL : stockage LAPACK |
| 392 |
void Matrice::set_lu() const { |
| 393 |
if (lu != 0x0) { |
| 394 |
assert (permute != 0x0) ; |
| 395 |
return ; |
| 396 |
} |
| 397 |
else { |
| 398 |
// Decomposition LU |
| 399 |
int n = std->get_dim(0) ; |
| 400 |
int ldab, info ; |
| 401 |
permute = new Itbl(n) ; |
| 402 |
permute->set_etat_qcq() ; |
| 403 |
|
| 404 |
// Cas d'une matrice a bandes |
| 405 |
if (band != 0x0) { |
| 406 |
assert (band->get_etat() == ETATQCQ) ; |
| 407 |
ldab = 2*kl+ku+1 ; |
| 408 |
lu = new Tbl(*band) ; |
| 409 |
|
| 410 |
F77_dgbtrf(&n, &n, &kl, &ku, lu->t, &ldab, permute->t, &info) ; |
| 411 |
} |
| 412 |
else { // matrice generale |
| 413 |
assert (std->get_etat() == ETATQCQ) ; |
| 414 |
ldab = n ; |
| 415 |
lu = new Tbl(*std) ; |
| 416 |
|
| 417 |
F77_dgetrf(&n, &n, lu->t, &ldab, permute->t, &info) ; |
| 418 |
} |
| 419 |
} |
| 420 |
return ; |
| 421 |
} |
| 422 |
|
| 423 |
// Solution de Ax = B : utilisation de LAPACK et decomposition lu. |
| 424 |
Tbl Matrice::inverse (const Tbl& source) const { |
| 425 |
|
| 426 |
assert(lu != 0x0) ; |
| 427 |
assert(lu->get_etat() == ETATQCQ) ; |
| 428 |
assert(permute != 0x0) ; |
| 429 |
assert(permute->get_etat() == ETATQCQ) ; |
| 430 |
|
| 431 |
int n = source.get_dim(0) ; |
| 432 |
assert (get_dim(1) == n) ; |
| 433 |
int ldab, info ; |
| 434 |
const char* trans ; |
| 435 |
int nrhs = 1 ; |
| 436 |
int ldb = n ; |
| 437 |
|
| 438 |
Tbl res(source) ; |
| 439 |
|
| 440 |
if (band != 0x0) { //Cas d'une matrice a bandes |
| 441 |
ldab = 2*kl+ku+1 ; |
| 442 |
trans = "N" ; |
| 443 |
F77_dgbtrs(trans, &n, &kl, &ku, &nrhs, lu->t, |
| 444 |
&ldab, permute->t, res.t, &ldb, &info); |
| 445 |
} |
| 446 |
else { // Cas general |
| 447 |
ldab = n ; |
| 448 |
trans = "T" ; // stockage different entre le C et le fortran |
| 449 |
F77_dgetrs(trans, &n, &nrhs, lu->t, &ldab, permute->t, |
| 450 |
res.t, &ldb, &info) ; |
| 451 |
} |
| 452 |
|
| 453 |
return res ; |
| 454 |
} |
| 455 |
|
| 456 |
// Renvoit les valeurs propres de la matrice (appel de LAPACK) : |
| 457 |
Tbl Matrice::val_propre() const { |
| 458 |
|
| 459 |
assert (etat != ETATNONDEF) ; |
| 460 |
assert (std != 0x0) ; |
| 461 |
|
| 462 |
const char* jobvl = "N" ; |
| 463 |
const char* jobvr = "N" ; |
| 464 |
|
| 465 |
int n = get_dim(0) ; |
| 466 |
assert (n == get_dim(1)) ; |
| 467 |
|
| 468 |
double* a = new double [n*n] ; |
| 469 |
for (int i=0 ; i<n*n ; i++) |
| 470 |
a[i] = std->t[i] ; |
| 471 |
|
| 472 |
int lda = n ; |
| 473 |
double* wr = new double[n] ; |
| 474 |
double* wi = new double[n] ; |
| 475 |
|
| 476 |
int ldvl = 1 ; |
| 477 |
double* vl = 0x0 ; |
| 478 |
int ldvr = 1 ; |
| 479 |
double* vr = 0x0 ; |
| 480 |
|
| 481 |
int ldwork = 3*n ; |
| 482 |
double* work = new double[ldwork] ; |
| 483 |
|
| 484 |
int info ; |
| 485 |
|
| 486 |
F77_dgeev(jobvl, jobvr, &n, a, &lda, wr, wi, vl, &ldvl, vr, &ldvr, |
| 487 |
work, &ldwork, &info) ; |
| 488 |
|
| 489 |
Tbl result(2, n) ; |
| 490 |
result.set_etat_qcq() ; |
| 491 |
|
| 492 |
for (int i=0 ; i<n ; i++) { |
| 493 |
result.set(0, i) = wr[n-i-1] ; |
| 494 |
result.set(1, i) = wi[n-i-1] ; |
| 495 |
} |
| 496 |
|
| 497 |
delete [] wr ; |
| 498 |
delete [] wi ; |
| 499 |
delete [] a ; |
| 500 |
delete [] work ; |
| 501 |
|
| 502 |
return result ; |
| 503 |
|
| 504 |
} |
| 505 |
|
| 506 |
// les valeurs vecteurs propres de la matrice (appel de LAPACK) : |
| 507 |
Matrice Matrice::vect_propre() const { |
| 508 |
|
| 509 |
assert (etat != ETATNONDEF) ; |
| 510 |
assert (std != 0x0) ; |
| 511 |
|
| 512 |
const char* jobvl = "V" ; |
| 513 |
const char* jobvr = "N" ; |
| 514 |
|
| 515 |
int n = get_dim(0) ; |
| 516 |
assert (n == get_dim(1)) ; |
| 517 |
|
| 518 |
double* a = new double [n*n] ; |
| 519 |
for (int i=0 ; i<n*n ; i++) |
| 520 |
a[i] = std->t[i] ; |
| 521 |
|
| 522 |
int lda = n ; |
| 523 |
double* wr = new double[n] ; |
| 524 |
double* wi = new double[n] ; |
| 525 |
|
| 526 |
int ldvl = n ; |
| 527 |
double* vl = new double[ldvl*ldvl] ; |
| 528 |
int ldvr = 1 ; |
| 529 |
double* vr = 0x0 ; |
| 530 |
|
| 531 |
int ldwork = 4*n ; |
| 532 |
double* work = new double[ldwork] ; |
| 533 |
|
| 534 |
int info ; |
| 535 |
|
| 536 |
F77_dgeev(jobvl, jobvr, &n, a, &lda, wr, wi, vl, &ldvl, vr, &ldvr, |
| 537 |
work, &ldwork, &info) ; |
| 538 |
|
| 539 |
|
| 540 |
Matrice res (n,n) ; |
| 541 |
res.set_etat_qcq() ; |
| 542 |
|
| 543 |
int conte = 0 ; |
| 544 |
for (int i=0 ; i<n ; i++) |
| 545 |
for (int j=0 ; j<n ; j++) { |
| 546 |
res.set(j,n-i-1) = vl[conte] ; |
| 547 |
conte ++ ; |
| 548 |
} |
| 549 |
|
| 550 |
delete [] wr ; |
| 551 |
delete [] wi ; |
| 552 |
delete [] a ; |
| 553 |
delete [] work ; |
| 554 |
delete [] vl ; |
| 555 |
|
| 556 |
return res ; |
| 557 |
} |
| 558 |
|
| 559 |
// Calcul le determinant : |
| 560 |
double Matrice::determinant() const { |
| 561 |
|
| 562 |
int n = get_dim(0) ; |
| 563 |
assert(n == get_dim(1)) ; |
| 564 |
|
| 565 |
Tbl valp(val_propre()) ; |
| 566 |
double result = 1 ; |
| 567 |
for (int i = 0 ; i<n ; i++) |
| 568 |
if (valp(1, i) == 0) |
| 569 |
result *= valp(0, i) ; |
| 570 |
else { |
| 571 |
result*= valp(0, i)*valp(0, i)+valp(1, i)*valp(1, i) ; |
| 572 |
i++ ; |
| 573 |
} |
| 574 |
return result ; |
| 575 |
} |
| 576 |
|
| 577 |
// Transposee |
| 578 |
Matrice Matrice::transpose() const { |
| 579 |
|
| 580 |
int nbl = std->get_dim(1) ; |
| 581 |
int nbc = std->get_dim(0) ; |
| 582 |
|
| 583 |
Matrice resu(nbc, nbl) ; |
| 584 |
|
| 585 |
if (etat == ETATZERO) { |
| 586 |
resu.set_etat_zero() ; |
| 587 |
} |
| 588 |
else{ |
| 589 |
assert(etat == ETATQCQ) ; |
| 590 |
resu.set_etat_qcq() ; |
| 591 |
for (int i=0; i<nbc; i++) { |
| 592 |
for (int j=0; j<nbl; j++) { |
| 593 |
resu.set(i,j) = (*std)(j,i) ; |
| 594 |
} |
| 595 |
} |
| 596 |
} |
| 597 |
return resu ; |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
// Operateurs d'arithmetique |
| 602 |
void Matrice::operator+=(const Matrice& a) { |
| 603 |
assert((std != 0x0)&&(a.std != 0x0)) ; |
| 604 |
std->operator+=(*a.std) ; |
| 605 |
} |
| 606 |
|
| 607 |
void Matrice::operator-=(const Matrice& a) { |
| 608 |
assert((std != 0x0)&&(a.std != 0x0)) ; |
| 609 |
std->operator-=(*a.std) ; |
| 610 |
} |
| 611 |
|
| 612 |
void Matrice::operator+=(double x) { |
| 613 |
assert(std != 0x0); |
| 614 |
std->operator+=(x) ; |
| 615 |
} |
| 616 |
|
| 617 |
void Matrice::operator-=(double x) { |
| 618 |
assert(std != 0x0); |
| 619 |
std->operator-=(x) ; |
| 620 |
} |
| 621 |
|
| 622 |
void Matrice::operator*=(double x) { |
| 623 |
assert(std != 0x0); |
| 624 |
std->operator*=(x) ; |
| 625 |
} |
| 626 |
|
| 627 |
void Matrice::operator/=(double x) { |
| 628 |
assert(std != 0x0); |
| 629 |
assert(x != 0) ; |
| 630 |
std->operator/=(x) ; |
| 631 |
} |
| 632 |
|
| 633 |
// Operateurs d'arithmetique non membres |
| 634 |
Matrice operator+ (const Matrice& a, const Matrice& b) { |
| 635 |
assert((a.std != 0x0) && (b.std != 0x0)) ; |
| 636 |
Matrice res(*a.std+*b.std) ; |
| 637 |
return res ; |
| 638 |
} |
| 639 |
|
| 640 |
Matrice operator- (const Matrice& a, const Matrice& b) { |
| 641 |
assert((a.std != 0x0) && (b.std != 0x0)) ; |
| 642 |
Matrice res(*a.std-*b.std) ; |
| 643 |
return res ; |
| 644 |
} |
| 645 |
|
| 646 |
Matrice operator* (const Matrice& a, double x) { |
| 647 |
assert(a.std != 0x0) ; |
| 648 |
Matrice res(*a.std*x); |
| 649 |
return res ; |
| 650 |
} |
| 651 |
|
| 652 |
Matrice operator* (double x, const Matrice& a) { |
| 653 |
assert(a.std != 0x0) ; |
| 654 |
Matrice res(*a.std*x); |
| 655 |
return res ; |
| 656 |
} |
| 657 |
|
| 658 |
Matrice operator* (const Matrice& aa, const Matrice& bb) { |
| 659 |
|
| 660 |
int nbla = aa.std->get_dim(1) ; |
| 661 |
int nbca = aa.std->get_dim(0) ; |
| 662 |
#ifndef NDEBUG |
| 663 |
int nblb = bb.std->get_dim(1) ; |
| 664 |
#endif |
| 665 |
int nbcb = bb.std->get_dim(0) ; |
| 666 |
|
| 667 |
assert( nbca == nblb ) ; |
| 668 |
|
| 669 |
Matrice resu(nbla, nbcb) ; |
| 670 |
|
| 671 |
if ( (aa.get_etat() == ETATZERO) || (bb.get_etat() == ETATZERO) ) { |
| 672 |
resu.set_etat_zero() ; |
| 673 |
} |
| 674 |
else { |
| 675 |
assert( aa.get_etat() == ETATQCQ ) ; |
| 676 |
assert( bb.get_etat() == ETATQCQ ) ; |
| 677 |
resu.set_etat_qcq() ; |
| 678 |
for (int i=0; i<nbla; i++) { |
| 679 |
for (int j=0; j<nbcb; j++) { |
| 680 |
double sum = 0 ; |
| 681 |
for (int k=0; k<nbca; k++) { |
| 682 |
sum += aa(i,k) * bb(k, j) ; |
| 683 |
} |
| 684 |
resu.set(i,j) = sum ; |
| 685 |
} |
| 686 |
|
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
return resu ; |
| 691 |
} |
| 692 |
|
| 693 |
Matrice operator/ (const Matrice& a, double x) { |
| 694 |
assert (x != 0) ; |
| 695 |
assert(a.std != 0x0) ; |
| 696 |
Matrice res(*a.std/x); |
| 697 |
return res ; |
| 698 |
} |
| 699 |
} |