| 1 | /*************************************************************************** | 
|---|
| 2 | * blitz/tiny/matvec.h   TinyMatrix/TinyVector product metaprogram | 
|---|
| 3 | * | 
|---|
| 4 | * $Id: matvec.h,v 1.1.1.1 1999-11-26 16:37:08 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.1.1.1  1999/04/09  17:59:03  ansari | 
|---|
| 27 | * Creation module DPC/Blitz (blitz 0.4) Reza 09/04/99 | 
|---|
| 28 | * | 
|---|
| 29 | * Revision 1.3  1998/03/14 00:08:44  tveldhui | 
|---|
| 30 | * 0.2-alpha-05 | 
|---|
| 31 | * | 
|---|
| 32 | * Revision 1.2  1998/03/14 00:04:47  tveldhui | 
|---|
| 33 | * 0.2-alpha-05 | 
|---|
| 34 | * | 
|---|
| 35 | * Revision 1.1  1997/07/16 14:51:20  tveldhui | 
|---|
| 36 | * Update: Alpha release 0.2 (Arrays) | 
|---|
| 37 | * | 
|---|
| 38 | */ | 
|---|
| 39 |  | 
|---|
| 40 | #ifndef BZ_META_MATVEC_H | 
|---|
| 41 | #define BZ_META_MATVEC_H | 
|---|
| 42 |  | 
|---|
| 43 | #ifndef BZ_BLITZ_H | 
|---|
| 44 | #include <blitz/blitz.h> | 
|---|
| 45 | #endif | 
|---|
| 46 |  | 
|---|
| 47 | #ifndef BZ_VECEXPRWRAP_H | 
|---|
| 48 | #include <blitz/vecexprwrap.h> | 
|---|
| 49 | #endif | 
|---|
| 50 |  | 
|---|
| 51 | #ifndef BZ_METAPROG_H | 
|---|
| 52 | #include <blitz/meta/metaprog.h> | 
|---|
| 53 | #endif | 
|---|
| 54 |  | 
|---|
| 55 | BZ_NAMESPACE(blitz) | 
|---|
| 56 |  | 
|---|
| 57 | // Forward declarations | 
|---|
| 58 | template<int N_rows, int N_columns, int N_rowStride, int N_colStride, | 
|---|
| 59 | int N_vecStride, int J> | 
|---|
| 60 | class _bz_meta_matrixVectorProduct2; | 
|---|
| 61 |  | 
|---|
| 62 |  | 
|---|
| 63 | template<class T_numtype1, class T_numtype2, int N_rows, int N_columns, | 
|---|
| 64 | int N_rowStride, int N_colStride, int N_vecStride> | 
|---|
| 65 | class _bz_tinyMatrixVectorProduct { | 
|---|
| 66 | public: | 
|---|
| 67 | typedef BZ_PROMOTE(T_numtype1, T_numtype2) T_numtype; | 
|---|
| 68 |  | 
|---|
| 69 | _bz_tinyMatrixVectorProduct(const _bz_tinyMatrixVectorProduct<T_numtype1, | 
|---|
| 70 | T_numtype2, N_rows, N_columns, N_rowStride, N_colStride, | 
|---|
| 71 | N_vecStride>& z) | 
|---|
| 72 | : matrix_(z.matrix_), vector_(z.vector_) | 
|---|
| 73 | { } | 
|---|
| 74 |  | 
|---|
| 75 | _bz_tinyMatrixVectorProduct(const T_numtype1* matrix, | 
|---|
| 76 | const T_numtype2* vector) | 
|---|
| 77 | : matrix_(matrix), vector_(vector) | 
|---|
| 78 | { } | 
|---|
| 79 |  | 
|---|
| 80 | T_numtype operator[](unsigned i) const | 
|---|
| 81 | { | 
|---|
| 82 | return _bz_meta_matrixVectorProduct2<N_rows, N_columns, N_rowStride, | 
|---|
| 83 | N_colStride, N_vecStride, 0>::f(matrix_, vector_, i); | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | T_numtype operator()(unsigned i) const | 
|---|
| 87 | { | 
|---|
| 88 | return _bz_meta_matrixVectorProduct2<N_rows, N_columns, N_rowStride, | 
|---|
| 89 | N_colStride, N_vecStride, 0>::f(matrix_, vector_, i); | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | enum { | 
|---|
| 93 | _bz_staticLengthCount = 1, | 
|---|
| 94 | _bz_dynamicLengthCount = 0, | 
|---|
| 95 | _bz_staticLength = N_rows | 
|---|
| 96 |  | 
|---|
| 97 | #ifdef BZ_HAVE_COSTS | 
|---|
| 98 | , | 
|---|
| 99 | _bz_costPerEval = 2 * N_columns * costs::memoryAccess | 
|---|
| 100 | + (N_columns-1) * costs::add | 
|---|
| 101 | #endif | 
|---|
| 102 |  | 
|---|
| 103 | }; | 
|---|
| 104 |  | 
|---|
| 105 | unsigned _bz_suggestLength() const | 
|---|
| 106 | { | 
|---|
| 107 | return N_rows; | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | _bz_bool _bz_hasFastAccess() const | 
|---|
| 111 | { return _bz_true; } | 
|---|
| 112 |  | 
|---|
| 113 | T_numtype _bz_fastAccess(unsigned i) const | 
|---|
| 114 | { | 
|---|
| 115 | return _bz_meta_matrixVectorProduct2<N_rows, N_columns, N_rowStride, | 
|---|
| 116 | N_colStride, N_vecStride, 0>::f(matrix_, vector_, i); | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | unsigned length(unsigned recommendedLength) const | 
|---|
| 120 | { return N_rows; } | 
|---|
| 121 |  | 
|---|
| 122 | const T_numtype1* matrix() const | 
|---|
| 123 | { return matrix_; } | 
|---|
| 124 |  | 
|---|
| 125 | const T_numtype2* vector() const | 
|---|
| 126 | { return vector_; } | 
|---|
| 127 |  | 
|---|
| 128 | protected: | 
|---|
| 129 | const T_numtype1* matrix_; | 
|---|
| 130 | const T_numtype2* vector_; | 
|---|
| 131 | }; | 
|---|
| 132 |  | 
|---|
| 133 | template<class T_numtype1, class T_numtype2, int N_rows, int N_columns> | 
|---|
| 134 | inline _bz_VecExpr<_bz_tinyMatrixVectorProduct<T_numtype1, T_numtype2, | 
|---|
| 135 | N_rows, N_columns, N_columns, 1, 1> > | 
|---|
| 136 | product(const TinyMatrix<T_numtype1, N_rows, N_columns>& matrix, | 
|---|
| 137 | const TinyVector<T_numtype2, N_columns>& vector) | 
|---|
| 138 | { | 
|---|
| 139 | typedef _bz_tinyMatrixVectorProduct<T_numtype1, T_numtype2, N_rows, | 
|---|
| 140 | N_columns, N_columns, 1, 1> T_expr; | 
|---|
| 141 | return _bz_VecExpr<T_expr>(T_expr(matrix.data(), vector.data())); | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | // Template metaprogram for matrix-vector multiplication | 
|---|
| 145 |  | 
|---|
| 146 | template<int N_rows, int N_columns, int N_rowStride, int N_colStride, | 
|---|
| 147 | int N_vecStride, int J> | 
|---|
| 148 | class _bz_meta_matrixVectorProduct2 { | 
|---|
| 149 |  | 
|---|
| 150 | public: | 
|---|
| 151 | enum { go = J < (N_columns-1) }; | 
|---|
| 152 |  | 
|---|
| 153 | template<class T_numtype1, class T_numtype2> | 
|---|
| 154 | static inline BZ_PROMOTE(T_numtype1, T_numtype2) | 
|---|
| 155 | f(const T_numtype1* matrix, const T_numtype2* vector, int i) | 
|---|
| 156 | { | 
|---|
| 157 | return matrix[i * N_rowStride + J * N_colStride] | 
|---|
| 158 | * vector[J * N_vecStride] | 
|---|
| 159 |  | 
|---|
| 160 | + _bz_meta_matrixVectorProduct2<N_rows * go, N_columns * go, | 
|---|
| 161 | N_rowStride * go, N_colStride * go, N_vecStride * go, (J+1)*go> | 
|---|
| 162 | ::f(matrix, vector, i); | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 | }; | 
|---|
| 166 |  | 
|---|
| 167 | template<> | 
|---|
| 168 | class _bz_meta_matrixVectorProduct2<0,0,0,0,0,0> { | 
|---|
| 169 | public: | 
|---|
| 170 | static inline _bz_meta_nullOperand f(const void*, const void*, int) | 
|---|
| 171 | { return _bz_meta_nullOperand(); } | 
|---|
| 172 | }; | 
|---|
| 173 |  | 
|---|
| 174 | template<int N_rows, int N_columns, int N_rowStride, int N_colStride, | 
|---|
| 175 | int N_vecStride, int I> | 
|---|
| 176 | class _bz_meta_matrixVectorProduct { | 
|---|
| 177 | public: | 
|---|
| 178 | enum { go = I < (N_rows - 1) }; | 
|---|
| 179 |  | 
|---|
| 180 | template<class T_numtype1, class T_numtype2, class T_numtype3> | 
|---|
| 181 | static inline void f(TinyVector<T_numtype3, N_rows>& result, | 
|---|
| 182 | const T_numtype1* matrix, const T_numtype2* vector) | 
|---|
| 183 | { | 
|---|
| 184 | result[I] = _bz_meta_matrixVectorProduct2<N_rows, N_columns, | 
|---|
| 185 | N_rowStride, N_colStride, N_vecStride, 0>::f(matrix,vector, I); | 
|---|
| 186 |  | 
|---|
| 187 | _bz_meta_matrixVectorProduct<N_rows * go, N_columns * go, | 
|---|
| 188 | N_rowStride * go, N_colStride * go, N_vecStride * go, (I+1)*go> | 
|---|
| 189 | ::f(result, matrix, vector); | 
|---|
| 190 | } | 
|---|
| 191 | }; | 
|---|
| 192 |  | 
|---|
| 193 | template<> | 
|---|
| 194 | class _bz_meta_matrixVectorProduct<0,0,0,0,0,0> { | 
|---|
| 195 | public: | 
|---|
| 196 | static inline void f(const _bz_tinyBase&, const void*, const void*) | 
|---|
| 197 | { } | 
|---|
| 198 | }; | 
|---|
| 199 |  | 
|---|
| 200 | BZ_NAMESPACE_END | 
|---|
| 201 |  | 
|---|
| 202 | #endif // BZ_META_MATVEC_H | 
|---|
| 203 |  | 
|---|