By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,513 Members | 1,629 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,513 IT Pros & Developers. It's quick & easy.

valarray

P: n/a
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

Nov 5 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Oliver Block wrote:
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
Nov 5 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.