valarray

O

Oliver Block

I have a (9x9) valarray

std::valarray<int> va(81);
va = 0; // fil all elements with zeros

I am taking row slices like that:

std::slice_array<int> slr = va[std::slice(r-1,9,9)]; // r = row; a
value between 1 and 9
std::valarray<int> row = slr;

and column slices like that:

std::slice_array<int> slc = va[std::slice((c-1)*9, 9, 1)]; // c =
col; a value between 1 and 9
std::valarray<int> col = slc;

which does work without any problem.

But when I try to extract a subarray:

size_t len[] = {3,3};
size_t dist[] = {9, 1};
std::valarray<size_t> lens(len, 2);
std::valarray<size_t> dists(dist, 2);

int st_r = ((r-1)/3)*3+1; // r (s.o.)
int st_c = ((c-1)/3)*3+1; // c (s.o.)
int st_i = rc2i( st_r, st_c ); // int rc2i(int row, int col) {
return (col-1) * 9 + (row-1); }

std::gslice_array<int> gslsa = va[std::gslice(st_i, lens, dists)];
std::valarray<int> subarray = gslsa;

the first element is always the value of element 0 of the valarray,
even if my function rc2i calculates the correct values for st_i!

Is it likely, that there is a bug in the sources of the library or did
I just not fully understand the concept of gslice_array/gslice?

I mean I'am sure I didn't understant it completely but I don't see the
mistake I possibly make! :)

Hoping for some hints.

bye,

Oliver
 
J

John Harrison

Oliver said:
I have a (9x9) valarray

std::valarray<int> va(81);
va = 0; // fil all elements with zeros

I am taking row slices like that:

std::slice_array<int> slr = va[std::slice(r-1,9,9)]; // r = row; a
value between 1 and 9
std::valarray<int> row = slr;

and column slices like that:

std::slice_array<int> slc = va[std::slice((c-1)*9, 9, 1)]; // c =
col; a value between 1 and 9
std::valarray<int> col = slc;

which does work without any problem.

But when I try to extract a subarray:

size_t len[] = {3,3};
size_t dist[] = {9, 1};
std::valarray<size_t> lens(len, 2);
std::valarray<size_t> dists(dist, 2);

int st_r = ((r-1)/3)*3+1; // r (s.o.)
int st_c = ((c-1)/3)*3+1; // c (s.o.)
int st_i = rc2i( st_r, st_c ); // int rc2i(int row, int col) {
return (col-1) * 9 + (row-1); }

std::gslice_array<int> gslsa = va[std::gslice(st_i, lens, dists)];
std::valarray<int> subarray = gslsa;

the first element is always the value of element 0 of the valarray,
even if my function rc2i calculates the correct values for st_i!

Is it likely, that there is a bug in the sources of the library or did
I just not fully understand the concept of gslice_array/gslice?

I mean I'am sure I didn't understant it completely but I don't see the
mistake I possibly make! :)

Hoping for some hints.

Well your code worked for me. However it does have one error in it. You
are not meant to create gslice_array or slice_array objects directly.

std::valarray<int> row = va[std::slice(r-1,9,9)];

std::valarray<int> subarray = va[std::gslice(st_i, lens, dists)];

is how you should do it. Try that and see if it works.

john
 

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

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top