Skip to content

Instantly share code, notes, and snippets.

@Tuhin-thinks
Last active November 24, 2022 18:33
Show Gist options
  • Save Tuhin-thinks/33c8ecab8a00801763ad29a7d7866646 to your computer and use it in GitHub Desktop.
Save Tuhin-thinks/33c8ecab8a00801763ad29a7d7866646 to your computer and use it in GitHub Desktop.
A utility for matrix operation
import pprint
from math import radians, sin, cos
import numpy as np
class Angle:
"""
class for interconversion of angle in degrees and radians
"""
def __init__(self, angle) -> None:
"""
:param angle: Angle in degrees
"""
self.angle = angle
@property
def radians(self):
return radians(self.angle)
@property
def degrees(self):
return self.angle
def trig(angle: Angle):
return sin(angle.radians), cos(angle.radians)
def print_matrix(transform, message=None, indent=10):
if message:
print(message)
matrix_4x4_obj = transform
for i in range(4):
_row = []
for j in range(4):
el = matrix_4x4_obj[i][j]
if type(el) == str:
item = el
else:
item = round(el, 3)
_row.append(str(item))
format_row = ("{:" + "^" + str(indent) + "}") * len(_row)
print(format_row.format(*_row))
def transpose_4x4_matrix(matrix_4x4):
transponsed_matrix_4x4 = list([list([0 for j in range(4)]) for i in range(4)])
for i in range(4):
for j in range(4):
transponsed_matrix_4x4[i][j] = matrix_4x4[j][i]
return transponsed_matrix_4x4
def matrix_dot_product(mat_1, mat_2):
return list(np.dot(mat_1, mat_2)) # type: ignore
def matrix_str_dot_product(mat_1, mat_2):
def solve_prod_01(tup):
_elems = []
_has_zero = False
for _el in tup:
try:
if int(_el) == 0:
return "0", True
elif int(_el) == 1:
pass
except ValueError:
_elems.append(_el)
return "*".join(_elems) or "1", False
m2_shape = len(mat_2), len(mat_2[0])
m1_shape = len(mat_1), len(mat_1[0])
# basic condition checking for matrix multiplication feasibility
if m1_shape[1] != m2_shape[0]:
raise ValueError(f"Number of rows in first matrix should match with number of columns in second matrix."
f" {m1_shape[1]} != {m2_shape[0]}")
new_mat = [list([0] * m2_shape[1]) for _ in range(m2_shape[0])]
for ri_1 in range(m1_shape[0]):
for it in range(m2_shape[1]):
nm_row = []
for x in range(m1_shape[1]):
el1 = mat_1[ri_1][x]
el2 = mat_2[x][it]
expr, has_zero = solve_prod_01((el1, el2))
if not has_zero:
nm_row.append(expr)
_expression = "+".join(nm_row)
_expr_str = ""
if len(nm_row) > 1:
_expr_str = f"({_expression})"
else:
_expr_str = _expression
new_mat[ri_1][it] = _expr_str if _expression else 0
return new_mat
if __name__ == '__main__':
v1 = [
['-l2*sin_a1'],
[0],
['l2*cos_a1'],
[1]
]
m1 = [['cos_a3*cos_a2', '-sin_a3', 'cos_a3*sin_a2', 0],
['(cos_-a4*sin_a3*cos_a2+sin_a4*-sin_a2)',
'cos_-a4*cos_a3',
'(cos_-a4*sin_a3*sin_a2+sin_a4*cos_a2)',
0],
['(-sin_a4*sin_a3*cos_a2+cos_a4*-sin_a2)',
'-sin_a4*cos_a3',
'(-sin_a4*sin_a3*sin_a2+cos_a4*cos_a2)',
0],
[0, 0, 0, '1']]
pprint.PrettyPrinter().pprint(matrix_str_dot_product(m1, v1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment