-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpokit.f
4635 lines (4407 loc) · 157 KB
/
expokit.f
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
*----------------------------------------------------------------------|
subroutine DMEXPV( n, m, t, v, w, tol, anorm,
. wsp,lwsp, iwsp,liwsp, matvec, itrace,iflag )
implicit none
integer n, m, lwsp, liwsp, itrace, iflag, iwsp(liwsp)
double precision t, tol, anorm, v(n), w(n), wsp(lwsp)
external matvec
*-----Purpose----------------------------------------------------------|
*
*--- DMEXPV computes w = exp(t*A)*v - Customised for MARKOV CHAINS.
*
* It does not compute the matrix exponential in isolation but
* instead, it computes directly the action of the exponential
* operator on the operand vector. This way of doing so allows
* for addressing large sparse problems.
*
* The method used is based on Krylov subspace projection
* techniques and the matrix under consideration interacts only
* via the external routine `matvec' performing the matrix-vector
* product (matrix-free method).
*
* This is a customised version for Markov Chains. This means that a
* check is done within this code to ensure that the resulting vector
* w is a probability vector, i.e., w must have all its components
* in [0,1], with sum equal to 1. This check is done at some expense
* and the user may try DGEXPV which is cheaper since it ignores
* probability constraints.
*
* IMPORTANT: The check assumes that the transition rate matrix Q
* satisfies Qe = 0, where e=(1,...,1)'. Don't use DMEXPV
* if this condition does not hold. Use DGEXPV instead.
* DMEXPV/DGEXPV require the matrix-vector product
* y = A*x = Q'*x, i.e, the TRANSPOSE of Q times a vector.
* Failure to remember this leads to wrong results.
*
*-----Arguments--------------------------------------------------------|
*
* n : (input) order of the principal matrix A.
*
* m : (input) maximum size for the Krylov basis.
*
* t : (input) time at wich the solution is needed (can be < 0).
*
* v(n) : (input) given operand vector.
*
* w(n) : (output) computed approximation of exp(t*A)*v.
*
* tol : (input/output) the requested acurracy tolerance on w.
* If on input tol=0.0d0 or tol is too small (tol.le.eps)
* the internal value sqrt(eps) is used, and tol is set to
* sqrt(eps) on output (`eps' denotes the machine epsilon).
* (`Happy breakdown' is assumed if h(j+1,j) .le. anorm*tol)
*
* anorm : (input) an approximation of some norm of A.
*
* wsp(lwsp): (workspace) lwsp .ge. n*(m+1)+n+(m+2)^2+4*(m+2)^2+ideg+1
* +---------+-------+---------------+
* (actually, ideg=6) V H wsp for PADE
*
* iwsp(liwsp): (workspace) liwsp .ge. m+2
*
* matvec : external subroutine for matrix-vector multiplication.
* synopsis: matvec( x, y )
* double precision x(*), y(*)
* computes: y(1:n) <- A*x(1:n)
* where A is the principal matrix.
*
* IMPORTANT: DMEXPV requires the product y = Ax = Q'x, i.e.
* the TRANSPOSE of the transition rate matrix.
*
* itrace : (input) running mode. 0=silent, 1=print step-by-step info
*
* iflag : (output) exit flag.
* <0 - bad input arguments
* 0 - no problem
* 1 - maximum number of steps reached without convergence
* 2 - requested tolerance was too high
*
*-----Accounts on the computation--------------------------------------|
* Upon exit, an interested user may retrieve accounts on the
* computations. They are located in the workspace arrays wsp and
* iwsp as indicated below:
*
* location mnemonic description
* -----------------------------------------------------------------|
* iwsp(1) = nmult, number of matrix-vector multiplications used
* iwsp(2) = nexph, number of Hessenberg matrix exponential evaluated
* iwsp(3) = nscale, number of repeated squaring involved in Pade
* iwsp(4) = nstep, number of integration steps used up to completion
* iwsp(5) = nreject, number of rejected step-sizes
* iwsp(6) = ibrkflag, set to 1 if `happy breakdown' and 0 otherwise
* iwsp(7) = mbrkdwn, if `happy brkdown', basis-size when it occured
* -----------------------------------------------------------------|
* wsp(1) = step_min, minimum step-size used during integration
* wsp(2) = step_max, maximum step-size used during integration
* wsp(3) = x_round, maximum among all roundoff errors (lower bound)
* wsp(4) = s_round, sum of roundoff errors (lower bound)
* wsp(5) = x_error, maximum among all local truncation errors
* wsp(6) = s_error, global sum of local truncation errors
* wsp(7) = tbrkdwn, if `happy breakdown', time when it occured
* wsp(8) = t_now, integration domain successfully covered
* wsp(9) = hump, i.e., max||exp(sA)||, s in [0,t] (or [t,0] if t<0)
* wsp(10) = ||w||/||v||, scaled norm of the solution w.
* -----------------------------------------------------------------|
* The `hump' is a measure of the conditioning of the problem. The
* matrix exponential is well-conditioned if hump = 1, whereas it is
* poorly-conditioned if hump >> 1. However the solution can still be
* relatively fairly accurate even when the hump is large (the hump
* is an upper bound), especially when the hump and the scaled norm
* of w [this is also computed and returned in wsp(10)] are of the
* same order of magnitude (further details in reference below).
* Markov chains are usually well-conditioned problems.
*
*----------------------------------------------------------------------|
*-----The following parameters may also be adjusted herein-------------|
*
integer mxstep, mxreject, ideg
double precision delta, gamma
parameter( mxstep = 500,
. mxreject = 0,
. ideg = 6,
. delta = 1.2d0,
. gamma = 0.9d0 )
* mxstep : maximum allowable number of integration steps.
* The value 0 means an infinite number of steps.
*
* mxreject: maximum allowable number of rejections at each step.
* The value 0 means an infinite number of rejections.
*
* ideg : the Pade approximation of type (ideg,ideg) is used as
* an approximation to exp(H). The value 0 switches to the
* uniform rational Chebyshev approximation of type (14,14)
*
* delta : local truncation error `safety factor'
*
* gamma : stepsize `shrinking factor'
*
*----------------------------------------------------------------------|
* Roger B. Sidje (rbs@maths.uq.edu.au)
* EXPOKIT: Software Package for Computing Matrix Exponentials.
* ACM - Transactions On Mathematical Software, 24(1):130-156, 1998
*----------------------------------------------------------------------|
*
integer i, j, k1, mh, mx, iv, ih, j1v, ns, ifree, lfree, iexph,
. ireject,ibrkflag,mbrkdwn, nmult, nreject, nexph, nscale,
. nstep
double precision sgn, t_out, tbrkdwn, step_min,step_max, err_loc,
. s_error, x_error, t_now, t_new, t_step, t_old,
. xm, beta, break_tol, p1, p2, p3, eps, rndoff,
. vnorm, avnorm, hj1j, hij, hump, SQR1,
. roundoff, s_round, x_round
intrinsic AINT,ABS,DBLE,LOG10,MAX,MIN,NINT,SIGN,SQRT
double precision DDOT, DNRM2, DASUM
*--- check restrictions on input parameters ...
iflag = 0
if ( lwsp.lt.n*(m+2)+5*(m+2)**2+ideg+1 ) iflag = -1
if ( liwsp.lt.m+2 ) iflag = -2
if ( m.ge.n .or. m.le.0 ) iflag = -3
if ( iflag.ne.0 ) stop 'bad sizes (in input of DMEXPV)'
*
*--- initialisations ...
*
k1 = 2
mh = m + 2
iv = 1
ih = iv + n*(m+1) + n
ifree = ih + mh*mh
lfree = lwsp - ifree + 1
ibrkflag = 0
mbrkdwn = m
nmult = 0
nreject = 0
nexph = 0
nscale = 0
sgn = SIGN( 1.0d0,t )
t_out = ABS( t )
tbrkdwn = 0.0d0
step_min = t_out
step_max = 0.0d0
nstep = 0
s_error = 0.0d0
s_round = 0.0d0
x_error = 0.0d0
x_round = 0.0d0
t_now = 0.0d0
t_new = 0.0d0
p1 = 4.0d0/3.0d0
1 p2 = p1 - 1.0d0
p3 = p2 + p2 + p2
eps = ABS( p3-1.0d0 )
if ( eps.eq.0.0d0 ) go to 1
if ( tol.le.eps ) tol = SQRT( eps )
rndoff = eps*anorm
break_tol = 1.0d-7
*>>> break_tol = tol
*>>> break_tol = anorm*tol
call DCOPY( n, v,1, w,1 )
beta = DNRM2( n, w,1 )
vnorm = beta
hump = beta
*
*--- obtain the very first stepsize ...
*
SQR1 = SQRT( 0.1d0 )
xm = 1.0d0/DBLE( m )
p1 = tol*(((m+1)/2.72D0)**(m+1))*SQRT(2.0D0*3.14D0*(m+1))
t_new = (1.0d0/anorm)*(p1/(4.0d0*beta*anorm))**xm
p1 = 10.0d0**(NINT( LOG10( t_new )-SQR1 )-1)
t_new = AINT( t_new/p1 + 0.55d0 ) * p1
*
*--- step-by-step integration ...
*
100 if ( t_now.ge.t_out ) goto 500
nstep = nstep + 1
t_step = MIN( t_out-t_now, t_new )
p1 = 1.0d0/beta
do i = 1,n
wsp(iv + i-1) = p1*w(i)
enddo
do i = 1,mh*mh
wsp(ih+i-1) = 0.0d0
enddo
*
*--- Arnoldi loop ...
*
j1v = iv + n
do 200 j = 1,m
nmult = nmult + 1
call matvec(n, wsp(j1v-n), wsp(j1v) )
do i = 1,j
hij = DDOT( n, wsp(iv+(i-1)*n),1, wsp(j1v),1 )
call DAXPY( n, -hij, wsp(iv+(i-1)*n),1, wsp(j1v),1 )
wsp(ih+(j-1)*mh+i-1) = hij
enddo
hj1j = DNRM2( n, wsp(j1v),1 )
*--- if `happy breakdown' go straightforward at the end ...
if ( hj1j.le.break_tol ) then
print*,'happy breakdown: mbrkdwn =',j,' h =',hj1j
k1 = 0
ibrkflag = 1
mbrkdwn = j
tbrkdwn = t_now
t_step = t_out-t_now
goto 300
endif
wsp(ih+(j-1)*mh+j) = hj1j
call DSCAL( n, 1.0d0/hj1j, wsp(j1v),1 )
j1v = j1v + n
200 continue
nmult = nmult + 1
call matvec(n, wsp(j1v-n), wsp(j1v) )
avnorm = DNRM2( n, wsp(j1v),1 )
*
*--- set 1 for the 2-corrected scheme ...
*
300 continue
wsp(ih+m*mh+m+1) = 1.0d0
*
*--- loop while ireject<mxreject until the tolerance is reached ...
*
ireject = 0
401 continue
*
*--- compute w = beta*V*exp(t_step*H)*e1 ..
*
nexph = nexph + 1
mx = mbrkdwn + k1
if ( ideg.ne.0 ) then
*--- irreducible rational Pade approximation ...
call DGPADM( ideg, mx, sgn*t_step, wsp(ih),mh,
. wsp(ifree),lfree, iwsp, iexph, ns, iflag )
iexph = ifree + iexph - 1
nscale = nscale + ns
else
*--- uniform rational Chebyshev approximation ...
iexph = ifree
do i = 1,mx
wsp(iexph+i-1) = 0.0d0
enddo
wsp(iexph) = 1.0d0
call DNCHBV(mx,sgn*t_step,wsp(ih),mh,wsp(iexph),wsp(ifree+mx))
endif
402 continue
*
*--- error estimate ...
*
if ( k1.eq.0 ) then
err_loc = tol
else
p1 = ABS( wsp(iexph+m) ) * beta
p2 = ABS( wsp(iexph+m+1) ) * beta * avnorm
if ( p1.gt.10.0d0*p2 ) then
err_loc = p2
xm = 1.0d0/DBLE( m )
elseif ( p1.gt.p2 ) then
err_loc = (p1*p2)/(p1-p2)
xm = 1.0d0/DBLE( m )
else
err_loc = p1
xm = 1.0d0/DBLE( m-1 )
endif
endif
*
*--- reject the step-size if the error is not acceptable ...
*
if ( (k1.ne.0) .and. (err_loc.gt.delta*t_step*tol) .and.
. (mxreject.eq.0 .or. ireject.lt.mxreject) ) then
t_old = t_step
t_step = gamma * t_step * (t_step*tol/err_loc)**xm
p1 = 10.0d0**(NINT( LOG10( t_step )-SQR1 )-1)
t_step = AINT( t_step/p1 + 0.55d0 ) * p1
if ( itrace.ne.0 ) then
print*,'t_step =',t_old
print*,'err_loc =',err_loc
print*,'err_required =',delta*t_old*tol
print*,'stepsize rejected, stepping down to:',t_step
endif
ireject = ireject + 1
nreject = nreject + 1
if ( mxreject.ne.0 .and. ireject.gt.mxreject ) then
print*,"Failure in DMEXPV: ---"
print*,"The requested tolerance is too high."
Print*,"Rerun with a smaller value."
iflag = 2
return
endif
goto 401
endif
*
*--- now update w = beta*V*exp(t_step*H)*e1 and the hump ...
*
mx = mbrkdwn + MAX( 0,k1-1 )
call DGEMV( 'n', n,mx,beta,wsp(iv),n,wsp(iexph),1,0.0d0,w,1 )
beta = DNRM2( n, w,1 )
hump = MAX( hump, beta )
*
*--- Markov model constraints ...
*
j = 0
do i = 1,n
if ( w(i).lt.0.0d0 ) then
w(i) = 0.0d0
j = j + 1
endif
enddo
p1 = DASUM( n, w,1 )
if ( j.gt.0 ) call DSCAL( n, 1.0d0/p1, w,1 )
roundoff = DABS( 1.0d0-p1 ) / DBLE( n )
*
*--- suggested value for the next stepsize ...
*
t_new = gamma * t_step * (t_step*tol/err_loc)**xm
p1 = 10.0d0**(NINT( LOG10( t_new )-SQR1 )-1)
t_new = AINT( t_new/p1 + 0.55d0 ) * p1
err_loc = MAX( err_loc, roundoff )
err_loc = MAX( err_loc, rndoff )
*
*--- update the time covered ...
*
t_now = t_now + t_step
*
*--- display and keep some information ...
*
if ( itrace.ne.0 ) then
print*,'integration',nstep,'---------------------------------'
print*,'scale-square =',ns
print*,'wnorm =',beta
print*,'step_size =',t_step
print*,'err_loc =',err_loc
print*,'roundoff =',roundoff
print*,'next_step =',t_new
endif
step_min = MIN( step_min, t_step )
step_max = MAX( step_max, t_step )
s_error = s_error + err_loc
s_round = s_round + roundoff
x_error = MAX( x_error, err_loc )
x_round = MAX( x_round, roundoff )
if ( mxstep.eq.0 .or. nstep.lt.mxstep ) goto 100
iflag = 1
500 continue
iwsp(1) = nmult
iwsp(2) = nexph
iwsp(3) = nscale
iwsp(4) = nstep
iwsp(5) = nreject
iwsp(6) = ibrkflag
iwsp(7) = mbrkdwn
wsp(1) = step_min
wsp(2) = step_max
wsp(3) = x_round
wsp(4) = s_round
wsp(5) = x_error
wsp(6) = s_error
wsp(7) = tbrkdwn
wsp(8) = sgn*t_now
wsp(9) = hump/vnorm
wsp(10) = beta/vnorm
END
*----------------------------------------------------------------------|
*----------------------------------------------------------------------|
subroutine DGPADM( ideg,m,t,H,ldh,wsp,lwsp,ipiv,iexph,ns,iflag )
implicit none
integer ideg, m, ldh, lwsp, iexph, ns, iflag, ipiv(m)
double precision t, H(ldh,m), wsp(lwsp)
*-----Purpose----------------------------------------------------------|
*
* Computes exp(t*H), the matrix exponential of a general matrix in
* full, using the irreducible rational Pade approximation to the
* exponential function exp(x) = r(x) = (+/-)( I + 2*(q(x)/p(x)) ),
* combined with scaling-and-squaring.
*
*-----Arguments--------------------------------------------------------|
*
* ideg : (input) the degre of the diagonal Pade to be used.
* a value of 6 is generally satisfactory.
*
* m : (input) order of H.
*
* H(ldh,m) : (input) argument matrix.
*
* t : (input) time-scale (can be < 0).
*
* wsp(lwsp) : (workspace/output) lwsp .ge. 4*m*m+ideg+1.
*
* ipiv(m) : (workspace)
*
*>>>> iexph : (output) number such that wsp(iexph) points to exp(tH)
* i.e., exp(tH) is located at wsp(iexph ... iexph+m*m-1)
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* NOTE: if the routine was called with wsp(iptr),
* then exp(tH) will start at wsp(iptr+iexph-1).
*
* ns : (output) number of scaling-squaring used.
*
* iflag : (output) exit flag.
* 0 - no problem
* <0 - problem
*
*----------------------------------------------------------------------|
* Roger B. Sidje (rbs@maths.uq.edu.au)
* EXPOKIT: Software Package for Computing Matrix Exponentials.
* ACM - Transactions On Mathematical Software, 24(1):130-156, 1998
*----------------------------------------------------------------------|
*
Cf2py intent(in) ideg
Cf2py intent(in) m
Cf2py intent(in) H
Cf2py intent(in) t
Cf2py intent(in) wsp
Cf2py intent(in) ipiv
Cf2py intent(out) iexph
Cf2py intent(out) ns
Cf2py intent(out) iflag
integer mm,i,j,k,ih2,ip,iq,iused,ifree,iodd,icoef,iput,iget
double precision hnorm,scale,scale2,cp,cq
intrinsic INT,ABS,DBLE,LOG,MAX
*--- check restrictions on input parameters ...
mm = m*m
iflag = 0
if ( ldh.lt.m ) iflag = -1
if ( lwsp.lt.4*mm+ideg+1 ) iflag = -2
if ( iflag.ne.0 ) stop 'bad sizes (in input of DGPADM)'
*
*--- initialise pointers ...
*
icoef = 1
ih2 = icoef + (ideg+1)
ip = ih2 + mm
iq = ip + mm
ifree = iq + mm
*
*--- scaling: seek ns such that ||t*H/2^ns|| < 1/2;
* and set scale = t/2^ns ...
*
do i = 1,m
wsp(i) = 0.0d0
enddo
do j = 1,m
do i = 1,m
wsp(i) = wsp(i) + ABS( H(i,j) )
enddo
enddo
hnorm = 0.0d0
do i = 1,m
hnorm = MAX( hnorm,wsp(i) )
enddo
hnorm = ABS( t*hnorm )
if ( hnorm.eq.0.0d0 ) stop 'Error - null H in input of DGPADM.'
ns = MAX( 0,INT(LOG(hnorm)/LOG(2.0d0))+2 )
scale = t / DBLE(2**ns)
scale2 = scale*scale
*
*--- compute Pade coefficients ...
*
i = ideg+1
j = 2*ideg+1
wsp(icoef) = 1.0d0
do k = 1,ideg
wsp(icoef+k) = (wsp(icoef+k-1)*DBLE( i-k ))/DBLE( k*(j-k) )
enddo
*
*--- H2 = scale2*H*H ...
*
call DGEMM( 'n','n',m,m,m,scale2,H,ldh,H,ldh,0.0d0,wsp(ih2),m )
*
*--- initialize p (numerator) and q (denominator) ...
*
cp = wsp(icoef+ideg-1)
cq = wsp(icoef+ideg)
do j = 1,m
do i = 1,m
wsp(ip + (j-1)*m + i-1) = 0.0d0
wsp(iq + (j-1)*m + i-1) = 0.0d0
enddo
wsp(ip + (j-1)*(m+1)) = cp
wsp(iq + (j-1)*(m+1)) = cq
enddo
*
*--- Apply Horner rule ...
*
iodd = 1
k = ideg - 1
100 continue
iused = iodd*iq + (1-iodd)*ip
call DGEMM( 'n','n',m,m,m, 1.0d0,wsp(iused),m,
. wsp(ih2),m, 0.0d0,wsp(ifree),m )
do j = 1,m
wsp(ifree+(j-1)*(m+1)) = wsp(ifree+(j-1)*(m+1))+wsp(icoef+k-1)
enddo
ip = (1-iodd)*ifree + iodd*ip
iq = iodd*ifree + (1-iodd)*iq
ifree = iused
iodd = 1-iodd
k = k-1
if ( k.gt.0 ) goto 100
*
*--- Obtain (+/-)(I + 2*(p\q)) ...
*
if ( iodd .eq. 1 ) then
call DGEMM( 'n','n',m,m,m, scale,wsp(iq),m,
. H,ldh, 0.0d0,wsp(ifree),m )
iq = ifree
else
call DGEMM( 'n','n',m,m,m, scale,wsp(ip),m,
. H,ldh, 0.0d0,wsp(ifree),m )
ip = ifree
endif
call DAXPY( mm, -1.0d0,wsp(ip),1, wsp(iq),1 )
call DGESV( m,m, wsp(iq),m, ipiv, wsp(ip),m, iflag )
if ( iflag.ne.0 ) stop 'Problem in DGESV (within DGPADM)'
call DSCAL( mm, 2.0d0, wsp(ip), 1 )
do j = 1,m
wsp(ip+(j-1)*(m+1)) = wsp(ip+(j-1)*(m+1)) + 1.0d0
enddo
iput = ip
if ( ns.eq.0 .and. iodd.eq.1 ) then
call DSCAL( mm, -1.0d0, wsp(ip), 1 )
goto 200
endif
*
*-- squaring : exp(t*H) = (exp(t*H))^(2^ns) ...
*
iodd = 1
do k = 1,ns
iget = iodd*ip + (1-iodd)*iq
iput = (1-iodd)*ip + iodd*iq
call DGEMM( 'n','n',m,m,m, 1.0d0,wsp(iget),m, wsp(iget),m,
. 0.0d0,wsp(iput),m )
iodd = 1-iodd
enddo
200 continue
iexph = iput
END
*----------------------------------------------------------------------|
*----------------------------------------------------------------------|
subroutine DSPADM( ideg,m,t,H,ldh,wsp,lwsp,ipiv,iexph,ns,iflag )
implicit none
integer ideg, m, ldh, lwsp, iexph, ns, iflag, ipiv(m)
double precision t, H(ldh,m), wsp(lwsp)
*-----Purpose----------------------------------------------------------|
*
* Computes exp(t*H), the matrix exponential of a symmetric matrix
* in full, using the irreducible rational Pade approximation to the
* exponential function exp(x) = r(x) = (+/-)( I + 2*(q(x)/p(x)) ),
* combined with scaling-and-squaring.
*
*-----Arguments--------------------------------------------------------|
*
* ideg : (input) the degre of the diagonal Pade to be used.
* a value of 6 is generally satisfactory.
*
* m : (input) order of H.
*
* H(ldh,m) : (input) argument matrix (both lower and upper parts).
*
* t : (input) time-scale (can be < 0).
*
* wsp(lwsp) : (workspace/output) lwsp .ge. 4*m*m+ideg+1.
*
* ipiv(m) : (workspace)
*
*>>>> iexph : (output) number such that wsp(iexph) points to exp(tH)
* i.e., exp(tH) is located at wsp(iexph ... iexph+m*m-1)
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* NOTE: if the routine was called with wsp(iptr),
* then exp(tH) will start at wsp(iptr+iexph-1).
*
* ns : (output) number of scaling-squaring used.
*
* iflag : (output) exit flag.
* 0 - no problem
* <0 - problem
*
*----------------------------------------------------------------------|
* Roger B. Sidje (rbs@maths.uq.edu.au)
* EXPOKIT: Software Package for Computing Matrix Exponentials.
* ACM - Transactions On Mathematical Software, 24(1):130-156, 1998
*----------------------------------------------------------------------|
*
integer mm,i,j,k,ih2,ip,iq,iused,ifree,iodd,icoef,iput,iget
double precision hnorm,scale,scale2,cp,cq
intrinsic INT,ABS,DBLE,LOG,MAX
*--- check restrictions on input parameters ...
mm = m*m
iflag = 0
if ( ldh.lt.m ) iflag = -1
if ( lwsp.lt.4*mm+ideg+1 ) iflag = -2
if ( iflag.ne.0 ) stop 'bad sizes (in input of DSPADM)'
*
*--- initialise pointers ...
*
icoef = 1
ih2 = icoef + (ideg+1)
ip = ih2 + mm
iq = ip + mm
ifree = iq + mm
*
*--- scaling: seek ns such that ||t*H/2^ns|| < 1/2;
* and set scale = t/2^ns ...
*
do i = 1,m
wsp(i) = 0.0d0
enddo
do j = 1,m
do i = 1,m
wsp(i) = wsp(i) + ABS( H(i,j) )
enddo
enddo
hnorm = 0.0d0
do i = 1,m
hnorm = MAX( hnorm,wsp(i) )
enddo
hnorm = ABS( t*hnorm )
if ( hnorm.eq.0.0d0 ) stop 'Error - null H in input of DSPADM.'
ns = MAX( 0,INT(LOG(hnorm)/LOG(2.0d0))+2 )
scale = t / DBLE(2**ns)
scale2 = scale*scale
*
*--- compute Pade coefficients ...
*
i = ideg+1
j = 2*ideg+1
wsp(icoef) = 1.0d0
do k = 1,ideg
wsp(icoef+k) = (wsp(icoef+k-1)*DBLE( i-k ))/DBLE( k*(j-k) )
enddo
*
*--- H2 = scale2*H*H ...
*
call DGEMM( 'n','n',m,m,m,scale2,H,ldh,H,ldh,0.0d0,wsp(ih2),m )
*
*--- initialize p (numerator) and q (denominator) ...
*
cp = wsp(icoef+ideg-1)
cq = wsp(icoef+ideg)
do j = 1,m
do i = 1,m
wsp(ip + (j-1)*m + i-1) = 0.0d0
wsp(iq + (j-1)*m + i-1) = 0.0d0
enddo
wsp(ip + (j-1)*(m+1)) = cp
wsp(iq + (j-1)*(m+1)) = cq
enddo
*
*--- Apply Horner rule ...
*
iodd = 1
k = ideg - 1
100 continue
iused = iodd*iq + (1-iodd)*ip
call DGEMM( 'n','n',m,m,m, 1.0d0,wsp(iused),m,
. wsp(ih2),m, 0.0d0,wsp(ifree),m )
do j = 1,m
wsp(ifree+(j-1)*(m+1)) = wsp(ifree+(j-1)*(m+1))+wsp(icoef+k-1)
enddo
ip = (1-iodd)*ifree + iodd*ip
iq = iodd*ifree + (1-iodd)*iq
ifree = iused
iodd = 1-iodd
k = k-1
if ( k.gt.0 ) goto 100
*
*--- Obtain (+/-)(I + 2*(p\q)) ...
*
if ( iodd .eq. 1 ) then
call DGEMM( 'n','n',m,m,m, scale,wsp(iq),m,
. H,ldh, 0.0d0,wsp(ifree),m )
iq = ifree
else
call DGEMM( 'n','n',m,m,m, scale,wsp(ip),m,
. H,ldh, 0.0d0,wsp(ifree),m )
ip = ifree
endif
call DAXPY( mm, -1.0d0,wsp(ip),1, wsp(iq),1 )
call DSYSV( 'U',m,m,wsp(iq),m,ipiv,wsp(ip),m,wsp(ih2),mm,iflag )
if ( iflag.ne.0 ) stop 'Problem in DSYSV (within DSPADM)'
call DSCAL( mm, 2.0d0, wsp(ip), 1 )
do j = 1,m
wsp(ip+(j-1)*(m+1)) = wsp(ip+(j-1)*(m+1)) + 1.0d0
enddo
iput = ip
if ( ns.eq.0 .and. iodd.eq.1 ) then
call DSCAL( mm, -1.0d0, wsp(ip), 1 )
goto 200
endif
*
*-- squaring : exp(t*H) = (exp(t*H))^(2^ns) ...
*
iodd = 1
do k = 1,ns
iget = iodd*ip + (1-iodd)*iq
iput = (1-iodd)*ip + iodd*iq
call DGEMM( 'n','n',m,m,m, 1.0d0,wsp(iget),m, wsp(iget),m,
. 0.0d0,wsp(iput),m )
iodd = 1-iodd
enddo
200 continue
iexph = iput
END
*----------------------------------------------------------------------|
*----------------------------------------------------------------------|
subroutine ZGPADM(ideg,m,t,H,ldh,wsp,lwsp,ipiv,iexph,ns,iflag)
implicit none
double precision t
integer ideg, m, ldh, lwsp, iexph, ns, iflag, ipiv(m)
complex*16 H(ldh,m), wsp(lwsp)
*-----Purpose----------------------------------------------------------|
*
* Computes exp(t*H), the matrix exponential of a general complex
* matrix in full, using the irreducible rational Pade approximation
* to the exponential exp(z) = r(z) = (+/-)( I + 2*(q(z)/p(z)) ),
* combined with scaling-and-squaring.
*
*-----Arguments--------------------------------------------------------|
*
* ideg : (input) the degre of the diagonal Pade to be used.
* a value of 6 is generally satisfactory.
*
* m : (input) order of H.
*
* H(ldh,m) : (input) argument matrix.
*
* t : (input) time-scale (can be < 0).
*
* wsp(lwsp) : (workspace/output) lwsp .ge. 4*m*m+ideg+1.
*
* ipiv(m) : (workspace)
*
*>>>> iexph : (output) number such that wsp(iexph) points to exp(tH)
* i.e., exp(tH) is located at wsp(iexph ... iexph+m*m-1)
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* NOTE: if the routine was called with wsp(iptr),
* then exp(tH) will start at wsp(iptr+iexph-1).
*
* ns : (output) number of scaling-squaring used.
*
* iflag : (output) exit flag.
* 0 - no problem
* <0 - problem
*
*----------------------------------------------------------------------|
* Roger B. Sidje (rbs@maths.uq.edu.au)
* EXPOKIT: Software Package for Computing Matrix Exponentials.
* ACM - Transactions On Mathematical Software, 24(1):130-156, 1998
*----------------------------------------------------------------------|
*
integer i,j,k,icoef,mm,ih2,iodd,iused,ifree,iq,ip,iput,iget
double precision hnorm
complex*16 cp, cq, scale, scale2, ZERO, ONE
parameter( ZERO=(0.0d0,0.0d0), ONE=(1.0d0,0.0d0) )
intrinsic ABS, CMPLX, DBLE, INT, LOG, MAX
*--- check restrictions on input parameters ...
mm = m*m
iflag = 0
if ( ldh.lt.m ) iflag = -1
if ( lwsp.lt.4*mm+ideg+1 ) iflag = -2
if ( iflag.ne.0 ) stop 'bad sizes (in input of ZGPADM)'
*
*--- initialise pointers ...
*
icoef = 1
ih2 = icoef + (ideg+1)
ip = ih2 + mm
iq = ip + mm
ifree = iq + mm
*
*--- scaling: seek ns such that ||t*H/2^ns|| < 1/2;
* and set scale = t/2^ns ...
*
do i = 1,m
wsp(i) = ZERO
enddo
do j = 1,m
do i = 1,m
wsp(i) = wsp(i) + ABS( H(i,j) )
enddo
enddo
hnorm = 0.0d0
do i = 1,m
hnorm = MAX( hnorm,DBLE(wsp(i)) )
enddo
hnorm = ABS( t*hnorm )
if ( hnorm.eq.0.0d0 ) stop 'Error - null H in input of ZGPADM.'
ns = MAX( 0,INT(LOG(hnorm)/LOG(2.0d0))+2 )
scale = CMPLX( t/DBLE(2**ns),0.0d0 )
scale2 = scale*scale
*
*--- compute Pade coefficients ...
*
i = ideg+1
j = 2*ideg+1
wsp(icoef) = ONE
do k = 1,ideg
wsp(icoef+k) = (wsp(icoef+k-1)*DBLE( i-k ))/DBLE( k*(j-k) )
enddo
*
*--- H2 = scale2*H*H ...
*
call ZGEMM( 'n','n',m,m,m,scale2,H,ldh,H,ldh,ZERO,wsp(ih2),m )
*
*--- initialise p (numerator) and q (denominator) ...
*
cp = wsp(icoef+ideg-1)
cq = wsp(icoef+ideg)
do j = 1,m
do i = 1,m
wsp(ip + (j-1)*m + i-1) = ZERO
wsp(iq + (j-1)*m + i-1) = ZERO
enddo
wsp(ip + (j-1)*(m+1)) = cp
wsp(iq + (j-1)*(m+1)) = cq
enddo
*
*--- Apply Horner rule ...
*
iodd = 1
k = ideg - 1
100 continue
iused = iodd*iq + (1-iodd)*ip
call ZGEMM( 'n','n',m,m,m, ONE,wsp(iused),m,
. wsp(ih2),m, ZERO,wsp(ifree),m )
do j = 1,m
wsp(ifree+(j-1)*(m+1)) = wsp(ifree+(j-1)*(m+1))+wsp(icoef+k-1)
enddo
ip = (1-iodd)*ifree + iodd*ip
iq = iodd*ifree + (1-iodd)*iq
ifree = iused
iodd = 1-iodd
k = k-1
if ( k.gt.0 ) goto 100
*
*--- Obtain (+/-)(I + 2*(p\q)) ...
*
if ( iodd.ne.0 ) then
call ZGEMM( 'n','n',m,m,m, scale,wsp(iq),m,
. H,ldh, ZERO,wsp(ifree),m )
iq = ifree
else
call ZGEMM( 'n','n',m,m,m, scale,wsp(ip),m,
. H,ldh, ZERO,wsp(ifree),m )
ip = ifree
endif
call ZAXPY( mm, -ONE,wsp(ip),1, wsp(iq),1 )
call ZGESV( m,m, wsp(iq),m, ipiv, wsp(ip),m, iflag )
if ( iflag.ne.0 ) stop 'Problem in ZGESV (within ZGPADM)'
call ZDSCAL( mm, 2.0d0, wsp(ip), 1 )
do j = 1,m
wsp(ip+(j-1)*(m+1)) = wsp(ip+(j-1)*(m+1)) + ONE
enddo
iput = ip
if ( ns.eq.0 .and. iodd.ne.0 ) then
call ZDSCAL( mm, -1.0d0, wsp(ip), 1 )
goto 200
endif
*
*-- squaring : exp(t*H) = (exp(t*H))^(2^ns) ...
*
iodd = 1
do k = 1,ns
iget = iodd*ip + (1-iodd)*iq
iput = (1-iodd)*ip + iodd*iq
call ZGEMM( 'n','n',m,m,m, ONE,wsp(iget),m, wsp(iget),m,
. ZERO,wsp(iput),m )
iodd = 1-iodd
enddo
200 continue
iexph = iput
END
*----------------------------------------------------------------------|
subroutine ZHPADM(ideg,m,t,H,ldh,wsp,lwsp,ipiv,iexph,ns,iflag)
implicit none
double precision t
integer ideg, m, ldh, lwsp, iexph, ns, iflag, ipiv(m)
complex*16 H(ldh,m), wsp(lwsp)
*-----Purpose----------------------------------------------------------|
*
* Computes exp(t*H), the matrix exponential of an Hermitian matrix
* in full, using the irreducible rational Pade approximation to the
* exponential function exp(z) = r(z) = (+/-)( I + 2*(q(z)/p(z)) ),
* combined with scaling-and-squaring.
*
*-----Arguments--------------------------------------------------------|
*
* ideg : (input) the degre of the diagonal Pade to be used.
* a value of 6 is generally satisfactory.
*
* m : (input) order of H.
*
* H(ldh,m) : (input) argument matrix (both lower and upper parts).
*
* t : (input) time-scale (can be < 0).
*
* wsp(lwsp) : (workspace/output) lwsp .ge. 4*m*m+ideg+1.
*
* ipiv(m) : (workspace)
*
*>>>> iexph : (output) number such that wsp(iexph) points to exp(tH)
* i.e., exp(tH) is located at wsp(iexph ... iexph+m*m-1)
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* NOTE: if the routine was called with wsp(iptr),
* then exp(tH) will start at wsp(iptr+iexph-1).
*
* ns : (output) number of scaling-squaring used.
*
* iflag : (output) exit flag.
* 0 - no problem
* <0 - problem
*
*----------------------------------------------------------------------|
* Roger B. Sidje (rbs@maths.uq.edu.au)
* EXPOKIT: Software Package for Computing Matrix Exponentials.
* ACM - Transactions On Mathematical Software, 24(1):130-156, 1998
*----------------------------------------------------------------------|
*
integer i,j,k,icoef,mm,ih2,iodd,iused,ifree,iq,ip,iput,iget
double precision hnorm
complex*16 cp, cq, scale, scale2, ZERO, ONE
parameter( ZERO=(0.0d0,0.0d0), ONE=(1.0d0,0.0d0) )
intrinsic ABS, CMPLX, DBLE, INT, LOG, MAX
*--- check restrictions on input parameters ...
mm = m*m
iflag = 0
if ( ldh.lt.m ) iflag = -1