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