473,670 Members | 2,646 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<i nt> >, 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 2089
"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<i nt> >, 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<i nt> >, 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::iterato rs. 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.goo gle.com...
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<i nt> >, 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<i nt> >, 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_e lem_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<i nt> > 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<i nt> > 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
3326
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 my particular application. One of the advantages is that the _compiler_ can force inner matrix dimensions used in multiplication to agree. A _complie-time_ error will be triggered if you write A * B and the number of coluns in A does not equal the...
1
1689
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; ++k) { cout << matrix << " "; }
1
1689
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* cc, int lnt) { //cc += lnt; while(lnt--) { *cc++ = *bb++ * *aa++; } }
2
1830
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++) { Matrixj]=1;
3
3089
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() { // Write to console
14
6211
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 exponential). Includes: - trace - transpose - conjugate - determinant - inverse - eigenvectors/values (for symmetric matrices)
2
8555
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 numeric because this is a personal programming exercise for me in creating an operational class in pure python for some *basic* matrix operations). 1. Please take a look at the __init__ function and comment on the initialization of the list data...
0
2806
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 Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY...
2
2660
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 matrix{ public: matrix();
0
8471
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8661
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7421
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6216
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4213
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4393
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2802
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1795
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.