473,549 Members | 2,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is it possible to create matrixes with vector <vector <double >> ?

Is it possible to create matrixes with vector <vector <double >> ? If
it is possible which is the element m23 ?
You excuse but I am not an expert

Thanks
ROB
Sep 24 '05 #1
14 2642
Assuming that you mean 4x4 matrices as in "Direct3D" or "OpenGL" (the
m23 is reason for the assumption), yes, you can, but no, you shouldn't.

Here's alternative you might want to look into further:

class matrix4x4
{
double m[4][4];
// TODO: writing accessors, constructor, et cetera left as
// exercise to the reader :)
};

If you want to be more generic, maybe something like this:

template <typename scalar, int xsize, int ysize>
class matrix
{
scalar m[xsize][ysize];
// same exercise as above..
};

What's is it that you really want to achieve? Above is just stab in the
dark which is kinda gay. Help me, to help you... (where is this quote
from, anyone? :)

Sep 24 '05 #2

"persenaama " <ju***@liimatta .org> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.com...
Assuming that you mean 4x4 matrices as in "Direct3D" or "OpenGL" (the
m23 is reason for the assumption), yes, you can, but no, you shouldn't.

Here's alternative you might want to look into further:

class matrix4x4
{
double m[4][4];
// TODO: writing accessors, constructor, et cetera left as
// exercise to the reader :)
};

If you want to be more generic, maybe something like this:

template <typename scalar, int xsize, int ysize>
class matrix
{
scalar m[xsize][ysize];
// same exercise as above..
};

What's is it that you really want to achieve? Above is just stab in the
dark which is kinda gay. Help me, to help you... (where is this quote
from, anyone? :)


I may disagree with the template approach listed above. While template helps
produce flexible design, making the dimension of a matrix bundled with type
at compile time reduces runtime flexibility.

What if you need a matrix of dimension only known at runtime?

My approach would be:

template <typename ElementT, typename AllocatorT>
class matrix
{
public:
typedef ElementT element_type;
typedef std::size_t size_type;

private:
std::vector<ele ment_type, AllocatorT> repr;
size_type num_rows;
size_type num_cols;

public:
matrix(size_typ e r, size_type c);
size_type row_size(void);
size_type col_size(void);
element_type& operator() (size_type r, size_type c);
const element_type& operator()(size _type r, size_type c) const;

// ... etc
};
Sep 24 '05 #3
No problem, I want to point out that I assumed D3D/GL or similiar
purpose (however the double is dubious in that context :)

Sep 24 '05 #4
LumisROB wrote:
Is it possible to create matrixes with vector <vector <double >> ?
Not quite, but

vector <vector <double > >
^^^
would work.

If it is possible which is the element m23 ?


Do you mean, the entry in row 2 and column 3? Well, that depends on whether
you think of your vector< vector< double > > as a vector of row-vectors
vector of column vectors.
Having said this, let me add:

Don't roll your own matrix 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
Sep 24 '05 #5
On Sat, 24 Sep 2005 10:05:24 GMT, LumisROB
<lu************ **@yahoo.com> wrote:

For everobody:

Thanks for the help!!!

I apologize you, I have been a little clear but my goodness.. I didn't
think could enter so many other considerations.

We see better thing it would serve me:

1) I would like to find a basic solution (simple for a beginner of
the c++) of the type double m [..] [..] that however allows me to
change dimension of the array during the execution of the program
2) I have to overcome the limitation of the length of the arrays that
is of 2^31 (also in the environment to 64 bit in which I operate. This
is due to the fact that the code has to talk with Matlab that has this
limitation). Someone has told me that for instance making double
m[2^6][2^6] I succeed in revolving the problem (in an operating system
to 64 bit even if the compiler is to 32 bit. is it true?)

For Kay-Uwe Bux

Thanks of the very beautiful panning indeed. The problem is that some
classes result to have the 2^31 limitation that I have said above.
Besides I have to make simple algebraic calculations. Certainly, if I
didn't have that limitation I would have used some librariez of those
from you pointed out me
Thanks for your time
ROB
Sep 24 '05 #6
LumisROB wrote:
On Sat, 24 Sep 2005 10:05:24 GMT, LumisROB
<lu************ **@yahoo.com> wrote:

For everobody:

Thanks for the help!!!

I apologize you, I have been a little clear but my goodness.. I didn't
think could enter so many other considerations.

We see better thing it would serve me:

