TRIQS/nda 2.0.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
ex4.cpp
1#include <nda/nda.hpp>
2#include <array>
3#include <iostream>
4#include <utility>
5
6int main() {
7 // create a full view on an array
8 auto A = nda::array<int, 2>(5, 5);
9 for (int i = 0; auto &x : A) x = i++;
10 auto A_v = A();
11 std::cout << "A_v = " << A_v << std::endl;
12
13 // create a view of a view
14 auto A_vv = A_v();
15 std::cout << "A_vv = " << A_vv << std::endl;
16
17 // check the value type of the view and assign to it
18 static_assert(std::is_same_v<decltype(A_v)::value_type, int>);
19 A_v(0, 0) = -1;
20 std::cout << "A = " << A << std::endl;
21
22 // taking a view of a const array
23 auto A_vc = static_cast<const nda::array<int, 2>>(A)();
24 static_assert(std::is_same_v<decltype(A_vc)::value_type, const int>);
25
26 // taking a view of a view with a const value type
27 auto A_vvc = A_vc();
28 static_assert(std::is_same_v<decltype(A_vvc)::value_type, const int>);
29
30 // original array
31 A(0, 0) = 0;
32 std::cout << "A = " << A << std::endl;
33
34 // slice containing every other column
35 auto S_1 = A(nda::range::all, nda::range(0, 5, 2));
36 std::cout << "S_1 = " << S_1 << std::endl;
37
38 // slice containing every other row of S_1
39 auto S_2 = S_1(nda::range(0, 5, 2), nda::range::all);
40 std::cout << "S_2 = " << S_2 << std::endl;
41
42 // slice containing every other row and column
43 auto S_3 = A(nda::range(0, 5, 2), nda::range(0, 5, 2));
44 std::cout << "S_3 = " << S_3 << std::endl;
45
46 // make a copy of the view using nda::make_regular
47 auto A_tmp = nda::make_regular(S_3);
48
49 // assign a scalar to a view
50 S_3 = 0;
51 std::cout << "S_3 = " << S_3 << std::endl;
52
53 // check the changes to the original array and other views
54 std::cout << "S_1 = " << S_1 << std::endl;
55 std::cout << "A = " << A << std::endl;
56
57 // assign an array to a view
58 S_3 = A_tmp;
59 std::cout << "A = " << A << std::endl;
60
61 // copy construct a view
62 auto S_3_copy = S_3;
63 std::cout << "S_3.data() == S_3_copy.data() = " << (S_3.data() == S_3_copy.data()) << std::endl;
64
65 // move construct a view
66 auto S_3_move = std::move(S_3);
67 std::cout << "S_3.data() == S_3_move.data() = " << (S_3.data() == S_3_move.data()) << std::endl;
68
69 // copy assign to a view
70 auto B = nda::array<int, 2>(S_3.shape());
71 auto B_v = B();
72 B_v = S_3;
73 std::cout << "B = " << B << std::endl;
74
75 // move assign to a view
76 auto C = nda::array<int, 2>(S_3.shape());
78 C_v = std::move(S_3);
79 std::cout << "C = " << C << std::endl;
80
81 // arithmetic operations and math functions
82 C_v = S_3 * 2 + nda::pow(S_3, 2);
83 std::cout << "C = " << C << std::endl;
84
85 // algorithms
86 std::cout << "min_element(S_3) = " << nda::min_element(S_3) << std::endl;
87 std::cout << "max_element(S_3) = " << nda::max_element(S_3) << std::endl;
88 std::cout << "sum(S_3) = " << nda::sum(S_3) << std::endl;
89
90 // rebind a view
91 std::cout << "S_3.data() == C_v.data() = " << (S_3.data() == C_v.data()) << std::endl;
92 C_v.rebind(S_3);
93 std::cout << "S_3.data() == C_v.data() = " << (S_3.data() == C_v.data()) << std::endl;
94
95 // take a view on a std::array
96 std::array<double, 5> arr{1.0, 2.0, 3.0, 4.0, 5.0};
97 auto arr_v = nda::basic_array_view(arr);
98 std::cout << "arr_v = " << arr_v << std::endl;
99
100 // change the value of the vector through the view
101 arr_v *= 2.0;
102 std::cout << "arr = (";
103 for (auto x : arr) std::cout << " " << x;
104 std::cout << " )" << std::endl;
105
106 // original array
107 auto D = nda::array<int, 2>(3, 4);
108 for (int i = 0; auto &x : D) x = i++;
109 std::cout << "D = " << D << std::endl;
110
111 // create a transposed view
112 auto D_t = nda::transpose(D);
113 std::cout << "D_t = " << D_t << std::endl;
114
115 // reshape the array
116 auto D_r = nda::reshape(D, 2, 6);
117 std::cout << "D_r = " << D_r << std::endl;
118
119 // reshape the view
120 auto D_tr = nda::reshape(D_t, 2, 6);
121 std::cout << "D_tr = " << D_tr << std::endl;
122
123 // flatten the view
124 auto D_t_flat = nda::flatten(D_t);
125 std::cout << "D_t_flat = " << D_t_flat << std::endl;
126
127 // flatten the array using reshape
128 auto D_flat = nda::reshape(D, D.size());
129 std::cout << "D_flat = " << D_flat << std::endl;
130}
A generic view of a multi-dimensional array.
ValueType const * data() const noexcept
Get a pointer to the actual data (in general this is not the beginning of the memory block for a view...
void rebind(basic_array_view< T, R, LP, A, AP, OP > v) noexcept
Rebind the current view to another view.
auto max_element(A const &a)
Find the maximum element of an array.
auto sum(A const &a)
Sum all the elements of an nda::Array object.
auto min_element(A const &a)
Find the minimum element of an array.
decltype(auto) make_regular(A &&a)
Make a given object regular.
auto flatten(A &&a)
Flatten an nda::basic_array or nda::basic_array_view to a 1-dimensional array/view by reshaping it.
auto transpose(A &&a)
Transpose the memory layout of an nda::MemoryArray or an nda::expr_call.
auto reshape(A &&a, std::array< Int, R > const &new_shape)
Reshape an nda::basic_array or nda::basic_array_view.
auto pow(A &&a, double p)
Function pow for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types).
basic_array_view< ValueType, Rank, Layout, 'A', default_accessor, borrowed<> > array_view
Alias template of an nda::basic_array_view with an 'A' algebra, nda::default_accessor and nda::borrow...
basic_array< ValueType, Rank, Layout, 'A', ContainerPolicy > array
Alias template of an nda::basic_array with an 'A' algebra.
Includes all relevant headers for the core nda library.