18
19
20
24#include "./accessors.hpp"
25#include "./concepts.hpp"
26#include "./declarations.hpp"
27#include "./layout/policies.hpp"
28#include "./layout_transforms.hpp"
29#include "./macros.hpp"
30#include "./mapped_functions.hpp"
31#include "./mem/policies.hpp"
32#include "./stdutil/array.hpp"
42
43
44
47
48
49
50
51
52
53
54 template <Scalar S,
std::integral Int =
long>
56 auto r = matrix<S>(dim, dim);
62
63
64
65
66
67
68
69 template <ArrayOfRank<2> M>
71 static_assert(get_rank<M> == 2,
"Error in nda::trace: Array/View must have rank 2");
72 EXPECTS(m.shape()[0] == m.shape()[1]);
73 auto r = get_value_t<M>{};
74 auto d = m.shape()[0];
75 for (
int i = 0; i < d; ++i) r += m(i, i);
80
81
82
83
84
85
86
87
88
89
90 template <ArrayOfRank<2> M>
92 if constexpr (is_complex_v<
typename M::value_type>)
93 return conj(transpose(m));
99
100
101
102
103
104
105 template <MemoryArrayOfRank<2> M>
107 long dim = std::min(m.shape()[0], m.shape()[1]);
108 long stride = stdutil::sum(m.indexmap().strides());
109 using vector_view_t =
115
116
117
118
119
120
121 template <
typename V>
122 requires(std::ranges::contiguous_range<V>
or ArrayOfRank<V, 1>)
124 if constexpr (std::ranges::contiguous_range<V>) {
127 auto m = matrix<std::remove_const_t<
typename V::value_type>>::zeros({v.size(), v.size()});
134
135
136
137
138
139
140
141
142
143
144
145
146
147 template <ArrayOfRank<2> A, ArrayOfRank<2> B>
148 requires(std::same_as<get_value_t<A>, get_value_t<B>>)
149 matrix<get_value_t<A>>
vstack(A
const &a, B
const &b) {
150 static_assert(get_rank<A> == 2,
"Error in nda::vstack: Only rank 2 arrays/views are allowed");
151 static_assert(get_rank<A> == 2,
"Error in nda::vstack: Only rank 2 arrays/views are allowed");
152 EXPECTS_WITH_MESSAGE(a.shape()[1] == b.shape()[1],
"Error in nda::vstack: The second dimension of the two matrices must be equal");
154 auto [n, q] = a.shape();
155 auto p = b.shape()[0];
156 matrix<get_value_t<A>> res(n + p, q);
157 res(range(0, n), range::all) = a;
158 res(range(n, n + p), range::all) = b;
A generic view of a multi-dimensional array.
idx_map(std::array< long, Rank > const &shape, std::array< long, Rank > const &strides) noexcept(!check_stride_order)
Construct a new map from a given shape and strides.
auto eye(Int dim)
Create an identity nda::matrix with ones on the diagonal.
ArrayOfRank< 1 > auto diagonal(M &m)
Get a view of the diagonal of a 2-dimensional array/view.
ArrayOfRank< 2 > auto diag(V const &v)
Get a new nda::matrix with the given values on the diagonal.
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.
#define EXPECTS_WITH_MESSAGE(X,...)
Strided (non-contiguous) layout policy with C-order (row-major order).
Memory policy using an nda::mem::handle_borrowed.
Default accessor for various array and view types.