vec_arith.h

Go to the documentation of this file.
00001 /*
00002   -------------------------------------------------------------------
00003   
00004   Copyright (C) 2006, 2007, 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 &v, vec2_t &v2) {
00054     for(size_t i=0;i<N;i++) v2[i]=v[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 &m, mat2_t &m2) {
00061     for(size_t i=0;i<M;i++) {
00062       for(size_t j=0;j<N;j++) {
00063         m2[i][j]=m[i][j];
00064       }
00065     }
00066   }
00067 
00068   /** \brief Naive complex vector copy
00069    */
00070   template<class vec_t, class vec2_t> 
00071     void vector_cx_copy(size_t N, vec_t &v, vec2_t &v2) {
00072     for(size_t i=0;i<N;i++) {
00073       v2[i].dat[0]=v[i].dat[0];
00074       v2[i].dat[1]=v[i].dat[1];
00075     }
00076   }
00077   
00078   /** \brief Naive complex matrix copy
00079    */
00080   template<class mat_t, class mat2_t> 
00081     void matrix_cx_copy(size_t M, size_t N, mat_t &m, mat2_t &m2) {
00082     for(size_t i=0;i<M;i++) {
00083       for(size_t j=0;j<N;j++) {
00084         m2[i][j].dat[0]=m[i][j].dat[0];
00085         m2[i][j].dat[1]=m[i][j].dat[1];
00086       }
00087     }
00088   }
00089 
00090   /** 
00091       \brief The header macro for vector-vector addition
00092 
00093       Given types \c vec1, \c vec2, and \c vec_3, this macro
00094       provides the function declaration for adding two vectors
00095       using the form
00096       \code
00097       vec1 operator+(const vec2 &x, const vec3 &y);
00098       \endcode
00099 
00100       The corresponding definition is given in 
00101       \ref O2SCL_OPSRC_VEC_VEC_ADD.
00102 
00103       By default, the following operators are defined:
00104       \code
00105       ovector operator+(ovector_view &x, ovector_view &y);
00106       ovector operator+(ovector_view &x, uvector_view &y);
00107       ovector operator+(uvector_view &x, ovector_view &y);
00108       uvector operator+(uvector_view &x, uvector_view &y);
00109       ovector_cx operator+(ovector_cx_view &x, ovector_cx_view &y);
00110       ovector_cx operator+(ovector_cx_view &x, uvector_cx_view &y);
00111       ovector_cx operator+(uvector_cx_view &x, ovector_cx_view &y);
00112       uvector_cx operator+(uvector_cx_view &x, uvector_cx_view &y);
00113       \endcode      
00114   */
00115 #define O2SCL_OP_VEC_VEC_ADD(vec1,vec2,vec3) vec1 operator+     \
00116     (const vec2 &x, const vec3 &y);
00117   
00118   O2SCL_OP_VEC_VEC_ADD(o2scl::ovector,o2scl::ovector_view,o2scl::ovector_view)
00119     O2SCL_OP_VEC_VEC_ADD(o2scl::ovector,o2scl::ovector_view,
00120                          o2scl::uvector_view)
00121     O2SCL_OP_VEC_VEC_ADD(o2scl::ovector,o2scl::uvector_view,
00122                          o2scl::ovector_view)
00123     O2SCL_OP_VEC_VEC_ADD(o2scl::uvector,o2scl::uvector_view,
00124                          o2scl::uvector_view)
00125     O2SCL_OP_VEC_VEC_ADD(o2scl::ovector_cx,o2scl::ovector_cx_view,
00126                        o2scl::ovector_cx_view)
00127     O2SCL_OP_VEC_VEC_ADD(o2scl::ovector_cx,o2scl::ovector_cx_view,
00128                        o2scl::uvector_cx_view)
00129     O2SCL_OP_VEC_VEC_ADD(o2scl::ovector_cx,o2scl::uvector_cx_view,
00130                        o2scl::ovector_cx_view)
00131     O2SCL_OP_VEC_VEC_ADD(o2scl::uvector_cx,o2scl::uvector_cx_view,
00132                        o2scl::uvector_cx_view)
00133     
00134 #ifdef O2SCL_NEVER_DEFINED
00135     }
00136 {
00137 #endif
00138   
00139   /** 
00140       \brief The header macro for vector-vector subtraction
00141 
00142       Given types \c vec1, \c vec2, and \c vec_3, this macro
00143       provides the function declaration for adding two vectors
00144       using the form
00145       \code
00146       vec1 operator-(const vec2 &x, const vec3 &y);
00147       \endcode
00148 
00149       The corresponding definition is given in 
00150       \ref O2SCL_OPSRC_VEC_VEC_SUB.
00151 
00152       By default, the following operators are defined:
00153       \code
00154       ovector operator-(ovector_view &x, ovector_view &y);
00155       ovector operator-(ovector_view &x, uvector_view &y);
00156       ovector operator-(uvector_view &x, ovector_view &y);
00157       uvector operator-(uvector_view &x, uvector_view &y);
00158       ovector_cx operator-(ovector_cx_view &x, ovector_cx_view &y);
00159       ovector_cx operator-(ovector_cx_view &x, uvector_cx_view &y);
00160       ovector_cx operator-(uvector_cx_view &x, ovector_cx_view &y);
00161       uvector_cx operator-(uvector_cx_view &x, uvector_cx_view &y);
00162       \endcode
00163   */
00164 #define O2SCL_OP_VEC_VEC_SUB(vec1,vec2,vec3) vec1 operator-     \
00165     (const vec2 &x, const vec3 &y);
00166   
00167   O2SCL_OP_VEC_VEC_SUB(o2scl::ovector,o2scl::ovector_view,o2scl::ovector_view)
00168     O2SCL_OP_VEC_VEC_SUB(o2scl::ovector,o2scl::ovector_view,
00169                          o2scl::uvector_view)
00170     O2SCL_OP_VEC_VEC_SUB(o2scl::ovector,o2scl::uvector_view,
00171                          o2scl::ovector_view)
00172     O2SCL_OP_VEC_VEC_SUB(o2scl::uvector,o2scl::uvector_view,
00173                          o2scl::uvector_view)
00174     O2SCL_OP_VEC_VEC_SUB(o2scl::ovector_cx,o2scl::ovector_cx_view,
00175                        o2scl::ovector_cx_view)
00176     O2SCL_OP_VEC_VEC_SUB(o2scl::ovector_cx,o2scl::ovector_cx_view,
00177                        o2scl::uvector_cx_view)
00178     O2SCL_OP_VEC_VEC_SUB(o2scl::ovector_cx,o2scl::uvector_cx_view,
00179                        o2scl::ovector_cx_view)
00180     O2SCL_OP_VEC_VEC_SUB(o2scl::uvector_cx,o2scl::uvector_cx_view,
00181                        o2scl::uvector_cx_view)
00182       
00183 #ifdef O2SCL_NEVER_DEFINED
00184     }
00185 {
00186 #endif
00187 
00188   /** 
00189       \brief The header macro for matrix-vector (right) multiplication
00190 
00191       Given types \c vec1, \c vec2, and \c mat, this macro
00192       provides the function declaration for adding two vectors
00193       using the form
00194       \code
00195       vec1 operator*(const mat &m, const vec3 &x);
00196       \endcode
00197 
00198       The corresponding definition is given in 
00199       \ref O2SCL_OPSRC_MAT_VEC_MULT.
00200 
00201       By default, the following operators are defined:
00202       \code
00203       ovector operator*(omatrix_view &x, ovector_view &y);
00204       ovector operator*(omatrix_view &x, uvector_view &y);
00205       ovector operator*(umatrix_view &x, ovector_view &y);
00206       uvector operator*(umatrix_view &x, uvector_view &y);
00207       \endcode
00208   */
00209 #define O2SCL_OP_MAT_VEC_MULT(vec1,vec2,mat) vec1 operator*     \
00210     (const mat &m, const vec2 &x);
00211   
00212   O2SCL_OP_MAT_VEC_MULT(o2scl::ovector,o2scl::ovector_view,o2scl::omatrix_view)
00213     O2SCL_OP_MAT_VEC_MULT(o2scl::ovector,o2scl::ovector_view,
00214                           o2scl::umatrix_view)
00215     O2SCL_OP_MAT_VEC_MULT(o2scl::ovector,o2scl::uvector_view,
00216                           o2scl::omatrix_view)
00217     O2SCL_OP_MAT_VEC_MULT(o2scl::uvector,o2scl::uvector_view,
00218                           o2scl::umatrix_view)
00219     
00220 #ifdef O2SCL_NEVER_DEFINED
00221     }
00222 {
00223 #endif
00224 
00225   /** 
00226       \brief The header macro for complex matrix-vector (right) 
00227       multiplication
00228 
00229       Given types \c vec1, \c vec2, and \c mat, this macro
00230       provides the function declaration for adding two vectors
00231       using the form
00232       \code
00233       vec1 operator*(const mat &m, const vec3 &x);
00234       \endcode
00235 
00236       The corresponding definition is given in 
00237       \ref O2SCL_OPSRC_CMAT_CVEC_MULT.
00238 
00239       By default, the following operators are defined:
00240       \code
00241       ovector_cx operator*(omatrix_cx_view &x, ovector_cx_view &y);
00242       ovector_cx operator*(omatrix_cx_view &x, uvector_cx_view &y);
00243       ovector_cx operator*(umatrix_cx_view &x, ovector_cx_view &y);
00244       uvector_cx operator*(umatrix_cx_view &x, uvector_cx_view &y);
00245       \endcode
00246   */
00247 #define O2SCL_OP_CMAT_CVEC_MULT(vec1,vec2,mat) vec1 operator*   \
00248     (const mat &m, const vec2 &x);
00249   
00250   O2SCL_OP_CMAT_CVEC_MULT(o2scl::ovector_cx,o2scl::ovector_cx_view,
00251                         o2scl::omatrix_cx_view)
00252     O2SCL_OP_CMAT_CVEC_MULT(o2scl::ovector_cx,o2scl::ovector_cx_view,
00253                           o2scl::umatrix_cx_view)
00254     O2SCL_OP_CMAT_CVEC_MULT(o2scl::ovector_cx,o2scl::uvector_cx_view,
00255                           o2scl::omatrix_cx_view)
00256     O2SCL_OP_CMAT_CVEC_MULT(o2scl::uvector_cx,o2scl::uvector_cx_view,
00257                           o2scl::umatrix_cx_view)
00258 
00259 #ifdef O2SCL_NEVER_DEFINED
00260     }
00261 {
00262 #endif
00263 
00264   /** 
00265       \brief The header macro for vector-matrix (left) 
00266       multiplication
00267 
00268       Given types \c vec1, \c vec2, and \c mat, this macro
00269       provides the function declaration for adding two vectors
00270       using the form
00271       \code
00272       vec1 operator*(const vec3 &x, const mat &m);
00273       \endcode
00274 
00275       The corresponding definition is given in 
00276       \ref O2SCL_OPSRC_VEC_MAT_MULT.
00277   */
00278 #define O2SCL_OP_VEC_MAT_MULT(vec1,vec2,mat) vec1 operator*     \
00279     (const vec2 &x, const mat &m);
00280   
00281   O2SCL_OP_VEC_MAT_MULT(o2scl::ovector,o2scl::ovector_view,o2scl::omatrix_view)
00282     O2SCL_OP_VEC_MAT_MULT(o2scl::ovector,o2scl::ovector_view,
00283                           o2scl::umatrix_view)
00284     O2SCL_OP_VEC_MAT_MULT(o2scl::ovector,o2scl::uvector_view,
00285                           o2scl::omatrix_view)
00286     O2SCL_OP_VEC_MAT_MULT(o2scl::uvector,o2scl::uvector_view,
00287                           o2scl::umatrix_view)
00288 
00289 #ifdef O2SCL_NEVER_DEFINED
00290     }
00291 {
00292 #endif
00293 
00294   /// The header macro for the \c trans_mult form of vector * matrix
00295 #define O2SCL_OP_TRANS_MULT(vec1,vec2,mat) vec1 trans_mult      \
00296     (const vec2 &x, const mat &m);
00297   
00298   O2SCL_OP_TRANS_MULT(o2scl::ovector,o2scl::ovector_view,o2scl::omatrix_view)
00299     O2SCL_OP_TRANS_MULT(o2scl::ovector,o2scl::ovector_view,o2scl::umatrix_view)
00300     O2SCL_OP_TRANS_MULT(o2scl::ovector,o2scl::uvector_view,o2scl::omatrix_view)
00301     O2SCL_OP_TRANS_MULT(o2scl::uvector,o2scl::uvector_view,o2scl::umatrix_view)
00302             
00303 #ifdef O2SCL_NEVER_DEFINED
00304     }
00305 {
00306 #endif
00307 
00308   /** 
00309       \brief The header macro for vector scalar (dot) product
00310 
00311       Given types \c vec1, \c vec2, and \c dtype, this macro
00312       provides the function declaration for adding two vectors
00313       using the form
00314       \code
00315       dtype operator*(const vec1 &x, const vec2 &y);
00316       \endcode
00317 
00318       The corresponding definition is given in 
00319       \ref O2SCL_OPSRC_DOT_PROD.
00320   */
00321 #define O2SCL_OP_DOT_PROD(dtype,vec1,vec2) dtype dot    \
00322     (const vec1 &x, const vec2 &y);
00323             
00324   O2SCL_OP_DOT_PROD(double,o2scl::ovector_view,o2scl::ovector_view)
00325     O2SCL_OP_DOT_PROD(double,o2scl::ovector_view,o2scl::uvector_view)
00326     O2SCL_OP_DOT_PROD(double,o2scl::uvector_view,o2scl::ovector_view)
00327     O2SCL_OP_DOT_PROD(double,o2scl::uvector_view,o2scl::uvector_view)
00328 
00329 #ifdef O2SCL_NEVER_DEFINED
00330     }
00331 {
00332 #endif
00333 
00334   /** 
00335       \brief The header macro for complex vector scalar (dot) 
00336       product
00337       
00338       Given types \c vec1, \c vec2, and \c dtype, this macro
00339       provides the function declaration for adding two vectors
00340       using the form
00341       \code
00342       dtype operator*(const vec1 &x, const vec2 &y);
00343       \endcode
00344 
00345       The corresponding definition is given in 
00346       \ref O2SCL_OPSRC_CX_DOT_PROD.
00347   */
00348 #define O2SCL_OP_CX_DOT_PROD(dtype,vec1,vec2) dtype dot \
00349     (const vec1 &x, const vec2 &y);
00350 
00351   O2SCL_OP_CX_DOT_PROD(gsl_complex,o2scl::ovector_cx_view,
00352                      o2scl::ovector_cx_view)
00353     O2SCL_OP_CX_DOT_PROD(gsl_complex,o2scl::ovector_cx_view,
00354                        o2scl::uvector_cx_view)
00355     O2SCL_OP_CX_DOT_PROD(gsl_complex,o2scl::uvector_cx_view,
00356                        o2scl::ovector_cx_view)
00357     O2SCL_OP_CX_DOT_PROD(gsl_complex,o2scl::uvector_cx_view,
00358                        o2scl::uvector_cx_view)
00359 
00360 #ifdef O2SCL_NEVER_DEFINED
00361     }
00362 {
00363 #endif
00364 
00365   /** 
00366       \brief The header macro for scalar-vector multiplication
00367       
00368       Given types \c vecv, \c vec, and \c dtype, this macro
00369       provides the function declaration for adding two vectors
00370       using the form
00371       \code
00372       vec operator*(const dtype &x, const vecv &y);
00373       \endcode
00374 
00375       The corresponding definition is given in 
00376       \ref O2SCL_OPSRC_SCA_VEC_MULT.
00377   */
00378 #define O2SCL_OP_SCA_VEC_MULT(dtype,vecv,vec) vec operator*     \
00379     (const dtype &x, const vecv &y);
00380   
00381   O2SCL_OP_SCA_VEC_MULT(double,o2scl::ovector_view,o2scl::ovector)
00382     O2SCL_OP_SCA_VEC_MULT(double,o2scl::uvector_view,o2scl::uvector)
00383     
00384 #ifdef O2SCL_NEVER_DEFINED
00385     }
00386 {
00387 #endif
00388 
00389   /** 
00390       \brief The header macro for vector-scalar multiplication
00391       
00392       Given types \c vecv, \c vec, and \c dtype, this macro
00393       provides the function declaration for adding two vectors
00394       using the form
00395       \code
00396       vec operator*(const vecv &x, const dtype &y);
00397       \endcode
00398 
00399       The corresponding definition is given in 
00400       \ref O2SCL_OPSRC_VEC_SCA_MULT.
00401   */
00402 #define O2SCL_OP_VEC_SCA_MULT(dtype,vecv,vec) vec operator*     \
00403     (const vecv &x, const dtype &y);
00404   
00405   O2SCL_OP_VEC_SCA_MULT(double,o2scl::ovector_view,o2scl::ovector)
00406     O2SCL_OP_VEC_SCA_MULT(double,o2scl::uvector_view,o2scl::uvector)
00407 
00408 #ifdef O2SCL_NEVER_DEFINED
00409     }
00410 {
00411 #endif
00412               
00413   /** 
00414       \brief The header macro for pairwise vector * vector (where either 
00415       vector can be real or complex)
00416       
00417       Given types \c vec1, \c vec2, and \c vec3, this macro
00418       provides the function declaration for adding two vectors
00419       using the form
00420       \code
00421       vec1 pair_prod(const vec2 &x, const vec3 &y);
00422       \endcode
00423 
00424       The corresponding definition is given in 
00425       \ref O2SCL_OPSRC_VEC_VEC_PRO.
00426   */
00427 #define O2SCL_OP_VEC_VEC_PRO(vec1,vec2,vec3) vec1 pair_prod     \
00428     (const vec2 &x, const vec3 &y);
00429   
00430   O2SCL_OP_VEC_VEC_PRO(o2scl::ovector,o2scl::ovector_view,o2scl::ovector_view)
00431     O2SCL_OP_VEC_VEC_PRO(o2scl::ovector,o2scl::ovector_view,
00432                          o2scl::uvector_view)
00433     O2SCL_OP_VEC_VEC_PRO(o2scl::ovector,o2scl::uvector_view,
00434                          o2scl::ovector_view)
00435     O2SCL_OP_VEC_VEC_PRO(o2scl::uvector,o2scl::uvector_view,
00436                          o2scl::uvector_view)
00437     O2SCL_OP_VEC_VEC_PRO(o2scl::ovector_cx,o2scl::ovector_cx_view,
00438                          o2scl::ovector_view)
00439     O2SCL_OP_VEC_VEC_PRO(o2scl::ovector_cx,o2scl::ovector_cx_view,
00440                          o2scl::uvector_view)
00441     O2SCL_OP_VEC_VEC_PRO(o2scl::ovector_cx,o2scl::uvector_cx_view,
00442                          o2scl::ovector_view)
00443     O2SCL_OP_VEC_VEC_PRO(o2scl::uvector_cx,o2scl::uvector_cx_view,
00444                          o2scl::uvector_view)
00445     O2SCL_OP_VEC_VEC_PRO(o2scl::ovector_cx,o2scl::ovector_view,
00446                          o2scl::ovector_cx_view)
00447     O2SCL_OP_VEC_VEC_PRO(o2scl::ovector_cx,o2scl::ovector_view,
00448                          o2scl::uvector_cx_view)
00449     O2SCL_OP_VEC_VEC_PRO(o2scl::ovector_cx,o2scl::uvector_view,
00450                          o2scl::ovector_cx_view)
00451     O2SCL_OP_VEC_VEC_PRO(o2scl::uvector_cx,o2scl::uvector_view,
00452                          o2scl::uvector_cx_view)
00453     O2SCL_OP_VEC_VEC_PRO(o2scl::ovector_cx,o2scl::ovector_cx_view,
00454                        o2scl::ovector_cx_view)
00455     O2SCL_OP_VEC_VEC_PRO(o2scl::ovector_cx,o2scl::ovector_cx_view,
00456                        o2scl::uvector_cx_view)
00457     O2SCL_OP_VEC_VEC_PRO(o2scl::ovector_cx,o2scl::uvector_cx_view,
00458                        o2scl::ovector_cx_view)
00459     O2SCL_OP_VEC_VEC_PRO(o2scl::uvector_cx,o2scl::uvector_cx_view,
00460                        o2scl::uvector_cx_view)
00461     
00462     /** 
00463         \brief The source code macro for vector-vector addition
00464 
00465         This define macro generates the function definition. See
00466         the function declaration \ref O2SCL_OP_VEC_VEC_ADD
00467     */
00468 #define O2SCL_OPSRC_VEC_VEC_ADD(vec1,vec2,vec3) vec1 o2scl_arith::operator+\
00469       (const vec2 &x, const vec3 &y) {                                  \
00470       size_t m=x.size();                                                \
00471       if (y.size()<m) m=y.size();                                       \
00472       vec1 r(m);                                                        \
00473       for(size_t i=0;i<m;i++) {                                         \
00474         r[i]=x[i]+y[i];                                                 \
00475       }                                                                 \
00476       return r;                                                         \
00477     }
00478     
00479     /** 
00480         \brief The source code macro for vector-vector subtraction
00481 
00482         This define macro generates the function definition. See
00483         the function declaration \ref O2SCL_OP_VEC_VEC_SUB
00484     */
00485 #define O2SCL_OPSRC_VEC_VEC_SUB(vec1,vec2,vec3) vec1 o2scl_arith::operator-\
00486       (const vec2 &x, const vec3 &y) {                                  \
00487       size_t m=x.size();                                                \
00488       if (y.size()<m) m=y.size();                                       \
00489       vec1 r(m);                                                        \
00490       for(size_t i=0;i<m;i++) {                                         \
00491         r[i]=x[i]-y[i];                                                 \
00492       }                                                                 \
00493       return r;                                                         \
00494     }
00495 
00496     /** 
00497         \brief The source code macro for matrix * vector
00498 
00499         This define macro generates the function definition. See
00500         the function declaration \ref O2SCL_OP_MAT_VEC_MULT
00501     */
00502 #define O2SCL_OPSRC_MAT_VEC_MULT(vec1,vec2,mat) vec1 o2scl_arith::operator*\
00503       (const mat &m, const vec2 &x) {                                   \
00504       size_t nr=m.rows();                                               \
00505       size_t nc=m.cols();                                               \
00506       vec1 res(nr);                                                     \
00507       for(size_t i=0;i<nr;i++) {                                        \
00508         double r=0.0;                                                   \
00509         for(size_t j=0;j<nc;j++) {                                      \
00510           r+=m[i][j]*x[j];                                              \
00511         }                                                               \
00512         res[i]=r;                                                       \
00513       }                                                                 \
00514       return res;                                                       \
00515     } 
00516   
00517     /** 
00518         \brief The source code macro for complex matrix * complex vector
00519 
00520         This define macro generates the function definition. See
00521         the function declaration \ref O2SCL_OP_CMAT_CVEC_MULT
00522     */
00523 #define O2SCL_OPSRC_CMAT_CVEC_MULT(vec1,vec2,mat) vec1 o2scl_arith::operator* \
00524       (const mat &m, const vec2 &x) {                                   \
00525       size_t nr=m.rows();                                               \
00526       size_t nc=m.cols();                                               \
00527       vec1 res(nr);                                                     \
00528       for(size_t i=0;i<nr;i++) {                                        \
00529         double re=0.0;                                                  \
00530         double im=0.0;                                                  \
00531         for(size_t j=0;j<nc;j++) {                                      \
00532           gsl_complex g=m[i][j]*x[j];                                   \
00533           re+=g.dat[0];                                                 \
00534           im+=g.dat[1];                                                 \
00535         }                                                               \
00536         res[i].dat[0]=re;                                               \
00537         res[i].dat[1]=im;                                               \
00538       }                                                                 \
00539       return res;                                                       \
00540     } 
00541   
00542     /** 
00543         \brief The source code macro for the operator form of vector * matrix
00544 
00545         This define macro generates the function definition. See
00546         the function declaration \ref O2SCL_OP_VEC_MAT_MULT
00547     */
00548 #define O2SCL_OPSRC_VEC_MAT_MULT(vec1,vec2,mat) vec1 o2scl_arith::operator*\
00549       (const vec2 &x, const mat &m) {                                   \
00550       size_t nr=m.rows();                                               \
00551       size_t nc=m.cols();                                               \
00552       vec1 res(nr);                                                     \
00553       for(size_t j=0;j<nc;j++) {                                        \
00554         double r=0.0;                                                   \
00555         for(size_t i=0;i<nr;i++) {                                      \
00556           r+=x[i]*m[i][j];                                              \
00557         }                                                               \
00558         res[j]=r;                                                       \
00559       }                                                                 \
00560       return res;                                                       \
00561     } 
00562   
00563     /** 
00564         \brief The source code macro for the \c trans_mult form of 
00565         vector * matrix
00566 
00567         This define macro generates the function definition. See
00568         the function declaration \ref O2SCL_OP_TRANS_MULT
00569     */
00570 #define O2SCL_OPSRC_TRANS_MULT(vec1,vec2,mat) vec1 o2scl_arith::trans_mult\
00571       (const vec2 &x, const mat &m) {                                   \
00572       size_t nr=m.rows();                                               \
00573       size_t nc=m.cols();                                               \
00574       vec1 res(nr);                                                     \
00575       for(size_t j=0;j<nc;j++) {                                        \
00576         double r=0.0;                                                   \
00577         for(size_t i=0;i<nr;i++) {                                      \
00578           r+=x[i]*m[i][j];                                              \
00579         }                                                               \
00580         res[j]=r;                                                       \
00581       }                                                                 \
00582       return res;                                                       \
00583     } 
00584   
00585     /** 
00586         \brief The source code macro for a vector dot product
00587 
00588         This define macro generates the function definition. See
00589         the function declaration \ref O2SCL_OP_DOT_PROD
00590     */
00591 #define O2SCL_OPSRC_DOT_PROD(dtype,vec1,vec2) dtype o2scl_arith::dot    \
00592       (const vec1 &x, const vec2 &y) {                                  \
00593       size_t m=x.size();                                                \
00594       if (y.size()<m) m=y.size();                                       \
00595       dtype r=0;                                                        \
00596       for(size_t i=0;i<m;i++) {                                         \
00597         r+=x[i]*y[i];                                                   \
00598       }                                                                 \
00599       return r;                                                         \
00600     }
00601             
00602     /** 
00603         \brief The source code macro for a complex vector dot product
00604 
00605         This define macro generates the function definition. See
00606         the function declaration \ref O2SCL_OP_CX_DOT_PROD
00607     */
00608 #define O2SCL_OPSRC_CX_DOT_PROD(dtype,vec1,vec2) dtype o2scl_arith::dot \
00609       (const vec1 &x, const vec2 &y) {                                  \
00610       size_t m=x.size();                                                \
00611       if (y.size()<m) m=y.size();                                       \
00612       dtype r={{0.0,0.0}};                                              \
00613       for(size_t i=0;i<m;i++) {                                         \
00614         r+=x[i]*y[i];                                                   \
00615       }                                                                 \
00616       return r;                                                         \
00617     }
00618 
00619     /** 
00620         \brief The source code macro for vector=scalar*vector
00621 
00622         This define macro generates the function definition. See
00623         the function declaration \ref O2SCL_OP_SCA_VEC_MULT
00624     */
00625 #define O2SCL_OPSRC_SCA_VEC_MULT(dtype,vecv,vec) vec o2scl_arith::operator*\
00626       (const dtype &x, const vecv &y) {                                 \
00627       size_t m=y.size();                                                \
00628       vec r(m);                                                         \
00629       for(size_t i=0;i<m;i++) {                                         \
00630         r[i]=x*y[i];                                                    \
00631       }                                                                 \
00632       return r;                                                         \
00633     }
00634   
00635     /** 
00636         \brief The source code macro for vector=vector*scalar
00637 
00638         This define macro generates the function definition. See
00639         the function declaration \ref O2SCL_OP_VEC_SCA_MULT
00640     */
00641 #define O2SCL_OPSRC_VEC_SCA_MULT(dtype,vecv,vec) vec o2scl_arith::operator*\
00642       (const vecv &x, const dtype &y) {                                 \
00643       size_t m=x.size();                                                \
00644       vec r(m);                                                         \
00645       for(size_t i=0;i<m;i++) {                                         \
00646         r[i]=x[i]*y;                                                    \
00647       }                                                                 \
00648       return r;                                                         \
00649     }
00650     
00651     /** 
00652         \brief The source code macro for pairwise vector * vector (where
00653         either vector can be real or complex)
00654         
00655         This define macro generates the function definition. See
00656         the function declaration \ref O2SCL_OP_VEC_VEC_PRO
00657   */
00658 #define O2SCL_OPSRC_VEC_VEC_PRO(vec1,vec2,vec3) vec1 o2scl_arith::pair_prod \
00659       (const vec2 &x, const vec3 &y) {                                  \
00660       size_t m=x.size();                                                \
00661       if (y.size()<m) m=y.size();                                       \
00662       vec1 r(m);                                                        \
00663       for(size_t i=0;i<m;i++) {                                         \
00664         r[i]=x[i]*y[i];                                                 \
00665       }                                                                 \
00666       return r;                                                         \
00667     }
00668     
00669     }
00670 
00671 #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