vec_arith.h

Go to the documentation of this file.
00001 /*
00002   -------------------------------------------------------------------
00003   
00004   Copyright (C) 2006, 2007, 2008, Andrew W. Steiner
00005   
00006   This file is part of O2scl.
00007   
00008   O2scl is free software; you can redistribute it and/or modify
00009   it under the terms of the GNU General Public License as published by
00010   the Free Software Foundation; either version 3 of the License, or
00011   (at your option) any later version.
00012   
00013   O2scl is distributed in the hope that it will be useful,
00014   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016   GNU General Public License for more details.
00017   
00018   You should have received a copy of the GNU General Public License
00019   along with O2scl. If not, see <http://www.gnu.org/licenses/>.
00020 
00021   -------------------------------------------------------------------
00022 */
00023 
00024 #ifndef O2SCL_VEC_ARITH_H
00025 #define O2SCL_VEC_ARITH_H
00026 
00027 /** \file vec_arith.h
00028     \brief Vector and matrix arithmetic
00029 
00030     \todo Properly document the operators defined as macros
00031 
00032     \future Define operators for complex vector * real matrix
00033     \future These should be replaced by the BLAS routines where possible?
00034 */
00035 
00036 #include <iostream>
00037 #include <complex>
00038 #include <o2scl/cx_arith.h>
00039 #include <o2scl/ovector_tlate.h>
00040 #include <o2scl/omatrix_tlate.h>
00041 #include <o2scl/uvector_tlate.h>
00042 #include <o2scl/umatrix_tlate.h>
00043 #include <o2scl/ovector_cx_tlate.h>
00044 #include <o2scl/omatrix_cx_tlate.h>
00045 #include <o2scl/uvector_cx_tlate.h>
00046 #include <o2scl/umatrix_cx_tlate.h>
00047 
00048 namespace o2scl_arith {
00049   
00050   /** \brief Naive vector copy
00051    */
00052   template<class vec_t, class vec2_t> 
00053     void vector_copy(size_t N, vec_t &src, vec2_t &dest) {
00054     for(size_t i=0;i<N;i++) dest[i]=src[i];
00055   }
00056   
00057   /** \brief Naive matrix copy
00058    */
00059   template<class mat_t, class mat2_t> 
00060     void matrix_copy(size_t M, size_t N, mat_t &src, mat2_t &dest) {
00061     for(size_t i=0;i<M;i++) {
00062       for(size_t j=0;j<N;j++) {
00063         dest[i][j]=src[i][j];
00064       }
00065     }
00066   }
00067 
00068   /** 
00069       \brief Naive complex vector copy
00070 
00071       \todo Make this more generic?
00072   */
00073   template<class vec_t, class vec2_t> 
00074     void vector_cx_copy(size_t N, vec_t &src, vec2_t &dest) {
00075     for(size_t i=0;i<N;i++) {
00076       dest[i].dat[0]=src[i].dat[0];
00077       dest[i].dat[1]=src[i].dat[1];
00078     }
00079   }
00080   
00081   /** 
00082       \brief Naive complex matrix copy
00083 
00084       \todo Make this more generic?
00085   */
00086   template<class mat_t, class mat2_t> 
00087     void matrix_cx_copy(size_t M, size_t N, mat_t &src, mat2_t &dest) {
00088     for(size_t i=0;i<M;i++) {
00089       for(size_t j=0;j<N;j++) {
00090         dest[i][j].dat[0]=src[i][j].dat[0];
00091         dest[i][j].dat[1]=src[i][j].dat[1];
00092       }
00093     }
00094   }
00095 
00096   /** 
00097       \brief The header macro for vector-vector addition
00098 
00099       Given types \c vec1, \c vec2, and \c vec_3, this macro
00100       provides the function declaration for adding two vectors
00101       using the form
00102       \code
00103       vec1 operator+(const vec2 &x, const vec3 &y);
00104       \endcode
00105 
00106       The corresponding definition is given in 
00107       \ref O2SCL_OPSRC_VEC_VEC_ADD.
00108 
00109       By default, the following operators are defined:
00110       \code
00111       ovector operator+(ovector_view &x, ovector_view &y);
00112       ovector operator+(ovector_view &x, uvector_view &y);
00113       ovector operator+(uvector_view &x, ovector_view &y);
00114       uvector operator+(uvector_view &x, uvector_view &y);
00115       ovector_cx operator+(ovector_cx_view &x, ovector_cx_view &y);
00116       ovector_cx operator+(ovector_cx_view &x, uvector_cx_view &y);
00117       ovector_cx operator+(uvector_cx_view &x, ovector_cx_view &y);
00118       uvector_cx operator+(uvector_cx_view &x, uvector_cx_view &y);
00119       \endcode      
00120   */
00121 #define O2SCL_OP_VEC_VEC_ADD(vec1,vec2,vec3) vec1 operator+     \
00122     (const vec2 &x, const vec3 &y);
00123 
00124 #ifndef DOXYGENP
00125   
00126   O2SCL_OP_VEC_VEC_ADD(o2scl::ovector,o2scl::ovector_view,o2scl::ovector_view)
00127     O2SCL_OP_VEC_VEC_ADD(o2scl::ovector,o2scl::ovector_view,
00128                          o2scl::uvector_view)
00129     O2SCL_OP_VEC_VEC_ADD(o2scl::ovector,o2scl::uvector_view,
00130                          o2scl::ovector_view)
00131     O2SCL_OP_VEC_VEC_ADD(o2scl::uvector,o2scl::uvector_view,
00132                          o2scl::uvector_view)
00133     O2SCL_OP_VEC_VEC_ADD(o2scl::ovector_cx,o2scl::ovector_cx_view,
00134                          o2scl::ovector_cx_view)
00135     O2SCL_OP_VEC_VEC_ADD(o2scl::ovector_cx,o2scl::ovector_cx_view,
00136                          o2scl::uvector_cx_view)
00137     O2SCL_OP_VEC_VEC_ADD(o2scl::ovector_cx,o2scl::uvector_cx_view,
00138                          o2scl::ovector_cx_view)
00139     O2SCL_OP_VEC_VEC_ADD(o2scl::uvector_cx,o2scl::uvector_cx_view,
00140                          o2scl::uvector_cx_view)
00141 
00142 #endif
00143     
00144 #ifdef O2SCL_NEVER_DEFINED
00145     }
00146 {
00147 #endif
00148   
00149   /** 
00150       \brief The header macro for vector-vector subtraction
00151 
00152       Given types \c vec1, \c vec2, and \c vec_3, this macro
00153       provides the function declaration for adding two vectors
00154       using the form
00155       \code
00156       vec1 operator-(const vec2 &x, const vec3 &y);
00157       \endcode
00158 
00159       The corresponding definition is given in 
00160       \ref O2SCL_OPSRC_VEC_VEC_SUB.
00161 
00162       By default, the following operators are defined:
00163       \code
00164       ovector operator-(ovector_view &x, ovector_view &y);
00165       ovector operator-(ovector_view &x, uvector_view &y);
00166       ovector operator-(uvector_view &x, ovector_view &y);
00167       uvector operator-(uvector_view &x, uvector_view &y);
00168       ovector_cx operator-(ovector_cx_view &x, ovector_cx_view &y);
00169       ovector_cx operator-(ovector_cx_view &x, uvector_cx_view &y);
00170       ovector_cx operator-(uvector_cx_view &x, ovector_cx_view &y);
00171       uvector_cx operator-(uvector_cx_view &x, uvector_cx_view &y);
00172       \endcode
00173   */
00174 #define O2SCL_OP_VEC_VEC_SUB(vec1,vec2,vec3) vec1 operator-     \
00175     (const vec2 &x, const vec3 &y);
00176   
00177 #ifndef DOXYGENP
00178 
00179   O2SCL_OP_VEC_VEC_SUB(o2scl::ovector,o2scl::ovector_view,o2scl::ovector_view)
00180     O2SCL_OP_VEC_VEC_SUB(o2scl::ovector,o2scl::ovector_view,
00181                          o2scl::uvector_view)
00182     O2SCL_OP_VEC_VEC_SUB(o2scl::ovector,o2scl::uvector_view,
00183                          o2scl::ovector_view)
00184     O2SCL_OP_VEC_VEC_SUB(o2scl::uvector,o2scl::uvector_view,
00185                          o2scl::uvector_view)
00186     O2SCL_OP_VEC_VEC_SUB(o2scl::ovector_cx,o2scl::ovector_cx_view,
00187                          o2scl::ovector_cx_view)
00188     O2SCL_OP_VEC_VEC_SUB(o2scl::ovector_cx,o2scl::ovector_cx_view,
00189                          o2scl::uvector_cx_view)
00190     O2SCL_OP_VEC_VEC_SUB(o2scl::ovector_cx,o2scl::uvector_cx_view,
00191                          o2scl::ovector_cx_view)
00192     O2SCL_OP_VEC_VEC_SUB(o2scl::uvector_cx,o2scl::uvector_cx_view,
00193                          o2scl::uvector_cx_view)
00194 
00195 #endif
00196           
00197 #ifdef O2SCL_NEVER_DEFINED
00198     }
00199 {
00200 #endif
00201 
00202   /** 
00203       \brief The header macro for matrix-vector (right) multiplication
00204 
00205       Given types \c vec1, \c vec2, and \c mat, this macro
00206       provides the function declaration for adding two vectors
00207       using the form
00208       \code
00209       vec1 operator*(const mat &m, const vec3 &x);
00210       \endcode
00211 
00212       The corresponding definition is given in 
00213       \ref O2SCL_OPSRC_MAT_VEC_MULT.
00214 
00215       By default, the following operators are defined:
00216       \code
00217       ovector operator*(omatrix_view &x, ovector_view &y);
00218       ovector operator*(omatrix_view &x, uvector_view &y);
00219       ovector operator*(umatrix_view &x, ovector_view &y);
00220       uvector operator*(umatrix_view &x, uvector_view &y);
00221       \endcode
00222   */
00223 #define O2SCL_OP_MAT_VEC_MULT(vec1,vec2,mat) vec1 operator*     \
00224     (const mat &m, const vec2 &x);
00225   
00226 #ifndef DOXYGENP
00227 
00228   O2SCL_OP_MAT_VEC_MULT(o2scl::ovector,o2scl::ovector_view,o2scl::omatrix_view)
00229     O2SCL_OP_MAT_VEC_MULT(o2scl::ovector,o2scl::ovector_view,
00230                           o2scl::umatrix_view)
00231     O2SCL_OP_MAT_VEC_MULT(o2scl::ovector,o2scl::uvector_view,
00232                           o2scl::omatrix_view)
00233     O2SCL_OP_MAT_VEC_MULT(o2scl::uvector,o2scl::uvector_view,
00234                           o2scl::umatrix_view)
00235 
00236 #endif
00237     
00238 #ifdef O2SCL_NEVER_DEFINED
00239     }
00240 {
00241 #endif
00242 
00243   /** 
00244       \brief The header macro for complex matrix-vector (right) 
00245       multiplication
00246 
00247       Given types \c vec1, \c vec2, and \c mat, this macro
00248       provides the function declaration for adding two vectors
00249       using the form
00250       \code
00251       vec1 operator*(const mat &m, const vec3 &x);
00252       \endcode
00253 
00254       The corresponding definition is given in 
00255       \ref O2SCL_OPSRC_CMAT_CVEC_MULT.
00256 
00257       By default, the following operators are defined:
00258       \code
00259       ovector_cx operator*(omatrix_cx_view &x, ovector_cx_view &y);
00260       ovector_cx operator*(omatrix_cx_view &x, uvector_cx_view &y);
00261       ovector_cx operator*(umatrix_cx_view &x, ovector_cx_view &y);
00262       uvector_cx operator*(umatrix_cx_view &x, uvector_cx_view &y);
00263       \endcode
00264   */
00265 #define O2SCL_OP_CMAT_CVEC_MULT(vec1,vec2,mat) vec1 operator*   \
00266     (const mat &m, const vec2 &x);
00267   
00268 #ifndef DOXYGENP
00269 
00270   O2SCL_OP_CMAT_CVEC_MULT(o2scl::ovector_cx,o2scl::ovector_cx_view,
00271                           o2scl::omatrix_cx_view)
00272     O2SCL_OP_CMAT_CVEC_MULT(o2scl::ovector_cx,o2scl::ovector_cx_view,
00273                             o2scl::umatrix_cx_view)
00274     O2SCL_OP_CMAT_CVEC_MULT(o2scl::ovector_cx,o2scl::uvector_cx_view,
00275                             o2scl::omatrix_cx_view)
00276     O2SCL_OP_CMAT_CVEC_MULT(o2scl::uvector_cx,o2scl::uvector_cx_view,
00277                             o2scl::umatrix_cx_view)
00278 
00279 #endif
00280 
00281 #ifdef O2SCL_NEVER_DEFINED
00282     }
00283 {
00284 #endif
00285 
00286   /** 
00287       \brief The header macro for vector-matrix (left) 
00288       multiplication
00289 
00290       Given types \c vec1, \c vec2, and \c mat, this macro
00291       provides the function declaration for adding two vectors
00292       using the form
00293       \code
00294       vec1 operator*(const vec3 &x, const mat &m);
00295       \endcode
00296 
00297       The corresponding definition is given in 
00298       \ref O2SCL_OPSRC_VEC_MAT_MULT.
00299   */
00300 #define O2SCL_OP_VEC_MAT_MULT(vec1,vec2,mat) vec1 operator*     \
00301     (const vec2 &x, const mat &m);
00302   
00303 #ifndef DOXYGENP
00304 
00305   O2SCL_OP_VEC_MAT_MULT(o2scl::ovector,o2scl::ovector_view,o2scl::omatrix_view)
00306     O2SCL_OP_VEC_MAT_MULT(o2scl::ovector,o2scl::ovector_view,
00307                           o2scl::umatrix_view)
00308     O2SCL_OP_VEC_MAT_MULT(o2scl::ovector,o2scl::uvector_view,
00309                           o2scl::omatrix_view)
00310     O2SCL_OP_VEC_MAT_MULT(o2scl::uvector,o2scl::uvector_view,
00311                           o2scl::umatrix_view)
00312 
00313 #endif
00314 
00315 #ifdef O2SCL_NEVER_DEFINED
00316     }
00317 {
00318 #endif
00319 
00320   /// The header macro for the \c trans_mult form of vector * matrix
00321 #define O2SCL_OP_TRANS_MULT(vec1,vec2,mat) vec1 trans_mult      \
00322     (const vec2 &x, const mat &m);
00323   
00324 #ifndef DOXYGENP
00325 
00326   O2SCL_OP_TRANS_MULT(o2scl::ovector,o2scl::ovector_view,o2scl::omatrix_view)
00327     O2SCL_OP_TRANS_MULT(o2scl::ovector,o2scl::ovector_view,o2scl::umatrix_view)
00328     O2SCL_OP_TRANS_MULT(o2scl::ovector,o2scl::uvector_view,o2scl::omatrix_view)
00329     O2SCL_OP_TRANS_MULT(o2scl::uvector,o2scl::uvector_view,o2scl::umatrix_view)
00330 
00331 #endif
00332             
00333 #ifdef O2SCL_NEVER_DEFINED
00334     }
00335 {
00336 #endif
00337 
00338   /** 
00339       \brief The header macro for vector scalar (dot) product
00340 
00341       Given types \c vec1, \c vec2, and \c dtype, this macro
00342       provides the function declaration for adding two vectors
00343       using the form
00344       \code
00345       dtype operator*(const vec1 &x, const vec2 &y);
00346       \endcode
00347 
00348       The corresponding definition is given in 
00349       \ref O2SCL_OPSRC_DOT_PROD.
00350   */
00351 #define O2SCL_OP_DOT_PROD(dtype,vec1,vec2) dtype dot    \
00352     (const vec1 &x, const vec2 &y);
00353             
00354 #ifndef DOXYGENP
00355 
00356   O2SCL_OP_DOT_PROD(double,o2scl::ovector_view,o2scl::ovector_view)
00357     O2SCL_OP_DOT_PROD(double,o2scl::ovector_view,o2scl::uvector_view)
00358     O2SCL_OP_DOT_PROD(double,o2scl::uvector_view,o2scl::ovector_view)
00359     O2SCL_OP_DOT_PROD(double,o2scl::uvector_view,o2scl::uvector_view)
00360 
00361 #endif
00362 
00363 #ifdef O2SCL_NEVER_DEFINED
00364     }
00365 {
00366 #endif
00367 
00368   /** 
00369       \brief The header macro for complex vector scalar (dot) 
00370       product
00371       
00372       Given types \c vec1, \c vec2, and \c dtype, this macro
00373       provides the function declaration for adding two vectors
00374       using the form
00375       \code
00376       dtype operator*(const vec1 &x, const vec2 &y);
00377       \endcode
00378 
00379       The corresponding definition is given in 
00380       \ref O2SCL_OPSRC_CX_DOT_PROD.
00381   */
00382 #define O2SCL_OP_CX_DOT_PROD(dtype,vec1,vec2) dtype dot \
00383     (const vec1 &x, const vec2 &y);
00384 
00385 #ifndef DOXYGENP
00386 
00387   O2SCL_OP_CX_DOT_PROD(gsl_complex,o2scl::ovector_cx_view,
00388                        o2scl::ovector_cx_view)
00389     O2SCL_OP_CX_DOT_PROD(gsl_complex,o2scl::ovector_cx_view,
00390                          o2scl::uvector_cx_view)
00391     O2SCL_OP_CX_DOT_PROD(gsl_complex,o2scl::uvector_cx_view,
00392                          o2scl::ovector_cx_view)
00393     O2SCL_OP_CX_DOT_PROD(gsl_complex,o2scl::uvector_cx_view,
00394                          o2scl::uvector_cx_view)
00395 
00396 #endif
00397 
00398 #ifdef O2SCL_NEVER_DEFINED
00399     }
00400 {
00401 #endif
00402 
00403   /** 
00404       \brief The header macro for scalar-vector multiplication
00405       
00406       Given types \c vecv, \c vec, and \c dtype, this macro
00407       provides the function declaration for adding two vectors
00408       using the form
00409       \code
00410       vec operator*(const dtype &x, const vecv &y);
00411       \endcode
00412 
00413       The corresponding definition is given in 
00414       \ref O2SCL_OPSRC_SCA_VEC_MULT.
00415   */
00416 #define O2SCL_OP_SCA_VEC_MULT(dtype,vecv,vec) vec operator*     \
00417     (const dtype &x, const vecv &y);
00418   
00419 #ifndef DOXYGENP
00420 
00421   O2SCL_OP_SCA_VEC_MULT(double,o2scl::ovector_view,o2scl::ovector)
00422     O2SCL_OP_SCA_VEC_MULT(double,o2scl::uvector_view,o2scl::uvector)
00423 
00424 #endif
00425     
00426 #ifdef O2SCL_NEVER_DEFINED
00427     }
00428 {
00429 #endif
00430 
00431   /** 
00432       \brief The header macro for vector-scalar multiplication
00433       
00434       Given types \c vecv, \c vec, and \c dtype, this macro
00435       provides the function declaration for adding two vectors
00436       using the form
00437       \code
00438       vec operator*(const vecv &x, const dtype &y);
00439       \endcode
00440 
00441       The corresponding definition is given in 
00442       \ref O2SCL_OPSRC_VEC_SCA_MULT.
00443   */
00444 #define O2SCL_OP_VEC_SCA_MULT(dtype,vecv,vec) vec operator*     \
00445     (const vecv &x, const dtype &y);
00446   
00447 #ifndef DOXYGENP
00448 
00449   O2SCL_OP_VEC_SCA_MULT(double,o2scl::ovector_view,o2scl::ovector)
00450     O2SCL_OP_VEC_SCA_MULT(double,o2scl::uvector_view,o2scl::uvector)
00451 
00452 #endif
00453 
00454 #ifdef O2SCL_NEVER_DEFINED
00455     }
00456 {
00457 #endif
00458               
00459   /** 
00460       \brief The header macro for pairwise vector * vector (where either 
00461       vector can be real or complex)
00462       
00463       Given types \c vec1, \c vec2, and \c vec3, this macro
00464       provides the function declaration for adding two vectors
00465       using the form
00466       \code
00467       vec1 pair_prod(const vec2 &x, const vec3 &y);
00468       \endcode
00469 
00470       The corresponding definition is given in 
00471       \ref O2SCL_OPSRC_VEC_VEC_PRO.
00472   */
00473 #define O2SCL_OP_VEC_VEC_PRO(vec1,vec2,vec3) vec1 pair_prod     \
00474     (const vec2 &x, const vec3 &y);
00475   
00476 #ifndef DOXYGENP
00477 
00478   O2SCL_OP_VEC_VEC_PRO(o2scl::ovector,o2scl::ovector_view,o2scl::ovector_view)
00479     O2SCL_OP_VEC_VEC_PRO(o2scl::ovector,o2scl::ovector_view,
00480                          o2scl::uvector_view)
00481     O2SCL_OP_VEC_VEC_PRO(o2scl::ovector,o2scl::uvector_view,
00482                          o2scl::ovector_view)
00483     O2SCL_OP_VEC_VEC_PRO(o2scl::uvector,o2scl::uvector_view,
00484                          o2scl::uvector_view)
00485     O2SCL_OP_VEC_VEC_PRO(o2scl::ovector_cx,o2scl::ovector_cx_view,
00486                          o2scl::ovector_view)
00487     O2SCL_OP_VEC_VEC_PRO(o2scl::ovector_cx,o2scl::ovector_cx_view,
00488                          o2scl::uvector_view)
00489     O2SCL_OP_VEC_VEC_PRO(o2scl::ovector_cx,o2scl::uvector_cx_view,
00490                          o2scl::ovector_view)
00491     O2SCL_OP_VEC_VEC_PRO(o2scl::uvector_cx,o2scl::uvector_cx_view,
00492                          o2scl::uvector_view)
00493     O2SCL_OP_VEC_VEC_PRO(o2scl::ovector_cx,o2scl::ovector_view,
00494                          o2scl::ovector_cx_view)
00495     O2SCL_OP_VEC_VEC_PRO(o2scl::ovector_cx,o2scl::ovector_view,
00496                          o2scl::uvector_cx_view)
00497     O2SCL_OP_VEC_VEC_PRO(o2scl::ovector_cx,o2scl::uvector_view,
00498                          o2scl::ovector_cx_view)
00499     O2SCL_OP_VEC_VEC_PRO(o2scl::uvector_cx,o2scl::uvector_view,
00500                          o2scl::uvector_cx_view)
00501     O2SCL_OP_VEC_VEC_PRO(o2scl::ovector_cx,o2scl::ovector_cx_view,
00502                          o2scl::ovector_cx_view)
00503     O2SCL_OP_VEC_VEC_PRO(o2scl::ovector_cx,o2scl::ovector_cx_view,
00504                          o2scl::uvector_cx_view)
00505     O2SCL_OP_VEC_VEC_PRO(o2scl::ovector_cx,o2scl::uvector_cx_view,
00506                          o2scl::ovector_cx_view)
00507     O2SCL_OP_VEC_VEC_PRO(o2scl::uvector_cx,o2scl::uvector_cx_view,
00508                          o2scl::uvector_cx_view)
00509 
00510 #endif
00511     
00512     /** 
00513         \brief The source code macro for vector-vector addition
00514 
00515         This define macro generates the function definition. See
00516         the function declaration \ref O2SCL_OP_VEC_VEC_ADD
00517     */
00518 #define O2SCL_OPSRC_VEC_VEC_ADD(vec1,vec2,vec3) vec1 o2scl_arith::operator+ \
00519       (const vec2 &x, const vec3 &y) {                                  \
00520       size_t m=x.size();                                                \
00521       if (y.size()<m) m=y.size();                                       \
00522       vec1 r(m);                                                        \
00523       for(size_t i=0;i<m;i++) {                                         \
00524         r[i]=x[i]+y[i];                                                 \
00525       }                                                                 \
00526       return r;                                                         \
00527     }
00528     
00529     /** 
00530         \brief The source code macro for vector-vector subtraction
00531 
00532         This define macro generates the function definition. See
00533         the function declaration \ref O2SCL_OP_VEC_VEC_SUB
00534     */
00535 #define O2SCL_OPSRC_VEC_VEC_SUB(vec1,vec2,vec3) vec1 o2scl_arith::operator- \
00536       (const vec2 &x, const vec3 &y) {                                  \
00537       size_t m=x.size();                                                \
00538       if (y.size()<m) m=y.size();                                       \
00539       vec1 r(m);                                                        \
00540       for(size_t i=0;i<m;i++) {                                         \
00541         r[i]=x[i]-y[i];                                                 \
00542       }                                                                 \
00543       return r;                                                         \
00544     }
00545 
00546     /** 
00547         \brief The source code macro for matrix * vector
00548 
00549         This define macro generates the function definition. See
00550         the function declaration \ref O2SCL_OP_MAT_VEC_MULT
00551     */
00552 #define O2SCL_OPSRC_MAT_VEC_MULT(vec1,vec2,mat) vec1 o2scl_arith::operator* \
00553       (const mat &m, const vec2 &x) {                                   \
00554       size_t nr=m.rows();                                               \
00555       size_t nc=m.cols();                                               \
00556       vec1 res(nr);                                                     \
00557       for(size_t i=0;i<nr;i++) {                                        \
00558         double r=0.0;                                                   \
00559         for(size_t j=0;j<nc;j++) {                                      \
00560           r+=m[i][j]*x[j];                                              \
00561         }                                                               \
00562         res[i]=r;                                                       \
00563       }                                                                 \
00564       return res;                                                       \
00565     } 
00566   
00567     /** 
00568         \brief The source code macro for complex matrix * complex vector
00569 
00570         This define macro generates the function definition. See
00571         the function declaration \ref O2SCL_OP_CMAT_CVEC_MULT
00572     */
00573 #define O2SCL_OPSRC_CMAT_CVEC_MULT(vec1,vec2,mat) vec1 o2scl_arith::operator* \
00574       (const mat &m, const vec2 &x) {                                   \
00575       size_t nr=m.rows();                                               \
00576       size_t nc=m.cols();                                               \
00577       vec1 res(nr);                                                     \
00578       for(size_t i=0;i<nr;i++) {                                        \
00579         double re=0.0;                                                  \
00580         double im=0.0;                                                  \
00581         for(size_t j=0;j<nc;j++) {                                      \
00582           gsl_complex g=m[i][j]*x[j];                                   \
00583           re+=g.dat[0];                                                 \
00584           im+=g.dat[1];                                                 \
00585         }                                                               \
00586         res[i].dat[0]=re;                                               \
00587         res[i].dat[1]=im;                                               \
00588       }                                                                 \
00589       return res;                                                       \
00590     } 
00591   
00592     /** 
00593         \brief The source code macro for the operator form of vector * matrix
00594 
00595         This define macro generates the function definition. See
00596         the function declaration \ref O2SCL_OP_VEC_MAT_MULT
00597     */
00598 #define O2SCL_OPSRC_VEC_MAT_MULT(vec1,vec2,mat) vec1 o2scl_arith::operator* \
00599       (const vec2 &x, const mat &m) {                                   \
00600       size_t nr=m.rows();                                               \
00601       size_t nc=m.cols();                                               \
00602       vec1 res(nr);                                                     \
00603       for(size_t j=0;j<nc;j++) {                                        \
00604         double r=0.0;                                                   \
00605         for(size_t i=0;i<nr;i++) {                                      \
00606           r+=x[i]*m[i][j];                                              \
00607         }                                                               \
00608         res[j]=r;                                                       \
00609       }                                                                 \
00610       return res;                                                       \
00611     } 
00612   
00613     /** 
00614         \brief The source code macro for the \c trans_mult form of 
00615         vector * matrix
00616 
00617         This define macro generates the function definition. See
00618         the function declaration \ref O2SCL_OP_TRANS_MULT
00619     */
00620 #define O2SCL_OPSRC_TRANS_MULT(vec1,vec2,mat) vec1 o2scl_arith::trans_mult \
00621       (const vec2 &x, const mat &m) {                                   \
00622       size_t nr=m.rows();                                               \
00623       size_t nc=m.cols();                                               \
00624       vec1 res(nr);                                                     \
00625       for(size_t j=0;j<nc;j++) {                                        \
00626         double r=0.0;                                                   \
00627         for(size_t i=0;i<nr;i++) {                                      \
00628           r+=x[i]*m[i][j];                                              \
00629         }                                                               \
00630         res[j]=r;                                                       \
00631       }                                                                 \
00632       return res;                                                       \
00633     } 
00634   
00635     /** 
00636         \brief The source code macro for a vector dot product
00637 
00638         This define macro generates the function definition. See
00639         the function declaration \ref O2SCL_OP_DOT_PROD
00640     */
00641 #define O2SCL_OPSRC_DOT_PROD(dtype,vec1,vec2) dtype o2scl_arith::dot    \
00642       (const vec1 &x, const vec2 &y) {                                  \
00643       size_t m=x.size();                                                \
00644       if (y.size()<m) m=y.size();                                       \
00645       dtype r=0;                                                        \
00646       for(size_t i=0;i<m;i++) {                                         \
00647         r+=x[i]*y[i];                                                   \
00648       }                                                                 \
00649       return r;                                                         \
00650     }
00651             
00652     /** 
00653         \brief The source code macro for a complex vector dot product
00654 
00655         This define macro generates the function definition. See
00656         the function declaration \ref O2SCL_OP_CX_DOT_PROD
00657     */
00658 #define O2SCL_OPSRC_CX_DOT_PROD(dtype,vec1,vec2) dtype o2scl_arith::dot \
00659       (const vec1 &x, const vec2 &y) {                                  \
00660       size_t m=x.size();                                                \
00661       if (y.size()<m) m=y.size();                                       \
00662       dtype r={{0.0,0.0}};                                              \
00663       for(size_t i=0;i<m;i++) {                                         \
00664         r+=x[i]*y[i];                                                   \
00665       }                                                                 \
00666       return r;                                                         \
00667     }
00668 
00669     /** 
00670         \brief The source code macro for vector=scalar*vector
00671 
00672         This define macro generates the function definition. See
00673         the function declaration \ref O2SCL_OP_SCA_VEC_MULT
00674     */
00675 #define O2SCL_OPSRC_SCA_VEC_MULT(dtype,vecv,vec) vec o2scl_arith::operator* \
00676       (const dtype &x, const vecv &y) {                                 \
00677       size_t m=y.size();                                                \
00678       vec r(m);                                                         \
00679       for(size_t i=0;i<m;i++) {                                         \
00680         r[i]=x*y[i];                                                    \
00681       }                                                                 \
00682       return r;                                                         \
00683     }
00684   
00685     /** 
00686         \brief The source code macro for vector=vector*scalar
00687 
00688         This define macro generates the function definition. See
00689         the function declaration \ref O2SCL_OP_VEC_SCA_MULT
00690     */
00691 #define O2SCL_OPSRC_VEC_SCA_MULT(dtype,vecv,vec) vec o2scl_arith::operator* \
00692       (const vecv &x, const dtype &y) {                                 \
00693       size_t m=x.size();                                                \
00694       vec r(m);                                                         \
00695       for(size_t i=0;i<m;i++) {                                         \
00696         r[i]=x[i]*y;                                                    \
00697       }                                                                 \
00698       return r;                                                         \
00699     }
00700     
00701     /** 
00702         \brief The source code macro for pairwise vector * vector (where
00703         either vector can be real or complex)
00704         
00705         This define macro generates the function definition. See
00706         the function declaration \ref O2SCL_OP_VEC_VEC_PRO
00707     */
00708 #define O2SCL_OPSRC_VEC_VEC_PRO(vec1,vec2,vec3) vec1 o2scl_arith::pair_prod \
00709       (const vec2 &x, const vec3 &y) {                                  \
00710       size_t m=x.size();                                                \
00711       if (y.size()<m) m=y.size();                                       \
00712       vec1 r(m);                                                        \
00713       for(size_t i=0;i<m;i++) {                                         \
00714         r[i]=x[i]*y[i];                                                 \
00715       }                                                                 \
00716       return r;                                                         \
00717     }
00718     
00719     /** 
00720         \brief The header macro for vector==vector 
00721 
00722         Given types \c vec1 and \c vec2, this macro provides the
00723         function declaration for vector equality comparisons using
00724         \code
00725         bool operator==(const vec1 &x, const vec2 &y);
00726         \endcode
00727 
00728         \note Two vectors with different sizes are defined to 
00729         be not equal, no matter what their contents.
00730 
00731         The corresponding definition is given in 
00732         \ref O2SCL_OPSRC_VEC_VEC_EQUAL.
00733     */
00734 #define O2SCL_OP_VEC_VEC_EQUAL(vec1,vec2) bool operator==       \
00735       (const vec1 &x, const vec2 &y);
00736     
00737 #ifndef DOXYGENP
00738     
00739     O2SCL_OP_VEC_VEC_EQUAL(o2scl::ovector_view,
00740                            o2scl::ovector_view)
00741       O2SCL_OP_VEC_VEC_EQUAL(o2scl::ovector_view,
00742                              o2scl::uvector_view)
00743       O2SCL_OP_VEC_VEC_EQUAL(o2scl::uvector_view,
00744                              o2scl::ovector_view)
00745       O2SCL_OP_VEC_VEC_EQUAL(o2scl::uvector_view,
00746                              o2scl::uvector_view)
00747       /*
00748       O2SCL_OP_VEC_VEC_EQUAL(o2scl::ovector_cx_view,
00749                              o2scl::ovector_view)
00750       O2SCL_OP_VEC_VEC_EQUAL(o2scl::ovector_cx_view,
00751                              o2scl::uvector_view)
00752       O2SCL_OP_VEC_VEC_EQUAL(o2scl::uvector_cx_view,
00753                              o2scl::ovector_view)
00754       O2SCL_OP_VEC_VEC_EQUAL(o2scl::uvector_cx_view,
00755                              o2scl::uvector_view)
00756       O2SCL_OP_VEC_VEC_EQUAL(o2scl::ovector_view,
00757                              o2scl::ovector_cx_view)
00758       O2SCL_OP_VEC_VEC_EQUAL(o2scl::ovector_view,
00759                              o2scl::uvector_cx_view)
00760       O2SCL_OP_VEC_VEC_EQUAL(o2scl::uvector_view,
00761                              o2scl::ovector_cx_view)
00762       O2SCL_OP_VEC_VEC_EQUAL(o2scl::uvector_view,
00763                              o2scl::uvector_cx_view)
00764       O2SCL_OP_VEC_VEC_EQUAL(o2scl::ovector_cx_view,
00765                              o2scl::ovector_cx_view)
00766       O2SCL_OP_VEC_VEC_EQUAL(o2scl::ovector_cx_view,
00767                              o2scl::uvector_cx_view)
00768       O2SCL_OP_VEC_VEC_EQUAL(o2scl::uvector_cx_view,
00769                              o2scl::ovector_cx_view)
00770       O2SCL_OP_VEC_VEC_EQUAL(o2scl::uvector_cx_view,
00771                              o2scl::uvector_cx_view)
00772       */
00773 
00774 #endif
00775 
00776 #ifdef O2SCL_NEVER_DEFINED
00777       }
00778 {
00779 #endif
00780               
00781   /** 
00782       \brief The source code macro vector==vector
00783         
00784       \note Two vectors with different sizes are defined to 
00785       be not equal, no matter what their contents.
00786 
00787       This define macro generates the function definition. See
00788       the function declaration \ref O2SCL_OP_VEC_VEC_EQUAL
00789   */
00790 #define O2SCL_OPSRC_VEC_VEC_EQUAL(vec1,vec2) bool o2scl_arith::operator== \
00791     (const vec1 &x, const vec2 &y) {                                    \
00792     size_t m=x.size();                                                  \
00793     size_t n=y.size();                                                  \
00794     if (m!=n) return false;                                             \
00795     for(size_t i=0;i<m;i++) {                                           \
00796       if (x[i]!=y[i]) return false;                                     \
00797     }                                                                   \
00798     return true;                                                        \
00799   }
00800     
00801   /** 
00802       \brief The header macro for vector!=vector 
00803 
00804       Given types \c vec1 and \c vec2, this macro provides the
00805       function declaration for vector inequality comparisons using
00806       \code
00807       bool operator==(const vec1 &x, const vec2 &y);
00808       \endcode
00809 
00810       \note Two vectors with different sizes are defined to 
00811       be not equal, no matter what their contents.
00812       
00813       The corresponding definition is given in 
00814       \ref O2SCL_OPSRC_VEC_VEC_NEQUAL.
00815   */
00816 #define O2SCL_OP_VEC_VEC_NEQUAL(vec1,vec2) bool operator!=      \
00817     (const vec1 &x, const vec2 &y);
00818     
00819 #ifndef DOXYGENP
00820     
00821   O2SCL_OP_VEC_VEC_NEQUAL(o2scl::ovector_view,
00822                           o2scl::ovector_view)
00823     O2SCL_OP_VEC_VEC_NEQUAL(o2scl::ovector_view,
00824                             o2scl::uvector_view)
00825     O2SCL_OP_VEC_VEC_NEQUAL(o2scl::uvector_view,
00826                             o2scl::ovector_view)
00827     O2SCL_OP_VEC_VEC_NEQUAL(o2scl::uvector_view,
00828                             o2scl::uvector_view)
00829     /*
00830     O2SCL_OP_VEC_VEC_NEQUAL(o2scl::ovector_cx_view,
00831                             o2scl::ovector_view)
00832     O2SCL_OP_VEC_VEC_NEQUAL(o2scl::ovector_cx_view,
00833                             o2scl::uvector_view)
00834     O2SCL_OP_VEC_VEC_NEQUAL(o2scl::uvector_cx_view,
00835                             o2scl::ovector_view)
00836     O2SCL_OP_VEC_VEC_NEQUAL(o2scl::uvector_cx_view,
00837                             o2scl::uvector_view)
00838     O2SCL_OP_VEC_VEC_NEQUAL(o2scl::ovector_view,
00839                             o2scl::ovector_cx_view)
00840     O2SCL_OP_VEC_VEC_NEQUAL(o2scl::ovector_view,
00841                             o2scl::uvector_cx_view)
00842     O2SCL_OP_VEC_VEC_NEQUAL(o2scl::uvector_view,
00843                             o2scl::ovector_cx_view)
00844     O2SCL_OP_VEC_VEC_NEQUAL(o2scl::uvector_view,
00845                             o2scl::uvector_cx_view)
00846     O2SCL_OP_VEC_VEC_NEQUAL(o2scl::ovector_cx_view,
00847                             o2scl::ovector_cx_view)
00848     O2SCL_OP_VEC_VEC_NEQUAL(o2scl::ovector_cx_view,
00849                             o2scl::uvector_cx_view)
00850     O2SCL_OP_VEC_VEC_NEQUAL(o2scl::uvector_cx_view,
00851                             o2scl::ovector_cx_view)
00852     O2SCL_OP_VEC_VEC_NEQUAL(o2scl::uvector_cx_view,
00853                             o2scl::uvector_cx_view)
00854     */
00855 
00856 #endif
00857 
00858 #ifdef O2SCL_NEVER_DEFINED
00859     }
00860 {
00861 #endif
00862               
00863   /** 
00864       \brief The source code macro vector!=vector
00865         
00866       \note Two vectors with different sizes are defined to 
00867       be not equal, no matter what their contents.
00868 
00869       This define macro generates the function definition. See
00870       the function declaration \ref O2SCL_OP_VEC_VEC_NEQUAL
00871   */
00872 #define O2SCL_OPSRC_VEC_VEC_NEQUAL(vec1,vec2) bool o2scl_arith::operator!= \
00873     (const vec1 &x, const vec2 &y) {                                    \
00874     size_t m=x.size();                                                  \
00875     size_t n=y.size();                                                  \
00876     if (m!=n) return true;                                              \
00877     for(size_t i=0;i<m;i++) {                                           \
00878       if (x[i]!=y[i]) return true;                                      \
00879     }                                                                   \
00880     return false;                                                       \
00881   }
00882 
00883   // end of o2scl_arith namespace    
00884 }
00885 
00886 #endif

Documentation generated with Doxygen and provided under the GNU Free Documentation License. See License Information for details.

Project hosting provided by SourceForge.net Logo, O2scl Sourceforge Project Page