Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
conjugate.hpp
Go to the documentation of this file.
1#pragma once
2
13
14#include <cstddef>
15#include <memory>
16#include <new>
17#include <type_traits>
18#include <utility>
19
20namespace uni20
21{
23template <TensorView Tensor> class ConstTensorView {
24 public:
25 using tensor_type = std::remove_cvref_t<Tensor>;
28 using async_alias_tag = void;
29
31 explicit constexpr ConstTensorView(tensor_type const& tensor) noexcept : tensor_(std::addressof(tensor)) {}
32
36 explicit constexpr ConstTensorView(tensor_type const* tensor) noexcept : tensor_(tensor) {}
37
38#if UNI20_HAS_MPREAL
41 [[nodiscard]] Precision default_precision() const
43 {
44 return this->base().default_precision();
45 }
46
48 [[nodiscard]] std::optional<Precision> default_precision_if_set() const
50 {
51 return this->base().default_precision_if_set();
52 }
53#endif
54
56 [[nodiscard]] constexpr decltype(auto) backend_selector() const
57 noexcept(noexcept(std::declval<tensor_type const&>().backend_selector()))
58 {
59 return this->base().backend_selector();
60 }
61
63 [[nodiscard]] constexpr decltype(auto) mdspan() const
64 noexcept(noexcept(std::declval<tensor_type const&>().mdspan()))
66 {
67 return this->base().mdspan();
68 }
69
71 [[nodiscard]] constexpr decltype(auto) mdspec() const
72 noexcept(noexcept(mdspec_of(std::declval<tensor_type const&>())))
73 {
74 return mdspec_of(this->base());
75 }
76
78 [[nodiscard]] constexpr decltype(auto) extents() const
79 noexcept(noexcept(std::declval<tensor_type const&>().extents()))
80 {
81 return this->base().extents();
82 }
83
85 [[nodiscard]] constexpr decltype(auto) extent(std::size_t axis) const
86 noexcept(noexcept(std::declval<tensor_type const&>().extent(axis)))
87 {
88 return this->base().extent(axis);
89 }
90
92 [[nodiscard]] constexpr tensor_type const& base() const noexcept { return *std::launder(tensor_); }
93
94 private:
95 tensor_type const* tensor_;
96};
97
102template <TensorView Tensor> class ConjugatedTensorView {
103 public:
104 using tensor_type = std::remove_cvref_t<Tensor>;
107 using async_alias_tag = void;
108
110 explicit constexpr ConjugatedTensorView(tensor_type const& tensor) noexcept : tensor_(std::addressof(tensor)) {}
111
115 explicit constexpr ConjugatedTensorView(tensor_type const* tensor) noexcept : tensor_(tensor) {}
116
117#if UNI20_HAS_MPREAL
120 [[nodiscard]] Precision default_precision() const
122 {
123 return this->base().default_precision();
124 }
125
127 [[nodiscard]] std::optional<Precision> default_precision_if_set() const
129 {
130 return this->base().default_precision_if_set();
131 }
132#endif
133
135 [[nodiscard]] constexpr decltype(auto) backend_selector() const
136 noexcept(noexcept(std::declval<tensor_type const&>().backend_selector()))
137 {
138 return this->base().backend_selector();
139 }
140
142 [[nodiscard]] constexpr auto mdspan() const
143 noexcept(noexcept(uni20::conj(std::declval<tensor_type const&>().mdspan())))
145 {
146 return uni20::conj(this->base().mdspan());
147 }
148
150 [[nodiscard]] constexpr auto mdspec() const
151 noexcept(noexcept(uni20::conj(mdspec_of(std::declval<tensor_type const&>()))))
152 {
153 return uni20::conj(mdspec_of(this->base()));
154 }
155
157 [[nodiscard]] constexpr decltype(auto) extents() const
158 noexcept(noexcept(std::declval<tensor_type const&>().extents()))
159 {
160 return this->base().extents();
161 }
162
164 [[nodiscard]] constexpr decltype(auto) extent(std::size_t axis) const
165 noexcept(noexcept(std::declval<tensor_type const&>().extent(axis)))
166 {
167 return this->base().extent(axis);
168 }
169
171 [[nodiscard]] constexpr tensor_type const& base() const noexcept { return *std::launder(tensor_); }
172
173 private:
174 tensor_type const* tensor_;
175};
176
178template <TensorView Tensor>
179 requires(std::is_lvalue_reference_v<Tensor &&> && Complex<tensor_element_t<Tensor>>)
180[[nodiscard]] constexpr auto conj(Tensor&& tensor)
181{
183}
184
186template <TensorView Tensor> [[nodiscard]] constexpr auto conj(ConjugatedTensorView<Tensor>& view) noexcept
187{
189}
190
192template <TensorView Tensor> [[nodiscard]] constexpr auto conj(ConjugatedTensorView<Tensor> const& view) noexcept
193{
195}
196
198template <TensorView Tensor> [[nodiscard]] constexpr auto conj(ConjugatedTensorView<Tensor>&& view) noexcept
199{
201}
202
204template <TensorView Tensor>
205 requires Complex<tensor_element_t<Tensor>>
206[[nodiscard]] constexpr auto conj(ConstTensorView<Tensor>& view) noexcept
207{
209}
210
212template <TensorView Tensor>
213 requires Complex<tensor_element_t<Tensor>>
214[[nodiscard]] constexpr auto conj(ConstTensorView<Tensor> const& view) noexcept
215{
217}
218
220template <TensorView Tensor>
221 requires Complex<tensor_element_t<Tensor>>
222[[nodiscard]] constexpr auto conj(ConstTensorView<Tensor>&& view) noexcept
223{
225}
226
228template <TensorView Tensor>
229 requires(std::is_lvalue_reference_v<Tensor &&> && !Complex<tensor_element_t<Tensor>>)
230[[nodiscard]] constexpr auto conj(Tensor&& tensor) noexcept -> std::remove_reference_t<Tensor> const&
231{
232 return tensor;
233}
234
235} // namespace uni20
Non-owning tensor view whose immediate or deferred metadata presents conjugated values.
Definition conjugate.hpp:102
constexpr decltype(auto) backend_selector() const noexcept(noexcept(std::declval< tensor_type const & >().backend_selector()))
Return the underlying tensor's backend selector.
Definition conjugate.hpp:135
constexpr decltype(auto) extent(std::size_t axis) const noexcept(noexcept(std::declval< tensor_type const & >().extent(axis)))
Return one underlying tensor extent.
Definition conjugate.hpp:164
constexpr ConjugatedTensorView(tensor_type const *tensor) noexcept
Bind a conjugating view to externally retained tensor storage.
Definition conjugate.hpp:115
constexpr ConjugatedTensorView(tensor_type const &tensor) noexcept
Bind a lazy conjugating view to an existing tensor.
Definition conjugate.hpp:110
constexpr decltype(auto) extents() const noexcept(noexcept(std::declval< tensor_type const & >().extents()))
Return the underlying tensor extents.
Definition conjugate.hpp:157
constexpr auto mdspan() const noexcept(noexcept(uni20::conj(std::declval< tensor_type const & >().mdspan())))
Resolve the conjugating mdspan view.
Definition conjugate.hpp:142
constexpr tensor_type const & base() const noexcept
Return the tensor referenced by this view.
Definition conjugate.hpp:171
std::remove_cvref_t< Tensor > tensor_type
Definition conjugate.hpp:104
tensor_storage_policy_t< tensor_type > storage_policy
Definition conjugate.hpp:105
constexpr auto mdspec() const noexcept(noexcept(uni20::conj(mdspec_of(std::declval< tensor_type const & >()))))
Return conjugated immediate or deferred multidimensional metadata.
Definition conjugate.hpp:150
void async_alias_tag
Marks this descriptor as a structurally copied async alias.
Definition conjugate.hpp:107
Non-owning read-only view of a tensor-level object.
Definition conjugate.hpp:23
constexpr ConstTensorView(tensor_type const *tensor) noexcept
Bind a read-only view to externally retained tensor storage.
Definition conjugate.hpp:36
constexpr decltype(auto) mdspec() const noexcept(noexcept(mdspec_of(std::declval< tensor_type const & >())))
Return the underlying tensor's read-only immediate or deferred metadata.
Definition conjugate.hpp:71
constexpr decltype(auto) extent(std::size_t axis) const noexcept(noexcept(std::declval< tensor_type const & >().extent(axis)))
Return one underlying tensor extent.
Definition conjugate.hpp:85
constexpr decltype(auto) backend_selector() const noexcept(noexcept(std::declval< tensor_type const & >().backend_selector()))
Return the underlying tensor's backend selector.
Definition conjugate.hpp:56
constexpr decltype(auto) extents() const noexcept(noexcept(std::declval< tensor_type const & >().extents()))
Return the underlying tensor extents.
Definition conjugate.hpp:78
constexpr ConstTensorView(tensor_type const &tensor) noexcept
Bind a read-only view to an existing tensor.
Definition conjugate.hpp:31
void async_alias_tag
Marks this descriptor as a structurally copied async alias.
Definition conjugate.hpp:28
constexpr tensor_type const & base() const noexcept
Return the tensor referenced by this view.
Definition conjugate.hpp:92
constexpr decltype(auto) mdspan() const noexcept(noexcept(std::declval< tensor_type const & >().mdspan()))
Resolve the underlying tensor's read-only mdspan.
Definition conjugate.hpp:63
tensor_storage_policy_t< tensor_type > storage_policy
Definition conjugate.hpp:26
std::remove_cvref_t< Tensor > tensor_type
Definition conjugate.hpp:25
Exact arithmetic or an explicit binary working precision.
Definition core/precision.hpp:13
General-purpose owning tensor with immediate or deferred mdspan metadata.
Definition basic_tensor.hpp:109
TensorView whose mdspec is already an mdspan.
Definition tensor/concepts.hpp:100
Mdspan accessor adaptor for lazy complex conjugation.
uni20::complex< T > conj(detail::standard_complex< T > x)
Returns the complex conjugate for complex-valued scalars.
Definition math.hpp:70
Definition qnum.hpp:403
Definition async/async.hpp:26
typename detail::TensorStoragePolicy< std::remove_cvref_t< Tensor > >::type tensor_storage_policy_t
Storage-policy type exposed by a tensor-level object, or void.
Definition tensor/concepts.hpp:40
constexpr bool has_runtime_precision_v
Whether numerical values of this scalar family carry runtime precision.
Definition runtime_precision.hpp:10
constexpr decltype(auto) mdspec_of(Tensor &tensor) noexcept(noexcept(tensor.mdspec()))
Return a tensor's immediate or descriptor-backed multidimensional metadata.
Definition tensor/concepts.hpp:50
std::remove_cv_t< typename tensor_mdspec_t< T >::element_type > tensor_element_t
Scalar value type exposed by a tensor-level object.
Definition tensor/concepts.hpp:126
Concepts for tensor-level objects that resolve mdspan kernel operands.