| 1 | *----------------------------------------------------------------------| |
| 2 | subroutine DGPADM( ideg,m,t,H,ldh,wsp,lwsp,ipiv,iexph,ns,iflag ) |
| 3 | |
| 4 | implicit none |
| 5 | integer ideg, m, ldh, lwsp, iexph, ns, iflag, ipiv(m) |
| 6 | double precision t, H(ldh,m), wsp(lwsp) |
| 7 | |
| 8 | *-----Purpose----------------------------------------------------------| |
| 9 | * |
| 10 | * Computes exp(t*H), the matrix exponential of a general matrix in |
| 11 | * full, using the irreducible rational Pade approximation to the |
| 12 | * exponential function exp(x) = r(x) = (+/-)( I + 2*(q(x)/p(x)) ), |
| 13 | * combined with scaling-and-squaring. |
| 14 | * |
| 15 | *-----Arguments--------------------------------------------------------| |
| 16 | * |
| 17 | * ideg : (input) the degre of the diagonal Pade to be used. |
| 18 | * a value of 6 is generally satisfactory. |
| 19 | * |
| 20 | * m : (input) order of H. |
| 21 | * |
| 22 | * H(ldh,m) : (input) argument matrix. |
| 23 | * |
| 24 | * t : (input) time-scale (can be < 0). |
| 25 | * |
| 26 | * wsp(lwsp) : (workspace/output) lwsp .ge. 4*m*m+ideg+1. |
| 27 | * |
| 28 | * ipiv(m) : (workspace) |
| 29 | * |
| 30 | *>>>> iexph : (output) number such that wsp(iexph) points to exp(tH) |
| 31 | * i.e., exp(tH) is located at wsp(iexph ... iexph+m*m-1) |
| 32 | * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 33 | * NOTE: if the routine was called with wsp(iptr), |
| 34 | * then exp(tH) will start at wsp(iptr+iexph-1). |
| 35 | * |
| 36 | * ns : (output) number of scaling-squaring used. |
| 37 | * |
| 38 | * iflag : (output) exit flag. |
| 39 | * 0 - no problem |
| 40 | * <0 - problem |
| 41 | * |
| 42 | *----------------------------------------------------------------------| |
| 43 | * Roger B. Sidje ([email protected]) |
| 44 | * EXPOKIT: Software Package for Computing Matrix Exponentials. |
| 45 | * ACM - Transactions On Mathematical Software, 24(1):130-156, 1998 |
| 46 | *----------------------------------------------------------------------| |
| 47 | * |
| 48 | integer mm,i,j,k,ih2,ip,iq,iused,ifree,iodd,icoef,iput,iget |
| 49 | double precision hnorm,scale,scale2,cp,cq |
| 50 | |
| 51 | intrinsic INT,ABS,DBLE,LOG,MAX |
| 52 | |
| 53 | *--- check restrictions on input parameters ... |
| 54 | mm = m*m |
| 55 | iflag = 0 |
| 56 | if ( ldh.lt.m ) iflag = -1 |
| 57 | if ( lwsp.lt.4*mm+ideg+1 ) iflag = -2 |
| 58 | if ( iflag.ne.0 ) stop 'bad sizes (in input of DGPADM)' |
| 59 | * |
| 60 | *--- initialise pointers ... |
| 61 | * |
| 62 | icoef = 1 |
| 63 | ih2 = icoef + (ideg+1) |
| 64 | ip = ih2 + mm |
| 65 | iq = ip + mm |
| 66 | ifree = iq + mm |
| 67 | * |
| 68 | *--- scaling: seek ns such that ||t*H/2^ns|| < 1/2; |
| 69 | * and set scale = t/2^ns ... |
| 70 | * |
| 71 | do i = 1,m |
| 72 | wsp(i) = 0.0d0 |
| 73 | enddo |
| 74 | do j = 1,m |
| 75 | do i = 1,m |
| 76 | wsp(i) = wsp(i) + ABS( H(i,j) ) |
| 77 | enddo |
| 78 | enddo |
| 79 | hnorm = 0.0d0 |
| 80 | do i = 1,m |
| 81 | hnorm = MAX( hnorm,wsp(i) ) |
| 82 | enddo |
| 83 | hnorm = ABS( t*hnorm ) |
| 84 | if ( hnorm.eq.0.0d0 ) stop 'Error - null H in input of DGPADM.' |
| 85 | ns = MAX( 0,INT(LOG(hnorm)/LOG(2.0d0))+2 ) |
| 86 | scale = t / DBLE(2**ns) |
| 87 | scale2 = scale*scale |
| 88 | * |
| 89 | *--- compute Pade coefficients ... |
| 90 | * |
| 91 | i = ideg+1 |
| 92 | j = 2*ideg+1 |
| 93 | wsp(icoef) = 1.0d0 |
| 94 | do k = 1,ideg |
| 95 | wsp(icoef+k) = (wsp(icoef+k-1)*DBLE( i-k ))/DBLE( k*(j-k) ) |
| 96 | enddo |
| 97 | * |
| 98 | *--- H2 = scale2*H*H ... |
| 99 | * |
| 100 | call DGEMM( 'n','n',m,m,m,scale2,H,ldh,H,ldh,0.0d0,wsp(ih2),m ) |
| 101 | * |
| 102 | *--- initialize p (numerator) and q (denominator) ... |
| 103 | * |
| 104 | cp = wsp(icoef+ideg-1) |
| 105 | cq = wsp(icoef+ideg) |
| 106 | do j = 1,m |
| 107 | do i = 1,m |
| 108 | wsp(ip + (j-1)*m + i-1) = 0.0d0 |
| 109 | wsp(iq + (j-1)*m + i-1) = 0.0d0 |
| 110 | enddo |
| 111 | wsp(ip + (j-1)*(m+1)) = cp |
| 112 | wsp(iq + (j-1)*(m+1)) = cq |
| 113 | enddo |
| 114 | * |
| 115 | *--- Apply Horner rule ... |
| 116 | * |
| 117 | iodd = 1 |
| 118 | k = ideg - 1 |
| 119 | 100 continue |
| 120 | iused = iodd*iq + (1-iodd)*ip |
| 121 | call DGEMM( 'n','n',m,m,m, 1.0d0,wsp(iused),m, |
| 122 | . wsp(ih2),m, 0.0d0,wsp(ifree),m ) |
| 123 | do j = 1,m |
| 124 | wsp(ifree+(j-1)*(m+1)) = wsp(ifree+(j-1)*(m+1))+wsp(icoef+k-1) |
| 125 | enddo |
| 126 | ip = (1-iodd)*ifree + iodd*ip |
| 127 | iq = iodd*ifree + (1-iodd)*iq |
| 128 | ifree = iused |
| 129 | iodd = 1-iodd |
| 130 | k = k-1 |
| 131 | if ( k.gt.0 ) goto 100 |
| 132 | * |
| 133 | *--- Obtain (+/-)(I + 2*(p\q)) ... |
| 134 | * |
| 135 | if ( iodd .eq. 1 ) then |
| 136 | call DGEMM( 'n','n',m,m,m, scale,wsp(iq),m, |
| 137 | . H,ldh, 0.0d0,wsp(ifree),m ) |
| 138 | iq = ifree |
| 139 | else |
| 140 | call DGEMM( 'n','n',m,m,m, scale,wsp(ip),m, |
| 141 | . H,ldh, 0.0d0,wsp(ifree),m ) |
| 142 | ip = ifree |
| 143 | endif |
| 144 | call DAXPY( mm, -1.0d0,wsp(ip),1, wsp(iq),1 ) |
| 145 | call DGESV( m,m, wsp(iq),m, ipiv, wsp(ip),m, iflag ) |
| 146 | if ( iflag.ne.0 ) stop 'Problem in DGESV (within DGPADM)' |
| 147 | call DSCAL( mm, 2.0d0, wsp(ip), 1 ) |
| 148 | do j = 1,m |
| 149 | wsp(ip+(j-1)*(m+1)) = wsp(ip+(j-1)*(m+1)) + 1.0d0 |
| 150 | enddo |
| 151 | iput = ip |
| 152 | if ( ns.eq.0 .and. iodd.eq.1 ) then |
| 153 | call DSCAL( mm, -1.0d0, wsp(ip), 1 ) |
| 154 | goto 200 |
| 155 | endif |
| 156 | * |
| 157 | *-- squaring : exp(t*H) = (exp(t*H))^(2^ns) ... |
| 158 | * |
| 159 | iodd = 1 |
| 160 | do k = 1,ns |
| 161 | iget = iodd*ip + (1-iodd)*iq |
| 162 | iput = (1-iodd)*ip + iodd*iq |
| 163 | call DGEMM( 'n','n',m,m,m, 1.0d0,wsp(iget),m, wsp(iget),m, |
| 164 | . 0.0d0,wsp(iput),m ) |
| 165 | iodd = 1-iodd |
| 166 | enddo |
| 167 | 200 continue |
| 168 | iexph = iput |
| 169 | END |
| 170 | *----------------------------------------------------------------------| |