2

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?

P. Aumann
  • 41
  • 4

2 Answers2

1

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.

gbe
  • 671
  • 3
  • 11
  • Thanks for your answer! Well actually it's not important for the calculation that these objects are actually operators of a hilbert space... It could be anything that has three parameters (that's why I called it abstract). I just want to tell sympy: Here are some objects that have three parameters... do the calculation for me and consider a list of rules I give you for that calculation... – P. Aumann Oct 29 '16 at 09:18
0

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).

asmeurer
  • 86,894
  • 26
  • 169
  • 240