Questions tagged [valarray]

C++ std::valarray is the class for representing and manipulating arrays of values. It supports element-wise mathematical operations and various forms of generalized subscript operators, slicing and indirect access.

A valarray object is designed to hold an array of values and easily perform mathematical operations on them. It also allows special mechanisms to refer to subsets of elements in the arrays.

Most mathematical operations can be applied directly to valarray objects, including arithmetical and comparison operators, affecting all its elements.

The valarray specification allows for libraries to implement it with several efficiency optimizations, such as parallelization of certain operations, memory recycling or support for copy-on-reference / copy-on-write optimizations.

std::valarray and helper classes are defined to be free of certain forms of aliasing, thus allowing operations on these classes to be optimized similar to the effect of the keyword restrict in the C programming language.

Some C++ standard library implementations use expression templates to implement efficient operations on std::valarray (e.g. GNU libstdc++ and LLVM libc++).

Further details:

110 questions
3
votes
1 answer

Is std::begin on an empty std::valarray undefined behavior?

I was playing around with std::valarray and UndefinedBehaviorSanitizer, and noticed that std::begin an empty std::valarray causes undefined behavior. Here is the code: #include int main() { std::valarray a; …
EFanZh
  • 2,357
  • 3
  • 30
  • 63
3
votes
1 answer

Is it correct to assign a slice_array to another slice_array?

int input[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; std::valarray test(input, sizeof(input)/sizeof(input[0])); const std::slice_array s1 = test[std::slice(1, 3, 2)]; const std::slice_array s2 = test[std::slice(0, 3, 1)]; // is it…
issac
  • 155
  • 8
3
votes
1 answer

Why is std::valarray non-arithmetic?

I've noticed that std::is_arithmetic> evaluates to false. Isn't the entire point of valarrays to facilitate overloading scalar code to 'auto-vectorise' without resorting to boost etc. ? Why does it not inherit the arithmetic…
Manuel S
  • 43
  • 4
3
votes
1 answer

How can I do a dot product between a matrix and a vector in c++

There are a function called inner_product, but I failed miserably in use that. I'll need to use this function several times for different matrices and vectors. Bellow my current code: std::vector> matrix_a = {{0, 0}, …
messier
  • 65
  • 1
  • 7
3
votes
1 answer

Pointer into vector, but not into a valarray?

I'm trying to extract a const pointer to part way through an array. I found it works fine when using a vector, but won't compile (VS 2008) when using a valarray. Can somebody explain what the problem is? struct vector_test { std::vector
wxffles
  • 864
  • 6
  • 20
3
votes
1 answer

Using .sum() and += on std::valarray

I am using the type std::valarray> and wish to sum each of the contained valarrays element wise, to leave a std::valarray. The C++ documentation states that the operator .sum() can be applied to std::valarray so long…
Watw
  • 133
  • 3
3
votes
1 answer

Why is std::slice_array::operator= const?

All overloads of assignment operators of std::slice_array are const member functions, but why is it designed so? Maybe the reason is that std::slice_array is designed to be a proxy class, and these assignment operators just modify the referred…
xskxzr
  • 12,442
  • 12
  • 37
  • 77
3
votes
1 answer

Converting a valarray to a vector without copying

I have a really large valarray that I need to convert to a vector because a library I'm using only takes a vector as input. I'm wondering if it's possible to convert from valarray to vector without copying. Here's what I have: #include…
Phlox Midas
  • 4,093
  • 4
  • 35
  • 56
3
votes
1 answer

How do I initialize a valarray from a vector?

Is it ok/safe/good practice to write the following in order to fill a valarray with the content of a vector? vector myVect = {2,0,1,0,0,1,1,0,0,0}; // Any vector valarray myVala ( &(myVect[0]), myVect.size() ); //Edit: as suggested by Xeo,…
wxc3
  • 31
  • 1
  • 3
3
votes
2 answers

Strange C++ compile error with valarrays

I have a strange compile error using valarrays in C++. This is a stripped down version of my code: #include #include using namespace std; bool test(const int &x,const valarray &a,const valarray &b) { return…
2
votes
2 answers

is there any way to avoid the copy from and to between the valarray and array?

I have a lot of data in a list, say several kbytes in each element, I would like to extract each by each to do some numeric processing. These data are originally stored as float[]. Since the processing involves a lot of indexing and global…
shangping
  • 989
  • 2
  • 9
  • 25
2
votes
1 answer

C++ class with nested expression templates

I want to define a class, called Nested here, that will contains two or more (one here) data members that support arithmetic operations using expression templates, for example an std::valarray. For this class itself, I am defining its own expression…
Davide
  • 1,415
  • 2
  • 11
  • 13
2
votes
2 answers

valarray in-place operation gives different result as a temporary assignment

the following program: #include #include using namespace std; int main() { int init[] = {1, 1}; // Example 1 valarray a(init, 2); // In-place assignment a[slice(0, 2, 1)] = a[slice(0, 2, 1)] +…
András Gyömrey
  • 1,770
  • 1
  • 15
  • 36
2
votes
0 answers

std::valarray, std:vector or arma::vec?

I am not sure if this is the place for this question, but here it goes. I am working on computational physics model in C++ and I received a big code that I am tiding up. The field is quantum electronics, so there are lots of integrals and…
2
votes
1 answer

How to integrate std::valarray with gsl?

I am relatively new to C++, but I have some (scarce) coding and numerical experience. I know that this question gets posted every now and then, how do you integrate an array. In MATLAB you can force your array to be a function (I forgot how, but I…