Post allocating vectors?

M

mlt

I am trying to do the following statement using std::vector and
ublas::vector:

boost_vector vec1(3);
vec1(0) = 1;
vec1(1) = 2;
vec1(2) = 3;
P[0][0] = P[0][0] + vec1;

But making the read from P[0][0] before assigning to P[0][0] gives an error.

This is how the code is called:



typedef boost::numeric::ublas::vector<double> boost_vector;
std::vector< std::vector<boost_vector > > P;
testUblas(P);

// where:

void testUblas(std::vector< std::vector<boost_vector > > & P )
{
int rows = 3;
P.resize(rows);
for (int j=0; j<rows; j++)
{
P[j].resize(3);
}

boost_vector vec1(3);
vec1(0) = 1;
vec1(1) = 2;
vec1(2) = 3;
P[0][0] = P[0][0] + vec1; // Error

}

I have tried: P[j].clear() in the initializer loop but it does not help. How
do I make a correct initialzation of P after passing it to testUblas?
 
A

Alf P. Steinbach

* mlt:
I am trying to do the following statement using std::vector and
ublas::vector:

boost_vector vec1(3);
vec1(0) = 1;
vec1(1) = 2;
vec1(2) = 3;
P[0][0] = P[0][0] + vec1;

But making the read from P[0][0] before assigning to P[0][0] gives an
error.

You're lucky that it's detected.

It's not about reading before assigning.

It is about accessing an array position that does not exist at all.

This is how the code is called:

typedef boost::numeric::ublas::vector<double> boost_vector;
std::vector< std::vector<boost_vector > > P;

Here P is empty. There is no index 0.

testUblas(P);

// where:

void testUblas(std::vector< std::vector<boost_vector > > & P )
{
int rows = 3;
P.resize(rows);

P now has 3 elements each of which is an empty vector.

for (int j=0; j<rows; j++)
{
P[j].resize(3);
}

boost_vector vec1(3);
vec1(0) = 1;
vec1(1) = 2;
vec1(2) = 3;
P[0][0] = P[0][0] + vec1; // Error

}

I have tried: P[j].clear() in the initializer loop but it does not help.
How do I make a correct initialzation of P after passing it to testUblas?

Depends what you want. P has three dimensions. If it is to carry any data
(except the number of dimensions and each dimension's size) then all three
dimensions need to be of non-zero size. The number of data elements is D1*D2*D3
where Di is the size of dimension i. Any of those is 0, you have no data.


Cheers & hth.,

- Alf
 
M

mlt

Alf P. Steinbach said:
* mlt:
I am trying to do the following statement using std::vector and
ublas::vector:

boost_vector vec1(3);
vec1(0) = 1;
vec1(1) = 2;
vec1(2) = 3;
P[0][0] = P[0][0] + vec1;

But making the read from P[0][0] before assigning to P[0][0] gives an
error.

You're lucky that it's detected.

It's not about reading before assigning.

It is about accessing an array position that does not exist at all.

This is how the code is called:

typedef boost::numeric::ublas::vector<double> boost_vector;
std::vector< std::vector<boost_vector > > P;

Here P is empty. There is no index 0.

testUblas(P);

// where:

void testUblas(std::vector< std::vector<boost_vector > > & P )
{
int rows = 3;
P.resize(rows);

P now has 3 elements each of which is an empty vector.

for (int j=0; j<rows; j++)
{
P[j].resize(3);
}

boost_vector vec1(3);
vec1(0) = 1;
vec1(1) = 2;
vec1(2) = 3;
P[0][0] = P[0][0] + vec1; // Error

}

I have tried: P[j].clear() in the initializer loop but it does not help.
How do I make a correct initialzation of P after passing it to testUblas?

Depends what you want. P has three dimensions. If it is to carry any data
(except the number of dimensions and each dimension's size) then all three
dimensions need to be of non-zero size. The number of data elements is
D1*D2*D3 where Di is the size of dimension i. Any of those is 0, you have
no data.


Ok I now use this kind of initialization in the function that get a
reference to a

std::vector< std::vector<boost_vector > >

type:



int rows = 3;
int columns = 3;
int dim = 3;
P.resize(rows);
for (int i=0; i<rows; i++)
{
P.resize(columns);
for (int j=0; j<columns; j++ )
{
vector_type temp(dim);
P[j] = temp;
}

}


But I thought there would be an easier way to do this eventhough its done on
a reference.
 

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,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top