Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

tnt_transv.h

Go to the documentation of this file.
00001 /*
00002 *
00003 * Template Numerical Toolkit (SPUC): Linear Algebra Module
00004 *
00005 * Mathematical and Computational Sciences Division
00006 * National Institute of Technology,
00007 * Gaithersburg, MD USA
00008 *
00009 *
00010 * This software was developed at the National Institute of Standards and
00011 * Technology (NIST) by employees of the Federal Government in the course
00012 * of their official duties. Pursuant to title 17 Section 105 of the
00013 * United States Code, this software is not subject to copyright protection
00014 * and is in the public domain.  The Template Numerical Toolkit (SPUC) is
00015 * an experimental system.  NIST assumes no responsibility whatsoever for
00016 * its use by other parties, and makes no guarantees, expressed or implied,
00017 * about its quality, reliability, or any other characteristic.
00018 *
00019 * BETA VERSION INCOMPLETE AND SUBJECT TO CHANGE
00020 * see http://math.nist.gov/tnt for latest updates.
00021 *
00022 */
00023 
00024 
00025 
00026 // Matrix Transpose Views
00027 
00028 #ifndef TRANSV_H
00029 #define TRANSV_H
00030 
00031 #include <iostream>
00032 #include <cassert>
00033 
00034 namespace SPUC {
00035 
00037 template <class array2D> class transpose_view
00038 {
00039     protected:
00040 
00041         const array2D &  A_;
00042 
00043     public:
00044 
00045         typedef typename array2D::element_type T;
00046         typedef         T   value_type;
00047         typedef         T   element_type;
00048         typedef         T*  pointer;
00049         typedef         T*  iterator;
00050         typedef         T&  reference;
00051         typedef const   T*  const_iterator;
00052         typedef const   T&  const_reference;
00053 
00054 
00055         const array2D & array()  const { return A_; }
00056         subscript num_rows() const { return A_.num_cols();}
00057         subscript num_cols() const { return A_.num_rows();}
00058         subscript lbound() const { return A_.lbound(); }
00059         subscript dim(subscript i) const
00060         {
00061 #ifdef SPUC_BOUNDS_CHECK
00062             assert( A_.lbound() <= i);
00063             assert( i<= A_.lbound()+1);
00064 #endif
00065             if (i== A_.lbound())
00066                 return num_rows();
00067             else
00068                 return num_cols();
00069         }
00070 
00071 
00072         transpose_view(const transpose_view<array2D> &A) : A_(A.A_) {};
00073         transpose_view(const array2D &A) : A_(A) {};
00074 
00075 
00076         inline const typename array2D::element_type & operator()(
00077                                                                                                                                  int i, int j) const
00078                         {
00079 #ifdef SPUC_BOUNDS_CHECK
00080                                 assert(lbound()<=i);
00081                                 assert(i<=A_.num_cols() + lbound() - 1);
00082                                 assert(lbound()<=j);
00083                                 assert(j<=A_.num_rows() + lbound() - 1);
00084 #endif
00085                                 
00086                                 return A_(j,i);
00087                         }
00088 
00089 
00090 };
00092  template <class Matrix> transpose_view<Matrix> transpose_view(const Matrix &A)
00093 {
00094     return transpose_view<Matrix>(A);
00095  }
00096 template <class Matrix, class T>
00097         Array1D<T> matmult(
00098                                            const transpose_view<Matrix> & A, 
00099                                            const Array1D<T> &B)
00100 {
00101     int  M = A.dim1();
00102     int  N = A.dim2();
00103         
00104     assert(B.dim() == N);
00105         
00106     Array1D<T> x(N);
00107 
00108     int i, j;
00109     T tmp = 0;
00110 
00111     for (i=1; i<=M; i++)
00112     {
00113         tmp = 0;
00114         for (j=1; j<=N; j++)
00115             tmp += A(i,j) * B(j);
00116         x(i) = tmp;
00117     }
00118 
00119     return x;
00120 }
00121 
00122 template <class Matrix, class T>
00123 inline Array1D<T> operator*(const transpose_view<Matrix> & A, const Array1D<T> &B)
00124 {
00125     return matmult(A,B);
00126 }
00127 
00128 
00129 template <class Matrix>
00130 std::ostream& operator<<(std::ostream &s, const transpose_view<Matrix> &A)
00131 {
00132     int M=A.dim1();
00133     int N=A.dim2();
00134 
00135     int start = A.lbound();
00136     int Mend = M + A.lbound() - 1;
00137     int Nend = N + A.lbound() - 1;
00138 
00139     s << M << "  " << N << endl;
00140     for (int i=start; i<=Mend; i++)
00141     {
00142         for (int j=start; j<=Nend; j++)
00143         {
00144             s << A(i,j) << " ";
00145         }
00146         s << endl;
00147     }
00148 
00149 
00150     return s;
00151 }
00152 
00153 } // namespace SPUC
00154 
00155 #endif
00156 // TRANSV_H

Generated on Fri Sep 16 11:02:17 2005 for spuc by  doxygen 1.4.4