473,372 Members | 949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

simple matrix

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
7 2074
"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
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
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
1
by: SUPER_SOCKO | last post by:
Suppose I have a matrix M x N dimension. Normally, to print all of the elements in the matrix. I write the following codes: Suppose M = 5, N = 4: for(int j=0; j<5; ++j) { for(int k=0; k<4;...
1
by: Rohan Shah | last post by:
i am looking for some simple matrix routines: vector*matrix, matrix*matrix etc...in the form of code like the following that multiplies two vectors: void vmult(double* aa, double* bb, double*...
2
by: bluekite2000 | last post by:
I ran the following code for a square matrix whose size ranges from 1x1 to 1000x1000 start=clock(); //sequential access time measurement for (i=0;i<Msize;i++) for(j=0;j<Nsize;j++) {...
3
by: | last post by:
I am trying to compile a simple c# class in Web Matrix called howdy.cs: // Program start class public class HowdyPartner { // Main begins program execution public static void Main() { //...
14
by: Paul McGuire | last post by:
I've posted a simple Matrix class on my website as a small-footprint package for doing basic calculations on matrices up to about 10x10 in size (no theoretical limit, but performance on inverse is...
2
by: DarrenWeber | last post by:
Below is a module (matrix.py) with a class to implement some basic matrix operations on a 2D list. Some things puzzle me about the best way to do this (please don't refer to scipy, numpy and...
0
by: DarrenWeber | last post by:
# Copyright (C) 2007 Darren Lee Weber # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free...
2
by: rijaalu | last post by:
I am designing a matrix class that performs addition, multicpication, substraction and division. When ever i complie the code it shows an error. include <iostream> using namespace std; class...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.