Lapack++
gmf.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_FLOAT_H_
00042 #define _LA_GEN_MAT_FLOAT_H_
00043 
00044 #include "arch.h"
00045 #include "lafnames.h"
00046 #include VECTOR_FLOAT_H
00047 #include LA_INDEX_H
00048 
00049 class LaGenMatComplex;
00050 class LaGenMatDouble;
00051 class LaGenMatFloat;
00052 class LaGenMatInt;
00053 class LaGenMatLongInt;
00054 
00055 
00056 class DLLIMPORT LaGenMatFloat
00057 {
00058 public:
00060     typedef float value_type;
00064     typedef LaGenMatFloat matrix_type;
00067     typedef VectorFloat vec_type;
00068 private:
00069     vec_type     v;
00070     LaIndex         ii[2];
00071     int             dim[2];  // size of original matrix, not submatrix
00072     int             sz[2];   // size of this submatrix
00073     void init(int m, int n);
00074     static int  debug_; // trace all entry and exits into methods and
00075     // operators of this class.  This variable is
00076     // explicitly initalized in lagenmatfloat.cc
00077 
00078     static int      *info_;   // print matrix info only, not values
00079     //   originally 0, set to 1, and then
00080     //   reset to 0 after use.
00081     // use as in
00082     //
00083     //    std::cout << B.info() << std::endl;
00084     //
00085     // this *info_ member is unique in that it really isn't
00086     // part of the matrix info, just a flag as to how
00087     // to print it.   We've included in this beta release
00088     // as part of our testing, but we do not expect it
00089     // to be user accessable.
00090     // It has to be declared as global static
00091     // so that we may monitor expresssions like
00092     // X::(const &X) and still utilize without violating
00093     // the "const" condition.
00094     // Because this *info_ is used at most one at a time,
00095     // there is no harm in keeping only one copy of it,
00096     // also, we do not need to malloc free space every time
00097     // we call a matrix constructor.
00098 
00099 
00100     int shallow_; // set flag to '0' in order to return matrices
00101     // by value from functions without unecessary
00102     // copying.
00103 
00104 
00105     // users shouldn't be able to modify assignment semantics..
00106     //
00107     //LaGenMatFloat& shallow_assign();
00108 
00109 public:
00110 
00111 
00112 
00113     /*::::::::::::::::::::::::::*/
00114 
00115     /* Constructors/Destructors */
00116 
00117     /*::::::::::::::::::::::::::*/
00118 
00119 
00120     LaGenMatFloat();
00121     LaGenMatFloat(int, int);
00122 
00159     LaGenMatFloat(float* v, int m, int n, bool row_ordering = false);
00160 
00183     LaGenMatFloat(const LaGenMatFloat&);
00184     virtual ~LaGenMatFloat();
00185 
00186 
00191     bool is_zero() const;
00192 
00195     bool is_submatrixview() const
00196     {
00197         return size(0) != gdim(0) || size(1) != gdim(1);
00198     };
00199 
00204     bool has_unitstride() const
00205     {
00206         return inc(0) == 1 && inc(1) == 1;
00207     };
00208 
00211     bool equal_to(const matrix_type& mat) const;
00213 
00214 
00215     /*::::::::::::::::::::::::::::::::*/
00216 
00217     /*  Indices and access operations */
00218 
00219     /*::::::::::::::::::::::::::::::::*/
00220 
00221     inline int size(int d) const;   // submatrix size
00224     inline int cols() const
00225     {
00226         return size(1);
00227     }
00230     inline int rows() const
00231     {
00232         return size(0);
00233     }
00234     inline int inc(int d) const;    // explicit increment
00235     inline int gdim(int d) const;   // global dimensions
00236     inline int start(int d) const;  // return ii[d].start()
00237     inline int end(int d) const;    // return ii[d].end()
00238     inline LaIndex index(int d) const;// index
00239     inline int ref_count() const;
00240     inline LaGenMatFloat& shallow_assign();
00241     inline float* addr() const;       // begining addr of data space
00242 
00243     inline float& operator()(int i, int j);
00244     inline const float& operator()(int i, int j) const;
00245     LaGenMatFloat operator()(const LaIndex& I, const LaIndex& J) ;
00246     LaGenMatFloat operator()(const LaIndex& I, const LaIndex& J) const;
00253     LaGenMatFloat row(int k);
00260     LaGenMatFloat row(int k) const;
00267     LaGenMatFloat col(int k);
00274     LaGenMatFloat col(int k) const;
00275 
00276     LaGenMatFloat& operator=(float s);
00312     LaGenMatFloat& operator=(const LaGenMatFloat& s); //copy
00313 
00314     LaGenMatFloat& operator+=(float s);
00315     LaGenMatFloat& add(float s);
00316 
00317     LaGenMatFloat& resize(int m, int n);
00318     LaGenMatFloat& resize(const LaGenMatFloat& s);
00319     LaGenMatFloat& ref(const LaGenMatFloat& s);
00320     LaGenMatFloat& inject(const LaGenMatFloat& s);
00321     LaGenMatFloat& copy(const LaGenMatFloat& s);
00322 
00327     LaGenMatFloat copy() const;
00328 
00334     matrix_type repmat (int M, int N) const;
00337     value_type trace () const;
00341     matrix_type diag () const;
00343 
00344 
00345     inline int shallow() const      // read global shallow flag
00346     {
00347         return shallow_;
00348     }
00349     inline int debug() const;       // read global debug flag
00350     inline int debug(int d);        // set global debug flag
00351     inline const LaGenMatFloat& info() const
00352     {
00353         int *t = info_;
00354         *t = 1;
00355         return *this;
00356     };
00357 
00358     //* I/O *//
00359     friend DLLIMPORT std::ostream& operator<<(std::ostream&, const LaGenMatFloat&);
00360     std::ostream& Info(std::ostream& s) const
00361     {
00362         s << "Size: (" << size(0) << "x" << size(1) << ") " ;
00363         s << "Indeces: " << ii[0] << " " << ii[1];
00364         s << "#ref: " << ref_count() << "addr: " << addr() << std::endl;
00365         return s;
00366     };
00367 
00371     LaGenMatComplex to_LaGenMatComplex() const;
00373     LaGenMatDouble to_LaGenMatDouble() const;
00375     LaGenMatInt to_LaGenMatInt() const;
00377     LaGenMatLongInt to_LaGenMatLongInt() const;
00379 
00380 
00386     static matrix_type zeros (int N, int M = 0);
00390     static matrix_type ones (int N, int M = 0);
00394     static matrix_type eye (int N, int M = 0);
00403     static matrix_type rand (int N, int M,
00404                              value_type low = 0, value_type high = 1);
00408     static matrix_type from_diag (const matrix_type &vect);
00412     static matrix_type linspace (value_type start, value_type end,
00413                                  int nr_points);
00415 
00416 };  //* End of LaGenMatFloat Class *//
00417 
00418 
00419 namespace la
00420 {
00423 typedef LaGenMatFloat fmat;
00424 } // namespace
00425 
00433 DLLIMPORT
00434 std::ostream& operator<<(std::ostream&, const LaGenMatFloat&);
00435 
00436 
00437 
00438 //* Member Functions *//
00439 
00440 
00441 
00442 inline int LaGenMatFloat::size(int d) const
00443 {
00444     return sz[d];
00445 }
00446 
00447 inline int LaGenMatFloat::inc(int d) const
00448 {
00449     return ii[d].inc();
00450 }
00451 
00452 inline int LaGenMatFloat::gdim(int d) const
00453 {
00454     return dim[d];
00455 }
00456 
00457 inline int LaGenMatFloat::start(int d) const
00458 {
00459     return ii[d].start();
00460 }
00461 
00462 inline int LaGenMatFloat::end(int d) const
00463 {
00464     return ii[d].end();
00465 }
00466 
00467 inline int LaGenMatFloat::ref_count() const
00468 {
00469     return v.ref_count();
00470 }
00471 
00472 
00473 inline LaIndex LaGenMatFloat::index(int d)  const
00474 {
00475     return ii[d];
00476 }
00477 
00478 inline float* LaGenMatFloat::addr() const
00479 {
00480     return  v.addr();
00481 }
00482 
00483 inline int LaGenMatFloat::debug() const
00484 {
00485     return debug_;
00486 }
00487 
00488 inline int LaGenMatFloat::debug(int d)
00489 {
00490     return debug_ = d;
00491 }
00492 
00493 inline float& LaGenMatFloat::operator()(int i, int j)
00494 {
00495 
00496 #ifdef LA_BOUNDS_CHECK
00497     assert(i >= 0);
00498     assert(i < size(0));
00499     assert(j >= 0);
00500     assert(j < size(1));
00501 #endif
00502     return v( dim[0] * (ii[1].start() + j * ii[1].inc()) +
00503               ii[0].start() + i * ii[0].inc());
00504 }
00505 
00506 inline const float& LaGenMatFloat::operator()(int i, int j) const
00507 {
00508 
00509 #ifdef LA_BOUNDS_CHECK
00510     assert(i >= 0);
00511     assert(i < size(0));
00512     assert(j >= 0);
00513     assert(j < size(1));
00514 #endif
00515 
00516     return v( dim[0] * (ii[1].start() + j * ii[1].inc()) +
00517               ii[0].start() + i * ii[0].inc());
00518 }
00519 
00520 
00521 
00522 
00523 inline  LaGenMatFloat&  LaGenMatFloat::shallow_assign()
00524 {
00525     shallow_ = 1;
00526     return *this;
00527 }
00528 
00529 
00530 
00531 
00532 
00533 #endif
00534 // _LA_GEN_MAT_H_