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
188
votes
10 answers

C++ valarray vs. vector

I like vectors a lot. They're nifty and fast. But I know this thing called a valarray exists. Why would I use a valarray instead of a vector? I know valarrays have some syntactic sugar, but other than that, when are they useful?
rlbond
  • 65,341
  • 56
  • 178
  • 228
24
votes
7 answers

Why is valarray so slow?

I am trying to use valarray since it is much like MATLAB while operating vector and matrices. I first did some performance check and found that valarray cannot achieve the performance declared as in the book C++ programming language by…
shangping
  • 989
  • 2
  • 9
  • 25
19
votes
3 answers

What is the difference between std::valarray and std::array

valarray class look's same to array class, can you please explain me where would I prefer valarray over array or vice versa?
codekiddy
  • 5,897
  • 9
  • 50
  • 80
19
votes
1 answer

Why are std::vector and std::valarray initializing constructors different?

I have just been burned by the following. If I want to initialize a std::vector of n elements with a constant X I do this: std::vector v(n, X); But if I need to initialize a std::valarray of n elements with a constant X I need to swap the…
17
votes
1 answer

Why is valarray so slow on Visual Studio 2015?

To speed up the calculations in my library, I decided to use the std::valarray class. The documentation says: std::valarray and helper classes are defined to be free of certain forms of aliasing, thus allowing operations on these classes to be …
dilbert
  • 173
  • 7
15
votes
3 answers

C++ range-based for loop over valarray rvalue is not working

I would like to iterate over a temporary valarray, but it isn't working. Here is my (non-working) code: #include #include int main() { using namespace std; valarray numerators = {99, 26, 25}; …
Jasha
  • 5,507
  • 2
  • 33
  • 44
14
votes
1 answer

Where is it a good idea to use "std::valarray"?

I read about std::valarray in a C++ book writen by Nicolai M. Josuttis. He writes in his book The C++ Standard Library, chapter 17.4: The valarray classes were not designed very well. In fact, nobody tried to determine whether the final…
Jayesh
  • 4,755
  • 9
  • 32
  • 62
12
votes
1 answer

What's wrong with std::valarray's operator*?

Consider the following MCVE, where I have two value arrays where w is two times v (try it out here): #include using namespace std; int main() { valarray v { 1, 2, 3 }; for ([[maybe_unused]] auto x : v) {} // Ok auto w = v *…
andreee
  • 4,459
  • 22
  • 42
12
votes
1 answer

valarray with arithmetic operations return type

When I write a simple arithmetic expression with valarray and assign the result to auto I get a segfault when I try to access the result on gcc. #include #include using std::ostream; using std::valarray; ostream&…
towi
  • 21,587
  • 28
  • 106
  • 187
11
votes
1 answer

Result of summing std::valarray differs when changing optimization levels

I have this piece of code summing std::valarray's: #include #include #include int main() { std::vector> vectorOfValarrays{{1, 1}, {2, 2}, {3, 3}}; std::valarray sumOfValarrays(2); …
10
votes
1 answer

Is the glibcxx STL incorrect in its implementation of std::valarray::sum()?

I was toying with valarrays when I hit something I consider a bug in my compiler's STL implementation. Here is the smallest example I could produce: #include #include #include #include #include…
fjardon
  • 7,921
  • 22
  • 31
10
votes
1 answer

C++11: should I use valarray or vector for numerical computing

The question of vector vs valarray has already been asked here. My question refers specifically to the case of C++11. I have been reading a "A Tour of C++" and "The C++ Programming Language". Both books are written by Bjarne Stroustrup. In the first…
BigONotation
  • 4,406
  • 5
  • 43
  • 72
10
votes
1 answer

Assign a std::vector to a std::valarray

I have a vector, so a table (matrix) of values. Columns contains position and velocity of a planet, so rows stores data of the same planet. I want to transform a row in a valarray because I need math operations. Then I want to store…
user1434698
8
votes
1 answer

Why is there no std::data() overload for std::valarray?

C++11 introduced std::begin(std::valarray&) as well as std::end(std::valarray&). C++17 introduced std::data() which works with std::vector, std::array, C-style arrays, etc. But why wasn't an overloaded std::data() introduced for…
Emile Cormier
  • 28,391
  • 15
  • 94
  • 122
8
votes
1 answer

Slicing a slice using the Standard Library

Is there a functionality in the Standard Library which allows me to slice a std::slice or do I need to write something like std::slice refine(std::slice const& first, std::slice const& second) { return { first.start() + second.start() *…
0xbadf00d
  • 17,405
  • 15
  • 67
  • 107
1
2 3 4 5 6 7 8