TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
matrix_functions.hpp
Go to the documentation of this file.
1// Copyright (c) 2019--present, The Simons Foundation
2// This file is part of TRIQS/nda and is licensed under the Apache License, Version 2.0.
3// SPDX-License-Identifier: Apache-2.0
4// See LICENSE in the root of this distribution for details.
5
10
11#pragma once
12
13#include "./accessors.hpp"
14#include "./concepts.hpp"
15#include "./declarations.hpp"
16#include "./layout/policies.hpp"
18#include "./macros.hpp"
20#include "./mem/policies.hpp"
21#include "./stdutil/array.hpp"
22
23#include <algorithm>
24#include <concepts>
25#include <iostream>
26#include <ranges>
27#include <type_traits>
28
29namespace nda {
30
35
44 template <Scalar S = double, std::integral Int = long>
45 auto eye(Int dim) {
46 auto r = matrix<S>(dim, dim);
47 r = S{1};
48 return r;
49 }
50
59 template <ArrayOfRank<2> M>
60 auto trace(M const &m) {
61 static_assert(get_rank<M> == 2, "Error in nda::trace: Array/View must have rank 2");
62 EXPECTS(m.shape()[0] == m.shape()[1]);
63 auto r = get_value_t<M>{};
64 auto d = m.shape()[0];
65 for (int i = 0; i < d; ++i) r += m(i, i);
66 return r;
67 }
68
80 template <ArrayOfRank<2> M>
81 ArrayOfRank<2> auto dagger(M const &m) {
83 return conj(transpose(m));
84 else
85 return transpose(m);
86 }
87
95 template <MemoryArrayOfRank<2> M>
96 ArrayOfRank<1> auto diagonal(M &&m) { // NOLINT
97 long dim = std::min(m.shape()[0], m.shape()[1]);
98 long stride = stdutil::sum(m.indexmap().strides());
99 using vector_view_t =
100 basic_array_view<std::remove_reference_t<decltype(*m.data())>, 1, C_stride_layout, 'V', nda::default_accessor, nda::borrowed<>>;
101 return vector_view_t{C_stride_layout::mapping<1>{{dim}, {stride}}, m.data()};
102 }
103
111 template <typename V>
112 requires(std::ranges::contiguous_range<V> or ArrayOfRank<V, 1>)
113 ArrayOfRank<2> auto diag(V const &v) {
114 if constexpr (std::ranges::contiguous_range<V>) {
115 return diag(nda::basic_array_view{v});
116 } else {
117 auto m = matrix<std::remove_const_t<typename V::value_type>>::zeros({v.size(), v.size()});
118 diagonal(m) = v;
119 return m;
120 }
121 }
122
137 template <ArrayOfRank<2> A, ArrayOfRank<2> B>
138 requires(std::same_as<get_value_t<A>, get_value_t<B>>) // NB the get_value_t gets rid of const if any
139 matrix<get_value_t<A>> vstack(A const &a, B const &b) {
140 static_assert(get_rank<A> == 2, "Error in nda::vstack: Only rank 2 arrays/views are allowed");
141 static_assert(get_rank<A> == 2, "Error in nda::vstack: Only rank 2 arrays/views are allowed");
142 EXPECTS_WITH_MESSAGE(a.shape()[1] == b.shape()[1], "Error in nda::vstack: The second dimension of the two matrices must be equal");
143
144 auto [n, q] = a.shape();
145 auto p = b.shape()[0];
146 matrix<get_value_t<A>> res(n + p, q);
147 res(range(0, n), range::all) = a;
148 res(range(n, n + p), range::all) = b;
149 return res;
150 }
151
161 template <Matrix A>
162 bool is_matrix_square(A const &a, bool print_error = false) {
163 auto const [m, n] = a.shape();
164 if (m != n and print_error) std::cerr << "Error in nda::is_matrix_square: Dimensions are: (" << m << "," << n << ")\n" << std::endl;
165 return m == n;
166 }
167
177 template <Matrix A>
178 bool is_matrix_diagonal(A const &a, bool print_error = false) {
179 bool const r = is_matrix_square(a) and a == diag(diagonal(a));
180 if (not r and print_error) std::cerr << "Error in nda::is_matrix_diagonal: Non-diagonal matrix: " << a << std::endl;
181 return r;
182 }
183
185
186} // namespace nda
Defines accessors for nda::array objects (cf. std::default_accessor).
Provides utility functions for std::array.
A generic view of a multi-dimensional array.
Check if a given type is an nda::Array of a certain rank.
Definition concepts.hpp:241
Provides concepts for the nda library.
Provides various convenient aliases and helper functions for nda::basic_array and nda::basic_array_vi...
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 zeros(std::array< Int, Rank > const &shape)
Make an array of the given shape on the given address space and zero-initialize it.
auto transpose(A &&a)
Transpose the memory layout of an nda::MemoryArray or an nda::expr_call.
bool is_matrix_diagonal(A const &a, bool print_error=false)
Check if a given matrix is diagonal, i.e. if it is square (see nda::is_matrix_square) and all the the...
ArrayOfRank< 1 > auto diagonal(M &&m)
Get a view of the diagonal of a 2-dimensional array/view.
bool is_matrix_square(A const &a, bool print_error=false)
Check if a given matrix is square, i.e. if the first dimension has the same extent as the second dime...
matrix< get_value_t< A > > vstack(A const &a, B const &b)
Stack two 2-dimensional arrays/views vertically.
auto trace(M const &m)
Get the trace of a 2-dimensional square array/view.
ArrayOfRank< 2 > auto dagger(M const &m)
Get the conjugate transpose of 2-dimensional array/view.
decltype(auto) conj(A &&a)
Function conj for nda::ArrayOrScalar types (lazy and coefficient-wise for nda::Array types with a com...
basic_array< ValueType, 2, Layout, 'M', ContainerPolicy > matrix
Alias template of an nda::basic_array with rank 2 and an 'M' algebra.
constexpr int get_rank
Constexpr variable that specifies the rank of an nda::Array or of a contiguous 1-dimensional range.
Definition traits.hpp:126
std::decay_t< decltype(get_first_element(std::declval< A const >()))> get_value_t
Get the value type of an array/view or a scalar type.
Definition traits.hpp:182
constexpr auto sum(std::array< T, R > const &a)
Calculate the sum of all elements in a std::array.
Definition array.hpp:316
constexpr bool is_complex_v
Constexpr variable that is true if type T is a std::complex type.
Definition traits.hpp:65
Provides definitions of various layout policies.
Provides functions to transform the memory layout of an nda::basic_array or nda::basic_array_view.
Macros used in the nda library.
Provides some custom implementations of standard mathematical functions used for lazy,...
Defines various memory handling policies.
Strided (non-contiguous) layout policy with C-order (row-major order).
Definition policies.hpp:68
idx_map< Rank, 0, C_stride_order< Rank >, layout_prop_e::none > mapping
Multi-dimensional to flat index mapping.
Definition policies.hpp:71
Memory policy using an nda::mem::handle_borrowed.
Definition policies.hpp:97
Default accessor for various array and view types.
Definition accessors.hpp:25