468,785 Members | 1,645 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,785 developers. It's quick & easy.

2 straight forward Questions

Righty,
1: Is there a standard library that contain matrices and complex numbers. I
need to find eigen values of a 3x3 matrix.

2: Is there a way of getting the pointer to the start of an array from the
data stored in a std vector. I load loads of floats into a vector at the
moment to store vertex infomration for openGL, but to render them, I need to
pass a pointer to the beginning of the array to OpenGL, not a vector. At the
moment I'm just compying them into another array, but this seems like a bit
of a waste:

vector<float> vertices;

//Copy data into 'vertices'

Then before rendering I have to :
float* pVertexArray = new float[vertices.size()];
for(int i=0;i< vertices.size();i++) pVertexArray[i] = vertices[i];
But this seems silly!

Thanks Mike.
Jul 22 '05 #1
11 1363

"Michael" <sl***********@hotmail.com> wrote in message
news:c7**********@hercules.btinternet.com...
Righty,
1: Is there a standard library that contain matrices and complex numbers. I need to find eigen values of a 3x3 matrix.
There are complex numbers in the header file <complex>

std::complex<double> c(1.0, 2.0);
std::complex<double> d = c*c;

etc.

There are no matrices as such. But you have a lot of options. You could
write a simple class that does all you need (I think that would be best). Or
you could use a vector of vectors. Of you could use valarray.

2: Is there a way of getting the pointer to the start of an array from the data stored in a std vector.
Yes.
I load loads of floats into a vector at the
moment to store vertex infomration for openGL, but to render them, I need to pass a pointer to the beginning of the array to OpenGL, not a vector. At the moment I'm just compying them into another array, but this seems like a bit of a waste:

vector<float> vertices;

//Copy data into 'vertices'

Then before rendering I have to :
float* pVertexArray = new float[vertices.size()];
for(int i=0;i< vertices.size();i++) pVertexArray[i] = vertices[i];
But this seems silly!


Right, just do this

some_function(&vectices[0]);

john
Jul 22 '05 #2
Michael wrote:
Righty,
1: Is there a standard library that contain matrices
No, but there are plenty of third-party libraries available, many of
them open source. Try Google.
and complex numbers.
#include <complex>
I need to find eigen values of a 3x3 matrix.

2: Is there a way of getting the pointer to the start of an array from the
data stored in a std vector.


&v[0]; // Not sure the standard mandates this, but I think there's a DR.
Jul 22 '05 #3

