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