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
6
votes
1 answer

Why do some assignment operators for the helper classes of std::valarray return void?

For example, the assignment operators for std::slice_array: void operator=(const valarray&) const; //#1 void operator=(const T&) const; //#2 const slice_array& operator=(const slice_array&) const; //#3 #1 and #2 return void, but #3 returns const…
6
votes
1 answer

Is it safe to modify elements of std::valarray concurrently?

If I've understood correctly, since C++11 it has been safe to call const member functions of a container concurrently and modify the elements of a container as long as the container itself is not modified as part of the operation (as seen from e.g.…
tsnorri
  • 1,966
  • 5
  • 21
  • 29
6
votes
1 answer

Why the capturing lambda cannot be applied to the std::valarray?

What unqualifies capturing lambda from being passed to apply method of std::valarray? consider following code: int main() { std::valarray arr = {1, 2, 3, 4, 5, 6}; auto arr1 = arr.apply([](int val) { return val * 2; }); // compiles …
kreuzerkrieg
  • 3,009
  • 3
  • 28
  • 59
6
votes
3 answers

Is it a bad idea to replace POD C-style array with std::valarray?

I'm working with a code base that is poorly written and has a lot of memory leaks. It uses a lot of structs that contains raw pointers, which are mostly used as dynamic arrays. Although the structs are often passed between functions, the allocation…
user3528438
  • 2,737
  • 2
  • 23
  • 42
6
votes
1 answer

Valarray and custom allocator

Why std::valarray does not support custom allocators? How designed its memory management? Is there new-based or malloc-based allocator used? All other containers generally provide a possibility to specify custom allocator. Say, std::vector in…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
6
votes
2 answers

valarray vs. vector: Why was valarray introduced?

Yes, this has been asked before, and the answer has been: valarrays (value arrays) are intended to bring some of the speed of Fortran to C++. You wouldn't make a valarray of pointers so the compiler can make assumptions about the code and optimise…
user541686
  • 205,094
  • 128
  • 528
  • 886
5
votes
1 answer

What is the return type of the STL algorithm "count", on a valarray

I'm using Visual Studio 2010 Pro on a Windows 7 64bit machine and I want to use count (from the header) on a valarray: int main() { valarray v(false,10); for (int i(0);i<10;i+=3) v[i]=true; cout <<…
Benny K
  • 1,957
  • 18
  • 33
5
votes
1 answer

Why does valarray assignment not resize assignee per the documentation?

Code: #include #include using namespace std; int main() { valarray v0(2, 4); valarray v1; v1 = v0; cout << "v0.size: " << v0.size() << endl; cout << "v1.size: " << v1.size() << endl; cout << "v0[0]:…
Bryant
  • 334
  • 1
  • 11
5
votes
1 answer

std::valarray and parallelization

May be it is so stupid question. On this site I read that The valarray specification allows for libraries to implement it with several efficiency optimizations, such as parallelization of certain operations What is at the moment with…
5
votes
1 answer

valarray on aligned memory for SSE / AVX

Is there a way to ensure valarray uses aligned memory so it could be vectorized with SSE and AVX? As far as I know the STL doesn't guarantee alignment, and you can not pass an allocator to valarray. Is there another way to achieve this? Thanks…
litro
  • 195
  • 1
  • 11
4
votes
1 answer

Is it a bug of the implementation of std::valarray in gcc?

I tried the following program #include #include int main( void ) { std::valarray v1 = { 1, 2, 3, 4, 5 }; std::valarray v2 = { 1, 2, 3, 4, 5 }; auto v3 = v1 * v2; for ( const auto &item : v3 )…
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
4
votes
2 answers

valarray divide its element

When I divide a valarray by its first element, only the first element becomes 1 and others keep their original value. #include #include using namespace std; int main() { valarray arr({5,10,15,20,25}); …
apple apple
  • 10,292
  • 2
  • 16
  • 36
4
votes
1 answer

In what sense is valarray free from aliasing?

An oft-made claim is that std::valarray was intended to eliminate some forms of aliasing in order to enable better optimization (e.g. see valarray vs. vector: Why was valarray introduced?) Can anyone elaborate on this claim? It seems to me that…
Brian Bi
  • 111,498
  • 10
  • 176
  • 312
3
votes
1 answer

Is `valarray` capable of harnessing SIMD support?

I am aware that the valarray class was originally implemented with the aim to optimize high-speed numerical computations. As a drawback, many of its design aspects that support this - for instance the restrict-type mechanism that limits aliasing for…
Giogre
  • 1,444
  • 7
  • 19
3
votes
0 answers

Why don't std::valarray get more attention from all C++ compilers? Expression templates mean big speedup to certain math-heavy tasks

Some codes like this (I'm not the author but I appreciate the work): // 22 cycles per pixel mandelbrot (cascadelake) #include #include #include #include #include #include #include…