473,734 Members | 2,211 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Expression templates and generic matrix operator

Hi!

The question is product of laziness. I want to write generic operators for
matrix computation which would adapt to different types 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 (I'm to
lazy
for that :) ). Here is the code I hope could do that:
//Matrix Trait
template <int r, int c, class Matrix>
struct IsZero
{
enum { ret = false };
};

class LowerTriangMatr ix;
//all elements above main diagonal in LTM are 0
template <int r, int c>
struct IsZero<r, c, LowerTriangMatr ix>
{
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>::Ap ply((Type)0, r(row,col))
: IsZero<row, col, Rhs>::ret ?
OpTag<Type>::Ap ply(l(row,col), (Type)0)
: OpTag<Type>::Ap ply( l(row, col), r(row,col) );
//return OpTag::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 really don't
have time to try it only to find out that it's impossible and I'll be really
grateful for any suggestion/advice.

P.S. I'm aware that this probably requires unwinding for loops and that it
would generate a lot of code, but it can still be used for small matrices.

Best regards,
Zoran Stipanicev
Jan 6 '06 #1
0 1384

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2493
by: Tim Wesson | last post by:
Does anyone know if it is possible to choose between the below templates (or similar ones) according to the promotion rules for types T and U? Thanks, Tim Wesson.
8
1448
by: bartek | last post by:
Hello, I've been toying around with expression templates recently while writing a simple static lexical analyser, hence I have some questions which keep bothering me... The expression objects are wrapped around following the 'curious base' pattern. E.g.
2
2285
by: Steven T. Hatton | last post by:
Is there a way to forward declare a template? I've been trying to resolve a problem resulting from a recursive templet reference. The problem comes about when I attempt to #include a file which itself has a #include of a file with a function defined in it. I've simplified the code to the point that the reason for arranging things as I have is completely lost, as is all the functionality. I started out with this:...
3
1703
by: Drew McCormack | last post by:
Does operator() get inherited? I would have thought so, but I can't get the following to work: I have a base class that takes its derived class as a template template parameter (the Barton-Nackmann trick). It has a operator() defined. template <typename NumType, template <typename T> class DerivedType> class Matrix { public:
11
2600
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them by myself. I know there are already tons of vector and matrix implementations, but I wanted to have one taylored for my needs and without debugging someones else code. Also is's become somewhat personal meanwhile ;-).
17
3321
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the way they are. ***** Summary & Questions ***** In a nutshell, the current .NET generics & .NET framework make it sometimes difficult or even impossible to write truly generic code. For example, it seems to be impossible to write a truly generic
6
1306
by: Zoran Stipanicev | last post by:
Hi! I've changed the code to use Apply instead of operator() and now I get this errors: (1) left of '.Apply' must have class/struct/union (2) left of '.GetRowNum' must have class/struct/union (3) 'return' : cannot convert from 'Expression<Lhs,Rhs>' to 'Expression<Lhs,Rhs>' Cannot copy construct struct 'Expression<Lhs,Rhs>' due to ambiguous copy constructors or no available copy constructor
7
369
by: veganders | last post by:
When making a general matrix class I came across the following situation that I don't have an explanation for. For some friend functions I have to declare them in advance while other works anyway. Could anyone care to explain why I have to do it in this way? Best regards, Anders Code follows below
29
2937
by: Dexter | last post by:
This Java based utility may be invoked from Java code to parse mathematical expressions. It is useful for programmers developing calculators, graphing utilities or other math related programs. Download for free at http://www.thinkanddone.com/prog/java/parser.html If the above link does not work try http://www.britishcomputercolleges.com/prog/java/parser2.html
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.