Multiply Matrices Using NumPy

learnwebgl.brown37.net/transformations2/transformations_matrices.html

#! /usr/bin/python3
# ===================================================================
# NumPy matrix multiplication
# ===================================================================
#
# ---- Identity:
#
# tranI  = np.array([ [1, 0, 0, 0],
#                     [0, 1, 0, 0],
#                     [0, 0, 1, 0],
#                     [0, 0, 0, 1] ])
#
# ---- Scale:
#
# tranS  = np.array([ [sx,  0,  0, 0],
#                     [ 0, sy,  0, 0],
#                     [ 0,  0, sz, 0],
#                     [ 0,  0,  0, 1] ])
#
# ---- Translate:
#
# Translation equations:
# x + tx = x'      # x translation equation
# y + ty = y'      # y translation equation
# z + tz = z'      # z translation equation
#
# Translation matrix:
# tranT  = np.array([ [1, 0, 0, tx],
#                     [0, 1, 0, ty],
#                     [0, 0, 1, tz],
#                     [0, 0, 0,  1] ])
#
# ---- Rotate:
#
# deg = 36.0                   # degrees
# rad = np.deg2rad(deg)        # radians
#
# ---- Rotate around the Z axis
#
# transZ  = np.array([ [np.cos(rad), -np.sin(rad), 0, 0],
#                      [np.sin(rad),  np.cos(rad), 0, 0],
#                      [0,                      0, 1, 0],
#                      [0,                      0, 0, 1] ])
#
# ---- Rotate around the Y axis
#
# transY  = np.array([ [ np.cos(rad), np.sin(rad), 0, 0],
#                      [           0,          1,  0, 0],
#                      [-np.sin(rad), np.cos(rad), 0, 0],
#                      [           0,           0, 0, 1] ])
#
# ---- Rotate around the X axis
#
# transX  = np.array([ [          1,            0, 0, 0]'
#                      [np.cos(rad), -np.sin(rad), 0, 0],
#                      [np.sin(rad),  np.cos(rad), 0, 0],
#                      [          0,            0, 0, 1] ])
#
# ===================================================================

import numpy as np

x = 100                        # x coordinate
y = 100                        # y coordinate
z = 100                        # z coordinate
s = 2                          # scale factor

deg = 90
rad = np.deg2rad(deg)

print()
print('---- 3D Scale --------------------------------------')

trans  = np.array([ [s, 0, 0, 0],
                    [0, s, 0, 0],
                    [0, 0, s, 0],
                    [0, 0, 0, 1] ])

coords = np.array([x,y,z,1])

m = np.matmul(trans,coords)    # matrix multiplication
                               # trans * coords
print(m)

m = np.matmul(coords,trans)    # matrix multiplication
                               # coords * trans
print(m)

print('---- 2D Scale --------------------------------------')

trans2  = np.array([ [s, 0, 0],
                     [0, s, 0],
                     [0, 0, 1] ])

coords2 = np.array([x,y,1])

m = np.matmul(trans2,coords2)  # matrix multiplication
                               # trans * coords
print(m)

print()
print('---- 3D Translate ----------------------------------')

tx = 20.0                      # x translation
ty = 30.0                      # y translation
tz = 40.0                      # z translation

trans  = np.array([ [1, 0, 0, tx],
                    [0, 1, 0, ty],
                    [0, 0, 1, tz],
                    [0, 0, 0, 1 ] ])

coords = np.array([x,y,z,1])

m = np.matmul(trans,coords)    # matrix multiplication
                               # trans * coords
print(m)

print('---- 2D Translate ----------------------------------')

trans2  = np.array([ [1, 0, tx],
                     [0, 1, ty],
                     [0, 0, 1 ] ])

coords2 = np.array([x,y,1])

m = np.matmul(trans2,coords2)  # matrix multiplication
                               # trans * coords
print(m)

print()
print('---- 3D Rotate Around Z Axis -----------------------')

trans3  = np.array([ [np.cos(rad), -np.sin(rad), 0, 0],
                     [np.sin(rad),  np.cos(rad), 0, 0],
                     [0,                      0, 1, 0],
                     [0,                      0, 0, 1] ])

coords3 = np.array([x,y,z,1])

m = np.matmul(trans3,coords3)  # matrix multiplication
                               # trans * coords
print(m)

print('---- 2D Rotate Around Z Axis -----------------------')

trans4  = np.array([ [np.cos(rad), -np.sin(rad), 0],
                     [np.sin(rad),  np.cos(rad), 0],
                     [0,                      0, 1] ])

coords4 = np.array([x,y,1])

m = np.matmul(trans4,coords4)  # matrix multiplication
                               # trans * coords
print(m)

print()
print('---- Do Nothing ------------------------------------')

trans5  = np.array([ [1, 0, 0, 0],
                     [0, 1, 0, 0],
                     [0, 0, 1, 0],
                     [0, 0, 0, 1] ])

coords5 = np.array([x,y,z,1])

m = np.matmul(trans5,coords5)  # matrix multiplication
                               # trans * coords
print(m)


print()
print('---- Rotate Then Translate in X --------------------')

coordsC = np.array([x,y,z,1])

print(coordsC)

tx = -50                       # x translation
ty = 0                         # y translation
tz = 0                         # z translation


transR  = np.array([ [np.cos(rad), -np.sin(rad), 0, 0],
                     [np.sin(rad),  np.cos(rad), 0, 0],
                     [0,                      0, 1, 0],
                     [0,                      0, 0, 1] ])

transT = np.array([ [1, 0, 0, tx],
                    [0, 1, 0, ty],
                    [0, 0, 1, tz],
                    [0, 0, 0, 1 ] ])

print(f'---- rotate {deg} (z axis)')

m = np.matmul(transR,coordsC)  # matrix multiplication
                               # trans * coords
print(m)

print(f'---- translate {tx},{ty},{tz}')

m = np.matmul(transT,coordsC)  # matrix multiplication
                               # trans * coords
print(m)

print(f'---- rotate {deg} then translate {tx},{ty},{tz}')

m = np.matmul(transT,transR)   # matrix multiplication
                               # transR * transT

mm = np.matmul(m,coordsC)      # matrix multiplication
                               # m * coords
print(mm)

print()
print(f'X={mm[0]}')
print(f'Y={mm[1]}')
print(f'Z={mm[2]}')
print()