Cytnx v0.7.6
Loading...
Searching...
No Matches
linalg.hpp
Go to the documentation of this file.
1#ifndef _linalg_H_
2#define _linalg_H_
3
4#include "Type.hpp"
5#include "cytnx_error.hpp"
6#include "Tensor.hpp"
7#include "Storage.hpp"
8#include "UniTensor.hpp"
9#include "Scalar.hpp"
10#include "LinOp.hpp"
11#include <functional>
12
13
14namespace cytnx{
15 //class Tensor; //fwd
16 //class UniTensor; //fwd
17 //class LinOp; //fwd
18
19 namespace linalg{
20
21
22
23 // Add:
24 //==================================================
29 template<class T>
30 cytnx::UniTensor Add(const T &lc,const cytnx::UniTensor &Rt);
31 template<class T>
32 cytnx::UniTensor Add(const cytnx::UniTensor &Lt,const T &rc);
33
34 // Sub:
35 //==================================================
40 template<class T>
41 cytnx::UniTensor Sub(const T &lc, const cytnx::UniTensor &Rt);
42 template<class T>
43 cytnx::UniTensor Sub(const cytnx::UniTensor &Lt, const T &rc);
44
45 // Mul:
46 //==================================================
51 template<class T>
52 cytnx::UniTensor Mul(const T &lc,const cytnx::UniTensor &Rt);
53 template<class T>
54 cytnx::UniTensor Mul(const cytnx::UniTensor &Lt,const T &rc);
55
56 // Div:
57 //==================================================
62 template<class T>
63 cytnx::UniTensor Div(const T &lc,const cytnx::UniTensor &Rt);
64 template<class T>
65 cytnx::UniTensor Div(const cytnx::UniTensor &Lt,const T &rc);
66
67
68 // Mod:
69 //==================================================
74 template<class T>
75 cytnx::UniTensor Mod(const T &lc,const cytnx::UniTensor &Rt);
76 template<class T>
77 cytnx::UniTensor Mod(const cytnx::UniTensor &Lt,const T &rc);
78
79
80 std::vector<cytnx::UniTensor> Svd(const cytnx::UniTensor &Tin, const bool &is_U=true, const bool &is_vT=true);
81 std::vector<cytnx::UniTensor> Svd_truncate(const cytnx::UniTensor &Tin, const cytnx_uint64 &keepdim, const double &err=0, const bool &is_U=true, const bool &is_vT=true, const bool &return_err=false);
82 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>());
83
84 cytnx::UniTensor ExpH(const cytnx::UniTensor &Tin, const double &a=1, const double &b=0);
85 cytnx::UniTensor ExpM(const cytnx::UniTensor &Tin, const double &a=1, const double &b=0);
86 cytnx::UniTensor Trace(const cytnx::UniTensor &Tin, const cytnx_int64 &a=0, const cytnx_int64 &b=1, const bool &by_label=false);
87 std::vector<cytnx::UniTensor> Qr(const cytnx::UniTensor &Tin, const bool &is_tau=false);
88 std::vector<cytnx::UniTensor> Qdr(const cytnx::UniTensor &Tin, const bool &is_tau=false);
89
90 // Pow:
91 //==================================================
99 UniTensor Pow(const UniTensor &Tin, const double &p);
100
109 void Pow_(UniTensor &Tin, const double &p);
110
111 }//linalg
112
114 template<class T>
116 template<class T>
118
120 template<class T>
122 template<class T>
124
126 template<class T>
128 template<class T>
130
132 template<class T>
134 template<class T>
136
138 template<class T>
140 template<class T>
142}
143
144//====================================================================================
145//====================================================================================
146//====================================================================================
147namespace cytnx{
152 namespace linalg{
153 Tensor Add(const Tensor &Lt, const Tensor &Rt);
154 template<class T>
155 Tensor Add(const T &lc, const Tensor &Rt);
156 template<class T>
157 Tensor Add(const Tensor &Lt, const T &rc);
158
159 void iAdd(Tensor &Lt, const Tensor &Rt);
160
161
162 // Sub:
163 //==================================================
167 Tensor Sub(const Tensor &Lt, const Tensor &Rt);
168 template<class T>
169 Tensor Sub(const T &lc, const Tensor &Rt);
170 template<class T>
171 Tensor Sub(const Tensor &Lt, const T &rc);
172
173 void iSub(Tensor &Lt, const Tensor &Rt);
174
175 // Mul:
176 //==================================================
180 Tensor Mul(const Tensor &Lt, const Tensor &Rt);
181 template<class T>
182 Tensor Mul(const T &lc, const Tensor &Rt);
183 template<class T>
184 Tensor Mul(const Tensor &Lt, const T &rc);
185
186 void iMul(Tensor &Lt, const Tensor &Rt);
187
188 // Div:
189 //==================================================
193 Tensor Div(const Tensor &Lt, const Tensor &Rt);
194 template<class T>
195 Tensor Div(const T &lc, const Tensor &Rt);
196 template<class T>
197 Tensor Div(const Tensor &Lt, const T &rc);
198
199 void iDiv(Tensor &Lt, const Tensor &Rt);
200
201 // Mod:
202 //==================================================
206 Tensor Mod(const Tensor &Lt, const Tensor &Rt);
207 template<class T>
208 Tensor Mod(const T &lc, const Tensor &Rt);
209 template<class T>
210 Tensor Mod(const Tensor &Lt, const T &rc);
211
212
213
214 // Cpr:
215 //==================================================
219 Tensor Cpr(const Tensor &Lt, const Tensor &Rt);
220 template<class T>
221 Tensor Cpr(const T &lc, const Tensor &Rt);
222 template<class T>
223 Tensor Cpr(const Tensor &Lt, const T &rc);
224
225
226
227 // Norm:
228 //=================================================
238 Tensor Norm(const Tensor &Tl);
239
240 // Det:
241 //=================================================
250 Tensor Det(const Tensor &Tl);
251
252
253
254 // Svd:
255 //==================================================
267 std::vector<Tensor> Svd(const Tensor &Tin, const bool &is_U=true, const bool &is_vT=true);
268
269
270
271 // Svd_truncate:
272 //==================================================
273 std::vector<Tensor> Svd_truncate(const Tensor &Tin, const cytnx_uint64 &keepdim, const double &err=0, const bool &is_U=true, const bool &is_vT=true, const bool &return_err=false);
274
275
276 // Hosvd:
277 std::vector<Tensor> Hosvd(const Tensor &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>());
278
279
280 // Qr:
281 //==================================================
292 std::vector<Tensor> Qr(const Tensor &Tin, const bool &is_tau=false);
293
294 // Qdr:
295 //==================================================
307 std::vector<Tensor> Qdr(const Tensor &Tin, const bool &is_tau=false);
308
309
310 // Eigh:
311 //==================================================
319 std::vector<Tensor> Eigh(const Tensor &Tin, const bool &is_V=true, const bool &row_v=false);
320
321 // Eig:
322 //==================================================
331 std::vector<Tensor> Eig(const Tensor &Tin, const bool &is_V=true, const bool &row_v=false);
332
333
334
335 // Trace:
336 //==================================================
342 Tensor Trace(const Tensor &Tn, const cytnx_uint64 &axisA=0, const cytnx_uint64 &axisB=1);
343
344 // Min:
345 //==================================================
351 Tensor Min(const Tensor &Tn);
352
353
354 // Max:
355 //==================================================
361 Tensor Max(const Tensor &Tn);
362
363 // Sum:
364 //==================================================
369 Tensor Sum(const Tensor &Tn);
370
371
372 // Matmul:
373 //==================================================
379 Tensor Matmul(const Tensor &TL, const Tensor &TR);
380
381
382 // Matmul:
383 //==================================================
389 Tensor Matmul_dg(const Tensor &Tl, const Tensor &Tr);
390
391
392
393 // InvM:
394 //==================================================
402 Tensor InvM(const Tensor &Tin);
411 void InvM_(Tensor &Tin);
412
413
414 //Inv:
415 //==================================================
427 Tensor Inv(const Tensor &Tin, const double &clip);
428
441 void Inv_(Tensor &Tin, const double &clip);
442
443
444 // Conj:
445 //==================================================
455 Tensor Conj(const Tensor &Tin);
463 void Conj_(Tensor &Tin);
464
465
466 // Exp:
467 //==================================================
474 Tensor Exp(const Tensor &Tin);
475
482 Tensor Expf(const Tensor &Tin);
483
493 void Exp_(Tensor &Tin);
494
504 void Expf_(Tensor &Tin);
505
506
507 // Pow:
508 //==================================================
516 Tensor Pow(const Tensor &Tin, const double &p);
517
526 void Pow_(Tensor &Tin, const double &p);
527
528
529 // Abs:
530 //==================================================
538 Tensor Abs(const Tensor &Tin);
539
547 void Abs_(Tensor &Tin);
548
549
550
551 // Diag:
552 //==================================================
565 Tensor Diag(const Tensor &Tin);
566
567 //Tensordot:
568 //==================================================
584 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);
585
586 //Tensordot_dg:
587 //==================================================
603 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);
604
605
606
607 //Outer:
608 //==================================================
623 Tensor Outer(const Tensor &Tl, const Tensor &Tr);
624
625 //Kron:
626 //==================================================
644 Tensor Kron(const Tensor &Tl,const Tensor &Tr, const bool &Tl_pad_left=false, const bool &Tr_pad_left=false);
645
646 //VectorDot:
647 //=================================================
663 Tensor Vectordot(const Tensor &Tl, const Tensor &Tr, const bool &is_conj=false);
664
665
666 //Dot:
667 //=================================================
686 Tensor Dot(const Tensor &Tl, const Tensor &Tr);
687
688
689
690
691 //Tridiag:
692 //===========================================
709 std::vector<Tensor> Tridiag(const Tensor &Diag, const Tensor &Sub_diag, const bool &is_V=true, const bool &is_row=false);
710
711
712
713 //ExpH:
714 //===========================================
727 Tensor ExpH(const Tensor &in,const cytnx_double &a=1, const cytnx_double &b=0);
728
729 //ExpM:
730 //===========================================
743 Tensor ExpM(const Tensor &in,const cytnx_double &a=1, const cytnx_double &b=0);
744
745
746
747
748
749
750
751 //Lanczos:
752 //===========================================
776 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);
777
778 //Lanczos:
779 //===========================================
798 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);
799
800 //Lanczos:
801 //===============================================
820 std::vector<UniTensor> Lanczos_Gnd_Ut(LinOp *Hop, const UniTensor &Tin, const double &CvgCrit=1.0e-14, const bool &is_V=true, const bool &verbose=false, const unsigned int &Maxiter=100000);
821
822
823
824
825
826 //Lstsq:
827 //===========================================
845 std::vector<Tensor> Lstsq(const Tensor &A, const Tensor &b, const float &rcond=-1);
846
847 }// namespace linalg
848
849
850 // operators:
851 Tensor operator+(const Tensor &Lt, const Tensor &Rt);
852 template<class T>
853 Tensor operator+(const T &lc, const Tensor &Rt);
854 template<class T>
855 Tensor operator+(const Tensor &Lt, const T &rc);
856
857 //------------------------------------
858 Tensor operator-(const Tensor &Lt, const Tensor &Rt);
859 template<class T>
860 Tensor operator-(const T &lc, const Tensor &Rt);
861 template<class T>
862 Tensor operator-(const Tensor &Lt, const T &rc);
863
864
865 //-----------------------------------
866 Tensor operator*(const Tensor &Lt, const Tensor &Rt);
867 template<class T>
868 Tensor operator*(const T &lc, const Tensor &Rt);
869 template<class T>
870 Tensor operator*(const Tensor &Lt, const T &rc);
871
872
873 //----------------------------------
874 Tensor operator/(const Tensor &Lt, const Tensor &Rt);
875 template<class T>
876 Tensor operator/(const T &lc, const Tensor &Rt);
877 template<class T>
878 Tensor operator/(const Tensor &Lt, const T &rc);
879
880 //----------------------------------
881 Tensor operator%(const Tensor &Lt, const Tensor &Rt);
882 template<class T>
883 Tensor operator%(const T &lc, const Tensor &Rt);
884 template<class T>
885 Tensor operator%(const Tensor &Lt, const T &rc);
886
887
888 //----------------------------------
889 Tensor operator==(const Tensor &Lt, const Tensor &Rt);
890 template<class T>
891 Tensor operator==(const T &lc, const Tensor &Rt);
892 template<class T>
893 Tensor operator==(const Tensor &Lt, const T &rc);
894
895
896
897
898}
899
900
901#endif
Definition LinOp.hpp:20
an tensor (multi-dimensional array)
Definition Tensor.hpp:344
An Enhanced tensor specifically designed for physical Tensor network simulation.
Definition UniTensor.hpp:1122
std::vector< cytnx::UniTensor > Qr(const cytnx::UniTensor &Tin, const bool &is_tau=false)
Tensor Dot(const Tensor &Tl, const Tensor &Tr)
dot product of two arrays.
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 iSub(Tensor &Lt, const Tensor &Rt)
void Abs_(Tensor &Tin)
inplace perform elementwiase absolute value.
void Pow_(UniTensor &Tin, const double &p)
inplace perform power on all the elements in UniTensor.
Tensor Abs(const Tensor &Tin)
Elementwise absolute value.
Tensor Outer(const Tensor &Tl, const Tensor &Tr)
perform outer produces of two rank-1 Tensor.
Tensor Conj(const Tensor &Tin)
Conjugate all the element in Tensor.
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.
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.
void Expf_(Tensor &Tin)
inplace perform Exponential on all the element in Tensor.
std::vector< UniTensor > Lanczos_Gnd_Ut(LinOp *Hop, const 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...
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
cytnx::UniTensor ExpM(const cytnx::UniTensor &Tin, const double &a=1, const double &b=0)
std::vector< cytnx::UniTensor > Svd_truncate(const cytnx::UniTensor &Tin, const cytnx_uint64 &keepdim, const double &err=0, const bool &is_U=true, const bool &is_vT=true, const bool &return_err=false)
Tensor Inv(const Tensor &Tin, const double &clip)
Element-wise inverse with clip.
cytnx::UniTensor Mul(const cytnx::UniTensor &Lt, const cytnx::UniTensor &Rt)
element-wise subtract
Tensor Max(const Tensor &Tn)
get the maximum element.
cytnx::UniTensor Sub(const cytnx::UniTensor &Lt, const cytnx::UniTensor &Rt)
element-wise subtract
Tensor Norm(const Tensor &Tl)
calculate the norm of a tensor.
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 >())
void Inv_(Tensor &Tin, const double &clip)
inplace perform Element-wise inverse with clip.
Tensor Vectordot(const Tensor &Tl, const Tensor &Tr, const bool &is_conj=false)
perform inner product of vectors
void Exp_(Tensor &Tin)
inplace perform Exponential on all the element in Tensor.
Tensor Exp(const Tensor &Tin)
Exponential all the element in Tensor.
cytnx::UniTensor Trace(const cytnx::UniTensor &Tin, const cytnx_int64 &a=0, const cytnx_int64 &b=1, const bool &by_label=false)
Tensor Kron(const Tensor &Tl, const Tensor &Tr, const bool &Tl_pad_left=false, const bool &Tr_pad_left=false)
perform kronecker produces of two Tensor.
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< cytnx::UniTensor > Qdr(const cytnx::UniTensor &Tin, const bool &is_tau=false)
void InvM_(Tensor &Tin)
inplace perform Matrix inverse.
void iDiv(Tensor &Lt, const Tensor &Rt)
cytnx::UniTensor Add(const cytnx::UniTensor &Lt, const cytnx::UniTensor &Rt)
element-wise add
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...
std::vector< Tensor > Tridiag(const Tensor &Diag, const Tensor &Sub_diag, const bool &is_V=true, const bool &is_row=false)
perform diagonalization of symmetric tri-diagnoal matrix.
std::vector< cytnx::UniTensor > Svd(const cytnx::UniTensor &Tin, const bool &is_U=true, const bool &is_vT=true)
void Conj_(Tensor &Tin)
inplace perform Conjugate on all the element in Tensor.
void iAdd(Tensor &Lt, const Tensor &Rt)
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...
Tensor Cpr(const Tensor &Lt, const Tensor &Rt)
element-wise compare
UniTensor Pow(const UniTensor &Tin, const double &p)
take power p on all the elements in UniTensor.
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 ExpH(const cytnx::UniTensor &Tin, const double &a=1, const double &b=0)
void iMul(Tensor &Lt, const Tensor &Rt)
cytnx::UniTensor Div(const cytnx::UniTensor &Lt, const cytnx::UniTensor &Rt)
element-wise divide
Definition Accessor.hpp:12
cytnx::UniTensor operator*(const cytnx::UniTensor &Lt, const cytnx::UniTensor &Rt)
double cytnx_double
Definition Type.hpp:20
uint32_t cytnx_uint32
Definition Type.hpp:23
cytnx::UniTensor operator-(const cytnx::UniTensor &Lt, const cytnx::UniTensor &Rt)
Tensor operator==(const Tensor &Lt, const Tensor &Rt)
uint64_t cytnx_uint64
Definition Type.hpp:22
int64_t cytnx_int64
Definition Type.hpp:25
cytnx::UniTensor operator%(const cytnx::UniTensor &Lt, const cytnx::UniTensor &Rt)
cytnx::UniTensor operator+(const cytnx::UniTensor &Lt, const cytnx::UniTensor &Rt)
cytnx::UniTensor operator/(const cytnx::UniTensor &Lt, const cytnx::UniTensor &Rt)