Numpy has many nice features for formatted output, but something I miss is the ability to print more than one array/matrix on the same line. What I mean is easiest to explain with an example. Given the following code:
A = np.random.randint(10, size = (4, 4))
B = np.random.randint(10, size = (4, 4))
niceprint(A, "*", B, "=", A @ B)
How can you implement niceprint
so that it prints the following on the console?
[[7 1 0 4] [[8 6 2 6] [[ 80 45 54 83]
[8 1 5 8] * [0 3 8 5] = [147 91 123 140]
[3 7 2 4] [7 8 7 3] [ 62 55 108 95]
[8 8 2 8]] [6 0 8 9]] [126 88 158 166]]
Don't write it yourself; use something common and off-the-shelf like Sympy.
import numpy as np
import sympy
A = sympy.Matrix(np.random.randint(10, size=(4, 4)))
B = sympy.Matrix(np.random.randint(10, size=(4, 4)))
sympy.pretty_print(
sympy.Eq(
sympy.MatMul(A, B),
A @ B,
)
)
⎡4 2 1 5⎤ ⎡3 0 7 7⎤ ⎡77 54 42 58 ⎤
⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎢1 6 5 1⎥ ⎢9 5 0 1⎥ ⎢76 82 29 57 ⎥
⎢ ⎥⋅⎢ ⎥ = ⎢ ⎥
⎢3 3 7 0⎥ ⎢2 9 4 8⎥ ⎢50 78 49 80 ⎥
⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎣7 6 2 8⎦ ⎣9 7 2 4⎦ ⎣151 104 73 103⎦