| 1 | /***************************************************************************
 | 
|---|
| 2 |  * blitz/tiny/matvec.h   TinyMatrix/TinyVector product metaprogram
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  * $Id: matvec.h,v 1.1.1.1 1999-04-09 17:59:03 ansari Exp $
 | 
|---|
| 5 |  *
 | 
|---|
| 6 |  * Copyright (C) 1997,1998 Todd Veldhuizen <tveldhui@seurat.uwaterloo.ca>
 | 
|---|
| 7 |  *
 | 
|---|
| 8 |  * This program is free software; you can redistribute it and/or
 | 
|---|
| 9 |  * modify it under the terms of the GNU General Public License
 | 
|---|
| 10 |  * as published by the Free Software Foundation; either version 2
 | 
|---|
| 11 |  * of the License, or (at your option) any later version.
 | 
|---|
| 12 |  *
 | 
|---|
| 13 |  * This program is distributed in the hope that it will be useful,
 | 
|---|
| 14 |  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 15 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 16 |  * GNU General Public License for more details.
 | 
|---|
| 17 |  *
 | 
|---|
| 18 |  * Suggestions:          blitz-suggest@cybervision.com
 | 
|---|
| 19 |  * Bugs:                 blitz-bugs@cybervision.com
 | 
|---|
| 20 |  *
 | 
|---|
| 21 |  * For more information, please see the Blitz++ Home Page:
 | 
|---|
| 22 |  *    http://seurat.uwaterloo.ca/blitz/
 | 
|---|
| 23 |  *
 | 
|---|
| 24 |  ***************************************************************************
 | 
|---|
| 25 |  * $Log: not supported by cvs2svn $
 | 
|---|
| 26 |  * Revision 1.3  1998/03/14 00:08:44  tveldhui
 | 
|---|
| 27 |  * 0.2-alpha-05
 | 
|---|
| 28 |  *
 | 
|---|
| 29 |  * Revision 1.2  1998/03/14 00:04:47  tveldhui
 | 
|---|
| 30 |  * 0.2-alpha-05
 | 
|---|
| 31 |  *
 | 
|---|
| 32 |  * Revision 1.1  1997/07/16 14:51:20  tveldhui
 | 
|---|
| 33 |  * Update: Alpha release 0.2 (Arrays)
 | 
|---|
| 34 |  *
 | 
|---|
| 35 |  */
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #ifndef BZ_META_MATVEC_H
 | 
|---|
| 38 | #define BZ_META_MATVEC_H
 | 
|---|
| 39 | 
 | 
|---|
| 40 | #ifndef BZ_BLITZ_H
 | 
|---|
| 41 |  #include <blitz/blitz.h>
 | 
|---|
| 42 | #endif
 | 
|---|
| 43 | 
 | 
|---|
| 44 | #ifndef BZ_VECEXPRWRAP_H
 | 
|---|
| 45 |  #include <blitz/vecexprwrap.h>
 | 
|---|
| 46 | #endif
 | 
|---|
| 47 | 
 | 
|---|
| 48 | #ifndef BZ_METAPROG_H
 | 
|---|
| 49 |  #include <blitz/meta/metaprog.h>
 | 
|---|
| 50 | #endif
 | 
|---|
| 51 | 
 | 
|---|
| 52 | BZ_NAMESPACE(blitz)
 | 
|---|
| 53 | 
 | 
|---|
| 54 | // Forward declarations
 | 
|---|
| 55 | template<int N_rows, int N_columns, int N_rowStride, int N_colStride,
 | 
|---|
| 56 |     int N_vecStride, int J>
 | 
|---|
| 57 | class _bz_meta_matrixVectorProduct2;
 | 
|---|
| 58 | 
 | 
|---|
| 59 | 
 | 
|---|
| 60 | template<class T_numtype1, class T_numtype2, int N_rows, int N_columns, 
 | 
|---|
| 61 |     int N_rowStride, int N_colStride, int N_vecStride>
 | 
