P. Aumann
P. Aumann

Reputation: 41

Working with abstract mathematical Operators/Objects in Sympy

I am wondering if there is an easy way to implement abstract mathematical Operators in Sympy. With operators I simply mean some objects that have 3 values as an input or 3 indices, something like "Operator(a,b,c)". Please note that I am refering to a mathematical operator (over a hilbert space) and not an operator in the context of programming. Depending on these values I want to teach Sympy how to multiply two Operators of this kind and how to multiply it with a float and so on. At some point of the calculation I want to replace these Operators with some others...

So far I couldn't figure out if sympy provides such an abstract calculation. Therefore I started to write a new Python-class for these objects, but this went beyond the scope of my limited knowledge in Python very fast... Is there a more easy way to implement that then creating a new class?

Upvotes: 2

Views: 279

Answers (2)

asmeurer
asmeurer

Reputation: 91620

Yes, you can do this. Just create a subclass of sympy.Function. You can specify the number of arguments with nargs

class Operator(Function):
    nargs = 3

If you want the function to evaluate for certain arguments, define the class function eval. It should return None for when it should remain unevaluated. For instance, to evaluate to 0 when all three arguments are 0, you might use

class Operator(Function):
    @classmethod
    def eval(cls, a, b, c):
        if a == b == c == 0:
            return Integer(0)

(note that nargs is not required if you define eval).

Upvotes: 0

gbe
gbe

Reputation: 681

You may also want to look at SageMath, in addition to SymPy, since Sage goes to great lengths to come with prebuilt mathematical structures. However, it's been development more with eyes towards algebraic geometry, various areas of algebra, and combinatorics. I'm not sure to what extent it implements any operator algebra.

Upvotes: 1

Related Questions