C:/programs/SCPPNT/src/include/transv.h

Go to the documentation of this file.
00001 /*! \file transv.h
00002  \brief Definition of class Transpose_View
00003  
00004  The Transpose_View class allows working with a tranpose of a matrix without
00005  creating a new matrix containing the transpose.
00006 
00007  */
00008 
00009 /*
00010  Simple C++ Numerical Toolkit (SCPPNT)
00011  http://www.smallwaters.com/software/cpp/scppnt.html
00012  This release updates original work contributed by 
00013  Brad Hanson (http://www.b-a-h.com/) in 2001.
00014 
00015  */
00016 
00017 // Modified from transv.h in:
00018 /*
00019  *
00020  * Template Numerical Toolkit (TNT): Linear Algebra Module
00021  *
00022  * Mathematical and Computational Sciences Division
00023  * National Institute of Technology,
00024  * Gaithersburg, MD USA
00025  *
00026  *
00027  * This software was developed at the National Institute of Standards and
00028  * Technology (NIST) by employees of the Federal Government in the course
00029  * of their official duties. Pursuant to title 17 Section 105 of the
00030  * United States Code, this software is not subject to copyright protection
00031  * and is in the public domain.  The Template Numerical Toolkit (TNT) is
00032  * an experimental system.  NIST assumes no responsibility whatsoever for
00033  * its use by other parties, and makes no guarantees, expressed or implied,
00034  * about its quality, reliability, or any other characteristic.
00035  *
00036  * BETA VERSION INCOMPLETE AND SUBJECT TO CHANGE
00037  * see http://math.nist.gov/tnt for latest updates.
00038  *
00039  */
00040 
00041 // Matrix Transpose Views
00042 
00043 #ifndef SCPPNT_TRANSV_H
00044 #define SCPPNT_TRANSV_H
00045 
00046 #ifdef SCPPNT_NO_DIR_PREFIX
00047 #include "scppnt.h"
00048 #include "scppnt_error.h"
00049 #include "matop.h"
00050 #include "vec.h"
00051 #else
00052 #include "scppnt/scppnt.h"
00053 #include "scppnt/scppnt_error.h"
00054 #include "scppnt/matop.h"
00055 #include "scppnt/vec.h"
00056 #endif
00057 
00058 #ifndef SCPPNT_NO_IO
00059 #include <iostream>
00060 #endif
00061 
00062 namespace SCPPNT
00063 {
00064 
00065   /*!
00066    \brief Allows use of the transpose of a matrix without creating a new matrix.
00067    
00068    This class allows working with a tranpose of a matrix without
00069    creating a new matrix containing the transpose. The transpose
00070    view is read-only. Only operations which do not change the
00071    content of the matrix can be performed with a transpose view.
00072 
00073    */
00074   template<class Array2D> class Transpose_View
00075   {
00076 
00077 private:
00078 
00079     const Array2D & A_; //!< Matrix that is the basis of the tranpose view
00080 
00081 public:
00082 
00083     typedef typename Array2D::size_type size_type; //!< Subscript type
00084     typedef typename Array2D::value_type value_type; //!< Type of elements stored in matrix
00085     typedef typename Array2D::element_type element_type; //!< Type of elements stored in matrix
00086     typedef typename Array2D::pointer pointer; //!< Pointer to type stored in matrix
00087     typedef typename Array2D::iterator iterator; //!< Iterator over elements in matrix
00088     typedef typename Array2D::reference reference; //!< Reference to type stored in matrix
00089     typedef typename Array2D::const_iterator const_iterator; //!< Iterator over elements in constant matrix
00090     typedef typename Array2D::const_reference const_reference; //!< Reference to type stored in constant matrix
00091 
00092     /**** Iterators ****/
00093 
00094     //! Iterator over constant elements of a row
00095     typedef typename Array2D::const_column_iterator const_row_iterator;
00096     //! Iterator over constant elements of a column
00097     typedef typename Array2D::const_row_iterator const_column_iterator;
00098 
00099     //! Iterator over row iterators (points to const_row_iterator object for a row)
00100     typedef typename Array2D::const_columns_iterator const_rows_iterator;
00101     //! Iterator over column iterators (points to const_column_iterator object for a column)
00102     typedef typename Array2D::const_rows_iterator const_columns_iterator;
00103 
00104     //! Iterator over constant elements of a matrix diagonal
00105     typedef typename Array2D::const_diag_iterator const_diag_iterator;
00106 
00107     //! Return matrix that is the basis of the transpose view
00108     const Array2D & array() const
00109     {
00110       return A_;
00111     }
00112 
00113     //! Return number of rows in matrix
00114     Subscript num_rows() const
00115     {
00116       return A_.num_columns();
00117     }
00118     //! Return number of columns in matrix
00119     Subscript num_columns() const
00120     {
00121       return A_.num_rows();
00122     }
00123     //! Returns lower bound of subscript
00124     Subscript lbound() const
00125     {
00126       return A_.lbound();
00127     }
00128     //! Return number of rows if argument is 1, number of columns if argument is 2
00129     Subscript dim(Subscript i) const
00130     {
00131       return (i == 1) ? A_.dim(2) : A_.dim(i-1);
00132     }
00133 
00134     //! Return total number of elements in matrix
00135     Subscript size() const
00136     {
00137       return A_.size();
00138     }
00139 
00140     /**** constructors ****/
00141 
00142     //! Copy constructor
00143     Transpose_View(const Transpose_View<Array2D> &A) :
00144       A_(A.A_)
00145     {
00146     }
00147     ;
00148 
00149     //! Constructor from matrix
00150     Transpose_View(Array2D &A) :
00151       A_(A)
00152     {
00153     }
00154     ;
00155 
00156     /**** Subscripting ****/
00157 
00158     //! Return iterator to constant elements in row i+1 (0-offset)
00159     const_row_iterator operator[](Subscript i) const
00160     {
00161       return A_.column(i+1);
00162     }
00163 
00164     //! Return constant element in row i and column j, where i and j are 1-offset indices
00165     const_reference operator()(Subscript i, Subscript j) const
00166     {
00167       return A_(j, i);
00168     }
00169 
00170     /**** Iterators ****/
00171 
00172     //! Return iterator pointing to row iterator for first row (iterator over row iterators)
00173     const_rows_iterator begin_rows() const
00174     {
00175       return (const_rows_iterator) A_.begin_columns();
00176     }
00177 
00178     //! Return iterator pointing to row iterator for one past last row
00179     const_rows_iterator end_rows() const
00180     {
00181       return (const_rows_iterator) A_.end_columns();
00182     }
00183 
00184     //! Return iterator pointing to column iterator for first column (iterator over column iterators)
00185     const_columns_iterator begin_columns() const
00186     {
00187       return (const_columns_iterator) A_.begin_rows();
00188     }
00189 
00190     //! Return iterator pointing to column iterator for one past last column
00191     const_columns_iterator end_columns() const
00192     {
00193       return (const_columns_iterator) A_.end_rows();
00194     }
00195 
00196     // Return row or column iterator for particular row or column.
00197     // Index is one-offset (first row or column is 1).
00198     // row(N+1) returns a row_iterator to one past the last row, where there are N rows
00199     // column(M+1) returns a column_iterator to one past the last column, where there are M columns
00200     //
00201 
00202     //! Return iterator pointing to first element in row 'index' (1-offset)
00203     const_row_iterator begin_row(Subscript index) const
00204     {
00205       return A_.begin_column(index);
00206     }
00207 
00208     //! Return iterator pointing to one past last element in row 'index' (1-offset)
00209     const_row_iterator end_row(Subscript index) const
00210     {
00211       return A_.end_column(index);
00212     }
00213 
00214     //! Return iterator pointing to first element in column 'index' (1-offset)
00215     const_column_iterator begin_column(Subscript index) const
00216     {
00217       return A_.begin_row(index);
00218     }
00219 
00220     //! Return iterator pointing to one past last element in column 'index' (1-offset)
00221     const_column_iterator end_column(Subscript index) const
00222     {
00223       return A_.end_row(index);
00224     }
00225 
00226     // Iterator over all elements of matrix.
00227     // Should only be used when the order in which the iteration takes place
00228     // over all elements of the matrix does not matter
00229 
00230     //! Return iterator pointing to first element of matrix
00231     const_iterator begin() const
00232     {
00233       return A_.begin();
00234     }
00235 
00236     //! Return iterator pointing to one past last element of matrix
00237     const_iterator end() const
00238     {
00239       return A_.end();
00240     }
00241 
00242     // Iterator over diagonals.
00243 
00244     /*!
00245      
00246      \brief Returns iterator pointing to first element of a matrix diagonal
00247      
00248      \param row is the row corresponding to the first element of the diagonal
00249      \param column is the column corresponding to the first element of the diagonal
00250      */
00251     const_diag_iterator begin_diagonal(Subscript row, Subscript column) const
00252     {
00253       return A_.begin_diagonal(column, row);
00254     }
00255 
00256     /*! 
00257      Returns iterator pointing to one past last element of matrix diagonal
00258      \param row is the row corresponding to the first element of the diagonal
00259      \param column is the column corresponding to the first element of the diagonal
00260      */
00261     const_diag_iterator end_diagonal(Subscript row, Subscript column) const
00262     {
00263       return A_.end_diagonal(column, row);
00264     }
00265   };
00266 
00267 #ifndef SCPPNT_NO_IO
00268   /*!
00269    \brief Write a matrix to an output stream.
00270    
00271    Writes the number of rows and columns, followed
00272    by a new line. The following lines contain the
00273    elements in the rows of the matrix.
00274    */
00275   template<class Array2D> std::ostream& operator<<(std::ostream &s,
00276       const Transpose_View<Array2D> &A)
00277   {
00278     Subscript M=A.num_rows();
00279     Subscript N=A.num_columns();
00280 
00281     Subscript start = A.lbound();
00282     Subscript Mend = M + A.lbound() - 1;
00283     Subscript Nend = N + A.lbound() - 1;
00284 
00285     s << M << "  " << N << std::endl;
00286     for (Subscript i=start; i<=Mend; i++)
00287     {
00288       for (Subscript j=start; j<=Nend; j++)
00289       {
00290         s << A(i, j) << " ";
00291       }
00292       s << std::endl;
00293     }
00294 
00295     return s;
00296   }
00297 #endif
00298 
00299   // *******************[ basic matrix operators ]***************************
00300 
00301   //! Add two matrices and return new matrix giving sum
00302   template<class Array2D, class M> inline M operator+(const Transpose_View<Array2D> &A, const M &B)
00303   {
00304     M sum(A.num_rows(), A.num_columns());
00305     matadd(A, B, sum);
00306     return sum;
00307   }
00308 
00309   //! Subtract matrix B from matrix A and return matrix giving difference
00310   template<class Array2D, class M> inline M operator-(const Transpose_View<Array2D> &A, const M &B)
00311   {
00312     M diff(A.num_rows(), A.num_columns());
00313     matsub(A, B, diff);
00314     return diff;
00315   }
00316 
00317   //! Multiplication operator returning a new matrix containing the matrix product A*B
00318   template<class Array2D, class M> inline M operator*(const Transpose_View<Array2D> &A, const M &B)
00319   {
00320     M c(A.num_rows(), B.num_columns());
00321     matmult(c, A, B);
00322     return c;
00323   }
00324 
00325   //! Return a new vector containing the product of the matrix A and the vector x
00326   template<class Array2D> inline Vector<typename Array2D::value_type> operator*(
00327       const Transpose_View<Array2D> &A, const Vector<typename Array2D::value_type> &x)
00328   {
00329     return matrix_times_vector<Transpose_View<Array2D>,
00330     Vector <typename Array2D::value_type> >(A, x);
00331   }
00332 
00333 } // namespace SCPPNT
00334 
00335 
00336 #endif
00337 // SCPPNT_TRANSV_H

Generated on Tue Dec 18 23:34:06 2007 for SCPPNT by  doxygen 1.5.4