35#include <initializer_list>
56template <
typename I,
bool = std::is_enum_v<I>>
60 : std::bool_constant<std::is_convertible_v<I, std::underlying_type_t<I>>> {};
70template <
typename T,
typename I>
73 if constexpr (std::is_enum_v<I>) {
77 if constexpr (std::is_signed_v<I> && std::is_unsigned_v<T>) {
79 }
else if constexpr (std::is_unsigned_v<I> && std::is_signed_v<T>) {
81 std::numeric_limits<T>::max()));
84 std::ostringstream
ss;
85 ss <<
"mptensor: index value " << +
value <<
" is out of range for "
86 << (std::is_signed_v<T> ?
"a signed" :
"an unsigned") <<
" index";
87 throw std::out_of_range(
ss.str());
89 return static_cast<T>(
value);
117 template <
typename U,
118 typename = std::enable_if_t<internal::is_index_value_v<U> &&
119 !std::is_same_v<U, T>>>
121 idx.reserve(index.size());
122 for (
const U&
v : index) idx.push_back(internal::checked_index_cast<T>(
v));
128 template <
typename...
Ints,
129 typename = std::enable_if_t<(
sizeof...(Ints) > 0) &&
130 (internal::is_index_value_v<Ints> && ...)>>
135 size_t size()
const {
return idx.size(); }
139 void sort() { std::sort(idx.begin(), idx.end()); }
150 const size_t n =
size();
153 std::vector<bool>
seen(
n,
false);
154 for (
size_t i = 0;
i <
n; ++
i) {
155 if (idx[
i] < 0 ||
static_cast<size_t>(idx[
i]) >=
n) {
156 std::ostringstream
ss;
157 ss <<
"mptensor: inverse_permutation() of " << *
this <<
": element "
159 <<
" at position " <<
i <<
" is out of range [0, " <<
n <<
")";
160 throw std::out_of_range(
ss.str());
162 const size_t j =
static_cast<size_t>(idx[
i]);
164 std::ostringstream
ss;
165 ss <<
"mptensor: inverse_permutation() of " << *
this <<
": " << +idx[
i]
166 <<
" appears more than once, so it is not a permutation";
167 throw std::invalid_argument(
ss.str());
170 inv[
j] =
static_cast<T>(
i);
184 idx.insert(idx.end(),
rhs.idx.begin(),
rhs.idx.end());
194 if (idx.
size() > 0)
os << idx[0];
195 for (
size_t i = 1;
i < idx.
size(); ++
i)
os <<
", " << idx[
i];
244 const std::ptrdiff_t
sn =
static_cast<std::ptrdiff_t
>(
n);
247 std::ostringstream
ss;
248 ss <<
"mptensor: " <<
kind <<
" " <<
v;
250 ss <<
" is out of range [" << -
sn <<
", " <<
sn
252 throw std::out_of_range(
ss.str());
254 return static_cast<size_t>(
v < 0 ?
v +
sn :
v);
271 size_t n,
size_t axis) {
272 const std::ptrdiff_t
pos =
static_cast<std::ptrdiff_t
>(
axis);
276 std::ostringstream
ss;
277 ss <<
"mptensor: slice [" <<
begin <<
", " <<
end <<
") on axis " <<
axis
278 <<
" is empty (normalized to [" <<
b <<
", " <<
e <<
"))";
279 throw std::out_of_range(
ss.str());
289 const size_t rank = shape.
size();
290 if (
begin.size() != rank ||
end.size() != rank) {
291 std::ostringstream
ss;
292 ss <<
"mptensor: slice bounds " <<
begin <<
" and " <<
end
293 <<
" do not match the tensor rank " << rank;
294 throw std::invalid_argument(
ss.str());
298 for (
size_t r = 0;
r < rank; ++
r) {
302 static_cast<std::ptrdiff_t
>(
r),
"axis");
306 const std::pair<size_t, size_t>
be =
326 for (
size_t i = 0;
i <
axes.size(); ++
i) {
328 static_cast<std::ptrdiff_t
>(
i));
344 std::ostringstream
ss;
345 ss <<
"mptensor: index " << idx <<
" has " << idx.
size()
346 <<
" elements, but the tensor has rank " << shape.
size();
347 throw std::invalid_argument(
ss.str());
351 for (
size_t k = 0;
k < idx.
size(); ++
k) {
353 static_cast<std::ptrdiff_t
>(
k));
369 for (
size_t k = 0;
k <
s.size(); ++
k) {
371 std::ostringstream
ss;
372 ss <<
"mptensor: shape " <<
s <<
" has a negative size at position " <<
k;
373 throw std::invalid_argument(
ss.str());
375 result[
k] =
static_cast<size_t>(
s[
k]);
385 for (
size_t k = 0;
k <
u.size(); ++
k) {
386 result[
k] = internal::checked_index_cast<std::ptrdiff_t>(
u[
k]);
401inline Index range(
const std::ptrdiff_t start,
const std::ptrdiff_t stop,
402 const std::ptrdiff_t
step) {
404 throw std::invalid_argument(
"mptensor: range() step must not be zero");
408 if (
step > 0 && start < stop) {
409 const size_t diff =
static_cast<size_t>(stop) -
static_cast<size_t>(start);
410 n = (
diff - 1) /
static_cast<size_t>(
step) + 1;
412 const size_t diff =
static_cast<size_t>(start) -
static_cast<size_t>(stop);
418 std::ptrdiff_t
v = start;
419 for (
size_t i = 0;
i <
n; ++
i) {
428inline Index range(
const std::ptrdiff_t start,
const std::ptrdiff_t stop) {
429 return range(start, stop, 1);
BasicIndex & operator+=(const BasicIndex &rhs)
Definition index.hpp:183
BasicIndex inverse_permutation() const
Inverse of a permutation: inv[(*this)[i]] = i.
Definition index.hpp:149
BasicIndex inverse() const
Same as inverse_permutation().
Definition index.hpp:177
const T & operator[](size_t i) const
Definition index.hpp:133
BasicIndex(const std::vector< U > &index)
Converting constructor from a vector of another integer type.
Definition index.hpp:120
void resize(size_t n)
Definition index.hpp:137
BasicIndex(std::initializer_list< T > list)
Definition index.hpp:124
bool operator==(const BasicIndex &rhs) const
Definition index.hpp:181
void push(T i)
Definition index.hpp:136
T value_type
Definition index.hpp:104
BasicIndex(Ints... js)
Python-like list literal, e.g. Index(0, 1, -1).
Definition index.hpp:131
std::vector< T > index_t
Definition index.hpp:105
T & operator[](size_t i)
Definition index.hpp:134
void sort()
Definition index.hpp:139
BasicIndex(const index_t &index)
Definition index.hpp:115
void assign(size_t n, const T j[])
Definition index.hpp:138
size_t size() const
Definition index.hpp:135
std::complex< double > complex
Definition complex.hpp:38
internal::UShape to_internal_shape(const Shape &s)
Definition index.hpp:366
Index to_public(const internal::UIndex &u)
Definition index.hpp:382
Index range(const std::ptrdiff_t start, const std::ptrdiff_t stop, const std::ptrdiff_t step)
Arithmetic sequence start, start+step, ... up to (not including) stop.
Definition index.hpp:401
std::ostream & operator<<(std::ostream &os, const BasicIndex< T > &idx)
Definition index.hpp:192
internal::UAxes normalize_axes(const Axes &axes, size_t rank)
Definition index.hpp:323
size_t normalize_index(std::ptrdiff_t i, size_t n)
Definition index.hpp:335
size_t normalize_axis(std::ptrdiff_t a, size_t rank)
Definition index.hpp:317
size_t normalize_slice_end(std::ptrdiff_t e, size_t n)
Definition index.hpp:360
BasicIndex< T > operator+(const BasicIndex< T > &lhs, const BasicIndex< T > &rhs)
Definition index.hpp:203
BasicIndex< std::ptrdiff_t > Index
Definition index.hpp:209
size_t normalize_value(std::ptrdiff_t v, size_t n, bool end_inclusive, const char *kind, std::ptrdiff_t position, const char *position_name="position")
Definition index.hpp:241
void normalize_slice_ranges(const Index &begin, const Index &end, const UShape &shape, UIndex &ubegin, UIndex &uend)
Definition index.hpp:286
T checked_index_cast(I value)
Definition index.hpp:71
std::pair< size_t, size_t > normalize_slice_range(std::ptrdiff_t begin, std::ptrdiff_t end, size_t n, size_t axis)
Definition index.hpp:269
constexpr bool is_index_value_v
Definition index.hpp:65
BasicIndex< std::size_t > UIndex
Definition index.hpp:221
UAxes identity_axes(size_t n)
Definition index.hpp:259
Definition complex.hpp:34