array_str

odl.util.utility.array_str(a, nprint=6)[source]

Stringification of an array.

Parameters:
aarray-like

The array to print.

nprintint, optional

Maximum number of elements to print per axis in a. For larger arrays, a summary is printed, with nprint // 2 elements on each side and ... in the middle (per axis).

Examples

Printing 1D arrays:

>>> print(array_str(np.arange(4)))
[0, 1, 2, 3]
>>> print(array_str(np.arange(10)))
[0, 1, 2, ..., 7, 8, 9]
>>> print(array_str(np.arange(10), nprint=10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

For 2D and higher, the nprint limitation applies per axis:

>>> print(array_str(np.arange(24).reshape(4, 6)))
[[ 0,  1,  2,  3,  4,  5],
 [ 6,  7,  8,  9, 10, 11],
 [12, 13, 14, 15, 16, 17],
 [18, 19, 20, 21, 22, 23]]
>>> print(array_str(np.arange(32).reshape(4, 8)))
[[ 0,  1,  2, ...,  5,  6,  7],
 [ 8,  9, 10, ..., 13, 14, 15],
 [16, 17, 18, ..., 21, 22, 23],
 [24, 25, 26, ..., 29, 30, 31]]
>>> print(array_str(np.arange(32).reshape(8, 4)))
[[ 0,  1,  2,  3],
 [ 4,  5,  6,  7],
 [ 8,  9, 10, 11],
 ...,
 [20, 21, 22, 23],
 [24, 25, 26, 27],
 [28, 29, 30, 31]]
>>> print(array_str(np.arange(64).reshape(8, 8)))
[[ 0,  1,  2, ...,  5,  6,  7],
 [ 8,  9, 10, ..., 13, 14, 15],
 [16, 17, 18, ..., 21, 22, 23],
 ...,
 [40, 41, 42, ..., 45, 46, 47],
 [48, 49, 50, ..., 53, 54, 55],
 [56, 57, 58, ..., 61, 62, 63]]

Printing of empty arrays and 0D arrays:

>>> print(array_str(np.array([])))  # 1D, size=0
[]
>>> print(array_str(np.array(1.0)))  # 0D, size=1
1.0

Small deviations from round numbers will be suppressed:

>>> # 2.0000000000000004 in double precision
>>> print(array_str((np.array([2.0]) ** 0.5) ** 2))
[ 2.]