TRIQS/nda 2.0.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
ex1.cpp
1#include <nda/nda.hpp>
2#include <nda/h5.hpp>
3#include <nda/linalg.hpp>
4#include <h5/h5.hpp>
5#include <iostream>
6
7int main() {
8 // create a 3x2 integer array and initialize it
9 auto A = nda::array<int, 2>(3, 2);
10 for (int i = 0; auto &x : A) x = i++;
11
12 // create a 3x2 integer array using initializer lists
13 auto A2 = nda::array<int, 2>{{0, 1}, {2, 3}, {4, 5}};
14
15 // create a 3x2 integer array in Fortran order and initialize it
17 for (int i = 0; auto &x : B) x = i++;
18
19 // print the formatted arrays, its sizes and its shapes
20 std::cout << "A = " << A << std::endl;
21 std::cout << "A.size() = " << A.size() << std::endl;
22 std::cout << "A.shape() = " << A.shape() << std::endl;
23 std::cout << std::endl;
24 std::cout << "B = " << B << std::endl;
25 std::cout << "B.size() = " << B.size() << std::endl;
26 std::cout << "B.shape() = " << B.shape() << std::endl;
27
28 // access and assign to a single element
29 A(2, 1) = 100;
30 B(2, 1) = A(2, 1);
31 std::cout << "A = " << A << std::endl;
32 std::cout << "B = " << B << std::endl;
33 std::cout << "B(2, 1) = " << B(2, 1) << std::endl;
34
35 // assigning a scalar to an array
36 A = 42;
37 std::cout << "A = " << A << std::endl;
38
39 // assigning an array to another array
40 A = B;
41 std::cout << "A = " << A << std::endl;
42
43 // create a view on A
44 auto A_v = A();
45 std::cout << "A_v =" << A_v << std::endl;
46 std::cout << "A_v.size() = " << A_v.size() << std::endl;
47 std::cout << "A_v.shape() = " << A_v.shape() << std::endl;
48
49 // manipulate the data in the view
50 A_v(0, 1) = -12;
51 std::cout << "A = " << A << std::endl;
52
53 // create a slice on A (a view on its second row)
54 auto A_s = A(1, nda::range::all);
55 std::cout << "A_s = " << A_s << std::endl;
56 std::cout << "A_s.size() = " << A_s.size() << std::endl;
57 std::cout << "A_s.shape() = " << A_s.shape() << std::endl;
58
59 // assign to a slice
60 A_s = nda::array<int, 1>{-1, -4};
61 std::cout << "A_s = " << A_s << std::endl;
62 std::cout << "A = " << A << std::endl;
63
64 // add two arrays element-wise
65 auto C = nda::array<int, 2>({{1, 2}, {3, 4}});
66 auto D = nda::array<int, 2>({{5, 6}, {7, 8}});
67 std::cout << C + D << std::endl;
68
69 // evaluate the lazy expression by assigning it to an existing array
70 auto E = nda::array<int, 2>(2, 2);
71 E = C + D;
72 std::cout << "E = " << E << std::endl;
73
74 // evaluate the lazy expression by constructing a new array
75 std::cout << "nda::array<int, 2>{C + D} = " << nda::array<int, 2>{C + D} << std::endl;
76
77 // evaluate the lazy expression using nda::make_regular
78 std::cout << "nda::make_regular(C + D) = " << nda::make_regular(C + D) << std::endl;
79
80 // multiply two arrays elementwise
81 std::cout << "C * D =" << nda::array<int, 2>(C * D) << std::endl;
82
83 // multiply two matrices
84 auto M1 = nda::matrix<int>({{1, 2}, {3, 4}});
85 auto M2 = nda::matrix<int>({{5, 6}, {7, 8}});
86 std::cout << "M1 * M2 =" << nda::matrix<int>(M1 * M2) << std::endl;
87
88 // element-wise square
90 std::cout << "C^2 =" << C_sq << std::endl;
91
92 // element-wise square root
93 nda::array<double, 2> C_sq_sqrt = nda::sqrt(C_sq);
94 std::cout << "sqrt(C^2) =" << C_sq_sqrt << std::endl;
95
96 // trace of a matrix
97 std::cout << "trace(M1) = " << nda::trace(M1) << std::endl;
98
99 // find the minimum/maximum element in an array
100 std::cout << "min_element(C) =" << nda::min_element(C) << std::endl;
101 std::cout << "max_element(C) =" << nda::max_element(C) << std::endl;
102
103 // is any (are all) element(s) greater than 1?
104 auto greater1 = nda::map([](int x) { return x > 1; })(C);
105 std::cout << "any(C > 1) = " << nda::any(greater1) << std::endl;
106 std::cout << "all(C > 1) = " << nda::all(greater1) << std::endl;
107
108 // sum/multiply all elements in an array
109 std::cout << "sum(C) = " << nda::sum(C) << std::endl;
110 std::cout << "product(C) = " << nda::product(C) << std::endl;
111
112 // write an array to an HDF5 file
113 h5::file out_file("ex1.h5", 'w');
114 h5::write(out_file, "C", C);
115
116 // read an array from an HDF5 file
117 nda::array<int, 2> C_copy;
118 h5::file in_file("ex1.h5", 'r');
119 h5::read(in_file, "C", C_copy);
120 std::cout << "C_copy = " << C_copy << std::endl;
121
122 // create a 2x2 matrix
123 auto M3 = nda::matrix<double>{{1, 2}, {3, 4}};
124
125 // get the inverse of the matrix (calls LAPACK routines)
126 auto M3_inv = nda::linalg::inv(M3);
127 std::cout << "M3_inv = " << M3_inv << std::endl;
128
129 // get the inverse of a matrix manually using its adjugate and determinant
130 auto M3_adj = nda::matrix<double>{{4, -2}, {-3, 1}};
131 auto M3_det = nda::linalg::det(M3);
132 auto M3_inv2 = nda::matrix<double>(M3_adj / M3_det);
133 std::cout << "M3_inv2 = " << M3_inv2 << std::endl;
134
135 // check the inverse using matrix-matrix multiplication
136 std::cout << "M3 * M3_inv = " << M3 * M3_inv << std::endl;
137 std::cout << "M3_inv * M3 = " << M3_inv * M3 << std::endl;
138
139 // solve a linear system using the inverse
140 auto b = nda::vector<double>{5, 8};
141 auto x = M3_inv * b;
142 std::cout << "M3 * x = " << M3 * x << std::endl;
143
144 // initialize a 4x7 array using CLEF automatic assignment
145 using namespace nda::clef::literals;
146 auto F = nda::array<int, 2>(4, 7);
147 F(i_, j_) << i_ * 7 + j_;
148 std::cout << "F = " << F << std::endl;
149}
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.
bool any(A const &a)
Does any of the elements of the array evaluate to true?
auto product(A const &a)
Multiply all the elements of an nda::Array object.
auto min_element(A const &a)
Find the minimum element of an array.
bool all(A const &a)
Do all elements of the array evaluate to true?
decltype(auto) make_regular(A &&a)
Make a given object regular.
auto trace(M const &m)
Get the trace of a 2-dimensional square array/view.
auto pow(A &&a, double p)
Function pow for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types).
auto sqrt(A &&a)
Function sqrt for non-matrix nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types...
mapped< F > map(F f)
Create a lazy function call expression on arrays/views.
Definition map.hpp:206
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.
constexpr auto j_
Generic placeholder #2.
Definition literals.hpp:29
constexpr auto i_
Generic placeholder #1.
Definition literals.hpp:26
auto det(M const &m)
Compute the determinant of an matrix .
Definition det.hpp:95
auto inv(M const &m)
Compute the inverse of an matrix .
Definition inv.hpp:141
Provides HDF5 support for the nda library.
Includes all relevant headers for the linear algebra functionality.
Includes all relevant headers for the core nda library.