-
Notifications
You must be signed in to change notification settings - Fork 0
/
power.py
88 lines (61 loc) · 3.28 KB
/
power.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
exec(compile(source=open('integer.py').read(), filename='integer.py', mode='exec'))
exec(compile(source=open('element.py').read(), filename='element.py', mode='exec'))
##need to deal with operations with one variable and an actual int (ex. x+2 or e^x*e^2)
##subtypes/subclasses? a power object is also a mult object which is also an element.
class power:
def __init__(self, element, exp):
if isinstance(exp, str):
exp = integer(exp)
self.exponent = exp
self.element= element
def __repr__(self):
return f"{self.element} ^ {self.exponent}"
##cases: 1.e^a*e^b 2.e1^a*e2^b
def __mul__(self,other):
if self.element == other.element:
if isinstance(self.exponent,integer) and isinstance(other.exponent,integer):
expnew = self.exponent + "+" + other.exponent
return power(self, expnew)
elif isinstance(self.exponent,int) and isinstance(other.exponent,integer):
expnew = str(self.exponent) + "+" + other.exponent
return power(self, expnew)
elif isinstance(self.exponent,integer) and isinstance(other.exponent,int):
expnew = self.exponent + "+" + str(other.exponent)
return power(self, expnew)
elif isinstance(self.exponent,int) and isinstance(other.exponent,int):
expnew = self.exponent + other.exponent
return power(self, expnew)
##if they are not both powers, we can just use mult from elements
else:
mult(self,other, self.element.group)
def __truediv__(self,other):
if self.element == other.element:
expnew = self.exponent + "-" + other.exponent
return power(self, expnew)
elif isinstance(self.exponent,int) and isinstance(other.exponent,integer):
expnew = str(self.exponent) + "-" + other.exponent
return power(self, expnew)
elif isinstance(self.exponent,integer) and isinstance(other.exponent,int):
expnew = self.exponent + "-" + str(other.exponent)
return power(self, expnew)
elif isinstance(self.exponent,int) and isinstance(other.exponent,int):
expnew = self.exponent - other.exponent
return power(self, expnew)
def __eq__(self, other):
if isinstance(self,power) and isinstance(other,power):
if self.element==other.element and self.exponent == other.exponent:
return True
else:
return False
#e^2=e*e property
# we can't have a mult object of length x, so this should only work for ints
#is encoding of mult still a list?
elif isinstance(self,power) and isinstance(other,power)==False:
if isinstance(self.exponent,int):
if self.element == other.element and self.exponent == len(other.list):
return True
elif isinstance(other,power) and isinstance(self,power)==False:
if isinstance(other.exponent,int):
if self.element == other.element and other.exponent == len(self.list):
return True
## Deal with x^{a+2}=x^a*x^2?