TRIQS/nda
2.0.0
Multi-dimensional array library for C++
Toggle main menu visibility
Loading...
Searching...
No Matches
ex3.cpp
1
#include <
nda/nda.hpp
>
2
#include <complex>
3
#include <iostream>
4
5
int
main() {
6
using namespace
std::complex_literals;
7
8
// assign a scalar to an array
9
auto
A =
nda::array<std::complex<double>
, 2>(3, 2);
10
A = 0.1 + 0.2i;
11
std::cout <<
"A = "
<< A << std::endl;
12
13
// assign a scalar to a matrix
14
auto
M =
nda::matrix<std::complex<double>
>(3, 2);
15
M = 0.1 + 0.2i;
16
std::cout <<
"M = "
<< M << std::endl;
17
18
// assign an array to an array
19
auto
A_arr =
nda::array<nda::array<int, 1>
, 1>(4);
20
nda::array<int, 1>
A_sub{1, 2, 3};
21
for
(
auto
&x : A_arr) x = A_sub;
22
std::cout <<
"A_arr = "
<< A_arr << std::endl;
23
24
// copy assignment
25
auto
M_copy =
nda::matrix<std::complex<double>
>(3, 2);
26
M_copy = M;
27
std::cout <<
"M_copy = "
<< M_copy << std::endl;
28
29
// move assignment
30
auto
M_move =
nda::matrix<std::complex<double>
>();
31
M_move = std::move(M_copy);
32
std::cout <<
"M_move = "
<< M_move << std::endl;
33
std::cout <<
"M_copy.empty() = "
<< M_copy.
empty
() << std::endl;
34
35
// assign a lazy expression
36
nda::matrix<std::complex<double>
,
nda::F_layout
> M_f(3, 2);
37
M_f = M + M;
38
std::cout <<
"M_f = "
<< M_f << std::endl;
39
40
// assign another array with a different layout and algebra
41
nda::array<std::complex<double>
, 2> A2;
42
A2 = M_f;
43
std::cout <<
"A2 = "
<< A2 << std::endl;
44
45
// assign a contiguous range to an 1-dimensional array
46
std::vector<long> vec{1, 2, 3, 4, 5};
47
auto
A_vec =
nda::array<long, 1>
();
48
A_vec = vec;
49
std::cout <<
"A_vec = "
<< A_vec << std::endl;
50
51
// initialize an array using traditional for-loops
52
auto
B =
nda::array<int, 2>
(2, 3);
53
for
(
int
i = 0; i < 2; ++i) {
54
for
(
int
j = 0; j < 3; ++j) {
55
B(i, j) = i * 3 + j;
56
}
57
}
58
std::cout <<
"B = "
<< B << std::endl;
59
60
// initialize an array using a range-based for-loop
61
auto
B2 =
nda::array<int, 2>
(2, 3);
62
for
(
int
i = 0;
auto
&x : B2) x = i++;
63
std::cout <<
"B2 = "
<< B2 << std::endl;
64
65
// initialize an array using nda::for_each
66
auto
B3 =
nda::array<int, 2>
(2, 3);
67
nda::for_each
(B3.shape(), [&B3](
auto
i,
auto
j) { B3(i, j) = i * 3 + j; });
68
std::cout <<
"B3 = "
<< B3 << std::endl;
69
70
// initialize an array using CLEF's automatic assignment
71
using namespace
nda::clef::literals;
72
auto
C =
nda::array<int, 4>
(2, 2, 2, 2);
73
C(
i_
,
j_
,
k_
,
l_
) <<
i_
* 8 +
j_
* 4 +
k_
* 2 +
l_
;
74
std::cout <<
"C = "
<< C << std::endl;
75
}
nda::basic_array::empty
bool empty() const
Is the view/array empty?
Definition
basic_array.hpp:661
nda::array
basic_array< ValueType, Rank, Layout, 'A', ContainerPolicy > array
Alias template of an nda::basic_array with an 'A' algebra.
Definition
declarations.hpp:64
nda::matrix
basic_array< ValueType, 2, Layout, 'M', ContainerPolicy > matrix
Alias template of an nda::basic_array with rank 2 and an 'M' algebra.
Definition
declarations.hpp:117
nda::clef::literals::j_
constexpr auto j_
Generic placeholder #2.
Definition
literals.hpp:29
nda::clef::literals::i_
constexpr auto i_
Generic placeholder #1.
Definition
literals.hpp:26
nda::clef::literals::k_
constexpr auto k_
Generic placeholder #3.
Definition
literals.hpp:32
nda::clef::literals::l_
constexpr auto l_
Generic placeholder #4.
Definition
literals.hpp:35
nda::for_each
__inline__ void for_each(std::array< Int, R > const &shape, F &&f)
Loop over all possible index values of a given shape and apply a function to them.
Definition
for_each.hpp:116
nda.hpp
Includes all relevant headers for the core nda library.
nda::F_layout
Contiguous layout policy with Fortran-order (column-major order).
Definition
policies.hpp:52
doc
examples
ex3.cpp
Generated by
1.17.0