Created
October 12, 2012 17:52
-
-
Save petrblahos/3880519 to your computer and use it in GitHub Desktop.
Symbolická matematika v Pythonu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Expression(object): | |
def __add__(self, rt): | |
return Plus(self, rt) | |
def __sub__(self, rt): | |
return Minus(self, rt) | |
def __mul__(self, rt): | |
return Multiply(self, rt) | |
def __div__(self, rt): | |
return Divide(self, rt) | |
class BinaryOperation(Expression): | |
def __init__(self, lt, rt): | |
self.lt = lt | |
self.rt = rt | |
def __str__(self): | |
return "(%s %s %s)" % (self.lt, self.__class__.OPERATOR, self.rt) | |
class Plus(BinaryOperation): | |
OPERATOR = "+" | |
class Minus(BinaryOperation): | |
OPERATOR = "-" | |
class Multiply(BinaryOperation): | |
OPERATOR = "*" | |
class Divide(BinaryOperation): | |
OPERATOR = "/" | |
class Primary(Expression): | |
pass | |
class Symbol(Primary): | |
def __init__(self, name): | |
self.name = name | |
def __str__(self): | |
return self.name | |
print Symbol("a")+Symbol("b")*Symbol("e")-Symbol("c")/Symbol("d") | |
print (Symbol("a")+Symbol("b"))*Symbol("e")-Symbol("c")/Symbol("d") | |
print (Symbol("a")+Symbol("b")*Symbol("e")-Symbol("c"))/Symbol("d") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment