-
Notifications
You must be signed in to change notification settings - Fork 10
/
tt.f90
1590 lines (1481 loc) · 47.7 KB
/
tt.f90
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
module tt_lib
use ptype_lib
use svd_lib
use mat_lib
use trans_lib
use time_lib
use say_lib
implicit none
integer,parameter :: tt_size=1023
double precision,parameter :: errval = -999.d0
type,public:: dtt
integer :: l=1
integer :: m=0
integer :: n(tt_size)=0
integer :: r(0:tt_size)=0
type(pointd3) :: u(tt_size)
!contains
! final :: dtt_dealloc
end type
type,public:: ztt
integer :: l=1
integer :: m=0
integer :: n(tt_size)
integer :: r(0:tt_size)
type(pointz3) :: u(tt_size)
!contains
! final :: ztt_dealloc
end type
type,public:: ttind
integer :: p(tt_size)=0
integer :: n(tt_size)=0
integer :: m=0
end type
interface nip
module procedure dtt_nip, ztt_nip
end interface
interface full
module procedure dtt_full, ztt_full
end interface
interface tijk
module procedure dtt_ijk,ztt_ijk
end interface
interface seti
module procedure dtt_seti,ztt_seti
end interface
interface sumall
module procedure dtt_sumall,ztt_sumall
end interface
interface numel
module procedure dtt_numel,ztt_numel
end interface
interface value
module procedure dtt_value,dtt_value0,ztt_value,ztt_value0
end interface
interface mem ! memory to keep all cores
module procedure dtt_mem,ztt_mem
end interface
!interface elem
! module procedure dtt_elem
!end interface
interface alloc
module procedure dtt_alloc,ztt_alloc
end interface
interface dealloc
module procedure dtt_dealloc,ztt_dealloc
end interface
interface ort
module procedure dtt_ort, ztt_ort
end interface
interface svd
module procedure dtt_svd,dtt_svd0, ztt_svd,ztt_svd0
end interface
interface say
module procedure dtt_say, ztt_say, ttind_say
end interface
interface sayfull
module procedure dtt_sayfull, ztt_sayfull
end interface
interface norm
module procedure dtt_norm,ztt_norm
end interface
interface rank
module procedure dtt_rank,ztt_rank
end interface
interface dot
module procedure dtt_dot, ztt_dot
end interface
interface copy
module procedure dtt_copy,ztt_copy
end interface
interface assignment (=)
module procedure dtt_assign, ztt_assign, ttind_assign
end interface
interface operator (*)
module procedure dttmul_dt, zttmul_zt
end interface
interface operator (.eq.)
module procedure ttind_eq
end interface
interface operator (.lt.)
module procedure ttind_lt
end interface
interface operator (.le.)
module procedure ttind_le
end interface
interface operator (.gt.)
module procedure ttind_gt
end interface
interface operator (.ge.)
module procedure ttind_ge
end interface
interface find
module procedure find_ttind
end interface
interface push
module procedure push_ttind
end interface
interface dble
module procedure dble_ttind
end interface
interface int
module procedure int_ttind
end interface
contains
! ORT
subroutine dtt_ort(arg)
![tt] ortogonalize from left
implicit none
type(dtt),intent(inout),target :: arg
character(len=*),parameter :: subnam='dtt_ort'
integer :: l,m,k,i,j,lwork,info,nn,rr,mn,mm,kk
integer,pointer :: r(:),n(:)
double precision,allocatable :: work(:),tau(:),mat(:),u(:)
double precision :: err,nrm
double precision,external :: dnrm2
!double precision :: t1,t2
l=arg%l; m=arg%m
if(m.lt.l)return
r=>arg%r; n=>arg%n
nn=maxval(n(l:m)); rr=maxval(r(l-1:m))
lwork=128*nn*rr
allocate(work(lwork),tau(nn*rr), mat(rr*nn*rr),u(rr*nn*rr), stat=info)
if(info.ne.0)then;write(*,*)subnam,': no memory';stop;endif
do k=l,m-1
mm=r(k-1)*n(k); nn=r(k); mn=min(mm,nn); kk=n(k+1)*r(k+1)
call dcopy(mm*nn, arg%u(k)%p,1,u,1)
!t1=timef()
call dgeqrf(mm,nn, u,mm,tau,work,lwork,info)
if(info.ne.0)then; write(*,*) subnam,': dgeqrf info: ',info; stop; end if
do j=1,nn
forall(i=1:min(j,mm)) mat(i+(j-1)*mn)=u(i+(j-1)*mm)
forall(i=min(j,mm)+1:mn) mat(i+(j-1)*mn)=0.d0
end do
call dorgqr(mm,mn,mn,u,mm,tau,work,lwork,info)
if(info.ne.0)then; write(*,*) subnam,': dorgqr info: ',info; stop; end if
call dcopy(mm*mn, u,1,arg%u(k)%p,1)
call dgemm('n','n',mn,kk,nn,1.d0,mat,mn,arg%u(k+1)%p,nn,0.d0,u,mn)
if(r(k).ne.mn)then
call dcopy(mm*mn, arg%u(k)%p,1,mat,1)
deallocate(arg%u(k)%p,arg%u(k+1)%p)
r(k)=mn
allocate(arg%u(k)%p(r(k-1),n(k),r(k)),arg%u(k+1)%p(r(k),n(k+1),r(k+1)))
call dcopy(mm*mn, mat,1,arg%u(k)%p,1)
end if
call dcopy(mn*kk, u,1,arg%u(k+1)%p,1)
end do
deallocate(work,tau,mat,u)
end subroutine
subroutine ztt_ort(arg)
![tt] ortogonalize from left
implicit none
type(ztt),intent(inout),target :: arg
character(len=*),parameter :: subnam='ztt_ort'
integer :: l,m,k,i,j,lwork,info,nn,rr,mn,mm,kk
integer,pointer :: r(:),n(:)
complex(8),allocatable :: work(:),tau(:),mat(:),u(:)
double precision :: err,nrm
double precision,external :: dznrm2
l=arg%l; m=arg%m
if(m.lt.l)return
r=>arg%r; n=>arg%n
nn=maxval(n(l:m)); rr=maxval(r(l-1:m))
lwork=128*nn*rr
allocate(work(lwork),tau(nn*rr), mat(rr*nn*rr),u(rr*nn*rr), stat=info)
if(info.ne.0)then;write(*,*)subnam,': no memory';stop;endif
do k=l,m-1
mm=r(k-1)*n(k); nn=r(k); mn=min(mm,nn); kk=n(k+1)*r(k+1)
call zcopy(mm*nn, arg%u(k)%p,1,u,1)
call zgeqrf(mm,nn, u,mm,tau,work,lwork,info)
if(info.ne.0)then; write(*,*) subnam,': zgeqrf info: ',info; stop; end if
do j=1,nn
forall(i=1:min(j,mm)) mat(i+(j-1)*mn)=u(i+(j-1)*mm)
forall(i=min(j,mm)+1:mn) mat(i+(j-1)*mn)=(0.d0,0.d0)
end do
call zungqr(mm,mn,mn,u,mm,tau,work,lwork,info)
if(info.ne.0)then; write(*,*) subnam,': zungqr info: ',info; stop; end if
!
nrm=dznrm2(mm*nn,arg%u(k)%p,1)
call zgemm('n','n',mm,nn,mn,(1.d0,0.d0),u,mm,mat,mn,(-1.d0,0.d0),arg%u(k)%p,mm)
err=dznrm2(mm*nn,arg%u(k)%p,1)
if(err.gt.1.d-10*nrm)then
write(*,*)subnam,': qr error: m,n: ',mm,nn
write(*,*)subnam,': err: ',err,' nrm ',nrm
stop
endif
!
call zcopy(mm*mn, u,1,arg%u(k)%p,1)
call zgemm('n','n',mn,kk,nn,(1.d0,0.d0),mat,mn,arg%u(k+1)%p,nn,(0.d0,0.d0),u,mn)
if(r(k).ne.mn)then
call zcopy(mm*mn, arg%u(k)%p,1,mat,1)
deallocate(arg%u(k)%p,arg%u(k+1)%p,stat=info)
if(info.ne.0)then;write(*,*)subnam,': cannot deallocate data';stop;endif
arg%r(k)=mn
allocate(arg%u(k)%p(r(k-1),n(k),r(k)),arg%u(k+1)%p(r(k),n(k+1),r(k+1)))
call zcopy(mm*mn, mat,1,arg%u(k)%p,1)
end if
call zcopy(mn*kk, u,1,arg%u(k+1)%p,1)
end do
deallocate(work,tau,mat,u)
end subroutine
! SVD
subroutine dtt_svd(arg,tol,rmax)
implicit none
type(dtt),intent(inout),target :: arg
double precision,intent(in) :: tol
character(len=*),parameter :: subnam='dtt_svd'
integer :: l,m,k,i,j,lwork,info,nn,rr,mn,mm,kk
integer,pointer :: r(:),n(:)
integer, optional :: rmax
double precision,allocatable :: work(:),s(:),mat(:),u(:)
double precision :: err,nrm
double precision,external :: dnrm2
l=arg%l; m=arg%m
if(m.le.l)return
r=>arg%r; n=>arg%n
nn=maxval(n(l:m)); rr=maxval(r(l-1:m))
lwork=128*nn*rr
allocate(work(lwork),s(nn*rr), mat(rr*nn*rr),u(rr*nn*rr), stat=info)
if(info.ne.0)then;write(*,*)subnam,': no memory';stop;endif
call dtt_ort(arg)
do k=m,l+1,-1
mm=r(k-1); nn=n(k)*r(k); mn=min(mm,nn); kk=r(k-2)*n(k-1)
call dgesvd('s','s',mm,nn,arg%u(k)%p,mm,s,mat,mm,u,mn,work,lwork,info)
if(info.ne.0)then; write(*,*)subnam,': dgesvd info: ',info; stop; end if
rr=chop(s(1:mn), tol/dsqrt(dble(m-l)))
if (present(rmax)) then
rr = min(rr, rmax)
end if
forall(i=1:mm,j=1:rr)mat((j-1)*mm+i)=s(j)*mat((j-1)*mm+i)
call d2submat(rr,nn,u,mn,arg%u(k)%p)
call dgemm('n','n',kk,rr,mm,1.d0,arg%u(k-1)%p,kk,mat,mm,0.d0,u,kk)
if(r(k-1).ne.rr)then
call dcopy(rr*nn, arg%u(k)%p,1,mat,1)
deallocate(arg%u(k-1)%p,arg%u(k)%p)
r(k-1)=rr
allocate(arg%u(k-1)%p(r(k-2),n(k-1),r(k-1)),arg%u(k)%p(r(k-1),n(k),r(k)))
call dcopy(rr*nn, mat,1,arg%u(k)%p,1)
end if
call dcopy(kk*rr,u,1,arg%u(k-1)%p,1)
end do
deallocate(work,s,mat,u)
end subroutine
subroutine ztt_svd(arg, tol, rmax)
implicit none
type(ztt),intent(inout),target :: arg
double precision,intent(in) :: tol
character(len=*),parameter :: subnam='ztt_svd'
integer :: l,m,k,i,j,lwork,info,nn,rr,mn,mm,kk
integer,pointer :: r(:),n(:)
integer, intent(in), optional :: rmax
double precision,allocatable :: s(:),rwork(:)
complex(8),allocatable :: work(:),mat(:),u(:)
double precision :: err,nrm
double precision,external :: dznrm2
l=arg%l; m=arg%m
if(m.le.l)return
r=>arg%r; n=>arg%n
nn=maxval(n(l:m)); rr=maxval(r(l-1:m))
lwork=128*nn*rr
allocate(work(lwork),rwork(8*nn*rr),s(nn*rr), mat(rr*nn*rr),u(rr*nn*rr), stat=info)
if(info.ne.0)then;write(*,*)subnam,': no memory';stop;endif
call ztt_ort(arg)
do k=m,l+1,-1
mm=r(k-1); nn=n(k)*r(k); mn=min(mm,nn); kk=r(k-2)*n(k-1)
call zgesvd('s','s',mm,nn,arg%u(k)%p,mm,s,mat,mm,u,mn,work,lwork,rwork,info)
if(info.ne.0)then; write(*,*)subnam,': dgesvd info: ',info; stop; end if
rr=chop(s(1:mn),tol/dsqrt(dble(m-l)))
if (present(rmax)) then
rr = min(rr, rmax)
end if
forall(i=1:mm,j=1:rr)mat((j-1)*mm+i)=s(j)*mat((j-1)*mm+i)
call z2submat(rr,nn,u,mn,arg%u(k)%p)
call zgemm('n','n',kk,rr,mm,(1.d0,0.d0),arg%u(k-1)%p,kk,mat,mm,(0.d0,0.d0),u,kk)
if(r(k-1).ne.rr)then
call zcopy(rr*nn, arg%u(k)%p,1,mat,1)
deallocate(arg%u(k-1)%p,arg%u(k)%p)
r(k-1)=rr
allocate(arg%u(k-1)%p(r(k-2),n(k-1),r(k-1)),arg%u(k)%p(r(k-1),n(k),r(k)))
call zcopy(rr*nn, mat,1,arg%u(k)%p,1)
end if
call zcopy(kk*rr,u,1,arg%u(k-1)%p,1)
end do
deallocate(work,rwork,s,mat,u)
end subroutine
subroutine dtt_svd0(n,a,tt,tol,rmax)
implicit none
integer,intent(in) :: n(:)
double precision,intent(in) :: a(*)
type(dtt),intent(inout),target :: tt
double precision,intent(in) :: tol
character(len=*),parameter :: subnam='dtt_svd0'
double precision,dimension(:),allocatable :: s,b,u,work
integer :: l,m,k,nn,mm,mn,info,i,lwork
integer, intent(in), optional :: rmax
integer,pointer :: r(:)
double precision,external :: dnrm2
l=tt%l; m=l+size(n)-1;tt%m=m;tt%n(l:m)=n; r=>tt%r; r(l-1)=1;r(m)=1
nn=product(n)
lwork=64*nn
if(.not.lwork.gt.0)lwork=16*nn
if(.not.lwork.gt.0)lwork=4*nn
if(.not.lwork.gt.0)then;write(*,*)subnam,': nn, lwork: ',nn,lwork;stop;endif
allocate(b(nn),u(nn),s(nn),work(lwork),stat=info)
if(info.ne.0)then;write(*,*)subnam,': not enough memory';stop;endif
if(dnrm2(nn,a,1).eq.0.d0)then;write(*,*)subnam,': zero norm input';tt%r=0;call dealloc(tt);return;endif
call dcopy(nn,a,1,b,1)
do k=m,l+1,-1
mm=r(l-1)*product(tt%n(l:k-1)); nn=tt%n(k)*r(k); mn=min(mm,nn)
call dgesvd('o','s',mm,nn,b,mm,s,b,1,u,mn,work,lwork,info)
if(info.ne.0)then;write(*,*)subnam,': info: ',info;stop;endif
!r(k-1)=chop(s(1:mn),tol/dsqrt(dble(m-l+1)))
if(s(1).ne.0.d0)then
r(k-1)=chop(s(1:mn), tol/dsqrt(dble(m-l)))
else
r(k-1)=0
endif
if (present(rmax)) then
r(k-1) = min(r(k-1), rmax)
end if
do i=1,r(k-1); call dscal(mm,s(i),b(1+(i-1)*mm),1);enddo
if(associated(tt%u(k)%p))deallocate(tt%u(k)%p)
allocate(tt%u(k)%p(r(k-1),tt%n(k),r(k)))
call d2submat(r(k-1),nn,u,mn,tt%u(k)%p)
end do
if(associated(tt%u(l)%p))deallocate(tt%u(l)%p)
allocate(tt%u(l)%p(r(l-1),tt%n(l),r(l)))
call dcopy(r(l-1)*tt%n(l)*r(l),b,1,tt%u(l)%p,1)
deallocate(work,b,u,s)
end subroutine
subroutine ztt_svd0(n,a,tt,tol,rmax)
implicit none
integer,intent(in) :: n(:)
complex(8),intent(in) :: a(*)
type(ztt),intent(inout),target :: tt
double precision,intent(in) :: tol
character(len=*),parameter :: subnam='ztt_svd0'
double precision,dimension(:),allocatable :: s,rwork
complex(8),dimension(:),allocatable :: b,u,work
integer :: l,m,k,nn,mm,mn,info,i,lwork
integer, intent(in), optional :: rmax
integer,pointer :: r(:)
double precision,external :: dznrm2
l=tt%l; m=l+size(n)-1;tt%m=m;tt%n(l:m)=n; r=>tt%r; r(l-1)=1;r(m)=1
nn=product(n)
lwork=64*nn
if(.not.lwork.gt.0)lwork=16*nn
if(.not.lwork.gt.0)then;write(*,*)subnam,': nn, lwork: ',nn,lwork;stop;endif
allocate(b(nn),u(nn),s(nn),work(lwork),rwork(8*nn),stat=info)
if(info.ne.0)then;write(*,*)subnam,': not enough memory';stop;endif
if(dznrm2(nn,a,1).eq.0.d0)then;write(*,*)subnam,': zero norm input';tt%r=0;call dealloc(tt);return;endif
call zcopy(nn,a,1,b,1)
do k=m,l+1,-1
mm=r(l-1)*product(tt%n(l:k-1)); nn=tt%n(k)*r(k); mn=min(mm,nn)
call zgesvd('o','s',mm,nn,b,mm,s,b,1,u,mn,work,lwork,rwork,info)
if(info.ne.0)then;write(*,*)subnam,': info: ',info;stop;endif
if(s(1).ne.0.d0)then
r(k-1)=chop(s(1:mn), tol/dsqrt(dble(m-l)))
else
r(k-1)=0
endif
if (present(rmax)) then
r(k-1) = min(r(k-1), rmax)
end if
do i=1,r(k-1); call zdscal(mm,s(i),b(1+(i-1)*mm),1);enddo
if(associated(tt%u(k)%p))deallocate(tt%u(k)%p)
allocate(tt%u(k)%p(r(k-1),tt%n(k),r(k)))
call z2submat(r(k-1),nn,u,mn,tt%u(k)%p)
end do
if(associated(tt%u(l)%p))deallocate(tt%u(l)%p)
allocate(tt%u(l)%p(r(l-1),tt%n(l),r(l)))
call zcopy(r(l-1)*tt%n(l)*r(l),b,1,tt%u(l)%p,1)
deallocate(work,b,u,rwork,s)
end subroutine
! GROUP
subroutine dtt_group(arg,grp,side)
!grp=[grp arg]
implicit none
type(dtt),intent(in),target :: arg
type(dtt),intent(inout),target :: grp
integer,intent(in),optional :: side
character(len=*),parameter :: subnam='dtt_group'
type(dtt) :: z
integer,pointer :: r(:),q(:),n(:)
integer :: l,m,k,sid,mm,ll,i,j
if(arg%l.ne.grp%l .or. arg%m.ne.grp%m)then;write(*,*)subnam,': length mismatch';stop;endif
l=arg%l; m=arg%m
if(.not.all(arg%n(l:m)==grp%n(l:m)))then;write(*,*)subnam,': size mismatch';stop;endif
n=>arg%n;r=>grp%r;q=>arg%r
if(present(side))then;sid=side;else;if(r(l-1).ge.r(m))then;sid=0;else;sid=1;endif;endif
z%l=l; z%m=m; z%n=n; z%r=0
select case(sid)
case(0)
if(r(m).ne.q(m))then;write(*,*)subnam,': right border ranks mismatch:',r(m),q(m);stop;endif
z%r(l-1:m-1)=r(l-1:m-1)+q(l-1:m-1); z%r(m)=r(m)
case(1)
if(r(l-1).ne.q(l-1))then;write(*,*)subnam,': left border ranks mismatch:',r(l-1),q(l-1);stop;endif
z%r(l-1)=r(l-1); z%r(l:m)=r(l:m)+q(l:m)
case default
write(*,*)subnam,': illegal side:',sid; stop
end select
call alloc(z)
if(sid.eq.1)then
forall(j=1:r(l)) z%u(l)%p(:,:, j)=grp%u(l)%p(:,:,j)
forall(j=1:q(l)) z%u(l)%p(:,:,r(l)+j)=arg%u(l)%p(:,:,j)
ll=l+1;mm=m
endif
if(sid.eq.0)then
forall(i=1:r(m-1)) z%u(m)%p( i,:,:)=grp%u(m)%p(i,:,:)
forall(i=1:q(m-1)) z%u(m)%p(r(m-1)+i,:,:)=arg%u(m)%p(i,:,:)
ll=l;mm=m-1
endif
do k=ll,mm
z%u(k)%p=0.d0
forall(i=1:r(k-1),j=1:r(k)) z%u(k)%p( i,:, j)=grp%u(k)%p(i,:,j)
forall(i=1:q(k-1),j=1:q(k)) z%u(k)%p(r(k-1)+i,:,r(k)+j)=arg%u(k)%p(i,:,j)
end do
grp=z
call dealloc(z)
end subroutine
subroutine ztt_group(arg,grp,side)
!grp=[grp arg]
implicit none
type(ztt),intent(in),target :: arg
type(ztt),intent(inout),target :: grp
integer,intent(in),optional :: side
character(len=*),parameter :: subnam='ztt_group'
type(ztt) :: z
integer,pointer :: r(:),q(:),n(:)
integer :: l,m,k,sid,mm,ll,i,j
if(arg%l.ne.grp%l .or. arg%m.ne.grp%m)then;write(*,*)subnam,': length mismatch';stop;endif
l=arg%l; m=arg%m
if(.not.all(arg%n(l:m)==grp%n(l:m)))then;write(*,*)subnam,': size mismatch';stop;endif
n=>arg%n;r=>grp%r;q=>arg%r
if(present(side))then;sid=side;else;if(r(l-1).ge.r(m))then;sid=0;else;sid=1;endif;endif
z%l=l; z%m=m; z%n=n; z%r=0
select case(sid)
case(0)
if(r(m).ne.q(m))then;write(*,*)subnam,': right border ranks mismatch:',r(m),q(m);stop;endif
z%r(l-1:m-1)=r(l-1:m-1)+q(l-1:m-1); z%r(m)=r(m)
case(1)
if(r(l-1).ne.q(l-1))then;write(*,*)subnam,': left border ranks mismatch:',r(l-1),q(l-1);stop;endif
z%r(l-1)=r(l-1); z%r(l:m)=r(l:m)+q(l:m)
case default
write(*,*)subnam,': illegal side:',sid; stop
end select
call alloc(z)
if(sid.eq.1)then
forall(j=1:r(l)) z%u(l)%p(:,:, j)=grp%u(l)%p(:,:,j)
forall(j=1:q(l)) z%u(l)%p(:,:,r(l)+j)=arg%u(l)%p(:,:,j)
ll=l+1;mm=m
endif
if(sid.eq.0)then
forall(i=1:r(m-1)) z%u(m)%p( i,:,:)=grp%u(m)%p(i,:,:)
forall(i=1:q(m-1)) z%u(m)%p(r(m-1)+i,:,:)=arg%u(m)%p(i,:,:)
ll=l;mm=m-1
endif
do k=ll,mm
z%u(k)%p=0.d0
forall(i=1:r(k-1),j=1:r(k)) z%u(k)%p( i,:, j)=grp%u(k)%p(i,:,j)
forall(i=1:q(k-1),j=1:q(k)) z%u(k)%p(r(k-1)+i,:,r(k)+j)=arg%u(k)%p(i,:,j)
end do
grp=z
call dealloc(z)
end subroutine
! ELEM TIJK
pure double precision function dtt_ijk(arg,ind) result (a)
implicit none
type(dtt),intent(in) :: arg
integer,intent(in) :: ind(:)
character(len=*),parameter :: subnam='dtt_ijk'
integer :: info,i,l,m,n(tt_size),r(0:tt_size)
double precision,pointer :: x(:,:),y(:,:),z(:,:)
l=arg%l;m=arg%m;n=arg%n;r=arg%r
if(any(ind(1:m-l+1)<=0).or.any(ind(1:m-l+1)>n(l:m)))then;a=-3.d0;return;endif
if(r(l-1).ne.1 .or. r(m).ne.1)then;a=-4.d0;return;endif
allocate(x(r(m-1),r(m)),stat=info)
if(info.ne.0)then;a=-1.d0;return;endif
x=arg%u(m)%p(:,ind(m-l+1),:)
do i=m-1,l,-1
allocate(y(r(i-1),r(i)),z(r(i-1),r(m)),stat=info)
if(info.ne.0)then;a=-2.d0;return;endif
y=arg%u(i)%p(:,ind(i-l+1),:)
z=matmul(y,x)
deallocate(x,y); x=>z; nullify(z)
end do
a=x(1,1)
deallocate(x)
end function
pure complex(8) function ztt_ijk(arg,ind) result (a)
implicit none
type(ztt),intent(in) :: arg
integer,intent(in) :: ind(:)
character(len=*),parameter :: subnam='ztt_ijk'
integer :: info,i,l,m,n(tt_size),r(0:tt_size)
complex(8),pointer :: x(:,:),y(:,:),z(:,:)
complex(8),parameter :: one=(1.d0,0.d0),zero=(0.d0,0.d0),im1=(0.d0,1.d0)
l=arg%l;m=arg%m;n=arg%n;r=arg%r
if(any(ind(1:m-l+1)<=0).or.any(ind(1:m-l+1)>n(l:m)))then;a=-3*one;return;endif
if(r(l-1).ne.1 .or. r(m).ne.1)then;a=-4*one;return;endif
allocate(x(r(m-1),r(m)),stat=info)
if(info.ne.0)then;a=-one;return;endif
x=arg%u(m)%p(:,ind(m-l+1),:)
do i=m-1,l,-1
allocate(y(r(i-1),r(i)),z(r(i-1),r(m)),stat=info)
if(info.ne.0)then;a=-2*one;return;endif
y=arg%u(i)%p(:,ind(i-l+1),:)
z=matmul(y,x)
deallocate(x,y); x=>z; nullify(z)
end do
a=x(1,1)
deallocate(x)
end function
subroutine dtt_elem(arg,ind,a)
implicit none
type(dtt),intent(in):: arg
integer,intent(in) :: ind(:)
double precision,intent(out) :: a(*)
character(len=*),parameter :: subnam='dtt_elem'
integer :: info,i,l,m,n(tt_size),r(0:tt_size)
double precision,pointer :: x(:,:),y(:,:),z(:,:)
l=arg%l;m=arg%m;n=arg%n;r=arg%r
if(any(ind(1:m-l+1)<=0).or.any(ind(1:m-l+1)>n(l:m)))then;write(*,*)subnam,': wrong index: ',ind;stop;endif
allocate(x(r(l-1),r(l)),stat=info)
if(info.ne.0)then;write(*,*)subnam,': allocation error: ',info;stop;endif
x=arg%u(l)%p(:,ind(1),:)
do i=l+1,m
allocate(y(r(i-1),r(i)),z(r(l-1),r(i)),stat=info)
if(info.ne.0)then;write(*,*)subnam,': allocation error: ',info;stop;endif
y=arg%u(i)%p(:,ind(i-l+1),:)
z=matmul(x,y)
deallocate(x,y); x=>z; nullify(z)
end do
call dcopy(r(l-1)*r(m),x,1,a,1)
deallocate(x)
end subroutine
! VAL
double precision function dtt_value(arg,x) result (val)
implicit none
type(dtt),intent(in) :: arg
double precision,intent(in) :: x(:)
integer :: l,m,r(0:tt_size),n(tt_size), id,dd,i,j,pos,mm,ind(tt_size)=0
double precision :: xx
val=0.d0
l=arg%l; m=arg%m; r=arg%r; n=arg%n; dd=size(x)
if(l.gt.m)return
mm=(m-l+1)/dd
do id=1,dd
xx=x(id)
if(xx.lt.0.d0)return
if(xx.gt.1.d0)xx=xx-int(xx)
do j=1,mm
pos=l+(id-1)*mm+mm-j
i=int(n(pos)*xx)
if(i.eq.n(pos))i=n(pos)-1
ind(pos-l+1)=i+1
xx=xx*n(pos)-i
end do
end do
val=dtt_ijk(arg,ind)
!write(*,'(3f20.12,1x,e20.12)') x,val
!write(*,'(127i1)')(mod(i,10),i=1,127)
!write(*,'(127i1)')ind
end function
complex(8) function ztt_value(arg,x) result (val)
implicit none
type(ztt),intent(in) :: arg
double precision,intent(in) :: x(:)
integer :: l,m,r(0:tt_size),n(tt_size), id,dd,i,j,pos,mm,ind(tt_size)=0
double precision :: xx
val=0.d0
l=arg%l; m=arg%m; r=arg%r; n=arg%n; dd=size(x)
if(l.gt.m)return
mm=(m-l+1)/dd
do id=1,dd
xx=x(id)
if(xx.lt.0.d0)return
if(xx.gt.1.d0)xx=xx-int(xx)
do j=1,mm
pos=l+(id-1)*mm+mm-j
i=int(n(pos)*xx)
if(i.eq.n(pos))i=n(pos)-1
ind(pos-l+1)=i+1
xx=xx*n(pos)-i
end do
end do
val=ztt_ijk(arg,ind)
end function
double precision function dtt_value0(arg,x) result (val)
implicit none
type(dtt),intent(in) :: arg
double precision,intent(in) :: x
double precision :: xx(1)
xx=x; val=dtt_value(arg,xx)
end function
complex(8) function ztt_value0(arg,x) result (val)
implicit none
type(ztt),intent(in) :: arg
double precision,intent(in) :: x
double precision :: xx(1)
xx=x; val=ztt_value(arg,xx)
end function
! SETI
subroutine dtt_seti(arg,pos,val)
implicit none
type(dtt),intent(inout) :: arg
integer,intent(in) :: pos,val
character(len=*),parameter :: subnam='dtt_seti'
integer :: l,m,n,p,q,i,j
double precision,allocatable :: a(:,:)
l=arg%l; m=arg%m
if(.not.(l.le.pos .and. pos.le.m))then;write(*,*)subnam,': pos not between l and m: ',pos,l,m;return;endif
n=arg%n(pos); p=arg%r(pos-1);q=arg%r(pos)
if(.not.(1.le.val .and. val.le.n))then;write(*,*)subnam,': val not between 1 and n: ',val,n;return;endif
allocate(a(p,q))
forall(i=1:p,j=1:q)a(i,j)=arg%u(pos)%p(i,val,j)
deallocate(arg%u(pos)%p)
allocate(arg%u(pos)%p(p,1,q))
call dcopy(p*q,a,1,arg%u(pos)%p,1)
deallocate(a)
arg%n(pos)=1
end subroutine
subroutine ztt_seti(arg,pos,val)
implicit none
type(ztt),intent(inout) :: arg
integer,intent(in) :: pos,val
character(len=*),parameter :: subnam='ztt_seti'
integer :: l,m,n,p,q,i,j
complex(8),allocatable :: a(:,:)
l=arg%l; m=arg%m
if(.not.(l.le.pos .and. pos.le.m))then;write(*,*)subnam,': pos not between l and m: ',pos,l,m;return;endif
n=arg%n(pos); p=arg%r(pos-1);q=arg%r(pos)
if(.not.(1.le.val .and. val.le.n))then;write(*,*)subnam,': val not between 1 and n: ',val,n;return;endif
allocate(a(p,q))
forall(i=1:p,j=1:q)a(i,j)=arg%u(pos)%p(i,val,j)
deallocate(arg%u(pos)%p)
allocate(arg%u(pos)%p(p,1,q))
call zcopy(p*q,a,1,arg%u(pos)%p,1)
deallocate(a)
arg%n(pos)=1
end subroutine
! SUMALL
double precision function dtt_sumall(arg) result(val)
implicit none
type(dtt),intent(in):: arg
character(len=*),parameter :: subnam='dtt_sumall'
integer :: info,i,j,p,q,l,m,n(tt_size),r(0:tt_size)
double precision,pointer :: x(:,:),y(:,:),z(:,:)
l=arg%l;m=arg%m;n=arg%n;r=arg%r; val=0.d0
if(r(l-1).gt.1 .or. r(m).gt.1) then; write(*,*)subnam,': matrix-valued output does not fit!';return;endif
allocate(x(r(l-1),r(l)),stat=info)
if(info.ne.0)then;write(*,*)subnam,': allocation error: ',info;stop;endif
forall(i=1:r(l-1),j=1:r(l))x(i,j)=0.d0
do p=1,n(l); forall(i=1:r(l-1),j=1:r(l))x(i,j)=x(i,j)+arg%u(l)%p(i,p,j); enddo
do q=l+1,m
allocate(y(r(q-1),r(q)),z(r(l-1),r(q)),stat=info)
if(info.ne.0)then;write(*,*)subnam,': allocation error: ',info;stop;endif
forall(i=1:r(q-1),j=1:r(q))y(i,j)=0.d0
do p=1,n(q); forall(i=1:r(q-1),j=1:r(q))y(i,j)=y(i,j)+arg%u(q)%p(i,p,j); enddo
z=matmul(x,y)
deallocate(x,y); x=>z; nullify(z)
end do
val=x(1,1)
deallocate(x)
end function
complex(8) function ztt_sumall(arg) result(val)
implicit none
type(ztt),intent(in):: arg
character(len=*),parameter :: subnam='ztt_sumall'
integer :: info,i,j,p,q,l,m,n(tt_size),r(0:tt_size)
complex(8),pointer :: x(:,:),y(:,:),z(:,:)
l=arg%l;m=arg%m;n=arg%n;r=arg%r; val=(0.d0,0.d0)
if(r(l-1).gt.1 .or. r(m).gt.1) then; write(*,*)subnam,': matrix-valued output does not fit!';return;endif
allocate(x(r(l-1),r(l)),stat=info)
if(info.ne.0)then;write(*,*)subnam,': allocation error: ',info;stop;endif
forall(i=1:r(l-1),j=1:r(l))x(i,j)=(0.d0,0.d0)
do p=1,n(l); forall(i=1:r(l-1),j=1:r(l))x(i,j)=x(i,j)+arg%u(l)%p(i,p,j); enddo
do q=l+1,m
allocate(y(r(q-1),r(q)),z(r(l-1),r(q)),stat=info)
if(info.ne.0)then;write(*,*)subnam,': allocation error: ',info;stop;endif
forall(i=1:r(q-1),j=1:r(q))y(i,j)=(0.d0,0.d0)
do p=1,n(q); forall(i=1:r(q-1),j=1:r(q))y(i,j)=y(i,j)+arg%u(q)%p(i,p,j); enddo
z=matmul(x,y)
deallocate(x,y); x=>z; nullify(z)
end do
val=x(1,1)
end function
! NUMEL
double precision function dtt_numel(arg) result (s)
implicit none
type(dtt),intent(in) :: arg
integer :: l,m,i
s=0.d0; l=arg%l; m=arg%m; if(l.gt.m)return
s=1.d0; do i=l,m;s=s*arg%n(i); enddo
return
end function
double precision function ztt_numel(arg) result (s)
implicit none
type(ztt),intent(in) :: arg
integer :: l,m,i
s=0.d0; l=arg%l; m=arg%m; if(l.gt.m)return
s=1.d0; do i=l,m;s=s*arg%n(i); enddo
return
end function
! NIP (pack)
subroutine dtt_nip(arg,ind)
implicit none
type(dtt),intent(inout),target :: arg
integer,intent(in),optional :: ind(:)
character(len=*),parameter :: subnam='dtt_nip'
integer :: l,m,p,k,maxr,maxn,info
integer,pointer :: r(:),n(:)
double precision,allocatable :: tmp(:)
l=arg%l; m=arg%m; r=>arg%r; n=>arg%n
maxn=maxval(n(l:m)); maxr=maxval(r(l-1:m))
allocate(tmp(maxr*maxn*maxr),stat=info)
if(info.ne.0)then;write(*,*)subnam,': cannot allocate tmp: ',info;stop;endif
if(present(ind))then
if(any(ind(1:m-l+1)<0) .or. any(ind(1:m-l+1)>n(l:m)))then
write(*,*)subnam,': illegal elements in ind'
write(*,*)ind(1:m-l+1)
call say(arg)
stop
end if
do p=l,m
if(ind(p-l+1).ne.0)then
do k=1,r(p)
call dcopy(r(p-1),arg%u(p)%p(1,ind(p-l+1),k),1,tmp(1+(k-1)*r(p-1)),1)
end do
deallocate(arg%u(p)%p)
allocate(arg%u(p)%p(r(p-1),1,r(p)))
call dcopy(r(p-1)*r(p),tmp,1,arg%u(p)%p,1)
n(p)=1
end if
end do
end if
do p=m,l+1,-1
if(n(p).eq.1 .or. n(p-1).eq.1)then
call dgemm('n','n',r(p-2)*n(p-1),n(p)*r(p),r(p-1),1.d0,arg%u(p-1)%p,r(p-2)*n(p-1),arg%u(p)%p,r(p-1),0.d0,tmp,r(p-2)*n(p-1))
deallocate(arg%u(p-1)%p, arg%u(p)%p)
allocate(arg%u(p-1)%p(r(p-2),n(p-1)*n(p),r(p)),stat=info)
if(info.ne.0)then;write(*,*)subnam,': cannot allocate carriage: ',p,info;stop;endif
call dcopy(r(p-2)*n(p-1)*n(p)*r(p),tmp,1,arg%u(p-1)%p,1)
n(p-1)=n(p-1)*n(p);r(p-1)=r(p); n(p)=0;r(p)=0
end if
end do
deallocate(tmp)
k=l
do p=l,m
if(n(p).gt.0)then
if(p.ne.k)then
if(associated(arg%u(k)%p))then;write(*,*)subnam,': position is associated, it shouldnot happen: ',p,k;stop;endif
allocate(arg%u(k)%p(r(k-1),n(p),r(p)))
if( size(arg%u(p)%p,1).ne.r(k-1) .or. size(arg%u(p)%p,2).ne.n(p) .or. size(arg%u(p)%p,3).ne.r(p) )then
write(*,*)subnam,': size mismatch! '
write(*,*)size(arg%u(p)%p,1),size(arg%u(p)%p,2),size(arg%u(p)%p,3)
write(*,*)r(k-1),n(p),r(p)
stop
end if
call dcopy(r(k-1)*n(p)*r(p), arg%u(p)%p,1,arg%u(k)%p,1)
deallocate(arg%u(p)%p)
n(k)=n(p);r(k)=r(p);n(p)=0;r(p)=0
end if
k=k+1
end if
end do
arg%m=k-1
end subroutine
subroutine ztt_nip(arg,ind)
implicit none
type(ztt),intent(inout),target :: arg
integer,intent(in),optional :: ind(:)
character(len=*),parameter :: subnam='ztt_nip'
complex(8),parameter :: zero=(0.d0,0.d0),one=(1.d0,0.d0)
integer :: l,m,p,k,maxr,maxn,info
integer,pointer :: r(:),n(:)
complex(8),allocatable :: tmp(:)
l=arg%l; m=arg%m; r=>arg%r; n=>arg%n
maxn=maxval(n(l:m)); maxr=maxval(r(l-1:m))
allocate(tmp(maxr*maxn*maxr),stat=info)
if(info.ne.0)then;write(*,*)subnam,': cannot allocate tmp: ',info;stop;endif
if(present(ind))then
if(any(ind(1:m-l+1)<0) .or. any(ind(1:m-l+1)>n(l:m)))then
write(*,*)subnam,': illegal elements in ind'
write(*,*)ind(1:m-l+1)
call say(arg)
stop
end if
do p=l,m
if(ind(p-l+1).ne.0)then
do k=1,r(p)
call zcopy(r(p-1),arg%u(p)%p(1,ind(p-l+1),k),1,tmp(1+(k-1)*r(p-1)),1)
end do
deallocate(arg%u(p)%p)
allocate(arg%u(p)%p(r(p-1),1,r(p)))
call zcopy(r(p-1)*r(p),tmp,1,arg%u(p)%p,1)
n(p)=1
end if
end do
end if
do p=m,l+1,-1
if(n(p).eq.1 .or. n(p-1).eq.1)then
call zgemm('n','n',r(p-2)*n(p-1),n(p)*r(p),r(p-1),one,arg%u(p-1)%p,r(p-2)*n(p-1),arg%u(p)%p,r(p-1),zero,tmp,r(p-2)*n(p-1))
deallocate(arg%u(p-1)%p, arg%u(p)%p)
allocate(arg%u(p-1)%p(r(p-2),n(p-1)*n(p),r(p)),stat=info)
if(info.ne.0)then;write(*,*)subnam,': cannot allocate carriage: ',p,info;stop;endif
call zcopy(r(p-2)*n(p-1)*n(p)*r(p),tmp,1,arg%u(p-1)%p,1)
n(p-1)=n(p-1)*n(p);r(p-1)=r(p); n(p)=0;r(p)=0
end if
end do
deallocate(tmp)
k=l
do p=l,m
if(n(p).gt.0)then
if(p.ne.k)then
if(associated(arg%u(k)%p))then;write(*,*)subnam,': position is associated, it shouldnot happen: ',p,k;stop;endif
allocate(arg%u(k)%p(r(k-1),n(p),r(p)))
if( size(arg%u(p)%p,1).ne.r(k-1) .or. size(arg%u(p)%p,2).ne.n(p) .or. size(arg%u(p)%p,3).ne.r(p) )then
write(*,*)subnam,': size mismatch! '
write(*,*)size(arg%u(p)%p,1),size(arg%u(p)%p,2),size(arg%u(p)%p,3)
write(*,*)r(k-1),n(p),r(p)
stop
end if
call zcopy(r(k-1)*n(p)*r(p), arg%u(p)%p,1,arg%u(k)%p,1)
deallocate(arg%u(p)%p)
n(k)=n(p);r(k)=r(p);n(p)=0;r(p)=0
end if
k=k+1
end if
end do
arg%m=k-1
end subroutine
! FULL
subroutine dtt_full(arg,a,alpha,beta,part,ind)
! A = [beta]*A + [alpha]*FULL(TT), TT = arg([l:m]), l,m=[part]
! A size r(l-1) *n(l)*...*n(m)* r(m)
implicit none
type(dtt),intent(in),target :: arg
double precision,intent(inout) :: a(*)
double precision,intent(in),optional :: alpha,beta
integer,intent(in),optional :: part(2),ind(:)
character(len=*),parameter :: subnam='dtt_full'
double precision :: alp
type(pointd) :: p(0:1)
integer,pointer :: r(:),n(:)
integer :: l,m,na,nb,mem,rr,info,i,j,pp
integer,allocatable :: ii(:),nn(:)
double precision,allocatable :: q(:)
if(present(alpha))then;alp=alpha;else;alp=1.d0;endif
if(present(part))then;l=part(1);m=part(2);else;l=arg%l;m=arg%m;endif
r=>arg%r; n=>arg%n
if(l.gt.m)then
write(*,*)subnam,': empty input';
do i=1,r(l-1) * product(nn(l:m)) * r(m); a(i)=0.d0; enddo
return
end if
allocate(ii(l:m),nn(l:m)); ii=0; nn(l:m)=arg%n(l:m)
if(present(ind))then
do i=l,m
ii(i)=ind(i-l+1)
if(ii(i).lt.0 .or. ii(i).gt.n(i))then;write(*,*)subnam,': invalid ind:',ind(1:m-l+1);stop;endif
if(ii(i).ne.0)nn(i)=1
end do
end if
na=r(l-1) * product(nn(l:m)) * r(m)
mem=na; rr=1
do i=l,m
if(.not.(r(l-1)*product(nn(l:i))*r(i).gt.0))then;write(*,*)subnam,': oversized mem';stop;endif
mem=max(mem, r(l-1)*product(nn(l:i))*r(i) )
rr=max(rr,r(i-1)*r(i))
end do
allocate(p(0)%p(mem),p(1)%p(mem),q(rr),stat=info)
if(info.ne.0)then;write(*,*)subnam,': not enough memory';stop;endif
if(ii(l).eq.0)then
call dcopy(r(l-1)*n(l)*r(l),arg%u(l)%p,1,p(0)%p,1)
else
do j=1,r(l);call dcopy(r(l-1),arg%u(l)%p(1,ii(l),j),1,p(0)%p(1+(j-1)*r(l-1)),1); enddo
end if
pp=0
do i=l+1,m
nb=r(l-1) * product(nn(l:i-1))
if(ii(i).eq.0)then
if(nb*n(i)*r(i).gt.mem)then;write(*,*)subnam,': nb-by-n-by-r > mem: ',nb,n(i),r(i),mem;stop;endif
call dgemm('n','n',nb,n(i)*r(i),r(i-1),1.d0,p(pp)%p,nb,arg%u(i)%p,r(i-1),0.d0,p(1-pp)%p,nb)
else
if(nb*r(i).gt.mem)then;write(*,*)subnam,': nb-by-r > mem: ',nb,r(i),mem;stop;endif
do j=1,r(i);call dcopy(r(i-1),arg%u(i)%p(1,ii(i),j),1,q(1+(j-1)*r(i-1)),1);enddo
call dgemm('n','n',nb,r(i),r(i-1),1.d0,p(pp)%p,nb,q,r(i-1),0.d0,p(1-pp)%p,nb)
end if
pp=1-pp
end do
if(present(beta))then
call dscal(na,beta,a,1)
call daxpy(na,alp,p(pp)%p,1,a,1)
else
call dcopy(na,p(pp)%p,1,a,1)
endif
deallocate(p(0)%p,p(1)%p,q,ii,nn)
end subroutine