1) I would like to find a basic solution (simple for a beginner of
the c++) of the type double m [..] [..] that however allows me to
change dimension of the array during the execution of the program
2) I have to overcome the limitation of the length of the arrays that
is of 2^31 (also in the environment to 64 bit in which I operate. This
is due to the fact that the code has to talk with Matlab that has this
limitation). Someone has told me that for instance making double
m[2^6][2^6] I succeed in revolving the problem (in an operating system
to 64 bit even if the compiler is to 32 bit. is it true?)

For Kay-Uwe Bux

Thanks of the very beautiful panning indeed. The problem is that some
classes result to have the 2^31 limitation that I have said above.
Besides I have to make simple algebraic calculations. Certainly, if I
didn't have that limitation I would have used some librariez of those
from you pointed out me


Hm, looks like you absolutely need to roll your own code.

May I ask what size of matrices you expect to typically run into? Also, do
your matrices have a special shape (like upper triangular) or are most of
the entries 0 for some other reason? I am asking since using some
simple-minded allocation strategy, 2^31 doubles would take on the order of
10GB of memory. And even if you can host that many entries, computations
will take forever. Thus, I am worried that some "basic solution" might not
be good enough for you.
Best

Kai-Uwe Bux

ps.: As for general advice on designing a matrix class, you might consider
not using m[row][col] as the way to access coefficients. If you overload

double & operator() ( size_type row, size_type col )

and

double const & operator() ( size_type row, size_type col ) const

you have greater flexibility in designing the internals of your class (this
can pay off for sparse matrix optimizations or special shape matrices). In
this case, you would use

m(row,col)

to access coefficients.

Sep 24 '05 #7
On Sat, 24 Sep 2005 10:38:58 -0400, Kai-Uwe Bux <jk********@gmx .net>
wrote:
I am asking since using some
simple-minded allocation strategy, 2^31 doubles would take on the order of
10GB of memory. And even if you can host that many entries, computations
will take forever. Thus, I am worried that some "basic solution" might not
be good enough for you.

The maximum dimension is a matrix of the type double m[10^5][10^5] to
intend us
Then 10^10 elements of double. Unfortunately the matrix has to be
dense
The most serious calculation that I have to do is a dot scalar product
among two lines.However I have to make many non sequential accesses to
the elements
ps.: As for general advice on designing a matrix class, you might consider
not using m[row][col] as the way to access coefficients. If you overload

double & operator() ( size_type row, size_type col )

and

double const & operator() ( size_type row, size_type col ) const

you have greater flexibility in designing the internals of your class (this
can pay off for sparse matrix optimizations or special shape matrices). In
this case, you would use

m(row,col)

to access coefficients.


Very beautiful.... I like more always the c++ for me that arrival from
the fortran..... unfortunately however the things are complicated for
me (I am a beginner in the c++) and I have little time to resolve this
problem... I would Be already happy to find a solution that allows me
to reach the finishing line. I had tried to resolve with a vector
<double> v without dimensions and despite it was a little efficient in
comparison to vector <double> v(100000000) (total of elements 10^8
--> ) it suited me if there had not been that accursed limitation -->

