10#include "utils/dynamic_arg_resolver.hpp"
14#include <initializer_list>
20 #include "backend/Scalar.hpp"
21 #include "backend/Storage.hpp"
22 #include "backend/Tensor_impl.hpp"
31 Tensor
operator+(
const Tensor &lhs,
const T &rc);
33 Tensor
operator-(
const Tensor &lhs,
const T &rhs);
35 Tensor
operator*(
const Tensor &lhs,
const T &rhs);
37 Tensor
operator/(
const Tensor &lhs,
const T &rhs);
47 boost::intrusive_ptr<Tensor_impl>
_insimpl;
48 std::vector<cytnx::Accessor>
_accs;
49 Tproxy(boost::intrusive_ptr<Tensor_impl>
_ptr,
const std::vector<cytnx::Accessor> &
accs)
59 const T &operator=(
const T &
rc) {
65 this->_insimpl->set(
_accs, tmp._impl);
207 Scalar::Sproxy
item()
const {
233 template <
class...
Ts>
239 template <
class...
Ts>
245 template <
class...
Ts>
251 template <
class...
Ts>
252 const Tproxy operator()(
const std::string &
e1,
const Ts &...
elems)
const {
257 template <
class...
Ts>
262 template <
class...
Ts>
271 std::vector<cytnx::Accessor> tmp =
accs;
282 std::vector<cytnx::Accessor> tmp =
accs;
287 std::vector<cytnx_int64> tmp =
accs;
291 std::vector<cytnx::Accessor>
acc_in;
292 for (
int i = 0;
i <
accs.size();
i++) {
298 std::vector<cytnx_int64> tmp =
accs;
302 std::vector<cytnx::Accessor>
acc_in;
303 for (
int i = 0;
i <
accs.size();
i++) {
309 std::vector<cytnx::Accessor>
acc_in;
310 for (
int i = 0;
i <
accs.size();
i++) {
319 void _Save(std::fstream &f)
const;
320 void _Load(std::fstream &f);
400 boost::intrusive_ptr<Tensor_impl>
_impl;
414 Tensor &operator=(
const Tensor &
rhs) {
452 boost::intrusive_ptr<Tensor_impl> tmp(
new Tensor_impl());
503 boost::intrusive_ptr<Tensor_impl> tmp(
new Tensor_impl());
514 unsigned int dtype()
const {
return this->_impl->dtype(); }
521 int device()
const {
return this->_impl->device(); }
528 std::string
dtype_str()
const {
return this->_impl->dtype_str(); }
535 std::string
device_str()
const {
return this->_impl->device_str(); }
541 const std::vector<cytnx_uint64> &
shape()
const {
return this->_impl->shape(); }
628 template <
class...
Ts>
631 this->_impl->permute_(
argv);
660 template <
class...
Ts>
732 this->_impl->reshape_(
shape);
736 this->_impl->reshape_(
shape);
738 template <
class...
Ts>
742 this->_impl->reshape_(
shape);
780 std::vector<cytnx_int64> tmp(
new_shape.size());
795 template <
class...
Ts>
866 const T &
at(
const std::vector<cytnx_uint64> &
locator)
const {
870 template <
class T,
class...
Ts>
873 return this->
at<T>(argv);
875 template <
class T,
class...
Ts>
878 return this->
at<T>(argv);
881 const Scalar::Sproxy
at(
const std::vector<cytnx_uint64> &
locator)
const {
882 return this->_impl->at(
locator);
885 Scalar::Sproxy
at(
const std::vector<cytnx_uint64> &
locator) {
return this->_impl->at(
locator); }
915 cytnx_error_msg(this->_impl->storage().size() != 1,
"[ERROR][Tensor.item<T>]%s",
916 "item can only be called from a Tensor with only one element\n");
917 return this->_impl->storage().at<
T>(0);
922 const T &
item()
const {
923 cytnx_error_msg(this->_impl->storage().size() != 1,
"[ERROR][Tensor.item<T>]%s",
924 "item can only be called from a Tensor with only one element\n");
925 return this->_impl->storage().at<
T>(0);
928 const Scalar::Sproxy
item()
const {
933 Scalar::Sproxy
item() {
1021 void set(
const std::initializer_list<cytnx::Accessor> &
accessors,
const T &
rc) {
1023 this->
set(args,
rc);
1054 this->_impl->fill(
val);
1062 if (this->
shape() != rhs.
shape())
return false;
1197 return *
this +=
rhs;
1217 return *
this -=
rhs;
1237 return *
this *=
rhs;
1259 return *
this /=
rhs;
1270 return *
this ==
rhs;
1382 "[ERROR] try to append a null Tensor.%s",
"\n");
1384 "[ERROR] try to append a Tensor with rank not match.%s",
"\n");
1386 for (
unsigned int i = 0;
i <
rhs.shape().size();
i++) {
1388 "[ERROR] dimension mismatch @ rhs.rank: [%d] this: [%d] rhs: [%d]\n",
i,
1395 if (
rhs.dtype() !=
this->dtype()) {
1399 if (!
in.is_contiguous())
1404 this->_impl->_shape[0] += 1;
1407 memcpy(((
char *)this->_impl->_storage.data()) +
1409 in._impl->_storage.data(),
Type.typeSize(
in.dtype()) *
Nelem);
1443 "[ERROR] try to append a null Tensor.%s",
"\n");
1445 "[ERROR] append a storage to Tensor can only accept rank-2 Tensor.%s",
"\n");
1451 if (
srhs.dtype() !=
this->dtype()) {
1456 this->_impl->_shape[0] += 1;
1458 this->_impl->_storage.resize(
oldsize +
in.size());
1459 memcpy(((
char *)this->_impl->_storage.data()) +
1461 in._impl->Mem,
Type.typeSize(
in.dtype()) *
in.size());
1503 "[ERROR] trying to append a scalar into multidimentional Tensor is not "
1504 "allow.\n Only rank-1 Tensor can accept scalar append.%s",
1507 "[ERROR] append require the Tensor to be contiguous. suggestion: call "
1508 "contiguous() or contiguous_() first.",
1510 this->_impl->_shape[0] += 1;
1511 this->_impl->_storage.append(
rhs);
1537 std::vector<Tensor>
Eigh(
const bool &
is_V =
true,
const bool &
row_v =
false)
const;
1648 std::ostream &operator<<(std::ostream &os,
const Tensor &in);
1649 std::ostream &operator<<(std::ostream &os,
const Tensor::Tproxy &in);
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:41
void append(const Storage &srhs)
the append function of the Storage.
Definition Tensor.hpp:1438
Tensor & operator*=(const T &rc)
multiplication assignment operator with a Tensor or a scalar.
Tensor & Inv_(const double &clip)
the Inv_ member function. Same as cytnx::linalg::Inv_(Tensor &Tin, const double &clip)
Tensor & operator/=(const T &rc)
division assignment operator with a Tensor or a scalar.
Tensor operator-()
The negation function.
Definition Tensor.hpp:1322
void fill(const T &val)
fill all the element of current Tensor with the value.
Definition Tensor.hpp:1053
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:615
Tensor reshape(const std::vector< cytnx_uint64 > &new_shape) const
Definition Tensor.hpp:779
Tensor(const std::vector< cytnx_uint64 > &shape, const unsigned int &dtype=Type.Double, const int &device=-1, const bool &init_zero=1)
Construct a new Tensor object.
Definition Tensor.hpp:480
void append(const T &rhs)
the append function of the scalar.
Definition Tensor.hpp:1501
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:1196
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:790
std::string device_str() const
the device (in string) of the Tensor
Definition Tensor.hpp:535
void reshape_(const std::vector< cytnx_int64 > &new_shape)
reshape the Tensor, inplacely
Definition Tensor.hpp:728
Tensor contiguous_()
Make the Tensor contiguous by coalescing the memory (storage), inplacely.
Definition Tensor.hpp:702
Tensor permute_(const std::vector< cytnx_uint64 > &rnks)
Definition Tensor.hpp:623
Tensor Mul(const T &rhs)
Multiplication function with a Tensor or a scalar. Same as cytnx::operator*(const Tensor &self,...
Definition Tensor.hpp:1226
unsigned int dtype() const
the dtype-id of the Tensor
Definition Tensor.hpp:514
Tensor Sub(const T &rhs)
Subtraction function with a Tensor or a scalar. Same as cytnx::operator-(const Tensor &self,...
Definition Tensor.hpp:1206
Tensor Inv(const double &clip) const
the Inv member function. Same as cytnx::linalg::Inv(const Tensor &Tin, const double &clip)
Tensor contiguous() const
Make the Tensor contiguous by coalescing the memory (storage).
Definition Tensor.hpp:682
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:858
Tensor reshape(const std::vector< cytnx_int64 > &new_shape) const
return a new Tensor that is reshaped.
Definition Tensor.hpp:770
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:914
Tensor clone() const
return a clone of the current Tensor.
Definition Tensor.hpp:566
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:1376
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:993
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:823
Tensor & Div_(const T &rhs)
Division function with a Tensor or a scalar, inplacely. Same as operator/=(const T &rhs).
Definition Tensor.hpp:1258
static Tensor Load(const std::string &fname)
Load current Tensor from file.
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 equivshape(const Tensor &rhs)
compare the shape of two tensors.
Definition Tensor.hpp:1061
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) ,...
std::string dtype_str() const
the dtype (in string) of the Tensor
Definition Tensor.hpp:528
Tensor & Mul_(const T &rhs)
Multiplication function with a Tensor or a scalar, inplacely. Same as operator*=(const T &rhs).
Definition Tensor.hpp:1236
cytnx_uint64 rank() const
the rank of the Tensor
Definition Tensor.hpp:547
const bool & is_contiguous() const
return whether the Tensor is contiguous or not.
Definition Tensor.hpp:621
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:1186
void flatten_()
The flatten function, inplacely.
Definition Tensor.hpp:1345
void Save(const std::string &fname) const
Save current Tensor to file.
Tensor flatten() const
The flatten function.
Definition Tensor.hpp:1331
Tensor & Conj_()
the Conj_ member function. Same as cytnx::linalg::Conj_(Tensor &Tin), where Tin is the current Tensor...
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:521
Tensor real()
return the real part of the tensor.
Tensor imag()
return the imaginary part of the tensor.
Tensor to(const int &device) const
copy a tensor to new device
Definition Tensor.hpp:592
void Tofile(std::fstream &f) const
Tensor get(const std::vector< cytnx::Accessor > &accessors) const
get elements using Accessor (C++ API) / slices (python API)
Definition Tensor.hpp:961
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:1016
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:654
Tensor Div(const T &rhs)
Division function with a Tensor or a scalar. Same as cytnx::operator/(const Tensor &self,...
Definition Tensor.hpp:1247
Tensor Mod(const T &rhs)
Definition Tensor.hpp:1312
void Init(const std::vector< cytnx_uint64 > &shape, const unsigned int &dtype=Type.Double, const int &device=-1, const bool &init_zero=true)
initialize a Tensor
Definition Tensor.hpp:450
Tensor Cpr(const T &rhs)
The comparison function.
Definition Tensor.hpp:1269
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:541
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:866
Storage & storage() const
return the storage of current Tensor.
Definition Tensor.hpp:1036
static Tensor from_storage(const Storage &in)
Convert a Storage to Tensor.
Definition Tensor.hpp:501
Tensor & Sub_(const T &rhs)
Subtraction function with a Tensor or a scalar, inplacely. Same as operator-=(const T &rhs).
Definition Tensor.hpp:1216
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:16
Helper function to print vector with ODT:
Definition Accessor.hpp:12
cytnx::UniTensor operator*(const cytnx::UniTensor &Lt, const cytnx::UniTensor &Rt)
The multiplication operator between two UniTensor.
double cytnx_double
Definition Type.hpp:53
uint32_t cytnx_uint32
Definition Type.hpp:56
bool cytnx_bool
Definition Type.hpp:64
std::complex< double > cytnx_complex128
Definition Type.hpp:63
float cytnx_float
Definition Type.hpp:54
int16_t cytnx_int16
Definition Type.hpp:60
std::complex< float > cytnx_complex64
Definition Type.hpp:62
cytnx::UniTensor operator-(const cytnx::UniTensor &Lt, const cytnx::UniTensor &Rt)
The subtraction operator between two UniTensor.
int32_t cytnx_int32
Definition Type.hpp:59
uint16_t cytnx_uint16
Definition Type.hpp:57
uint64_t cytnx_uint64
Definition Type.hpp:55
int64_t cytnx_int64
Definition Type.hpp:58
cytnx::UniTensor operator+(const cytnx::UniTensor &Lt, const cytnx::UniTensor &Rt)
The addtion operator between two UniTensor.
cytnx::UniTensor operator/(const cytnx::UniTensor &Lt, const cytnx::UniTensor &Rt)
The division operator between two UniTensor.