TRIQS/nda 2.0.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
ex2.cpp
1#include <nda/nda.hpp>
2#include <complex>
3#include <iostream>
4
5int main() {
6 // default constructor
7 auto A1 = nda::array<double, 2>();
8 std::cout << "A1 = " << A1 << std::endl;
9 std::cout << "A1.size() = " << A1.size() << std::endl;
10 std::cout << "A1.shape() = " << A1.shape() << std::endl;
11
12 // resize the array using the resize method
13 A1.resize(3, 2);
14 std::cout << "A1.size() = " << A1.size() << std::endl;
15 std::cout << "A1.shape() = " << A1.shape() << std::endl;
16
17 // resize using assignment
18 A1 = nda::array<double, 2>(10, 10);
19 std::cout << "A1.size() = " << A1.size() << std::endl;
20 std::cout << "A1.shape() = " << A1.shape() << std::endl;
21
22 // create a 100x100 complex matrix
23 auto M1 = nda::matrix<std::complex<double>>(100, 100);
24 std::cout << "M1.size() = " << M1.size() << std::endl;
25 std::cout << "M1.shape() = " << M1.shape() << std::endl;
26
27 // create a 1-dimensional vector of size 5 with the value 10 + 1i
28 using namespace std::complex_literals;
29 auto v1 = nda::vector<std::complex<double>>(5, 10 + 1i);
30 std::cout << "v1 = " << v1 << std::endl;
31 std::cout << "v1.size() = " << v1.size() << std::endl;
32 std::cout << "v1.shape() = " << v1.shape() << std::endl;
33
34 // copy constructor
35 auto v2 = v1;
36 std::cout << "v2 = " << v2 << std::endl;
37 std::cout << "v2.size() = " << v2.size() << std::endl;
38 std::cout << "v2.shape() = " << v2.shape() << std::endl;
39
40 // move constructor
41 auto v3 = std::move(v2);
42 std::cout << "v3 = " << v3 << std::endl;
43 std::cout << "v3.size() = " << v3.size() << std::endl;
44 std::cout << "v3.shape() = " << v3.shape() << std::endl;
45 std::cout << "v2.empty() = " << v2.empty() << std::endl;
46
47 // 1-dimensional array from a std::initializer_list
48 auto A1_il = nda::array<int, 1>{1, 2, 3, 4, 5};
49 std::cout << "A1_il = " << A1_il << std::endl;
50 std::cout << "A1_il.size() = " << A1_il.size() << std::endl;
51 std::cout << "A1_il.shape() = " << A1_il.shape() << std::endl;
52
53 // 2-dimensional array from a std::initializer_list
54 auto A2_il = nda::array<int, 2>{{1, 2}, {3, 4}, {5, 6}};
55 std::cout << "A2_il = " << A2_il << std::endl;
56 std::cout << "A2_il.size() = " << A2_il.size() << std::endl;
57 std::cout << "A2_il.shape() = " << A2_il.shape() << std::endl;
58
59 // 3-dimensional array from a std::initializer_list
60 auto A3_il = nda::array<int, 3>{{{1, 2}, {3, 4}, {5, 6}}, {{7, 8}, {9, 10}, {11, 12}}};
61 std::cout << "A3_il = " << A3_il << std::endl;
62 std::cout << "A3_il.size() = " << A3_il.size() << std::endl;
63 std::cout << "A3_il.shape() = " << A3_il.shape() << std::endl;
64
65 // construct an array from a lazy expression
66 nda::array<int, 1> A1_sum = A1_il + A1_il;
67 std::cout << "A1_sum = " << A1_sum << std::endl;
68 std::cout << "A1_sum.size() = " << A1_sum.size() << std::endl;
69 std::cout << "A1_sum.shape() = " << A1_sum.shape() << std::endl;
70
71 // construct an array from another array with a different memory layout
73 std::cout << "A2_f = " << A2_f << std::endl;
74 std::cout << "A2_f.size() = " << A2_f.size() << std::endl;
75 std::cout << "A2_f.shape() = " << A2_f.shape() << std::endl;
76
77 // 1-dimensional array with only even integers from 2 to 20
78 auto v4 = nda::arange(2, 20, 2);
79 std::cout << "v4 = " << v4 << std::endl;
80
81 // 3x3 identity matrix
82 auto I = nda::eye<double>(3);
83 std::cout << "I = " << I << std::endl;
84
85 // 2x2x2 array with random numbers from 0 to 1
86 auto R = nda::rand(2, 2, 2);
87 std::cout << "R = " << R << std::endl;
88 std::cout << "R.shape() = " << R.shape() << std::endl;
89
90 // 3x6 array with zeros
91 auto Z = nda::zeros<double>(3, 6);
92 std::cout << "Z = " << Z << std::endl;
93}
auto const & shape() const noexcept
Get the shape of the view/array.
long size() const noexcept
Get the total size of the view/array.
auto eye(Int dim)
Create an identity nda::matrix with ones on the diagonal.
auto zeros(std::array< Int, Rank > const &shape)
Make an array of the given shape on the given address space and zero-initialize it.
auto arange(long first, long last, long step=1)
Make a 1-dimensional integer array and initialize it with values of a given nda::range.
auto rand(std::array< Int, Rank > const &shape)
Make an array of the given shape and initialize it with random values from the uniform distribution o...
basic_array< ValueType, Rank, Layout, 'A', ContainerPolicy > array
Alias template of an nda::basic_array with an 'A' algebra.
basic_array< ValueType, 1, C_layout, 'V', ContainerPolicy > vector
Alias template of an nda::basic_array with rank 1 and a 'V' algebra.
basic_array< ValueType, 2, Layout, 'M', ContainerPolicy > matrix
Alias template of an nda::basic_array with rank 2 and an 'M' algebra.
Includes all relevant headers for the core nda library.