|---|
| 62 | class _bz_tinyMatrixVectorProduct {
 | 
|---|
| 63 | public:
 | 
|---|
| 64 |     typedef BZ_PROMOTE(T_numtype1, T_numtype2) T_numtype;
 | 
|---|
| 65 |     
 | 
|---|
| 66 |     _bz_tinyMatrixVectorProduct(const _bz_tinyMatrixVectorProduct<T_numtype1,
 | 
|---|
| 67 |         T_numtype2, N_rows, N_columns, N_rowStride, N_colStride, 
 | 
|---|
| 68 |         N_vecStride>& z)
 | 
|---|
| 69 |             : matrix_(z.matrix_), vector_(z.vector_)
 | 
|---|
| 70 |     { }
 | 
|---|
| 71 | 
 | 
|---|
| 72 |     _bz_tinyMatrixVectorProduct(const T_numtype1* matrix, 
 | 
|---|
| 73 |         const T_numtype2* vector)
 | 
|---|
| 74 |         : matrix_(matrix), vector_(vector)
 | 
|---|
| 75 |     { }
 | 
|---|
| 76 | 
 | 
|---|
| 77 |     T_numtype operator[](unsigned i) const
 | 
|---|
| 78 |     {
 | 
|---|
| 79 |         return _bz_meta_matrixVectorProduct2<N_rows, N_columns, N_rowStride,
 | 
|---|
| 80 |             N_colStride, N_vecStride, 0>::f(matrix_, vector_, i);
 | 
|---|
| 81 |     }
 | 
|---|
| 82 | 
 | 
|---|
| 83 |     T_numtype operator()(unsigned i) const
 | 
|---|
| 84 |     {
 | 
|---|
| 85 |         return _bz_meta_matrixVectorProduct2<N_rows, N_columns, N_rowStride,
 | 
|---|
| 86 |             N_colStride, N_vecStride, 0>::f(matrix_, vector_, i);
 | 
|---|
| 87 |     }
 | 
|---|
| 88 | 
 | 
|---|
| 89 |     enum {
 | 
|---|
| 90 |         _bz_staticLengthCount = 1,
 | 
|---|
| 91 |         _bz_dynamicLengthCount = 0,
 | 
|---|
| 92 |         _bz_staticLength = N_rows
 | 
|---|
| 93 | 
 | 
|---|
| 94 | #ifdef BZ_HAVE_COSTS
 | 
|---|
| 95 |      ,
 | 
|---|
| 96 |         _bz_costPerEval = 2 * N_columns * costs::memoryAccess
 | 
|---|
| 97 |             + (N_columns-1) * costs::add
 | 
|---|
| 98 | #endif
 | 
|---|
| 99 | 
 | 
|---|
| 100 |     };
 | 
|---|
| 101 | 
 | 
|---|
| 102 |     unsigned _bz_suggestLength() const
 | 
|---|
| 103 |     {
 | 
|---|
| 104 |         return N_rows;
 | 
|---|
| 105 |     }
 | 
|---|
| 106 | 
 | 
|---|
| 107 |     _bz_bool _bz_hasFastAccess() const
 | 
|---|
| 108 |     { return _bz_true; }
 | 
|---|
| 109 | 
 | 
|---|
| 110 |     T_numtype _bz_fastAccess(unsigned i) const
 | 
|---|
| 111 |     {
 | 
|---|
| 112 |         return _bz_meta_matrixVectorProduct2<N_rows, N_columns, N_rowStride,
 | 
|---|
| 113 |             N_colStride, N_vecStride, 0>::f(matrix_, vector_, i);
 | 
|---|
| 114 |     }
 | 
|---|
| 115 | 
 | 
|---|
| 116 |     unsigned length(unsigned recommendedLength) const
 | 
|---|
| 117 |     { return N_rows; }
 | 
|---|
| 118 | 
 | 
|---|
| 119 |     const T_numtype1* matrix() const
 | 
|---|
| 120 |     { return matrix_; }
 | 
|---|
| 121 | 
 | 
|---|
| 122 |     const T_numtype2* vector() const
 | 
|---|
| 123 |     { return vector_; }
 | 
|---|
| 124 | 
 | 
|---|
| 125 | protected:
 | 
|---|
| 126 |     const T_numtype1* matrix_;
 | 
|---|
| 127 |     const T_numtype2* vector_;
 | 
|---|
| 128 | };
 | 
|---|
| 129 | 
 | 
|---|
| 130 | template<class T_numtype1, class T_numtype2, int N_rows, int N_columns>
 | 
|---|
| 131 | inline _bz_VecExpr<_bz_tinyMatrixVectorProduct<T_numtype1, T_numtype2, 
 | 
|---|
| 132 |     N_rows, N_columns, N_columns, 1, 1> >
 | 
|---|
| 133 | product(const TinyMatrix<T_numtype1, N_rows, N_columns>& matrix,
 | 
|---|
| 134 |     const TinyVector<T_numtype2, N_columns>& vector)
 | 
