Lapack++
gmi.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.1)
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 //      ) deep (copy) 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_INT_H_
00042 #define _LA_GEN_MAT_INT_H_
00043 
00044 #include "arch.h"
00045 #include "lafnames.h"
00046 #include VECTOR_INT_H
00047 #include LA_INDEX_H
00048 
00049 class LaGenMatComplex;
00050 class LaGenMatDouble;
00051 class LaGenMatFloat;
00052 class LaGenMatInt;
00053 class LaGenMatLongInt;
00054 
00055 
00071 class DLLIMPORT LaGenMatInt
00072 {
00073 public:
00075     typedef int value_type;
00079     typedef LaGenMatInt matrix_type;
00082     typedef VectorInt vec_type;
00083 private:
00084     vec_type     v;
00085     LaIndex           ii[2];
00086     int             dim[2];  // size of original matrix, not submatrix
00087     int             sz[2];   // size of this submatrix
00088     void init(int m, int n);
00089     static int  debug_; // trace all entry and exits into methods and
00090     // operators of this class.  This variable is
00091     // explicitly initalized in lagenmatint.cc
00092 
00093     static int      *info_;   // print matrix info only, not values
00094     //   originally 0, set to 1, and then
00095     //   reset to 0 after use.
00096     // use as in
00097     //
00098     //    std::cout << B.info() << std::endl;
00099     //
00100     // this *info_ member is unique in that it really isn't
00101     // part of the matrix info, just a flag as to how
00102     // to print it.   We've included in this beta release
00103     // as part of our testing, but we do not expect it
00104     // to be user accessable.
00105     // It has to be declared as global static
00106     // so that we may monitor expresssions like
00107     // X::(const &X) and still utilize without violating
00108     // the "const" condition.
00109     // Because this *info_ is used at most one at a time,
00110     // there is no harm in keeping only one copy of it,
00111     // also, we do not need to malloc free space every time
00112     // we call a matrix constructor.
00113 
00114 
00115     int shallow_; // set flag to '0' in order to return matrices
00116     // by value from functions without unecessary
00117     // copying.
00118 
00119 
00120     // users shouldn't be able to modify assignment semantics..
00121     //
00122     //LaGenMatInt& shallow_assign();
00123 
00124 public:
00125 
00126 
00129     /*::::::::::::::::::::::::::*/
00130     /* Constructors/Destructors */
00131     /*::::::::::::::::::::::::::*/
00132 
00134     LaGenMatInt();
00135 
00138     LaGenMatInt(int m, int n);
00139 
00176     LaGenMatInt(int* v, int m, int n, bool row_ordering = false);
00177 
00200     LaGenMatInt(const LaGenMatInt&);
00201 
00205     LaGenMatInt& resize(int m, int n);
00206 
00210     LaGenMatInt& resize(const LaGenMatInt& s);
00211 
00214     virtual ~LaGenMatInt();
00215 
00216 
00221     bool is_zero() const;
00222 
00225     bool is_submatrixview() const
00226     {
00227         return size(0) != gdim(0) || size(1) != gdim(1);
00228     };
00229 
00234     bool has_unitstride() const
00235     {
00236         return inc(0) == 1 && inc(1) == 1;
00237     };
00238 
00241     bool equal_to(const matrix_type& mat) const;
00243 
00244 
00247     /*::::::::::::::::::::::::::::::::*/
00248     /*  Indices and access operations */
00249     /*::::::::::::::::::::::::::::::::*/
00250 
00253     inline int size(int d) const;   // submatrix size
00256     inline int cols() const
00257     {
00258         return size(1);
00259     }
00262     inline int rows() const
00263     {
00264         return size(0);
00265     }
00266 
00272     inline int inc(int d) const;    // explicit increment
00273 
00278     inline int gdim(int d) const;   // global dimensions
00279 
00284     inline int start(int d) const;  // return ii[d].start()
00285 
00290     inline int end(int d) const;    // return ii[d].end()
00291 
00296     inline LaIndex index(int d) const;// index
00297 
00301     inline int ref_count() const;
00302 
00305     inline int* addr() const;       // begining addr of data space
00307 
00310 
00325     inline int& operator()(int i, int j);
00326 
00341     inline const int& operator()(int i, int j) const;
00342 
00354     LaGenMatInt operator()(const LaIndex& I, const LaIndex& J) ;
00355 
00367     LaGenMatInt operator()(const LaIndex& I, const LaIndex& J) const;
00368 
00375     LaGenMatInt row(int k);
00382     LaGenMatInt row(int k) const;
00389     LaGenMatInt col(int k);
00396     LaGenMatInt col(int k) const;
00398 
00405     LaGenMatInt& operator=(int s);
00406 
00442     LaGenMatInt& operator=(const LaGenMatInt& s); //copy
00443 
00448     LaGenMatInt& operator+=(int s);
00449 
00454     LaGenMatInt& add(int s);
00455 
00464     LaGenMatInt& inject(const LaGenMatInt& s);
00465 
00470     LaGenMatInt& copy(const LaGenMatInt& s);
00471 
00476     LaGenMatInt copy() const;
00477 
00482     inline LaGenMatInt& shallow_assign();
00483 
00491     LaGenMatInt& ref(const LaGenMatInt& s);
00492 
00498     matrix_type repmat (int M, int N) const;
00501     value_type trace () const;
00505     matrix_type diag () const;
00507 
00508 
00512     inline int shallow() const      // read global shallow flag
00513     {
00514         return shallow_;
00515     }
00517     inline int debug() const;       // read global debug flag
00519     inline int debug(int d);        // set global debug flag
00531     inline const LaGenMatInt& info() const
00532     {
00533         *(const_cast<LaGenMatInt*>(this)->info_) = 1;
00534         return *this;
00535     };
00538     inline std::ostream& Info(std::ostream& s) const
00539     {
00540         s << "Size: (" << size(0) << "x" << size(1) << ") " ;
00541         s << "Indeces: " << ii[0] << " " << ii[1];
00542         s << "#ref: " << ref_count() << "addr: " << addr() << std::endl;
00543         return s;
00544     };
00546 
00550     friend DLLIMPORT std::ostream& operator<<(std::ostream&, const LaGenMatInt&);
00551 
00555     LaGenMatComplex to_LaGenMatComplex() const;
00557     LaGenMatDouble to_LaGenMatDouble() const;
00559     LaGenMatFloat to_LaGenMatFloat() const;
00561     LaGenMatLongInt to_LaGenMatLongInt() const;
00563 
00564 
00570     static matrix_type zeros (int N, int M = 0);
00574     static matrix_type ones (int N, int M = 0);
00578     static matrix_type eye (int N, int M = 0);
00587     static matrix_type rand (int N, int M,
00588                              value_type low = 0, value_type high = 1);
00592     static matrix_type from_diag (const matrix_type &vect);
00596     static matrix_type linspace (value_type start, value_type end,
00597                                  int nr_points);
00599 
00600 };  //* End of LaGenMatInt Class *//
00601 
00602 
00603 namespace la
00604 {
00606 typedef LaGenMatInt imat;
00607 } // namespace
00608 
00616 DLLIMPORT
00617 std::ostream& operator<<(std::ostream&, const LaGenMatInt&);
00618 
00619 
00620 
00621 //* Member Functions *//
00622 
00623 
00624 
00625 inline int LaGenMatInt::size(int d) const
00626 {
00627     return sz[d];
00628 }
00629 
00630 inline int LaGenMatInt::inc(int d) const
00631 {
00632     return ii[d].inc();
00633 }
00634 
00635 inline int LaGenMatInt::gdim(int d) const
00636 {
00637     return dim[d];
00638 }
00639 
00640 inline int LaGenMatInt::start(int d) const
00641 {
00642     return ii[d].start();
00643 }
00644 
00645 inline int LaGenMatInt::end(int d) const
00646 {
00647     return ii[d].end();
00648 }
00649 
00650 inline int LaGenMatInt::ref_count() const
00651 {
00652     return v.ref_count();
00653 }
00654 
00655 
00656 inline LaIndex LaGenMatInt::index(int d)  const
00657 {
00658     return ii[d];
00659 }
00660 
00661 inline int* LaGenMatInt::addr() const
00662 {
00663     return  v.addr();
00664 }
00665 
00666 inline int LaGenMatInt::debug() const
00667 {
00668     return debug_;
00669 }
00670 
00671 inline int LaGenMatInt::debug(int d)
00672 {
00673     return debug_ = d;
00674 }
00675 
00676 inline int& LaGenMatInt::operator()(int i, int j)
00677 {
00678 
00679 #ifdef LA_BOUNDS_CHECK
00680     assert(i >= 0);
00681     assert(i < size(0));
00682     assert(j >= 0);
00683     assert(j < size(1));
00684 #endif
00685     return v( dim[0] * (ii[1].start() + j * ii[1].inc()) +
00686               ii[0].start() + i * ii[0].inc());
00687 }
00688 
00689 inline const int& LaGenMatInt::operator()(int i, int j) const
00690 {
00691 
00692 #ifdef LA_BOUNDS_CHECK
00693     assert(i >= 0);
00694     assert(i < size(0));
00695     assert(j >= 0);
00696     assert(j < size(1));
00697 #endif
00698 
00699     return v( dim[0] * (ii[1].start() + j * ii[1].inc()) +
00700               ii[0].start() + i * ii[0].inc());
00701 }
00702 
00703 
00704 
00705 
00706 inline  LaGenMatInt&  LaGenMatInt::shallow_assign()
00707 {
00708     shallow_ = 1;
00709     return *this;
00710 }
00711 
00712 
00713 
00714 
00715 
00716 #endif
00717 // _LA_GEN_MAT_H_