print_matrix Subroutine

public subroutine print_matrix(matrix, iunit)

Arguments

Type IntentOptional Attributes Name
class(*), intent(in) :: matrix(:,:)
integer, intent(in), optional :: iunit

Contents

Source Code


Source Code

    subroutine print_matrix(matrix, iunit)
        ! print a 2-D real matrix
        class(*), intent(in) :: matrix(:,:)
        integer, intent(in), optional :: iunit

        integer :: i, j, tmp_unit

        select type (matrix)
        type is (integer)
            if (present(iunit)) then
                do i = lbound(matrix,1), ubound(matrix,1)
                    write(iunit,*) matrix(i,:)
                end do
            else
                do i = lbound(matrix,1), ubound(matrix,1)
                    print *, matrix(i,:)
                end do
            end if
        type is (real(dp))
            if (present(iunit)) then
                do i = lbound(matrix,1),ubound(matrix,1)
                    do j = lbound(matrix,2), ubound(matrix,2) - 1
                        write(iunit,'(G25.17)', advance = 'no') matrix(i,j)
                    end do
                    write(iunit,'(G25.17)', advance = 'yes') matrix(i,j)
                end do
            else
                do i = lbound(matrix,1),ubound(matrix,1)
                    print *, matrix(i,:)
                end do
            end if
        type is (complex(dp))
            if (present(iunit)) then
                tmp_unit = iunit
            else
                tmp_unit = 6
            end if
            do i = lbound(matrix,1),ubound(matrix,1)
                do j = lbound(matrix,2), ubound(matrix,2)
                    if (j < ubound(matrix,2)) then
                        write(tmp_unit,fmt = '(F10.8,SP,F10.8,"i",1x)', advance = 'no') matrix(i,j)
                    else
                        write(tmp_unit,fmt = '(F10.8,SP,F10.8,"i")', advance = 'yes') matrix(i,j)
                    end if
                end do
            end do
        end select


    end subroutine print_matrix