TRIQS/nda 2.0.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
ex7.cpp
1#include <nda/nda.hpp>
2#include <nda/sym_grp.hpp>
3#include <array>
4#include <complex>
5#include <functional>
6#include <iostream>
7#include <tuple>
8#include <vector>
9
10int main() {
11 // size of the matrix
12 constexpr int N = 3;
13
14 // some useful typedefs
15 using idx_t = std::array<long, 2>;
16 using sym_t = std::tuple<idx_t, nda::operation>;
17 using sym_func_t = std::function<sym_t(idx_t const &)>;
18
19 // define a hermitian symmetry (satisfies nda::NdaSymmetry)
20 auto h_symmetry = [](idx_t const &x) {
21 if (x[0] == x[1]) return sym_t{x, nda::operation{false, false}}; // sign flip = false, complex conjugate = false
22 return sym_t{idx_t{x[1], x[0]}, nda::operation{false, true}}; // sign flip = false, complex conjugate = true
23 };
24
25 // construct the symmetry group
27 auto grp = nda::sym_grp{A, std::vector<sym_func_t>{h_symmetry}};
28
29 // check the number of symmetry classes
30 std::cout << "Number of symmetry classes: " << grp.num_classes() << std::endl;
31
32 // print the symmetry classes
33 for (int i = 1; auto const &c : grp.get_sym_classes()) {
34 std::cout << "Symmetry class " << i << ":" << std::endl;
35 for (auto const &x : c) {
36 std::cout << " Idx: " << x.first << ", Sign flip: " << x.second.sgn << ", Complex conjugation: " << x.second.cc << std::endl;
37 }
38 ++i;
39 }
40
41 // print flat indices
42 nda::array<int, 2> B(3, 3);
43 for (int i = 0; auto &x : B) x = i++;
44 std::cout << B << std::endl;
45
46 // define an initializer function
47 int count_eval = 0;
48 auto init_func = [&count_eval, &B](idx_t const &idx) {
49 ++count_eval;
50 const double val = B(idx[0], idx[1]);
51 return std::complex<double>{val, val};
52 };
53
54 // initialize the array using the symmetry group
55 grp.init(A, init_func);
56 std::cout << "A = " << A << std::endl;
57
58 // check the number of evaluations
59 std::cout << "Number of evaluations: " << count_eval << std::endl;
60
61 // get representative elements
62 auto reps = grp.get_representative_data(A);
63 auto reps_view = nda::array_view<std::complex<double>, 1>(reps);
64 std::cout << "Representative elements = " << reps_view << std::endl;
65
66 // use representative data to initialize a new array
67 reps_view *= 2.0;
68 nda::array<std::complex<double>, 2> B_sym(N, N);
69 grp.init_from_representative_data(B_sym, reps);
70 std::cout << "B_sym = " << B_sym << std::endl;
71
72 // symmetrize an already symmetric array
73 auto v1 = grp.symmetrize(A);
74 std::cout << "Symmetrized A = " << A << std::endl;
75 std::cout << "Max. violation at index " << v1.second << " = " << v1.first << std::endl;
76
77 // change an off-diagonal element
78 A(0, 2) *= 2.0;
79 std::cout << "A = " << A << std::endl;
80
81 // symmetrize again
82 auto v2 = grp.symmetrize(A);
83 std::cout << "Symmetrized A = " << A << std::endl;
84 std::cout << "Max. violation at index " << v2.second << " = " << v2.first << std::endl;
85}
Class representing a symmetry group.
Definition sym_grp.hpp:135
long num_classes() const
Get the number of symmetry classes.
Definition sym_grp.hpp:167
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.
A structure to capture combinations of complex conjugation and sign flip operations.
Definition sym_grp.hpp:37
Provides tools to use symmetries with nda objects.