Lapack++
gmc.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_COMPLEX_H_
00042 #define _LA_GEN_MAT_COMPLEX_H_
00043 
00044 #ifndef LA_COMPLEX_SUPPORT
00045 /* An application must define LA_COMPLEX_SUPPORT if it wants to use
00046  * complex numbers here. */
00047 # error "The macro LA_COMPLEX_SUPPORT needs to be defined if you want to use complex-valued matrices."
00048 #endif
00049 
00050 #include "arch.h"
00051 #include "lafnames.h"
00052 #include VECTOR_COMPLEX_H
00053 #include LA_INDEX_H
00054 #include LA_GEN_MAT_DOUBLE_H
00055 
00056 class LaGenMatComplex;
00057 class LaGenMatDouble;
00058 class LaGenMatFloat;
00059 class LaGenMatInt;
00060 class LaGenMatLongInt;
00061 
00088 class DLLIMPORT LaGenMatComplex
00089 {
00090 public:
00092     typedef COMPLEX value_type;
00096     typedef LaGenMatComplex matrix_type;
00099     typedef VectorComplex vec_type;
00100 private:
00101     vec_type     v;
00102     LaIndex           ii[2];
00103     int             dim[2];  // size of original matrix, not submatrix
00104     //int             sz[2];   // size of this submatrix
00105     void init(int m, int n);
00106     int size0;
00107     int size1;
00108     static int  debug_; // trace all entry and exits into methods and
00109     // operators of this class.  This variable is
00110     // explicitly initalized in lagenmatCOMPLEX.cc
00111 
00112     static int      *info_;   // print matrix info only, not values
00113     //   originally 0, set to 1, and then
00114     //   reset to 0 after use.
00115     // use as in
00116     //
00117     //    std::cout << B.info() << std::endl;
00118     //
00119     // this *info_ member is unique in that it really isn't
00120     // part of the matrix info, just a flag as to how
00121     // to print it.   We've included in this beta release
00122     // as part of our testing, but we do not expect it
00123     // to be user accessable.
00124     // It has to be declared as global static
00125     // so that we may monitor expresssions like
00126     // X::(const &X) and still utilize without violating
00127     // the "const" condition.
00128     // Because this *info_ is used at most one at a time,
00129     // there is no harm in keeping only one copy of it,
00130     // also, we do not need to malloc free space every time
00131     // we call a matrix constructor.
00132 
00133 
00134     int shallow_; // set flag to '0' in order to return matrices
00135     // by value from functions without unecessary
00136     // copying.
00137 
00138 
00139     // users shouldn't be able to modify assignment semantics..
00140     //
00141     //LaGenMatComplex& shallow_assign();
00142 
00143 public:
00144 
00145 
00148     /*::::::::::::::::::::::::::*/
00149     /* Constructors/Destructors */
00150     /*::::::::::::::::::::::::::*/
00151 
00153     LaGenMatComplex();
00154 
00157     LaGenMatComplex(int m, int n);
00158 
00195     LaGenMatComplex(COMPLEX*v, int m, int n, bool row_ordering = false);
00196 
00219     LaGenMatComplex(const LaGenMatComplex&);
00220 
00225     explicit LaGenMatComplex(const LaGenMatDouble& s_real,
00226                              const LaGenMatDouble& s_imag = LaGenMatDouble());
00227 
00231     LaGenMatComplex& resize(int m, int n);
00232 
00236     LaGenMatComplex& resize(const LaGenMatComplex& s);
00237 
00240     virtual ~LaGenMatComplex();
00242 
00243 
00248     bool is_zero() const;
00249 
00252     bool is_submatrixview() const
00253     {
00254         return size(0) != gdim(0) || size(1) != gdim(1);
00255     };
00256 
00261     bool has_unitstride() const
00262     {
00263         return inc(0) == 1 && inc(1) == 1;
00264     };
00265 
00268     bool equal_to(const LaGenMatComplex& mat) const;
00270 
00271 
00274     /*::::::::::::::::::::::::::::::::*/
00275     /*  Indices and access operations */
00276     /*::::::::::::::::::::::::::::::::*/
00277 
00280     inline int size(int d) const;   // submatrix size
00283     inline int cols() const
00284     {
00285         return size(1);
00286     }
00289     inline int rows() const
00290     {
00291         return size(0);
00292     }
00293 
00299     inline int inc(int d) const;    // explicit increment
00300 
00305     inline int gdim(int d) const;   // global dimensions
00306 
00311     inline int start(int d) const;  // return ii[d].start()
00312 
00317     inline int end(int d) const;    // return ii[d].end()
00318 
00323     inline LaIndex index(int d) const;// index
00324 
00328     inline int ref_count() const;
00329 
00332     inline COMPLEX* addr() const;       // begining addr of data space
00334 
00351     inline COMPLEX& operator()(int i, int j);
00352 
00367     inline const COMPLEX& operator()(int i, int j) const;
00368 
00380     LaGenMatComplex operator()(const LaIndex& I, const LaIndex& J) ;
00381 
00393     LaGenMatComplex operator()(const LaIndex& I, const LaIndex& J) const;
00394 
00401     LaGenMatComplex row(int k);
00408     LaGenMatComplex row(int k) const;
00415     LaGenMatComplex col(int k);
00422     LaGenMatComplex col(int k) const;
00424 
00431     LaGenMatComplex& operator=(COMPLEX s);
00432 
00433     // CS: addition
00438     LaGenMatComplex& operator=(const LaComplex& s);
00439 
00440     /* Set elements of left-hand size to the scalar value s. No
00441      * new matrix is created, so that if there are other matrices
00442      * that reference this memory space, they will also be
00443      * affected. */
00444     //LaGenMatComplex& operator=(const std::complex<double>& s);
00445     // CS: end
00446 
00482     LaGenMatComplex& operator=(const LaGenMatComplex& s); //copy
00483 
00492     LaGenMatComplex& operator+=(COMPLEX s);
00493 
00498     LaGenMatComplex& add(COMPLEX s);
00499 
00504     LaGenMatComplex& scale(const LaComplex& s);
00505 
00510     LaGenMatComplex& scale(COMPLEX s);
00511 
00516     LaGenMatComplex& operator*=(COMPLEX s);
00517 
00526     LaGenMatComplex& inject(const LaGenMatComplex& s);
00527 
00532     LaGenMatComplex& copy(const LaGenMatComplex& s);
00533 
00538     LaGenMatComplex copy() const;
00539 
00547     LaGenMatComplex& copy(const LaGenMatDouble& s_real,
00548                           const LaGenMatDouble& s_imag = LaGenMatDouble());
00549 
00554     inline LaGenMatComplex& shallow_assign();
00555 
00563     LaGenMatComplex& ref(const LaGenMatComplex& s);
00565 
00571     LaGenMatComplex repmat (int M, int N) const;
00574     value_type trace () const;
00578     LaGenMatComplex diag () const;
00579 
00584     LaGenMatDouble real() const;
00585 
00590     LaGenMatDouble imag() const;
00592 
00593 
00597     inline int shallow() const      // read global shallow flag
00598     {
00599         return shallow_;
00600     }
00602     inline int debug() const;       // read global debug flag
00604     inline int debug(int d);        // set global debug flag
00605 
00617     inline const LaGenMatComplex& info() const
00618     {
00619         *(const_cast<LaGenMatComplex*>(this)->info_) = 1;
00620         return *this;
00621     };
00622 
00625     inline std::ostream& Info(std::ostream& s) const
00626     {
00627         s << "Size: (" << size(0) << "x" << size(1) << ") " ;
00628         s << "Indeces: " << ii[0] << " " << ii[1];
00629         s << "#ref: " << ref_count() << "addr: " << addr() << std::endl;
00630         return s;
00631     };
00633 
00640     friend DLLIMPORT std::ostream& operator<<(std::ostream&, const LaGenMatComplex&);
00641 
00646     LaGenMatDouble real_to_LaGenMatDouble() const;
00649     LaGenMatFloat real_to_LaGenMatFloat() const;
00651     LaGenMatInt real_to_LaGenMatInt() const;
00654     LaGenMatLongInt real_to_LaGenMatLongInt() const;
00657     LaGenMatDouble imag_to_LaGenMatDouble() const;
00660     LaGenMatFloat imag_to_LaGenMatFloat() const;
00663     LaGenMatInt imag_to_LaGenMatInt() const;
00666     LaGenMatLongInt imag_to_LaGenMatLongInt() const;
00668 
00669 
00675     static LaGenMatComplex zeros (int N, int M = 0);
00679     static LaGenMatComplex ones (int N, int M = 0);
00683     static LaGenMatComplex eye (int N, int M = 0);
00693     static LaGenMatComplex rand (int N, int M,
00694                                  double low = 0, double high = 1);
00698     static LaGenMatComplex from_diag (const LaGenMatComplex &vect);
00702     static LaGenMatComplex linspace (value_type start,
00703                                      value_type end,
00704                                      int nr_points);
00706 
00707 };  //* End of LaGenMatComplex Class *//
00708 
00709 
00710 
00711 namespace la
00712 {
00715 typedef LaGenMatComplex cmat;
00716 } // namespace
00717 
00725 DLLIMPORT
00726 std::ostream& operator<<(std::ostream&, const LaGenMatComplex&);
00727 
00728 
00729 //* Member Functions *//
00730 
00731 
00732 
00733 inline int LaGenMatComplex::size(int d) const
00734 {
00735     if (d == 0)
00736         return size0;
00737     else
00738         return size1;
00739     //return sz[d];
00740 }
00741 
00742 inline int LaGenMatComplex::inc(int d) const
00743 {
00744     return ii[d].inc();
00745 }
00746 
00747 inline int LaGenMatComplex::gdim(int d) const
00748 {
00749     return dim[d];
00750 }
00751 
00752 inline int LaGenMatComplex::start(int d) const
00753 {
00754     return ii[d].start();
00755 }
00756 
00757 inline int LaGenMatComplex::end(int d) const
00758 {
00759     return ii[d].end();
00760 }
00761 
00762 inline int LaGenMatComplex::ref_count() const
00763 {
00764     return v.ref_count();
00765 }
00766 
00767 
00768 inline LaIndex LaGenMatComplex::index(int d)  const
00769 {
00770     return ii[d];
00771 }
00772 
00773 inline COMPLEX* LaGenMatComplex::addr() const
00774 {
00775     return  v.addr();
00776 }
00777 
00778 inline int LaGenMatComplex::debug() const
00779 {
00780     return debug_;
00781 }
00782 
00783 inline int LaGenMatComplex::debug(int d)
00784 {
00785     return debug_ = d;
00786 }
00787 
00788 inline COMPLEX& LaGenMatComplex::operator()(int i, int j)
00789 {
00790 
00791 #ifdef LA_BOUNDS_CHECK
00792     assert(i >= 0);
00793     assert(i < size(0));
00794     assert(j >= 0);
00795     assert(j < size(1));
00796 #endif
00797     return v( dim[0] * (ii[1].start() + j * ii[1].inc()) +
00798               ii[0].start() + i * ii[0].inc());
00799 }
00800 
00801 inline const COMPLEX& LaGenMatComplex::operator()(int i, int j) const
00802 {
00803 
00804 #ifdef LA_BOUNDS_CHECK
00805     assert(i >= 0);
00806     assert(i < size(0));
00807     assert(j >= 0);
00808     assert(j < size(1));
00809 #endif
00810 
00811     return v( dim[0] * (ii[1].start() + j * ii[1].inc()) +
00812               ii[0].start() + i * ii[0].inc());
00813 }
00814 
00815 
00816 
00817 
00818 inline  LaGenMatComplex&  LaGenMatComplex::shallow_assign()
00819 {
00820     shallow_ = 1;
00821     return *this;
00822 }
00823 
00824 
00825 
00826 #ifndef LA_COMPLEX_SUPPORT
00827 // Repeat this warning again
00828 # error "The macro LA_COMPLEX_SUPPORT needs to be defined if you want to use complex-valued matrices."
00829 #endif
00830 
00831 
00832 #endif
00833 // _LA_GEN_MAT_H_