vector <double> v(100000000) :
time of creation 30 sec (this speed has surprised me and therefore I
thought... perhaps naïvely, to build a vector <vector <double >> and
to brightly resolve..but from what seems me to understand it is not an
easy assignment it is not true?

Heartful thanks

ROB

Sep 24 '05 #8
On Sat, 24 Sep 2005 21:27:23 +1000, "benben" <moc.liamtoh@hg nohneb
read backward> wrote:

My approach would be:

template <typename ElementT, typename AllocatorT>
class matrix
{
public:
typedef ElementT element_type;
typedef std::size_t size_type;

private:
std::vector<ele ment_type, AllocatorT> repr;
size_type num_rows;
size_type num_cols;

public:
matrix(size_typ e r, size_type c);
size_type row_size(void);
size_type col_size(void);
element_type& operator() (size_type r, size_type c);
const element_type& operator()(size _type r, size_type c) const;

// ... etc
};

Indeed sublime.... but if enter in such a forest.. I am sure that I go
out consumes only from there after few months...... I arrive from the
fortran and I am moving the first footsteps in the c++ and... I assure
you that it is not so simple..

Heartful Thanks to you and to parsenaama

ROB
Sep 24 '05 #9
LumisROB wrote:
On Sat, 24 Sep 2005 10:38:58 -0400, Kai-Uwe Bux <jk********@gmx .net>
wrote:
I am asking since using some
simple-minded allocation strategy, 2^31 doubles would take on the order of
10GB of memory. And even if you can host that many entries, computations
will take forever. Thus, I am worried that some "basic solution" might not
be good enough for you.


The maximum dimension is a matrix of the type double m[10^5][10^5] to
intend us
Then 10^10 elements of double. Unfortunately the matrix has to be
dense
The most serious calculation that I have to do is a dot scalar product
among two lines.However I have to make many non sequential accesses to
the elements


Now that's a lot of entries. You might run into serious trouble here. There
are different limitations that you might face. For one, allocation of a
single block of that size is probably not supported. However, there might
be an even worse problem: the total size of the heap might be insufficient.
Also, if your compiler thinks that pointers are 32 bits, then there will be
no way to address 10^10 points in memory since 2^32 < 10^10. May I ask,
what platform will be running this code?
As for dense matrix implementations , there is a simple minded strategy that
avoids allocating rows*cols doubles in one big chunk:
#include <memory>

template < typename T >
struct matrix {

typedef typename std::size_t size_type;
typedef T value_type;

private:

size_type rows;
size_type cols;
typedef T* RowVector;
RowVector* data;

public:

matrix ( size_type r, size_type c, value_type t = value_type() )
: rows ( r )
, cols ( c )
, data ( new RowVector [ this->rows ] )
{
size_type rows_constructe d = 0;
try {
while ( rows_constructe d < rows ) {
this->data[ rows_constructe d ] = new value_type [ this->cols ];
for ( size_type j = 0; j < this->cols; ++ j ) {
this->data[rows_constructe d][j] = t;
}
++ rows_constructe d;
}
}
catch ( ... ) {
while ( rows_constructe d > 0 ) {
-- rows_constructe d;
delete [] this->data[ rows_constructe d ];
}
delete [] this->data;
throw;
}
}

~matrix ( void ) {
for ( size_type i = 0; i < rows; ++ i ) {
delete [] this->data[i];
}
delete [] this->data;
}

size_type row_size ( void ) const {
return ( this->rows );
}

size_type col_size ( void ) const {
return ( this->cols );
}

T & operator() ( size_type r, size_type c ) {
return( this->data[r][c] );
}

T const & operator() ( size_type r, size_type c ) const {
return( this->data[r][c] );
}

}; // class matrix<T>
This is untested code and I left out the copy constructor, and the
assignment operator. The price for allocating several blocks is a more
complicated constructor since cleaning up is more difficult. Also, you are
using a little more memory. Note that using vectors will incur additional
overhead as every vector stores its size.
Another idea would be to view a big matrix as a block matrix. But I am not
sure if that would help to circumvent the limitations that you are running
into.

Best

Kai-Uwe Bux

Sep 24 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2249
by: Dennis | last post by:
Hi I'm trying to implement a vector of vectors where find can be used to find a vector<double> in the vectors of vectors, that is hard to understand i guess. What I mean is that I got a vector foo containing vectors of the size 3. I then want to compare a vector<double> of size 3 (coord) with foo to find a sequence of elements in foo that...
2
11440
by: Pepijn Kenter | last post by:
Dear experts. I have a vector<float> and want to convert that to a vector<double>. I optimistically tried: #include <vector> #include <iostream> using namespace std; int main() {
20
17752
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; std::vector<double>::size_type size = src.size(); dest.reserve(size); for (std::vector<int>::size_type i = 0;
4
2474
by: Amit | last post by:
Hi, I was wondering how to define the + and = operator for a vector of double or float and do I need to define it explicitly? does it not get defined automatically(like copy constructor) if one does not define it explicitly? thanks, --A.
10
15636
by: bluekite2000 | last post by:
and why doesnt the standard vector have such conversion available?
7
12358
by: utab | last post by:
Dear all, I tried sth like this, compiles but segmentation fault error. In my reasoning field_values holds a vector<double> but when I tried, I understood that it is not the case :-). #include <iostream> #include <vector> using namespace std;
9
8221
by: richard_lavoie | last post by:
Hi, I have something like this: vector<floatvec1; and I want to cast it, so I use vector vec2<double= static_cast< vector<double(vec1); I always become a error: syntax error before `>' token in that line
32
3953
by: T. Crane | last post by:
Hi, I'm struggling with how to initialize a vector<vector<double>> object. I'm pulling data out of a file and storing it in the vector<vector<double>object. Because any given file will have a large amount of data, that I read off using an ifstream object, I don't want to use the push_back method because this grows the...
0
7446
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7715
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7469
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7808
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...
0
6040
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...
1
5368
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...
0
3498
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...
1
1057
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
757
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...

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.