1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
#ifndef FIXED_SIZE_H
#define FIXED_SIZE_H
/*
* templates for fixed size vector and matrix arithmetic
*
*/
/*
* Dot Product
*
*/
template<int N, int SX, int SY, class T>
class _dot
{
public:
inline T operator()(const T * X, const T * Y)
{
_dot<N-1,SX,SY,T> d;
return (*X) * (*Y) + d(X + SX, Y + SY);
}
};
template<int SX, int SY, class T>
class _dot<1,SX,SY,T>
{
public:
inline T operator()(const T * X, const T * Y)
{
return (*X) * (*Y);
}
};
template<int N, int SX, int SY, class T>
inline T dot(const T * X, const T * Y)
{
_dot<N,SX,SY,T> d;
return d(X, Y);
}
/*
* Matrix Vector Product Y = A*X
*
*/
template<int M, int N, int SX, int SY, class T>
class _matvec
{
public:
inline void operator()(const T * A, const T * X, T * Y)
{
*Y += dot<N,1,SX,T>(A,X);
_matvec<M-1,N,SX,SY,T> d;
d(A + N, X, Y + SY);
}
};
template<int N, int SX, int SY, class T>
class _matvec<1,N,SX,SY,T>
{
public:
inline void operator()(const T * A, const T * X, T * Y)
{
*Y += dot<N,1,SX,T>(A,X);
}
};
template<int M, int N, int SX, int SY, class T>
inline void matvec(const T * A, const T * X, T * Y)
{
_matvec<M,N,SX,SY,T> d;
d(A,X,Y);
}
/*
* Matrix Matrix Product C = A*B
*
* C is L*N
* A is L*M
* B is M*N
*
*/
template<int L, int M, int N, int U, class T>
class _matmat
{
public:
inline void operator()(const T * A, const T * B, T * C)
{
matvec<L,M,N,N>(A,B,C);
_matmat<L,M,N,U-1,T> d;
d(A, B + 1, C + 1);
}
};
template<int L, int M, int N, class T>
class _matmat<L,M,N,0,T>
{
public:
inline void operator()(const T * A, const T * B, T * C)
{
matvec<L,M,N,N>(A,B,C);
}
};
template<int L, int M, int N, class T>
inline void matmat(const T * A, const T * B, T * C)
{
_matmat<L,M,N,N-1,T> d;
d(A,B,C);
}
/*
* Binary vector operation Z = op(X,Y)
*
*/
template<int N, class T, class bin_op>
class _vec_binop_vec
{
public:
inline void operator()(const T * X, const T * Y, T * Z, const bin_op& op)
{
*Z = op( *X, *Y );
_vec_binop_vec<N-1,T,bin_op> d;
d(X + 1, Y + 1, Z + 1, op);
}
};
template<class T, class bin_op>
class _vec_binop_vec<1,T,bin_op>
{
public:
inline void operator()(const T * X, const T * Y, T * Z, const bin_op& op)
{
*Z = op( *X, *Y );
}
};
template<int N, class T, class bin_op>
inline void vec_binop_vec(const T * X, const T * Y, T * Z, const bin_op& op)
{
_vec_binop_vec<N,T,bin_op> d;
d(X,Y,Z,op);
}
#endif
|