|
Boost : |
From: Dave Steffen (steffend_at_[hidden])
Date: 2001-08-19 12:37:21
Ralf W. Grosse-Kunstleve writes:
> I am thinking about implementing a "small matrix" library for
> 2-dimensional arrays. (This would be similar to boost::array,
> i.e. the small matrices will usually live on the stack to avoid
> the dynamic memory allocation overhead.)
Yeah, I've had some thoughts about this too. Eventually this
will/can/would/should be part of the Boost matrix library (which
AFAIK will be == MTL version 3).
> One requirement is to have operators for *heterogeneous*
> arithmetic operations, e.g. multiplication. E.g. something
> like:
>
> template <class LR, class L, class R>
> matrix<LR> operator*(const matrix<L>& lhs, const matrix<R>& rhs)
> {
> // return the product
> }
>
> Is there a mechanism in C++ to define a table for the result type LR
> depending on the types of L and R?
Yes, use traits.
> For example, if L=int and R=int, the result type LR=int, but for
> L=double and R=int the result type should be LR=double, etc.
Quick example:
template <class L, class R> struct MatrixOpTraits
{ ... possible default result? ... };
template <> struct MatrixOpTraits<int,int>
{ typedef int Result; };
template <> struct MatrixOpTraits<double,int>
{ typedef double Result; };
Then, e.g.
template <class R, class L> inline
MatrixOpTraits<R,L>::Result // there's the return type
operator * (const Matrix<R>& A, const Matrix<L>& B)
{ ... }
On another subject, lots of cool things can be done if the size of
the vectors/matrices is known at run time. Template metaprogramming
techniques can be used to effectively unroll the loops at
compile-time, so that (for example) given 3-vectors a,b, and c, the
line
c = a + b;
expands (via template metaprogramming magic) to
c[0] = a[0] + b[0];
c[1] = a[1] + b[1];
c[2] = a[2] + b[2];
which is typically something the compiler's optimizer can go crazy
with.
--------------------------------------------------------------------------
Dave Steffen Wave after wave will flow with the tide
Numerica, Inc. And bury the world as it does
steffend_at_[hidden] Tide after tide will flow and recede
Leaving life to go on as it was...
- Peart / RUSH
"The reason that our people suffer in this way....
is that our ancestors failed to rule wisely". -General Choi, Hong Hi
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk