Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real(kind=sp), | intent(in) | :: | array(:,:) |
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
integer, | private | :: | i | ||||
integer, | private | :: | j | ||||
integer, | private | :: | ncols | ||||
integer, | private | :: | nrows | ||||
character(len=32), | private | :: | style | ||||
character(len=50), | private | :: | temp_str | ||||
integer, | private | :: | total_len |
function to_string_matrix_sp(array) result(trimmed_str) real(kind=sp), intent(in) :: array(:, :) character(len=:), allocatable :: trimmed_str character(len=50) :: temp_str character(len=32) :: style integer :: i, j, total_len, nrows, ncols nrows = size(array, 1) ncols = size(array, 2) ! Set up format write (style, '(A,I0,A)') '(F0.', sp_precision, ')' ! Estimate total length needed total_len = 10 + nrows ! for outer brackets and newlines do i = 1, nrows total_len = total_len + 3 ! for row brackets and comma do j = 1, ncols write (temp_str, style) array(i, j) total_len = total_len + len_trim(temp_str) + 2 ! +2 for ", " end do end do ! Allocate result string allocate (character(len=total_len) :: trimmed_str) ! Build the string with newlines trimmed_str = "["//new_line('a') do i = 1, nrows trimmed_str = trimmed_str//" [" do j = 1, ncols write (temp_str, style) array(i, j) if (j < ncols) then trimmed_str = trimmed_str//trim(temp_str)//", " else trimmed_str = trimmed_str//trim(temp_str) end if end do if (i < nrows) then trimmed_str = trimmed_str//"],"//new_line('a') else trimmed_str = trimmed_str//"]"//new_line('a') end if end do trimmed_str = trimmed_str//"]" end function to_string_matrix_sp