tensor_arithm.C

00001 /*
00002  *  Arithmetics functions for the Tensor class.
00003  *
00004  *  These functions are not member functions of the Tensor class.
00005  *
00006  *  (see file tensor.h for documentation).
00007  *
00008  */
00009 
00010 /*
00011  *   Copyright (c) 2003 Eric Gourgoulhon & Jerome Novak
00012  *   Copyright (c) 1999-2001 Philippe Grandclement (Tenseur version)
00013  *   Copyright (c) 2000-2001 Eric Gourgoulhon      (Tenseur version)
00014  *
00015  *   This file is part of LORENE.
00016  *
00017  *   LORENE is free software; you can redistribute it and/or modify
00018  *   it under the terms of the GNU General Public License as published by
00019  *   the Free Software Foundation; either version 2 of the License, or
00020  *   (at your option) any later version.
00021  *
00022  *   LORENE is distributed in the hope that it will be useful,
00023  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00024  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025  *   GNU General Public License for more details.
00026  *
00027  *   You should have received a copy of the GNU General Public License
00028  *   along with LORENE; if not, write to the Free Software
00029  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00030  *
00031  */
00032 
00033 char tensor_arithm_C[] = "$Header: /cvsroot/Lorene/C++/Source/Tensor/tensor_arithm.C,v 1.3 2004/01/08 09:24:11 e_gourgoulhon Exp $" ;
00034 
00035 /*
00036  * $Id: tensor_arithm.C,v 1.3 2004/01/08 09:24:11 e_gourgoulhon Exp $
00037  * $Log: tensor_arithm.C,v $
00038  * Revision 1.3  2004/01/08 09:24:11  e_gourgoulhon
00039  * Added arithmetics common to Scalar and Tensor.
00040  * Corrected treatment ETATUN in Tensor / Scalar.
00041  *
00042  * Revision 1.2  2003/10/01 13:04:44  e_gourgoulhon
00043  * The method Tensor::get_mp() returns now a reference (and not
00044  * a pointer) onto a mapping.
00045  *
00046  * Revision 1.1  2003/09/26 14:33:53  j_novak
00047  * Arithmetic functions for the class Tensor
00048  *
00049  *
00050  *
00051  * $Header: /cvsroot/Lorene/C++/Source/Tensor/tensor_arithm.C,v 1.3 2004/01/08 09:24:11 e_gourgoulhon Exp $
00052  *
00053  */
00054 
00055 // Headers C
00056 #include <stdlib.h>
00057 #include <assert.h>
00058 #include <math.h>
00059 
00060 // Headers Lorene
00061 #include "tensor.h"
00062 
00063             //********************//
00064             // OPERATEURS UNAIRES //
00065             //********************//
00066 
00067 Tensor operator+(const Tensor & t) {
00068 
00069     return t ; 
00070 
00071 }
00072 
00073 Tensor operator-(const Tensor & t) {
00074     
00075   Tensor res(t.get_mp(), t.get_valence(), t.get_index_type(), 
00076             t.get_triad()) ;
00077 
00078   for (int i=0 ; i<res.get_n_comp() ; i++) {
00079     Itbl ind (res.indices(i)) ;    
00080     res.set(ind) = -t(ind) ;
00081   }
00082   return res ;
00083 
00084 }
00085 
00086             //**********//
00087             // ADDITION //
00088             //**********//
00089 
00090 Tensor operator+(const Tensor & t1, const Tensor & t2) {
00091     
00092     assert (t1.get_valence() == t2.get_valence()) ;
00093     assert (t1.get_mp() == t2.get_mp()) ;
00094     if (t1.get_valence() != 0) {
00095     assert ( *(t1.get_triad()) == *(t2.get_triad()) ) ;
00096     }
00097     
00098     for (int i=0 ; i<t1.get_valence() ; i++)
00099     assert(t1.get_index_type(i) == t2.get_index_type(i)) ;
00100 
00101     Tensor res(t1.get_mp(), t1.get_valence(), t1.get_index_type(), 
00102             t1.get_triad()) ;
00103 
00104     for (int i=0 ; i<res.get_n_comp() ; i++) {
00105       Itbl ind (res.indices(i)) ;
00106       res.set(ind) = t1(ind) + t2(ind) ;
00107     }
00108     return res ;
00109 
00110 }
00111 
00112 
00113 Scalar operator+(const Tensor& t1, const Scalar& t2) {
00114     
00115     assert (t1.get_valence() == 0) ;
00116     assert (t1.get_mp() == t2.get_mp()) ;
00117 
00118     return  *(t1.cmp[0]) + t2 ; 
00119 
00120 }
00121 
00122 Scalar operator+(const Scalar& t1, const Tensor& t2) {
00123     
00124     assert (t2.get_valence() == 0) ;
00125     assert (t1.get_mp() == t2.get_mp()) ;
00126 
00127     return  t1 + *(t2.cmp[0]) ; 
00128 
00129 }
00130 
00131             //**************//
00132             // SOUSTRACTION //
00133             //**************//
00134 
00135 Tensor operator-(const Tensor & t1, const Tensor & t2) {
00136 
00137     assert (t1.get_valence() == t2.get_valence()) ;
00138     assert (t1.get_mp() == t2.get_mp()) ;
00139     if (t1.get_valence() != 0) {
00140     assert ( *(t1.get_triad()) == *(t2.get_triad()) ) ;
00141     }
00142     
00143     for (int i=0 ; i<t1.get_valence() ; i++)
00144     assert(t1.get_index_type(i) == t2.get_index_type(i)) ;
00145 
00146     Tensor res(t1.get_mp(), t1.get_valence(), t1.get_index_type(), 
00147             t1.get_triad()) ;
00148 
00149     for (int i=0 ; i<res.get_n_comp() ; i++) {
00150       Itbl ind (res.indices(i)) ;
00151       res.set(ind) = t1(ind) - t2(ind) ;
00152     }
00153     return res ;
00154 
00155 }
00156 
00157 Scalar operator-(const Tensor& t1, const Scalar& t2) {
00158     
00159     assert (t1.get_valence() == 0) ;
00160     assert (t1.get_mp() == t2.get_mp()) ;
00161 
00162     return  *(t1.cmp[0]) - t2 ; 
00163 
00164 }
00165 
00166 Scalar operator-(const Scalar& t1, const Tensor& t2) {
00167     
00168     assert (t2.get_valence() == 0) ;
00169     assert (t1.get_mp() == t2.get_mp()) ;
00170 
00171     return  t1 - *(t2.cmp[0]) ; 
00172 
00173 }
00174 
00175 
00176 
00177             //****************//
00178             // MULTIPLICATION //
00179             //****************//
00180 
00181 Tensor operator*(const Scalar& t1, const Tensor& t2) {
00182    
00183     assert (&(t1.get_mp()) == &(t2.get_mp())) ;
00184         
00185     if (t1.get_etat() == ETATUN) return t2 ;
00186     
00187     Tensor res(t2.get_mp(), t2.get_valence(), t2.get_index_type(), 
00188                t2.get_triad()) ;
00189     
00190     for (int ic=0 ; ic<res.get_n_comp() ; ic++) {
00191         Itbl ind = res.indices(ic) ;
00192         res.set(ind) = t1 * t2(ind) ;
00193     }
00194     
00195     return res ;
00196 }
00197 
00198 
00199 Tensor operator*(const Tensor& t2, const Scalar& t1) {
00200        
00201     return t1*t2 ;
00202 }
00203 
00204 
00205 
00206 Tensor operator*(double x, const Tensor& t) {
00207     
00208   Tensor res(t.get_mp(), t.get_valence(), t.get_index_type(), 
00209             t.get_triad()) ;
00210 
00211   for (int i=0 ; i<res.get_n_comp() ; i++) {
00212     Itbl ind (res.indices(i)) ;
00213     res.set(ind) = x*t(ind) ;
00214   }
00215 
00216   return res ; 
00217 
00218 }
00219 
00220 
00221 Tensor operator* (const Tensor& t, double x) {
00222     return x * t ;
00223 }
00224 
00225 Tensor operator*(int m, const Tensor& t) {
00226     return double(m) * t ; 
00227 }
00228 
00229 
00230 Tensor operator* (const Tensor& t, int m) {
00231     return double(m) * t ;
00232 }
00233 
00234 
00235             //**********//
00236             // DIVISION //
00237             //**********//
00238 
00239 Tensor operator/(const Tensor& t1, const Scalar& s2) {
00240     
00241     // Protections
00242     assert(s2.get_etat() != ETATNONDEF) ;
00243     assert(t1.get_mp() == s2.get_mp()) ;
00244 
00245     // Cas particuliers
00246     if (s2.get_etat() == ETATZERO) {
00247     cout << "Division by 0 in Tensor / Scalar !" << endl ;
00248     abort() ; 
00249     }
00250 
00251     if (s2.get_etat() == ETATUN) return t1 ;
00252 
00253     Tensor res(t1.get_mp(), t1.get_valence(), t1.get_index_type(), 
00254         t1.get_triad()) ;
00255 
00256     for (int i=0 ; i<res.get_n_comp() ; i++) {
00257     Itbl ind (res.indices(i)) ;
00258     res.set(ind) = t1(ind) / s2 ;       // Scalar / Scalar
00259     }
00260     return res ;
00261 
00262 }
00263 
00264 
00265 Tensor operator/ (const Tensor& t, double x) {
00266 
00267     if ( x == double(0) ) {
00268     cout << "Division by 0 in Tensor / double !" << endl ;
00269     abort() ;
00270     }
00271 
00272     if (x == double(1)) 
00273       return t ;
00274     else {
00275     Tensor res(t.get_mp(), t.get_valence(), t.get_index_type(), 
00276             t.get_triad()) ;
00277 
00278     for (int i=0 ; i<res.get_n_comp() ; i++) {
00279       Itbl ind (res.indices(i)) ;
00280       res.set(ind) = t(ind) / x ;       // Scalar / double
00281     }
00282     return res ; 
00283     }
00284 
00285 }
00286 
00287 Tensor operator/ (const Tensor& t, int m) {
00288 
00289     return t / double(m) ; 
00290 }
00291 
00292 
00293 
00294 
00295 
00296 
00297 

Generated on Tue Feb 7 01:35:21 2012 for LORENE by  doxygen 1.4.6