subroutine get_unit ( iunit ) !*****************************************************************************80 ! !! get_unit() returns a free FORTRAN unit number. ! ! Discussion: ! ! A "free" FORTRAN unit number is a value between 1 and 99 which ! is not currently associated with an I/O device. A free FORTRAN unit ! number is needed in order to open a file with the OPEN command. ! ! If IUNIT = 0, then no free FORTRAN unit could be found, although ! all 99 units were checked (except for units 5, 6 and 9, which ! are commonly reserved for console I/O). ! ! Otherwise, IUNIT is a value between 1 and 99, representing a ! free FORTRAN unit. Note that GET_UNIT assumes that units 5 and 6 ! are special, and will never return those values. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 18 September 2005 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Output, integer IUNIT, the free unit number. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer i integer ios integer iunit logical lopen iunit = 0 do i = 1, 99 if ( i /= 5 .and. i /= 6 .and. i /= 9 ) then inquire ( unit = i, opened = lopen, iostat = ios ) if ( ios == 0 ) then if ( .not. lopen ) then iunit = i return end if end if end if end do return end subroutine i4_fake_use ( n ) !*****************************************************************************80 ! !! i4_fake_use pretends to use a variable. ! ! Discussion: ! ! Some compilers will issue a warning if a variable is unused. ! Sometimes there's a good reason to include a variable in a program, ! but not to use it. Calling this function with that variable as ! the argument will shut the compiler up. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 21 April 2020 ! ! Author: ! ! John Burkardt ! ! Input: ! ! integer N, the variable to be "used". ! implicit none integer n if ( n /= n ) then write ( *, '(a)' ) ' i4_fake_use: variable is NAN.' end if return end 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 r8_fake_use ( x ) !*****************************************************************************80 ! !! r8_fake_use() pretends to use an R8 variable. ! ! Discussion: ! ! Some compilers will issue a warning if a variable is unused. ! Sometimes there's a good reason to include a variable in a program, ! but not to use it. Calling this function with that variable as ! the argument will shut the compiler up. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 21 April 2020 ! ! Author: ! ! John Burkardt ! ! Input: ! ! real ( kind = rk ) X, the variable to be "used". ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) real ( kind = rk ) x if ( x /= x ) then write ( *, '(a)' ) ' r8_fake_use: variable is NAN.' end if 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 r8ncf_print ( m, n, nz_num, rowcol, a, title ) !*****************************************************************************80 ! !! R8NCF_PRINT prints an R8NCF matrix. ! ! Discussion: ! ! The R8NCF storage format stores NZ_NUM, the number of nonzeros, ! a real array containing the nonzero values, a 2 by NZ_NUM integer ! array storing the row and column of each nonzero entry. ! ! The R8NCF format is used by NSPCG. NSPCG requires that the information ! for the diagonal entries of the matrix must come first. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 02 August 2006 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, N, the number of rows and columns of ! the matrix. ! ! Input, integer NZ_NUM, the number of nonzero elements in ! the matrix. ! ! Input, integer ROWCOL(2,NZ_NUM), the row and column indices ! of the nonzero elements. ! ! Input, real ( kind = rk ) A(NZ_NUM), the nonzero elements of the matrix. ! ! Input, character ( len = * ) TITLE, a title. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer nz_num real ( kind = rk ) a(nz_num) integer m integer n integer rowcol(2,nz_num) character ( len = * ) title call r8ncf_print_some ( m, n, nz_num, rowcol, a, 1, 1, m, n, title ) return end subroutine r8ncf_print_some ( m, n, nz_num, rowcol, a, ilo, jlo, & ihi, jhi, title ) !*****************************************************************************80 ! !! R8NCF_PRINT_SOME prints some of an R8NCF matrix. ! ! Discussion: ! ! The R8NCF storage format stores NZ_NUM, the number of nonzeros, ! a real array containing the nonzero values, a 2 by NZ_NUM integer ! array storing the row and column of each nonzero entry. ! ! The R8NCF format is used by NSPCG. NSPCG requires that the information ! for the diagonal entries of the matrix must come first. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 02 August 2006 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, N, the number of rows and columns of ! the matrix. ! ! Input, integer NZ_NUM, the number of nonzero elements ! in the matrix. ! ! Input, integer ROWCOL(2,NZ_NUM), the row and column indices ! of the nonzero elements. ! ! Input, real ( kind = rk ) A(NZ_NUM), the nonzero elements of the 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 nz_num real ( kind = rk ) a(nz_num) real ( kind = rk ) aij 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 integer k integer m integer n logical nonzero integer rowcol(2,nz_num) 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. ! nonzero = .false. aij = 0.0D+00 do j2 = 1, inc write ( ctemp(j2), '(f8.0,6x)' ) aij end do do k = 1, nz_num if ( & i == rowcol(1,k) .and. & j2lo <= rowcol(2,k) .and. & rowcol(2,k) <= j2hi ) then j2 = rowcol(2,k) - j2lo + 1 aij = a(k) if ( aij == 0.0D+00 ) then cycle end if nonzero = .true. write ( ctemp(j2), '(g14.6)' ) aij end if end do if ( nonzero ) then write ( *, '(i5,1x,5a14)' ) i, ( ctemp(j2), j2 = 1, inc ) end if end do end do return end subroutine r8st_cg ( n, nz_num, row, col, a, b, x ) !*****************************************************************************80 ! !! r8st_CG uses the conjugate gradient method on an r8st system. ! ! Discussion: ! ! The r8st storage format stores the row, column and value of each nonzero ! entry of a sparse matrix. ! ! It is possible that a pair of indices (I,J) may occur more than ! once. Presumably, in this case, the intent is that the actual value ! of A(I,J) is the sum of all such entries. This is not a good thing ! to do, but I seem to have come across this in MATLAB. ! ! The r8st format is used by CSPARSE ("sparse triplet"), SLAP ! ("nonsymmetric SLAP triad"), by MATLAB, and by SPARSEKIT ("COO" format). ! ! 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: ! ! 02 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, integer NZ_NUM, the number of nonzero elements in ! the matrix. ! ! Input, integer ROW(NZ_NUM), COL(NZ_NUM), the row and ! column indices of the nonzero elements. ! ! Input, real ( kind = rk ) A(NZ_NUM), the nonzero elements of 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 integer nz_num real ( kind = rk ) a(nz_num) real ( kind = rk ) alpha real ( kind = rk ) ap(n) real ( kind = rk ) b(n) real ( kind = rk ) beta integer col(nz_num) integer it real ( kind = rk ) p(n) real ( kind = rk ) pap real ( kind = rk ) pr real ( kind = rk ) r(n) integer row(nz_num) real ( kind = rk ) rap real ( kind = rk ) x(n) ! ! Initialize ! AP = A * x, ! R = b - A * x, ! P = b - A * x. ! call r8st_mv ( n, n, nz_num, row, col, 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 r8st_mv ( n, n, nz_num, row, col, a, p, ap ) ! ! Compute the dot products ! PAP = P*AP, ! PR = P*R ! Set ! ALPHA = PR / PAP. ! pap = dot_product ( p, ap ) pr = dot_product ( p, r ) 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, ap ) 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 r8st_check ( m, n, nz_num, row, col, check ) !*****************************************************************************80 ! !! r8st_CHECK checks that an r8st matrix data structure is properly sorted. ! ! Discussion: ! ! This routine assumes that the data structure has been sorted, ! so that the entries of ROW are ascending sorted, and that the ! entries of COL are ascending sorted, within the group of entries ! that have a common value of ROW. ! ! The r8st storage format stores the row, column and value of each nonzero ! entry of a sparse matrix. ! ! The r8st format is used by CSPARSE ("sparse triplet"), SLAP ! ("nonsymmetric SLAP triad"), by MATLAB, and by SPARSEKIT ("COO" format). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 11 July 2007 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, N, the number of rows and columns of ! the matrix. ! ! Input, integer NZ_NUM, the number of nonzero elements in ! the matrix. ! ! Input, integer ROW(NZ_NUM), COL(NZ_NUM), the row and ! column indices of the nonzero elements. ! ! Output, logical CHECK, is TRUE if the matrix is properly defined. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer nz_num logical check integer col(nz_num) integer k integer m integer n integer row(nz_num) check = .true. ! ! Check 1 <= ROW(*) <= M. ! do k = 1, nz_num if ( row(k) < 1 .or. m < row(k) ) then check = .false. return end if end do ! ! Check 1 <= COL(*) <= N. ! do k = 1, nz_num if ( col(k) < 1 .or. n < col(k) ) then check = .false. return end if end do ! ! Check that ROW(K) <= ROW(K+1). ! do k = 1, nz_num - 1 if ( row(k+1) < row(k) ) then check = .false. return end if end do ! ! Check that, if ROW(K) == ROW(K+1), that COL(K) < COL(K+1). ! do k = 1, nz_num - 1 if ( row(k) == row(k+1) ) then if ( col(k+1) <= col(k) ) then check = .false. return end if end if end do return end subroutine r8st_diagonal ( m, n, nz_num, row, col, a ) !*****************************************************************************80 ! !! r8st_DIAGONAL reorders an r8st matrix so diagonal entries are first. ! ! Discussion: ! ! The r8st storage format corresponds to the SLAP Triad format. ! ! The r8st storage format stores the row, column and value of each nonzero ! entry of a sparse matrix. The entries may be given in any order. No ! check is made for the erroneous case in which a given matrix entry is ! specified more than once. ! ! This routine reorders the entries of A so that the first N entries ! are exactly the diagonal entries of the matrix, in order. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 21 September 2015 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, N, the order of the matrix. ! ! Input, integer NZ_NUM, the number of nonzero elements in ! the matrix. ! ! Input/output, integer ROW(NZ_NUM), COL(NZ_NUM), the row and ! column indices of the nonzero elements. ! ! Input/output, real ( kind = rk ) A(NZ_NUM), the nonzero elements ! of the matrix. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer nz_num real ( kind = rk ) a(nz_num) integer col(nz_num) integer found integer i integer j integer k integer m integer n integer row(nz_num) real ( kind = rk ) t found = 0 do k = 1, nz_num do while ( row(k) == col(k) ) if ( row(k) == k ) then found = found + 1 exit end if i = row(k) j = row(i) row(i) = row(k) row(k) = j j = col(i) col(i) = col(k) col(k) = j t = a(i) a(i) = a(k) a(k) = t found = found + 1 if ( min ( m, n ) <= found ) then exit end if end do if ( min ( m, n ) <= found ) then exit end if end do if ( found < min ( m, n ) ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'r8st_DIAGONAL - Warning!' write ( *, '(a,i8)' ) & ' Number of diagonal entries expected: ', min ( m, n ) write ( *, '(a,i8)' ) ' Number found was ', found end if return end subroutine r8st_dif2 ( m, n, nz_num, row, col, a ) !*****************************************************************************80 ! !! r8st_DIF2 returns the DIF2 matrix in r8st 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: ! ! 21 September 2015 ! ! 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 number of rows and columns. ! ! Input, integer NZ_NUM, the number of nonzero elements in ! the matrix. ! ! Output, integer ROW(NZ_NUM), COL(NZ_NUM), the row and ! column indices of the nonzero elements. ! ! Output, real ( kind = rk ) A(NZ_NUM), the nonzero elements of the matrix. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n integer nz_num real ( kind = rk ) a(nz_num) integer col(nz_num) integer i integer j integer k integer m integer row(nz_num) k = 0 do i = 1, m j = i - 1 if ( 1 <= j .and. j <= n ) then k = k + 1 row(k) = i col(k) = j a(k) = -1.0D+00 end if j = i if ( 1 <= j .and. j <= n ) then k = k + 1 row(k) = i col(k) = j a(k) = 2.0D+00 end if j = i + 1 if ( 1 <= j .and. j <= n ) then k = k + 1 row(k) = i col(k) = i + 1 a(k) = -1.0D+00 end if end do return end subroutine r8st_ij_to_k ( nz_num, row, col, i, j, k ) !*****************************************************************************80 ! !! r8st_IJ_TO_K seeks the compressed index of the (I,J) entry of A. ! ! Discussion: ! ! If A(I,J) is nonzero, then its value is stored in location K. ! ! This routine searches the r8st storage structure for the index K ! corresponding to (I,J), returning -1 if no such entry was found. ! ! This routine assumes that the data structure has been sorted, ! so that the entries of ROW are ascending sorted, and that the ! entries of COL are ascending sorted, within the group of entries ! that have a common value of ROW. ! ! The r8st storage format stores the row, column and value of each nonzero ! entry of a sparse matrix. ! ! The r8st format is used by CSPARSE ("sparse triplet"), SLAP ! ("nonsymmetric SLAP triad"), by MATLAB, and by SPARSEKIT ("COO" format). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 11 July 2007 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer NZ_NUM, the number of nonzero elements in ! the matrix. ! ! Input, integer ROW(NZ_NUM), COL(NZ_NUM), the row and ! column indices of the nonzero elements. ! ! Input, integer I, J, the row and column indices of the ! matrix entry. ! ! Output, integer K, the r8st index of the (I,J) entry. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer nz_num integer col(nz_num) integer hi integer i integer j integer k integer lo integer md integer row(nz_num) lo = 1 hi = nz_num do if ( hi < lo ) then k = -1 exit end if md = ( lo + hi ) / 2 if ( row(md) < i .or. ( row(md) == i .and. col(md) < j ) ) then lo = md + 1 else if ( i < row(md) .or. ( row(md) == i .and. j < col(md) ) ) then hi = md - 1 else k = md exit end if end do return end subroutine r8st_indicator ( m, n, nz_num, row, col, a ) !*****************************************************************************80 ! !! r8st_INDICATOR sets up an r8st 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 "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 r8st storage format stores the row, column and value of each nonzero ! entry of a sparse matrix. ! ! It is possible that a pair of indices (I,J) may occur more than ! once. Presumably, in this case, the intent is that the actual value ! of A(I,J) is the sum of all such entries. This is not a good thing ! to do, but I seem to have come across this in MATLAB. ! ! The r8st format is used by CSPARSE ("sparse triplet"), SLAP ! ("nonsymmetric SLAP triad"), by MATLAB, and by SPARSEKIT ("COO" format). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 22 January 2004 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, N, the number of rows and columns of ! the matrix. ! ! Input, integer NZ_NUM, the number of nonzero elements in ! the matrix. ! ! Input, integer ROW(NZ_NUM), COL(NZ_NUM), the row and ! column indices of the nonzero elements. ! ! Output, real ( kind = rk ) A(NZ_NUM), the nonzero elements of the matrix. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer nz_num real ( kind = rk ) a(nz_num) integer col(nz_num) integer fac integer i integer i4_log_10 integer j integer k integer m integer n integer row(nz_num) call i4_fake_use ( col(1) ) call i4_fake_use ( m ) fac = 10 ** ( i4_log_10 ( n ) + 1 ) do k = 1, nz_num i = row(k) j = col(k) a(k) = real ( fac * i + j, kind = rk ) end do return end subroutine r8st_jac_sl ( n, nz_num, row, col, a, b, x, it_max ) !*****************************************************************************80 ! !! r8st_JAC_SL solves an r8st system using Jacobi iteration. ! ! Discussion: ! ! The r8st storage format corresponds to the SLAP Triad format. ! ! The r8st storage format stores the row, column and value of each nonzero ! entry of a sparse matrix. The entries may be given in any order. No ! check is made for the erroneous case in which a given matrix entry is ! specified more than once. ! ! This routine REQUIRES that the matrix be square, that the matrix ! have nonzero diagonal entries, and that the first N entries of ! the array A be exactly the diagonal entries of the matrix, in order. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 21 September 2015 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N, the order of the matrix. ! ! Input, integer NZ_NUM, the number of nonzero elements in ! the matrix. ! ! Input, integer ROW(NZ_NUM), COL(NZ_NUM), the row and column ! indices of the nonzero elements. ! ! Input, real ( kind = rk ) A(NZ_NUM), the nonzero elements of the matrix. ! ! Input, real ( kind = rk ) B(N), the right hand side of the linear system. ! ! Input/output, real ( kind = rk ) X(N), an approximate solution ! to the system. ! ! Input, integer IT_MAX, the maximum number of iterations. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n integer nz_num real ( kind = rk ) a(nz_num) real ( kind = rk ) b(n) integer col(nz_num) integer i integer it_max integer it_num integer j integer k integer row(nz_num) real ( kind = rk ) x(n) real ( kind = rk ) x_new(n) ! ! Ensure the matrix has diagonal terms first! ! call r8st_diagonal ( n, n, nz_num, row, col, a ) ! ! Carry out iteration. ! do it_num = 1, it_max ! ! Initialize to right hand side. ! x_new(1:n) = b(1:n) ! ! Subtract off-diagonal terms. ! do k = n + 1, nz_num i = row(k) j = col(k) x_new(i) = x_new(i) - a(k) * x(j) end do ! ! Divide by diagonal terms. ! x_new(1:n) = x_new(1:n) / a(1:n) ! ! Update. ! x(1:n) = x_new(1:n) end do return end subroutine r8st_mtv ( m, n, nz_num, row, col, a, x, b ) !*****************************************************************************80 ! !! r8st_MTV multiplies an R8VEC times an r8st matrix. ! ! Discussion: ! ! The r8st storage format stores the row, column and value of each nonzero ! entry of a sparse matrix. ! ! It is possible that a pair of indices (I,J) may occur more than ! once. Presumably, in this case, the intent is that the actual value ! of A(I,J) is the sum of all such entries. This is not a good thing ! to do, but I seem to have come across this in MATLAB. ! ! The r8st format is used by CSPARSE ("sparse triplet"), SLAP ! ("nonsymmetric SLAP triad"), by MATLAB, and by SPARSEKIT ("COO" format). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 21 January 2004 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, N, the number of rows and columns of ! the matrix. ! ! Input, integer NZ_NUM, the number of nonzero elements in ! the matrix. ! ! Input, integer ROW(NZ_NUM), COL(NZ_NUM), the row and column ! indices of the nonzero elements. ! ! Input, real ( kind = rk ) A(NZ_NUM), the nonzero elements of the matrix. ! ! Input, real ( kind = rk ) X(M), the vector to be multiplied by A. ! ! Output, real ( kind = rk ) B(N), the product vector A'*X. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer m integer n integer nz_num real ( kind = rk ) a(nz_num) real ( kind = rk ) b(n) integer col(nz_num) integer i integer j integer k integer row(nz_num) real ( kind = rk ) x(m) b(1:n) = 0.0D+00 do k = 1, nz_num i = row(k) j = col(k) b(j) = b(j) + a(k) * x(i) end do return end subroutine r8st_mv ( m, n, nz_num, row, col, a, x, b ) !*****************************************************************************80 ! !! r8st_MV multiplies an r8st matrix by an R8VEC. ! ! Discussion: ! ! The r8st storage format stores the row, column and value of each nonzero ! entry of a sparse matrix. ! ! It is possible that a pair of indices (I,J) may occur more than ! once. Presumably, in this case, the intent is that the actual value ! of A(I,J) is the sum of all such entries. This is not a good thing ! to do, but I seem to have come across this in MATLAB. ! ! The r8st format is used by CSPARSE ("sparse triplet"), SLAP ! ("nonsymmetric SLAP triad"), by MATLAB, and by SPARSEKIT ("COO" format). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 21 January 2004 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, N, the number of rows and columns of ! the matrix. ! ! Input, integer NZ_NUM, the number of nonzero elements in ! the matrix. ! ! Input, integer ROW(NZ_NUM), COL(NZ_NUM), the row and ! column indices of the nonzero elements. ! ! Input, real ( kind = rk ) A(NZ_NUM), the nonzero elements of the matrix. ! ! Input, real ( kind = rk ) X(N), the vector to be multiplied by A. ! ! Output, real ( kind = rk ) B(M), the product vector A*X. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer m integer n integer nz_num real ( kind = rk ) a(nz_num) real ( kind = rk ) b(m) integer col(nz_num) integer i integer j integer k integer row(nz_num) real ( kind = rk ) x(n) b(1:m) = 0.0D+00 do k = 1, nz_num i = row(k) j = col(k) b(i) = b(i) + a(k) * x(j) end do return end subroutine r8st_print ( m, n, nz_num, row, col, a, title ) !*****************************************************************************80 ! !! r8st_PRINT prints an r8st matrix. ! ! Discussion: ! ! This version of r8st_PRINT has been specifically modified to allow, ! and correctly handle, the case in which a single matrix location ! A(I,J) is referenced more than once by the sparse matrix structure. ! In such cases, the routine prints out the sum of all the values. ! ! The r8st storage format stores the row, column and value of each nonzero ! entry of a sparse matrix. ! ! It is possible that a pair of indices (I,J) may occur more than ! once. Presumably, in this case, the intent is that the actual value ! of A(I,J) is the sum of all such entries. This is not a good thing ! to do, but I seem to have come across this in MATLAB. ! ! The r8st format is used by CSPARSE ("sparse triplet"), SLAP ! ("nonsymmetric SLAP triad"), by MATLAB, and by SPARSEKIT ("COO" format). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 21 January 2004 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, N, the number of rows and columns of ! the matrix. ! ! Input, integer NZ_NUM, the number of nonzero elements in ! the matrix. ! ! Input, integer ROW(NZ_NUM), COL(NZ_NUM), the row and column ! indices of the nonzero elements. ! ! Input, real ( kind = rk ) A(NZ_NUM), the nonzero elements of the matrix. ! ! Input, character ( len = * ) TITLE, a title. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer nz_num real ( kind = rk ) a(nz_num) integer col(nz_num) integer m integer n integer row(nz_num) character ( len = * ) title call r8st_print_some ( m, n, nz_num, row, col, a, 1, 1, m, n, title ) return end subroutine r8st_print_some ( m, n, nz_num, row, col, a, ilo, jlo, & ihi, jhi, title ) !*****************************************************************************80 ! !! r8st_PRINT_SOME prints some of an r8st matrix. ! ! Discussion: ! ! This version of r8st_PRINT_SOME has been specifically modified to allow, ! and correctly handle, the case in which a single matrix location ! A(I,J) is referenced more than once by the sparse matrix structure. ! In such cases, the routine prints out the sum of all the values. ! ! The r8st storage format stores the row, column and value of each nonzero ! entry of a sparse matrix. ! ! It is possible that a pair of indices (I,J) may occur more than ! once. Presumably, in this case, the intent is that the actual value ! of A(I,J) is the sum of all such entries. This is not a good thing ! to do, but I seem to have come across this in MATLAB. ! ! The r8st format is used by CSPARSE ("sparse triplet"), SLAP ! ("nonsymmetric SLAP triad"), by MATLAB, and by SPARSEKIT ("COO" format). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 06 September 2006 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, N, the number of rows and columns ! of the matrix. ! ! Input, integer NZ_NUM, the number of nonzero elements ! in the matrix. ! ! Input, integer ROW(NZ_NUM), COL(NZ_NUM), the row and column ! indices of the nonzero elements. ! ! Input, real ( kind = rk ) A(NZ_NUM), the nonzero elements of the 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 nz_num real ( kind = rk ) a(nz_num) real ( kind = rk ) aij(incx) integer col(nz_num) integer i integer i2hi integer i2lo integer ihi integer ilo integer inc integer j integer j2 integer j2hi integer j2lo integer jhi integer jlo integer k integer m integer n integer row(nz_num) 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)' ) ' ' write ( *, '('' Col: '',5(i7,7x))' ) ( j, j = j2lo, j2hi ) 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. ! aij(1:inc) = 0.0D+00 ! ! Is matrix entry K actually the value of A(I,J), with J2LO <= J <= J2HI? ! Because MATLAB seems to allow for multiple (I,J,A) entries, we have ! to sum up what we find. ! do k = 1, nz_num if ( i == row(k) .and. & j2lo <= col(k) .and. & col(k) <= j2hi ) then j2 = col(k) - j2lo + 1 aij(j2) = aij(j2) + a(k) end if end do if ( any ( aij(1:inc) /= 0.0D+00 ) ) then write ( *, '(i5,1x,5g14.6)' ) i, aij(1:inc) end if end do end do return end subroutine r8st_random ( m, n, nz_num, row, col, a ) !*****************************************************************************80 ! !! r8st_RANDOM sets a random r8st matrix. ! ! Discussion: ! ! The r8st storage format stores the row, column and value of each nonzero ! entry of a sparse matrix. ! ! It is possible that a pair of indices (I,J) may occur more than ! once. Presumably, in this case, the intent is that the actual value ! of A(I,J) is the sum of all such entries. This is not a good thing ! to do, but I seem to have come across this in MATLAB. ! ! The r8st format is used by CSPARSE ("sparse triplet"), SLAP ! ("nonsymmetric SLAP triad"), by MATLAB, and by SPARSEKIT ("COO" format). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 22 January 2004 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, N, the number of rows and columns of ! the matrix. ! ! Input, integer NZ_NUM, the number of nonzero elements ! in the matrix. ! ! Input, integer ROW(NZ_NUM), COL(NZ_NUM), the row and column ! indices of the nonzero elements. ! ! Output, real ( kind = rk ) A(NZ_NUM), the nonzero elements of the matrix. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer nz_num real ( kind = rk ) a(nz_num) integer col(nz_num) integer m integer n integer row(nz_num) call i4_fake_use ( m ) call i4_fake_use ( n ) call i4_fake_use ( row(1) ) call i4_fake_use ( col(1) ) call random_number ( harvest = a(1:nz_num) ) return end subroutine r8st_read ( input_file, m, n, nz_num, row, col, a ) !*****************************************************************************80 ! !! r8st_READ reads an r8st matrix from a file. ! ! Discussion: ! ! This routine needs the value of NZ_NUM, which can be determined ! by a call to r8st_READ_SIZE. ! ! The r8st storage format stores the row, column and value of each nonzero ! entry of a sparse matrix. ! ! It is possible that a pair of indices (I,J) may occur more than ! once. Presumably, in this case, the intent is that the actual value ! of A(I,J) is the sum of all such entries. This is not a good thing ! to do, but I seem to have come across this in MATLAB. ! ! The r8st format is used by CSPARSE ("sparse triplet"), SLAP ! ("nonsymmetric SLAP triad"), by MATLAB, and by SPARSEKIT ("COO" format). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 03 August 2006 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) INPUT_FILE, the name of the file to be read. ! ! Unused, integer M, N, the number of rows and columns of the matrix. ! ! Input, integer NZ_NUM, the number of nonzero elements ! in the matrix. ! ! Output, integer ROW(NZ_NUM), COL(NZ_NUM), the row and ! column indices of the nonzero elements. ! ! Output, real ( kind = rk ) A(NZ_NUM), the nonzero elements ! of the matrix. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer nz_num real ( kind = rk ) a(nz_num) integer col(nz_num) character ( len = * ) input_file integer input_unit integer ios integer k integer m integer n integer row(nz_num) call i4_fake_use ( m ) call i4_fake_use ( n ) call get_unit ( input_unit ) open ( unit = input_unit, file = input_file, status = 'old', & iostat = ios ) if ( ios /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'r8st_READ - Fatal error!' write ( *, '(a)' ) ' Could not open the input file "' & // trim ( input_file ) // '".' stop 1 end if do k = 1, nz_num read ( input_unit, *, iostat = ios ) row(k), col(k), a(k) if ( ios /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'r8st_READ - Fatal error!' write ( *, '(a,i8)' ) ' I/O error while reading record ', k stop 1 end if end do close ( unit = input_unit ) return end subroutine r8st_read_size ( input_file, m, n, nz_num ) !*****************************************************************************80 ! !! r8st_READ_SIZE reads the size of an r8st matrix from a file. ! ! Discussion: ! ! The value of NZ_NUM is simply the number of records in the input file. ! ! The values of M and N are determined as the maximum entry in the row ! and column vectors. ! ! The r8st storage format stores the row, column and value of each nonzero ! entry of a sparse matrix. ! ! It is possible that a pair of indices (I,J) may occur more than ! once. Presumably, in this case, the intent is that the actual value ! of A(I,J) is the sum of all such entries. This is not a good thing ! to do, but I seem to have come across this in MATLAB. ! ! The r8st format is used by CSPARSE ("sparse triplet"), SLAP ! ("nonsymmetric SLAP triad"), by MATLAB, and by SPARSEKIT ("COO" format). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 03 August 2006 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, character ( len = * ) INPUT_FILE, the name of the file to ! be read. ! ! Output, integer M, N, the number of rows and columns ! of the matrix. ! ! Output, integer NZ_NUM, the number of nonzero elements ! in the matrix. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) real ( kind = rk ) a_k integer col_k character ( len = * ) input_file integer input_unit integer ios integer m integer n integer nz_num integer row_k call get_unit ( input_unit ) open ( unit = input_unit, file = input_file, status = 'old', & iostat = ios ) if ( ios /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'r8st_READ_SIZE - Fatal error!' write ( *, '(a)' ) ' Could not open the input file "' & // trim ( input_file ) // '".' stop 1 end if m = 0 n = 0 nz_num = 0 do read ( input_unit, *, iostat = ios ) row_k, col_k, a_k if ( ios /= 0 ) then exit end if nz_num = nz_num + 1 m = max ( m, row_k ) n = max ( n, col_k ) end do close ( unit = input_unit ) return end subroutine r8st_res ( m, n, nz_num, row, col, a, x, b, r ) !*****************************************************************************80 ! !! r8st_RES computes the residual R = B-A*X for r8st matrices. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 02 June 2014 ! ! 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, integer NZ_NUM, the number of nonzero elements in ! the matrix. ! ! Input, integer ROW(NZ_NUM), COL(NZ_NUM), the row and ! column indices of the nonzero elements. ! ! Input, real ( kind = rk ) A(NZ_NUM), the nonzero elements of the matrix. ! ! Input, real ( kind = rk ) X(N), the vector to be multiplied by A. ! ! Input, real ( kind = rk ) B(M), the desired result A * x. ! ! Output, real ( kind = rk ) R(M), the residual R = B - A * X. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer m integer n integer nz_num real ( kind = rk ) a(nz_num) real ( kind = rk ) b(m) integer col(nz_num) real ( kind = rk ) r(m) integer row(nz_num) real ( kind = rk ) x(n) call r8st_mv ( m, n, nz_num, row, col, a, x, r ) r(1:m) = b(1:m) - r(1:m) return end subroutine r8st_to_r8ge ( m, n, nz_num, row, col, a, b ) !*****************************************************************************80 ! !! r8st_TO_R8GE converts an r8st matrix to an R8GE matrix. ! ! Discussion: ! ! The r8st storage format stores the row, column and value of each nonzero ! entry of a sparse matrix. ! ! It is possible that a pair of indices (I,J) may occur more than ! once. Presumably, in this case, the intent is that the actual value ! of A(I,J) is the sum of all such entries. This is not a good thing ! to do, but I seem to have come across this in MATLAB. ! ! The r8st format is used by CSPARSE ("sparse triplet"), SLAP ! ("nonsymmetric SLAP triad"), by MATLAB, and by SPARSEKIT ("COO" format). ! ! 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: ! ! 22 January 2004 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, N, the number of rows and columns of ! the matrix. ! ! Input, integer NZ_NUM, the number of nonzero elements ! in the matrix. ! ! Input, integer ROW(NZ_NUM), COL(NZ_NUM), the row and column ! indices of the nonzero elements. ! ! Input, real ( kind = rk ) A(NZ_NUM), the nonzero elements of the matrix. ! ! Output, real ( kind = rk ) B(M,N), the R8GE matrix. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer m integer n integer nz_num real ( kind = rk ) a(nz_num) real ( kind = rk ) b(m,n) integer col(nz_num) integer k integer row(nz_num) b(1:m,1:n) = 0.0D+00 do k = 1, nz_num b(row(k),col(k)) = a(k) end do return end subroutine r8st_to_r8ncf ( m, n, nz_num, row, col, a, rowcol ) !*****************************************************************************80 ! !! r8st_TO_R8NCF converts an r8st matrix to an R8NCF matrix. ! ! Discussion: ! ! The r8st and R8NCF formats are essentially identical, except that ! r8st keeps separate ROW and COLUMN vectors, while R8NCF uses a single ! ROWCOL array. Therefore, the input values NZ_NUM and A used in ! the r8st representation can be regarded as part of the output ! values used for the R8NCF representation. ! ! The r8st storage format stores the row, column and value of each nonzero ! entry of a sparse matrix. ! ! It is possible that a pair of indices (I,J) may occur more than ! once. Presumably, in this case, the intent is that the actual value ! of A(I,J) is the sum of all such entries. This is not a good thing ! to do, but I seem to have come across this in MATLAB. ! ! The r8st format is used by CSPARSE ("sparse triplet"), SLAP ! ("nonsymmetric SLAP triad"), by MATLAB, and by SPARSEKIT ("COO" format). ! ! The R8NCF storage format stores NZ_NUM, the number of nonzeros, ! a real array containing the nonzero values, a 2 by NZ_NUM integer ! array storing the row and column of each nonzero entry. ! ! The R8NCF format is used by NSPCG. NSPCG requires that the information ! for the diagonal entries of the matrix must come first. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 02 August 2006 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Unused, integer M, N, the number of rows and columns of the matrix. ! ! Input, integer NZ_NUM, the number of nonzero elements ! in the matrix. ! ! Input, integer ROW(NZ_NUM), COL(NZ_NUM), the row and column ! indices of the nonzero elements. ! ! Input, real ( kind = rk ) A(NZ_NUM), the nonzero elements of the matrix. ! ! Output, integer ROWCOL(2,NZ_NUM), the R8NCF row and column ! index vector. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer nz_num real ( kind = rk ) a(nz_num) integer col(nz_num) integer m integer n integer row(nz_num) integer rowcol(2,nz_num) call i4_fake_use ( m ) call i4_fake_use ( n ) call r8_fake_use ( a(1) ) rowcol(1,1:nz_num) = row(1:nz_num) rowcol(2,1:nz_num) = col(1:nz_num) return end subroutine r8st_write ( m, n, nz_num, row, col, a, output_file ) !*****************************************************************************80 ! !! r8st_WRITE writes an r8st matrix to a file. ! ! Discussion: ! ! The r8st storage format stores the row, column and value of each nonzero ! entry of a sparse matrix. ! ! It is possible that a pair of indices (I,J) may occur more than ! once. Presumably, in this case, the intent is that the actual value ! of A(I,J) is the sum of all such entries. This is not a good thing ! to do, but I seem to have come across this in MATLAB. ! ! The r8st format is used by CSPARSE ("sparse triplet"), SLAP ! ("nonsymmetric SLAP triad"), by MATLAB, and by SPARSEKIT ("COO" format). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 02 September 2006 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, N, the number of rows and columns of ! the matrix. ! ! Input, integer NZ_NUM, the number of nonzero elements in ! the matrix. ! ! Input, integer ROW(NZ_NUM), COL(NZ_NUM), the row and column ! indices of the nonzero elements. ! ! Input, real ( kind = rk ) A(NZ_NUM), the nonzero elements ! of the matrix. ! ! Input, character ( len = * ) OUTPUT_FILE, the name of the file to which ! the information is to be written. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer nz_num real ( kind = rk ) a(nz_num) integer col(nz_num) integer ios integer k integer m integer n character ( len = * ) output_file integer output_unit integer row(nz_num) call i4_fake_use ( m ) call i4_fake_use ( n ) call get_unit ( output_unit ) open ( unit = output_unit, file = output_file, status = 'replace', & iostat = ios ) if ( ios /= 0 ) then write ( *, '(a)' ) ' ' write ( *, '(a)' ) 'r8st_WRITE - Fatal error!' write ( *, '(a)' ) ' Could not open the output file "' & // trim ( output_file ) // '".' stop 1 end if do k = 1, nz_num write ( output_unit, '(2x,i8,2x,i8,2x,g16.8)' ) row(k), col(k), a(k) end do close ( unit = output_unit ) return end subroutine r8st_zeros ( m, n, nz_num, row, col, a ) !*****************************************************************************80 ! !! r8st_ZEROS zeros out an r8st matrix. ! ! Discussion: ! ! The r8st storage format stores the row, column and value of each nonzero ! entry of a sparse matrix. ! ! Perhaps we must point out that when we say "nonzero" elements in the ! discussion below, we mean "potentially nonzero" elements. In other words, ! the sparse matrix sets aside space for elements that are allowed to be ! nonzero, but may of course take on zero values as well. It's actually ! really the entries for which we don't set aside space that we are sure ! about. Those entries are zero. ! ! The r8st format is used by CSPARSE ("sparse triplet"), SLAP ! ("nonsymmetric SLAP triad"), by MATLAB, and by SPARSEKIT ("COO" format). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 07 April 2012 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, N, the number of rows and columns of ! the matrix. ! ! Input, integer NZ_NUM, the number of nonzero elements in ! the matrix. ! ! Input, integer ROW(NZ_NUM), COL(NZ_NUM), the row and column ! indices of the nonzero elements. ! ! Output, real ( kind = rk ) A(NZ_NUM), the nonzero elements of the matrix. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer m integer n integer nz_num real ( kind = rk ) a(nz_num) integer col(nz_num) integer row(nz_num) call i4_fake_use ( m ) call i4_fake_use ( n ) call i4_fake_use ( col(1) ) call i4_fake_use ( row(1) ) a(1:nz_num) = 0.0D+00 return end subroutine r8vec_indicator1 ( n, a ) !*****************************************************************************80 ! !! R8VEC_INDICATOR1 sets an R8VEC to the indicator1 vector. ! ! Discussion: ! ! A(1:N) = (/ 1 : N /) ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 06 September 2006 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer N, the number of elements of A. ! ! Output, real ( kind = rk ) A(N), the array to be initialized. ! 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 subroutine r8vec_print ( n, a, title ) !*****************************************************************************80 ! !! R8VEC_PRINT prints an R8VEC. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 16 December 1999 ! ! 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 ( *, '(i8,g14.6)' ) 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: ! ! 16 September 2003 ! ! 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 if ( all ( a(1:n) == aint ( a(1:n) ) ) ) then do i = 1, n write ( *, '(i8,2x,i8)' ) i, int ( a(i) ) end do else if ( all ( abs ( a(1:n) ) < 1000000.0D+00 ) ) then do i = 1, n write ( *, '(i8,2x,f14.6)' ) i, a(i) end do else do i = 1, n write ( *, '(i8,2x,g14.6)' ) i, a(i) end do end if else if ( 3 <= max_print ) then if ( all ( a(1:max_print-2) == aint ( a(1:max_print-2) ) ) ) then do i = 1, max_print - 2 write ( *, '(i8,2x,i8)' ) i, int ( a(i) ) end do else if ( all ( abs ( a(1:max_print-2) ) < 1000000.0D+00 ) ) then do i = 1, max_print - 2 write ( *, '(i8,2x,f14.6)' ) i, a(i) end do else do i = 1, max_print - 2 write ( *, '(i8,2x,g14.6)' ) i, a(i) end do end if write ( *, '(a)' ) '...... ..............' i = n if ( a(i) == real ( int ( a(i) ), kind = rk ) ) then write ( *, '(i8,2x,i8)' ) i, int ( a(i) ) else if ( abs ( a(i) ) < 1000000.0D+00 ) then write ( *, '(i8,2x,f14.6)' ) i, a(i) else write ( *, '(i8,2x,g14.6)' ) i, a(i) end if else if ( all ( a(1:max_print-1) == aint ( a(1:max_print-1) ) ) ) then do i = 1, max_print - 1 write ( *, '(i8,2x,i8)' ) i, int ( a(i) ) end do else if ( all ( abs ( a(1:max_print-1) ) < 1000000.0D+00 ) ) then do i = 1, max_print - 1 write ( *, '(i8,2x,f14.6)' ) i, a(i) end do else do i = 1, max_print - 1 write ( *, '(i8,2x,g14.6)' ) i, a(i) end do end if i = max_print if ( a(i) == aint ( a(i) ) ) then write ( *, '(i8,2x,i8,a)' ) i, int ( a(i) ), '...more entries...' else if ( abs ( a(i) ) < 1000000.0D+00 ) then write ( *, '(i8,2x,f14.6,a)' ) i, a(i), '...more entries...' else write ( *, '(i8,2x,g14.6,a)' ) i, a(i), '...more entries...' end if 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.2,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