-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathalgs.cc
More file actions
1049 lines (967 loc) · 28.5 KB
/
algs.cc
File metadata and controls
1049 lines (967 loc) · 28.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright 2018 The Simons Foundation, Inc. - All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include <limits>
#include <stdexcept>
#include <tuple>
#include "itensor/tensor/lapack_wrap.h"
#include "itensor/tensor/algs.h"
#include "itensor/util/iterate.h"
#include "itensor/global.h"
using std::move;
using std::sqrt;
using std::tuple;
using std::make_tuple;
using std::tie;
namespace itensor {
namespace detail {
int
hermitianDiag(int N, Real *Udata, Real *ddata)
{
LAPACK_INT info = 0;
dsyev_wrapper('V','U',N,Udata,ddata,info);
return info;
}
int
hermitianDiag(int N, Cplx *Udata,Real *ddata)
{
return zheev_wrapper(N,Udata,ddata);
}
int
QR(int M, int N, int Rrows, Real *Qdata, Real *Rdata)
{
LAPACK_INT info = 0;
std::vector<LAPACK_REAL> tau(N);
dgeqrf_wrapper(&M, &N, Qdata, &M, tau.data(), &info);
for(int i = 0; i < Rrows; i++)
for(int j = i; j < N; j++)
{
Rdata[i + j*Rrows] = Qdata[i+j*M];
}
int min = M < N ? M : N;
dorgqr_wrapper(&M, &Rrows, &min, Qdata, &M, tau.data(), &info);
return info;
}
int
QR(int M, int N, int Rrows, Cplx *Qdata, Cplx *Rdata)
{
LAPACK_INT info = 0;
std::vector<LAPACK_COMPLEX> tau(N);
zgeqrf_wrapper(&M, &N, Qdata, &M, tau.data(), &info);
for(int i = 0; i < Rrows; i++)
for(int j = i; j < N; j++)
{
Rdata[i + j*Rrows] = Qdata[i+j*M];
}
int min = M < N ? M : N;
zungqr_wrapper(&M, &Rrows, &min, Qdata, &M, tau.data(), &info);
return info;
}
int
SVD_gesdd(int M, int N, Cplx * Adata, Cplx * Udata, Real * Ddata, Cplx * Vdata)
{
LAPACK_INT info = 0;
char S = 'S';
zgesdd_wrapper(&S, &M, &N, Adata, Ddata, Udata, Vdata, &info);
return info;
}
int
SVD_gesdd(int M, int N, Real * Adata, Real * Udata, Real * Ddata, Real * Vdata)
{
LAPACK_INT info = 0;
char S = 'S';
dgesdd_wrapper(&S, &M, &N, Adata, Ddata, Udata, Vdata, &info);
return info;
}
int
SVD_gesvd(int M, int N, Cplx * Adata, Cplx * Udata, Real * Ddata, Cplx * Vdata)
{
LAPACK_INT info = 0;
char S = 'S';
zgesvd_wrapper(&S, &M, &N, Adata, Ddata, Udata, Vdata, &info);
return info;
}
int
SVD_gesvd(int M, int N, Real * Adata, Real * Udata, Real * Ddata, Real * Vdata)
{
LAPACK_INT info = 0;
char S = 'S';
dgesvd_wrapper(&S, &M, &N, Adata, Ddata, Udata, Vdata, &info);
return info;
}
} //namespace detail
//void
//diagHermitian(MatrixRefc const& Mre,
// MatrixRefc const& Mim,
// MatrixRef const& Ure,
// MatrixRef const& Uim,
// VectorRef const& d)
// {
// auto N = ncols(Mre);
// if(N != nrows(Mre))
// {
// printfln("Mre is %dx%d",nrows(Mre),ncols(Mre));
// throw std::runtime_error("diagHermitian: Input Matrix must be square");
// }
// if(N != nrows(Mim) || N != ncols(Mim))
// {
// printfln("Mim is %dx%d",nrows(Mim),ncols(Mim));
// throw std::runtime_error("diagHermitian: Input Matrix must be square, and real and imag part same size");
// }
//
//#ifdef DEBUG
// if(N < 1) throw std::runtime_error("diagHermitian: 0 dimensional matrix");
// if(!(nrows(Ure) == N && ncols(Ure) == N))
// throw std::runtime_error("diagHermitian: Ure should have same dims as M");
// if(!(nrows(Uim) == N && ncols(Uim) == N))
// throw std::runtime_error("diagHermitian: Uim should have same dims as M");
// if(d.size() != N)
// throw std::runtime_error("diagHermitian: d size should be linear size of M");
// if(!isContiguous(Ure))
// throw std::runtime_error("diagHermitian: Ure must be contiguous");
// if(!isContiguous(Uim))
// throw std::runtime_error("diagHermitian: Uim must be contiguous");
// if(!isContiguous(d))
// throw std::runtime_error("diagHermitian: d must be contiguous");
//#endif
//
// //Set Mc = -M so eigenvalues will be sorted from largest to smallest
// auto Mc = std::vector<Cplx>(N*N);
// if(isContiguous(Mre) && isContiguous(Mim))
// {
// copyNegElts(Mre.data(),Mim.data(),Mc);
// }
// else
// {
// copyNegElts(Mre.cbegin(),Mim.cbegin(),Mc);
// }
//
// auto info = zheev_wrapper(N,Mc.data(),d.data());
// if(info != 0)
// {
// throw std::runtime_error("Error condition in diagHermitian");
// }
//
// //Correct eigenvalue signs
// d *= -1;
//
// //Following code assumes Ure and Uim are contiguous
// auto ur = Ure.data();
// auto ui = Uim.data();
// for(auto& z : Mc)
// {
// (*ur) = realRef(z);
// (*ui) = imagRef(z);
// ++ur;
// ++ui;
// }
// }
//
//void
//diagHermitian(MatrixRefc const& Mre,
// MatrixRefc const& Mim,
// Matrix & Ure,
// Matrix & Uim,
// VectorRef const& d)
// {
// resize(Ure,nrows(Mre),ncols(Mre));
// resize(Uim,nrows(Mre),ncols(Mre));
// diagHermitian(Mre,Mim,makeRef(Ure),makeRef(Uim),d);
// }
//
//void
//diagHermitian(MatrixRefc const& Mre,
// MatrixRefc const& Mim,
// Matrix & Ure,
// Matrix & Uim,
// Vector & d)
// {
// resize(Ure,nrows(Mre),ncols(Mre));
// resize(Uim,nrows(Mre),ncols(Mre));
// resize(d,nrows(Mre));
// diagHermitian(Mre,Mim,makeRef(Ure),makeRef(Uim),makeRef(d));
// }
template<typename value_type>
void
diagGeneralRef(MatRefc<value_type> const& M,
MatrixRef const& Rr,
MatrixRef const& Ri,
MatrixRef const& Lr,
MatrixRef const& Li,
VectorRef const& dr,
VectorRef const& di)
{
auto N = ncols(M);
if(N < 1) throw std::runtime_error("diagGeneral: 0 dimensional matrix");
if(N != nrows(M))
{
printfln("M is %dx%d",nrows(M),ncols(M));
throw std::runtime_error("diagGeneral: Input Matrix must be square");
}
#ifdef DEBUG
if(!isContiguous(Rr))
throw std::runtime_error("diagGeneral: Rr must be contiguous");
if(!isContiguous(Ri))
throw std::runtime_error("diagGeneral: Ri must be contiguous");
if(Lr && !isContiguous(Lr))
throw std::runtime_error("diagGeneral: Lr must be contiguous");
if(Li && !isContiguous(Li))
throw std::runtime_error("diagGeneral: Li must be contiguous");
if(!isContiguous(dr))
throw std::runtime_error("diagGeneral: dr must be contiguous");
if(!isContiguous(di))
throw std::runtime_error("diagGeneral: di must be contiguous");
#endif
struct Diag
{
LAPACK_INT static
call(LAPACK_INT N, Real const* Mdata, Real *Ldata, Real *Rdata, Real *drdata, Real *didata)
{
auto cl = (Ldata==nullptr) ? 'N' : 'V';
return dgeev_wrapper(cl,'V',N,Mdata,drdata,didata,Ldata,Rdata);
}
LAPACK_INT static
call(LAPACK_INT N, Cplx const* Mdata, Cplx *Ldata, Cplx *Rdata, Real *drdata, Real *didata)
{
auto d = std::vector<Cplx>(N);
auto cl = (Ldata==nullptr) ? 'N' : 'V';
auto info = zgeev_wrapper(cl,'V',N,Mdata,d.data(),Ldata,Rdata);
for(size_t n = 0ul; n < d.size(); ++n)
{
*drdata = d[n].real();
*didata = d[n].imag();
++drdata;
++didata;
}
return info;
}
};
auto R = Mat<value_type>(N,N);
auto L = Mat<value_type>{};
if(Lr && Li) resize(L,N,N);
auto info = Diag::call(N,M.data(),L.data(),R.data(),dr.data(),di.data());
if(info != 0)
{
//println("M = \n",M);
throw std::runtime_error("Error condition in diagGeneral");
}
struct Unpack
{
void static
call(VectorRef di, MatrixRef Vr, MatrixRef Vi, MatrixRefc V)
{
//Unpack information in V
//back into actual eigenvectors
auto N = di.size();
decltype(N) n = 0;
while(n < N)
{
if(di(n) > 0)
{
//complex eigenvalue pair
column(Vr,n) &= column(V,n);
column(Vr,n+1) &= column(V,n);
column(Vi,n) &= column(V,n+1);
column(Vi,n+1) &= column(V,n+1);
column(Vi,n+1) *= -1;
n += 2;
}
else
{
column(Vr,n) &= column(V,n);
stdx::fill(column(Vi,n),0.);
n += 1;
}
}
}
void static
call(VectorRef di, MatrixRef Vr, MatrixRef Vi, CMatrixRefc V)
{
auto N = di.size();
for(decltype(N) c = 0; c < N; ++c)
for(decltype(N) r = 0; r < N; ++r)
{
Vr(r,c) = V(r,c).real();
Vi(r,c) = V(r,c).imag();
}
}
};
auto Rref = isTransposed(M) ? transpose(R) : makeRef(R);
Unpack::call(makeRef(di),makeRef(Rr),makeRef(Ri),Rref);
if(L)
{
auto Lref = isTransposed(M) ? transpose(L) : makeRef(L);
Unpack::call(makeRef(di),makeRef(Lr),makeRef(Li),Lref);
Error("Inverse step not fully implemented");
//for(auto n : range(N))
// {
// auto facr = column(Lr,n)*column(Rr,n)+column(Li,n)*column(Ri,n);
// auto faci = column(Lr,n)*column(Ri,n)-column(Li,n)*column(Rr,n);
// auto z = Cplx(facr,faci);
// printfln("z %d = %.4E",n,z);
// if(std::abs(z) <= 1E-16) Error("Ill conditioned or non-invertible matrix");
// z = 1./z;
// column(Lr,n) &= column(Lr,n)*z.real()+column(Li,n)*z.imag();
// column(Li,n) &= column(Lr,n)*z.imag()-column(Li,n)*z.real();
// }
}
}
template void
diagGeneralRef(MatRefc<Real> const& M,MatrixRef const& Rr,MatrixRef const& Ri,
MatrixRef const& Lr,MatrixRef const& Li,VectorRef const& dr,VectorRef const& di);
template void
diagGeneralRef(MatRefc<Cplx> const& M,MatrixRef const& Rr,MatrixRef const& Ri,
MatrixRef const& Lr,MatrixRef const& Li,VectorRef const& dr,VectorRef const& di);
//
// orthog
//
template<typename V>
void
orthog(MatRef<V> M,
size_t numpass)
{
auto nkeep = std::min(nrows(M), ncols(M));
auto dots = Vec<V>(nkeep);
for(auto i : range(nkeep))
{
//normalize column i
auto coli = column(M,i);
auto nrm = norm(coli);
if(nrm == 0.0)
{
randomize(coli);
nrm = norm(coli);
}
coli /= nrm;
if(i == 0) continue;
auto Mcols = columns(M,0,i);
auto dotsref = subVector(dots,0,i);
for(auto pass : range1(numpass))
{
// does dotsref &= dag(Mcols) * coli:
auto ccoli = conj(coli);
mult(Mcols,makeRef(ccoli),dotsref,true);
conjugate(dotsref);
// does coli -= Mcols * dotsref:
multSub(Mcols,dotsref,coli);
nrm = norm(coli);
if(nrm < 1E-3) --pass; //orthog is suspect
if(nrm < 1E-10) // What if a subspace was zero in all vectors?
{
randomize(coli);
nrm = norm(coli);
}
coli /= nrm;
}
}
}
template void orthog(MatRef<Real> M, size_t numpass);
template void orthog(MatRef<Cplx> M, size_t numpass);
//Real static
//sqr(Real x) { return x*x; }
//void
//orthog(MatrixRef Mr,
// MatrixRef Mi,
// size_t numpass)
// {
// auto nkeep = std::min(nrows(Mr), ncols(Mr));
// auto Dr = Vector(nkeep);
// auto Di = Vector(nkeep);
// auto cnorm = [](VectorRefc const& r,
// VectorRefc const& i)
// {
// return sqrt(sqr(norm(r))+sqr(norm(i)));
// };
// for(auto n : range(nkeep))
// {
// //normalize column n
// auto cr = column(Mr,n);
// auto ci = column(Mi,n);
// auto nrm = cnorm(cr,ci);
// if(nrm == 0.0)
// {
// randomize(cr);
// randomize(ci);
// nrm = cnorm(cr,ci);
// }
// cr /= nrm;
// ci /= nrm;
// if(n == 0) continue;
//
// auto mr = columns(Mr,0,n);
// auto mi = columns(Mi,0,n);
// auto dr = subVector(Dr,0,n);
// auto di = subVector(Di,0,n);
// for(auto pass : range1(numpass))
// {
// //// does dotsref &= transpose(Mcols) * coli:
// //mult(transpose(Mcols),coli,dotsref);
// dr &= transpose(mr)*cr+transpose(mi)*ci;
// di &= transpose(mr)*ci-transpose(mi)*cr;
// cr -= mr*dr-mi*di;
// ci -= mr*di+mi*dr;
//
// nrm = cnorm(cr,ci);
// if(nrm < 1E-3) --pass; //orthog is suspect
// if(nrm < 1E-10) // What if a subspace was zero in all vectors?
// {
// randomize(cr);
// randomize(ci);
// nrm = cnorm(cr,ci);
// }
// cr /= nrm;
// ci /= nrm;
// }
// }
// }
//
// SVD
//
//#define CHKSVD
void
checksvd(MatrixRefc const& A,
MatrixRefc const& U,
VectorRefc const& D,
MatrixRefc const& V)
{
Matrix Ach(U);
for(auto i : range1(D.size())) column(Ach,i) *= D(i);
Ach = Ach * transpose(V);
Ach -= A;
printfln("relative error with sqrt in low level svd is %.5E",norm(Ach)/norm(A));
}
tuple<bool,size_t> // == (done, start)
checkSVDDone(VectorRefc const& D,
Real thresh)
{
auto N = D.size();
if(N <= 1 || thresh <= 0)
{
//println("Got zero thresh");
return make_tuple(true,1);
}
auto D1t = D(0)*thresh;
size_t start = 1;
for(; start < N; ++start)
{
if(D(start) < D1t) break;
}
if(start >= (N-1))
return make_tuple(true,start);
return make_tuple(false,start);
}
template<typename T>
void
SVDRefImpl(MatRefc<T> const& M,
MatRef<T> const& U,
VectorRef const& D,
MatRef<T> const& V,
const Args & args)
{
auto Mr = nrows(M);
auto thresh = args.getReal("SVDThreshold",SVD_THRESH);
//Form 'density matrix' rho
Mat<T> rho, Mconj, tempV, R;
if(isCplx(M))
{
Mconj = conj(M);
rho = M * transpose(Mconj);
}
else
{
rho = M * transpose(M);
}
//Diagonalize rho: evals are squares of singular vals
diagHermitian(rho,U,D);
//Put result of Mt*U==(V*D) in V storage
if(isCplx(M)) mult(transpose(Mconj),U,V);
else mult(transpose(M),U,V);
QR(V, tempV, R, {"Complete=",false, "PositiveDiagonal=",true});
V &= std::move(tempV);
for(decltype(D.size()) i = 0; i < D.size(); ++i)
{
D(i) = std::real(R(i,i));
}
auto [done,start] = checkSVDDone(D,thresh);
if(done) return;
//
//Recursively SVD part of B
//for greater final accuracy
//
auto n = Mr-start;
//reuse rho's storage to avoid allocation
auto mv = move(rho);
reduceCols(mv,n);
auto u = columns(U,start,ncols(U));
auto v = columns(V,start,ncols(V));
//b should be close to diagonal
//but may not be perfect - fix it up below
mult(M,v,mv);
Mat<T> b;
if(isCplx(M)) b = conj(transpose(u))*mv;
else b = transpose(u)*mv;
auto d = subVector(D,start,Mr);
Mat<T> bu(n,n),
bv(n,n);
SVDRefImpl(makeRef(b),makeRef(bu),d,makeRef(bv),args);
//reuse mv's storage to avoid allocation
auto W = move(mv);
mult(u,bu,W);
u &= W;
auto X = v*bv;
v &= X;
}
template<typename T>
void
SVDRef(MatRefc<T> const& M,
MatRef<T> const& U,
VectorRef const& D,
MatRef<T> const& V,
const Args & args)
{
auto Mr = nrows(M), Mc = ncols(M);
if(Mr > Mc)
{
SVDRef(transpose(M),V,D,U,args);
conjugate(V);
conjugate(U);
}
else
{
#ifdef DEBUG
if(!(nrows(U)==Mr && ncols(U)==Mr))
throw std::runtime_error("SVD (ref version), wrong size of U");
if(!(nrows(V)==Mc && ncols(V)==Mr))
throw std::runtime_error("SVD (ref version), wrong size of V");
if(D.size()!=Mr)
throw std::runtime_error("SVD (ref version), wrong size of D");
#endif
auto svdMethod = args.getString("SVDMethod","ITensor");
if(svdMethod=="ITensor")
{
SVDRefImpl(M,U,D,V,args);
}
else if(svdMethod == "gesdd" or svdMethod == "gesvd")
{
SVDRefLAPACK(M,U,D,V,args);
}
else
{
throw std::runtime_error("Unsupported SVD method: "+svdMethod);
}
}
#ifdef CHKSVD
checksvd(M,U,D,V);
#endif
}
template void SVDRef(MatRefc<Real> const&,MatRef<Real> const&, VectorRef const&, MatRef<Real> const&,const Args&);
template void SVDRef(MatRefc<Cplx> const&,MatRef<Cplx> const&, VectorRef const&, MatRef<Cplx> const&, const Args&);
//void
//SVDRef(MatrixRefc const& Mre,
// MatrixRefc const& Mim,
// MatrixRef const& Ure,
// MatrixRef const& Uim,
// VectorRef const& D,
// MatrixRef const& Vre,
// MatrixRef const& Vim,
// Real thresh)
// {
// auto Mr = nrows(Mre),
// Mc = ncols(Mim);
//
// if(Mr > Mc)
// {
// SVDRef(transpose(Mre),transpose(Mim),Vre,Vim,D,Ure,Uim,thresh);
// Uim *= -1;
// Vim *= -1;
// return;
// }
//
//#ifdef DEBUG
// if(!(nrows(Mim)==Mr && ncols(Mim)==Mc))
// throw std::runtime_error("SVD (ref version), Mim must have same dims as Mre");
// if(!(nrows(Ure)==Mr && ncols(Ure)==Mr))
// throw std::runtime_error("SVD (ref version), wrong size of Ure");
// if(!(nrows(Uim)==Mr && ncols(Uim)==Mr))
// throw std::runtime_error("SVD (ref version), wrong size of Uim");
// if(!(nrows(Vre)==Mc && ncols(Vre)==Mr))
// throw std::runtime_error("SVD (ref version), wrong size of Vre");
// if(!(nrows(Vim)==Mc && ncols(Vim)==Mr))
// throw std::runtime_error("SVD (ref version), wrong size of Vim");
// if(D.size()!=Mr)
// throw std::runtime_error("SVD (ref version), wrong size of D");
//#endif
//
// //Form 'density matrix' rho
// auto rhore = Mre*transpose(Mre) + Mim*transpose(Mim);
// auto rhoim = Mim*transpose(Mre) - Mre*transpose(Mim);
//
// //Diagonalize rho: evals are squares of singular vals
// diagHermitian(rhore,rhoim,Ure,Uim,D);
//
// for(auto& el : D)
// {
// if(el < 0) el = 0.;
// else el = std::sqrt(el);
// }
// size_t nlarge = 0;
// auto rthresh = D(0)*thresh;
// for(decltype(Mr) n = 0; n < Mr; ++n)
// {
// if(D(n) < rthresh)
// {
// nlarge = n;
// break;
// }
// }
//
// //Compute Mt*U = V*D
// Vre &= transpose(Mre)*Ure + transpose(Mim)*Uim;
// Vim &= transpose(Mre)*Uim - transpose(Mim)*Ure;
//
// for(decltype(nlarge) n = 0; n < nlarge; ++n)
// {
// column(Vre,n) /= D(n);
// column(Vim,n) /= D(n);
// }
// if(nlarge < Mr)
// {
// //Much more accurate than dividing
// //by smallest singular values
// auto Vcr = columns(Vre,nlarge,Mr);
// auto Vci = columns(Vim,nlarge,Mr);
// orthog(Vcr,Vci,2);
// }
//
// bool done = false;
// size_t start = 1;
// tie(done,start) = checkSVDDone(D,thresh);
// if(done) return;
//
// //
// //Recursively SVD part of B
// //for greater final accuracy
// //
// auto n = Mr-start;
//
// //{
// //println("Method 1");
// ////TEST VERSION - SLOW!
// //auto Tre = Mre*Vre - Mim*Vim;
// //auto Tim = Mre*Vim + Mim*Vre;
// //auto Bre = transpose(Ure)*Tre + transpose(Uim)*Tim;
// //auto Bim = transpose(Ure)*Tim - transpose(Uim)*Tre;
//
// //auto bre = Matrix{subMatrix(Bre,start,Mr,start,Mr)};
// //auto bim = Matrix{subMatrix(Bim,start,Mr,start,Mr)};
//
// //auto d = subVector(D,start,Mr);
// //Matrix ure(n,n),
// // uim(n,n),
// // vre(n,n),
// // vim(n,n);
// //SVDRef(bre,bim,ure,uim,d,vre,vim,thresh);
//
// //auto nure = columns(Ure,start,Mr);
// //auto nuim = columns(Uim,start,Mr);
// //auto tmpre = nure*ure - nuim*uim;
// //auto tmpim = nuim*ure + nure*uim;
// //nure &= tmpre;
// //nuim &= tmpim;
//
// //auto nvre = columns(Vre,start,Mr);
// //auto nvim = columns(Vim,start,Mr);
// //tmpre = nvre*vre - nvim*vim;
// //tmpim = nvim*vre + nvre*vim;
// //nvre &= tmpre;
// //nvim &= tmpim;
// //}
//
// //reuse storage of rho to hold mv=M*columns(V,start,Mr)
// auto mvre = move(rhore);
// auto mvim = move(rhoim);
// reduceCols(mvre,n);
// reduceCols(mvim,n);
//
// auto ure = columns(Ure,start,Mr);
// auto uim = columns(Uim,start,Mr);
// auto vre = columns(Vre,start,Mr);
// auto vim = columns(Vim,start,Mr);
//
// mvre = Mre*vre - Mim*vim;
// mvim = Mre*vim + Mim*vre;
//
// auto utre = transpose(ure);
// auto utim = transpose(uim);
//
// //b (=ut*M*v) should be close to diagonal
// //but may not be perfect - fix it up below
// auto bre = utre*mvre + utim*mvim;
// auto bim = utre*mvim - utim*mvre;
// auto d = subVector(D,start,Mr);
// Matrix bure(n,n),
// buim(n,n),
// bvre(n,n),
// bvim(n,n);
// SVDRef(bre,bim,bure,buim,d,bvre,bvim,thresh);
//
// auto Nure = ure*bure-uim*buim;
// auto Nuim = ure*buim+uim*bure;
// ure &= Nure;
// uim &= Nuim;
//
// auto Nvre = vre*bvre-vim*bvim;
// auto Nvim = vre*bvim+vim*bvre;
// vre &= Nvre;
// vim &= Nvim;
//
//#ifdef CHKSVD
// checksvd(M,U,D,V);
//#endif
//
// return;
// }
//
//void
//SVD(MatrixRefc const& Mre,
// MatrixRefc const& Mim,
// Matrix & Ure,
// Matrix & Uim,
// Vector & D,
// Matrix & Vre,
// Matrix & Vim,
// Real thresh)
// {
// auto Mr = nrows(Mre),
// Mc = ncols(Mim);
// auto nsv = std::min(Mr,Mc);
// resize(Ure,Mr,nsv);
// resize(Uim,Mr,nsv);
// resize(Vre,Mc,nsv);
// resize(Vim,Mc,nsv);
// resize(D,nsv);
// SVDRef(Mre,Mim,Ure,Uim,D,Vre,Vim,thresh);
// }
namespace exptH_detail {
int
expPade(MatRef<Real> const& F, int N, int ideg)
{
LAPACK_INT info = 0;
//
// Scaling: seek ns such that ||F/2^ns|| < 1/2
// and set scale = 1/2^ns
//
auto ns = dlange_wrapper('I',N,N,F.data());// infinite norm of the matrix to be exponentiated
#ifdef DEBUG
if(ns == 0) throw std::runtime_error("padeExp: null input matrix");
#endif
ns = std::max(0,(int)(std::log(ns)/log(2))+2);
Real scale = std::pow(2,-ns);
Real scale2 = scale*scale;
//
// Compute Pade coefficient
//
std::vector<Real> coef(ideg+1);
coef[0] = 1.0;
for(int k = 1; k <= ideg; ++k)
{
coef[k] = coef[k-1]*((double)(ideg+1-k)/(double)(k*(2*ideg+1-k)));
}
//
// H^2 = scale2*F*F
//
auto F2 = Mat<Real>(N,N);
gemm(F,F,makeRef(F2),scale2,0);
//
// Initialize P and Q
//
auto P = Mat<Real>(N,N);
auto Q = Mat<Real>(N,N);
for(auto j : range(N))
{
Q(j,j) = coef[ideg];
P(j,j) = coef[ideg-1];
}
//
// Horner evaluation of the irreducible fraction:
// Apply Horner rule
//
bool odd = true;
for(int k = ideg-1; k > 0; --k)
{
if(odd)
{
Q = Q*F2;
for(auto j : range(N))
{
Q(j,j) += coef[k-1];
}
}
else
{
P = P*F2;
for(auto j : range(N))
{
P(j,j) += coef[k-1];
}
}
odd = !odd;
}
//
// Horner evaluation of the irreducible fraction:
// Obtain (+/-)(I+2*(P\Q))
//
if(odd)
{
Q = scale*Q*F;
}
else
{
P = scale*P*F;
}
Q -= P;
info = dgesv_wrapper(N,N,Q.data(),P.data());
if(info != 0) return info;
P *= 2.0;
for(auto j : range(N))
{
P(j,j) += 1.0;
}
if(ns == 0 && odd)
{
P *= -1.0;
}
//
// Squaring: exp(F) = (exp(F))^(2^ns)
//
for(int k = 1; k <= ns; ++k)
{
P = P*P;
}
//auto pend = P.data()+P.size();
//auto f = Fdata;
//for(auto p = P.data(); p != pend; ++p,++f)
// {
// *f = *p;
// }
F &= P;//deep copy
return info;
}
int
expPade(MatRef<Cplx> const& F, int N, int ideg)
{
LAPACK_INT info = 0;
//
// Scaling: seek ns such that ||F/2^ns|| < 1/2
// and set scale = 1/2^ns
//
auto ns = zlange_wrapper('I',N,N,F.data());
#ifdef DEBUG
if(ns == 0) throw std::runtime_error("padeExp: null input matrix");
#endif
ns = std::max(0,(int)(std::log(ns)/log(2))+2);
Real scale = std::pow(2,-ns);
Real scale2 = scale*scale;
//
// Compute Pade coefficient
//
std::vector<Real> coef(ideg+1);
coef[0] = 1.0;
for(int k = 1; k <= ideg; ++k)
{
coef[k] = coef[k-1]*((double)(ideg+1-k)/(double)(k*(2*ideg+1-k)));
}
//
// H^2 = scale2*F*F
//
auto F2 = Mat<Cplx>(N,N);
gemm(F,F,makeRef(F2),scale2,0);
//
// Initialize P and Q
//
auto P = Mat<Cplx>(N,N);
auto Q = Mat<Cplx>(N,N);
for(auto j : range(N))
{
Q(j,j) = coef[ideg];
P(j,j) = coef[ideg-1];
}
//
// Horner evaluation of the irreducible fraction:
// Apply Horner rule
//
bool odd = true;
for(int k = ideg-1; k > 0; --k)
{
if(odd)
{
Q = Q*F2;
for(auto j : range(N))
{
Q(j,j) += coef[k-1];
}
}
else
{
P = P*F2;
for(auto j : range(N))
{
P(j,j) += coef[k-1];
}
}
odd = !odd;
}