stdlib icon indicating copy to clipboard operation
stdlib copied to clipboard

stdlib_io: add `print_array` function to print arrays to output units

Open zoziha opened this issue 8 months ago • 1 comments

Description

Add print_array subroutine, which is used to print two-dimensional arrays of integer and floating-point types. These two types are more commonly used in Fortran. It supports arguments such as delimiter, output unit, output format, and whether to print briefly.

In the non-brief output mode, the print_array function can produce an output result style similar to that of savetxt in stdlib_io. The print_array function is mainly convenient for users to output to the screen (standard output) and is likely to be mostly used for code debugging.

Prior Art:

  • disp in Matlab;
  • The default print of Python and Julia;
  • brief_print in Armadillo.

This PR will replace #520, and is related to #40, but is not sufficient to close #40.

Remaining to be discussed

  • Is it necessary to support delimiters of any length, not just those with a length of 1? In addition to the print_array, it also includes the savetxt routine in stdlib_io.
  • Currently, print_array is designed to assist users in program debugging and does not provide file write error reports like savetxt does.

zoziha avatar Apr 20 '25 12:04 zoziha

Hi, I noticed that complex arrays are printed as separate real and imaginary parts, rather than (a,b) or a + bi.

complex, dimension(2, 2) :: a = reshape([cmplx(1,2), cmplx(3,4), cmplx(5,6), cmplx(7,8)], [2,2])
print *, a

print "(a)", "=== print_array 1 ==="
call print_array(a, unit=6, fmt='(f7.1)', delimiter='|', brief=.false.)

The above code displayed

             (1.00000000,2.00000000)             (3.00000000,4.00000000)             (5.00000000,6.00000000)             (7.00000000,8.00000000)
=== print_array 1 ===
    1.0|    2.0|    5.0|    6.0
    3.0|    4.0|    7.0|    8.0

For readability, it might be nicer to display them as in brackets or in a+bi form. One idea could be to add an optional complex_format flag (or similar) that controls whether complex numbers are printed as (a,b) or a + bi.

Mahmood-Sinan avatar Oct 13 '25 12:10 Mahmood-Sinan