# Metaprogramming nightmare

This topic is 4449 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi there! I've been assigned to code a program for sparse matrix and vector multiplication using metaprogramming, that is, recursive templated structures. It turns out that my code works...only for full matrixes. It has to be very quick since it is for scientific computing. Has anyone any idea on how to do this stuff? Thanks, Carol

##### Share on other sites
Yes, but we can't give you an answer until you post a question.

##### Share on other sites
You really should not post homework/assignments here and expect others to do them for you.

However, if you have specific code-related questions that shows you have been working on it yourself, we may be able to help you.

For instance, if there's a specific part of your code giving you problems, post it, and someone may be able to help you. Don't just ask if someone else can do the entire thing.

##### Share on other sites
Hey,I just wanted to know if someone had any idea =)
Here is my code:

template<int dim>
struct SparseDot
{
static double Get(int i,int index,const Array<1,vector<Member>,Brick>& matrix,
const Vector<N,double>& vect)
{
return SparseDot<dim / 2> :: Get(i,index,matrix,vect) +
SparseDot<dim - (dim / 2)> :: Get(i,index + (dim / 2),matrix,vect);
}
};

template<>
struct SparseDot<1>
{
static double Get(int i,int index,const Array<1,vector<Member>,Brick>& matrix,
const Vector<N,double>& vect)
{

return (matrix(i))[index].value * vect((matrix(i))[index].col);

}
};

This one works well if the matrix is full(I am using a library named POOMA in conjunction with STL. The matrix is a POOMA Array of STL vectors, and the vector is a POOMA vector). On another function I iterate over the matrix lines, and then the code shown above does a vector vector multiplication(since each matrix line can be considered a vector).
The problem is, the templated recursion uses a defined dimension(for instance, a #define N 1000) but when the matrix is sparse, obviously this results in Segmentation Fault.
And now I stuck. Anybody help?

Thanks,
Carol

##### Share on other sites
I've worked on some sparse matrices for school, and it basically used linked lists for vectors and a linked list of linked lists for a matrix. More information about that should be all over the web and in almost any linear algebra text. As for implementing it using templates, that's more difficult. I assume it'll look something like the normal version.

tj963

##### Share on other sites
Why reinvent the wheel?

##### Share on other sites
Quote:
 Original post by MaulingMonkeyWhy reinvent the wheel?

Let me guess...
Quote:
 I've been assigned to code a program...

I think I got it [smile]

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 32
• ### Forum Statistics

• Total Topics
632630
• Total Posts
3007526
• ### Who's Online (See full list)

There are no registered users currently online

×