valarray
Initialization
#include <iostream>
#include <valarray>
#include <iterator>
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<*it<<" ";
std::advance(it, 1);
}
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
std::valarray<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
display(std::begin(v), std::end(v));
std::valarray<int> v2(v[std::slice(1, 3, 2)]);
display(std::begin(v2), std::end(v2));
return 0;
}
Operator
#include <iostream>
#include <valarray>
#include <iterator>
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<*it<<" ";
std::advance(it, 1);
}
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
std::valarray<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
display(std::begin(v), std::end(v));//1 2 3 4 5 6 7 8 9 10
std::valarray<int> v2(v[std::slice(1, 3, 2)]);
display(std::begin(v2), std::end(v2));//2 4 6
std::valarray<int> v3;
v3 = v*10;
display(std::begin(v3), std::end(v3));//10 20 30 40 50 60 70 80 90 100
return 0;
}
Operations
#include <iostream>
#include <valarray>
#include <iterator>
template <class T>
T increment(T x)
{
return ++x;
}
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<*it<<" ";
std::advance(it, 1);
}
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
std::valarray<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
//apply
std::valarray<int> v2 = v.apply(increment);//2 3 4 5 6 7 8 9 10 11
//cshift
std::valarray<int> v3 = v.cshift(1);//2 3 4 5 6 7 8 9 10 1
//max
std::cout<<"Max: "<<v.max()<<std::endl;
//min
std::cout<<"Min: "<<v.min()<<std::endl;
//shift
std::valarray<int> v4 = v.shift(2);//3 4 5 6 7 8 9 10 0 0
//size
std::cout<<"Size: "<<v4.size()<<std::endl;
//sum
std::cout<<"Sum: "<<v.sum()<<std::endl;
display(std::begin(v4), std::end(v4));
return 0;
}
Arithmetics
#include <iostream>
#include <valarray>
#include <iterator>
#include <cstdlib>
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<*it<<" ";
std::advance(it, 1);
}
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
std::valarray<float> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::valarray<float> cosV = std::cos(v);
display(std::begin(cosV), std::end(cosV));
std::valarray<float> sinV = std::sin(v);
display(std::begin(sinV), std::end(sinV));
std::valarray<float> logV = std::log(v);
display(std::begin(logV), std::end(logV));
std::valarray<float> expV = std::exp(v);
display(std::begin(expV), std::end(expV));
return 0;
}
Slice Array
#include <iostream>
#include <valarray>
#include <iterator>
#include <cstdlib>
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<*it<<" ";
std::advance(it, 1);
}
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
std::valarray<float> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::slice s = std::slice(1, 4, 2);
v[s] *= std::valarray<float>(10, 4);
display(std::begin(v), std::end(v));
return 0;
}
Mask Array
#include <iostream>
#include <valarray>
#include <iterator>
#include <cstdlib>
template <class T>
void display(T it, T end)
{
while(it != end)
{
std::cout<<*it<<" ";
std::advance(it, 1);
}
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
std::valarray<float> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::valarray<bool> mask(10);
for(int i = 0; i < 10; i++) mask[i] = (i%2 == 1);
v[mask] *= std::valarray<float>(10, 5);
display(std::begin(v), std::end(v));
v[!mask] = 0;
display(std::begin(v), std::end(v));
return 0;
}
Reference