Cytnx v0.7.4
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 // Add:
22 //==================================================
27 template<class T>
28 cytnx::UniTensor Add(const T &lc,const cytnx::UniTensor &Rt);
29 template<class T>
30 cytnx::UniTensor Add(const cytnx::UniTensor &Lt,const T &rc);
31
32 // Sub:
33 //==================================================
38 template<class T>
39 cytnx::UniTensor Sub(const T &lc, const cytnx::UniTensor &Rt);
40 template<class T>
41 cytnx::UniTensor Sub(const cytnx::UniTensor &Lt, const T &rc);
42
43 // Mul:
44 //==================================================
49 template<class T>
50 cytnx::UniTensor Mul(const T &lc,const cytnx::UniTensor &Rt);
51 template<class T>
52 cytnx::UniTensor Mul(const cytnx::UniTensor &Lt,const T &rc);
53
54 // Div:
55 //==================================================
60 template<class T>
61 cytnx::UniTensor Div(const T &lc,const cytnx::UniTensor &Rt);
62 template<class T>
63 cytnx::UniTensor Div(const cytnx::UniTensor &Lt,const T &rc);
64
65
66 // Mod:
67 //==================================================
72 template<class T>
73 cytnx::UniTensor Mod(const T &lc,const cytnx::UniTensor &Rt);
74 template<class T>
75 cytnx::UniTensor Mod(const cytnx::UniTensor &Lt,const T &rc);
76
77
78 std::vector<cytnx::UniTensor> Svd(const cytnx::UniTensor &Tin, const bool &is_U=true, const bool &is_vT=true);
79 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);
80 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>());
81
82 cytnx::UniTensor ExpH(const cytnx::UniTensor &Tin, const double &a=1, const double &b=0);
83 cytnx::UniTensor ExpM(const cytnx::UniTensor &Tin, const double &a=1, const double &b=0);
84 cytnx::UniTensor Trace(const cytnx::UniTensor &Tin, const cytnx_int64 &a=0, const cytnx_int64 &b=1, const bool &by_label=false);
85 std::vector<cytnx::UniTensor> Qr(const cytnx::UniTensor &Tin, const bool &is_tau=false);
86 std::vector<cytnx::UniTensor> Qdr(const cytnx::UniTensor &Tin, const bool &is_tau=false);
87
88 // Pow:
89 //==================================================
97 UniTensor Pow(const UniTensor &Tin, const double &p);
98
107 void Pow_(UniTensor &Tin, const double &p);
108
109 }//linalg
110
112 template<class T>
114 template<class T>
116
118 template<class T>
120 template<class T>
122
124 template<class T>
126 template<class T>
128
130 template<class T>
132 template<class T>
134
136 template<class T>
138 template<class T>
140}
141
142//====================================================================================
143//====================================================================================
144//====================================================================================
145namespace cytnx{
150 namespace linalg{
151 Tensor Add(const Tensor &Lt, const Tensor &Rt);
152 template<class T>
153 Tensor Add(const T &lc, const Tensor &Rt);
154 template<class T>
155 Tensor Add(const Tensor &Lt, const T &rc);
156
157 void iAdd(Tensor &Lt, const Tensor &Rt);
158
159
160 // Sub:
161 //==================================================
165 Tensor Sub(const Tensor &Lt, const Tensor &Rt);
166 template<class T>
167 Tensor Sub(const T &lc, const Tensor &Rt);
168 template<class T>
169 Tensor Sub(const Tensor &Lt, const T &rc);
170
171 void iSub(Tensor &Lt, const Tensor &Rt);
172
173 // Mul:
174 //==================================================
178 Tensor Mul(const Tensor &Lt, const Tensor &Rt);
179 template<class T>
180 Tensor Mul(const T &lc, const Tensor &Rt);
181 template<class T>
182 Tensor Mul(const Tensor &Lt, const T &rc);
183
184 void iMul(Tensor &Lt, const Tensor &Rt);
185
186 // Div:
187 //==================================================
191 Tensor Div(const Tensor &Lt, const Tensor &Rt);
192 template<class T>
193 Tensor Div(const T &lc, const Tensor &Rt);
194 template<class T>
195 Tensor Div(const Tensor &Lt, const T &rc);
196
197 void iDiv(Tensor &Lt, const Tensor &Rt);
198
199 // Mod:
200 //==================================================
204 Tensor Mod(const Tensor &Lt, const Tensor &Rt);
205 template<class T>
206 Tensor Mod(const T &lc, const Tensor &Rt);
207 template<class T>
208 Tensor Mod(const Tensor &Lt, const T &rc);
209
210
211
212 // Cpr:
213 //==================================================
217 Tensor Cpr(const Tensor &Lt, const Tensor &Rt);
218 template<class T>
219 Tensor Cpr(const T &lc, const Tensor &Rt);
220 template<class T>
221 Tensor Cpr(const Tensor &Lt, const T &rc);
222
223
224
225 // Norm:
226 //=================================================
236 Tensor Norm(const Tensor &Tl);
237
238 // Det:
239 //=================================================
248 Tensor Det(const Tensor &Tl);
249
250
251
252 // Svd:
253 //==================================================
265 std::vector<Tensor> Svd(const Tensor &Tin, const bool &is_U=true, const bool &is_vT=true);
266
267
268
269 // Svd_truncate:
270 //==================================================
271 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);
272
273
274 // Hosvd:
275 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>());
276
277
278 // Qr:
279 //==================================================
290 std::vector<Tensor> Qr(const Tensor &Tin, const bool &is_tau=false);
291
292 // Qdr:
293 //==================================================
305 std::vector<Tensor> Qdr(const Tensor &Tin, const bool &is_tau=false);
306
307
308 // Eigh:
309 //==================================================
317 std::vector<Tensor> Eigh(const Tensor &Tin, const bool &is_V=true, const bool &row_v=false);
318
319 // Eig:
320 //==================================================
329 std::vector<Tensor> Eig(const Tensor &Tin, const bool &is_V=true, const bool &row_v=false);
330
331
332
333 // Trace:
334 //==================================================
340 Tensor Trace(const Tensor &Tn, const cytnx_uint64 &axisA=0, const cytnx_uint64 &axisB=1);
341
342 // Min:
343 //==================================================
349 Tensor Min(const Tensor &Tn);
350
351
352 // Max:
353 //==================================================
359 Tensor Max(const Tensor &Tn);
360
361 // Sum:
362 //==================================================
367 Tensor Sum(const Tensor &Tn);
368
369
370 // Matmul:
371 //==================================================
377 Tensor Matmul(const Tensor &TL, const Tensor &TR);
378
379
380 // Matmul:
381 //==================================================
387 Tensor Matmul_dg(const Tensor &Tl, const Tensor &Tr);
388
389
390
391 // InvM:
392 //==================================================
400 Tensor InvM(const Tensor &Tin);
409 void InvM_(Tensor &Tin);
410
411
412 //Inv:
413 //==================================================
425 Tensor Inv(const Tensor &Tin, const double &clip);
426
439 void Inv_(Tensor &Tin, const double &clip);
440
441
442 // Conj:
443 //==================================================
453 Tensor Conj(const Tensor &Tin);
461 void Conj_(Tensor &Tin);
462
463
464 // Exp:
465 //==================================================
472 Tensor Exp(const Tensor &Tin);
473
480 Tensor Expf(const Tensor &Tin);
481
491 void Exp_(Tensor &Tin);
492
502 void Expf_(Tensor &Tin);
503
504
505 // Pow:
506 //==================================================
514 Tensor Pow(const Tensor &Tin, const double &p);
515
524 void Pow_(Tensor &Tin, const double &p);
525
526
527 // Abs:
528 //==================================================
536 Tensor Abs(const Tensor &Tin);
537
545 void Abs_(Tensor &Tin);
546
547
548
549 // Diag:
550 //==================================================
563 Tensor Diag(const Tensor &Tin);
564
565 //Tensordot:
566 //==================================================
582 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);
583
584 //Tensordot_dg:
585 //==================================================
601 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);
602
603
604
605 //Outer:
606 //==================================================
621 Tensor Outer(const Tensor &Tl, const Tensor &Tr);
622
623 //Kron:
624 //==================================================
642 Tensor Kron(const Tensor &Tl,const Tensor &Tr, const bool &Tl_pad_left=false, const bool &Tr_pad_left=false);
643
644 //VectorDot:
645 //=================================================
661 Tensor Vectordot(const Tensor &Tl, const Tensor &Tr, const bool &is_conj=false);
662
663
664 //Dot:
665 //=================================================
684 Tensor Dot(const Tensor &Tl, const Tensor &Tr);
685
686
687
688
689 //Tridiag:
690 //===========================================
707 std::vector<Tensor> Tridiag(const Tensor &Diag, const Tensor &Sub_diag, const bool &is_V=true, const bool &is_row=false);
708
709
710
711 //ExpH:
712 //===========================================
725 Tensor ExpH(const Tensor &in,const cytnx_double &a=1, const cytnx_double &b=0);
726
727 //ExpM:
728 //===========================================
741 Tensor ExpM(const Tensor &in,const cytnx_double &a=1, const cytnx_double &b=0);
742
743
744
745
746
747
748
749 //Lanczos:
750 //===========================================
774 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);
775
776 //Lanczos:
777 //===========================================
796 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);
797
798 //Lanczos:
799 //===============================================
818 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);
819
820
821
822
823
824 //Lstsq:
825 //===========================================
843 std::vector<Tensor> Lstsq(const Tensor &A, const Tensor &b, const float &rcond=-1);
844
845 }// namespace linalg
846
847
848 // operators:
849 Tensor operator+(const Tensor &Lt, const Tensor &Rt);
850 template<class T>
851 Tensor operator+(const T &lc, const Tensor &Rt);
852 template<class T>
853 Tensor operator+(const Tensor &Lt, const T &rc);
854
855 //------------------------------------
856 Tensor operator-(const Tensor &Lt, const Tensor &Rt);
857 template<class T>
858 Tensor operator-(const T &lc, const Tensor &Rt);
859 template<class T>
860 Tensor operator-(const Tensor &Lt, const T &rc);
861
862
863 //-----------------------------------
864 Tensor operator*(const Tensor &Lt, const Tensor &Rt);
865 template<class T>
866 Tensor operator*(const T &lc, const Tensor &Rt);
867 template<class T>
868 Tensor operator*(const Tensor &Lt, const T &rc);
869
870
871 //----------------------------------
872 Tensor operator/(const Tensor &Lt, const Tensor &Rt);
873 template<class T>
874 Tensor operator/(const T &lc, const Tensor &Rt);
875 template<class T>
876 Tensor operator/(const Tensor &Lt, const T &rc);
877
878 //----------------------------------
879 Tensor operator%(const Tensor &Lt, const Tensor &Rt);
880 template<class T>
881 Tensor operator%(const T &lc, const Tensor &Rt);
882 template<class T>
883 Tensor operator%(const Tensor &Lt, const T &rc);
884
885
886 //----------------------------------
887 Tensor operator==(const Tensor &Lt, const Tensor &Rt);
888 template<class T>
889 Tensor operator==(const T &lc, const Tensor &Rt);
890 template<class T>
891 Tensor operator==(const Tensor &Lt, const T &rc);
892
893
894
895
896}
897
898
899#endif
Definition LinOp.hpp:20
an tensor (multi-dimensional array)
Definition Tensor.hpp:333
An Enhanced tensor specifically designed for physical Tensor network simulation.
Definition UniTensor.hpp:1123
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)