LORENE
tensor_sym_arithm.C
1 /*
2  * Arithmetics Tensor_sym
3  *
4  * (see file tensor.h for documentation)
5  *
6  */
7 
8 /*
9  * Copyright (c) 2004 Eric Gourgoulhon & Jerome Novak
10  *
11  * Copyright (c) 1999-2001 Philippe Grandclement (for preceding class Tenseur)
12  *
13  * This file is part of LORENE.
14  *
15  * LORENE is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * LORENE is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with LORENE; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28  *
29  */
30 
31 
32 char tensor_sym_arithm_C[] = "$Header: /cvsroot/Lorene/C++/Source/Tensor/tensor_sym_arithm.C,v 1.3 2014/10/13 08:53:44 j_novak Exp $" ;
33 
34 /*
35  * $Id: tensor_sym_arithm.C,v 1.3 2014/10/13 08:53:44 j_novak Exp $
36  * $Log: tensor_sym_arithm.C,v $
37  * Revision 1.3 2014/10/13 08:53:44 j_novak
38  * Lorene classes and functions now belong to the namespace Lorene.
39  *
40  * Revision 1.2 2014/10/06 15:13:20 j_novak
41  * Modified #include directives to use c++ syntax.
42  *
43  * Revision 1.1 2004/01/08 09:22:40 e_gourgoulhon
44  * First version.
45  *
46  *
47  * $Header: /cvsroot/Lorene/C++/Source/Tensor/tensor_sym_arithm.C,v 1.3 2014/10/13 08:53:44 j_novak Exp $
48  *
49  */
50 
51 // Headers C
52 #include <cstdlib>
53 #include <cassert>
54 #include <cmath>
55 
56 // Headers Lorene
57 #include "tensor.h"
58  //********************//
59  // OPERATEURS UNAIRES //
60  //********************//
61 
62 namespace Lorene {
64 
65  return t ;
66 
67 }
68 
69 
71 
72  Tensor_sym res(tt.get_mp(), tt.get_valence(), tt.get_index_type(),
73  *(tt.get_triad()), tt.sym_index1(), tt.sym_index2()) ;
74 
75  for (int ic=0 ; ic<res.get_n_comp() ; ic++) {
76  Itbl ind = res.indices(ic) ;
77  res.set(ind) = -tt(ind) ;
78  }
79  return res ;
80 
81 }
82 
83  //**********//
84  // ADDITION //
85  //**********//
86 
87 
88 Tensor_sym operator+(const Tensor_sym& t1, const Tensor_sym& t2) {
89 
90  assert (t1.get_valence() == t2.get_valence()) ;
91  assert (t1.get_mp() == t2.get_mp()) ;
92  assert ( *(t1.get_triad()) == *(t2.get_triad()) ) ;
93 
94  for (int id=0 ; id<t1.get_valence() ; id++)
95  assert(t1.get_index_type(id) == t2.get_index_type(id)) ;
96 
97  int ids1 = t1.sym_index1() ;
98  int ids2 = t1.sym_index2() ;
99 
100  assert(t2.sym_index1() == ids1) ;
101  assert(t2.sym_index2() == ids2) ;
102 
103  Tensor_sym res(t1.get_mp(), t1.get_valence(), t1.get_index_type(),
104  *(t1.get_triad()), ids1, ids2) ;
105 
106  for (int ic=0 ; ic<res.get_n_comp() ; ic++) {
107  Itbl ind = res.indices(ic) ;
108  res.set(ind) = t1(ind) + t2(ind) ;
109  }
110  return res ;
111 
112 }
113 
114  //**************//
115  // SOUSTRACTION //
116  //**************//
117 
118 
119 Tensor_sym operator-(const Tensor_sym& t1, const Tensor_sym& t2) {
120 
121  assert (t1.get_valence() == t2.get_valence()) ;
122  assert (t1.get_mp() == t2.get_mp()) ;
123  assert ( *(t1.get_triad()) == *(t2.get_triad()) ) ;
124 
125  for (int id=0 ; id<t1.get_valence() ; id++)
126  assert(t1.get_index_type(id) == t2.get_index_type(id)) ;
127 
128  int ids1 = t1.sym_index1() ;
129  int ids2 = t1.sym_index2() ;
130 
131  assert(t2.sym_index1() == ids1) ;
132  assert(t2.sym_index2() == ids2) ;
133 
134  Tensor_sym res(t1.get_mp(), t1.get_valence(), t1.get_index_type(),
135  *(t1.get_triad()), ids1, ids2) ;
136 
137  for (int ic=0 ; ic<res.get_n_comp() ; ic++) {
138  Itbl ind = res.indices(ic) ;
139  res.set(ind) = t1(ind) - t2(ind) ;
140  }
141  return res ;
142 
143 }
144 
145 
146 
147  //****************//
148  // MULTIPLICATION //
149  //****************//
150 
151 
152 Tensor_sym operator*(const Scalar& t1, const Tensor_sym& t2) {
153 
154  assert (&(t1.get_mp()) == &(t2.get_mp())) ;
155 
156  if (t1.get_etat() == ETATUN) return t2 ;
157 
158  Tensor_sym res(t2.get_mp(), t2.get_valence(), t2.get_index_type(),
159  *(t2.get_triad()), t2.sym_index1(), t2.sym_index2()) ;
160 
161  for (int ic=0 ; ic<res.get_n_comp() ; ic++) {
162  Itbl ind = res.indices(ic) ;
163  res.set(ind) = t1 * t2(ind) ;
164  }
165 
166  return res ;
167 }
168 
169 
170 Tensor_sym operator*(const Tensor_sym& t2, const Scalar& t1) {
171 
172  return t1*t2 ;
173 }
174 
175 
176 
177 Tensor_sym operator*(double x, const Tensor_sym& tt) {
178 
179  Tensor_sym res(tt.get_mp(), tt.get_valence(), tt.get_index_type(),
180  *(tt.get_triad()), tt.sym_index1(), tt.sym_index2()) ;
181 
182  for (int ic=0 ; ic<res.get_n_comp() ; ic++) {
183  Itbl ind = res.indices(ic) ;
184  res.set(ind) = x * tt(ind) ;
185  }
186 
187  return res ;
188 
189 }
190 
191 
192 Tensor_sym operator*(const Tensor_sym& t, double x) {
193  return x * t ;
194 }
195 
196 
197 Tensor_sym operator*(int m, const Tensor_sym& t) {
198  return double(m) * t ;
199 }
200 
201 
202 Tensor_sym operator*(const Tensor_sym& t, int m) {
203  return double(m) * t ;
204 }
205 
206 
207  //**********//
208  // DIVISION //
209  //**********//
210 
211 Tensor_sym operator/(const Tensor_sym& t1, const Scalar& s2) {
212 
213  // Protections
214  assert(s2.get_etat() != ETATNONDEF) ;
215  assert(t1.get_mp() == s2.get_mp()) ;
216 
217  // Cas particuliers
218  if (s2.get_etat() == ETATZERO) {
219  cout << "Division by 0 in Tensor_sym / Scalar !" << endl ;
220  abort() ;
221  }
222 
223  if (s2.get_etat() == ETATUN) return t1 ;
224 
225  Tensor_sym res(t1.get_mp(), t1.get_valence(), t1.get_index_type(),
226  *(t1.get_triad()), t1.sym_index1(), t1.sym_index2()) ;
227 
228  for (int ic=0 ; ic<res.get_n_comp() ; ic++) {
229  Itbl ind = res.indices(ic) ;
230  res.set(ind) = t1(ind) / s2 ;
231  }
232 
233  return res ;
234 
235 }
236 
237 
238 Tensor_sym operator/(const Tensor_sym& tt, double x) {
239 
240  if ( x == double(0) ) {
241  cout << "Division by 0 in Tensor_sym / double !" << endl ;
242  abort() ;
243  }
244 
245  if ( x == double(1) ) return tt ;
246  else {
247  Tensor_sym res(tt.get_mp(), tt.get_valence(), tt.get_index_type(),
248  *(tt.get_triad()), tt.sym_index1(), tt.sym_index2()) ;
249 
250  for (int ic=0 ; ic<res.get_n_comp() ; ic++) {
251  Itbl ind = res.indices(ic) ;
252  res.set(ind) = tt(ind) / x ;
253  }
254 
255  return res ;
256  }
257 
258 }
259 
260 
261 Tensor_sym operator/(const Tensor_sym& t, int m) {
262 
263  return t / double(m) ;
264 }
265 
266 
267 }
Basic integer array class.
Definition: itbl.h:122
Tensor field of valence 0 (or component of a tensorial field).
Definition: scalar.h:387
int get_etat() const
Returns the logical state ETATNONDEF (undefined), ETATZERO (null) or ETATQCQ (ordinary).
Definition: scalar.h:554
Symmetric tensors (with respect to two of their arguments).
Definition: tensor.h:1037
Base_val operator*(const Base_val &, const Base_val &)
This operator is used when calling multiplication or division of Valeur .
Cmp operator-(const Cmp &)
- Cmp
Definition: cmp_arithm.C:108
Cmp operator/(const Cmp &, const Cmp &)
Cmp / Cmp.
Definition: cmp_arithm.C:457
Cmp operator+(const Cmp &)
Definition: cmp_arithm.C:104
const Map & get_mp() const
Returns the mapping.
Definition: tensor.h:861
int sym_index1() const
Number of the first symmetric index (0<= id_sym1 < valence )
Definition: tensor.h:1149
int get_index_type(int i) const
Gives the type (covariant or contravariant) of the index number i .
Definition: tensor.h:886
virtual Itbl indices(int pos) const
Returns the indices of a component given by its position in the array cmp .
Definition: tensor_sym.C:310
int sym_index2() const
Number of the second symmetric index (id_sym1 < id_sym2 < valence )
Definition: tensor.h:1154
int get_valence() const
Returns the valence.
Definition: tensor.h:869
int get_n_comp() const
Returns the number of stored components.
Definition: tensor.h:872
Scalar & set(const Itbl &ind)
Returns the value of a component (read/write version).
Definition: tensor.C:654
const Base_vect * get_triad() const
Returns the vectorial basis (triad) on which the components are defined.
Definition: tensor.h:866
Lorene prototypes.
Definition: app_hor.h:64