Comments (9)
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)
from sympy.
Hi , Greetings !
I've enhanced the code to handle cases where one of the operands isn't a quaternion. Now, if one operand is a regular number, the output is None / Not implemented , indicating the operation couldn't be performed . Let me know if this works and this was what we were suppose to fix ! I have attached the code above . Thank you
from sympy.
Hallo Rohanberiwall,
your code works as it is. Thank you. But the problem is that it doesn't change Quaternion, but introduces a new class MyQuaternion with the consequence, that in all of my code I have to change Quaternion with MyQuaternion. And I do not know if this really works. With my simple workaround I only have to define the Blacklist in octonion.py resp. sedenion.py, nothing more.
from sympy.
Hallo Rohanberiwall,
as an example here is my patch dieter.py
from sympy import Quaternion, core
def dummy_add(self,other):
if type(other) in Quaternion.Blacklist:
return NotImplemented
else:
return self.add(other)
Quaternion.__add__ = dummy_add
def dummy_sub(self,other):
if type(other) in Quaternion.Blacklist:
return NotImplemented
else:
return self.add(other*-1)
Quaternion.__sub__ = dummy_sub
def dummy_mul(self,other):
if type(other) in Quaternion.Blacklist:
return NotImplemented
else:
return self._generic_mul(self, core.sympify(other,strict=True))
Quaternion.__mul__ = dummy_mul
def dummy_truediv(self,other):
if type(other) in Quaternion.Blacklist:
return NotImplemented
else:
return self * core.sympify(other)**-1
Quaternion.__truediv__ = dummy_truediv__
and after the definition of class(Octonion)
I add
Quaternion.Blacklist = [Octonion]
from dieter import *
This works as intended.
Greetings,
Dieter
from sympy.
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.
from sympy.
Hi
I've carefully reviewed your feedback. I understand your preference for minimal changes to the existing Quaternion class. I'll work on implementing the suggested approach using the patch file (dieter.py) to modify the behavior directly. I'll ensure that the solution aligns with our project requirements and provides a seamless integration with the existing codebase. Please give me some suggesstion on if we can make more classes or we have to stick to the classes that are already written in the code .
Thanks
from sympy.
Hallo,
I've taken a look into the documentation of sympy's Quaternion. There is no other existing operator which needs adaption. radd etc. are treated by the calling program. All the better if you can integrate return NotImplemented
without a Blacklist.
Thanks
from sympy.
Prefect ! I will work on this and get back to you . Thanks !!
from sympy.
Hi ! This is still open right ? I have made a few modification , I will be sharing it soon .
from sympy.
Related Issues (20)
- RecursionError in sympy.solvers.inequalities.reduce_inequalities with custom function HOT 1
- The `point_cflexure` method doesn't return points when a region of the bending moment line is 0.
- variable of differentiation returns True from `has_free` HOT 3
- Symbolic specifying of locations that are "just" before or "just" after another location. HOT 8
- parse_mathematica cannot handle greek letters HOT 1
- FiniteField.__call__ returns different types depending on whether flint is installed or not HOT 6
- Incorrect partial fraction expansion with floats HOT 3
- Sphinx build failing regularly HOT 1
- calling `latex` on `Array` with less rows than columns, and at least 10 columns, emit bad latex
- Does solve(...) respects symbol(..., real=True) assumption?
- Sympy results are int, not Integer HOT 1
- Regression: lambdify of Derivative instance now throws PrintMethodNotImplementedError HOT 8
- sp.solve: expected an integer, got rational HOT 2
- Cannot determine truth value of Relational HOT 1
- plot error after a "clean" simpify check up HOT 4
- collect function bug HOT 5
- BUG: inconsistent results while parsing unpaired bracket
- collect function bug HOT 1
- Incorrect behaviour for solve_poly_system HOT 2
- [1.13.0rc1] possible regression in comparison (`numbers.One()` VS `float(1.0)`) ? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sympy.