private subroutine to print a matrix in format
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real(kind=dp), | intent(in) | :: | mat(:,:) | |||
character(len=*), | intent(in) | :: | format_type |
format can be mathematica or numpy |
||
integer(kind=default_int), | intent(in), | optional | :: | n_cols | ||
integer(kind=default_int), | intent(in), | optional | :: | n_rows |
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
character(len=1), | private | :: | close_bracket | ||||
integer(kind=default_int), | private | :: | i | ||||
integer(kind=default_int), | private | :: | j | ||||
integer(kind=default_int), | private | :: | loop_bound_i | ||||
integer(kind=default_int), | private | :: | loop_bound_j | ||||
character(len=1), | private | :: | open_bracket |
subroutine print_matrix_in_format(mat, format_type, n_cols, n_rows) !! private subroutine to print a matrix in format real(kind=dp), intent(in) :: mat(:, :) character(len=*), intent(in) :: format_type !! format can be mathematica or numpy character(len=1) :: open_bracket, close_bracket integer(kind=default_int), intent(in), optional :: n_cols, n_rows integer(kind=default_int) :: i, j, loop_bound_i, loop_bound_j if (present(n_cols) .and. present(n_rows)) then loop_bound_i = n_cols loop_bound_j = n_rows else loop_bound_i = size(mat, 1) loop_bound_j = size(mat, 2) end if ! Select brackets based on format type if (format_type == "NUMPY") then open_bracket = "[" close_bracket = "]" else if (format_type == "MATHEMATICA") then open_bracket = "{" close_bracket = "}" else print *, "Error: Unsupported format type. Defaulting to NumPy format." open_bracket = "[" close_bracket = "]" end if ! Print the matrix in the selected format print *, "Matrix (", trim(format_type), " format):" print *, open_bracket do i = 1, loop_bound_i write (*, "(A)", advance="no") open_bracket ! Start of a row do j = 1, loop_bound_j if (j == loop_bound_j) then ! Last element in the row write (*, fmt_edge, advance="no") mat(i, j) else ! Elements in between write (*, fmt_in, advance="no") mat(i, j) end if end do if (i == loop_bound_i) then print *, close_bracket ! Close bracket without a comma for the last row else print *, close_bracket, "," ! Close bracket with a comma for all other rows end if end do print *, close_bracket end subroutine print_matrix_in_format