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
1
vote
3 answers

C++ valarray vs array size allocation

I am allocating a multidimensional valarray of size 2000x2000 and it is working smoothly. valarray> D(valarray(-2,2000), 2000); D[1999][1999] = 2000; However, if I try to allocate a normal array and access an element, I get…
1
vote
1 answer

Getting std::valarray to (google/quick) bench properly

I'm trying to compare the performance of using std::valarray vs. std::vector/std::transform operations using Google Bench. I'm using QuickBench. My code (for QuickBench) is #include class RG { public: double operator()() noexcept {…
JHBonarius
  • 10,824
  • 3
  • 22
  • 41
1
vote
1 answer

Is there a better way to implement the const version of member function row() without using const_cast?

The example comes from the demonstration for use of std::slice and std::slice_array: https://en.cppreference.com/w/cpp/numeric/valarray/slice https://en.cppreference.com/w/cpp/numeric/valarray/slice_array How should I properly define the const…
ypan1988
  • 71
  • 1
  • 5
1
vote
0 answers

Why do I get different behavior of mask_array assignment?

#include #include typedef std::valarray valarray_t; typedef std::valarray maskarray_t; int main(void) { valarray_t::value_type ibuf[10] = {0,1,2,3,4,5,6,7,8,9}; valarray_t vi1(ibuf,…
issac
  • 155
  • 8
1
vote
1 answer

Add of std::valarray got different sizes with different operand orders

Add of std::valarray got different sizes with different operand orders. Code is as the following: #include #include using namespace std; int main() { std::valarray v0{3.f, 0.f}; std::valarray v1{0.f}; …
chaosink
  • 1,329
  • 13
  • 27
1
vote
1 answer

assigning to gslice_array gives runtime error

I'm trying to construct a class derived from std::valarray to use my own methods on it. I have encountered a problem about assigning values using operator[]. After a lot of effort, I think I finally detected the problem. While assigning to…
Burak
  • 2,251
  • 1
  • 16
  • 33
1
vote
1 answer

What is the difference between the two snippets?

I need some explanation for [](int x){return x=x+5;}. What does [] (int x) mean? I ran the varr.apply(incelemby5) where incelemby5 increases array elements by 5. Got the same results varr1 = varr.apply([](int x){return x=x+5;}); int incelemby5(int…
adi_226
  • 41
  • 1
  • 7
1
vote
3 answers

Convert valarray to basic array

We can initialize a valarray from a basic user defined array as following: int arr[3]={0}; valarray test(arr, sizeof(arr)/sizeof(int)); How can we move the other way around? Suppose we have a valarray and we need to convert back to a…
mibrahimy
  • 722
  • 4
  • 18
1
vote
1 answer

Constant-time access to an arbitrary element in a list (C++)

I'm currently working on the implementation of an algorithm that I would like to show that it can work in constant-time, even with a very large number of elements. Unfortunately I need a data structure where to store the elements. When the number of…
1
vote
0 answers

C++ return intermediate reference

I have a 2d version of valarray called matrix. I want to access one column of it and use it as the left value outside of the class. The IDE gives me the following error on the return line. I guess [] operator of valarray returns an intermediate…
Chris Zhang
  • 113
  • 1
  • 6
1
vote
4 answers

Invalid range expression of std::valarray in range for loop

I have a simple usage of traversing a temporary std::valarray expression in range for loop, but got error: invalid range expression ... main.cpp #include #include int main() { std::valarray xxx {2,7,1,8,2,8}; …
Lance LI
  • 113
  • 3
  • 9
1
vote
2 answers

Why does this code fail to compile?

I know it's a pretty general title, but I have some code and it strikes me as weird that it cant compile. Here is a demo of the issue. If you change scalar_t from double to float the code compiles fine. Why cant float be promoted to double here? In…
Max Ehrlich
  • 2,479
  • 1
  • 32
  • 44
1
vote
1 answer

Does resize guarantee an std::valarray to be zeroed?

Does x.resize(1024) guarantee that a valarray x will be zeroed? Does std::valarray z(1024); guarantee that it will be filled with zeros as well? Is this true for Mac, Windows, Linux? Is this true even for C++03 (I'm not using C++11)? (All…
Basj
  • 41,386
  • 99
  • 383
  • 673
1
vote
1 answer

Accessing column slices of 2D valarrays

Consider the following code snippet, #include #include using namespace std; std::ostream & operator<<(std::ostream & out, const std::valarray inputVector); typedef std::valarray > val2d; int main() { …
Naveen
  • 458
  • 1
  • 10
  • 29
1
vote
0 answers

Constructing a valarray using raw data

It seems I might be using std::valarray<_Tp>s for some computational work (suppose _Tp is uint64_t). Unfortunately, the following hold: my code receives raw arrays - uint64_t*s and a length value - I can't change signatures/APIs. They're…
einpoklum
  • 118,144
  • 57
  • 340
  • 684