Cytnx 1.1.2.dev761+ge28145acd
Loading...
Searching...
No Matches
linalg.hpp
Go to the documentation of this file.
1#ifndef CYTNX_LINALG_H_
2#define CYTNX_LINALG_H_
3
4#include "LinOp.hpp"
5#include "Tensor.hpp"
6#include "Type.hpp"
7#include "UniTensor.hpp"
8#include "cytnx_error.hpp"
9#include "random.hpp"
10
11#include <functional>
12#include <type_traits>
13
14#include "backend/Scalar.hpp"
15#include "backend/Storage.hpp"
16
17namespace cytnx {
18
19 // The scalar-like operand concepts (cytnx_scalar_value, cytnx_scalar_like,
20 // cytnx_unitensor_scalar_like) are defined in Tensor.hpp, which this header includes. They must
21 // be declared there because cytnx_scalar_like names Tensor::Tproxy, and because Tensor.hpp
22 // forward-declares the constrained `Tensor <op> T` operators -- those forward declarations have
23 // to carry the identical constraint or they form a distinct, unconstrained overload set
24 // (#1003, operator hygiene -- Ian's note).
25
51
61 template <cytnx_unitensor_scalar_like T>
63
73 template <cytnx_unitensor_scalar_like T>
75
90
100 template <cytnx_unitensor_scalar_like T>
102
112 template <cytnx_unitensor_scalar_like T>
114
129
139 template <cytnx_unitensor_scalar_like T>
141
151 template <cytnx_unitensor_scalar_like T>
153
168
178 template <cytnx_unitensor_scalar_like T>
180
190 template <cytnx_unitensor_scalar_like T>
192
204
214 template <cytnx_scalar_value T>
216
226 template <cytnx_scalar_value T>
228
239 namespace linalg {
240
241 // Add:
242 //==================================================
265
298 template <class T>
299 cytnx::UniTensor Add(const T &lc, const cytnx::UniTensor &Rt);
300
334 template <class T>
335 cytnx::UniTensor Add(const cytnx::UniTensor &Lt, const T &rc);
336
337 // Sub:
338 //==================================================
362
395 template <class T>
396 cytnx::UniTensor Sub(const T &lc, const cytnx::UniTensor &Rt);
397
430 template <class T>
431 cytnx::UniTensor Sub(const cytnx::UniTensor &Lt, const T &rc);
432
433 // Mul:
434 //==================================================
458
491 template <class T>
492 cytnx::UniTensor Mul(const T &lc, const cytnx::UniTensor &Rt);
493
526 template <class T>
527 cytnx::UniTensor Mul(const cytnx::UniTensor &Lt, const T &rc);
528
529 // Div:
530 //==================================================
554
588 template <class T>
589 cytnx::UniTensor Div(const T &lc, const cytnx::UniTensor &Rt);
590
624 template <class T>
625 cytnx::UniTensor Div(const cytnx::UniTensor &Lt, const T &rc);
626
627 // Mod:
628 //==================================================
654
685 template <class T>
686 cytnx::UniTensor Mod(const T &lc, const cytnx::UniTensor &Rt);
687
718 template <class T>
719 cytnx::UniTensor Mod(const cytnx::UniTensor &Lt, const T &rc);
720
728 std::vector<cytnx::UniTensor> Svd(const cytnx::UniTensor &Tin, const bool &is_UvT = true);
729
739 std::vector<cytnx::UniTensor> Gesvd(const cytnx::UniTensor &Tin, const bool &is_U = true,
740 const bool &is_vT = true);
741
753 std::vector<cytnx::UniTensor> Svd_truncate(const cytnx::UniTensor &Tin,
754 const cytnx_uint64 &keepdim, const double &err = 0.,
755 const bool &is_UvT = true,
756 const unsigned int &return_err = 0,
757 const cytnx_uint64 &mindim = 1);
758
813 std::vector<cytnx::UniTensor> Svd_truncate(const cytnx::UniTensor &Tin,
814 const cytnx_uint64 &keepdim,
815 std::vector<cytnx_uint64> min_blockdim,
816 const double &err = 0., const bool &is_UvT = true,
817 const unsigned int &return_err = 0,
818 const cytnx_uint64 &mindim = 1);
819
831 std::vector<cytnx::UniTensor> Gesvd_truncate(const cytnx::UniTensor &Tin,
832 const cytnx_uint64 &keepdim,
833 const double &err = 0., const bool &is_U = true,
834 const bool &is_vT = true,
835 const unsigned int &return_err = 0,
836 const cytnx_uint64 &mindim = 1);
837
851 std::vector<cytnx::UniTensor> Gesvd_truncate(
852 const cytnx::UniTensor &Tin, const cytnx_uint64 &keepdim,
853 std::vector<cytnx_uint64> min_blockdim, const double &err = 0., const bool &is_U = true,
854 const bool &is_vT = true, const unsigned int &return_err = 0, const cytnx_uint64 &mindim = 1);
855
856 // Rsvd:
857 //==================================================
907 std::vector<cytnx::UniTensor> Rsvd(const cytnx::UniTensor &Tin, cytnx_uint64 keepdim,
908 double err = 0., bool is_U = true, bool is_vT = true,
909 unsigned int return_err = 0, cytnx_uint64 mindim = 1,
910 cytnx_uint64 oversampling_summand = 10,
911 double oversampling_factor = 1.,
912 cytnx_uint64 power_iteration = 0,
913 unsigned int seed = random::__static_random_device());
914
948 std::vector<cytnx::UniTensor> Rsvd(const cytnx::UniTensor &Tin, cytnx_uint64 keepdim,
949 const std::vector<cytnx_uint64> min_blockdim,
950 double err = 0., bool is_U = true, bool is_vT = true,
951 unsigned int return_err = 0, cytnx_uint64 mindim = 1,
952 cytnx_uint64 oversampling_summand = 10,
953 double oversampling_factor = 1.,
954 cytnx_uint64 power_iteration = 0,
955 unsigned int seed = random::__static_random_device());
956
957 std::vector<cytnx::UniTensor> Hosvd(
958 const cytnx::UniTensor &Tin, const std::vector<cytnx_uint64> &mode,
959 const bool &is_core = true, const bool &is_Ls = false,
960 const std::vector<cytnx_int64> &trucate_dim = std::vector<cytnx_int64>());
961
970 template <typename T>
971 cytnx::UniTensor ExpH(const cytnx::UniTensor &Tin, const T &a, const T &b = 0);
972
980 template <typename T>
981 cytnx::UniTensor ExpM(const cytnx::UniTensor &Tin, const T &a, const T &b = 0);
982
992
1001
1010 cytnx::UniTensor Trace(const cytnx::UniTensor &Tin, const cytnx_int64 &a = 0,
1011 const cytnx_int64 &b = 1);
1012
1019 cytnx::UniTensor Trace(const cytnx::UniTensor &Tin, const std::string &a, const std::string &b);
1020
1029 std::vector<cytnx::UniTensor> Qr(const cytnx::UniTensor &Tin, const bool &is_tau = false);
1030
1039 std::vector<cytnx::UniTensor> Qdr(const cytnx::UniTensor &Tin, const bool &is_tau = false);
1040
1041 // Pow:
1042 //==================================================
1061 cytnx::UniTensor Pow(const cytnx::UniTensor &Tin, const double &p);
1062
1081 void Pow_(cytnx::UniTensor &Tin, const double &p);
1082
1083 // Inv:
1084 //==================================================
1111 cytnx::UniTensor Inv(const cytnx::UniTensor &Tin, double clip = -1.);
1112
1139 void Inv_(cytnx::UniTensor &Tin, double clip = -1.);
1140
1148
1155
1156 //====================================================================================
1157 // [Tensor]
1158 // ====================================================================================
1159 //====================================================================================
1160
1184 Tensor Add(const Tensor &Lt, const Tensor &Rt);
1185
1207 template <class T>
1208 Tensor Add(const T &lc, const Tensor &Rt);
1209
1231 template <class T>
1232 Tensor Add(const Tensor &Lt, const T &rc);
1233
1262 void iAdd(Tensor &Lt, const Tensor &Rt, bool rhs_is_weak_scalar = false);
1263
1264 // Sub:
1265 //==================================================
1288 Tensor Sub(const Tensor &Lt, const Tensor &Rt);
1289
1311 template <class T>
1312 Tensor Sub(const T &lc, const Tensor &Rt);
1313
1335 template <class T>
1336 Tensor Sub(const Tensor &Lt, const T &rc);
1337
1366 void iSub(Tensor &Lt, const Tensor &Rt, bool rhs_is_weak_scalar = false);
1367
1368 // Mul:
1369 //==================================================
1392 Tensor Mul(const Tensor &Lt, const Tensor &Rt);
1393
1415 template <class T>
1416 Tensor Mul(const T &lc, const Tensor &Rt);
1417
1439 template <class T>
1440 Tensor Mul(const Tensor &Lt, const T &rc);
1441
1470 void iMul(Tensor &Lt, const Tensor &Rt, bool rhs_is_weak_scalar = false);
1471
1472 // Div:
1473 //==================================================
1497 Tensor Div(const Tensor &Lt, const Tensor &Rt);
1498
1521 template <class T>
1522 Tensor Div(const T &lc, const Tensor &Rt);
1523
1546 template <class T>
1547 Tensor Div(const Tensor &Lt, const T &rc);
1548
1577 void iDiv(Tensor &Lt, const Tensor &Rt, bool rhs_is_weak_scalar = false);
1578
1579 // Mod:
1580 //==================================================
1581
1604 Tensor Mod(const Tensor &Lt, const Tensor &Rt);
1605
1626 template <class T>
1627 Tensor Mod(const T &lc, const Tensor &Rt);
1628
1649 template <class T>
1650 Tensor Mod(const Tensor &Lt, const T &rc);
1651
1652 // Cpr:
1653 //==================================================
1679 Tensor Cpr(const Tensor &Lt, const Tensor &Rt);
1680
1705 template <class T>
1706 Tensor Cpr(const T &lc, const Tensor &Rt);
1707
1732 template <class T>
1733 Tensor Cpr(const Tensor &Lt, const T &rc);
1734
1735 // Norm:
1736 //=================================================
1747 [[deprecated("use norm() (returns Scalar) instead")]] Tensor Norm(const Tensor &Tl);
1748
1749 // norm:
1750 //=================================================
1760 Scalar norm(const Tensor &Tl);
1761
1762 // Norm:
1763 //=================================================
1774 [[deprecated("use norm() (returns Scalar) instead")]] Tensor Norm(const cytnx::UniTensor &uTl);
1775
1776 // norm:
1777 //=================================================
1787 Scalar norm(const cytnx::UniTensor &uTl);
1788
1789 // Det:
1790 //=================================================
1799 Tensor Det(const Tensor &Tl);
1800
1801 // randomized isometries:
1802 //==================================================
1830 Tensor Rand_isometry(const Tensor &Tin, const cytnx_uint64 &keepdim,
1831 const cytnx_uint64 &power_iteration = 2,
1832 const unsigned int &seed = random::__static_random_device());
1833
1834 // Svd:
1835 //==================================================
1861 std::vector<Tensor> Svd(const Tensor &Tin, const bool &is_UvT = true);
1862
1863 // Gesvd:
1864 //==================================================
1892 std::vector<Tensor> Gesvd(const Tensor &Tin, const bool &is_U = true, const bool &is_vT = true);
1893
1894 // Svd_truncate:
1895 //==================================================
1940 std::vector<Tensor> Svd_truncate(const Tensor &Tin, const cytnx_uint64 &keepdim,
1941 const double &err = 0., const bool &is_UvT = true,
1942 const unsigned int &return_err = 0,
1943 const cytnx_uint64 &mindim = 1);
1944
1945 // Rsvd:
1946 //==================================================
1993 std::vector<Tensor> Rsvd(const Tensor &Tin, cytnx_uint64 keepdim, double err = 0.,
1994 bool is_U = true, bool is_vT = true, unsigned int return_err = 0,
1995 cytnx_uint64 mindim = 1, cytnx_uint64 oversampling_summand = 10,
1996 double oversampling_factor = 1., cytnx_uint64 power_iteration = 0,
1997 unsigned int seed = random::__static_random_device());
1998 // Gesvd_truncate:
1999 //==================================================
2028 std::vector<Tensor> Gesvd_truncate(const Tensor &Tin, const cytnx_uint64 &keepdim,
2029 const double &err = 0., const bool &is_U = true,
2030 const bool &is_vT = true, const unsigned int &return_err = 0,
2031 const cytnx_uint64 &mindim = 1);
2032
2033 // Hosvd:
2034 std::vector<Tensor> Hosvd(
2035 const Tensor &Tin, const std::vector<cytnx_uint64> &mode, const bool &is_core = true,
2036 const bool &is_Ls = false,
2037 const std::vector<cytnx_int64> &trucate_dim = std::vector<cytnx_int64>());
2038
2039 // Qr:
2040 //==================================================
2074 std::vector<Tensor> Qr(const Tensor &Tin, const bool &is_tau = false);
2075
2076 // Qdr:
2077 //==================================================
2097 std::vector<Tensor> Qdr(const Tensor &Tin, const bool &is_tau = false);
2098
2099 // Eigh:
2100 //==================================================
2123 std::vector<Tensor> Eigh(const Tensor &Tin, const bool &is_V = true, const bool &row_v = false);
2124
2125 std::vector<UniTensor> Eigh(const cytnx::UniTensor &Tin, const bool &is_V = true,
2126 const bool &row_v = false);
2127
2181 std::vector<UniTensor> Eigh_truncate(const cytnx::UniTensor &Tin, cytnx_uint64 keepdim,
2182 double err = 0., bool is_V = true,
2183 unsigned int return_err = 0, cytnx_uint64 mindim = 1);
2184
2202 std::vector<UniTensor> Eigh_truncate(const cytnx::UniTensor &Tin, cytnx_uint64 keepdim,
2203 std::vector<cytnx_uint64> min_blockdim, double err = 0.,
2204 bool is_V = true, unsigned int return_err = 0,
2205 cytnx_uint64 mindim = 1);
2206
2207 // Eig:
2208 //==================================================
2232 std::vector<Tensor> Eig(const Tensor &Tin, const bool &is_V = true, const bool &row_v = false);
2233
2234 std::vector<UniTensor> Eig(const cytnx::UniTensor &Tin, const bool &is_V = true,
2235 const bool &row_v = false);
2236
2237 // Trace:
2238 //==================================================
2252 Tensor Trace(const Tensor &Tn, const cytnx_uint64 &axisA = 0, const cytnx_uint64 &axisB = 1);
2253
2254 // Min:
2255 //==================================================
2261 Tensor Min(const Tensor &Tn);
2262
2263 // Max:
2264 //==================================================
2270 Tensor Max(const Tensor &Tn);
2271
2272 // Sum:
2273 //==================================================
2278 Tensor Sum(const Tensor &Tn);
2279
2280 // Matmul:
2281 //==================================================
2293 Tensor Matmul(const Tensor &TL, const Tensor &TR);
2294
2295 // Matmul_dg:
2296 //==================================================
2303 Tensor Matmul_dg(const Tensor &Tl, const Tensor &Tr);
2304
2305 // InvM:
2306 //==================================================
2313 Tensor InvM(const Tensor &Tin);
2322 void InvM_(Tensor &Tin);
2323 void InvM_(UniTensor &Tin);
2324
2325 // Inv:
2326 //==================================================
2351 Tensor Inv(const Tensor &Tin, const double &clip = -1.);
2352
2377 void Inv_(Tensor &Tin, const double &clip = -1.);
2378
2379 // Conj:
2380 //==================================================
2395 Tensor Conj(const Tensor &Tin);
2396
2405 void Conj_(Tensor &Tin);
2406
2407 // Exp:
2408 //==================================================
2422 Tensor Exp(const Tensor &Tin);
2423
2434 [[deprecated("use Exp() (dtype-preserving) instead")]] Tensor Expf(const Tensor &Tin);
2435
2446 void Exp_(Tensor &Tin);
2447
2456 [[deprecated("use Exp_() (dtype-preserving) instead")]] void Expf_(Tensor &Tin);
2457
2458 // Pow:
2459 //==================================================
2472 Tensor Pow(const Tensor &Tin, const double &p);
2473
2487 void Pow_(Tensor &Tin, const double &p);
2488
2489 // Abs:
2490 //==================================================
2498 Tensor Abs(const Tensor &Tin);
2499
2507 void Abs_(Tensor &Tin);
2508
2509 // Diag:
2510 //==================================================
2519 Tensor Diag(const Tensor &Tin);
2520
2521 // Tensordot:
2522 //==================================================
2538 Tensor Tensordot(const Tensor &Tl, const Tensor &Tr, const std::vector<cytnx_uint64> &idxl,
2539 const std::vector<cytnx_uint64> &idxr, const bool &cacheL = false,
2540 const bool &cacheR = false);
2541
2542 // Tensordot_dg:
2543 //==================================================
2561 Tensor Tensordot_dg(const Tensor &Tl, const Tensor &Tr, const std::vector<cytnx_uint64> &idxl,
2562 const std::vector<cytnx_uint64> &idxr, const bool &diag_L);
2563
2564 // Outer:
2565 //==================================================
2576 Tensor Outer(const Tensor &Tl, const Tensor &Tr);
2577
2578 // Kron:
2579 //==================================================
2596 Tensor Kron(const Tensor &lhs, const Tensor &rhs, bool lhs_pad_left = false,
2597 bool rhs_pad_left = false);
2598
2599 // Directsum:
2600 //==================================================
2615 Tensor Directsum(const Tensor &T1, const Tensor &T2,
2616 const std::vector<cytnx_uint64> &shared_axes);
2617
2618 // VectorDot:
2619 //=================================================
2631 Tensor Vectordot(const Tensor &Tl, const Tensor &Tr, const bool &is_conj = false);
2632
2633 // Dot:
2634 //=================================================
2654 Tensor Dot(const Tensor &Tl, const Tensor &Tr);
2655
2656 // Tridiag:
2657 //===========================================
2680 std::vector<Tensor> Tridiag(const Tensor &Diag, const Tensor &Sub_diag, const bool &is_V = true,
2681 const bool &is_row = false, bool throw_excp = false);
2682
2683 // ExpH:
2684 //===========================================
2702 template <typename T>
2703 Tensor ExpH(const Tensor &in, const T &a, const T &b = 0);
2716 Tensor ExpH(const Tensor &in);
2717
2718 // ExpM:
2719 //===========================================
2732 template <typename T>
2733 Tensor ExpM(const Tensor &in, const T &a, const T &b = 0);
2734
2745 Tensor ExpM(const Tensor &in);
2746
2747 // Arnoldi:
2748 //===========================================
2794 std::vector<Tensor> Arnoldi(LinOp *Hop, const Tensor &Tin = Tensor(),
2795 const std::string which = "LM", const cytnx_uint64 &maxiter = 10000,
2796 const cytnx_double &cvg_crit = 0, const cytnx_uint64 &k = 1,
2797 const bool &is_V = true, const cytnx_int32 &ncv = 0,
2798 const bool &verbose = false);
2799
2800 // Arnoldi:
2801 //===========================================
2855 std::vector<UniTensor> Arnoldi(LinOp *Hop, const cytnx::UniTensor &Tin,
2856 const std::string which = "LM",
2857 const cytnx_uint64 &maxiter = 10000,
2858 const cytnx_double &cvg_crit = 1.0e-9, const cytnx_uint64 &k = 1,
2859 const bool &is_V = true, const cytnx_int32 &ncv = 0,
2860 const bool &verbose = false);
2861
2862 // Lanczos:
2863 //===========================================
2895 std::vector<Tensor> Lanczos(LinOp *Hop, const Tensor &Tin = Tensor(),
2896 const std::string method = "Gnd", const double &CvgCrit = 1.0e-14,
2897 const unsigned int &Maxiter = 10000, const cytnx_uint64 &k = 1,
2898 const bool &is_V = true, const bool &is_row = false,
2899 const cytnx_uint32 &max_krydim = 0, const bool &verbose = false);
2940 std::vector<Tensor> Lanczos(LinOp *Hop, const Tensor &Tin = Tensor(),
2941 const std::string which = "SA", const cytnx_uint64 &maxiter = 10000,
2942 const cytnx_double &cvg_crit = 0, const cytnx_uint64 &k = 1,
2943 const bool &is_V = true, const cytnx_int32 &ncv = 0,
2944 const bool &verbose = false);
2992 std::vector<UniTensor> Lanczos(LinOp *Hop, const cytnx::UniTensor &Tin,
2993 const std::string which = "SA",
2994 const cytnx_uint64 &maxiter = 10000,
2995 const cytnx_double &cvg_crit = 0, const cytnx_uint64 &k = 1,
2996 const bool &is_V = true, const cytnx_int32 &ncv = 0,
2997 const bool &verbose = false);
2998
2999 // Lanczos:
3000 //===========================================
3035 std::vector<UniTensor> Lanczos(LinOp *Hop, const cytnx::UniTensor &Tin = UniTensor(),
3036 const std::string method = "Gnd",
3037 const double &CvgCrit = 1.0e-14,
3038 const unsigned int &Maxiter = 10000, const cytnx_uint64 &k = 1,
3039 const bool &is_V = true, const bool &is_row = false,
3040 const cytnx_uint32 &max_krydim = 4, const bool &verbose = false);
3041
3042 // Lanczos:
3043 //===========================================
3065 std::vector<Tensor> Lanczos_ER(LinOp *Hop, const cytnx_uint64 &k = 1, const bool &is_V = true,
3066 const cytnx_uint64 &maxiter = 10000,
3067 const double &CvgCrit = 1.0e-14, const bool &is_row = false,
3068 const Tensor &Tin = Tensor(), const cytnx_uint32 &max_krydim = 4,
3069 const bool &verbose = false);
3070
3071 // Lanczos:
3072 //===========================================
3103 std::vector<Tensor> Lanczos_Gnd(LinOp *Hop, const double &CvgCrit = 1.0e-14,
3104 const bool &is_V = true, const Tensor &Tin = Tensor(),
3105 const bool &verbose = false,
3106 const unsigned int &Maxiter = 100000);
3107
3108 // Lanczos:
3109 //===============================================
3149 std::vector<UniTensor> Lanczos_Gnd_Ut(LinOp *Hop, const cytnx::UniTensor &Tin,
3150 const double &CvgCrit = 1.0e-14, const bool &is_V = true,
3151 const bool &verbose = false,
3152 const unsigned int &Maxiter = 100000);
3153
3154 // Lanczos_Exp:
3155 //===============================================
3186 UniTensor Lanczos_Exp(LinOp *Hop, const cytnx::UniTensor &v, const Scalar &tau,
3187 const double &CvgCrit = 1.0e-10, const unsigned int &Maxiter = 100000,
3188 const bool &verbose = false);
3189
3190 // Lstsq:
3191 //===========================================
3217 std::vector<Tensor> Lstsq(const Tensor &A, const Tensor &b, const float &rcond = -1);
3218
3233 Tensor Ger(const Tensor &x, const Tensor &y, const Scalar &a = Scalar());
3234
3252 void Gemm_(const Scalar &a, const Tensor &x, const Tensor &y, const Scalar &b, Tensor &c);
3253
3269 Tensor Gemm(const Scalar &a, const Tensor &x, const Tensor &y);
3270
3296 void Gemm_Batch(const std::vector<Scalar> &alpha_array, const std::vector<Tensor> &a_tensors,
3297 const std::vector<Tensor> &b_tensors, const std::vector<Scalar> &beta_array,
3298 std::vector<Tensor> &c_tensors, const std::vector<cytnx_int64> &group_size);
3299
3300 } // namespace linalg
3301
3302 // operators:
3313 Tensor operator+(const Tensor &Lt, const Tensor &Rt);
3314
3324 template <cytnx_scalar_like T>
3325 Tensor operator+(const T &lc, const Tensor &Rt);
3326
3336 template <cytnx_scalar_like T>
3337 Tensor operator+(const Tensor &Lt, const T &rc);
3338
3339 //------------------------------------
3350 Tensor operator-(const Tensor &Lt, const Tensor &Rt);
3351
3361 template <cytnx_scalar_like T>
3362 Tensor operator-(const T &lc, const Tensor &Rt);
3363
3373 template <cytnx_scalar_like T>
3374 Tensor operator-(const Tensor &Lt, const T &rc);
3375
3376 //-----------------------------------
3387 Tensor operator*(const Tensor &Lt, const Tensor &Rt);
3388
3398 template <cytnx_scalar_like T>
3399 Tensor operator*(const T &lc, const Tensor &Rt);
3400
3410 template <cytnx_scalar_like T>
3411 Tensor operator*(const Tensor &Lt, const T &rc);
3412
3413 //----------------------------------
3426 Tensor operator/(const Tensor &Lt, const Tensor &Rt);
3427
3438 template <cytnx_scalar_like T>
3439 Tensor operator/(const T &lc, const Tensor &Rt);
3440
3451 template <cytnx_scalar_like T>
3452 Tensor operator/(const Tensor &Lt, const T &rc);
3453
3454 //----------------------------------
3465 Tensor operator%(const Tensor &Lt, const Tensor &Rt);
3466
3476 template <cytnx_scalar_value T>
3477 Tensor operator%(const T &lc, const Tensor &Rt);
3478
3488 template <cytnx_scalar_value T>
3489 Tensor operator%(const Tensor &Lt, const T &rc);
3490
3491 //----------------------------------
3501 Tensor operator==(const Tensor &Lt, const Tensor &Rt);
3502
3512 template <cytnx_scalar_value T>
3513 Tensor operator==(const T &lc, const Tensor &Rt);
3514
3524 template <cytnx_scalar_value T>
3525 Tensor operator==(const Tensor &Lt, const T &rc);
3526
3527} // namespace cytnx
3528
3529#endif // CYTNX_LINALG_H_
Definition LinOp.hpp:18
an tensor (multi-dimensional array)
Definition Tensor.hpp:33
An Enhanced tensor specifically designed for physical Tensor network simulation.
Definition UniTensor.hpp:2773
linear algebra related functions.
cytnx::UniTensor Conj(const cytnx::UniTensor &UT)
Elementwise conjugate of the UniTensor.
cytnx::UniTensor Inv(const cytnx::UniTensor &Tin, double clip=-1.)
Element-wise (pseudo-)inverse.
std::vector< cytnx::UniTensor > Qr(const cytnx::UniTensor &Tin, const bool &is_tau=false)
Perform the QR decomposition on a UniTensor.
Tensor Dot(const Tensor &Tl, const Tensor &Tr)
dot product of two arrays.
cytnx::UniTensor Trace(const cytnx::UniTensor &Tin, const cytnx_int64 &a=0, const cytnx_int64 &b=1)
Perform the trace over two legs of a UniTensor.
Tensor Tensordot(const Tensor &Tl, const Tensor &Tr, const std::vector< cytnx_uint64 > &idxl, const std::vector< cytnx_uint64 > &idxr, const bool &cacheL=false, const bool &cacheR=false)
perform tensor dot by sum out the indices assigned of two Tensors.
std::vector< Tensor > Eig(const Tensor &Tin, const bool &is_V=true, const bool &row_v=false)
eigen-value decomposition for generic square matrix
Tensor Sum(const Tensor &Tn)
get the sum of all the elements.
Tensor Min(const Tensor &Tn)
get the minimum element.
void Gemm_(const Scalar &a, const Tensor &x, const Tensor &y, const Scalar &b, Tensor &c)
Blas Gemm, performing , inplacely.
std::vector< UniTensor > Eigh_truncate(const cytnx::UniTensor &Tin, cytnx_uint64 keepdim, double err=0., bool is_V=true, unsigned int return_err=0, cytnx_uint64 mindim=1)
Hermitian eigen-decomposition of a UniTensor with truncation of the eigenpairs.
void Abs_(Tensor &Tin)
inplace perform elementwiase absolute value. @This is just a inplace version of Abs....
Tensor Abs(const Tensor &Tin)
Elementwise absolute value.
Tensor Outer(const Tensor &Tl, const Tensor &Tr)
perform outer produces of two rank-1 Tensor.
std::vector< cytnx::UniTensor > Svd(const cytnx::UniTensor &Tin, const bool &is_UvT=true)
Perform Singular-Value decomposition on a UniTensor using divide-and-conquer method.
Tensor Matmul(const Tensor &TL, const Tensor &TR)
perform matrix multiplication on two tensors.
cytnx::UniTensor Mod(const cytnx::UniTensor &Lt, const cytnx::UniTensor &Rt)
element-wise modulo
Tensor Expf(const Tensor &Tin)
Exponential all the element in Tensor (deprecated).
Tensor Diag(const Tensor &Tin)
return a diagonal tensor with diagonal elements provided as Tin.
std::vector< Tensor > Lstsq(const Tensor &A, const Tensor &b, const float &rcond=-1)
Return the least-squares solution to a linear matrix equation.
std::vector< Tensor > Tridiag(const Tensor &Diag, const Tensor &Sub_diag, const bool &is_V=true, const bool &is_row=false, bool throw_excp=false)
perform diagonalization of symmetric tri-diagnoal matrix.
void Expf_(Tensor &Tin)
inplace perform Exponential on all the element in Tensor (deprecated).
Tensor Det(const Tensor &Tl)
Calculate the determinant of a tensor.
std::vector< Tensor > Eigh(const Tensor &Tin, const bool &is_V=true, const bool &row_v=false)
eigen-value decomposition for Hermitian matrix
std::vector< cytnx::UniTensor > Gesvd_truncate(const cytnx::UniTensor &Tin, const cytnx_uint64 &keepdim, const double &err=0., const bool &is_U=true, const bool &is_vT=true, const unsigned int &return_err=0, const cytnx_uint64 &mindim=1)
Perform Singular-Value decomposition on a UniTensor with truncation.
void Gemm_Batch(const std::vector< Scalar > &alpha_array, const std::vector< Tensor > &a_tensors, const std::vector< Tensor > &b_tensors, const std::vector< Scalar > &beta_array, std::vector< Tensor > &c_tensors, const std::vector< cytnx_int64 > &group_size)
Blas Gemm_Batch, performing many(batch) , inplacely. You do not need to consider the row-major or co...
UniTensor Lanczos_Exp(LinOp *Hop, const cytnx::UniTensor &v, const Scalar &tau, const double &CvgCrit=1.0e-10, const unsigned int &Maxiter=100000, const bool &verbose=false)
Perform the Lanczos algorithm for hermitian operator to approximate .
void Conj_(cytnx::UniTensor &UT)
Inplace elementwise conjugate of the UniTensor.
std::vector< cytnx::UniTensor > Gesvd(const cytnx::UniTensor &Tin, const bool &is_U=true, const bool &is_vT=true)
Perform Singular-Value decomposition on a UniTensor using ?gesvd method.
cytnx::UniTensor Pow(const cytnx::UniTensor &Tin, const double &p)
Take the power p of all elements in a UniTensor.
cytnx::UniTensor Mul(const cytnx::UniTensor &Lt, const cytnx::UniTensor &Rt)
The multiplication function between two UniTensor.
std::vector< Tensor > Lanczos(LinOp *Hop, const Tensor &Tin=Tensor(), const std::string method="Gnd", const double &CvgCrit=1.0e-14, const unsigned int &Maxiter=10000, const cytnx_uint64 &k=1, const bool &is_V=true, const bool &is_row=false, const cytnx_uint32 &max_krydim=0, const bool &verbose=false)
perform Lanczos for hermitian/symmetric matrices or linear function.
Tensor Max(const Tensor &Tn)
get the maximum element.
cytnx::UniTensor Sub(const cytnx::UniTensor &Lt, const cytnx::UniTensor &Rt)
The subtraction function between two UniTensor.
Tensor Norm(const Tensor &Tl)
Calculate the norm of a tensor.
Tensor Rand_isometry(const Tensor &Tin, const cytnx_uint64 &keepdim, const cytnx_uint64 &power_iteration=2, const unsigned int &seed=random::__static_random_device())
Generate an isometrized left isometry for a rank-2 Tensor (a matrix), such that Q * Qdag * Tin approx...
std::vector< cytnx::UniTensor > Hosvd(const cytnx::UniTensor &Tin, const std::vector< cytnx_uint64 > &mode, const bool &is_core=true, const bool &is_Ls=false, const std::vector< cytnx_int64 > &trucate_dim=std::vector< cytnx_int64 >())
Tensor Vectordot(const Tensor &Tl, const Tensor &Tr, const bool &is_conj=false)
perform inner product of vectors
void iDiv(Tensor &Lt, const Tensor &Rt, bool rhs_is_weak_scalar=false)
The inplace division function for Tensor, inplacely.
void Exp_(Tensor &Tin)
inplace perform Exponential on all the element in Tensor.
std::vector< cytnx::UniTensor > Svd_truncate(const cytnx::UniTensor &Tin, const cytnx_uint64 &keepdim, const double &err=0., const bool &is_UvT=true, const unsigned int &return_err=0, const cytnx_uint64 &mindim=1)
Perform Singular-Value decomposition on a UniTensor with truncation.
Tensor Exp(const Tensor &Tin)
Exponential all the element in Tensor.
Tensor Ger(const Tensor &x, const Tensor &y, const Scalar &a=Scalar())
Blas Ger, performing return = a*vec(x)*vec(y)^T.
void Pow_(cytnx::UniTensor &Tin, const double &p)
Take the power p of all elements in a UniTensor, inplacely.
Tensor Gemm(const Scalar &a, const Tensor &x, const Tensor &y)
Blas Gemm, performing return.
Scalar norm(const Tensor &Tl)
Calculate the norm of a tensor.
void iAdd(Tensor &Lt, const Tensor &Rt, bool rhs_is_weak_scalar=false)
The addition function for Tensor, inplacely.
std::vector< Tensor > Lanczos_ER(LinOp *Hop, const cytnx_uint64 &k=1, const bool &is_V=true, const cytnx_uint64 &maxiter=10000, const double &CvgCrit=1.0e-14, const bool &is_row=false, const Tensor &Tin=Tensor(), const cytnx_uint32 &max_krydim=4, const bool &verbose=false)
perform Lanczos for hermitian/symmetric matrices or linear function.
std::vector< UniTensor > Lanczos_Gnd_Ut(LinOp *Hop, const cytnx::UniTensor &Tin, const double &CvgCrit=1.0e-14, const bool &is_V=true, const bool &verbose=false, const unsigned int &Maxiter=100000)
perform Lanczos for hermitian/symmetric matrices or linear function to get ground state and lowest ei...
std::vector< cytnx::UniTensor > Qdr(const cytnx::UniTensor &Tin, const bool &is_tau=false)
Perform the QDR decomposition on a UniTensor.
void InvM_(Tensor &Tin)
inplace matrix inverse.
cytnx::UniTensor Add(const cytnx::UniTensor &Lt, const cytnx::UniTensor &Rt)
The addition function between two UniTensor.
Tensor Tensordot_dg(const Tensor &Tl, const Tensor &Tr, const std::vector< cytnx_uint64 > &idxl, const std::vector< cytnx_uint64 > &idxr, const bool &diag_L)
perform tensor dot by sum out the indices assigned of two Tensors, with either one of them to be a ra...
Tensor Directsum(const Tensor &T1, const Tensor &T2, const std::vector< cytnx_uint64 > &shared_axes)
perform directsum of two Tensor.
void iSub(Tensor &Lt, const Tensor &Rt, bool rhs_is_weak_scalar=false)
The subtraction function for Tensot, inplscely.
cytnx::UniTensor ExpM(const cytnx::UniTensor &Tin, const T &a, const T &b=0)
Perform the exponential function on a UniTensor.
void iMul(Tensor &Lt, const Tensor &Rt, bool rhs_is_weak_scalar=false)
The multiplication function for Tensor, inplacely.
std::vector< Tensor > Arnoldi(LinOp *Hop, const Tensor &Tin=Tensor(), const std::string which="LM", const cytnx_uint64 &maxiter=10000, const cytnx_double &cvg_crit=0, const cytnx_uint64 &k=1, const bool &is_V=true, const cytnx_int32 &ncv=0, const bool &verbose=false)
Performs Arnoldi iteration for matrices or linear functions.
std::vector< Tensor > Lanczos_Gnd(LinOp *Hop, const double &CvgCrit=1.0e-14, const bool &is_V=true, const Tensor &Tin=Tensor(), const bool &verbose=false, const unsigned int &Maxiter=100000)
perform Lanczos for hermitian/symmetric matrices or linear function to get ground state and lowest ei...
cytnx::UniTensor ExpH(const cytnx::UniTensor &Tin, const T &a, const T &b=0)
Perform the exponential function on a UniTensor, which the blocks are Hermitian matrix.
std::vector< cytnx::UniTensor > Rsvd(const cytnx::UniTensor &Tin, cytnx_uint64 keepdim, double err=0., bool is_U=true, bool is_vT=true, unsigned int return_err=0, cytnx_uint64 mindim=1, cytnx_uint64 oversampling_summand=10, double oversampling_factor=1., cytnx_uint64 power_iteration=0, unsigned int seed=random::__static_random_device())
Perform a randomized truncated Singular-Value decomposition of a UniTensor.
Tensor Cpr(const Tensor &Lt, const Tensor &Rt)
The comparison function for Tensor.
Tensor Kron(const Tensor &lhs, const Tensor &rhs, bool lhs_pad_left=false, bool rhs_pad_left=false)
perform kronecker produces of two Tensor.
void Inv_(cytnx::UniTensor &Tin, double clip=-1.)
Element-wise (pseudo-)inverse, inplacely.
Tensor Matmul_dg(const Tensor &Tl, const Tensor &Tr)
perform matrix multiplication on two Tensors with one rank-1 and the other rank-2 where the rank-1 re...
Tensor InvM(const Tensor &Tin)
Matrix inverse.
cytnx::UniTensor Div(const cytnx::UniTensor &Lt, const cytnx::UniTensor &Rt)
The division function between two UniTensor.
std::random_device __static_random_device
Definition UniTensor.hpp:27
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.
Tensor operator==(const Tensor &Lt, const Tensor &Rt)
The comparison operator for Tensor.
cytnx::UniTensor operator%(const cytnx::UniTensor &Lt, const cytnx::UniTensor &Rt)
The modulo 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.