Generic matrix operators

S

Stipanicev

I want to write generic operators for
matrix computation which would adapt to different types (shapes) of matrices
i.e. when
adding square matrix and lower triangle matrix it would add only elements on
and
under main diagonal and copy the rest from square matrix. Basically it would
allow adding new matrix type (shape) without writing new operators. Here is
the
code I hope could do that:


//Matrix Trait
//ret depends on matrix shape
template <int r, int c, class Matrix>
struct IsZero
{
enum { ret = false };
};

class LowerTriangMatrix;
//all elements above main diagonal in LTM are 0
template <int r, int c>
struct IsZero<r, c, LowerTriangMatrix>
{
enum { ret = (c > r) ? true : false; };
};


//expresion tree node
template <class Lhs, class OpTag, class Rhs>
struct Expression
{
typedef Type typename Lhs::T;
Expression(Lhs const& lhs, Rhs const& rhs)
: l(lhs), r(rhs)
{
}
template <int row, int col>
Type Apply(/*unsigned row, unsigned col*/) const
{
return IsZero<row, col, Lhs>::ret ?
IsZero<row, col, Rhs>::ret ?
(Type)0 : OpTag<Type>::Apply((Type)0, r(row,col))
: IsZero<row, col, Rhs>::ret ?
OpTag<Type>::Apply(l(row,col), (Type)0)
: OpTag<Type>::Apply( l(row, col), r(row,col) );
}
Lhs const& l;
Rhs const& r;
};

//OpTag
template <typename Lhs>
struct Plus
{
static Lhs Apply(Lhs& lhs, Lhs& rhs)
{
return lhs + rhs;
}
};


And the question is can this be done? And can I do it this way? I'll be
really
grateful for any suggestion/advice.


Best regards,
Zoran Stipanicev
 
V

Victor Bazarov

Stipanicev said:
I want to write generic operators for
matrix computation [..]

Zoran,

With all due respect to your effort, this is not a chat room,
not an IRC channel, you cannot expect answers to such a complex
topic sometimes even within a day. Have patience, please. If
somebody sees that your post has gone unanswered, perhaps they
will spend time needed to study it and to figure what to tell
you. We all are busy with our lives (however unlikely that
might seem from our participation here), and your inquiry does
require some thought. I assure you, most of regulars here do
read all the posts and do try to assist those in need.

Thank you for your trust in the community and your understanding.

V
 
Z

zs

Hi!

I agree with You Victor, and I apologize for my impatience.
And as I said before I would be very grateful for any advice/suggestion
(even the short one's like "it's not possible" or "it's
possible but not meaningful").

Best regards,
Stipanicev.
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top