-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding NotImplemented to standard operators in Quaternion #26355
Comments
from sympy import Quaternion
class NotImplQuaternion:
def __init__(self, operation):
self.operation = operation
def __str__(self):
return f"One of the quaternions is not implemented for {self.operation}"
class MyQuaternion:
Blacklist = []
def __init__(self, a=0, b=0, c=0, d=0):
try:
self.quaternion = Quaternion(a, b, c, d)
except Exception as e:
print("An error occurred while initializing MyQuaternion:", e)
def __add__(self, other):
try:
if type(other) in MyQuaternion.Blacklist:
return NotImplemented
elif isinstance(other, (int, float)):
return NotImplQuaternion("addition")
else:
result = self.quaternion + other.quaternion
return MyQuaternion(*result.args)
except Exception as e:
print("An error occurred during addition:", e)
def __sub__(self, other):
try:
if type(other) in MyQuaternion.Blacklist:
return NotImplemented
elif isinstance(other, (int, float)):
return NotImplQuaternion("subtraction")
else:
result = self.quaternion - other.quaternion
return MyQuaternion(*result.args)
except Exception as e:
print("An error occurred during subtraction:", e)
def __mul__(self, other):
try:
if type(other) in MyQuaternion.Blacklist:
return NotImplemented
elif isinstance(other, (int, float)):
return NotImplQuaternion("multiplication")
else:
result = self.quaternion * other.quaternion
return MyQuaternion(*result.args)
except Exception as e:
print("An error occurred during multiplication:", e)
def __truediv__(self, other):
try:
if type(other) in MyQuaternion.Blacklist:
return NotImplemented
elif isinstance(other, (int, float)):
return NotImplQuaternion("division")
else:
result = self.quaternion / other.quaternion
return MyQuaternion(*result.args)
except Exception as e:
print("An error occurred during division:", e)
def __str__(self):
real, imag_i, imag_j, imag_k = self.quaternion.args
return f"Result of operation: ({real} + {imag_i}*i + {imag_j}*j + {imag_k}*k)"
q1 = MyQuaternion(1, 2, 3, 4)
q2 = MyQuaternion(5, 6, 7, 8)
result_add = q1 + q2
print("Result of addition:", result_add)
result_sub = q1 - q2
print("Result of subtraction:", result_sub)
result_mul = q1 * q2
print("Result of multiplication:", result_mul)
result_div = q1 / q2
print("Result of division:", result_div)
print("Below are the test cases the returns Not implemented")
regular_number = 5
result_add = q1 + regular_number
print("Result of addition:", result_add)
result_sub = q1 - regular_number
print("Result of subtraction:", result_sub)
result_mul = q1 * regular_number
print("Result of multiplication:", result_mul)
result_div = q1 / regular_number
print("Result of division:", result_div) |
Hi , Greetings ! |
Hallo Rohanberiwall, |
Hallo Rohanberiwall,
and after the definition of
This works as intended. Greetings, |
I think the content of dieter.py can be realized in quaternion.py without changing the logic of Quaternion in any way. Nothing more has to be changed! In the same way other operators can be changed similarly if needed. |
Hi |
Hallo, |
Prefect ! I will work on this and get back to you . Thanks !! |
Hi ! This is still open right ? I have made a few modification , I will be sharing it soon . |
I think it's possible to change
Quaternion.__add__(self,other)
in classsympy.Quaternion
(and other operators likeadd
) without changing the actual code ofsympy.Quaternion
in any essential way by introducing a Blacklist withBlacklist = []
in class definion of Quaternion and then to replace f.i.
by
Similarly for the other operators. This would allow constructs like
a+b
with Quaterniona
andb
of a type unknown to Quaternion. With a construct like above actuallya+b
would be resolved asself.__radd__(a)
withself = b
.__radd__ then can handle internally
a+b`. Without replacement the interpreter does not know what to do. See also my issueThe text was updated successfully, but these errors were encountered: