Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > questions regarding class

Reply
Thread Tools

questions regarding class

 
 
xuatla
Guest
Posts: n/a
 
      09-16-2005
Hi,

I am not sure whether I describe the subject correctly.

What I want to do is for a matrix class

---------------------------
class CMatrix
{
private:
size_t m, n;
double **nodes;

private:
bool checkSize(...); // check size and initialization if needed.

public:
CMatrix operator + (const CMatrix& m1);
CMatrix operator += (const CMatrix& m1);
......
}

CMatrix CMatrix:perator+(const CMatrix& m1)
{
CMatrix res(m1);
for ....

return res;
}
-----------------------------

If I need to perform a lot of addition and use + (discarding += first,
since I want to write something like a+b+c-2*d...), then the program
will do a lot of memory allocation and release (for instantiation of
"res" in "+"). I want to know:

1. is the instantiation of class fast or not? (compared with some math
operations, e.g., one + between two double values).
for memory allocation, I use:
nodes = new double*[m];
for (size_t i=0; i<m; i++) nodes[i]=new double[n];

2. is it good or not to set up a global variable for the above "res" so
that I can skip the instantiation part? (suppose the memory size is not
a problem)

3. how to do (2)? I think about static member:
static CMatrix _mattmp;
do I need to put this inside the class or outside?

Thanks a lot!

Regards,
X
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      09-16-2005
xuatla wrote:

> Hi,
>
> I am not sure whether I describe the subject correctly.
>
> What I want to do is for a matrix class

[snip]

Don't roll your own code unless you absolutely have to.

a) Numerical linear algebra is *hard*: algorithms that you might recall from
your college linear algebra class routinely run into trouble because
numbers on a computer like double simply do not obey nice mathematical
rules. (Small determinants or bad choices for pivots can completely
obliterate a matrix inversion).

b) It is difficult to get this kind of code right, and when you have it
right, it is very hard to make it efficient. The state of the art uses
heavy templating machinery to avoid unnecessary copy constructions and help
the compiler unroll those many loops that you are running into.

c) There is no need for you to reinvent the wheel. Google for C++ linear
algebra libraries or visit http://www.oonumerics.org.


Best

Kai-Uwe Bux

 
Reply With Quote
 
 
 
 
xuatla
Guest
Posts: n/a
 
      09-16-2005
Kai-Uwe Bux wrote:
> xuatla wrote:
>
>
>>Hi,
>>
>>I am not sure whether I describe the subject correctly.
>>
>>What I want to do is for a matrix class

>
> [snip]
>
> Don't roll your own code unless you absolutely have to.
>
> a) Numerical linear algebra is *hard*: algorithms that you might recall from
> your college linear algebra class routinely run into trouble because
> numbers on a computer like double simply do not obey nice mathematical
> rules. (Small determinants or bad choices for pivots can completely
> obliterate a matrix inversion).
>
> b) It is difficult to get this kind of code right, and when you have it
> right, it is very hard to make it efficient. The state of the art uses
> heavy templating machinery to avoid unnecessary copy constructions and help
> the compiler unroll those many loops that you are running into.
>
> c) There is no need for you to reinvent the wheel. Google for C++ linear
> algebra libraries or visit http://www.oonumerics.org.
>
>
> Best
>
> Kai-Uwe Bux
>

Thank you very much for the answer!

X
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A couple of questions regarding class design Michael W. Ryder Ruby 34 04-26-2008 12:40 PM
Query regarding calling parent's class method which is overridden in child class??? parkarumesh@gmail.com Java 0 04-26-2007 09:24 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Questions on Canon 300D and etc. questions regarding digital photography Progressiveabsolution Digital Photography 12 03-24-2005 05:18 PM
Newbie questions - Couple of VC++ questions regarding dlls and VB6 Ali Syed C Programming 3 10-13-2004 10:15 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57