expression template help

R

Rex_chaos

Hi all,
As some book tells, I try the following example of expression
template.

template < typename LeftOpd, typename Op, typename RightOpd >
struct LOP
{
LeftOpd lod;
RightOpd rod;

LOP(LeftOpd lhs, RightOpd rhs): lod(lhs), rod(rhs) {}

ADT operator[](const unsigned int i)
{
return Op::apply(lod, rod);
}
};

struct Plus
{
static double apply( double a, double b) { return a+b; }
};

template <typename LeftOpd>
LOP< LeftOpd, Plus, Vec > operator+(LeftOpd a, Vec b)
{
return LOP<LeftOpd, Plus, Vec>(a, b);
};

It is a *very* simple example. I am trying to modify it. I have
written my vector class (a template class, says MyVec<T>). So here
Plus and opeator+ should also be modified to a template structure and
template function. I try the following code(I am sorry, I really have
no idea how to do it)

template < typename LeftOpd, typename Op<typename ADT>, typename
RightOpd >
struct LOP
{
LeftOpd lod;
RightOpd rod;

LOP(LeftOpd lhs, RightOpd rhs): lod(lhs), rod(rhs) {}

ADT operator[](const unsigned int i)
{
return Op<ADT>::apply(lod, rod);
}
};

template <typename ADT>
struct Plus
{
static ADT apply( ADT a, ADT b) { return a+b; }
};

template <typename LeftOpd, typename ADT>
LOP< LeftOpd, Plus<ADT>, MyVec<ADT> > operator+(LeftOpd a, MyVec<ADT>
b)
{
return LOP<LeftOpd, Plus<ADT>, MyVec<ADT> >(a, b);
};

The code don't work. I am looking for your help.

BTW, I have also defined my Matrix class. I would like to generalize
the expression template. For instance, I hope the following code works
with the help of expression template

MyMat<double> M(10,10);
MyVec<double> V(10); // a column vector

(M*V + 2.3*M)*M

Any idea?
 
D

David B. Held

Rex_chaos said:
[...]
For instance, I hope the following code works with the help
of expression template

MyMat<double> M(10,10);
MyVec<double> V(10); // a column vector

(M*V + 2.3*M)*M

Any idea?

Yes. Take a look at Blitz++.

Dave
 
T

tom_usenet

Hi all,
As some book tells, I try the following example of expression
template.

template < typename LeftOpd, typename Op, typename RightOpd >
struct LOP
{
LeftOpd lod;
RightOpd rod;

LOP(LeftOpd lhs, RightOpd rhs): lod(lhs), rod(rhs) {}

ADT operator[](const unsigned int i)
{
return Op::apply(lod, rod);
}
};

struct Plus
{
static double apply( double a, double b) { return a+b; }
};

template <typename LeftOpd>
LOP< LeftOpd, Plus, Vec > operator+(LeftOpd a, Vec b)
{
return LOP<LeftOpd, Plus, Vec>(a, b);
};

It is a *very* simple example. I am trying to modify it. I have
written my vector class (a template class, says MyVec<T>). So here
Plus and opeator+ should also be modified to a template structure and
template function. I try the following code(I am sorry, I really have
no idea how to do it)

template < typename LeftOpd, typename Op<typename ADT>, typename
RightOpd >
struct LOP
{
LeftOpd lod;
RightOpd rod;

LOP(LeftOpd lhs, RightOpd rhs): lod(lhs), rod(rhs) {}

ADT operator[](const unsigned int i)
{
return Op<ADT>::apply(lod, rod);
}
};


That class shouldn't have changed at all. It was already a general
vector binary operation class. The syntax you've used is illegal in
any case (I think you're trying to make Op a template template
parameter, but there's no need anyway).
template <typename ADT>
struct Plus
{
static ADT apply( ADT a, ADT b) { return a+b; }
};

That's fine. You should probably add a typedef for ADT into the struct
too.
template <typename LeftOpd, typename ADT>
LOP< LeftOpd, Plus<ADT>, MyVec<ADT> > operator+(LeftOpd a, MyVec<ADT>
b)
{
return LOP<LeftOpd, Plus<ADT>, MyVec<ADT> >(a, b);
};

That looks ok to me, although you are passing everything by value,
which is a no-no for vectors. I think you need a class that acts as a
reference to a vector.
The code don't work. I am looking for your help.

BTW, I have also defined my Matrix class. I would like to generalize
the expression template. For instance, I hope the following code works
with the help of expression template

MyMat<double> M(10,10);
MyVec<double> V(10); // a column vector

(M*V + 2.3*M)*M

Any idea?

Getting that to work will require a lot of code. There are various
expression template libraries out there, such as Blitz++ and PETE
http://www.codesourcery.com/pooma/pete. See also www.oonumerics.org.
You should be able to find various papers on the subject which might
help you. However, I suspect you've started from Todd Veldhuizen's
paper in any case.

Tom
 
R

Rex_chaos

template < typename LeftOpd, typename Op<typename ADT>, typename
RightOpd >
struct LOP
{
LeftOpd lod;
RightOpd rod;

LOP(LeftOpd lhs, RightOpd rhs): lod(lhs), rod(rhs) {}

ADT operator[](const unsigned int i)
{
return Op<ADT>::apply(lod, rod);
}
};


That class shouldn't have changed at all. It was already a general
vector binary operation class. The syntax you've used is illegal in
any case (I think you're trying to make Op a template template
parameter, but there's no need anyway).


Here is a problem. If I don't modifiy the class, how can I tell
LOP::apply to return a general type (i.e. ADT)?
That's fine. You should probably add a typedef for ADT into the struct
too.
What do you mean by 'add a typedef for ADT'? Do you mean it's no need
to build the struct as a template here?
 
T

tom_usenet

template < typename LeftOpd, typename Op<typename ADT>, typename
RightOpd >
struct LOP
{
LeftOpd lod;
RightOpd rod;

LOP(LeftOpd lhs, RightOpd rhs): lod(lhs), rod(rhs) {}

ADT operator[](const unsigned int i)
{
return Op<ADT>::apply(lod, rod);
}
};


That class shouldn't have changed at all. It was already a general
vector binary operation class. The syntax you've used is illegal in
any case (I think you're trying to make Op a template template
parameter, but there's no need anyway).


Here is a problem. If I don't modifiy the class, how can I tell
LOP::apply to return a general type (i.e. ADT)?


If you add a typedef to your Op classes, then you can do:

template < typename LeftOpd, typename Op, typename RightOpd >
struct LOP
{
LeftOpd lod;
RightOpd rod;
typedef typename Op::ADT ADT;

LOP(LeftOpd lhs, RightOpd rhs): lod(lhs), rod(rhs) {}

ADT operator[](const unsigned int i)
{
return Op<ADT>::apply(lod, rod);
}
};

The idea is that the Ops interface includes a typedef for the kind of
ADT that the methods are dealing in.
What do you mean by 'add a typedef for ADT'? Do you mean it's no need
to build the struct as a template here?

template <typename A>
struct Plus
{
typedef A ADT;
static ADT apply( ADT a, ADT b) { return a+b; }
};

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,269
Latest member
vinaykumar_nevatia23

Latest Threads

Top