10#include "utils/dynamic_arg_resolver.hpp"
15#include <initializer_list>
18#include "backend/Scalar.hpp"
19#include "backend/Storage.hpp"
20#include "backend/Tensor_impl.hpp"
39 boost::intrusive_ptr<Tensor_impl>
_insimpl;
40 std::vector<cytnx::Accessor>
_accs;
41 Tproxy(boost::intrusive_ptr<Tensor_impl>
_ptr,
const std::vector<cytnx::Accessor> &
accs)
57 this->_insimpl->set(
_accs,
tmp._impl);
199 Scalar::Sproxy
item()
const {
225 template <
class...
Ts>
230 template <
class...
Ts>
235 template <
class...
Ts>
240 template <
class...
Ts>
241 const Tproxy operator()(
const std::string &
e1,
const Ts &...
elems)
const {
245 template <
class...
Ts>
250 template <
class...
Ts>
258 Tproxy operator[](
const std::initializer_list<cytnx::Accessor> &
accs) {
259 std::vector<cytnx::Accessor>
tmp =
accs;
262 Tproxy operator[](
const std::vector<cytnx::Accessor> &
accs) {
266 const Tproxy operator[](
const std::vector<cytnx::Accessor> &
accs)
const {
269 const Tproxy operator[](
const std::initializer_list<cytnx::Accessor> &
accs)
const {
270 std::vector<cytnx::Accessor>
tmp =
accs;
274 Tproxy operator[](
const std::initializer_list<cytnx_int64> &
accs) {
275 std::vector<cytnx_int64>
tmp =
accs;
278 Tproxy operator[](
const std::vector<cytnx_int64> &
accs) {
279 std::vector<cytnx::Accessor>
acc_in;
280 for (
int i = 0;
i <
accs.size();
i++) {
285 const Tproxy operator[](
const std::initializer_list<cytnx_int64> &
accs)
const {
286 std::vector<cytnx_int64>
tmp =
accs;
289 const Tproxy operator[](
const std::vector<cytnx_uint64> &
accs)
const {
290 std::vector<cytnx::Accessor>
acc_in;
291 for (
int i = 0;
i <
accs.size();
i++) {
296 const Tproxy operator[](
const std::vector<cytnx_int64> &
accs)
const {
297 std::vector<cytnx::Accessor>
acc_in;
298 for (
int i = 0;
i <
accs.size();
i++) {
307 void _Save(std::fstream &
f)
const;
308 void _Load(std::fstream &
f);
388 boost::intrusive_ptr<Tensor_impl>
_impl;
498 template <
typename Variant>
499 struct exclude_first;
501 template <
typename First,
typename... Rest>
502 struct exclude_first<std::variant<First, Rest...>> {
503 using type = std::variant<Rest...>;
522 template <
typename T>
525 "[ERROR] Attempt to convert dtype %d (%s) to pointer of type %s",
527 Type_class::getname(Type_class::cy_typeid_v<std::remove_cv_t<T>>).
c_str());
528 return static_cast<T *
>(this->_impl->_storage._impl->data());
545 template <
typename T>
548 this->
dtype() != Type_class::cy_typeid_gpu_v<std::remove_cv_t<T>>,
549 "[ERROR] Attempt to convert dtype %d (%s) to GPU pointer of type %s", this->
dtype(),
551 Type_class::getname(Type_class::cy_typeid_gpu_v<std::remove_cv_t<T>>).
c_str());
552 return static_cast<T *
>(this->_impl->_storage._impl->data());
574 unsigned int dtype()
const {
return this->_impl->dtype(); }
581 int device()
const {
return this->_impl->device(); }
588 std::string
dtype_str()
const {
return this->_impl->dtype_str(); }
595 std::string
device_str()
const {
return this->_impl->device_str(); }
601 const std::vector<cytnx_uint64> &
shape()
const {
return this->_impl->shape(); }
630 bool is_void()
const {
return this->_impl->is_void(); }
636 bool is_scalar()
const {
return this->_impl->is_scalar(); }
723 template <
class...
Ts>
726 this->_impl->permute_(
argv);
755 template <
class...
Ts>
831 this->_impl->reshape_(
shape);
839 template <
class...
Ts>
842 this->_impl->reshape_(
shape);
894 template <
class...
Ts>
965 const T &
at(
const std::vector<cytnx_uint64> &
locator)
const {
970 T &
at(
const std::initializer_list<cytnx_uint64> &
locator) {
974 const T &
at(
const std::initializer_list<cytnx_uint64> &
locator)
const {
977 template <
class T,
class...
Ts>
980 return this->
at<T>(argv);
982 template <
class T,
class...
Ts>
985 return this->
at<T>(argv);
988 const Scalar::Sproxy
at(
const std::vector<cytnx_uint64> &
locator)
const {
989 return this->_impl->at(
locator);
992 Scalar::Sproxy
at(
const std::vector<cytnx_uint64> &
locator) {
return this->_impl->at(
locator); }
993 const Scalar::Sproxy
at(
const std::initializer_list<cytnx_uint64> &
locator)
const {
994 return this->
at(std::vector<cytnx_uint64>(
locator));
996 Scalar::Sproxy
at(
const std::initializer_list<cytnx_uint64> &
locator) {
997 return this->
at(std::vector<cytnx_uint64>(
locator));
1028 cytnx_error_msg(this->_impl->storage().size() != 1,
"[ERROR][Tensor.item<T>]%s",
1029 "item can only be called from a Tensor with only one element\n");
1030 return this->_impl->storage().at<
T>(0);
1035 const T &
item()
const {
1036 cytnx_error_msg(this->_impl->storage().size() != 1,
"[ERROR][Tensor.item<T>]%s",
1037 "item can only be called from a Tensor with only one element\n");
1038 return this->_impl->storage().at<
T>(0);
1041 const Scalar::Sproxy
item()
const {
1042 cytnx_error_msg(this->_impl->storage().size() != 1,
"[ERROR][Tensor.item]%s",
1043 "item can only be called from a Tensor with only one element\n");
1044 Scalar::Sproxy
out(this->
storage()._impl, 0);
1048 Scalar::Sproxy
item() {
1049 cytnx_error_msg(this->_impl->storage().size() != 1,
"[ERROR][Tensor.item]%s",
1050 "item can only be called from a Tensor with only one element\n");
1051 Scalar::Sproxy
out(this->
storage()._impl, 0);
1082 std::vector<cytnx_int64> &
removed)
const {
1089 std::vector<cytnx_int64>
removed;
1148 void set(
const std::initializer_list<cytnx::Accessor> &
accessors,
const T &
rc) {
1150 this->
set(args,
rc);
1181 this->_impl->fill(
val);
1189 if (this->
shape() != rhs.
shape())
return false;
1324 return *
this +=
rhs;
1344 return *
this -=
rhs;
1364 return *
this *=
rhs;
1386 return *
this /=
rhs;
1397 return *
this ==
rhs;
1512 "[ERROR] try to append a Tensor with rank not match.%s",
"\n");
1514 for (
unsigned int i = 0;
i <
rhs.shape().
size();
i++) {
1516 "[ERROR] dimension mismatch @ rhs.rank: [%d] this: [%d] rhs: [%d]\n",
i,
1523 if (
rhs.dtype() !=
this->dtype()) {
1527 if (!
rhs.is_contiguous())
1532 this->_impl->_shape[0] += 1;
1535 memcpy(((
char *)this->_impl->_storage.data()) +
1537 in._impl->_storage.data(),
Type.typeSize(
in.dtype()) *
Nelem);
1569 cytnx_error_msg(srhs.size() == 0,
"[ERROR] try to append an empty Storage.%s",
"\n");
1571 "[ERROR] append a storage to Tensor can only accept rank-2 Tensor.%s",
"\n");
1579 if (
srhs.dtype() !=
this->dtype()) {
1584 this->_impl->_shape[0] += 1;
1586 this->_impl->_storage.resize(
oldsize +
in.size());
1587 memcpy(((
char *)this->_impl->_storage.data()) +
1589 in._impl->data(),
Type.typeSize(
in.dtype()) *
in.size());
1632 "[ERROR] trying to append a scalar into multidimentional Tensor is not "
1633 "allow.\n Only rank-1 Tensor can accept scalar append.%s",
1636 "[ERROR] append require the Tensor to be contiguous. suggestion: call "
1637 "contiguous() or contiguous_() first.",
1639 this->_impl->_shape[0] += 1;
1640 this->_impl->_storage.append(
rhs);
1666 std::vector<Tensor>
Eigh(
const bool &
is_V =
true,
const bool &
row_v =
false)
const;
1729 [[deprecated(
"use norm() (returns Scalar) instead")]]
Tensor Norm()
const;
1797 concept cytnx_scalar_value =
1798 CytnxType<std::remove_cvref_t<T>> || std::is_same_v<std::remove_cvref_t<T>, Scalar>;
1802 concept cytnx_scalar_like =
1803 cytnx_scalar_value<T> || std::is_same_v<std::remove_cvref_t<T>,
Tensor::Tproxy> ||
1804 std::is_same_v<std::remove_cvref_t<T>, Scalar::Sproxy>;
1809 concept cytnx_unitensor_scalar_like =
1810 cytnx_scalar_value<T> || std::is_same_v<std::remove_cvref_t<T>, Scalar::Sproxy>;
1814 template <cytnx_scalar_like T>
1816 template <cytnx_scalar_like T>
1818 template <cytnx_scalar_like T>
1820 template <cytnx_scalar_like T>
1835 std::ostream &operator<<(std::ostream &os,
const Tensor &in);
1836 std::ostream &operator<<(std::ostream &os,
const Tensor::Tproxy &in);
constexpr Type_class Type
data type
Definition Type.hpp:553
object that mimic the python slice to access elements in C++ [this is for c++ API only].
Definition Accessor.hpp:17
an tensor (multi-dimensional array)
Definition Tensor.hpp:33
void append(const Storage &srhs)
the append function of the Storage.
Definition Tensor.hpp:1566
bool is_void() const
whether the Tensor is uninitialized.
Definition Tensor.hpp:630
Tensor & operator*=(const T &rc)
multiplication assignment operator with a Tensor or a scalar.
Tensor & operator/=(const T &rc)
division assignment operator with a Tensor or a scalar.
Tensor operator-()
The negation function.
Definition Tensor.hpp:1449
void fill(const T &val)
fill all the element of current Tensor with the value.
Definition Tensor.hpp:1180
Tensor InvM() const
the InvM member function. Same as cytnx::linalg::InvM(const Tensor &Tin), where Tin is the current Te...
bool same_data(const Tensor &rhs) const
Check whether two tensors share the same internal memory.
void to_(const int &device)
move the current Tensor to the device.
Definition Tensor.hpp:710
Tensor & permute_(const std::vector< cytnx_uint64 > &rnks)
Definition Tensor.hpp:718
bool is_scalar() const
whether the Tensor is an initialized rank-0 scalar Tensor
Definition Tensor.hpp:636
Tensor reshape(const std::vector< cytnx_uint64 > &new_shape) const
Definition Tensor.hpp:879
void append(const T &rhs)
the append function of the scalar.
Definition Tensor.hpp:1629
Tensor & operator-=(const T &rc)
subtraction assignment operator with a Tensor or a scalar.
Tensor & Add_(const T &rhs)
Addition function with a Tensor or a scalar, inplacely. Same as operator+=(const T &rhs).
Definition Tensor.hpp:1323
std::vector< cytnx_int64 > strides() const
the storage strides of the Tensor
Tensor Abs() const
the Abs member function. Same as linalg::Abs(const Tensor &Tin), where Tin is the current Tensor.
Tensor reshape(const std::initializer_list< cytnx_int64 > &new_shape) const
Definition Tensor.hpp:889
std::string device_str() const
the device (in string) of the Tensor
Definition Tensor.hpp:595
Tensor contiguous_()
Make the Tensor contiguous by coalescing the memory (storage), inplacely.
Definition Tensor.hpp:799
Tensor Inv(const double &clip=-1.) const
Apply the inverse on each entry of the Tensor.
Tensor Mul(const T &rhs)
Multiplication function with a Tensor or a scalar. Same as cytnx::operator*(const Tensor &self,...
Definition Tensor.hpp:1353
unsigned int dtype() const
the dtype-id of the Tensor
Definition Tensor.hpp:574
Tensor Sub(const T &rhs)
Subtraction function with a Tensor or a scalar. Same as cytnx::operator-(const Tensor &self,...
Definition Tensor.hpp:1333
Tensor contiguous() const
Make the Tensor contiguous by coalescing the memory (storage).
Definition Tensor.hpp:779
void Tofile(const std::string &fname) const
Save current Tensor to the binary file.
T & at(const std::vector< cytnx_uint64 > &locator)
Get an element at specific location.
Definition Tensor.hpp:957
Tensor reshape(const std::vector< cytnx_int64 > &new_shape) const
return a new Tensor that is reshaped.
Definition Tensor.hpp:870
static Tensor Fromfile(const std::string &fname, const unsigned int &dtype, const cytnx_int64 &count=-1)
Load current Tensor from the binary file.
T & item()
get the element from a rank-0 Tensor.
Definition Tensor.hpp:1027
Tensor clone() const
return a clone of the current Tensor.
Definition Tensor.hpp:661
Tensor(const std::vector< cytnx_uint64 > &shape, unsigned int dtype=Type.Double, int device=-1, bool init_zero=true)
Construct a new Tensor object.
Definition Tensor.hpp:472
std::vector< Tensor > Eigh(const bool &is_V=true, const bool &row_v=false) const
the Eigh member function. Same as cytnx::linalg::Eigh(const Tensor &Tin, const bool &is_V,...
void Tofile(const char *fname) const
void append(const Tensor &rhs)
the append function.
Definition Tensor.hpp:1503
static Tensor Load(const char *fname)
void Save(const char *fname) const
void set(const std::vector< cytnx::Accessor > &accessors, const Tensor &rhs)
set elements with the input Tensor using Accessor (C++ API) / slices (python API)
Definition Tensor.hpp:1120
static Tensor Fromfile(const char *fname, const unsigned int &dtype, const cytnx_int64 &count=-1)
Tensor Norm() const
the Norm member function. Same as linalg::Norm(const Tensor &Tin), where Tin is the current Tensor.
Tensor astype(const int &new_type) const
return a new Tensor that cast to different dtype.
Definition Tensor.hpp:922
Tensor & Div_(const T &rhs)
Division function with a Tensor or a scalar, inplacely. Same as operator/=(const T &rhs).
Definition Tensor.hpp:1385
make_variant_from_transform_t< typename internal::exclude_first< Type_list >::type, std::add_pointer > pointer_types
Definition Tensor.hpp:511
pointer_types ptr() const
static Tensor Load(const std::string &fname)
Load current Tensor from file.
cytnx_uint64 size() const
Return the total number of logical elements in the Tensor.
Definition Tensor.hpp:624
Tensor & operator+=(const T &rc)
addition assignment operator with a Tensor or a scalar.
Tensor Conj() const
the Conj member function. Same as cytnx::linalg::Conj(const Tensor &Tin), where Tin is the current Te...
Tensor Trace(const cytnx_uint64 &a=0, const cytnx_uint64 &b=1) const
the Trace member function. Same as linalg::Trace(const Tensor &Tin, const cytnx_uint64 &a,...
bool is_empty() const
whether the Tensor is initialized and has no elements
Definition Tensor.hpp:642
bool equivshape(const Tensor &rhs)
compare the shape of two tensors.
Definition Tensor.hpp:1188
Tensor & Pow_(const cytnx_double &p)
the Pow_ member function. Same as linalg::Pow_(Tensor &Tin, const cytnx_double &p),...
std::vector< Tensor > Svd(const bool &is_UvT=true) const
the SVD member function. Same as cytnx::linalg::Svd(const Tensor &Tin, const bool &is_UvT) ,...
void Init(const std::vector< cytnx_uint64 > &shape, unsigned int dtype=Type.Double, int device=-1, bool init_zero=true)
initialize a Tensor
Definition Tensor.hpp:438
std::string dtype_str() const
the dtype (in string) of the Tensor
Definition Tensor.hpp:588
Tensor & Mul_(const T &rhs)
Multiplication function with a Tensor or a scalar, inplacely. Same as operator*=(const T &rhs).
Definition Tensor.hpp:1363
cytnx_uint64 rank() const
the rank of the Tensor
Definition Tensor.hpp:618
Tensor get(const std::vector< cytnx::Accessor > &accessors, std::vector< cytnx_int64 > &removed) const
get elements using Accessor (C++ API) / slices (python API)
Definition Tensor.hpp:1081
const bool & is_contiguous() const
return whether the Tensor is contiguous or not.
Definition Tensor.hpp:716
Tensor Exp() const
the Exp member function. Same as linalg::Exp(const Tensor &Tin), where Tin is the current Tensor.
Tensor & Abs_()
the Abs_ member function. Same as linalg::Abs_(Tensor &Tin), where Tin is the current Tensor.
Tensor Add(const T &rhs)
Addition function with a Tensor or a scalar. Same as cytnx::operator+(const Tensor &self,...
Definition Tensor.hpp:1313
void flatten_()
The flatten function, inplacely.
Definition Tensor.hpp:1472
void Save(const std::string &fname) const
Save current Tensor to file.
Tensor flatten() const
The flatten function.
Definition Tensor.hpp:1458
Tensor(std::initializer_list< cytnx_uint64 > shape, unsigned int dtype=Type.Double, int device=-1, bool init_zero=true)
Definition Tensor.hpp:477
Tensor & Conj_()
the Conj_ member function. Same as cytnx::linalg::Conj_(Tensor &Tin), where Tin is the current Tensor...
T * ptr_as() const
Definition Tensor.hpp:523
Tensor Pow(const cytnx_double &p) const
the Pow member function. Same as linalg::Pow(const Tensor &Tin, const cytnx_double &p),...
int device() const
the device-id of the Tensor
Definition Tensor.hpp:581
Tensor real()
return the real part of the tensor.
Tensor imag()
return the imaginary part of the tensor.
void Init(std::initializer_list< cytnx_uint64 > shape, unsigned int dtype=Type.Double, int device=-1, bool init_zero=true)
Definition Tensor.hpp:444
Tensor to(const int &device) const
copy a tensor to new device
Definition Tensor.hpp:687
Tensor & reshape_(const std::vector< cytnx_int64 > &new_shape)
reshape the Tensor, inplacely
Definition Tensor.hpp:824
void Tofile(std::fstream &f) const
Tensor get(const std::vector< cytnx::Accessor > &accessors) const
Definition Tensor.hpp:1087
Scalar norm() const
the norm member function. Same as linalg::norm(const Tensor &Tin), where Tin is the current Tensor....
void set(const std::vector< cytnx::Accessor > &accessors, const T &rc)
set elements with the input constant using Accessor (C++ API) / slices (python API)
Definition Tensor.hpp:1143
Tensor Max() const
the Max member function. Same as linalg::Max(const Tensor &Tin), where Tin is the current Tensor.
Tensor permute(const std::vector< cytnx_uint64 > &rnks) const
perform tensor permute on the cytnx::Tensor and return a new instance.
Definition Tensor.hpp:749
Tensor Div(const T &rhs)
Division function with a Tensor or a scalar. Same as cytnx::operator/(const Tensor &self,...
Definition Tensor.hpp:1374
Tensor Mod(const T &rhs)
Definition Tensor.hpp:1439
Tensor Cpr(const T &rhs)
The comparison function.
Definition Tensor.hpp:1396
Tensor & Inv_(const double &clip=-1.)
Apply the inverse on each entry of the Tensor.
Tensor & Exp_()
the Exp_ member function. Same as linalg::Exp_(Tensor &Tin), where Tin is the current Tensor.
Tensor & InvM_()
the InvM_ member function. Same as cytnx::linalg::InvM_(Tensor &Tin), where Tin is the current Tensor...
const std::vector< cytnx_uint64 > & shape() const
the shape of the Tensor
Definition Tensor.hpp:601
Tensor Min() const
the Min member function. Same as linalg::Min(const Tensor &Tin), where Tin is the current Tensor.
const T & at(const std::vector< cytnx_uint64 > &locator) const
Definition Tensor.hpp:965
Storage & storage() const
return the storage of current Tensor.
Definition Tensor.hpp:1163
static Tensor from_storage(const Storage &in)
Convert a Storage to Tensor.
Definition Tensor.hpp:561
Tensor & Sub_(const T &rhs)
Subtraction function with a Tensor or a scalar, inplacely. Same as operator-=(const T &rhs).
Definition Tensor.hpp:1343
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:118
Definition Accessor.hpp:12
cytnx::UniTensor operator*(const cytnx::UniTensor &Lt, const cytnx::UniTensor &Rt)
The multiplication operator between two UniTensor.
cytnx::UniTensor operator-(const cytnx::UniTensor &Lt, const cytnx::UniTensor &Rt)
The subtraction operator between two UniTensor.
cytnx::UniTensor operator+(const cytnx::UniTensor &Lt, const cytnx::UniTensor &Rt)
The addition operator between two UniTensor.
cytnx::UniTensor operator/(const cytnx::UniTensor &Lt, const cytnx::UniTensor &Rt)
The division operator between two UniTensor.