GithubHelp home page GithubHelp logo

Comments (9)

Rohanberiwal avatar Rohanberiwal commented on June 8, 2024
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.

Rohanberiwal avatar Rohanberiwal commented on June 8, 2024

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.

Kadelka avatar Kadelka commented on June 8, 2024

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.

Kadelka avatar Kadelka commented on June 8, 2024

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.

Kadelka avatar Kadelka commented on June 8, 2024

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.

Rohanberiwal avatar Rohanberiwal commented on June 8, 2024

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.

Kadelka avatar Kadelka commented on June 8, 2024

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.

Rohanberiwal avatar Rohanberiwal commented on June 8, 2024

Prefect ! I will work on this and get back to you . Thanks !!

from sympy.

Rohanberiwal avatar Rohanberiwal commented on June 8, 2024

Hi ! This is still open right ? I have made a few modification , I will be sharing it soon .

from sympy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.