Skip to content

Commit

Permalink
Passing of unknown attributes to SymPy matrix class
Browse files Browse the repository at this point in the history
  • Loading branch information
alopezrivera committed Dec 16, 2021
1 parent 4f84a65 commit e691e56
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
from alexandria.shell import print_color


class R:
class M:
"""
General rotation matrix
-----------------------
General matrix class
--------------------
"""
def __init__(self, matrix, params):
"""
Expand All @@ -29,6 +29,24 @@ def __init__(self, matrix, params):
self.matrix = matrix
self.params = params

def __getitem__(self, item):
"""
Retrieve
"""
return self.matrix[item]

def __getattr__(self, item):
"""
Pass unknown attributes to matrix class
"""
if item in self.__dict__.keys():
return self.item
else:
try:
self.matrix.__dict__[item]
except KeyError:
raise AttributeError(f"{self.matrix.__class__} has no attribute <{item}>")

"""
Properties
"""
Expand All @@ -55,6 +73,10 @@ def I(self):
inv_matrix.subs(param, -param)
return R(matrix=inv_matrix, params=self.params)

def diff(self):
dm = sp.diff(self.matrix, self.params)
return R(matrix=dm, params=self.params | dm.free_symbols)

"""
Operators
"""
Expand All @@ -78,9 +100,6 @@ def __mul__(self, other):
def __add__(self, other):
return self._operate(other, lambda m1, m2: m1 + m2)

def __getitem__(self, item):
return self.matrix.item(item)

"""
Export
"""
Expand Down Expand Up @@ -168,9 +187,10 @@ def latex(self, var="T"):
f"\n {var} = {sp.latex(self.matrix)}\n" + \
r"\end{equation}"
print_color(lx, "blue")
return lx


class T(R):
class T(M):
"""
Transformation Matrix
---------------------
Expand All @@ -181,9 +201,7 @@ class T(R):
"""
def __init__(self, delta):

params = {delta} if 'symbol' in str(delta.__class__) else delta.free_symbols

super(T, self).__init__(self._matrix(delta), params)
super(T, self).__init__(self._matrix(delta), {delta})

# Transform transpose
self.T = self.T()
Expand Down

0 comments on commit e691e56

Please sign in to comment.