Debian Codegraph @

C++/Include/tbl.h

raw symbols SCIP
1 /*
2 * Definition of Lorene class Tbl
3 *
4 */
5
6 /*
7 * Copyright (c) 1999-2000 Jean-Alain Marck
8 * Copyright (c) 1999-2001 Eric Gourgoulhon
9 *
10 * This file is part of LORENE.
11 *
12 * LORENE is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * LORENE is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with LORENE; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 */
27
28
29 #ifndef __TBL_H_
30 #define __TBL_H_
31
32 /*
33 * Classe de tableaux de dimension determinee (actuellement 1, 2 et 3)
34 *
35 */
36
37 /*
38 * $Id: tbl.h,v 1.13 2014/10/13 08:52:37 j_novak Exp $
39 * $Log: tbl.h,v $
40 * Revision 1.13 2014/10/13 08:52:37 j_novak
41 * Lorene classes and functions now belong to the namespace Lorene.
42 *
43 * Revision 1.12 2014/10/06 15:09:40 j_novak
44 * Modified #include directives to use c++ syntax.
45 *
46 * Revision 1.11 2012/01/17 10:20:57 j_penner
47 * function added: Heaviside
48 *
49 * Revision 1.10 2006/09/25 10:01:45 p_grandclement
50 * Addition of N-dimensional Tbl
51 *
52 * Revision 1.9 2004/12/02 09:33:04 p_grandclement
53 * *** empty log message ***
54 *
55 * Revision 1.8 2004/03/22 13:12:43 j_novak
56 * Modification of comments to use doxygen instead of doc++
57 *
58 * Revision 1.7 2003/11/06 14:43:37 e_gourgoulhon
59 * Gave a name to const arguments in certain method prototypes (e.g.
60 * constructors) to correct a bug of DOC++.
61 *
62 * Revision 1.6 2002/10/16 14:36:29 j_novak
63 * Reorganization of #include instructions of standard C++, in order to
64 * use experimental version 3 of gcc.
65 *
66 * Revision 1.5 2002/09/24 10:49:42 e_gourgoulhon
67 *
68 * Modif commentaires.
69 *
70 * Revision 1.4 2002/09/24 08:33:35 e_gourgoulhon
71 *
72 * Added constructor from Matrice
73 *
74 * Revision 1.3 2002/06/17 14:05:17 j_novak
75 * friend functions are now also declared outside the class definition
76 *
77 * Revision 1.2 2002/01/03 13:18:40 j_novak
78 * Optimization: the members set(i,j) and operator(i,j) of class Matrice are
79 * now defined inline. Matrice is a friend class of Tbl.
80 *
81 * Revision 1.1.1.1 2001/11/20 15:19:27 e_gourgoulhon
82 * LORENE
83 *
84 * Revision 2.11 1999/12/02 17:52:59 phil
85 * *** empty log message ***
86 *
87 * Revision 2.10 1999/11/23 13:31:27 eric
88 * Le constructeur Tbl::Tbl(const Dim_tbl ) devient Tbl::Tbl(const Dim_tbl& ).
89 * Le constructeur Tbl::Tbl(const Grille3d* ) devient
90 * Tbl(const Grille3d& ).
91 *
92 * Revision 2.9 1999/11/15 16:36:08 eric
93 * Le membre dim est desormais un Dim_tbl et non plus un pointeur sur un
94 * Dim_tbl.
95 *
96 * Revision 2.8 1999/10/29 15:04:07 eric
97 * Suppression des fonctions membres min() et max():
98 * elles deviennent des fonctions externes.
99 * Ajout de fonctions mathematiques (abs, norme, etc...).
100 *
101 * Revision 2.7 1999/10/18 15:05:48 eric
102 * La fonction membre annule() est rebaptisee annule_hard().
103 *
104 * Revision 2.6 1999/10/01 10:18:18 eric
105 * Amelioration des commentaires.
106 *
107 * Revision 2.5 1999/09/30 12:50:59 eric
108 * Constructeur a 1 parametre rendu explicit.
109 * Amelioration des commentaires.
110 *
111 * Revision 2.4 1999/09/24 14:23:01 eric
112 * Depoussierage.
113 * Documentation.
114 *
115 * Revision 2.3 1999/03/02 18:54:36 eric
116 * Ajout de la fonction affiche_seuil.
117 *
118 * Revision 2.2 1999/02/23 10:40:55 hyc
119 * *** empty log message ***
120 *
121 *
122 * Revision 2.0 1998/12/02 11:11:16 hyc
123 * Version 2
124 *
125 * $Header: /cvsroot/Lorene/C++/Include/tbl.h,v 1.13 2014/10/13 08:52:37 j_novak Exp $
126 *
127 */
128
129
130 // Fichiers includes
131 #include <cassert>
132 #include <cstdlib>
133
134 #include "type_parite.h"
135 #include "dim_tbl.h"
136 #include "itbl.h"
137
138 namespace Lorene {
139 class Grille3d ;
140 class Matrice ;
141
142
143 /**
144 * Basic array class.
145 *
146 * This class is essentially an \c double array class. The elements of the
147 * array are stored continuously using the C convention.
148 * A \c Tbl is initialy created with a \a logical state
149 * \c ETATNONDEF (i.e. undefined), except by the copy constructor and
150 * the constructor from a file.
151 * The general logical state of an initialized
152 * \c Tbl is \c ETATQCQ; it is the only state for which the memory
153 * allocation is performed for the \c double} array \c t.
154 * The value zero is treated as a special logical state (\c ETATZERO),
155 * without any memory allocation.
156 * Arithmetic operations are provided with the usual meaning (see
157 * below).
158 * \ingroup (util)
159 *
160 */
161 class Tbl {
162
163 friend class Matrice ;
164
165 // Data :
166 // -----
167 private:
168 /// logical state (\c ETATNONDEF, \c ETATQCQ or \c ETATZERO).
169 int etat ;
170
171 public:
172 Dim_tbl dim ; ///< Number of dimensions, size,...
173 double* t ; ///< The array of \c double
174
175 // Constructors - Destructor
176 // -------------------------
177
178 public:
179 /**
180 * 1D constructor
181 * @param size0 [input] Number of elements of the array \c t.
182 * Will be assigned to \c dim.dim[0].
183 */
184 explicit Tbl(int size0) ;
185
186 /**
187 * 2D constructor
188 * @param size1 [input] Defines the range [0, size1-1] of the
189 * outermost index in the storage of the array \c t.
190 * Will be assigned to \c dim.dim[1].
191 * @param size0 [input] Defines the range [0, size0-1] of the
192 * innermost index in the storage of the array \c t.
193 * Will be assigned to \c dim.dim[0].
194 */
195 Tbl(int size1, int size0) ;
196
197 /**
198 * 3D constructor
199 * @param size2 [input] Defines the range [0, size2-1] of the
200 * outermost index in the storage of the array \c t.
201 * Will be assigned to \c dim.dim[2].
202 * @param size1 [input] Defines the range [0, size1-1] of the
203 * intermediate index in the storage of the array \c t.
204 * Will be assigned to \c dim.dim[1].
205 * @param size0 [input] Defines the range [0, size0-1] of the
206 * innermost index in the storage of the array \c t.
207 * Will be assigned to \c dim.dim[0].
208 */
209 Tbl(int size2, int size1, int size0) ;
210 /**
211 * N-dimensional constructor
212 * @param sizes [input] the sizes of each dimensions
213 */
214 Tbl(Itbl sizes) ;
215
216 explicit Tbl(const Grille3d& grid) ; ///< Constructor from a 3D grid
217 explicit Tbl(const Dim_tbl& dim) ; ///< Constructor from a \c Dim_tbl
218 /// Constructor from a file (see \c sauve(FILE* ))
219 explicit Tbl(FILE* ) ;
220 Tbl(const Tbl& a) ; ///< Copy constructor
221
222 /** Constructor from a matrix.
223 * If the matrix has only one row or one column, the \c Tbl
224 * is 1D, otherwise it is 2D.
225 */
226 explicit Tbl(const Matrice& mat) ;
227
228
229 ~Tbl() ; ///< Destructor
230
231 // Assignement
232 // -----------
233 void operator=(const Tbl& ) ; ///< Assignment to another \c Tbl
234 void operator=(double ) ; ///< Assignment to a \c double
235 void operator=(int ) ; ///< Assignment to a \c int
236
237 // Memory management
238 // -----------------
239 private:
240 /** Logical destructor: dellocates the memory occupied by the array
241 * \c t and sets the logical state to ETATNONDEF.
242 */
243 void del_t() ;
244
245 public:
246
247 /**
248 * Sets the logical state to \c ETATNONDEF (undefined).
249 * Deallocates the memory occupied by the \c double array \c t.
250 */
251 void set_etat_nondef() ;
252
253 /**
254 * Sets the logical state to \c ETATZERO (zero).
255 * Deallocates the memory occupied by the \c double array \c t.
256 */
257 void set_etat_zero() ;
258
259 /**
260 * Sets the logical state to \c ETATQCQ (ordinary state).
261 * If the state (member \c etat) is already \c ETATQCQ, this
262 * function does nothing. Otherwise, it performs the memory allocation
263 * for the \c double array \c t.
264 */
265 void set_etat_qcq() ;
266
267 /**
268 * Sets the \c Tbl to zero in a hard way.
269 * 1/ Sets the logical state to \c ETATQCQ, i.e. to an ordinary state.
270 * 2/ Allocates the memory of the \c double array \c t, and fills it
271 * with zeros. NB: this function must be used for debugging purposes only.
272 * For other operations, the function \c set_etat_zero() must
273 * be perferred.
274 */
275 void annule_hard() ;
276
277 // Access to individual elements
278 // -----------------------------
279 public:
280 /// Read/write of a particular element (index \c i) (1D case)
281 double& set(int i) {
282 assert (etat == ETATQCQ) ;
283 assert( dim.ndim == 1 ) ;
284 assert( i >= 0 ) ;
285 assert( i < dim.dim[0] ) ;
286 return t[i] ;
287 } ;
288
289 /// Affectation of a particular element (index \c i) (1D case)
290 void affecte(int i, double val) {
291 assert (etat == ETATQCQ) ;
292 assert( dim.ndim == 1 ) ;
293 assert( i >= 0 ) ;
294 assert( i < dim.dim[0] ) ;
295 t[i] = val ;
296 } ;
297 /// Read-only of a particular element (index \c i) (1D case)
298 double operator()(int i) const {
299 assert(etat != ETATNONDEF) ;
300 assert( dim.ndim == 1 ) ;
301 assert( i >= 0 ) ;
302 assert( i < dim.dim[0] ) ;
303 if (etat == ETATZERO) {
304 double zero = 0. ;
305 return zero ;
306 }
307 else return t[i] ;
308 };
309
310 /// Read/write of a particular element (index \c (j,i)) (2D case)
311 double& set(int j, int i) {
312 assert (etat == ETATQCQ) ;
313 assert( dim.ndim == 2 ) ;
314 assert( (i>=0) && (i<dim.dim[0]) ) ;
315 assert( (j>=0) && (j<dim.dim[1]) ) ;
316 return t[dim.dim[0] * j + i] ;
317 };
318
319 /// Read-only of a particular element (index \c (j,i)) (2D case)
320 double operator()(int j, int i) const {
321 assert(etat != ETATNONDEF) ;
322 assert( dim.ndim == 2 ) ;
323 assert( (i>=0) && (i<dim.dim[0]) ) ;
324 assert( (j>=0) && (j<dim.dim[1]) ) ;
325 if (etat == ETATZERO) {
326 double zero = 0. ;
327 return zero ;
328 }
329 else return t[dim.dim[0] * j + i] ;
330 };
331
332 /// Read/write of a particular element (index \c (k,j,i)) (3D case)
333 double& set(int k, int j, int i) {
334 assert (etat == ETATQCQ) ;
335 assert( dim.ndim == 3 ) ;
336 assert( (i>=0) && (i<dim.dim[0]) ) ;
337 assert( (j>=0) && (j<dim.dim[1]) ) ;
338 assert( (k>=0) && (k<dim.dim[2]) ) ;
339 return t[dim.dim[1]*dim.dim[0]*k + dim.dim[0]*j + i] ;
340 };
341
342 /// Read-only of a particular element (index \c (k,j,i)) (3D case)
343 double operator()(int k, int j, int i) const {
344 assert(etat != ETATNONDEF) ;
345 assert( dim.ndim == 3 ) ;
346 assert( (i>=0) && (i<dim.dim[0]) ) ;
347 assert( (j>=0) && (j<dim.dim[1]) ) ;
348 assert( (k>=0) && (k<dim.dim[2]) ) ;
349 if (etat == ETATZERO) {
350 double zero = 0. ;
351 return zero ;
352 }
353 else return t[dim.dim[1]*dim.dim[0]*k + dim.dim[0]*j + i] ;
354 };
355
356 /// Read/write of a particular element (multi-dimensional case)
357 double& set(const Itbl place) {
358 int n = place.get_dim(0) ;
359 assert (n == dim.ndim) ;
360 for (int i=0 ; i<n ; i++)
361 assert( (place(i)>=0) && (place(i)<dim.dim[0]) ) ;
362 int pos = 0 ;
363 for (int d=n-1 ; d>=0 ; d--) {
364 pos*=dim.dim[d] ;
365 pos += place(d) ;
366 }
367 return t[pos] ;
368 };
369
370 /// Read-only of a particular element (index \c (k,j,i)) (multi-dimensional case)
371 double operator()(const Itbl place) const {
372 assert(etat != ETATNONDEF) ;
373 int n = place.get_dim(0) ;
374 assert (n == dim.ndim) ;
375 for (int i=0 ; i<n ; i++)
376 assert( (place(i)>=0) && (place(i)<dim.dim[0]) ) ;
377 if (etat == ETATZERO) {
378 double zero = 0. ;
379 return zero ;
380 }
381 else {
382 int pos = 0 ;
383 for (int d=n-1 ; d>=0 ; d--) {
384 pos*=dim.dim[d] ;
385 pos += place(d) ;
386 }
387 return t[pos] ;
388 }
389 };
390
391 // Extraction of information
392 // -------------------------
393 /// Gives the logical state
394 int get_etat() const { return etat ; };
395
396 /// Gives the total size (ie \c dim.taille)
397 int get_taille() const { return dim.taille ; };
398
399 /// Gives the number of dimensions (ie \c dim.ndim)
400 int get_ndim() const { return dim.ndim ; };
401
402 /// Gives the i-th dimension (ie \c dim.dim[i])
403 int get_dim(int i) const {
404 assert( (i>=0) && (i<dim.ndim) ) ;
405 return dim.dim[i] ;
406 };
407
408 // Outputs
409 // -------
410 public:
411 void sauve(FILE* ) const ; ///< Save in a file
412
413 /** Prints only the values greater than a given threshold.
414 * @param ostr [input] Output stream used for the printing
415 * @param precision [input] Number of printed digits (default: 4)
416 * @param threshold [input] Value above which an array element is printed
417 * (default: 1.e-7)
418 */
419 void affiche_seuil(ostream& ostr, int precision = 4,
420 double threshold = 1.e-7) const ;
421 /// Display
422 friend ostream& operator<<(ostream& , const Tbl& ) ;
423
424 // Member arithmetics
425 // ------------------
426 public:
427
428 void operator+=(const Tbl &) ; ///< Addition of a \c Tbl to \c this
429 void operator+=(double) ; ///< Addition of a \c double to \c this
430 void operator-=(const Tbl &) ; ///< Subtraction of a \c Tbl to \c this
431 void operator-=(double) ; ///< Subtraction of a \c double to \c this
432 void operator*=(const Tbl &) ; ///< Multiplication of \c this by a \c Tbl
433 void operator*=(double) ; ///< Multiplication of \c this by a \c double
434 void operator/=(const Tbl &) ; ///< Division of \c this by a \c Tbl
435 void operator/=(double) ; ///< Division of \c this by a \c double
436
437 } ;
438 ostream& operator<<(ostream& , const Tbl& ) ;
439
440
441 /**
442 * \defgroup tbl_m Tbl Mathematics
443 * \ingroup (util)
444 * @{
445 */
446 Tbl operator+(const Tbl&) ; ///< + Tbl
447 Tbl operator-(const Tbl&) ; ///< \c - Tbl
448 Tbl operator+(const Tbl&, const Tbl&) ; ///< Tbl + Tbl
449 Tbl operator+(const Tbl&, double) ; ///< Tbl + double
450 Tbl operator+(double, const Tbl&) ; ///< double + Tbl
451 Tbl operator+(const Tbl&, int) ; ///< Tbl + int
452 Tbl operator+(int, const Tbl&) ; ///< int + Tbl
453 Tbl operator-(const Tbl&, const Tbl&) ; ///< Tbl - Tbl
454 Tbl operator-(const Tbl&, double) ; ///< Tbl - double
455 Tbl operator-(double, const Tbl&) ; ///< double - Tbl
456 Tbl operator-(const Tbl&, int) ; ///< Tbl - int
457 Tbl operator-(int, const Tbl&) ; ///< int - Tbl
458 Tbl operator*(const Tbl&, const Tbl&) ; ///< Tbl * Tbl
459 Tbl operator*(const Tbl&, double) ; ///< Tbl * double
460 Tbl operator*(double, const Tbl&) ; ///< double * Tbl
461 Tbl operator*(const Tbl&, int) ; ///< Tbl * int
462 Tbl operator*(int, const Tbl&) ; ///< int * Tbl
463 Tbl operator/(const Tbl&, const Tbl&) ; ///< Tbl / Tbl
464 Tbl operator/(const Tbl&, double) ; ///< Tbl / double
465 Tbl operator/(double, const Tbl&) ; ///< double / Tbl
466 Tbl operator/(const Tbl&, int) ; ///< Tbl / int
467 Tbl operator/(int, const Tbl&) ; ///< int / Tbl
468
469 Tbl sin(const Tbl& ) ; ///< Sine
470 Tbl cos(const Tbl& ) ; ///< Cosine
471 Tbl tan(const Tbl& ) ; ///< Tangent
472 Tbl asin(const Tbl& ) ; ///< Arcsine
473 Tbl acos(const Tbl& ) ; ///< Arccosine
474 Tbl atan(const Tbl& ) ; ///< Arctangent
475 Tbl exp(const Tbl& ) ; ///< Exponential
476 Tbl Heaviside(const Tbl& ) ; ///< Heaviside function
477 Tbl log(const Tbl& ) ; ///< Neperian logarithm
478 Tbl log10(const Tbl& ) ; ///< Basis 10 logarithm
479 Tbl sqrt(const Tbl& ) ; ///< Square root
480 Tbl racine_cubique (const Tbl&) ; ///< cube root
481 Tbl pow(const Tbl& , int ) ; ///< Power \f${\tt Tbl}^{\tt int}\f$
482 Tbl pow(const Tbl& , double ) ; ///< Power \f${\tt Tbl}^{\tt double}\f$
483 Tbl abs(const Tbl& ) ; ///< Absolute value
484 double max(const Tbl& ) ; ///< Maximum value of the \c Tbl elements
485 double min(const Tbl& ) ; ///< Minimum value of the \c Tbl elements
486
487 /// Sum of the absolute values of all the \c Tbl elements
488 double norme(const Tbl& ) ;
489
490 /**
491 * Relative difference between two \c Tbl (norme version).
492 * Returns \c norme(a-b)/norme(b) unless \c b=0, in which
493 * case it returns \c norme(a-b).
494 */
495 double diffrel(const Tbl& a, const Tbl& b) ;
496
497 /**
498 * Relative difference between two \c Tbl (max version).
499 * Returns \c max(abs(a-b))/max(abs(b)) unless \c b=0, in which
500 * case it returns \c max(abs(a-b)).
501 */
502 double diffrelmax(const Tbl& a, const Tbl& b) ;
503
504 /** @}*/
505
506
507 }
508 #endif