|---|
| 135 | {
 | 
|---|
| 136 |     typedef _bz_tinyMatrixVectorProduct<T_numtype1, T_numtype2, N_rows, 
 | 
|---|
| 137 |         N_columns, N_columns, 1, 1> T_expr;
 | 
|---|
| 138 |     return _bz_VecExpr<T_expr>(T_expr(matrix.data(), vector.data()));
 | 
|---|
| 139 | }
 | 
|---|
| 140 | 
 | 
|---|
| 141 | // Template metaprogram for matrix-vector multiplication
 | 
|---|
| 142 | 
 | 
|---|
| 143 | template<int N_rows, int N_columns, int N_rowStride, int N_colStride,
 | 
|---|
| 144 |     int N_vecStride, int J>
 | 
|---|
| 145 | class _bz_meta_matrixVectorProduct2 {
 | 
|---|
| 146 | 
 | 
|---|
| 147 | public:
 | 
|---|
| 148 |     enum { go = J < (N_columns-1) };
 | 
|---|
| 149 |    
 | 
|---|
| 150 |     template<class T_numtype1, class T_numtype2> 
 | 
|---|
| 151 |     static inline BZ_PROMOTE(T_numtype1, T_numtype2)
 | 
|---|
| 152 |     f(const T_numtype1* matrix, const T_numtype2* vector, int i)
 | 
|---|
| 153 |     {
 | 
|---|
| 154 |         return matrix[i * N_rowStride + J * N_colStride]
 | 
|---|
| 155 |             * vector[J * N_vecStride]
 | 
|---|
| 156 | 
 | 
|---|
| 157 |             + _bz_meta_matrixVectorProduct2<N_rows * go, N_columns * go,
 | 
|---|
| 158 |                 N_rowStride * go, N_colStride * go, N_vecStride * go, (J+1)*go>
 | 
|---|
| 159 |                 ::f(matrix, vector, i);
 | 
|---|
| 160 |     }
 | 
|---|
| 161 |     
 | 
|---|
| 162 | };
 | 
|---|
| 163 | 
 | 
|---|
| 164 | template<>
 | 
|---|
| 165 | class _bz_meta_matrixVectorProduct2<0,0,0,0,0,0> {
 | 
|---|
| 166 | public:
 | 
|---|
| 167 |     static inline _bz_meta_nullOperand f(const void*, const void*, int)
 | 
|---|
| 168 |     { return _bz_meta_nullOperand(); }
 | 
|---|
| 169 | };
 | 
|---|
| 170 | 
 | 
|---|
| 171 | template<int N_rows, int N_columns, int N_rowStride, int N_colStride,
 | 
|---|
| 172 |     int N_vecStride, int I>
 | 
|---|
| 173 | class _bz_meta_matrixVectorProduct {
 | 
|---|
| 174 | public:
 | 
|---|
| 175 |     enum { go = I < (N_rows - 1) };
 | 
|---|
| 176 | 
 | 
|---|
| 177 |     template<class T_numtype1, class T_numtype2, class T_numtype3>
 | 
|---|
| 178 |     static inline void f(TinyVector<T_numtype3, N_rows>& result,
 | 
|---|
| 179 |         const T_numtype1* matrix, const T_numtype2* vector)
 | 
|---|
| 180 |     {
 | 
|---|
| 181 |         result[I] = _bz_meta_matrixVectorProduct2<N_rows, N_columns,
 | 
|---|
| 182 |             N_rowStride, N_colStride, N_vecStride, 0>::f(matrix,vector, I);
 | 
|---|
| 183 | 
 | 
|---|
| 184 |         _bz_meta_matrixVectorProduct<N_rows * go, N_columns * go,
 | 
|---|
| 185 |             N_rowStride * go, N_colStride * go, N_vecStride * go, (I+1)*go>
 | 
|---|
| 186 |               ::f(result, matrix, vector);
 | 
|---|
| 187 |     }
 | 
|---|
| 188 | };
 | 
|---|
| 189 | 
 | 
|---|
| 190 | template<>
 | 
|---|
| 191 | class _bz_meta_matrixVectorProduct<0,0,0,0,0,0> {
 | 
|---|
| 192 | public:
 | 
|---|
| 193 |     static inline void f(const _bz_tinyBase&, const void*, const void*)
 | 
|---|
| 194 |     { }
 | 
|---|
| 195 | };
 | 
|---|
| 196 | 
 | 
|---|
| 197 | BZ_NAMESPACE_END
 | 
|---|
| 198 | 
 | 
|---|
| 199 | #endif // BZ_META_MATVEC_H
 | 
|---|
| 200 | 
 | 
|---|