simple matrix

E

Erik Borgstr?m

Hi,

I simply want to use simple matrices of ints or doubles in C++ and I
want all the good:

1) be able to use double-index, e.g. m[j]
2) not have to use pointers at all
3) do it fast

I know I could do 1&3 with ordinary int[][], but then if I want to
give my matrix as a parameter, I have to use pointers, and I find the
handling of pointers a bit difficult, so I want to avoid it.

1&2 could be done with vector<vector<int> >, but that's slow, isn't
it?

2&3 could be done with valarray, but they don't let you write m[j]
or do they?

Also, I don't want to write a class on my own, because I have to make
a bugfree and fast program, and so I prefer using a well-documented
standard class.

Is there a way to accomplish all this? Or do I ask for too much? Or
maybe vectors are not so slow after all?

thanks for your patience,
Erik
 
V

Victor Bazarov

Erik Borgstr?m said:
I simply want to use simple matrices of ints or doubles in C++ and I
want all the good:

1) be able to use double-index, e.g. m[j]
2) not have to use pointers at all
3) do it fast

I know I could do 1&3 with ordinary int[][], but then if I want to
give my matrix as a parameter, I have to use pointers, and I find the
handling of pointers a bit difficult, so I want to avoid it.


What you're looking for is a simple wrapper for the array with which
you're already familiar. Just wrap your 'ordinary int[][]' into
a class and pass it around as much as you want.
1&2 could be done with vector<vector<int> >, but that's slow, isn't
it?

Slow compared to what? One man's "slow" is another man's "thorough",
besides, vector wasn't designed for making matrices, really.
2&3 could be done with valarray, but they don't let you write m[j]
or do they?


Have an array of valarrays.
Also, I don't want to write a class on my own, because I have to make
a bugfree and fast program, and so I prefer using a well-documented
standard class.

So, if there isn't any well-documented standard class for some
problems (like FFT, graphs, etc.) who's going to make them? Do
you call yourself a programmer?
Is there a way to accomplish all this?

Of course there is. There is a way to accomplish anything. It's
all in the wrist, really.
Or do I ask for too much?

Of yourself or of the standard library?
Or
maybe vectors are not so slow after all?

Slow is (a) subjective and (b) should not be taken out of context.

V
 
J

Jeff Schwab

Erik said:
Hi,

I simply want to use simple matrices of ints or doubles in C++ and I
want all the good:

1) be able to use double-index, e.g. m[j]
2) not have to use pointers at all
3) do it fast

I know I could do 1&3 with ordinary int[][], but then if I want to
give my matrix as a parameter, I have to use pointers, and I find the
handling of pointers a bit difficult, so I want to avoid it.


You could wrap the int[][] in a struct and pass references.
1&2 could be done with vector<vector<int> >, but that's slow, isn't
it?

It depends what you're doing. I wrote a program involving matrix
manipulation based on vectors of vectors, and one of the bottlenecks has
been working with the vector::iterators. Accessing the values is ok,
it's the incrementing and comparison of the iterators that's been slow.
2&3 could be done with valarray, but they don't let you write m[j]
or do they?


Check out the std::slice class. I believe it provides the abstraction
you want.
Also, I don't want to write a class on my own, because I have to make
a bugfree and fast program, and so I prefer using a well-documented
standard class.

Right on, brother.
 
G

Gary

Erik Borgstr?m said:
Hi,

I simply want to use simple matrices of ints or doubles in C++ and I
want all the good:

1) be able to use double-index, e.g. m[j]
2) not have to use pointers at all
3) do it fast

I know I could do 1&3 with ordinary int[][], but then if I want to
give my matrix as a parameter, I have to use pointers, and I find the
handling of pointers a bit difficult, so I want to avoid it.

1&2 could be done with vector<vector<int> >, but that's slow, isn't
it?

2&3 could be done with valarray, but they don't let you write m[j]
or do they?

Also, I don't want to write a class on my own, because I have to make
a bugfree and fast program, and so I prefer using a well-documented
standard class.