"Michael" <sl***********@hotmail.com> wrote in message
news:c7**********@hercules.btinternet.com...
Righty,
1: Is there a standard library that contain matrices and complex numbers. I
need to find eigen values of a 3x3 matrix.
There is std::complex class (#include <complex>)
No class as such for matrices but you could take a look at valarray.
2: Is there a way of getting the pointer to the start of an array from the
data stored in a std vector. I load loads of floats into a vector at the
moment to store vertex infomration for openGL, but to render them, I need to
pass a pointer to the beginning of the array to OpenGL, not a vector. At the
moment I'm just compying them into another array, but this seems like a bit
of a waste:

vector<float> vertices;

//Copy data into 'vertices'

Then before rendering I have to :
float* pVertexArray = new float[vertices.size()];
for(int i=0;i< vertices.size();i++) pVertexArray[i] = vertices[i];


&vec[0] will give you the pointer to the first element.

-Sharad
Jul 22 '05 #4
Jeff Schwab wrote:
Michael wrote:
Is there a way of getting the pointer to the start of an array
from the data stored in a std vector.

&v[0]; // Not sure the standard mandates this, but I think there's a DR.


OK, it's (23.2.4.1) in the 2003 normative document.
Jul 22 '05 #5
Guys does this not violate the idea of encapsulation, you are making
assumptions about the implmentation that the data is stored contiguosly and
tightly packed?? Or is it just OK cos thats what the vector class is defined
as?
Mike
"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:c7************@ID-221354.news.uni-berlin.de...

"Michael" <sl***********@hotmail.com> wrote in message
news:c7**********@hercules.btinternet.com...
Righty,
1: Is there a standard library that contain matrices and complex numbers. I need to find eigen values of a 3x3 matrix.


There is std::complex class (#include <complex>)
No class as such for matrices but you could take a look at valarray.
2: Is there a way of getting the pointer to the start of an array from the data stored in a std vector. I load loads of floats into a vector at the
moment to store vertex infomration for openGL, but to render them, I need to pass a pointer to the beginning of the array to OpenGL, not a vector. At the moment I'm just compying them into another array, but this seems like a bit of a waste:

vector<float> vertices;

//Copy data into 'vertices'

Then before rendering I have to :
float* pVertexArray = new float[vertices.size()];
for(int i=0;i< vertices.size();i++) pVertexArray[i] = vertices[i];


&vec[0] will give you the pointer to the first element.

-Sharad

Jul 22 '05 #6

"Michael" <sl***********@hotmail.com> wrote in message
news:c7**********@hercules.btinternet.com...
Guys does this not violate the idea of encapsulation, you are making
assumptions about the implmentation that the data is stored contiguosly and tightly packed?? Or is it just OK cos thats what the vector class is defined as?


The standard guarantees that the data is stored contiguously.

john

Jul 22 '05 #7
"John Harrison" <jo*************@hotmail.com> wrote in message
news:c759t5$ilkh5$1@ID-
"Michael" <sl***********@hotmail.com> wrote in message
I need to
pass a pointer to the beginning of the array to OpenGL, not a vector

some_function(&vectices[0]);


This will fail for a vector of zero elements. If you know for certain
you'll have data, fine. Otherwise try

some_function(vectices.size() ? &vectices[0] : NULL);
Jul 22 '05 #8
Siemel Naran wrote:
"John Harrison" <jo*************@hotmail.com> wrote in message
news:c759t5$ilkh5$1@ID-
"Michael" <sl***********@hotmail.com> wrote in message


I need to
pass a pointer to the beginning of the array to OpenGL, not a vector


some_function(&vectices[0]);

This will fail for a vector of zero elements. If you know for certain
you'll have data, fine. Otherwise try

some_function(vectices.size() ? &vectices[0] : NULL);


Right-o. Another special case (other than an empty vector) is a
specialization like std::vector<bool>. I'm not sure what happens in
that case.
Jul 22 '05 #9

"Michael" <sl***********@hotmail.com> wrote in message
news:c7**********@hercules.btinternet.com...
Guys does this not violate the idea of encapsulation, you are making
assumptions about the implmentation that the data is stored contiguosly and
tightly packed?? Or is it just OK cos thats what the vector class is defined
as?


The 2003 "technical corrigendum" 23.2.4[1] says "The elements of a vector are
stored contiguously".

Jul 22 '05 #10
"Jeff Schwab" <je******@comcast.net> wrote in message
news:y9ydnVicnuTrdQvdRVn-
Siemel Naran

some_function(vectices.size() ? &vectices[0] : NULL);


Right-o. Another special case (other than an empty vector) is a
specialization like std::vector<bool>. I'm not sure what happens in
that case.


So just wondering, is there a way to turn off the specialization of
vector<bool>? It seems to intefere with generic code. In vector<T> people
are used to writing T* and T&, now they have to say vector<T>::reference.

As for your question, I look in the standard and there is no member function
vector::reference::operator&, and so &v[0] returns a vector<bool>::reference
*.

One of my smart reference classes has X::reference::operator& return a
counted_ptr<X::reference>, which seems safer.
Jul 22 '05 #11
In article <9d********************@bgtnsc04-news.ops.worldnet.att.net>,
"Siemel Naran" <Si*********@REMOVE.att.net> wrote:
As for your question, I look in the standard and there is no member function
vector::reference::operator&, and so &v[0] returns a vector<bool>::reference
*.

One of my smart reference classes has X::reference::operator& return a
counted_ptr<X::reference>, which seems safer.


<nod> That seems to be one of the better uses for overloading
operator&. I've been experimenting with
vector<bool>::reference::operator& returning a vector<bool>::iterator.

-Howard
Jul 22 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Steven T. Hatton | last post: by
5 posts views Thread by John Gabriele | last post: by
5 posts views Thread by Todd Huish | last post: by
1 post views Thread by doodoosy | last post: by
7 posts views Thread by RedLars | last post: by
5 posts views Thread by Markus Dehmann | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.