tensor_sym_arithm.C

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

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