-
Notifications
You must be signed in to change notification settings - Fork 0
/
arithmeticInterface.py
60 lines (48 loc) · 1.74 KB
/
arithmeticInterface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from abc import ABCMeta, abstractmethod
import polygnomeObject
class arithmeticInterface(polygnomeObject.polygnomeObject):
__metaclass__ = ABCMeta
"""
File: arithmeticInterface.py
Author: Chris Campbell
Email: c (dot) j (dot) campbell (at) ed (dot) ac (dot) uk
Github: https://github.com/campbellC
Description: A simple arithmetic interface that polynomials, tensors,
vectors, and coefficients will have to implement.
"""
##############################################################################
###### SORTING METHODS
##############################################################################
def clean(self): #This is the method that checks if for example we have x + x and simplifies it to 2 x.
return self
##############################################################################
###### MATHEMATICAL METHODS
##############################################################################
@abstractmethod
def isZero(self): pass
def __eq__(self,other):
x = self - other
x = x.clean()
if x.isZero():
return True
else:
return False
def __ne__(self,other):
return not self.__eq__(other)
@abstractmethod
def __add__(self,other): pass
@abstractmethod
def __mul__(self,other): pass
def __sub__(self,other):
return self + (other * (-1))
def __radd__(self,other): #addition is always commutative
return self + other
def __neg__(self):
return self * (-1)
def __pow__(self,other):
assert type(other) is int
assert other >= 0
if other == 0:
return 1
else:
return self * (self ** (other - 1 ))