Blender V2.61 - r43446

transv.h

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