CLHEP/Matrix/Vector.h

00001 // -*- C++ -*-
00002 // CLASSDOC OFF
00003 // $Id: Vector.h,v 1.17 2003/12/30 15:04:25 pfeiffer Exp $
00004 // ---------------------------------------------------------------------------
00005 // CLASSDOC ON
00006 //
00007 // This file is a part of the CLHEP - a Class Library for High Energy Physics.
00008 // 
00009 // 
00010 // Copyright Cornell University 1993, 1996, All Rights Reserved.
00011 // 
00012 // This software written by Nobu Katayama and Mike Smyth, Cornell University.
00013 // 
00014 // Redistribution and use in source and binary forms, with or without
00015 // modification, are permitted provided that the following conditions
00016 // are met:
00017 // 1. Redistributions of source code must retain the above copyright
00018 //    notice and author attribution, this list of conditions and the
00019 //    following disclaimer. 
00020 // 2. Redistributions in binary form must reproduce the above copyright
00021 //    notice and author attribution, this list of conditions and the
00022 //    following disclaimer in the documentation and/or other materials
00023 //    provided with the distribution.
00024 // 3. Neither the name of the University nor the names of its contributors
00025 //    may be used to endorse or promote products derived from this software
00026 //    without specific prior written permission.
00027 // 
00028 // Creation of derivative forms of this software for commercial
00029 // utilization may be subject to restriction; written permission may be
00030 // obtained from Cornell University.
00031 // 
00032 // CORNELL MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.  By way
00033 // of example, but not limitation, CORNELL MAKES NO REPRESENTATIONS OR
00034 // WARRANTIES OF MERCANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT
00035 // THE USE OF THIS SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS,
00036 // COPYRIGHTS, TRADEMARKS, OR OTHER RIGHTS.  Cornell University shall not be
00037 // held liable for any liability with respect to any claim by the user or any
00038 // other party arising from use of the program.
00039 //
00040 //   Although Vector and Matrix class are very much related, I like the typing
00041 //   information I get by making them different types. It is usually an error
00042 //   to use a Matrix where a Vector is expected, except in the case that the
00043 //   Matrix is a single column.  But this case can be taken care of by using
00044 //   constructors as conversions.  For this same reason, I don't want to make
00045 //   a Vector a derived class of Matrix.
00046 //
00047 
00048 #ifndef _Vector_H_
00049 #define _Vector_H_
00050 
00051 #ifdef GNUPRAGMA
00052 #pragma interface
00053 #endif
00054 
00055 #include "CLHEP/Matrix/GenMatrix.h"
00056 #ifdef HEP_USE_RANDOM
00057 class HepRandom;
00058 #endif
00059 
00060 class HepMatrix;
00061 class HepSymMatrix;
00062 class HepDiagMatrix;
00063 #ifdef HEP_USE_VECTOR_MODULE
00064 class Hep3Vector;
00065 #endif
00066 
00071 class HepVector : public HepGenMatrix {
00072 public:
00073    inline HepVector();
00074    // Default constructor. Gives vector of length 0.
00075    // Another Vector can be assigned to it.
00076 
00077    explicit HepVector(int p);
00078    HepVector(int p, int);
00079    // Constructor. Gives vector of length p.
00080 
00081 #ifdef HEP_USE_RANDOM
00082    HepVector(int p, HepRandom &r);
00083 #endif
00084 
00085    HepVector(const HepVector &v);
00086    HepVector(const HepMatrix &m);
00087    // Copy constructors.
00088    // Note that there is an assignment operator for v = Hep3Vector.
00089 
00090    virtual ~HepVector();
00091    // Destructor.
00092 
00093    inline const double & operator()(int row) const;
00094    inline double & operator()(int row);
00095    // Read or write a matrix element. 
00096    // ** Note that the indexing starts from (1). **
00097    
00098    inline const double & operator[](int row) const;
00099    inline double & operator[](int row);
00100    // Read and write an element of a Vector.
00101    // ** Note that the indexing starts from [0]. **
00102 
00103    inline virtual const double & operator()(int row, int col) const;
00104    inline virtual double & operator()(int row, int col);
00105    // Read or write a matrix element. 
00106    // ** Note that the indexing starts from (1,1). **
00107    // Allows accessing Vector using GenMatrix
00108 
00109    HepVector & operator*=(double t);
00110    // Multiply a Vector by a floating number. 
00111 
00112    HepVector & operator/=(double t); 
00113    // Divide a Vector by a floating number.
00114 
00115    HepVector & operator+=( const HepMatrix &v2);
00116    HepVector & operator+=( const HepVector &v2);
00117    HepVector & operator-=( const HepMatrix &v2);
00118    HepVector & operator-=( const HepVector &v2);
00119    // Add or subtract a Vector.
00120 
00121    HepVector & operator=( const HepVector &m2);
00122    // Assignment operators.
00123 
00124    HepVector& operator=(const HepMatrix &);
00125 #ifdef HEP_USE_VECTOR_MODULE
00126    HepVector& operator=(const Hep3Vector &);
00127 #endif
00128    // assignment operators from other classes.
00129 
00130    HepVector operator- () const;
00131    // unary minus, ie. flip the sign of each element.
00132 
00133    HepVector apply(double (*f)(double, int)) const;
00134    // Apply a function to all elements.
00135 
00136    HepVector sub(int min_row, int max_row) const;
00137    // Returns a sub vector.
00138 #ifdef HEP_CC_NEED_SUB_WITHOUT_CONST
00139    HepVector sub(int min_row, int max_row);
00140    // SGI CC bug. I have to have both with/without const. I should not need
00141    // one without const.
00142 #endif
00143 
00144    void sub(int row, const HepVector &v1);
00145    // Replaces a sub vector of a Vector with v1.
00146 
00147    inline double normsq() const;
00148    // Returns norm squared.
00149 
00150    inline double norm() const;
00151    // Returns norm.
00152 
00153    inline virtual int num_row() const;
00154    // Returns number of rows.
00155 
00156    inline virtual int num_col() const;
00157    // Number of columns. Always returns 1. Provided for compatibility with
00158    // GenMatrix. 
00159 
00160    HepMatrix T() const;
00161    // Returns the transpose of a Vector. Note that the returning type is
00162    // Matrix.
00163 
00164    friend inline void swap(HepVector &v1, HepVector &v2);
00165    // Swaps two vectors.
00166 
00167 protected:
00168    virtual inline int num_size() const;
00169 
00170 private:
00171    virtual void invert(int&);
00172    // produces an error. Demanded by GenMatrix
00173 
00174    friend class HepDiagMatrix;
00175    friend class HepSymMatrix;
00176    friend class HepMatrix;
00177    // friend classes
00178 
00179    friend double dot(const HepVector &v1, const HepVector &v2);
00180    // f = v1 * v2;
00181 
00182    friend HepVector operator+(const HepVector &v1, const HepVector &v2);
00183    friend HepVector operator-(const HepVector &v1, const HepVector &v2);
00184    friend HepVector operator*(const HepSymMatrix &m1, const HepVector &m2);
00185    friend HepVector operator*(const HepDiagMatrix &m1, const HepVector &m2);
00186    friend HepMatrix operator*(const HepVector &m1, const HepMatrix &m2);
00187    friend HepVector operator*(const HepMatrix &m1, const HepVector &m2);
00188 
00189    friend HepVector solve(const HepMatrix &a, const HepVector &v);
00190    friend void tridiagonal(HepSymMatrix *a,HepMatrix *hsm);
00191    friend void row_house(HepMatrix *,const HepMatrix &, double, int, int,
00192                          int, int);
00193    friend void row_house(HepMatrix *,const HepVector &, double, int, int);
00194    friend void back_solve(const HepMatrix &R, HepVector *b);
00195    friend void col_house(HepMatrix *,const HepMatrix &,double, int, int,
00196                          int, int);
00197    friend HepVector house(const HepSymMatrix &a,int row,int col);
00198    friend HepVector house(const HepMatrix &a,int row,int col);
00199    friend void house_with_update(HepMatrix *a,int row,int col);
00200    friend HepSymMatrix vT_times_v(const HepVector &v);
00201    friend HepVector qr_solve(HepMatrix *, const HepVector &);
00202 
00203    std::vector<double,Alloc<double,25> > m;
00204    int nrow;
00205 };
00206 
00207 //
00208 // Operations other than member functions
00209 //
00210 
00211 HepStd::ostream& operator<<(HepStd::ostream &s, const HepVector &v);
00212 // Write out Matrix, SymMatrix, DiagMatrix and Vector into ostream.
00213 
00214 HepVector operator*(const HepMatrix &m1, const HepVector &m2);
00215 HepVector operator*(double t, const HepVector &v1);
00216 HepVector operator*(const HepVector &v1, double t);
00217 // Multiplication operators.
00218 // Note that m *= x is always faster than m = m * x.
00219 
00220 HepVector operator/(const HepVector &v1, double t);
00221 // Divide by a real number.
00222 
00223 HepVector operator+(const HepMatrix &m1, const HepVector &v2);
00224 HepVector operator+(const HepVector &v1, const HepMatrix &m2);
00225 HepVector operator+(const HepVector &v1, const HepVector &v2);
00226 // Addition operators
00227 
00228 HepVector operator-(const HepMatrix &m1, const HepVector &v2);
00229 HepVector operator-(const HepVector &v1, const HepMatrix &m2);
00230 HepVector operator-(const HepVector &v1, const HepVector &v2);
00231 // subtraction operators
00232 
00233 HepVector dsum(const HepVector &s1, const HepVector &s2);
00234 // Direct sum of two vectors;
00235 
00236 #ifdef HEP_SHORT_NAMES
00237 typedef HepVector Vector;
00238 #endif
00239 
00240 #include "CLHEP/Matrix/Vector.icc"
00241 
00242 #endif 

Class Library for High Energy Physics (version 1.8)