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
2
votes
0 answers

using std::valarray and std::slice to copy stridden data

I'm trying efficiently to copy stridden data. Consider following function signature void foo(Array& input, Buffer& U, Buffer& V) where Buffer and Array are using Buffer = std::vector; using Array = std::valarray; Now, depending…
kreuzerkrieg
  • 3,009
  • 3
  • 28
  • 59
2
votes
2 answers

Multiplying a valarray of complex numbers by a scalar

valarray can be multiplied by a scalar. However, I get an error when I want to multiply by a scalar for a valarray>. I'm wondering if there's a way to do it in a pretty manner. Here's a reproduction of the…
Phlox Midas
  • 4,093
  • 4
  • 35
  • 56
2
votes
0 answers

Multiplying valarrays of different types

I am quite suprised that it's not possible to multiply two valarrays if one is of type double and second is int. Why is that? std::valarray vali(3); vali[0] = 0; vali[1] = 1; vali[2] = 2; std::valarray vald(3); vald[0] = 0; vald[1] =…
Tomas
  • 2,170
  • 10
  • 14
2
votes
1 answer

Can I get a const slice_array from a const valarray?

I have a function that accepts a const reference to a valarray, and I want to be able to slice the array and pass the slice into another function that expects a const slice_array. I know that I can just use operator[] and a slice to get a new,…
youngmit
  • 800
  • 7
  • 17
2
votes
1 answer

How to compare C++ slice_array? Why it cannot be compared as valarray?

I wonder why I cannot compare some_valarray[first_slice] < another_valarray[second_slice] as I compare some_valarray < another_valarray and how I can do that in simple way without copying? Of course, I can iterate over them in plain loop but, maybe,…
George Sovetov
  • 4,942
  • 5
  • 36
  • 57
2
votes
1 answer

See contents of valarray in CLion's debugger?

Is it possible to do something to easily see the contents of a valarray in Clion's debugger?
a06e
  • 18,594
  • 33
  • 93
  • 169
2
votes
4 answers

Best way to test for equality of two valarray?

I think that the default overload of == for valarray is not very convenient. By default x==y (for two valarrays x and y) returns a valarray, with true on the ith entry if x[i]==y[i]. Rather, I need a single bool, which tells me if both…
a06e
  • 18,594
  • 33
  • 93
  • 169
2
votes
2 answers

Initializing struct via member initialization list

So I'm learning C++ from Stephen Prata book and I want to do one exercise... So the problem is this: I want to use a std::valarray inside a struct, inside a class like this: class Wine { private: struct Pair { std::valarray
pmakal
  • 69
  • 1
  • 6
2
votes
2 answers

Most efficient way to pass data to a std::valarray from a std::vector

What's the most efficient way to set the data from a std::vector to a std::valarray? Say we have std::valarray my_valarray; and std::vector my_vector; and we want to copy the data across from my_vector to my_valarray: Option 1 (using…
Darien Pardinas
  • 5,910
  • 1
  • 41
  • 48
2
votes
0 answers

mask_array behavior in libstdc++

case 1: std::valarray data = {1,4,0,2,5}; std::valarray exp_mask = data <= 2; std::mask_array marr1 = data[mask]; marr1 = 10; case 2: std::valarray data = {1,4,0,2,5}; data[data <= 2] = 11; // 7 case 3: std::valarray data…
helloworld922
  • 10,801
  • 5
  • 48
  • 85
2
votes
3 answers

c++ reading fits file using ccfits

So... can anyone see what I'm doing wrong here?!? I'm trying to read a *.fits file in C++ using CCfits following their example at http://heasarc.gsfc.nasa.gov/fitsio/CCfits/html/readimage.html. #include #include #include…
razvanc
  • 930
  • 2
  • 11
  • 18
2
votes
1 answer

initialize stl valarray in one row

Hello I wannted to build a helper class to initialize a stl valarray. What I would like is to do the following: std::valarray vec(3); vlist_of(vec)(2)(3)(5); So I can just initialize the vectors at runtime using just one row command…
jamk
  • 836
  • 1
  • 10
  • 24
2
votes
1 answer

Promoting a raw pointer to valarray

I am developing a library which have C interface for compatibility purpose void interface(double* context, size_t num_elements); while context points to a raw memory storing num_elements doubles. In the remaining part of the code, is there any…
xis
  • 24,330
  • 9
  • 43
  • 59
1
vote
0 answers

How to efficiently slice c++ vector with omp?

I am having a hard time understanding the relationship between omp parallel and vector slicing within each thread. I was trying with std::vector but switched to std:valarray to try and minimize the slice copying, although I assume it is not relevant…
001001
  • 530
  • 1
  • 4
  • 13
1
vote
1 answer

Converting valarray to valarray

I'm trying to convert a valarray to a valarray of 0s and 1s. I hoped something like this would work (invoking integral promotion with unary +, as works on a single bool): #include #include int main() { …
Lynn
  • 10,425
  • 43
  • 75