| 1 |
/* |
| 2 |
* Arithmetical operations for class Mtbl_cf |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
/* |
| 7 |
* Copyright (c) 1999-2000 Jean-Alain Marck |
| 8 |
* Copyright (c) 1999-2001 Eric Gourgoulhon |
| 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 mtbl_cf_arithm_C[] = "$Header: /cvsroot/Lorene/C++/Source/Mtbl_cf/mtbl_cf_arithm.C,v 1.4 2014/10/13 08:53:08 j_novak Exp $" ; |
| 31 |
|
| 32 |
/* |
| 33 |
* $Id: mtbl_cf_arithm.C,v 1.4 2014/10/13 08:53:08 j_novak Exp $ |
| 34 |
* $Log: mtbl_cf_arithm.C,v $ |
| 35 |
* Revision 1.4 2014/10/13 08:53:08 j_novak |
| 36 |
* Lorene classes and functions now belong to the namespace Lorene. |
| 37 |
* |
| 38 |
* Revision 1.3 2014/10/06 15:13:15 j_novak |
| 39 |
* Modified #include directives to use c++ syntax. |
| 40 |
* |
| 41 |
* Revision 1.2 2002/10/16 14:36:43 j_novak |
| 42 |
* Reorganization of #include instructions of standard C++, in order to |
| 43 |
* use experimental version 3 of gcc. |
| 44 |
* |
| 45 |
* Revision 1.1.1.1 2001/11/20 15:19:27 e_gourgoulhon |
| 46 |
* LORENE |
| 47 |
* |
| 48 |
* Revision 2.6 2000/09/27 14:25:15 eric |
| 49 |
* Multiplication par un double : on met le resultat a ETATZERO si x == 0. |
| 50 |
* |
| 51 |
* Revision 2.5 2000/08/16 10:43:18 eric |
| 52 |
* Suppression du membre dzpuis. |
| 53 |
* |
| 54 |
* Revision 2.4 1999/10/26 08:09:03 eric |
| 55 |
* Ajout de protection dzpuis dans +=, -= |
| 56 |
* |
| 57 |
* Revision 2.3 1999/10/18 15:07:34 eric |
| 58 |
* La fonction membre annule() est rebaptisee annule_hard(). |
| 59 |
* |
| 60 |
* Revision 2.2 1999/10/13 15:50:49 eric |
| 61 |
* *** empty log message *** |
| 62 |
* |
| 63 |
* Revision 2.1 1999/10/01 14:50:14 eric |
| 64 |
* Ajout des operations manquantes. |
| 65 |
* |
| 66 |
* Revision 2.0 1999/06/23 12:36:43 phil |
| 67 |
* *** empty log message *** |
| 68 |
* |
| 69 |
* |
| 70 |
* $Header: /cvsroot/Lorene/C++/Source/Mtbl_cf/mtbl_cf_arithm.C,v 1.4 2014/10/13 08:53:08 j_novak Exp $ |
| 71 |
* |
| 72 |
*/ |
| 73 |
|
| 74 |
|
| 75 |
// Fichiers include |
| 76 |
// ---------------- |
| 77 |
#include <cmath> |
| 78 |
#include <cassert> |
| 79 |
#include <cstdlib> |
| 80 |
|
| 81 |
#include "mtbl_cf.h" |
| 82 |
|
| 83 |
//********************// |
| 84 |
// OPERATEURS UNAIRES // |
| 85 |
//********************// |
| 86 |
|
| 87 |
// + Mtbl_cf |
| 88 |
// --------- |
| 89 |
namespace Lorene { |
| 90 |
Mtbl_cf operator+(const Mtbl_cf& t1) |
| 91 |
{ |
| 92 |
// Protection |
| 93 |
assert(t1.get_etat() != ETATNONDEF) ; |
| 94 |
|
| 95 |
return t1 ; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
// - Mtbl_cf |
| 100 |
// --------- |
| 101 |
Mtbl_cf operator-(const Mtbl_cf& t1) |
| 102 |
{ |
| 103 |
|
| 104 |
// Protection |
| 105 |
assert(t1.get_etat() != ETATNONDEF) ; |
| 106 |
|
| 107 |
// Cas particulier |
| 108 |
if (t1.get_etat() == ETATZERO) { |
| 109 |
return t1 ; |
| 110 |
} |
| 111 |
|
| 112 |
// Cas general |
| 113 |
assert(t1.get_etat() == ETATQCQ) ; // sinon... |
| 114 |
Mtbl_cf r(t1) ; // Mtbl_cf resultat |
| 115 |
|
| 116 |
for (int i=0 ; i<r.get_nzone() ; i++) { |
| 117 |
*(r.t)[i] = -(*(t1.t)[i]) ; |
| 118 |
} |
| 119 |
return r ; |
| 120 |
} |
| 121 |
|
| 122 |
//**********// |
| 123 |
// ADDITION // |
| 124 |
//**********// |
| 125 |
|
| 126 |
// Mtbl_cf + Mtbl_cf |
| 127 |
// ----------------- |
| 128 |
Mtbl_cf operator+(const Mtbl_cf& t1, const Mtbl_cf& t2) |
| 129 |
{ |
| 130 |
// Protection |
| 131 |
assert(t1.get_etat() != ETATNONDEF) ; |
| 132 |
assert(t2.get_etat() != ETATNONDEF) ; |
| 133 |
assert(t1.get_mg() == t2.get_mg()) ; |
| 134 |
assert(t1.base == t2.base) ; |
| 135 |
|
| 136 |
// Cas particulier |
| 137 |
if (t1.get_etat() == ETATZERO) { |
| 138 |
return t2 ; |
| 139 |
} |
| 140 |
if (t2.get_etat() == ETATZERO) { |
| 141 |
return t1 ; |
| 142 |
} |
| 143 |
assert(t1.get_etat() == ETATQCQ) ; // sinon... |
| 144 |
assert(t2.get_etat() == ETATQCQ) ; // sinon... |
| 145 |
|
| 146 |
// Cas general |
| 147 |
int nz = t1.get_nzone() ; |
| 148 |
|
| 149 |
Mtbl_cf r(t1) ; // Mtbl resultat |
| 150 |
|
| 151 |
for (int i=0 ; i<nz ; i++) { |
| 152 |
*(r.t)[i] += *(t2.t)[i] ; |
| 153 |
} |
| 154 |
|
| 155 |
return r ; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
//**************// |
| 161 |
// SOUSTRACTION // |
| 162 |
//**************// |
| 163 |
|
| 164 |
// Mtbl_cf - Mtbl_cf |
| 165 |
// ----------------- |
| 166 |
Mtbl_cf operator-(const Mtbl_cf& t1, const Mtbl_cf& t2) |
| 167 |
{ |
| 168 |
// Protection |
| 169 |
assert(t1.get_etat() != ETATNONDEF) ; |
| 170 |
assert(t2.get_etat() != ETATNONDEF) ; |
| 171 |
assert(t1.get_mg() == t2.get_mg()) ; |
| 172 |
assert(t1.base == t2.base) ; |
| 173 |
|
| 174 |
// Cas particulier |
| 175 |
if (t1.get_etat() == ETATZERO) { |
| 176 |
return - t2 ; |
| 177 |
} |
| 178 |
if (t2.get_etat() == ETATZERO) { |
| 179 |
return t1 ; |
| 180 |
} |
| 181 |
assert(t1.get_etat() == ETATQCQ) ; // sinon... |
| 182 |
assert(t2.get_etat() == ETATQCQ) ; // sinon... |
| 183 |
|
| 184 |
// Cas general |
| 185 |
int nz = t1.get_nzone() ; |
| 186 |
|
| 187 |
Mtbl_cf r(t1) ; // Mtbl_cf resultat |
| 188 |
|
| 189 |
for (int i=0 ; i<nz ; i++) { |
| 190 |
*(r.t)[i] -= *(t2.t)[i] ; |
| 191 |
} |
| 192 |
|
| 193 |
return r ; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
//****************// |
| 198 |
// MULTIPLICATION // |
| 199 |
//****************// |
| 200 |
|
| 201 |
|
| 202 |
// Mtbl_cf * double |
| 203 |
// ---------------- |
| 204 |
Mtbl_cf operator*(const Mtbl_cf& t1, double x) // Mtbl_cf * double |
| 205 |
{ |
| 206 |
|
| 207 |
// Protection |
| 208 |
assert(t1.get_etat() != ETATNONDEF) ; |
| 209 |
|
| 210 |
// Cas particulier |
| 211 |
if ((t1.get_etat() == ETATZERO) || ( x == double(1) )) { |
| 212 |
return t1 ; |
| 213 |
} |
| 214 |
|
| 215 |
// Cas general |
| 216 |
assert(t1.get_etat() == ETATQCQ) ; // sinon... |
| 217 |
|
| 218 |
Mtbl_cf r(t1) ; // Mtbl_cf resultat |
| 219 |
|
| 220 |
if ( x == double(0) ) { |
| 221 |
r.set_etat_zero() ; |
| 222 |
} |
| 223 |
else{ |
| 224 |
int nz = t1.get_nzone() ; |
| 225 |
for (int i=0 ; i<nz ; i++) { |
| 226 |
*(r.t)[i] *= x ; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
return r ; |
| 231 |
} |
| 232 |
|
| 233 |
// double * Mtbl_cf |
| 234 |
// ---------------- |
| 235 |
Mtbl_cf operator*(double x, const Mtbl_cf& t1) |
| 236 |
{ |
| 237 |
return t1 * x ; |
| 238 |
} |
| 239 |
|
| 240 |
// Mtbl_cf * int |
| 241 |
// ------------- |
| 242 |
Mtbl_cf operator*(const Mtbl_cf& t1, int m) |
| 243 |
{ |
| 244 |
return t1 * double(m) ; |
| 245 |
} |
| 246 |
|
| 247 |
// int * Mtbl_cf |
| 248 |
// ------------- |
| 249 |
Mtbl_cf operator*(int m, const Mtbl_cf& t1) |
| 250 |
{ |
| 251 |
return t1 * double(m) ; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
//**********// |
| 256 |
// DIVISION // |
| 257 |
//**********// |
| 258 |
|
| 259 |
|
| 260 |
// Mtbl_cf / double |
| 261 |
// ---------------- |
| 262 |
Mtbl_cf operator/(const Mtbl_cf& t1, double x) |
| 263 |
{ |
| 264 |
|
| 265 |
// Protection |
| 266 |
assert(t1.get_etat() != ETATNONDEF) ; |
| 267 |
if ( x == double(0) ) { |
| 268 |
cout << "Mtbl_cf division by 0 !" << endl ; |
| 269 |
abort() ; |
| 270 |
} |
| 271 |
|
| 272 |
// Cas particulier |
| 273 |
if ((t1.get_etat() == ETATZERO) || ( x == double(1) )) { |
| 274 |
return t1 ; |
| 275 |
} |
| 276 |
|
| 277 |
// Cas general |
| 278 |
assert(t1.get_etat() == ETATQCQ) ; // sinon... |
| 279 |
|
| 280 |
Mtbl_cf r(t1) ; // Mtbl_cf resultat |
| 281 |
int nz = t1.get_nzone() ; |
| 282 |
for (int i=0 ; i<nz ; i++) { |
| 283 |
*(r.t)[i] /= x ; |
| 284 |
} |
| 285 |
|
| 286 |
return r ; |
| 287 |
} |
| 288 |
|
| 289 |
// Mtbl_cf / int |
| 290 |
// ------------- |
| 291 |
Mtbl_cf operator/(const Mtbl_cf& t1, int n) |
| 292 |
{ |
| 293 |
return t1/double(n) ; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
//*******************// |
| 299 |
// operateurs +=,... // |
| 300 |
//*******************// |
| 301 |
|
| 302 |
void Mtbl_cf::operator+=(const Mtbl_cf & mi) { |
| 303 |
|
| 304 |
// Protection |
| 305 |
assert(mg == mi.get_mg()) ; // meme grille |
| 306 |
assert(base == mi.base) ; // meme base |
| 307 |
assert(etat != ETATNONDEF) ; // etat defini |
| 308 |
assert(mi.get_etat() != ETATNONDEF) ; // etat defini |
| 309 |
|
| 310 |
// Cas particulier |
| 311 |
if (mi.get_etat() == ETATZERO) { |
| 312 |
return ; |
| 313 |
} |
| 314 |
|
| 315 |
// Cas general |
| 316 |
|
| 317 |
if (etat == ETATZERO) { |
| 318 |
annule_hard() ; |
| 319 |
} |
| 320 |
for (int i=0 ; i<nzone ; i++) { |
| 321 |
*(t[i]) += *(mi.t[i]) ; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
void Mtbl_cf::operator-=(const Mtbl_cf & mi) { |
| 326 |
|
| 327 |
// Protection |
| 328 |
assert(mg == mi.get_mg()) ; // meme grille |
| 329 |
assert(base == mi.base) ; // meme base |
| 330 |
assert(etat != ETATNONDEF) ; // etat defini |
| 331 |
assert(mi.get_etat() != ETATNONDEF) ; // etat defini |
| 332 |
|
| 333 |
// Cas particulier |
| 334 |
if (mi.get_etat() == ETATZERO) { |
| 335 |
return ; |
| 336 |
} |
| 337 |
|
| 338 |
// Cas general |
| 339 |
|
| 340 |
if (etat == ETATZERO) { |
| 341 |
annule_hard() ; |
| 342 |
} |
| 343 |
for (int i=0 ; i<nzone ; i++) { |
| 344 |
*(t[i]) -= *(mi.t[i]) ; |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
} |