1 | // -*- C++ -*- |
---|
2 | /*************************************************************************** |
---|
3 | * blitz/meta/matmat.h TinyMatrix matrix-matrix product metaprogram |
---|
4 | * |
---|
5 | * $Id: matmat.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_MATMAT_H |
---|
28 | #define BZ_META_MATMAT_H |
---|
29 | |
---|
30 | #ifndef BZ_TINYMAT_H |
---|
31 | #error <blitz/meta/matmat.h> must be included via <blitz/tinymat.h> |
---|
32 | #endif |
---|
33 | |
---|
34 | #include <blitz/meta/metaprog.h> |
---|
35 | #include <blitz/tinymatexpr.h> |
---|
36 | |
---|
37 | BZ_NAMESPACE(blitz) |
---|
38 | |
---|
39 | // Template metaprogram for matrix-matrix multiplication |
---|
40 | template<int N_rows1, int N_columns, int N_columns2, int N_rowStride1, |
---|
41 | int N_colStride1, int N_rowStride2, int N_colStride2, int K> |
---|
42 | class _bz_meta_matrixMatrixProduct { |
---|
43 | public: |
---|
44 | static const int go = (K != N_columns - 1) ? 1 : 0; |
---|
45 | |
---|
46 | template<typename T_numtype1, typename T_numtype2> |
---|
47 | static inline BZ_PROMOTE(T_numtype1, T_numtype2) |
---|
48 | f(const T_numtype1* matrix1, const T_numtype2* matrix2, int i, int j) |
---|
49 | { |
---|
50 | return matrix1[i * N_rowStride1 + K * N_colStride1] |
---|
51 | * matrix2[K * N_rowStride2 + j * N_colStride2] |
---|
52 | + _bz_meta_matrixMatrixProduct<N_rows1 * go, N_columns * go, |
---|
53 | N_columns2 * go, N_rowStride1 * go, N_colStride1 * go, |
---|
54 | N_rowStride2 * go, N_colStride2 * go, (K+1) * go> |
---|
55 | ::f(matrix1, matrix2, i, j); |
---|
56 | } |
---|
57 | }; |
---|
58 | |
---|
59 | template<> |
---|
60 | class _bz_meta_matrixMatrixProduct<0,0,0,0,0,0,0,0> { |
---|
61 | public: |
---|
62 | static inline _bz_meta_nullOperand f(const void*, const void*, int, int) |
---|
63 | { return _bz_meta_nullOperand(); } |
---|
64 | }; |
---|
65 | |
---|
66 | |
---|
67 | |
---|
68 | |
---|
69 | template<typename T_numtype1, typename T_numtype2, int N_rows1, int N_columns, |
---|
70 | int N_columns2, int N_rowStride1, int N_colStride1, |
---|
71 | int N_rowStride2, int N_colStride2> |
---|
72 | class _bz_tinyMatrixMatrixProduct { |
---|
73 | public: |
---|
74 | typedef BZ_PROMOTE(T_numtype1, T_numtype2) T_numtype; |
---|
75 | |
---|
76 | static const int rows = N_rows1, columns = N_columns2; |
---|
77 | |
---|
78 | _bz_tinyMatrixMatrixProduct(const T_numtype1* matrix1, |
---|
79 | const T_numtype2* matrix2) |
---|
80 | : matrix1_(matrix1), matrix2_(matrix2) |
---|
81 | { } |
---|
82 | |
---|
83 | _bz_tinyMatrixMatrixProduct(const _bz_tinyMatrixMatrixProduct<T_numtype1, |
---|
84 | T_numtype2, N_rows1, N_columns, N_columns2, N_rowStride1, N_colStride1, |
---|
85 | N_rowStride2, N_colStride2>& x) |
---|
86 | : matrix1_(x.matrix1_), matrix2_(x.matrix2_) |
---|
87 | { } |
---|
88 | |
---|
89 | const T_numtype1* matrix1() const |
---|
90 | { return matrix1_; } |
---|
91 | |
---|
92 | const T_numtype2* matrix2() const |
---|
93 | { return matrix2_; } |
---|
94 | |
---|
95 | T_numtype operator()(int i, int j) const |
---|
96 | { |
---|
97 | return _bz_meta_matrixMatrixProduct<N_rows1, N_columns, |
---|
98 | N_columns2, N_rowStride1, N_colStride1, N_rowStride2, |
---|
99 | N_colStride2, 0>::f(matrix1_, matrix2_, i, j); |
---|
100 | } |
---|
101 | |
---|
102 | protected: |
---|
103 | const T_numtype1* matrix1_; |
---|
104 | const T_numtype2* matrix2_; |
---|
105 | }; |
---|
106 | |
---|
107 | template<typename T_numtype1, typename T_numtype2, int N_rows1, int N_columns1, |
---|
108 | int N_columns2> |
---|
109 | inline |
---|
110 | _bz_tinyMatExpr<_bz_tinyMatrixMatrixProduct<T_numtype1, T_numtype2, N_rows1, |
---|
111 | N_columns1, N_columns2, N_columns1, 1, N_columns2, 1> > |
---|
112 | product(const TinyMatrix<T_numtype1, N_rows1, N_columns1>& a, |
---|
113 | const TinyMatrix<T_numtype2, N_columns1, N_columns2>& b) |
---|
114 | { |
---|
115 | typedef _bz_tinyMatrixMatrixProduct<T_numtype1, T_numtype2, |
---|
116 | N_rows1, N_columns1, N_columns2, N_columns1, 1, N_columns2, 1> T_expr; |
---|
117 | return _bz_tinyMatExpr<T_expr>(T_expr(a.data(), b.data())); |
---|
118 | } |
---|
119 | |
---|
120 | BZ_NAMESPACE_END |
---|
121 | |
---|
122 | #endif // BZ_META_MATMAT_H |
---|
123 | |
---|