2 straight forward Questions

M

Michael

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 = vertices;


But this seems silly!

Thanks Mike.
 
J

John Harrison

Michael said:
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 = vertices;


But this seems silly!


Right, just do this

some_function(&vectices[0]);

john
 
J

Jeff Schwab

Michael said:
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 said:
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.
 
S

Sharad Kala

Michael said:
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 = vertices;


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

-Sharad
 
J

Jeff Schwab

Jeff said:
Michael said:
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.
 
M

Michael

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 said:
Michael said:
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 = vertices;


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

-Sharad
 
J

John Harrison

Michael said:
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
 
S

Siemel Naran

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);
 
J

Jeff Schwab

Siemel said:
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.
 
S

Sharad Kala

Michael said:
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".
 
S

Siemel Naran

Jeff Schwab said:
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::eek:perator&, and so &v[0] returns a vector<bool>::reference
*.

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

Howard Hinnant

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

One of my smart reference classes has X::reference::eek:perator& 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::eek:perator& returning a vector<bool>::iterator.

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top