Python to perform arithmetic operations on two matrices
kodingwindow@kw:~$ python3
...
>>> from numpy import *
>>> m1 = matrix("1 2 3; 4 5 6; 7 8 9")
>>> m2 = matrix("9 8 7; 6 5 4; 3 2 1")
>>> m1
matrix([[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]])
>>> m2
matrix([[9, 8, 7],
        [6, 5, 4],
        [3, 2, 1]])

>>> m1 + m2
matrix([[10, 10, 10],
        [10, 10, 10],
        [10, 10, 10]])

>>> m1 - m2
matrix([[-8, -6, -4],
        [-2,  0,  2],
        [ 4,  6,  8]])

>>> m1 * m2
matrix([[ 30,  24,  18],
        [ 84,  69,  54],
        [138, 114,  90]])

>>> m1 / m2
matrix([[0.11111111, 0.25      , 0.42857143],
        [0.66666667, 1.        , 1.5       ],
        [2.33333333, 4.        , 9.        ]])
Advertisement