Cytnx v0.7.3
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 bool &is_U=true, const bool &is_vT=true);
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, const cytnx_int64 &b, 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
158
159 // Sub:
160 //==================================================
164 Tensor Sub(const Tensor &Lt, const Tensor &Rt);
165 template<class T>
166 Tensor Sub(const T &lc, const Tensor &Rt);
167 template<class T>
168 Tensor Sub(const Tensor &Lt, const T &rc);
169
170
171 // Mul:
172 //==================================================
176 Tensor Mul(const Tensor &Lt, const Tensor &Rt);
177 template<class T>
178 Tensor Mul(const T &lc, const Tensor &Rt);
179 template<class T>
180 Tensor Mul(const Tensor &Lt, const T &rc);
181
182
183
184 // Div:
185 //==================================================
189 Tensor Div(const Tensor &Lt, const Tensor &Rt);
190 template<class T>
191 Tensor Div(const T &lc, const Tensor &Rt);
192 template<class T>
193 Tensor Div(const Tensor &Lt, const T &rc);
194
195 // Mod:
196 //==================================================
200 Tensor Mod(const Tensor &Lt, const Tensor &Rt);
201 template<class T>
202 Tensor Mod(const T &lc, const Tensor &Rt);
203 template<class T>
204 Tensor Mod(const Tensor &Lt, const T &rc);
205
206
207
208 // Cpr:
209 //==================================================
213 Tensor Cpr(const Tensor &Lt, const Tensor &Rt);
214 template<class T>
215 Tensor Cpr(const T &lc, const Tensor &Rt);
216 template<class T>
217 Tensor Cpr(const Tensor &Lt, const T &rc);
218
219
220
221 // Norm:
222 //=================================================
232 Tensor Norm(const Tensor &Tl);
233
234 // Det:
235 //=================================================
244 Tensor Det(const Tensor &Tl);
245
246
247
248 // Svd:
249 //==================================================
261 std::vector<Tensor> Svd(const Tensor &Tin, const bool &is_U=true, const bool &is_vT=true);
262
263
264
265 // Svd_truncate:
266 //==================================================
267 std::vector<Tensor> Svd_truncate(const Tensor &Tin, const cytnx_uint64 &keepdim,const bool &is_U=true, const bool &is_vT=true);
268
269
270 // Hosvd:
271 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>());
272
273
274 // Qr:
275 //==================================================
286 std::vector<Tensor> Qr(const Tensor &Tin, const bool &is_tau=false);
287
288 // Qdr:
289 //==================================================
301 std::vector<Tensor> Qdr(const Tensor &Tin, const bool &is_tau=false);
302
303
304 // Eigh:
305 //==================================================
313 std::vector<Tensor> Eigh(const Tensor &Tin, const bool &is_V=true, const bool &row_v=false);
314
315 // Eig:
316 //==================================================
325 std::vector<Tensor> Eig(const Tensor &Tin, const bool &is_V=true, const bool &row_v=false);
326
327
328
329 // Trace:
330 //==================================================
336 Tensor Trace(const Tensor &Tn, const cytnx_uint64 &axisA=0, const cytnx_uint64 &axisB=1);
337
338 // Min:
339 //==================================================
345 Tensor Min(const Tensor &Tn);
346
347
348 // Max:
349 //==================================================
355 Tensor Max(const Tensor &Tn);
356
357 // Sum:
358 //==================================================
363 Tensor Sum(const Tensor &Tn);
364
365
366 // Matmul:
367 //==================================================
373 Tensor Matmul(const Tensor &TL, const Tensor &TR);
374
375
376 // Matmul:
377 //==================================================
383 Tensor Matmul_dg(const Tensor &Tl, const Tensor &Tr);
384
385
386
387 // InvM:
388 //==================================================
396 Tensor InvM(const Tensor &Tin);
405 void InvM_(Tensor &Tin);
406
407
408 //Inv:
409 //==================================================
421 Tensor Inv(const Tensor &Tin, const double &clip);
422
435 void Inv_(Tensor &Tin, const double &clip);
436
437
438 // Conj:
439 //==================================================
449 Tensor Conj(const Tensor &Tin);
457 void Conj_(Tensor &Tin);
458
459
460 // Exp:
461 //==================================================
468 Tensor Exp(const Tensor &Tin);
469
476 Tensor Expf(const Tensor &Tin);
477
487 void Exp_(Tensor &Tin);
488
498 void Expf_(Tensor &Tin);
499
500
501 // Pow:
502 //==================================================
510 Tensor Pow(const Tensor &Tin, const double &p);
511
520 void Pow_(Tensor &Tin, const double &p);
521
522
523 // Abs:
524 //==================================================
532 Tensor Abs(const Tensor &Tin);
533
541 void Abs_(Tensor &Tin);
542
543
544
545 // Diag:
546 //==================================================
559 Tensor Diag(const Tensor &Tin);
560
561 //Tensordot:
562 //==================================================
578 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);
579
580 //Tensordot_dg:
581 //==================================================
597 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);
598
599
600
601 //Outer:
602 //==================================================
617 Tensor Outer(const Tensor &Tl, const Tensor &Tr);
618
619 //Kron:
620 //==================================================
638 Tensor Kron(const Tensor &Tl,const Tensor &Tr, const bool &Tl_pad_left=false, const bool &Tr_pad_left=false);
639
640 //VectorDot:
641 //=================================================
657 Tensor Vectordot(const Tensor &Tl, const Tensor &Tr, const bool &is_conj=false);
658
659
660 //Dot:
661 //=================================================
680 Tensor Dot(const Tensor &Tl, const Tensor &Tr);
681
682
683
684
685 //Tridiag:
686 //===========================================
703 std::vector<Tensor> Tridiag(const Tensor &Diag, const Tensor &Sub_diag, const bool &is_V=true, const bool &is_row=false);
704
705
706
707 //ExpH:
708 //===========================================
721 Tensor ExpH(const Tensor &in,const cytnx_double &a=1, const cytnx_double &b=0);
722
723 //ExpM:
724 //===========================================
737 Tensor ExpM(const Tensor &in,const cytnx_double &a=1, const cytnx_double &b=0);
738
739
740
741
742
743
744
745 //Lanczos:
746 //===========================================
770 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);
771
772 //Lanczos:
773 //===========================================
792 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);
793
794 //Lstsq:
795 //===========================================
813 std::vector<Tensor> Lstsq(const Tensor &A, const Tensor &b, const float &rcond=-1);
814
815 }// namespace linalg
816
817
818 // operators:
819 Tensor operator+(const Tensor &Lt, const Tensor &Rt);
820 template<class T>
821 Tensor operator+(const T &lc, const Tensor &Rt);
822 template<class T>
823 Tensor operator+(const Tensor &Lt, const T &rc);
824
825 //------------------------------------
826 Tensor operator-(const Tensor &Lt, const Tensor &Rt);
827 template<class T>
828 Tensor operator-(const T &lc, const Tensor &Rt);
829 template<class T>
830 Tensor operator-(const Tensor &Lt, const T &rc);
831
832
833 //-----------------------------------
834 Tensor operator*(const Tensor &Lt, const Tensor &Rt);
835 template<class T>
836 Tensor operator*(const T &lc, const Tensor &Rt);
837 template<class T>
838 Tensor operator*(const Tensor &Lt, const T &rc);
839
840
841 //----------------------------------
842 Tensor operator/(const Tensor &Lt, const Tensor &Rt);
843 template<class T>
844 Tensor operator/(const T &lc, const Tensor &Rt);
845 template<class T>
846 Tensor operator/(const Tensor &Lt, const T &rc);
847
848 //----------------------------------
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 //----------------------------------
857 Tensor operator==(const Tensor &Lt, const Tensor &Rt);
858 template<class T>
859 Tensor operator==(const T &lc, const Tensor &Rt);
860 template<class T>
861 Tensor operator==(const Tensor &Lt, const T &rc);
862
863
864
865
866}
867
868
869#endif
Definition LinOp.hpp:19
an tensor (multi-dimensional array)
Definition Tensor.hpp:289
An Enhanced tensor specifically designed for physical Tensor network simulation.
Definition UniTensor.hpp:1072
cytnx::UniTensor Trace(const cytnx::UniTensor &Tin, const cytnx_int64 &a, const cytnx_int64 &b, const bool &by_label=false)
std::vector< cytnx::UniTensor > Qr(const cytnx::UniTensor &Tin, const bool &is_tau=false)
std::vector< cytnx::UniTensor > Svd_truncate(const cytnx::UniTensor &Tin, const cytnx_uint64 &keepdim, const bool &is_U=true, const bool &is_vT=true)
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 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.
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)
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.
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.
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.
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)
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)