questions regarding class

X

xuatla

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::eek: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=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
 
K

Kai-Uwe Bux

xuatla said:
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
 
X

xuatla

Kai-Uwe Bux said:
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
 

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,265
Latest member
TodLarocca

Latest Threads

Top