| 1 |
/* |
| 2 |
* Copyright (c) 1999-2001 Philippe Grandclement |
| 3 |
* |
| 4 |
* This file is part of LORENE. |
| 5 |
* |
| 6 |
* LORENE is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* LORENE is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with LORENE; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
|
| 23 |
char itbl_math_C[] = "$Header: /cvsroot/Lorene/C++/Source/Itbl/itbl_math.C,v 1.3 2014/10/13 08:53:01 j_novak Exp $" ; |
| 24 |
|
| 25 |
/* |
| 26 |
* $Id: itbl_math.C,v 1.3 2014/10/13 08:53:01 j_novak Exp $ |
| 27 |
* $Log: itbl_math.C,v $ |
| 28 |
* Revision 1.3 2014/10/13 08:53:01 j_novak |
| 29 |
* Lorene classes and functions now belong to the namespace Lorene. |
| 30 |
* |
| 31 |
* Revision 1.2 2014/10/06 15:13:11 j_novak |
| 32 |
* Modified #include directives to use c++ syntax. |
| 33 |
* |
| 34 |
* Revision 1.1.1.1 2001/11/20 15:19:27 e_gourgoulhon |
| 35 |
* LORENE |
| 36 |
* |
| 37 |
* Revision 2.3 1999/11/25 13:02:12 phil |
| 38 |
* *** empty log message *** |
| 39 |
* |
| 40 |
* Revision 2.2 1999/11/25 12:42:12 phil |
| 41 |
* conversion double->int |
| 42 |
* |
| 43 |
* Revision 2.1 1999/11/24 09:32:01 eric |
| 44 |
* fabs -> abs |
| 45 |
* |
| 46 |
* Revision 2.0 1999/11/17 16:04:44 phil |
| 47 |
* *** empty log message *** |
| 48 |
* |
| 49 |
* |
| 50 |
* $Header: /cvsroot/Lorene/C++/Source/Itbl/itbl_math.C,v 1.3 2014/10/13 08:53:01 j_novak Exp $ |
| 51 |
* |
| 52 |
*/ |
| 53 |
|
| 54 |
/* |
| 55 |
* Surcharge des |
| 56 |
* Fonctions mathematiques applicables aux classes de type |
| 57 |
* |
| 58 |
* Itbl |
| 59 |
* |
| 60 |
* Typiquement: max, norme ... |
| 61 |
* |
| 62 |
*/ |
| 63 |
|
| 64 |
// Headers C |
| 65 |
// --------- |
| 66 |
#include <cmath> |
| 67 |
#include <cstdlib> |
| 68 |
|
| 69 |
// Headers Lorene |
| 70 |
// -------------- |
| 71 |
#include "itbl.h" |
| 72 |
|
| 73 |
|
| 74 |
//----------------// |
| 75 |
// Absolute value // |
| 76 |
//----------------// |
| 77 |
|
| 78 |
namespace Lorene { |
| 79 |
Itbl abs(const Itbl& ti) |
| 80 |
{ |
| 81 |
// Protection |
| 82 |
assert(ti.get_etat() != ETATNONDEF) ; |
| 83 |
|
| 84 |
// Cas ETATZERO |
| 85 |
if (ti.get_etat() == ETATZERO) { |
| 86 |
return ti ; |
| 87 |
} |
| 88 |
|
| 89 |
// Cas general |
| 90 |
assert(ti.get_etat() == ETATQCQ) ; // sinon... |
| 91 |
|
| 92 |
Itbl to(ti.dim) ; // Itbl resultat |
| 93 |
to.set_etat_qcq() ; |
| 94 |
|
| 95 |
const int* xi = ti.t ; |
| 96 |
int* xo = to.t ; |
| 97 |
int taille = ti.get_taille() ; |
| 98 |
|
| 99 |
for (int i=0 ; i<taille ; i++) { |
| 100 |
xo[i] = abs( xi[i] ) ; |
| 101 |
} |
| 102 |
|
| 103 |
return to ; |
| 104 |
} |
| 105 |
|
| 106 |
//-------------------------------// |
| 107 |
// max // |
| 108 |
//-------------------------------// |
| 109 |
|
| 110 |
int max(const Itbl& ti) { |
| 111 |
|
| 112 |
// Protection |
| 113 |
assert(ti.get_etat() != ETATNONDEF) ; |
| 114 |
|
| 115 |
// Cas particulier |
| 116 |
if (ti.get_etat() == ETATZERO) { |
| 117 |
return 0 ; |
| 118 |
} |
| 119 |
|
| 120 |
// Cas general |
| 121 |
assert(ti.get_etat() == ETATQCQ) ; // sinon.... |
| 122 |
|
| 123 |
const int* x = ti.t ; |
| 124 |
int resu = x[0] ; |
| 125 |
for (int i=1; i<ti.get_taille(); i++) { |
| 126 |
if ( x[i] > resu ) resu = x[i] ; |
| 127 |
} |
| 128 |
|
| 129 |
return resu ; |
| 130 |
} |
| 131 |
|
| 132 |
//-------------------------------// |
| 133 |
// min // |
| 134 |
//-------------------------------// |
| 135 |
|
| 136 |
int min(const Itbl& ti) { |
| 137 |
|
| 138 |
// Protection |
| 139 |
assert(ti.get_etat() != ETATNONDEF) ; |
| 140 |
|
| 141 |
// Cas particulier |
| 142 |
if (ti.get_etat() == ETATZERO) { |
| 143 |
return 0 ; |
| 144 |
} |
| 145 |
|
| 146 |
// Cas general |
| 147 |
assert(ti.get_etat() == ETATQCQ) ; // sinon.... |
| 148 |
|
| 149 |
const int* x = ti.t ; |
| 150 |
int resu = x[0] ; |
| 151 |
for (int i=1; i<ti.get_taille(); i++) { |
| 152 |
if ( x[i] < resu ) resu = x[i] ; |
| 153 |
} |
| 154 |
|
| 155 |
return resu ; |
| 156 |
} |
| 157 |
|
| 158 |
//-------------------------------// |
| 159 |
// norme // |
| 160 |
//-------------------------------// |
| 161 |
|
| 162 |
int norme(const Itbl& ti) { |
| 163 |
|
| 164 |
// Protection |
| 165 |
assert(ti.get_etat() != ETATNONDEF) ; |
| 166 |
|
| 167 |
int resu = 0 ; |
| 168 |
|
| 169 |
if (ti.get_etat() != ETATZERO) { // on n'effectue la somme que si necessaire |
| 170 |
|
| 171 |
assert(ti.get_etat() == ETATQCQ) ; // sinon.... |
| 172 |
const int* x = ti.t ; |
| 173 |
for (int i=0; i<ti.get_taille(); i++) { |
| 174 |
resu += abs( x[i] ) ; |
| 175 |
} |
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
return resu ; |
| 180 |
} |
| 181 |
|
| 182 |
//-------------------------------// |
| 183 |
// diffrel // |
| 184 |
//-------------------------------// |
| 185 |
|
| 186 |
double diffrel(const Itbl& t1, const Itbl& t2) { |
| 187 |
|
| 188 |
// Protections |
| 189 |
assert(t1.get_etat() != ETATNONDEF) ; |
| 190 |
assert(t2.get_etat() != ETATNONDEF) ; |
| 191 |
|
| 192 |
int norm2 = norme(t2) ; |
| 193 |
int normdiff = norme(t1-t2) ; |
| 194 |
double resu ; |
| 195 |
if ( norm2 == 0 ) { |
| 196 |
resu = double(normdiff) ; |
| 197 |
} |
| 198 |
else { |
| 199 |
resu = double(normdiff) / double(norm2) ; |
| 200 |
} |
| 201 |
|
| 202 |
return resu ; |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
//-------------------------------// |
| 207 |
// diffrelmax // |
| 208 |
//-------------------------------// |
| 209 |
|
| 210 |
double diffrelmax(const Itbl& t1, const Itbl& t2) { |
| 211 |
|
| 212 |
// Protections |
| 213 |
assert(t1.get_etat() != ETATNONDEF) ; |
| 214 |
assert(t2.get_etat() != ETATNONDEF) ; |
| 215 |
|
| 216 |
int max2 = max(abs(t2)) ; |
| 217 |
int maxdiff = max(abs(t1-t2)) ; |
| 218 |
double resu ; |
| 219 |
if ( max2 == 0 ) { |
| 220 |
resu = double(maxdiff) ; |
| 221 |
} |
| 222 |
else { |
| 223 |
resu = double(maxdiff) / double(max2) ; |
| 224 |
} |
| 225 |
|
| 226 |
return resu ; |
| 227 |
|
| 228 |
} |
| 229 |
} |