Lapack++
gmd.h
Go to the documentation of this file.
00001 // -*-C++-*-
00002 
00003 // Copyright (C) 2004
00004 // Christian Stimming <stimming@tuhh.de>
00005 //
00006 // Row-order modifications by Jacob (Jack) Gryn <jgryn at cs dot yorku dot ca>
00007 
00008 // This library is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU Lesser General Public License as
00010 // published by the Free Software Foundation; either version 2, or (at
00011 // your option) any later version.
00012 
00013 // This library 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 Lesser General Public License for more details.
00017 
00018 // You should have received a copy of the GNU Lesser General Public License along
00019 // with this library; see the file COPYING.  If not, write to the Free
00020 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00021 // USA.
00022 
00027 //      LAPACK++ (V. 1.0a Beta)
00028 //      (C) 1992-1996 All Rights Reserved.
00029 //
00030 //      Lapack++ Rectangular Matrix Class
00031 //
00032 //      Dense (nonsingular) matrix, assumes no special structure or properties.
00033 //
00034 //      ) allows 2-d indexing
00035 //      ) non-unit strides
00036 //      ) inject assignment
00037 //      ) std::cout << A.info()  prints out internal states of A
00038 //      ) indexing via A(i,j) where i,j are either integers or
00039 //              LaIndex
00040 
00041 #ifndef _LA_GEN_MAT_DOUBLE_H_
00042 #define _LA_GEN_MAT_DOUBLE_H_
00043 
00044 #include "arch.h"
00045 #include "lafnames.h"
00046 #include VECTOR_DOUBLE_H
00047 #include LA_INDEX_H
00048 #include LA_GEN_MAT_FLOAT_H
00049 
00050 class LaGenMatComplex;
00051 class LaGenMatDouble;
00052 class LaGenMatFloat;
00053 class LaGenMatInt;
00054 class LaGenMatLongInt;
00055 
00078 class DLLIMPORT LaGenMatDouble
00079 {
00080 public:
00082     typedef double value_type;
00086     typedef LaGenMatDouble matrix_type;
00089     typedef VectorDouble vec_type;
00090 private:
00091     vec_type     v;
00092     LaIndex           ii[2];
00093     int             dim[2];  // size of original matrix, not submatrix
00094     int             sz[2];   // size of this submatrix
00095     void init(int m, int n);
00096     static int  debug_; // trace all entry and exits into methods and
00097     // operators of this class.  This variable is
00098     // explicitly initalized in gmd.cc
00099 
00100     static int      *info_;   // print matrix info only, not values
00101     //   originally 0, set to 1, and then
00102     //   reset to 0 after use.
00103     // use as in
00104     //
00105     //    std::cout << B.info() << std::endl;
00106     //
00107     // this *info_ member is unique in that it really isn't
00108     // part of the matrix info, just a flag as to how
00109     // to print it.   We've included in this beta release
00110     // as part of our testing, but we do not expect it
00111     // to be user accessable.
00112     // It has to be declared as global static
00113     // so that we may monitor expresssions like
00114     // X::(const &X) and still utilize without violating
00115     // the "const" condition.
00116     // Because this *info_ is used at most one at a time,
00117     // there is no harm in keeping only one copy of it,
00118     // also, we do not need to malloc free space every time
00119     // we call a matrix constructor.
00120 
00121 
00122     int shallow_; // set flag to '0' in order to return matrices
00123     // by value from functions without unecessary
00124     // copying.
00125 
00126 
00127     // users shouldn't be able to modify assignment semantics..
00128     //
00129     //LaGenMatDouble& shallow_assign();
00130 
00131 public:
00132 
00135     /*::::::::::::::::::::::::::*/
00136     /* Constructors/Destructors */
00137     /*::::::::::::::::::::::::::*/
00138 
00140     LaGenMatDouble();
00141 
00144     LaGenMatDouble(int m, int n);
00145 
00182     LaGenMatDouble(double*v, int m, int n, bool row_ordering = false);
00183 
00206     LaGenMatDouble(const LaGenMatDouble&);
00207 
00210     explicit LaGenMatDouble(const LaGenMatFloat&);
00211 
00215     LaGenMatDouble& resize(int m, int n);
00216 
00220     LaGenMatDouble& resize(const LaGenMatDouble& s);
00221 
00224     virtual ~LaGenMatDouble();
00226 
00227 
00232     bool is_zero() const;
00233 
00236     bool is_submatrixview() const
00237     {
00238         return size(0) != gdim(0) || size(1) != gdim(1);
00239     };
00240 
00245     bool has_unitstride() const
00246     {
00247         return inc(0) == 1 && inc(1) == 1;
00248     };
00249 
00252     bool equal_to(const LaGenMatDouble& mat) const;
00254 
00255 
00258     /*::::::::::::::::::::::::::::::::*/
00259     /*  Indices and access operations */
00260     /*::::::::::::::::::::::::::::::::*/
00261 
00264     inline int size(int d) const;   // submatrix size
00267     inline int cols() const
00268     {
00269         return size(1);
00270     }
00273     inline int rows() const
00274     {
00275         return size(0);
00276     }
00277 
00283     inline int inc(int d) const;    // explicit increment
00284 
00289     inline int gdim(int d) const;   // global dimensions
00290 
00295     inline int start(int d) const;  // return ii[d].start()
00296 
00301     inline int end(int d) const;    // return ii[d].end()
00302 
00307     inline LaIndex index(int d) const;// index
00308 
00312     inline int ref_count() const;
00313 
00316     inline double* addr() const;       // begining addr of data space
00318 
00335     inline double& operator()(int i, int j);
00336 
00350     inline const double& operator()(int i, int j) const;
00351 
00363     LaGenMatDouble operator()(const LaIndex& I, const LaIndex& J) ;
00364 
00376     LaGenMatDouble operator()(const LaIndex& I, const LaIndex& J) const;
00377 
00384     LaGenMatDouble row(int k);
00391     LaGenMatDouble row(int k) const;
00398     LaGenMatDouble col(int k);
00405     LaGenMatDouble col(int k) const;
00407 
00414     LaGenMatDouble& operator=(double s);
00415 
00451     LaGenMatDouble& operator=(const LaGenMatDouble& s);
00452 
00461     LaGenMatDouble& operator+=(double s);
00462 
00467     LaGenMatDouble& add(double s);
00468 
00473     LaGenMatDouble& operator*=(double s);
00474 
00479     LaGenMatDouble& scale(double s);
00480 
00489     LaGenMatDouble& inject(const LaGenMatDouble& s);
00490 
00495     LaGenMatDouble& copy(const LaGenMatDouble& s);
00496 
00501     LaGenMatDouble copy() const;
00502 
00507     inline LaGenMatDouble& shallow_assign();
00508 
00516     LaGenMatDouble& ref(const LaGenMatDouble& s);
00518 
00524     LaGenMatDouble repmat (int M, int N) const;
00527     value_type trace () const;
00531     LaGenMatDouble diag () const;
00533 
00537     inline int shallow() const      // read global shallow flag
00538     {
00539         return shallow_;
00540     }
00542     inline int debug() const;       // read global debug flag
00544     inline int debug(int d);        // set global debug flag
00556     inline const LaGenMatDouble& info() const
00557     {
00558         *(const_cast<LaGenMatDouble*>(this)->info_) = 1;
00559         return *this;
00560     };
00561 
00564     inline std::ostream& Info(std::ostream& s) const
00565     {
00566         s << "Size: (" << size(0) << "x" << size(1) << ") " ;
00567         s << "Indeces: " << ii[0] << " " << ii[1];
00568         s << "#ref: " << ref_count()
00569           << "addr: " << addr() << " shallow:" << shallow_ << std::endl;
00570         return s;
00571     };
00573 
00580     friend DLLIMPORT std::ostream& operator<<(std::ostream&, const LaGenMatDouble&);
00581 
00585     LaGenMatComplex to_LaGenMatComplex() const;
00587     LaGenMatFloat to_LaGenMatFloat() const;
00589     LaGenMatInt to_LaGenMatInt() const;
00591     LaGenMatLongInt to_LaGenMatLongInt() const;
00593 
00594 
00600     static LaGenMatDouble zeros (int N, int M = 0);
00604     static LaGenMatDouble ones (int N, int M = 0);
00608     static LaGenMatDouble eye (int N, int M = 0);
00617     static LaGenMatDouble rand (int N, int M,
00618                                 value_type low = 0, value_type high = 1);
00622     static LaGenMatDouble from_diag (const LaGenMatDouble &vect);
00626     static LaGenMatDouble linspace (value_type start, value_type end,
00627                                     int nr_points);
00629 
00630 };  //* End of LaGenMatDouble Class *//
00631 
00632 namespace la
00633 {
00635 typedef LaGenMatDouble mat;
00636 } // namespace
00637 
00645 DLLIMPORT
00646 std::ostream& operator<<(std::ostream&, const LaGenMatDouble&);
00647 
00648 
00649 //* Member Functions *//
00650 
00651 inline int LaGenMatDouble::size(int d) const
00652 {
00653     return sz[d];
00654 }
00655 
00656 inline int LaGenMatDouble::inc(int d) const
00657 {
00658     return ii[d].inc();
00659 }
00660 
00661 inline int LaGenMatDouble::gdim(int d) const
00662 {
00663     return dim[d];
00664 }
00665 
00666 inline int LaGenMatDouble::start(int d) const
00667 {
00668     return ii[d].start();
00669 }
00670 
00671 inline int LaGenMatDouble::end(int d) const
00672 {
00673     return ii[d].end();
00674 }
00675 
00676 inline int LaGenMatDouble::ref_count() const
00677 {
00678     return v.ref_count();
00679 }
00680 
00681 
00682 inline LaIndex LaGenMatDouble::index(int d)  const
00683 {
00684     return ii[d];
00685 }
00686 
00687 inline double* LaGenMatDouble::addr() const
00688 {
00689     return  v.addr();
00690 }
00691 
00692 inline int LaGenMatDouble::debug() const
00693 {
00694     return debug_;
00695 }
00696 
00697 inline int LaGenMatDouble::debug(int d)
00698 {
00699     return debug_ = d;
00700 }
00701 
00702 inline double& LaGenMatDouble::operator()(int i, int j)
00703 {
00704 #ifdef LA_BOUNDS_CHECK
00705     assert(i >= 0);
00706     assert(i < size(0));
00707     assert(j >= 0);
00708     assert(j < size(1));
00709 #endif
00710     return v( dim[0] * (ii[1].start() + j * ii[1].inc()) +
00711               ii[0].start() + i * ii[0].inc());
00712 }
00713 
00714 inline const double& LaGenMatDouble::operator()(int i, int j) const
00715 {
00716 
00717 #ifdef LA_BOUNDS_CHECK
00718     assert(i >= 0);
00719     assert(i < size(0));
00720     assert(j >= 0);
00721     assert(j < size(1));
00722 #endif
00723 
00724     return v( dim[0] * (ii[1].start() + j * ii[1].inc()) +
00725               ii[0].start() + i * ii[0].inc());
00726 }
00727 
00728 inline  LaGenMatDouble&  LaGenMatDouble::shallow_assign()
00729 {
00730     shallow_ = 1;
00731     return *this;
00732 }
00733 
00734 
00735 #endif
00736 // _LA_GEN_MAT_H_