By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,974 Members | 1,834 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,974 IT Pros & Developers. It's quick & easy.

simple matrix

P: n/a
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[i][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[i][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
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Erik Borgstr?m" <er************@yahoo.se> wrote...
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[i][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[i][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
Jul 22 '05 #2

P: n/a
Erik Borgstr?m wrote:
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[i][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[i][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.
Is there a way to accomplish all this? Or do I ask for too much? Or
maybe vectors are not so slow after all?


Jul 22 '05 #3

P: n/a
"Erik Borgstr?m" <er************@yahoo.se> wrote in message
news:9e**************************@posting.google.c om...
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[i][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[i][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.
--
Gary
Jul 22 '05 #4

P: n/a
Erik Borgstr?m wrote:
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[i][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[i][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";
}


Jul 22 '05 #5

P: n/a
Gianni Mariani wrote:
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.

Jul 22 '05 #6

P: n/a
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[i][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[i][j];

/erik
Jul 22 '05 #7

P: n/a
In article <9e**************************@posting.google.com >,
er************@yahoo.se (Erik Borgstr?m) wrote:
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[i][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[i][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.

Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.