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