TRIQS/nda 2.0.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
ex8.cpp
1#include <nda/nda.hpp>
2#include <nda/linalg.hpp>
3#include <nda/lapack.hpp>
4#include <complex>
5#include <iostream>
6
7int main() {
8 // construct a 4x3 matrix and a vector of size 3
11
12 // construct a 3x3 identity matrix
13 auto I = nda::eye<double>(3);
14 std::cout << "I = " << I << std::endl;
15
16 // construct a 2x2 diagonal matrix
17 auto D = nda::diag(nda::array<int, 1>{1, 2});
18 std::cout << "D = " << D << std::endl;
19
20 // assigning a scalar to a matrix and vector
21 M = 3.1415;
22 v = 2.7182;
23 std::cout << "M = " << M << std::endl;
24 std::cout << "v = " << v << std::endl;
25
26 // get a vector view of the diagonal of a matrix
27 auto d = nda::diagonal(D);
28 std::cout << "d = " << d << std::endl;
29 std::cout << "Algebra of d: " << nda::get_algebra<decltype(d)> << std::endl;
30
31 // transform an array into a matrix view
32 auto A = nda::array<int, 2>{{1, 2}, {3, 4}};
33 auto A_mv = nda::make_matrix_view(A);
34 std::cout << "A_mv = " << A_mv << std::endl;
35 std::cout << "Algebra of A: " << nda::get_algebra<decltype(A)> << std::endl;
36 std::cout << "Algebra of A_mv: " << nda::get_algebra<decltype(A_mv)> << std::endl;
37
38 // take the cross product of two vectors
39 auto v1 = nda::vector<double>{1.0, 2.0, 3.0};
40 auto v2 = nda::vector<double>{4.0, 5.0, 6.0};
41 auto v3 = nda::linalg::cross_product(v1, v2);
42 std::cout << "v1 = " << v1 << std::endl;
43 std::cout << "v2 = " << v2 << std::endl;
44 std::cout << "v3 = v1 x v2 = " << v3 << std::endl;
45
46 // check the cross product using the dot product
47 std::cout << "v1 . v3 = " << nda::linalg::dot(v1, v3) << std::endl;
48 std::cout << "v2 . v3 = " << nda::linalg::dot(v2, v3) << std::endl;
49
50 // cross product via matrix-vector product
51 auto M_v1 = nda::matrix<double>{{0, -v1[2], v1[1]}, {v1[2], 0, -v1[0]}, {-v1[1], v1[0], 0}};
52 std::cout << "M_v1 = " << M_v1 << std::endl;
53 auto v3_mv = M_v1 * v2;
54 std::cout << "v3_mv = " << v3_mv << std::endl;
55
56 // define a symmetric matrix
57 auto M1 = nda::matrix<double>{{4, -14, -12}, {-14, 10, 13}, {-12, 13, 1}};
58 std::cout << "M1 = " << M1 << std::endl;
59
60 // calculate the eigenvalues and eigenvectors of a symmetric matrix
61 auto [s, Q] = nda::linalg::eigh(M1);
62 std::cout << "Eigenvalues of M1: s = " << s << std::endl;
63 std::cout << "Eigenvectors of M1: Q = " << Q << std::endl;
64
65 // check the eigendecomposition
66 auto M1_reconstructed = Q * nda::diag(s) * nda::transpose(Q);
67 std::cout << "M1_reconstructed = " << M1_reconstructed << std::endl;
68
69 // define the linear system of equations and the right hand side matrix
70 auto A1 = nda::matrix<double, nda::F_layout>{{3, 2, -1}, {2, -2, 4}, {-1, 0.5, -1}};
71 auto b1 = nda::vector<double>{1, -2, 0};
72 std::cout << "A1 = " << A1 << std::endl;
73 std::cout << "b1 = " << b1 << std::endl;
74
75 // LU factorization using the LAPACK interface
76 auto ipiv = nda::vector<int>(3);
77 auto LU = A1;
78 auto info = nda::lapack::getrf(LU, ipiv);
79 if (info != 0) {
80 std::cerr << "Error: nda::lapack::getrf failed with error code " << info << std::endl;
81 return 1;
82 }
83
84 // solve the linear system of equations using the LU factorization
86 x1(nda::range::all, 0) = b1;
87 info = nda::lapack::getrs(LU, x1, ipiv);
88 if (info != 0) {
89 std::cerr << "Error: nda::lapack::getrs failed with error code " << info << std::endl;
90 return 1;
91 }
92 std::cout << "x1 = " << x1(nda::range::all, 0) << std::endl;
93
94 // check the solution
95 std::cout << "A1 * x1 = " << A1 * x1(nda::range::all, 0) << std::endl;
96}
auto eye(Int dim)
Create an identity nda::matrix with ones on the diagonal.
ArrayOfRank< 2 > auto diag(V const &v)
Get a new nda::matrix with the given values on the diagonal.
auto make_matrix_view(basic_array< T, R, LP, A, CP > const &a)
Make an nda::matrix_view of a given nda::basic_array.
auto transpose(A &&a)
Transpose the memory layout of an nda::MemoryArray or an nda::expr_call.
ArrayOfRank< 1 > auto diagonal(M &&m)
Get a view of the diagonal of a 2-dimensional array/view.
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 char get_algebra
Constexpr variable that specifies the algebra of a type.
Definition traits.hpp:116
int getrf(A &&a, IPIV &&ipiv)
Interface to the LAPACK getrf routine.
Definition getrf.hpp:58
int getrs(A const &a, B &&b, IPIV const &ipiv)
Interface to the LAPACK getrs routine.
Definition getrs.hpp:53
auto eigh(A const &a)
Compute the eigenvalues and eigenvectors of a real symmetric or complex hermitian matrix.
Definition eigh.hpp:162
auto dot(X const &x, Y const &y)
Compute the dot product of two nda::vector objects or the product of two scalars.
Definition dot.hpp:104
auto cross_product(X const &x, Y const &y)
Compute the cross product of two 3-dimensional vectors and .
Includes all LAPACK relevant headers.
Includes all relevant headers for the linear algebra functionality.
Includes all relevant headers for the core nda library.