function i4_log_10 ( i ) !*****************************************************************************80 ! !! i4_log_10() returns the integer part of the logarithm base 10 of an I4. ! ! Example: ! ! I I4_LOG_10 ! ----- -------- ! 0 0 ! 1 0 ! 2 0 ! 9 0 ! 10 1 ! 11 1 ! 99 1 ! 100 2 ! 101 2 ! 999 2 ! 1000 3 ! 1001 3 ! 9999 3 ! 10000 4 ! ! Discussion: ! ! I4_LOG_10 ( I ) + 1 is the number of decimal digits in I. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 08 June 2003 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer I, the number whose logarithm base 10 ! is desired. ! ! Output, integer I4_LOG_10, the integer part of the ! logarithm base 10 of the absolute value of X. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer i integer i_abs integer i4_log_10 integer ten_pow if ( i == 0 ) then i4_log_10 = 0 else i4_log_10 = 0 ten_pow = 10 i_abs = abs ( i ) do while ( ten_pow <= i_abs ) i4_log_10 = i4_log_10 + 1 ten_pow = ten_pow * 10 end do end if return end subroutine i4vec_print ( n, a, title ) !*****************************************************************************80 ! !! I4VEC_PRINT prints an I4VEC. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 28 November 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N, the number of components of the vector. ! ! Input, integer A(N), the vector to be printed. ! ! Input, character ( len = * ) TITLE, a title first. ! TITLE may be blank. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n integer a(n) integer big integer i character ( len = * ) title if ( title /= ' ' ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) trim ( title ) end if big = maxval ( abs ( a(1:n) ) ) write ( *, '(a)' ) ' ' if ( big < 1000 ) then do i = 1, n write ( *, '(i8,1x,i4)' ) i, a(i) end do else if ( big < 1000000 ) then do i = 1, n write ( *, '(i8,1x,i7)' ) i, a(i) end do else do i = 1, n write ( *, '(i8,i11)' ) i, a(i) end do end if return end function r8_sign ( x ) !*****************************************************************************80 ! !! R8_SIGN returns the sign of an R8. ! ! Discussion: ! ! value = -1 if X < 0; ! value = +1 if X => 0. ! ! Note that the standard FORTRAN90 "sign" function is more complicated. ! In particular, ! ! Z = sign ( X, Y ) ! ! means that ! ! Z = |X| if 0 <= Y; ! - |X| if Y < 0; ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 27 March 2004 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, real ( kind = rk ) X, the number whose sign is desired. ! ! Output, real ( kind = rk ) R8_SIGN, the sign of X: ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) real ( kind = rk ) r8_sign real ( kind = rk ) value real ( kind = rk ) x if ( x < 0.0D+00 ) then value = -1.0D+00 else value = +1.0D+00 end if r8_sign = value return end function r8_uniform_ab ( a, b ) !*****************************************************************************80 ! !! R8_UNIFORM_AB returns a scaled pseudorandom R8. ! ! Discussion: ! ! An R8 is a real ( kind = rk ) value. ! ! The pseudorandom number should be uniformly distributed ! between A and B. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 05 July 2006 ! ! Author: ! ! John Burkardt ! ! Input: ! ! real ( kind = rk ) A, B, the limits of the interval. ! ! Output: ! ! real ( kind = rk ) R8_UNIFORM_AB, a number strictly between A and B. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) real ( kind = rk ) a real ( kind = rk ) b real ( kind = rk ) r real ( kind = rk ) r8_uniform_ab call random_number ( harvest = r ) r8_uniform_ab = a + ( b - a ) * r return end subroutine r8col_swap ( m, n, a, j1, j2 ) !*****************************************************************************80 ! !! R8COL_SWAP swaps columns I and J of an R8COL. ! ! Discussion: ! ! An R8COL is an M by N array of R8's, regarded as an array of N columns, ! each of length M. ! ! Example: ! ! Input: ! ! M = 3, N = 4, J1 = 2, J2 = 4 ! ! A = ( ! 1. 2. 3. 4. ! 5. 6. 7. 8. ! 9. 10. 11. 12. ) ! ! Output: ! ! A = ( ! 1. 4. 3. 2. ! 5. 8. 7. 6. ! 9. 12. 11. 10. ) ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 05 December 2004 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, N, the number of rows and columns. ! ! Input/output, real ( kind = rk ) A(M,N), the M by N array. ! ! Input, integer J1, J2, the columns to be swapped. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer m integer n real ( kind = rk ) a(m,n) real ( kind = rk ) col(m) integer j1 integer j2 if ( j1 < 1 .or. n < j1 .or. j2 < 1 .or. n < j2 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'R8COL_SWAP - Fatal error!' write ( *, '(a)' ) ' J1 or J2 is out of bounds.' write ( *, '(a,i8)' ) ' J1 = ', j1 write ( *, '(a,i8)' ) ' J2 = ', j2 write ( *, '(a,i8)' ) ' NCOL = ', n stop 1 end if if ( j1 == j2 ) then return end if col(1:m) = a(1:m,j1) a(1:m,j1) = a(1:m,j2) a(1:m,j2) = col(1:m) return end subroutine r8ge_cg ( n, a, b, x ) !*****************************************************************************80 ! !! R8GE_CG uses the conjugate gradient method on an R8GE system. ! ! Discussion: ! ! The matrix A must be a positive definite symmetric band matrix. ! ! The method is designed to reach the solution after N computational ! steps. However, roundoff may introduce unacceptably large errors for ! some problems. In such a case, calling the routine again, using ! the computed solution as the new starting estimate, should improve ! the results. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 03 June 2014 ! ! Author: ! ! John Burkardt ! ! Reference: ! ! Frank Beckman, ! The Solution of Linear Equations by the Conjugate Gradient Method, ! in Mathematical Methods for Digital Computers, ! edited by John Ralston, Herbert Wilf, ! Wiley, 1967, ! ISBN: 0471706892, ! LC: QA76.5.R3. ! ! Parameters: ! ! Input, integer N, the order of the matrix. ! N must be positive. ! ! Input, real ( kind = rk ) A(N,N), the matrix. ! ! Input, real ( kind = rk ) B(N), the right hand side vector. ! ! Input/output, real ( kind = rk ) X(N). ! On input, an estimate for the solution, which may be 0. ! On output, the approximate solution vector. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n real ( kind = rk ) a(n,n) real ( kind = rk ) alpha real ( kind = rk ) ap(n) real ( kind = rk ) b(n) real ( kind = rk ) beta integer it real ( kind = rk ) p(n) real ( kind = rk ) pap real ( kind = rk ) pr real ( kind = rk ) r(n) real ( kind = rk ) rap real ( kind = rk ) x(n) ! ! Initialize ! AP = A * x, ! R = b - A * x, ! P = b - A * x. ! call r8ge_mv ( n, n, a, x, ap ) r(1:n) = b(1:n) - ap(1:n) p(1:n) = b(1:n) - ap(1:n) ! ! Do the N steps of the conjugate gradient method. ! do it = 1, n ! ! Compute the matrix*vector product AP=A*P. ! call r8ge_mv ( n, n, a, p, ap ) ! ! Compute the dot products ! PAP = P*AP, ! PR = P*R ! Set ! ALPHA = PR / PAP. ! pap = dot_product ( p(1:n), ap(1:n) ) pr = dot_product ( p(1:n), r(1:n) ) if ( pap == 0.0D+00 ) then return end if alpha = pr / pap ! ! Set ! X = X + ALPHA * P ! R = R - ALPHA * AP. ! x(1:n) = x(1:n) + alpha * p(1:n) r(1:n) = r(1:n) - alpha * ap(1:n) ! ! Compute the vector dot product ! RAP = R*AP ! Set ! BETA = - RAP / PAP. ! rap = dot_product ( r(1:n), ap(1:n) ) beta = - rap / pap ! ! Update the perturbation vector ! P = R + BETA * P. ! p(1:n) = r(1:n) + beta * p(1:n) end do return end subroutine r8ge_co ( n, a, pivot, rcond, z ) !*****************************************************************************80 ! !! R8GE_CO factors an R8GE matrix and estimates its condition number. ! ! Discussion: ! ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! R8GE storage is used by LINPACK and LAPACK. ! ! For the system A * X = B, relative perturbations in A and B ! of size EPSILON may cause relative perturbations in X of size ! EPSILON/RCOND. ! ! If RCOND is so small that the logical expression ! 1.0D+00 + rcond == 1.0D+00 ! is true, then A may be singular to working precision. In particular, ! RCOND is zero if exact singularity is detected or the estimate ! underflows. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 26 March 2004 ! ! Author: ! ! Original FORTRAN77 version by Dongarra, Bunch, Moler, Stewart. ! FORTRAN90 version by John Burkardt. ! ! Reference: ! ! Jack Dongarra, Jim Bunch, Cleve Moler, Pete Stewart, ! LINPACK User's Guide, ! SIAM, 1979, ! ISBN13: 978-0-898711-72-1, ! LC: QA214.L56. ! ! Parameters: ! ! Input, integer N, the order of the matrix A. ! ! Input/output, real ( kind = rk ) A(N,N). On input, a matrix to be factored. ! On output, the LU factorization of the matrix. ! ! Output, integer PIVOT(N), the pivot indices. ! ! Output, real ( kind = rk ) RCOND, an estimate of the reciprocal ! condition number of A. ! ! Output, real ( kind = rk ) Z(N), a work vector whose contents are ! usually unimportant. If A is close to a singular matrix, then Z is ! an approximate null vector in the sense that ! norm ( A * Z ) = RCOND * norm ( A ) * norm ( Z ). ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n real ( kind = rk ) a(n,n) real ( kind = rk ) anorm real ( kind = rk ) ek integer info integer j integer k integer l integer pivot(n) real ( kind = rk ) rcond real ( kind = rk ) s real ( kind = rk ) sm real ( kind = rk ) t real ( kind = rk ) wk real ( kind = rk ) wkm real ( kind = rk ) ynorm real ( kind = rk ) z(n) ! ! Compute the L1 norm of A. ! anorm = 0.0D+00 do j = 1, n anorm = max ( anorm, sum ( abs ( a(1:n,j) ) ) ) end do ! ! Compute the LU factorization. ! call r8ge_fa ( n, a, pivot, info ) ! ! RCOND = 1 / ( norm(A) * (estimate of norm(inverse(A))) ) ! ! estimate of norm(inverse(A)) = norm(Z) / norm(Y) ! ! where ! A * Z = Y ! and ! A' * Y = E ! ! The components of E are chosen to cause maximum local growth in the ! elements of W, where U'*W = E. The vectors are frequently rescaled ! to avoid overflow. ! ! Solve U' * W = E. ! ek = 1.0D+00 z(1:n) = 0.0D+00 do k = 1, n if ( z(k) /= 0.0D+00 ) then ek = sign ( ek, -z(k) ) end if if ( abs ( a(k,k) ) < abs ( ek - z(k) ) ) then s = abs ( a(k,k) ) / abs ( ek - z(k) ) z(1:n) = s * z(1:n) ek = s * ek end if wk = ek - z(k) wkm = -ek - z(k) s = abs ( wk ) sm = abs ( wkm ) if ( a(k,k) /= 0.0D+00 ) then wk = wk / a(k,k) wkm = wkm / a(k,k) else wk = 1.0D+00 wkm = 1.0D+00 end if if ( k + 1 <= n ) then do j = k + 1, n sm = sm + abs ( z(j) + wkm * a(k,j) ) z(j) = z(j) + wk * a(k,j) s = s + abs ( z(j) ) end do if ( s < sm ) then t = wkm - wk wk = wkm z(k+1:n) = z(k+1:n) + t * a(k,k+1:n) end if end if z(k) = wk end do t = sum ( abs ( z(1:n) ) ) z(1:n) = z(1:n) / t ! ! Solve L' * Y = W ! do k = n, 1, -1 z(k) = z(k) + sum ( a(k+1:n,k) * z(k+1:n) ) t = abs ( z(k) ) if ( 1.0D+00 < t ) then z(1:n) = z(1:n) / t end if l = pivot(k) t = z(l) z(l) = z(k) z(k) = t end do z(1:n) = z(1:n) / sum ( abs ( z(1:n) ) ) ynorm = 1.0D+00 ! ! Solve L * V = Y. ! do k = 1, n l = pivot(k) t = z(l) z(l) = z(k) z(k) = t z(k+1:n) = z(k+1:n) + z(k) * a(k+1:n,k) if ( 1.0D+00 < abs ( z(k) ) ) then ynorm = ynorm / abs ( z(k) ) z(1:n) = z(1:n) / abs ( z(k) ) end if end do s = sum ( abs ( z(1:n) ) ) z(1:n) = z(1:n) / s ynorm = ynorm / s ! ! Solve U * Z = V. ! do k = n, 1, -1 if ( abs ( a(k,k) ) < abs ( z(k) ) ) then s = abs ( a(k,k) ) / abs ( z(k) ) z(1:n) = s * z(1:n) ynorm = s * ynorm end if if ( a(k,k) /= 0.0D+00 ) then z(k) = z(k) / a(k,k) else z(k) = 1.0D+00 end if z(1:k-1) = z(1:k-1) - z(k) * a(1:k-1,k) end do ! ! Normalize Z in the L1 norm. ! s = 1.0D+00 / sum ( abs ( z(1:n) ) ) z(1:n) = s * z(1:n) ynorm = s * ynorm if ( anorm /= 0.0D+00 ) then rcond = ynorm / anorm else rcond = 0.0D+00 end if return end subroutine r8ge_det ( n, a_lu, pivot, det ) !*****************************************************************************80 ! !! R8GE_DET: determinant of a matrix factored by R8GE_FA or R8GE_TRF. ! ! Discussion: ! ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! R8GE storage is used by LINPACK and LAPACK. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 29 March 2003 ! ! Author: ! ! John Burkardt ! ! Reference: ! ! Jack Dongarra, Jim Bunch, Cleve Moler, Pete Stewart, ! LINPACK User's Guide, ! SIAM, 1979, ! ISBN13: 978-0-898711-72-1, ! LC: QA214.L56. ! ! Parameters: ! ! Input, integer N, the order of the matrix. ! N must be positive. ! ! Input, real ( kind = rk ) A_LU(N,N), the LU factors from R8GE_FA ! or R8GE_TRF. ! ! Input, integer PIVOT(N), as computed by R8GE_FA or R8GE_TRF. ! ! Output, real ( kind = rk ) DET, the determinant of the matrix. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n real ( kind = rk ) a_lu(n,n) real ( kind = rk ) det integer i integer pivot(n) det = 1.0D+00 do i = 1, n det = det * a_lu(i,i) if ( pivot(i) /= i ) then det = - det end if end do return end subroutine r8ge_dif2 ( m, n, a ) !*****************************************************************************80 ! !! R8GE_DIF2 returns the DIF2 matrix in R8GE format. ! ! Example: ! ! N = 5 ! ! 2 -1 . . . ! -1 2 -1 . . ! . -1 2 -1 . ! . . -1 2 -1 ! . . . -1 2 ! ! Properties: ! ! A is banded, with bandwidth 3. ! ! A is tridiagonal. ! ! Because A is tridiagonal, it has property A (bipartite). ! ! A is a special case of the TRIS or tridiagonal scalar matrix. ! ! A is integral, therefore det ( A ) is integral, and ! det ( A ) * inverse ( A ) is integral. ! ! A is Toeplitz: constant along diagonals. ! ! A is symmetric: A' = A. ! ! Because A is symmetric, it is normal. ! ! Because A is normal, it is diagonalizable. ! ! A is persymmetric: A(I,J) = A(N+1-J,N+1-I). ! ! A is positive definite. ! ! A is an M matrix. ! ! A is weakly diagonally dominant, but not strictly diagonally dominant. ! ! A has an LU factorization A = L * U, without pivoting. ! ! The matrix L is lower bidiagonal with subdiagonal elements: ! ! L(I+1,I) = -I/(I+1) ! ! The matrix U is upper bidiagonal, with diagonal elements ! ! U(I,I) = (I+1)/I ! ! and superdiagonal elements which are all -1. ! ! A has a Cholesky factorization A = L * L', with L lower bidiagonal. ! ! L(I,I) = sqrt ( (I+1) / I ) ! L(I,I-1) = -sqrt ( (I-1) / I ) ! ! The eigenvalues are ! ! LAMBDA(I) = 2 + 2 * COS(I*PI/(N+1)) ! = 4 SIN^2(I*PI/(2*N+2)) ! ! The corresponding eigenvector X(I) has entries ! ! X(I)(J) = sqrt(2/(N+1)) * sin ( I*J*PI/(N+1) ). ! ! Simple linear systems: ! ! x = (1,1,1,...,1,1), A*x=(1,0,0,...,0,1) ! ! x = (1,2,3,...,n-1,n), A*x=(0,0,0,...,0,n+1) ! ! det ( A ) = N + 1. ! ! The value of the determinant can be seen by induction, ! and expanding the determinant across the first row: ! ! det ( A(N) ) = 2 * det ( A(N-1) ) - (-1) * (-1) * det ( A(N-2) ) ! = 2 * N - (N-1) ! = N + 1 ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 01 July 2000 ! ! Author: ! ! John Burkardt ! ! Reference: ! ! Robert Gregory, David Karney, ! A Collection of Matrices for Testing Computational Algorithms, ! Wiley, 1969, ! ISBN: 0882756494, ! LC: QA263.68 ! ! Morris Newman, John Todd, ! Example A8, ! The evaluation of matrix inversion programs, ! Journal of the Society for Industrial and Applied Mathematics, ! Volume 6, Number 4, pages 466-476, 1958. ! ! John Todd, ! Basic Numerical Mathematics, ! Volume 2: Numerical Algebra, ! Birkhauser, 1980, ! ISBN: 0817608117, ! LC: QA297.T58. ! ! Joan Westlake, ! A Handbook of Numerical Matrix Inversion and Solution of ! Linear Equations, ! John Wiley, 1968, ! ISBN13: 978-0471936756, ! LC: QA263.W47. ! ! Parameters: ! ! Input, integer M, N, the order of the matrix. ! ! Output, real ( kind = rk ) A(M,N), the matrix. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer m integer n real ( kind = rk ) a(m,n) integer i integer j do j = 1, n do i = 1, m if ( j == i - 1 ) then a(i,j) = -1.0D+00 else if ( j == i ) then a(i,j) = 2.0D+00 else if ( j == i + 1 ) then a(i,j) = -1.0D+00 else a(i,j) = 0.0D+00 end if end do end do return end subroutine r8ge_dilu ( m, n, a, d ) !*****************************************************************************80 ! !! R8GE_DILU produces the diagonal incomplete LU factor of an R8GE matrix. ! ! Discussion: ! ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! R8GE storage is used by LINPACK and LAPACK. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 15 October 2003 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, the number of rows in A. ! ! Input, integer N, the number of columns in A. ! ! Input, real ( kind = rk ) A(M,N), the R8GE matrix. ! ! Output, real ( kind = rk ) D(M), the DILU factor. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer m integer n real ( kind = rk ) a(m,n) real ( kind = rk ) d(m) integer i integer j do i = 1, m if ( i <= n ) then d(i) = a(i,i) else d(i) = 0.0D+00 end if end do do i = 1, min ( m, n ) d(i) = 1.0D+00 / d(i) do j = i + 1, min ( m, n ) d(j) = d(j) - a(j,i) * d(i) * a(i,j) end do end do return end subroutine r8ge_fa ( n, a, pivot, info ) !*****************************************************************************80 ! !! R8GE_FA performs a LINPACK style PLU factorization of an R8GE matrix. ! ! Discussion: ! ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! R8GE storage is used by LINPACK and LAPACK. ! ! R8GE_FA is a simplified version of the LINPACK routine SGEFA. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 26 February 2001 ! ! Author: ! ! John Burkardt ! ! Reference: ! ! Jack Dongarra, Jim Bunch, Cleve Moler, Pete Stewart, ! LINPACK User's Guide, ! SIAM, 1979, ! ISBN13: 978-0-898711-72-1, ! LC: QA214.L56. ! ! Parameters: ! ! Input, integer N, the order of the matrix. ! N must be positive. ! ! Input/output, real ( kind = rk ) A(N,N), the matrix to be factored. ! On output, A contains an upper triangular matrix and the multipliers ! which were used to obtain it. The factorization can be written ! A = L * U, where L is a product of permutation and unit lower ! triangular matrices and U is upper triangular. ! ! Output, integer PIVOT(N), a vector of pivot indices. ! ! Output, integer INFO, singularity flag. ! 0, no singularity detected. ! nonzero, the factorization failed on the INFO-th step. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n real ( kind = rk ) a(n,n) integer i integer info integer pivot(n) integer j integer k integer l real ( kind = rk ) t info = 0 do k = 1, n - 1 ! ! Find L, the index of the pivot row. ! l = k do i = k + 1, n if ( abs ( a(l,k) ) < abs ( a(i,k) ) ) then l = i end if end do pivot(k) = l ! ! If the pivot index is zero, the algorithm has failed. ! if ( a(l,k) == 0.0D+00 ) then info = k write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'R8GE_FA - Fatal error!' write ( *, '(a,i8)' ) ' Zero pivot on step ', info stop 1 end if ! ! Interchange rows L and K if necessary. ! if ( l /= k ) then t = a(l,k) a(l,k) = a(k,k) a(k,k) = t end if ! ! Normalize the values that lie below the pivot entry A(K,K). ! a(k+1:n,k) = -a(k+1:n,k) / a(k,k) ! ! Row elimination with column indexing. ! do j = k + 1, n if ( l /= k ) then t = a(l,j) a(l,j) = a(k,j) a(k,j) = t end if a(k+1:n,j) = a(k+1:n,j) + a(k+1:n,k) * a(k,j) end do end do pivot(n) = n if ( a(n,n) == 0.0D+00 ) then info = n write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'R8GE_FA - Fatal error!' write ( *, '(a,i8)' ) ' Zero pivot on step ', info stop 1 end if return end subroutine r8ge_fs ( n, a, b, info ) !*****************************************************************************80 ! !! R8GE_FS factors and solves an R8GE system. ! ! Discussion: ! ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! R8GE storage is used by LINPACK and LAPACK. ! ! R8GE_FS does not save the LU factors of the matrix, and hence cannot ! be used to efficiently solve multiple linear systems, or even to ! factor A at one time, and solve a single linear system at a later time. ! ! R8GE_FS uses partial pivoting, but no pivot vector is required. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 04 March 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N, the order of the matrix. ! N must be positive. ! ! Input/output, real ( kind = rk ) A(N,N). ! On input, A is the coefficient matrix of the linear system. ! On output, A is in unit upper triangular form, and ! represents the U factor of an LU factorization of the ! original coefficient matrix. ! ! Input/output, real ( kind = rk ) B(N). ! On input, B is the right hand side of the linear system. ! On output, B is the solution of the linear system. ! ! Output, integer INFO, singularity flag. ! 0, no singularity detected. ! nonzero, the factorization failed on the INFO-th step. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n real ( kind = rk ) a(n,n) real ( kind = rk ) b(n) integer i integer info integer ipiv integer jcol real ( kind = rk ) piv real ( kind = rk ) row(n) real ( kind = rk ) temp info = 0 do jcol = 1, n ! ! Find the maximum element in column I. ! piv = abs ( a(jcol,jcol) ) ipiv = jcol do i = jcol + 1, n if ( piv < abs ( a(i,jcol) ) ) then piv = abs ( a(i,jcol) ) ipiv = i end if end do if ( piv == 0.0D+00 ) then info = jcol write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'R8GE_FS - Fatal error!' write ( *, '(a,i8)' ) ' Zero pivot on step ', info stop 1 end if ! ! Switch rows JCOL and IPIV, and B. ! if ( jcol /= ipiv ) then row(1:n) = a(jcol,1:n) a(jcol,1:n) = a(ipiv,1:n) a(ipiv,1:n) = row(1:n) temp = b(jcol) b(jcol) = b(ipiv) b(ipiv) = temp end if ! ! Scale the pivot row. ! a(jcol,jcol+1:n) = a(jcol,jcol+1:n) / a(jcol,jcol) b(jcol) = b(jcol) / a(jcol,jcol) a(jcol,jcol) = 1.0D+00 ! ! Use the pivot row to eliminate lower entries in that column. ! do i = jcol + 1, n if ( a(i,jcol) /= 0.0D+00 ) then temp = - a(i,jcol) a(i,jcol) = 0.0D+00 a(i,jcol+1:n) = a(i,jcol+1:n) + temp * a(jcol,jcol+1:n) b(i) = b(i) + temp * b(jcol) end if end do end do ! ! Back solve. ! do jcol = n, 2, -1 b(1:jcol-1) = b(1:jcol-1) - a(1:jcol-1,jcol) * b(jcol) end do return end subroutine r8ge_fss ( n, a, nb, b, info ) !*****************************************************************************80 ! !! R8GE_FSS factors and solves multiple R8GE systems. ! ! Discussion: ! ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! R8GE storage is used by LINPACK and LAPACK. ! ! This routine does not save the LU factors of the matrix, and hence cannot ! be used to efficiently solve multiple linear systems, or even to ! factor A at one time, and solve a single linear system at a later time. ! ! This routine uses partial pivoting, but no pivot vector is required. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 23 June 2009 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N, the order of the matrix. ! N must be positive. ! ! Input/output, real ( kind = rk ) A(N,N). ! On input, A is the coefficient matrix of the linear system. ! On output, A is in unit upper triangular form, and ! represents the U factor of an LU factorization of the ! original coefficient matrix. ! ! Input, integer NB, the number of right hand sides. ! ! Input/output, real ( kind = rk ) B(N,NB). ! On input, the right hand sides of the linear system. ! On output, the solutions of the linear systems. ! ! Output, integer INFO, singularity flag. ! 0, no singularity detected. ! nonzero, the factorization failed on the INFO-th step. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n integer nb real ( kind = rk ) a(n,n) real ( kind = rk ) b(n,nb) integer i integer info integer ipiv integer j integer jcol real ( kind = rk ) piv real ( kind = rk ) row(n) real ( kind = rk ) t(nb) real ( kind = rk ) temp info = 0 do jcol = 1, n ! ! Find the maximum element in column I. ! piv = abs ( a(jcol,jcol) ) ipiv = jcol do i = jcol + 1, n if ( piv < abs ( a(i,jcol) ) ) then piv = abs ( a(i,jcol) ) ipiv = i end if end do if ( piv == 0.0D+00 ) then info = jcol write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'R8GE_FSS - Fatal error!' write ( *, '(a,i8)' ) ' Zero pivot on step ', info stop 1 end if ! ! Switch rows JCOL and IPIV, and B. ! if ( jcol /= ipiv ) then row(1:n) = a(jcol,1:n) a(jcol,1:n) = a(ipiv,1:n) a(ipiv,1:n) = row(1:n) t(1:nb) = b(jcol,1:nb) b(jcol,1:nb) = b(ipiv,1:nb) b(ipiv,1:nb) = t(1:nb) end if ! ! Scale the pivot row. ! a(jcol,jcol+1:n) = a(jcol,jcol+1:n) / a(jcol,jcol) b(jcol,1:nb) = b(jcol,1:nb) / a(jcol,jcol) a(jcol,jcol) = 1.0D+00 ! ! Use the pivot row to eliminate lower entries in that column. ! do i = jcol + 1, n if ( a(i,jcol) /= 0.0D+00 ) then temp = - a(i,jcol) a(i,jcol) = 0.0D+00 a(i,jcol+1:n) = a(i,jcol+1:n) + temp * a(jcol,jcol+1:n) b(i,1:nb) = b(i,1:nb) + temp * b(jcol,1:nb) end if end do end do ! ! Back solve. ! do j = 1, nb do jcol = n, 2, -1 b(1:jcol-1,j) = b(1:jcol-1,j) - a(1:jcol-1,jcol) * b(jcol,j) end do end do return end subroutine r8ge_hilbert ( m, n, a ) !*****************************************************************************80 ! !! R8GE_HILBERT returns the Hilbert matrix. ! ! Formula: ! ! A(I,J) = 1 / ( I + J - 1 ) ! ! Example: ! ! N = 5 ! ! 1/1 1/2 1/3 1/4 1/5 ! 1/2 1/3 1/4 1/5 1/6 ! 1/3 1/4 1/5 1/6 1/7 ! 1/4 1/5 1/6 1/7 1/8 ! 1/5 1/6 1/7 1/8 1/9 ! ! Properties: ! ! A is a Hankel matrix: constant along anti-diagonals. ! ! A is positive definite. ! ! A is symmetric: A' = A. ! ! Because A is symmetric, it is normal. ! ! Because A is normal, it is diagonalizable. ! ! A is totally positive. ! ! A is a Cauchy matrix. ! ! A is nonsingular. ! ! A is very ill-conditioned. ! ! The entries of the inverse of A are all integers. ! ! The sum of the entries of the inverse of A is N*N. ! ! The ratio of the absolute values of the maximum and minimum ! eigenvalues is roughly EXP(3.5*N). ! ! The determinant of the Hilbert matrix of order 10 is ! 2.16417... * 10^(-53). ! ! If the (1,1) entry of the 5 by 5 Hilbert matrix is changed ! from 1 to 24/25, the matrix is exactly singular. And there ! is a similar rule for larger Hilbert matrices. ! ! The family of matrices is nested as a function of N. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 01 July 2000 ! ! Author: ! ! John Burkardt ! ! Reference: ! ! MD Choi, ! Tricks or treats with the Hilbert matrix, ! American Mathematical Monthly, ! Volume 90, 1983, pages 301-312. ! ! Robert Gregory, David Karney, ! A Collection of Matrices for Testing Computational Algorithms, ! Wiley, 1969, ! ISBN: 0882756494, ! LC: QA263.G68. ! ! Nicholas Higham, ! Accuracy and Stability of Numerical Algorithms, ! Society for Industrial and Applied Mathematics, Philadelphia, PA, ! USA, 1996; section 26.1. ! ! Donald Knuth, ! The Art of Computer Programming, ! Volume 1, Fundamental Algorithms, Second Edition ! Addison-Wesley, Reading, Massachusetts, 1973, page 37. ! ! Morris Newman, John Todd, ! Example A13, ! The evaluation of matrix inversion programs, ! Journal of the Society for Industrial and Applied Mathematics, ! Volume 6, 1958, pages 466-476. ! ! Joan Westlake, ! A Handbook of Numerical Matrix Inversion and Solution of ! Linear Equations, ! John Wiley, 1968, ! ISBN13: 978-0471936756, ! LC: QA263.W47. ! ! Parameters: ! ! Input, integer M, N, the order of the matrix. ! ! Output, real ( kind = rk ) A(M,N), the matrix. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer m integer n real ( kind = rk ) a(m,n) integer i integer j do j = 1, n do i = 1, m a(i,j) = 1.0D+00 / real ( i + j - 1, kind = rk ) end do end do return end subroutine r8ge_hilbert_inverse ( n, a ) !*****************************************************************************80 ! !! R8GE_HILBERT_INVERSE returns the inverse of the Hilbert matrix. ! ! Formula: ! ! A(I,J) = (-1)^(I+J) * (N+I-1)! * (N+J-1)! / ! [ (I+J-1) * ((I-1)! * (J-1)!)^2 * (N-I)! * (N-J)! ] ! ! Example: ! ! N = 5 ! ! 25 -300 1050 -1400 630 ! -300 4800 -18900 26880 -12600 ! 1050 -18900 79380 -117600 56700 ! -1400 26880 -117600 179200 -88200 ! 630 -12600 56700 -88200 44100 ! ! Properties: ! ! A is symmetric. ! ! Because A is symmetric, it is normal, so diagonalizable. ! ! A is almost impossible to compute accurately by general routines ! that compute the inverse. ! ! A is integral. ! ! The sum of the entries of A is N^2. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 03 May 2003 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N, the order of A. ! ! Output, real ( kind = rk ) A(N,N), the inverse Hilbert matrix. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n real ( kind = rk ) a(n,n) integer i integer j ! ! Set the (1,1) entry. ! a(1,1) = real ( n * n, kind = rk ) ! ! Define Row 1, Column J by recursion on Row 1 Column J-1 ! i = 1 do j = 2, n a(i,j) = - a(i,j-1) & * real ( ( n + j - 1 ) * ( i + j - 2 ) * ( n + 1 - j ), kind = rk ) & / real ( ( i + j - 1 ) * ( j - 1 ) * ( j - 1 ), kind = rk ) end do ! ! Define Row I by recursion on row I-1 ! do i = 2, n do j = 1, n a(i,j) = - a(i-1,j) & * real ( (n+i-1) * (i+j-2) * (n+1-i), kind = rk ) & / real ( (i+j-1) * ( i - 1 ) * ( i - 1 ), kind = rk ) end do end do return end subroutine r8ge_identity ( m, n, a ) !*****************************************************************************80 ! !! R8GE_IDENTITY copies the identity matrix to an R8GE matrix. ! ! Discussion: ! ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! R8GE storage is used by LINPACK and LAPACK. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 15 March 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, N, the order of A. ! ! Output, real ( kind = rk ) A(M,N), the identity matrix. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer m integer n real ( kind = rk ) a(m,n) integer i a(1:m,1:n) = 0.0D+00 do i = 1, min ( m, n ) a(i,i) = 1.0D+00 end do return end subroutine r8ge_ilu ( m, n, a, l, u ) !*****************************************************************************80 ! !! R8GE_ILU produces the incomplete LU factors of an R8GE matrix. ! ! Discussion: ! ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! R8GE storage is used by LINPACK and LAPACK. ! ! The incomplete LU factors of the M by N matrix A are: ! ! L, an M by M unit lower triangular matrix, ! U, an M by N upper triangular matrix ! ! with the property that L and U are computed in the same way as ! the usual LU factors, except that, whenever an off diagonal element ! of the original matrix is zero, then the corresponding value of ! U is forced to be zero. ! ! This condition means that it is no longer the case that A = L*U. ! ! On the other hand, L and U will have a simple sparsity structure ! related to that of A. The incomplete LU factorization is generally ! used as a preconditioner in iterative schemes applied to sparse ! matrices. It is presented here merely for illustration. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 07 May 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, the number of rows in A. ! ! Input, integer N, the number of columns in A. ! ! Input, real ( kind = rk ) A(M,N), the R8GE matrix. ! ! Output, real ( kind = rk ) L(M,M), the M by M unit lower triangular factor. ! ! Output, real ( kind = rk ) U(M,N), the M by N upper triangular factor. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer m integer n real ( kind = rk ) a(m,n) integer i integer j integer k real ( kind = rk ) l(m,m) real ( kind = rk ) u(m,n) ! ! Initialize: ! ! L := M by M Identity ! U := A ! call r8ge_identity ( m, m, l ) u(1:m,1:n) = a(1:m,1:n) do j = 1, min ( m - 1, n ) ! ! Zero out the entries in column J, from row J+1 to M. ! do i = j + 1, m if ( u(i,j) /= 0.0D+00 ) then l(i,j) = u(i,j) / u(j,j) u(i,j) = 0.0D+00 do k = j+1, n if ( u(i,k) /= 0.0D+00 ) then u(i,k) = u(i,k) - l(i,j) * u(j,k) end if end do end if end do end do return end subroutine r8ge_indicator ( m, n, a ) !*****************************************************************************80 ! !! R8GE_INDICATOR sets up an R8GE indicator matrix. ! ! Discussion: ! ! The "indicator matrix" simply has a value like I*10+J at every ! entry of a dense matrix, or at every entry of a compressed storage ! matrix for which storage is allocated. ! ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! R8GE storage is used by LINPACK and LAPACK. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 11 January 2004 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, the number of rows of the matrix. ! M must be positive. ! ! Input, integer N, the number of columns of the matrix. ! N must be positive. ! ! Output, real ( kind = rk ) A(M,N), the R8GE matrix. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer m integer n real ( kind = rk ) a(m,n) integer fac integer i integer i4_log_10 integer j fac = 10 ** ( i4_log_10 ( n ) + 1 ) do i = 1, m do j = 1, n a(i,j) = real ( fac * i + j, kind = rk ) end do end do return end subroutine r8ge_inverse ( n, a, pivot ) !*****************************************************************************80 ! !! R8GE_INVERSE computes the inverse of a matrix factored by R8GE_FA. ! ! Discussion: ! ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! R8GE storage is used by LINPACK and LAPACK. ! ! R8GE_INVERSE is a simplified standalone version of the LINPACK routine ! SGEDI. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 16 September 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N, the order of the matrix A. ! ! Input/output, real ( kind = rk ) A(N,N). ! On input, the factor information computed by R8GE_FA. ! On output, the inverse matrix. ! ! Input, integer PIVOT(N), the pivot vector from R8GE_FA. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n real ( kind = rk ) a(n,n) integer i integer pivot(n) integer j integer k real ( kind = rk ) t real ( kind = rk ) work(n) ! ! Compute Inverse(U). ! do k = 1, n a(k,k) = 1.0D+00 / a(k,k) a(1:k-1,k) = -a(1:k-1,k) * a(k,k) do j = k + 1, n t = a(k,j) a(k,j) = 0.0D+00 a(1:k,j) = a(1:k,j) + a(1:k,k) * t end do end do ! ! Form Inverse(U) * Inverse(L). ! do k = n - 1, 1, -1 work(k+1:n) = a(k+1:n,k) a(k+1:n,k) = 0.0D+00 do j = k + 1, n a(1:n,k) = a(1:n,k) + a(1:n,j) * work(j) end do if ( pivot(k) /= k ) then do i = 1, n t = a(i,k) a(i,k) = a(i,pivot(k)) a(i,pivot(k)) = t end do end if end do return end subroutine r8ge_ml ( n, a_lu, pivot, x, b, job ) !*****************************************************************************80 ! !! R8GE_ML computes A * x or A' * x, using R8GE_FA factors. ! ! Discussion: ! ! The R8GE storage format is used for a general M by N matrix. A storage ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! R8GE storage is used by LINPACK and LAPACK. ! ! It is assumed that R8GE_FA has overwritten the original matrix ! information by LU factors. R8GE_ML is able to reconstruct the ! original matrix from the LU factor data. ! ! R8GE_ML allows the user to check that the solution of a linear ! system is correct, without having to save an unfactored copy ! of the matrix. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 31 October 1998 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N, the order of the matrix. ! N must be positive. ! ! Input, real ( kind = rk ) A_LU(N,N), the LU factors from R8GE_FA. ! ! Input, integer PIVOT(N), the pivot vector computed by R8GE_FA. ! ! Input, real ( kind = rk ) X(N), the vector to be multiplied. ! ! Output, real ( kind = rk ) B(N), the result of the multiplication. ! ! Input, integer JOB, specifies the operation to be done: ! JOB = 0, compute A * x. ! JOB nonzero, compute A' * X. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n real ( kind = rk ) a_lu(n,n) real ( kind = rk ) b(n) integer i integer pivot(n) integer j integer job integer k real ( kind = rk ) t real ( kind = rk ) x(n) b(1:n) = x(1:n) if ( job == 0 ) then ! ! Y = U * X. ! do j = 1, n b(1:j-1) = b(1:j-1) + a_lu(1:j-1,j) * b(j) b(j) = a_lu(j,j) * b(j) end do ! ! B = PL * Y = PL * U * X = A * x. ! do j = n-1, 1, -1 b(j+1:n) = b(j+1:n) - a_lu(j+1:n,j) * b(j) k = pivot(j) if ( k /= j ) then t = b(k) b(k) = b(j) b(j) = t end if end do else ! ! Y = (PL)' * X: ! do j = 1, n - 1 k = pivot(j) if ( k /= j ) then t = b(k) b(k) = b(j) b(j) = t end if b(j) = b(j) - sum ( b(j+1:n) * a_lu(j+1:n,j) ) end do ! ! B = U' * Y = ( PL * U )' * X = A' * X. ! do i = n, 1, -1 b(i+1:n) = b(i+1:n) + b(i) * a_lu(i,i+1:n) b(i) = b(i) * a_lu(i,i) end do end if return end subroutine r8ge_mm ( n1, n2, n3, a, b, c ) !*****************************************************************************80 ! !! R8GE_MM multiplies two R8GE matrices. ! ! Discussion: ! ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! R8GE storage is used by LINPACK and LAPACK. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 12 September 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N1, N2, N3, the order of the matrices. ! N must be positive. ! ! Input, real ( kind = rk ) A(N1,N2), B(N2,N3), the factors. ! ! Output, real ( kind = rk ) C(N1,N3), the product. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n1 integer n2 integer n3 real ( kind = rk ) a(n1,n2) real ( kind = rk ) b(n2,n3) real ( kind = rk ) c(n1,n3) c(1:n1,1:n3) = matmul ( a(1:n1,1:n2), b(1:n2,1:n3) ) return end subroutine r8ge_mtm ( n1, n2, n3, a, b, c ) !*****************************************************************************80 ! !! R8GE_MTM computes the product C=A'*B for R8GE matrices. ! ! Discussion: ! ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! R8GE storage is used by LINPACK and LAPACK. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 02 February 2016 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N1, N2, N3, the order of the matrices. ! N must be positive. ! ! Input, real ( kind = rk ) A(N2,N1), B(N2,N3), the factors. ! ! Output, real ( kind = rk ) C(N1,N3), the product. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n1 integer n2 integer n3 real ( kind = rk ) a(n2,n1) real ( kind = rk ) b(n2,n3) real ( kind = rk ) c(n1,n3) c(1:n1,1:n3) = matmul ( transpose ( a(1:n2,1:n1) ), b(1:n2,1:n3) ) return end subroutine r8ge_mtv ( m, n, a, x, b ) !*****************************************************************************80 ! !! R8GE_MTV multiplies an R8VEC by an R8GE matrix. ! ! Discussion: ! ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! R8GE storage is used by LINPACK and LAPACK. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 11 January 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, the number of rows of the matrix. ! M must be positive. ! ! Input, integer N, the number of columns of the matrix. ! N must be positive. ! ! Input, real ( kind = rk ) A(M,N), the R8GE matrix. ! ! Input, real ( kind = rk ) X(M), the vector to be multiplied by A. ! ! Output, real ( kind = rk ) B(N), the product A' * x. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer m integer n real ( kind = rk ) a(m,n) real ( kind = rk ) b(n) real ( kind = rk ) x(m) b(1:n) = matmul ( transpose ( a(1:m,1:n) ), x(1:m) ) return end subroutine r8ge_mu ( m, n, a_lu, trans, pivot, x, b ) !*****************************************************************************80 ! !! R8GE_MU computes A * x or A' * x, using R8GE_TRF factors. ! ! Discussion: ! ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! R8GE storage is used by LINPACK and LAPACK. ! ! It is assumed that R8GE_TRF has overwritten the original matrix ! information by PLU factors. R8GE_MU is able to reconstruct the ! original matrix from the PLU factor data. ! ! R8GE_MU allows the user to check that the solution of a linear ! system is correct, without having to save an unfactored copy ! of the matrix. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 14 January 1999 ! ! Author: ! ! John Burkardt ! ! Reference: ! ! Edward Anderson, Zhaojun Bai, Christian Bischof, Susan Blackford, ! James Demmel, Jack Dongarra, Jeremy Du Croz, Anne Greenbaum, ! Sven Hammarling, Alan McKenney, Danny Sorensen, ! LAPACK User's Guide, ! Second Edition, ! SIAM, 1995. ! ! Parameters: ! ! Input, integer M, the number of rows in the matrix. ! ! Input, integer N, the number of columns in the matrix. ! ! Input, real ( kind = rk ) A_LU(M,N), the LU factors from R8GE_TRF. ! ! Input, character TRANS, specifies the form of the system of equations: ! 'N': A * x = b (No transpose) ! 'T': A'* X = B (Transpose) ! 'C': A'* X = B (Conjugate transpose = Transpose) ! ! Input, integer PIVOT(*), the pivot vector computed ! by R8GE_TRF. ! ! Input, real ( kind = rk ) X(*), the vector to be multiplied. ! For the untransposed case, X should have N entries. ! For the transposed case, X should have M entries. ! ! Output, real ( kind = rk ) B(*), the result of the multiplication. ! For the untransposed case, B should have M entries. ! For the transposed case, B should have N entries. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer m integer n real ( kind = rk ) a_lu(m,n) real ( kind = rk ) b(*) integer i integer pivot(*) integer j integer k integer mn_max integer npiv real ( kind = rk ) t character trans real ( kind = rk ) x(*) real ( kind = rk ), allocatable, dimension ( : ) :: y npiv = min ( m - 1, n ) mn_max = max ( m, n ) allocate ( y(1:mn_max) ) if ( trans == 'n' .or. trans == 'N' ) then ! ! Y[MN] = U[MNxN] * X[N]. ! y(1:n) = 0.0D+00 do j = 1, n do i = 1, min ( j, m ) y(i) = y(i) + a_lu(i,j) * x(j) end do end do ! ! Z[M] = L[MxMN] * Y[MN] = L[MxMN] * U[MNxN] * X[N]. ! do i = 1, m if ( i <= n ) then b(i) = y(i) else b(i) = 0.0D+00 end if end do do j = min ( m-1, n ), 1, -1 b(j+1:m) = b(j+1:m) + a_lu(j+1:m,j) * y(j) end do ! ! B = P * Z = P * L * Y = P * L * U * X = A * x. ! do j = npiv, 1, -1 k = pivot(j) if ( k /= j ) then t = b(k) b(k) = b(j) b(j) = t end if end do else if ( trans == 't' .or. trans == 'T' .or. & trans == 'c' .or. trans == 'C' ) then ! ! Y = tranpose(P) * X: ! do i = 1, npiv k = pivot(i) if ( k /= i ) then t = x(k) x(k) = x(i) x(i) = t end if end do do i = 1, n if ( i <= m ) then b(i) = x(i) else b(i) = 0.0D+00 end if end do ! ! Z = tranpose(L) * Y: ! do j = 1, min ( m - 1, n ) b(j) = b(j) + sum ( x(j+1:m) * a_lu(j+1:m,j) ) end do ! ! B = U' * Z. ! do i = m, 1, -1 b(i+1:n) = b(i+1:n) + b(i) * a_lu(i,i+1:n) if ( i <= n ) then b(i) = b(i) * a_lu(i,i) end if end do ! ! Now restore X. ! do i = npiv, 1, -1 k = pivot(i) if ( k /= i ) then t = x(k) x(k) = x(i) x(i) = t end if end do else write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'R8GE_MU - Fatal error!' write ( *, '(a)' ) ' Illegal value of TRANS = ' // trans stop 1 end if deallocate ( y ) return end subroutine r8ge_mv ( m, n, a, x, b ) !*****************************************************************************80 ! !! R8GE_MV multiplies an R8GE matrix by an R8VEC. ! ! Discussion: ! ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! R8GE storage is used by LINPACK and LAPACK. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 11 January 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, the number of rows of the matrix. ! M must be positive. ! ! Input, integer N, the number of columns of the matrix. ! N must be positive. ! ! Input, real ( kind = rk ) A(M,N), the R8GE matrix. ! ! Input, real ( kind = rk ) X(N), the vector to be multiplied by A. ! ! Output, real ( kind = rk ) B(M), the product A * x. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer m integer n real ( kind = rk ) a(m,n) real ( kind = rk ) b(m) real ( kind = rk ) x(n) b(1:m) = matmul ( a(1:m,1:n), x(1:n) ) return end subroutine r8ge_plu ( m, n, a, p, l, u ) !*****************************************************************************80 ! !! R8GE_PLU produces the PLU factors of an R8GE matrix. ! ! Discussion: ! ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! R8GE storage is used by LINPACK and LAPACK. ! ! The PLU factors of the M by N matrix A are: ! ! P, an M by M permutation matrix P, ! L, an M by M unit lower triangular matrix, ! U, an M by N upper triangular matrix. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 30 April 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, the number of rows in A. ! ! Input, integer N, the number of columns in A. ! ! Input, real ( kind = rk ) A(M,N), the R8GE matrix. ! ! Output, real ( kind = rk ) P(M,M), the M by M permutation factor. ! ! Output, real ( kind = rk ) L(M,M), the M by M unit lower triangular factor. ! ! Output, real ( kind = rk ) U(M,N), the M by N upper triangular factor. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer m integer n real ( kind = rk ) a(m,n) integer i integer j real ( kind = rk ) l(m,m) real ( kind = rk ) p(m,m) integer pivot_row real ( kind = rk ) pivot_value real ( kind = rk ) u(m,n) ! ! Initialize: ! ! P: = M by M Identity ! L: = M by M Identity ! U: = A ! call r8ge_identity ( m, m, l ) call r8ge_identity ( m, m, p ) u(1:m,1:n) = a(1:m,1:n) ! ! On step J, find the pivot row and the pivot value. ! do j = 1, min ( m-1, n ) pivot_value = 0.0D+00 pivot_row = -1 do i = j, m if ( pivot_value < abs ( u(i,j) ) ) then pivot_value = abs ( u(i,j) ) pivot_row = i end if end do ! ! If the pivot row is nonzero, swap rows J and PIVOT_ROW. ! if ( pivot_row /= -1 ) then call r8row_swap ( m, n, u, j, pivot_row ) call r8row_swap ( m, m, l, j, pivot_row ) call r8col_swap ( m, m, l, j, pivot_row ) call r8col_swap ( m, m, p, j, pivot_row ) ! ! Zero out the entries in column J, from row J+1 to M. ! do i = j + 1, m if ( u(i,j) /= 0.0D+00 ) then l(i,j) = u(i,j) / u(j,j) u(i,j) = 0.0D+00 u(i,j+1:n) = u(i,j+1:n) - l(i,j) * u(j,j+1:n) end if end do end if end do return end subroutine r8ge_poly ( n, a, p ) !*****************************************************************************80 ! !! R8GE_POLY computes the characteristic polynomial of an R8GE matrix. ! ! Discussion: ! ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! R8GE storage is used by LINPACK and LAPACK. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 04 December 1998 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N, the order of the matrix. ! N must be positive. ! ! Input, real ( kind = rk ) A(N,N), the R8GE matrix. ! ! Output, real ( kind = rk ) P(0:N), the coefficients of the characteristic ! polynomial of A. P(I) contains the coefficient of X**I. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n real ( kind = rk ) a(n,n) integer i integer order real ( kind = rk ) p(0:n) real ( kind = rk ) trace real ( kind = rk ) work1(n,n) real ( kind = rk ) work2(n,n) ! ! Initialize WORK1 to the identity matrix. ! call r8ge_identity ( n, n, work1 ) p(n) = 1.0D+00 do order = n-1, 0, -1 ! ! Work2 = A * WORK1. ! work2(1:n,1:n) = matmul ( a(1:n,1:n), work1(1:n,1:n) ) ! ! Take the trace. ! trace = 0.0D+00 do i = 1, n trace = trace + work2(i,i) end do ! ! P(ORDER) = - Trace ( WORK2 ) / ( N - ORDER ) ! p(order) = - trace / real ( n - order, kind = rk ) ! ! WORK1 := WORK2 + P(ORDER) * Identity. ! work1(1:n,1:n) = work2(1:n,1:n) do i = 1, n work1(i,i) = work1(i,i) + p(order) end do end do return end subroutine r8ge_print ( m, n, a, title ) !*****************************************************************************80 ! !! R8GE_PRINT prints an R8GE matrix. ! ! Discussion: ! ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! R8GE storage is used by LINPACK and LAPACK. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 07 May 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, the number of rows of the matrix. ! M must be positive. ! ! Input, integer N, the number of columns of the matrix. ! N must be positive. ! ! Input, real ( kind = rk ) A(M,N), the R8GE matrix. ! ! Input, character ( len = * ) TITLE, a title. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer m integer n real ( kind = rk ) a(m,n) character ( len = * ) title call r8ge_print_some ( m, n, a, 1, 1, m, n, title ) return end subroutine r8ge_print_some ( m, n, a, ilo, jlo, ihi, jhi, title ) !*****************************************************************************80 ! !! R8GE_PRINT_SOME prints some of an R8GE matrix. ! ! Discussion: ! ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! R8GE storage is used by LINPACK and LAPACK. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 21 March 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, the number of rows of the matrix. ! M must be positive. ! ! Input, integer N, the number of columns of the matrix. ! N must be positive. ! ! Input, real ( kind = rk ) A(M,N), the R8GE matrix. ! ! Input, integer ILO, JLO, IHI, JHI, the first row and ! column, and the last row and column to be printed. ! ! Input, character ( len = * ) TITLE, a title. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer, parameter :: incx = 5 integer m integer n real ( kind = rk ) a(m,n) character ( len = 14 ) ctemp(incx) integer i integer i2hi integer i2lo integer ihi integer ilo integer inc integer j integer j2 integer j2hi integer j2lo integer jhi integer jlo character ( len = * ) title write ( *, '(a)' ) ' ' write ( *, '(a)' ) trim ( title ) ! ! Print the columns of the matrix, in strips of 5. ! do j2lo = jlo, jhi, incx j2hi = j2lo + incx - 1 j2hi = min ( j2hi, n ) j2hi = min ( j2hi, jhi ) inc = j2hi + 1 - j2lo write ( *, '(a)' ) ' ' do j = j2lo, j2hi j2 = j + 1 - j2lo write ( ctemp(j2), '(i7,7x)' ) j end do write ( *, '('' Col: '',5a14)' ) ( ctemp(j2), j2 = 1, inc ) write ( *, '(a)' ) ' Row' write ( *, '(a)' ) ' ---' ! ! Determine the range of the rows in this strip. ! i2lo = max ( ilo, 1 ) i2hi = min ( ihi, m ) do i = i2lo, i2hi ! ! Print out (up to) 5 entries in row I, that lie in the current strip. ! do j2 = 1, inc j = j2lo - 1 + j2 write ( ctemp(j2), '(g14.6)' ) a(i,j) end do write ( *, '(i5,1x,5a14)' ) i, ( ctemp(j2), j2 = 1, inc ) end do end do return end subroutine r8ge_random ( m, n, a ) !*****************************************************************************80 ! !! R8GE_RANDOM randomizes an R8GE matrix. ! ! Discussion: ! ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! R8GE storage is used by LINPACK and LAPACK. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 11 August 2004 ! ! Author: ! ! John Burkardt ! ! Reference: ! ! Paul Bratley, Bennett Fox, Linus Schrage, ! A Guide to Simulation, ! Springer Verlag, pages 201-202, 1983. ! ! Bennett Fox, ! Algorithm 647: ! Implementation and Relative Efficiency of Quasirandom ! Sequence Generators, ! ACM Transactions on Mathematical Software, ! Volume 12, Number 4, pages 362-376, 1986. ! ! Peter Lewis, Allen Goodman, James Miller, ! A Pseudo-Random Number Generator for the System/360, ! IBM Systems Journal, ! Volume 8, pages 136-143, 1969. ! ! Parameters: ! ! Input, integer M, N, the number of rows and columns in ! the array. ! ! Output, real ( kind = rk ) A(M,N), the array. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer m integer n real ( kind = rk ) a(m,n) call random_number ( harvest = a(1:m,1:n) ) return end subroutine r8ge_res ( m, n, a, x, b, r ) !*****************************************************************************80 ! !! R8GE_RES computes the residual vector for an R8GE system. ! ! Discussion: ! ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! R8GE storage is used by LINPACK and LAPACK. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 03 October 2003 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, N, the number of rows and columns of ! the matrix. M and N must be positive. ! ! Input, real ( kind = rk ) A(M,N), the original, UNFACTORED R8GE matrix. ! ! Input, real ( kind = rk ) X(N), the estimated solution. ! ! Input, real ( kind = rk ) B(M), the right hand side vector. ! ! Output, real ( kind = rk ) R(M), the residual vector, b - A * x. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer m integer n real ( kind = rk ) a(m,n) real ( kind = rk ) b(m) real ( kind = rk ) r(m) real ( kind = rk ) x(n) r(1:m) = b(1:m) - matmul ( a(1:m,1:n), x(1:n) ) return end subroutine r8ge_sl ( n, a_lu, pivot, b, job ) !*****************************************************************************80 ! !! R8GE_SL solves a system factored by R8GE_FA. ! ! Discussion: ! ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! R8GE storage is used by LINPACK and LAPACK. ! ! R8GE_SL is a simplified version of the LINPACK routine SGESL. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 04 March 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N, the order of the matrix. ! N must be positive. ! ! Input, real ( kind = rk ) A_LU(N,N), the LU factors from R8GE_FA. ! ! Input, integer PIVOT(N), the pivot vector from R8GE_FA. ! ! Input/output, real ( kind = rk ) B(N). ! On input, the right hand side vector. ! On output, the solution vector. ! ! Input, integer JOB, specifies the operation. ! 0, solve A * x = b. ! nonzero, solve A' * x = b. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n real ( kind = rk ) a_lu(n,n) real ( kind = rk ) b(n) integer pivot(n) integer job integer k integer l real ( kind = rk ) t ! ! Solve A * x = b. ! if ( job == 0 ) then ! ! Solve PL * Y = B. ! do k = 1, n - 1 l = pivot(k) if ( l /= k ) then t = b(l) b(l) = b(k) b(k) = t end if b(k+1:n) = b(k+1:n) + a_lu(k+1:n,k) * b(k) end do ! ! Solve U * X = Y. ! do k = n, 1, -1 b(k) = b(k) / a_lu(k,k) b(1:k-1) = b(1:k-1) - a_lu(1:k-1,k) * b(k) end do ! ! Solve A' * X = B. ! else ! ! Solve U' * Y = B. ! do k = 1, n b(k) = ( b(k) - sum ( b(1:k-1) * a_lu(1:k-1,k) ) ) / a_lu(k,k) end do ! ! Solve ( PL )' * X = Y. ! do k = n - 1, 1, -1 b(k) = b(k) + sum ( b(k+1:n) * a_lu(k+1:n,k) ) l = pivot(k) if ( l /= k ) then t = b(l) b(l) = b(k) b(k) = t end if end do end if return end subroutine r8ge_sl_it ( n, a, a_lu, pivot, b, job, x, r ) !*****************************************************************************80 ! !! R8GE_SL_IT applies one step of iterative refinement following R8GE_SL. ! ! Discussion: ! ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! R8GE storage is used by LINPACK and LAPACK. ! ! It is assumed that: ! ! * the original matrix A has been factored by R8GE_FA; ! * the linear system A * x = b has been solved once by R8GE_SL. ! ! (Actually, it is not necessary to solve the system once using R8GE_SL. ! You may simply supply the initial estimated solution X = 0.) ! ! Each time this routine is called, it will compute the residual in ! the linear system, apply one step of iterative refinement, and ! add the computed correction to the current solution. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 15 May 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N, the order of the matrix. ! N must be positive. ! ! Input, real ( kind = rk ) A(N,N), the original, UNFACTORED R8GE matrix. ! ! Input, real ( kind = rk ) A_LU(N,N), the LU factors from R8GE_FA. ! ! Input, integer PIVOT(N), the pivot vector from R8GE_FA. ! ! Input, real ( kind = rk ) B(N), the right hand side vector. ! ! Input, integer JOB, specifies the operation. ! 0, solve A*X=B. ! nonzero, solve A'*X=B. ! ! Input/output, real ( kind = rk ) X(N), an estimate of the solution ! of A * x = b. On output, the solution has been improved by one ! step of iterative refinement. ! ! Output, real ( kind = rk ) R(N), contains the correction terms added to X. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n real ( kind = rk ) a(n,n) real ( kind = rk ) a_lu(n,n) real ( kind = rk ) b(n) integer pivot(n) integer job real ( kind = rk ) r(n) real ( kind = rk ) x(n) ! ! Compute the residual vector. ! call r8ge_res ( n, n, a, x, b, r ) ! ! Solve A * dx = r ! call r8ge_sl ( n, a_lu, pivot, r, job ) ! ! Add dx to x. ! x(1:n) = x(1:n) + r(1:n) return end subroutine r8ge_to_r8vec ( m, n, a, x ) !*****************************************************************************80 ! !! R8GE_TO_R8VEC copies an R8GE matrix to an R8VEC. ! ! Discussion: ! ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! R8GE storage is used by LINPACK and LAPACK. ! ! In C++ and FORTRAN, this routine is not really needed. In MATLAB, ! a data item carries its dimensionality implicitly, and so cannot be ! regarded sometimes as a vector and sometimes as an array. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 16 March 2004 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, N, the number of rows and columns in ! the array. ! ! Input, real ( kind = rk ) A(M,N), the array to be copied. ! ! Output, real ( kind = rk ) X(M*N), the vector. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer m integer n real ( kind = rk ) a(m,n) integer i integer j integer k real ( kind = rk ) x(m*n) k = 0 do j = 1, n do i = 1, m k = k + 1 x(k) = a(i,j) end do end do return end subroutine r8ge_transpose_print ( m, n, a, title ) !*****************************************************************************80 ! !! R8GE_TRANSPOSE_PRINT prints an R8GE, transposed. ! ! Discussion: ! ! An R8GE matrix is an MxN array of R8's, stored by (I,J) -> [I+J*M]. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 14 June 2004 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, N, the number of rows and columns. ! ! Input, real ( kind = rk ) A(M,N), an M by N matrix to be printed. ! ! Input, character ( len = * ) TITLE, a title. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer m integer n real ( kind = rk ) a(m,n) character ( len = * ) title call r8ge_transpose_print_some ( m, n, a, 1, 1, m, n, title ) return end subroutine r8ge_transpose_print_some ( m, n, a, ilo, jlo, ihi, jhi, title ) !*****************************************************************************80 ! !! R8GE_TRANSPOSE_PRINT_SOME prints some of an R8GE, transposed. ! ! Discussion: ! ! An R8GE matrix is an MxN array of R8's, stored by (I,J) -> [I+J*M]. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 10 September 2009 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, N, the number of rows and columns. ! ! Input, real ( kind = rk ) A(M,N), an M by N matrix to be printed. ! ! Input, integer ILO, JLO, the first row and column to print. ! ! Input, integer IHI, JHI, the last row and column to print. ! ! Input, character ( len = * ) TITLE, a title. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer, parameter :: incx = 5 integer m integer n real ( kind = rk ) a(m,n) character ( len = 14 ) ctemp(incx) integer i integer i2 integer i2hi integer i2lo integer ihi integer ilo integer inc integer j integer j2hi integer j2lo integer jhi integer jlo character ( len = * ) title write ( *, '(a)' ) ' ' write ( *, '(a)' ) trim ( title ) if ( m <= 0 .or. n <= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) ' (None)' return end if do i2lo = max ( ilo, 1 ), min ( ihi, m ), incx i2hi = i2lo + incx - 1 i2hi = min ( i2hi, m ) i2hi = min ( i2hi, ihi ) inc = i2hi + 1 - i2lo write ( *, '(a)' ) ' ' do i = i2lo, i2hi i2 = i + 1 - i2lo write ( ctemp(i2), '(i8,6x)' ) i end do write ( *, '('' Row '',5a14)' ) ctemp(1:inc) write ( *, '(a)' ) ' Col' write ( *, '(a)' ) ' ' j2lo = max ( jlo, 1 ) j2hi = min ( jhi, n ) do j = j2lo, j2hi do i2 = 1, inc i = i2lo - 1 + i2 write ( ctemp(i2), '(g14.6)' ) a(i,j) end do write ( *, '(i5,a,5a14)' ) j, ':', ( ctemp(i), i = 1, inc ) end do end do return end subroutine r8ge_trf ( m, n, a, pivot, info ) !*****************************************************************************80 ! !! R8GE_TRF performs a LAPACK-style PLU factorization of an R8GE matrix. ! ! Discussion: ! ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! R8GE storage is used by LINPACK and LAPACK. ! ! R8GE_TRF is a standalone version of the LAPACK routine SGETRF. ! ! The factorization uses partial pivoting with row interchanges, ! and has the form ! A = P * L * U ! where P is a permutation matrix, L is lower triangular with unit ! diagonal elements (lower trapezoidal if N < M), and U is upper ! triangular (upper trapezoidal if M < N). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 17 December 1998 ! ! Author: ! ! Original FORTRAN77 version by the LAPACK group. ! FORTRAN90 version by John Burkardt. ! ! Reference: ! ! Edward Anderson, Zhaojun Bai, Christian Bischof, Susan Blackford, ! James Demmel, Jack Dongarra, Jeremy Du Croz, Anne Greenbaum, ! Sven Hammarling, Alan McKenney, Danny Sorensen, ! LAPACK User's Guide, ! Second Edition, ! SIAM, 1995. ! ! Parameters: ! ! Input, integer M, the number of rows of the matrix A. ! 0 <= M. ! ! Input, integer N, the number of columns of the matrix A. ! 0 <= N. ! ! Input/output, real ( kind = rk ) A(M,N). ! On entry, the M by N matrix to be factored. ! On exit, the factors L and U from the factorization ! A = P*L*U; the unit diagonal elements of L are not stored. ! ! Output, integer PIVOT(min(M,N)), the pivot indices; ! for 1 <= I <= min(M,N), row i of the matrix was interchanged with ! row PIVOT(I). ! ! Output, integer INFO. ! = 0: successful exit ! < 0: if INFO = -K, the K-th argument had an illegal value ! > 0: if INFO = K, U(K,K) is exactly zero. The factorization ! has been completed, but the factor U is exactly ! singular, and division by zero will occur if it is used ! to solve a system of equations. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer m integer n real ( kind = rk ) a(m,n) integer i integer ii integer info integer pivot(*) integer j integer jj integer jp real ( kind = rk ) t ! ! Test the input parameters. ! info = 0 if ( m < 0 ) then info = - 1 return else if ( n < 0 ) then info = - 2 return end if if ( m == 0 .or. n == 0 ) then return end if do j = 1, min ( m, n ) ! ! Find the pivot. ! t = abs ( a(j,j) ) jp = j do i = j + 1, m if ( t < abs ( a(i,j) ) ) then t = abs ( a(i,j) ) jp = i end if end do pivot(j) = jp ! ! Apply the interchange to columns 1:N. ! Compute elements J+1:M of the J-th column. ! if ( a(jp,j) /= 0.0D+00 ) then if ( jp /= j ) then do jj = 1, n t = a(j,jj) a(j,jj) = a(jp,jj) a(jp,jj) = t end do end if if ( j < m ) then a(j+1:m,j) = a(j+1:m,j) / a(j,j) end if else if ( info == 0 ) then info = j end if ! ! Update the trailing submatrix. ! if ( j < min ( m, n ) ) then do ii = j+1, m a(ii,j+1:n) = a(ii,j+1:n) - a(ii,j) * a(j,j+1:n) end do end if end do return end subroutine r8ge_trs ( n, nrhs, trans, a, pivot, b, info ) !*****************************************************************************80 ! !! R8GE_TRS solves a system of linear equations factored by R8GE_TRF. ! ! Discussion: ! ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! R8GE storage is used by LINPACK and LAPACK. ! ! R8GE_TRS is a standalone version of the LAPACK routine SGETRS. ! ! R8GE_TRS solves a system of linear equations ! A * x = b or A' * X = B ! with a general N by N matrix A using the PLU factorization computed ! by R8GE_TRF. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 17 December 1998 ! ! Author: ! ! Original FORTRAN77 version by the LAPACK group. ! FORTRAN90 version by John Burkardt. ! ! Reference: ! ! Edward Anderson, Zhaojun Bai, Christian Bischof, Susan Blackford, ! James Demmel, Jack Dongarra, Jeremy Du Croz, Anne Greenbaum, ! Sven Hammarling, Alan McKenney, Danny Sorensen, ! LAPACK User's Guide, ! Second Edition, ! SIAM, 1995. ! ! Parameters: ! ! Input, integer N, the order of the matrix A. 0 <= N. ! ! Input, integer NRHS, the number of right hand sides. ! 0 <= NRHS. ! ! Input, character TRANS, specifies the form of the system of equations: ! 'N': A * x = b (No transpose) ! 'T': A'* X = B (Transpose) ! 'C': A'* X = B (Conjugate transpose = Transpose) ! ! Input, real ( kind = rk ) A(N,N), the factors L and U from the factorization ! A = P*L*U as computed by R8GE_TRF. ! ! Input, integer PIVOT(N), the pivot indices from R8GE_TRF. ! ! Input/output, real ( kind = rk ) B(N,NRHS). ! On entry, the right hand side matrix B. ! On exit, the solution matrix X. ! ! Output, integer INFO ! = 0: successful exit ! < 0: if INFO = -I, the I-th argument had an illegal value. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n integer nrhs real ( kind = rk ) a(n,n) real ( kind = rk ) b(n,nrhs) integer i integer info integer pivot(n) integer j integer k real ( kind = rk ) t character trans info = 0 if ( trans /= 'n' .and. trans /= 'N' .and. & trans /= 't' .and. trans /= 'T' .and. & trans /= 'c' .and. trans /= 'C' ) then info = - 1 return else if ( n < 0 ) then info = - 2 return else if ( nrhs < 0 ) then info = - 3 return end if if ( n == 0 .or. nrhs == 0 ) then return end if if ( trans == 'n' .or. trans == 'N' ) then ! ! Apply row interchanges to the right hand sides. ! do i = 1, n if ( pivot(i) /= i ) then do k = 1, nrhs t = b(i,k) b(i,k) = b(pivot(i),k) b(pivot(i),k) = t end do end if end do ! ! Solve L * x = b, overwriting b with x. ! do k = 1, nrhs do j = 1, n - 1 b(j+1:n,k) = b(j+1:n,k) - a(j+1:n,j) * b(j,k) end do end do ! ! Solve U * x = b, overwriting b with x. ! do k = 1, nrhs do j = n, 1, -1 b(j,k) = b(j,k) / a(j,j) b(1:j-1,k) = b(1:j-1,k) - a(1:j-1,j) * b(j,k) end do end do else ! ! Solve U' * x = b, overwriting b with x. ! do k = 1, nrhs do j = 1, n b(j,k) = b(j,k) / a(j,j) b(j+1:n,k) = b(j+1:n,k) - a(j,j+1:n) * b(j,k) end do end do ! ! Solve L' * x = b, overwriting b with x. ! do k = 1, nrhs do j = n, 2, -1 b(1:j-1,k) = b(1:j-1,k) - a(j,1:j-1) * b(j,k) end do end do ! ! Apply row interchanges to the solution vectors. ! do i = n, 1, -1 if ( pivot(i) /= i ) then do k = 1, nrhs t = b(i,k) b(i,k) = b(pivot(i),k) b(pivot(i),k) = t end do end if end do end if return end subroutine r8ge_zeros ( m, n, a ) !*****************************************************************************80 ! !! R8GE_ZEROS zeroes an R8GE matrix. ! ! Discussion: ! ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! R8GE storage is used by LINPACK and LAPACK. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 26 January 2013 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, the number of rows of the matrix. ! M must be positive. ! ! Input, integer N, the number of columns of the matrix. ! N must be positive. ! ! Output, real ( kind = rk ) A(M,N), the R8GE matrix. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer m integer n real ( kind = rk ) a(m,n) a(1:m,1:n) = 0.0D+00 return end subroutine r8row_swap ( m, n, a, i1, i2 ) !*****************************************************************************80 ! !! R8ROW_SWAP swaps two rows of an R8ROW. ! ! Discussion: ! ! An R8ROW is an M by N array of R8 values, regarded ! as an array of M rows of length N. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 05 December 2004 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, N, the number of rows and columns. ! ! Input/output, real ( kind = rk ) A(M,N), the M by N array. ! ! Input, integer I1, I2, the two rows to swap. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer m integer n real ( kind = rk ) a(m,n) integer i1 integer i2 real ( kind = rk ) row(n) if ( i1 < 1 .or. m < i1 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'R8ROW_SWAP - Fatal error!' write ( *, '(a)' ) ' I1 is out of range.' write ( *, '(a,i8)' ) ' I1 = ', i1 stop 1 end if if ( i2 < 1 .or. m < i2 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'R8ROW_SWAP - Fatal error!' write ( *, '(a)' ) ' I2 is out of range.' write ( *, '(a,i8)' ) ' I2 = ', i2 stop 1 end if if ( i1 == i2 ) then return end if row(1:n) = a(i1,1:n) a(i1,1:n) = a(i2,1:n) a(i2,1:n) = row(1:n) return end subroutine r8vec_indicator1 ( n, a ) !*****************************************************************************80 ! !! R8VEC_INDICATOR1 sets an R8VEC to the indicator vector (1,2,3,...). ! ! Discussion: ! ! An R8VEC is a vector of R8's. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 27 September 2014 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N, the number of elements of A. ! ! Output, real ( kind = rk ) A(N), the array. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n real ( kind = rk ) a(n) integer i do i = 1, n a(i) = real ( i, kind = rk ) end do return end function r8vec_norm ( n, a ) !*****************************************************************************80 ! !! R8VEC_NORM returns the L2 norm of an R8VEC. ! ! Discussion: ! ! An R8VEC is a vector of R8's. ! ! The vector L2 norm is defined as: ! ! R8VEC_NORM = sqrt ( sum ( 1 <= I <= N ) A(I)^2 ). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 21 August 2010 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N, the number of entries in A. ! ! Input, real ( kind = rk ) A(N), the vector whose L2 norm is desired. ! ! Output, real ( kind = rk ) R8VEC_NORM, the L2 norm of A. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n real ( kind = rk ) a(n) real ( kind = rk ) r8vec_norm r8vec_norm = sqrt ( sum ( a(1:n)**2 ) ) return end function r8vec_norm_affine ( n, v0, v1 ) !*****************************************************************************80 ! !! R8VEC_NORM_AFFINE returns the affine norm of an R8VEC. ! ! Discussion: ! ! An R8VEC is a vector of R8's. ! ! The affine vector L2 norm is defined as: ! ! R8VEC_NORM_AFFINE(V0,V1) ! = sqrt ( sum ( 1 <= I <= N ) ( V1(I) - V0(I) )^2 ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 27 October 2010 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N, the order of the vectors. ! ! Input, real ( kind = rk ) V0(N), the base vector. ! ! Input, real ( kind = rk ) V1(N), the vector whose affine norm is desired. ! ! Output, real ( kind = rk ) R8VEC_NORM_AFFINE, the L2 norm of V1-V0. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n real ( kind = rk ) r8vec_norm_affine real ( kind = rk ) v0(n) real ( kind = rk ) v1(n) r8vec_norm_affine = sqrt ( sum ( ( v0(1:n) - v1(1:n) )**2 ) ) return end subroutine r8vec_print ( n, a, title ) !*****************************************************************************80 ! !! R8VEC_PRINT prints an R8VEC. ! ! Discussion: ! ! An R8VEC is a vector of R8's. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 22 August 2000 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N, the number of components of the vector. ! ! Input, real ( kind = rk ) A(N), the vector to be printed. ! ! Input, character ( len = * ) TITLE, a title. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n real ( kind = rk ) a(n) integer i character ( len = * ) title write ( *, '(a)' ) ' ' write ( *, '(a)' ) trim ( title ) write ( *, '(a)' ) ' ' do i = 1, n write ( *, '(2x,i8,a,1x,g16.8)' ) i, ':', a(i) end do return end subroutine r8vec_print_some ( n, a, max_print, title ) !*****************************************************************************80 ! !! R8VEC_PRINT_SOME prints "some" of an R8VEC. ! ! Discussion: ! ! The user specifies MAX_PRINT, the maximum number of lines to print. ! ! If N, the size of the vector, is no more than MAX_PRINT, then ! the entire vector is printed, one entry per line. ! ! Otherwise, if possible, the first MAX_PRINT-2 entries are printed, ! followed by a line of periods suggesting an omission, ! and the last entry. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 19 December 2001 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N, the number of entries of the vector. ! ! Input, real ( kind = rk ) A(N), the vector to be printed. ! ! Input, integer MAX_PRINT, the maximum number of lines ! to print. ! ! Input, character ( len = * ) TITLE, a title. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n real ( kind = rk ) a(n) integer i integer max_print character ( len = * ) title if ( max_print <= 0 ) then return end if if ( n <= 0 ) then return end if write ( *, '(a)' ) ' ' write ( *, '(a)' ) trim ( title ) write ( *, '(a)' ) ' ' if ( n <= max_print ) then do i = 1, n write ( *, '(2x,i8,a,1x,g14.6)' ) i, ':', a(i) end do else if ( 3 <= max_print ) then do i = 1, max_print - 2 write ( *, '(2x,i8,a,1x,g14.6)' ) i, ':', a(i) end do write ( *, '(a)' ) ' ........ ..............' i = n write ( *, '(2x,i8,a,1x,g14.6)' ) i, ':', a(i) else do i = 1, max_print - 1 write ( *, '(2x,i8,a,1x,g14.6)' ) i, ':', a(i) end do i = max_print write ( *, '(2x,i8,a,1x,g14.6,2x,a)' ) i, ':', a(i), '...more entries...' end if return end subroutine r8vec_to_r8ge ( m, n, x, a ) !*****************************************************************************80 ! !! R8VEC_TO_R8GE copies an R8VEC into an R8GE matrix. ! ! Discussion: ! ! In C++ and FORTRAN, this routine is not really needed. In MATLAB, ! a data item carries its dimensionality implicitly, and so cannot be ! regarded sometimes as a vector and sometimes as an array. ! ! The R8GE storage format is used for a general M by N matrix. A storage ! space is made for each entry. The two dimensional logical ! array can be thought of as a vector of M*N entries, starting with ! the M entries in the column 1, then the M entries in column 2 ! and so on. Considered as a vector, the entry A(I,J) is then stored ! in vector location I+(J-1)*M. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 16 March 2004 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, N, the number of rows and columns ! in the array. ! ! Input, real ( kind = rk ) X(M*N), the vector to be copied into the array. ! ! Output, real ( kind = rk ) A(M,N), the array. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer m integer n real ( kind = rk ) a(m,n) integer i integer j integer k real ( kind = rk ) x(m*n) k = 0 do j = 1, n do i = 1, m k = k + 1 a(i,j) = x(k) end do end do return end subroutine r8vec2_print_some ( n, x1, x2, max_print, title ) !*****************************************************************************80 ! !! R8VEC2_PRINT_SOME prints "some" of an R8VEC2. ! ! Discussion: ! ! An R8VEC2 is a dataset consisting of N pairs of R8's, stored ! as two separate vectors A1 and A2. ! ! The user specifies MAX_PRINT, the maximum number of lines to print. ! ! If N, the size of the vectors, is no more than MAX_PRINT, then ! the entire vectors are printed, one entry of each per line. ! ! Otherwise, if possible, the first MAX_PRINT-2 entries are printed, ! followed by a line of periods suggesting an omission, ! and the last entry. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 10 September 2009 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N, the number of entries of the vectors. ! ! Input, real ( kind = rk ) X1(N), X2(N), the vector to be printed. ! ! Input, integer MAX_PRINT, the maximum number of lines ! to print. ! ! Input, character ( len = * ) TITLE, a title. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n integer i integer max_print character ( len = * ) title real ( kind = rk ) x1(n) real ( kind = rk ) x2(n) if ( max_print <= 0 ) then return end if if ( n <= 0 ) then return end if write ( *, '(a)' ) ' ' write ( *, '(a)' ) trim ( title ) write ( *, '(a)' ) ' ' if ( n <= max_print ) then do i = 1, n write ( *, '(2x,i8,2x,g14.6,2x,g14.6)' ) i, x1(i), x2(i) end do else if ( 3 <= max_print ) then do i = 1, max_print - 2 write ( *, '(2x,i8,2x,g14.6,2x,g14.6)' ) i, x1(i), x2(i) end do write ( *, '(a)' ) ' ...... .............. ..............' i = n write ( *, '(2x,i8,2x,g14.6,2x,g14.6)' ) i, x1(i), x2(i) else do i = 1, max_print - 1 write ( *, '(2x,i8,2x,g14.6,2x,g14.6)' ) i, x1(i), x2(i) end do i = max_print write ( *, '(2x,i8,2x,g14.6,2x,g14.6,2x,a)' ) i, x1(i), x2(i), & '...more entries...' end if return end subroutine timestamp ( ) !*****************************************************************************80 ! !! TIMESTAMP prints the current YMDHMS date as a time stamp. ! ! Example: ! ! 31 May 2001 9:45:54.872 AM ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 18 May 2013 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! None ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) character ( len = 8 ) ampm integer d integer h integer m integer mm character ( len = 9 ), parameter, dimension(12) :: month = (/ & 'January ', 'February ', 'March ', 'April ', & 'May ', 'June ', 'July ', 'August ', & 'September', 'October ', 'November ', 'December ' /) integer n integer s integer values(8) integer y call date_and_time ( values = values ) y = values(1) m = values(2) d = values(3) h = values(5) n = values(6) s = values(7) mm = values(8) if ( h < 12 ) then ampm = 'AM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Noon' else ampm = 'PM' end if else h = h - 12 if ( h < 12 ) then ampm = 'PM' else if ( h == 12 ) then if ( n == 0 .and. s == 0 ) then ampm = 'Midnight' else ampm = 'AM' end if end if end if write ( *, '(i2,1x,a,1x,i4,2x,i2,a1,i2.2,a1,i2.2,a1,i3.3,1x,a)' ) & d, trim ( month(m) ), y, h, ':', n, ':', s, '.', mm, trim ( ampm ) return end