Skip to content

Commit 84d0f28

Browse files
committed
Update valarray notes
1 parent 51cff6a commit 84d0f28

File tree

1 file changed

+109
-90
lines changed

1 file changed

+109
-90
lines changed

ValArray/valarray_notes.cpp

Lines changed: 109 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -15,111 +15,130 @@
1515
*
1616
*/
1717

18-
#include <algorithm>
1918
#include <cassert>
20-
#include <concepts>
19+
#include <cstdint>
20+
#include <cxxabi.h>
2121
#include <iostream>
22-
#include <limits>
22+
#include <memory>
2323
#include <span>
24+
#include <string>
2425
#include <type_traits>
26+
#include <typeinfo>
2527
#include <valarray>
2628

2729
#ifndef log
2830
#define log std::cout << __LINE__ << ": " << std::boolalpha
2931
#endif
3032

3133
template <typename T>
32-
requires std::is_arithmetic_v<T> && (!std::is_pointer_v<T>) && (!std::is_reference_v<T>)
33-
inline std::ostream& operator<<(std::ostream& os, const std::valarray<T>& values) noexcept {
34-
os << "[";
35-
if (values.size() >= 1) [[likely]] {
36-
for (const T* it = std::begin(values); it < std::end(values) - 1; it++) {
37-
os << *it << ", ";
38-
}
39-
os << values[values.size() - 1];
34+
requires std::is_arithmetic_v<T> && (!std::is_pointer_v<T>) &&
35+
(!std::is_reference_v<T>)
36+
inline std::ostream &operator<<(std::ostream &os,
37+
const std::valarray<T> &values) noexcept {
38+
os << "[";
39+
if (values.size() >= 1) [[likely]] {
40+
for (const T *it = std::begin(values); it < std::end(values) - 1; it++) {
41+
os << *it << ", ";
4042
}
41-
os << "]";
42-
return os;
43+
os << values[values.size() - 1];
44+
}
45+
os << "]";
46+
return os;
4347
}
4448

4549
template <>
46-
inline std::ostream& operator<<(std::ostream& os, const std::valarray<std::uint8_t>& values) noexcept = delete;
50+
inline std::ostream &
51+
operator<<(std::ostream &os,
52+
const std::valarray<std::uint8_t> &values) noexcept = delete;
53+
54+
template <typename T> std::string type_name() {
55+
int status = 0;
56+
std::unique_ptr<char, void (*)(void *)> res{
57+
abi::__cxa_demangle(typeid(T).name(), nullptr, nullptr, &status),
58+
std::free};
59+
return (status == 0) ? res.get() : typeid(T).name();
60+
}
4761

4862
int main() {
49-
static_assert(std::is_arithmetic_v<std::uint8_t>);
50-
51-
// log << std::is_arithmetic_v<std::uint8_t> << "\n";
52-
// log << std::numeric_limits<std::uint16_t>::min() << "\n";
53-
// log << std::numeric_limits<std::uint16_t>::max() << "\n";
54-
55-
// create an empty valarray
56-
std::valarray<float_t> val;
57-
log << val << "\n";
58-
log << val.size() << "\n";
59-
val.resize(1);
60-
val[0] = 3.14;
61-
log << val << "\n";
62-
63-
val.resize(2);
64-
val[1] = 2.71;
65-
log << val << "\n";
66-
67-
// create a valarray of size 5, initialized with values 0, 1, 2, 3, 4
68-
std::valarray<int> v{0, 1, 2, 3, 4};
69-
70-
// print the elements of the valarray
71-
log << v << "\n";
72-
73-
// apply a function to each element of the valarray
74-
auto f = [](int x) { return x * x; };
75-
auto v2 = std::valarray<int>(v.apply(f));
76-
77-
log << v2 << "\n";
78-
79-
// perform mathematical operations on the valarray
80-
std::valarray v3 = v + v2;
81-
log << "v3 is of type std::valarray<int>: " << std::is_same_v<decltype(v3), std::valarray<int>> << "\n";
82-
log << v3 << "\n";
83-
84-
auto v4 = v3.apply([](int x) { return x / 2; });
85-
log << "v4 is of type std::valarray<int>: " << std::is_same_v<decltype(v4), std::valarray<int>> << "\n";
86-
log << v4 << "\n";
87-
88-
// increment the value in v
89-
v += 1;
90-
log << v << "\n";
91-
92-
// compute the sum of the valarray
93-
int sum = v.sum();
94-
log << "sum of v: " << sum << "\n";
95-
96-
// compute the square of each element of the valarray
97-
std::valarray<int> v_squared = v * v;
98-
log << v_squared << "\n";
99-
100-
// compute the dot product of v and v_squared
101-
int dot_product = (v * v_squared).sum();
102-
log << "dot product of v and v_squared: " << dot_product << "\n";
103-
104-
v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
105-
log << v << "\n";
106-
107-
// create a slice with start index 2, length 3, and stride 2
108-
// s[2] = 3
109-
// s[2 + 2] == s[4] = 5
110-
// s[2 + 2 + 2] == s[6] = 7
111-
std::slice s{2, 3, 2};
112-
log << typeid(s).name() << "\n"; // class std::slice
113-
114-
// use the slice to create a new valarray
115-
std::valarray<int> result = v[s];
116-
// print the new valarray
117-
log << result << "\n";
118-
119-
// change the value of the original valarray
120-
v[s] = 0;
121-
// print the original valarray
122-
log << v << "\n";
123-
124-
return 0;
63+
static_assert(std::is_arithmetic_v<std::uint8_t>);
64+
65+
// log << std::is_arithmetic_v<std::uint8_t> << "\n";
66+
// log << std::numeric_limits<std::uint16_t>::min() << "\n";
67+
// log << std::numeric_limits<std::uint16_t>::max() << "\n";
68+
69+
// create an empty valarray
70+
std::valarray<float_t> val;
71+
log << val << "\n";
72+
log << val.size() << "\n";
73+
val.resize(1);
74+
val[0] = 3.14;
75+
log << val << "\n";
76+
77+
val.resize(2);
78+
val[1] = 2.71;
79+
log << val << "\n";
80+
81+
// create a valarray of size 5, initialized with values 0, 1, 2, 3, 4
82+
std::valarray<int> v{0, 1, 2, 3, 4};
83+
84+
// print the elements of the valarray
85+
log << v << "\n";
86+
87+
// apply a function to each element of the valarray
88+
auto f = [](int x) -> int { return x * x; };
89+
std::valarray v2 = std::valarray<int>(v.apply(f));
90+
91+
log << v2 << "\n";
92+
93+
// perform mathematical operations on the valarray
94+
std::valarray v3 = v + v2;
95+
log << "v3 is of type std::valarray<int>: "
96+
<< std::is_same_v<decltype(v3), std::valarray<int>> << "\n";
97+
log << v3 << "\n";
98+
99+
auto v4 = v3.apply([](int x) -> int { return x / 2; });
100+
log << "v4 is of type std::valarray<int>: "
101+
<< std::is_same_v<decltype(v4), std::valarray<int>> << "\n";
102+
log << typeid(v4).name() << "\n";
103+
log << type_name<decltype(v4)>() << "\n";
104+
// log << v4 << "\n";
105+
106+
// increment the value in v
107+
v += 1;
108+
log << v << "\n";
109+
110+
// compute the sum of the valarray
111+
int sum = v.sum();
112+
log << "sum of v: " << sum << "\n";
113+
114+
// compute the square of each element of the valarray
115+
std::valarray<int> v_squared = v * v;
116+
log << v_squared << "\n";
117+
118+
// compute the dot product of v and v_squared
119+
int dot_product = (v * v_squared).sum();
120+
log << "dot product of v and v_squared: " << dot_product << "\n";
121+
122+
v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
123+
log << v << "\n";
124+
125+
// create a slice with start index 2, length 3, and stride 2
126+
// s[2] = 3
127+
// s[2 + 2] == s[4] = 5
128+
// s[2 + 2 + 2] == s[6] = 7
129+
std::slice s{2, 3, 2};
130+
log << typeid(s).name() << "\n"; // class std::slice
131+
log << type_name<decltype(s)>() << "\n";
132+
133+
// use the slice to create a new valarray
134+
std::valarray<int> result = v[s];
135+
// print the new valarray
136+
log << result << "\n";
137+
138+
// change the value of the original valarray
139+
v[s] = 0;
140+
// print the original valarray
141+
log << v << "\n";
142+
143+
return 0;
125144
}

0 commit comments

Comments
 (0)