LORENE
itbl_math.C
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 
24 
25 /*
26  * $Id: itbl_math.C,v 1.4 2016/12/05 16:17:56 j_novak Exp $
27  * $Log: itbl_math.C,v $
28  * Revision 1.4 2016/12/05 16:17:56 j_novak
29  * Suppression of some global variables (file names, loch, ...) to prevent redefinitions
30  *
31  * Revision 1.3 2014/10/13 08:53:01 j_novak
32  * Lorene classes and functions now belong to the namespace Lorene.
33  *
34  * Revision 1.2 2014/10/06 15:13:11 j_novak
35  * Modified #include directives to use c++ syntax.
36  *
37  * Revision 1.1.1.1 2001/11/20 15:19:27 e_gourgoulhon
38  * LORENE
39  *
40  * Revision 2.3 1999/11/25 13:02:12 phil
41  * *** empty log message ***
42  *
43  * Revision 2.2 1999/11/25 12:42:12 phil
44  * conversion double->int
45  *
46  * Revision 2.1 1999/11/24 09:32:01 eric
47  * fabs -> abs
48  *
49  * Revision 2.0 1999/11/17 16:04:44 phil
50  * *** empty log message ***
51  *
52  *
53  * $Header: /cvsroot/Lorene/C++/Source/Itbl/itbl_math.C,v 1.4 2016/12/05 16:17:56 j_novak Exp $
54  *
55  */
56 
57 /*
58  * Surcharge des
59  * Fonctions mathematiques applicables aux classes de type
60  *
61  * Itbl
62  *
63  * Typiquement: max, norme ...
64  *
65  */
66 
67 // Headers C
68 // ---------
69 #include <cmath>
70 #include <cstdlib>
71 
72 // Headers Lorene
73 // --------------
74 #include "itbl.h"
75 
76 
77  //----------------//
78  // Absolute value //
79  //----------------//
80 
81 namespace Lorene {
82 Itbl abs(const Itbl& ti)
83 {
84  // Protection
85  assert(ti.get_etat() != ETATNONDEF) ;
86 
87  // Cas ETATZERO
88  if (ti.get_etat() == ETATZERO) {
89  return ti ;
90  }
91 
92  // Cas general
93  assert(ti.get_etat() == ETATQCQ) ; // sinon...
94 
95  Itbl to(ti.dim) ; // Itbl resultat
96  to.set_etat_qcq() ;
97 
98  const int* xi = ti.t ;
99  int* xo = to.t ;
100  int taille = ti.get_taille() ;
101 
102  for (int i=0 ; i<taille ; i++) {
103  xo[i] = abs( xi[i] ) ;
104  }
105 
106  return to ;
107 }
108 
109  //-------------------------------//
110  // max //
111  //-------------------------------//
112 
113 int max(const Itbl& ti) {
114 
115  // Protection
116  assert(ti.get_etat() != ETATNONDEF) ;
117 
118  // Cas particulier
119  if (ti.get_etat() == ETATZERO) {
120  return 0 ;
121  }
122 
123  // Cas general
124  assert(ti.get_etat() == ETATQCQ) ; // sinon....
125 
126  const int* x = ti.t ;
127  int resu = x[0] ;
128  for (int i=1; i<ti.get_taille(); i++) {
129  if ( x[i] > resu ) resu = x[i] ;
130  }
131 
132  return resu ;
133 }
134 
135  //-------------------------------//
136  // min //
137  //-------------------------------//
138 
139 int min(const Itbl& ti) {
140 
141  // Protection
142  assert(ti.get_etat() != ETATNONDEF) ;
143 
144  // Cas particulier
145  if (ti.get_etat() == ETATZERO) {
146  return 0 ;
147  }
148 
149  // Cas general
150  assert(ti.get_etat() == ETATQCQ) ; // sinon....
151 
152  const int* x = ti.t ;
153  int resu = x[0] ;
154  for (int i=1; i<ti.get_taille(); i++) {
155  if ( x[i] < resu ) resu = x[i] ;
156  }
157 
158  return resu ;
159 }
160 
161  //-------------------------------//
162  // norme //
163  //-------------------------------//
164 
165 int norme(const Itbl& ti) {
166 
167  // Protection
168  assert(ti.get_etat() != ETATNONDEF) ;
169 
170  int resu = 0 ;
171 
172  if (ti.get_etat() != ETATZERO) { // on n'effectue la somme que si necessaire
173 
174  assert(ti.get_etat() == ETATQCQ) ; // sinon....
175  const int* x = ti.t ;
176  for (int i=0; i<ti.get_taille(); i++) {
177  resu += abs( x[i] ) ;
178  }
179 
180  }
181 
182  return resu ;
183 }
184 
185  //-------------------------------//
186  // diffrel //
187  //-------------------------------//
188 
189 double diffrel(const Itbl& t1, const Itbl& t2) {
190 
191  // Protections
192  assert(t1.get_etat() != ETATNONDEF) ;
193  assert(t2.get_etat() != ETATNONDEF) ;
194 
195  int norm2 = norme(t2) ;
196  int normdiff = norme(t1-t2) ;
197  double resu ;
198  if ( norm2 == 0 ) {
199  resu = double(normdiff) ;
200  }
201  else {
202  resu = double(normdiff) / double(norm2) ;
203  }
204 
205  return resu ;
206 
207 }
208 
209  //-------------------------------//
210  // diffrelmax //
211  //-------------------------------//
212 
213 double diffrelmax(const Itbl& t1, const Itbl& t2) {
214 
215  // Protections
216  assert(t1.get_etat() != ETATNONDEF) ;
217  assert(t2.get_etat() != ETATNONDEF) ;
218 
219  int max2 = max(abs(t2)) ;
220  int maxdiff = max(abs(t1-t2)) ;
221  double resu ;
222  if ( max2 == 0 ) {
223  resu = double(maxdiff) ;
224  }
225  else {
226  resu = double(maxdiff) / double(max2) ;
227  }
228 
229  return resu ;
230 
231 }
232 }
int get_taille() const
Gives the total size (ie dim.taille )
Definition: itbl.h:320
Lorene prototypes.
Definition: app_hor.h:67
Basic integer array class.
Definition: itbl.h:122
Tbl min(const Cmp &)
Minimum values of a Cmp in each domain.
Definition: cmp_math.C:461
Tbl diffrel(const Cmp &a, const Cmp &b)
Relative difference between two Cmp (norme version).
Definition: cmp_math.C:507
Dim_tbl dim
Number of dimensions, size,...
Definition: itbl.h:131
Tbl norme(const Cmp &)
Sums of the absolute values of all the values of the Cmp in each domain.
Definition: cmp_math.C:484
Tbl max(const Cmp &)
Maximum values of a Cmp in each domain.
Definition: cmp_math.C:438
int get_etat() const
Gives the logical state.
Definition: itbl.h:317
Cmp abs(const Cmp &)
Absolute value.
Definition: cmp_math.C:413
int * t
The array of int &#39;s.
Definition: itbl.h:132
Tbl diffrelmax(const Cmp &a, const Cmp &b)
Relative difference between two Cmp (max version).
Definition: cmp_math.C:542