Is there a way to accomplish all this? Or do I ask for too much? Or
maybe vectors are not so slow after all?


Besides rolling your own (which you will completely understand) check out
apmatrix class and other available implementations.
All are non-standard, of course.
 
G

Gianni Mariani

Erik said:
Hi,

I simply want to use simple matrices of ints or doubles in C++ and I
want all the good:

1) be able to use double-index, e.g. m[j]
2) not have to use pointers at all
3) do it fast

I know I could do 1&3 with ordinary int[][], but then if I want to
give my matrix as a parameter, I have to use pointers, and I find the
handling of pointers a bit difficult, so I want to avoid it.

1&2 could be done with vector<vector<int> >, but that's slow, isn't
it?

2&3 could be done with valarray, but they don't let you write m[j]
or do they?

Also, I don't want to write a class on my own, because I have to make
a bugfree and fast program, and so I prefer using a well-documented
standard class.


If you have a special need, there is no reason to avoid it writing your
own class. The WORST thing is to try to fit a square peg in a round hole.
Is there a way to accomplish all this? Or do I ask for too much? Or
maybe vectors are not so slow after all?

To do 1), you need to overload operator[] if you want anything better
than an plain array. You can make use of all the work in std::vector
(see below).

However, I reccomend that you not fear pointers. They are often the
most efficient way to manage complex data structures.


#include <vector>

template <typename w_elem_type>
class matrix
{
public:
typedef int t_Size;

t_Size m_columns;
t_Size m_rows;

std::vector<w_elem_type> m_data;

matrix( t_Size i_columns = 0, t_Size i_rows = 0 )
: m_columns( i_columns ),
m_rows( i_rows ),
m_data( i_columns * i_rows )
{
}

w_elem_type * operator[]( t_Size i_index )
{
return & ( m_data[ i_index * m_rows ] );
}

template <typename w_Type, int w_columns, int w_rows>
matrix( const w_Type (&i_array)[w_columns][w_rows] )
: m_columns( w_columns ),
m_rows( w_rows ),
m_data( & (i_array[0][0]), & (i_array[w_columns][w_rows]) )
{
}

};



#include <iostream>

double array[3][4] = {
{ 1.0, 2.0, 3.3, 4.4 },
{ 1.0, 2.0, 3.3, 4.4 },
{ 1.0, 2.0, 3.3, 4.5 },
};

int main()
{
matrix<float> mat1( 3, 4 );
matrix<float> mat2;
matrix<float> mat3( array );

mat2 = mat3;

std::cout << mat2[2][3] << "\n";
}
 
G

Gianni Mariani

Gianni said:
Erik Borgstr?m wrote:

oops - a bug
template <typename w_Type, int w_columns, int w_rows>
matrix( const w_Type (&i_array)[w_columns][w_rows] )
: m_columns( w_columns ),
m_rows( w_rows ),
m_data( & (i_array[0][0]), & (i_array[w_columns][w_rows]) )

m_data( & (i_array[0][0]), & (i_array[w_columns-1][w_rows]) )

- fixed.
 
E

Erik Borgstr?m

Hi,

Thanks for all the help so far. To be more precise:

My matrices will be used only as constant containers. Let's say I want
to compute the product of lots of pairs of them, even though I'm
really computing something else. The point is that I need to use
m[j] a lot of times to acces the value.

Q: how many times slower is vector<vector<int> > than int[][] when I
write:

int a = m[j];

/erik
 
C

Clark Cox

Hi,

Thanks for all the help so far. To be more precise:

My matrices will be used only as constant containers. Let's say I want
to compute the product of lots of pairs of them, even though I'm
really computing something else. The point is that I need to use
m[j] a lot of times to acces the value.

Q: how many times slower is vector<vector<int> > than int[][] when I
write:

int a = m[j];


It might be a million times slower, it might be faster, there is no way
to say. The answer is likely different for every platform.
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top