473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

valarray

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
1 2057
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Christian Brechbühler | last post by:
The template std::valarray behaves pretty much like a mathematical vector. Arithmetic operators apply elementwise. Now I'd like to extend this to a user-defined type, e.g., complex. ...
6
by: Steven T. Hatton | last post by:
I bought Josuttis's book on the repeated recommendations of people in this newsgroup. http://www.josuttis.com/libbook/ One of the first things I looked up was the std::valarray<>. And what I...
3
by: Peter | last post by:
Hi everybody, I am unfortunately stuck with a probably very simple problem. I made a class called Particle with a valarray (STL) as a class member. The code for the class goes like this: ...
1
by: ES Kim | last post by:
comp.std.c++ would be a better place for this question. Forgive me, but I can't post anything on moderated newsgroups for some reason. valarray doesn't have iterators of its own, which makes...
1
by: ES Kim | last post by:
Here's a code fragment from TC++PL, p679: void f(valarray<double>& v) { size_t i = { 3, 2, 1, 0 }; valarray<size_t> index(i, 4); valarray<double> vv = log(v); } Every compiler I tried...
2
by: Michael Hopkins | last post by:
Hi all I have a subclass of valarray<T> thus template <typename T> class uo_val : public std::valarray<T> { public: uo_val ( ) : std::valarray<T>() {} uo_val (const int sz ) :...
1
by: Dack | last post by:
Hi, I want to track memory leaks in my application (that is using <valarray>). I used the following code: #define _CRTDBG_MAP_ALLOC #include <stdlib.h> #include <crtdbg.h> But then, when I...
9
by: Jim | last post by:
Hi, I want to declare that that a valarray of a certain name exist at the beginning of some code, but I can't instatiate it until I've read in some parameters later on in a for loop i.e. int...
2
by: john | last post by:
Hi, in TC++PL3 on page 665, regarding valarray member functions, it is mentioned: "valarray operator-() const; // result= -v for every element // similarly: +, ~, !" I checked the web and...
43
by: john | last post by:
Hi, in TC++PL 3 on pages 674-675 it is mentioned: "Maybe your first idea for a two-dimensional vector was something like this: class Matrix { valarray< valarray<doublev; public: // ... };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.