Print a packed lower triangular matrix of values
n_elements must be n*(n + 1)/2 for some n. If it is not, nothing is printed: err is set to ERROR_VALIDATION when it is present, and the complaint goes to stdout when it is not. Neither path aborts, which is how this routine has always behaved.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=int32), | intent(in) | :: | packed(:) | |||
| integer(kind=default_int), | intent(in) | :: | n_elements | |||
| character(len=*), | intent(in), | optional | :: | format_type | ||
| type(error_t), | intent(inout), | optional | :: | err |
set to ERROR_VALIDATION when n_elements is not a packed triangle size, or when format_type is not supported |
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| character(len=1), | private | :: | close_bracket | ||||
| integer(kind=default_int), | private | :: | i | ||||
| integer(kind=default_int), | private | :: | idx | ||||
| integer(kind=default_int), | private | :: | j | ||||
| integer(kind=default_int), | private | :: | n | ||||
| real(kind=dp), | private | :: | n_real | ||||
| character(len=1), | private | :: | open_bracket | ||||
| character(len=20), | private | :: | print_format |
subroutine print_packed_matrix_int32(packed, n_elements, format_type, err) !! Print a packed lower triangular matrix of ${T} values !! !! n_elements must be n*(n + 1)/2 for some n. If it is not, nothing is !! printed: err is set to ERROR_VALIDATION when it is present, and the !! complaint goes to stdout when it is not. Neither path aborts, which is !! how this routine has always behaved. integer(int32), intent(in) :: packed(:) integer(default_int), intent(in) :: n_elements character(len=*), intent(in), optional :: format_type type(error_t), intent(inout), optional :: err !! set to ERROR_VALIDATION when n_elements is not a packed triangle !! size, or when format_type is not supported character(len=20) :: print_format character(len=1) :: open_bracket, close_bracket integer(default_int) :: i, j, idx, n real(dp) :: n_real ! Determine format print_format = pic_optional(format_type, default_format) call set_brackets(print_format, open_bracket, close_bracket, err) ! Compute n from packed size using proper real arithmetic n_real = (-1.0_dp + sqrt(1.0_dp + 8.0_dp*real(n_elements, dp)))/2.0_dp n = int(n_real + 0.5_dp, default_int) if (n*(n + 1)/2 /= n_elements) then if (present(err)) then call error_raise(err, ERROR_VALIDATION, "pic_print_array: n_elements does not form a valid packed triangle") else print *, "Error: n_elements does not form a valid packed triangle" end if return end if ! Print lower triangle directly from packed array print *, open_bracket idx = 0 do i = 1, n write (*, "(A)", advance="no") open_bracket do j = 1, i idx = idx + 1 if (j == i) then write (*, "(A)", advance="no") to_char(packed(idx)) else write (*, "(A)", advance="no") trim(to_char(packed(idx))//", ") end if end do if (i == n) then print *, close_bracket else print *, close_bracket, "," end if end do print *, close_bracket end subroutine print_packed_matrix_int32