-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiple_alpha_term.py
More file actions
154 lines (127 loc) · 5.47 KB
/
multiple_alpha_term.py
File metadata and controls
154 lines (127 loc) · 5.47 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
class MultipleAlphaTerm:
def __init__(self, _terms: list):
self.terms: list = _terms
self.__coefficient = self.get_coefficient()
self.seperated_terms = self.seperate_by_alphas()
def set_coefficient(self, value):
self.__coefficient = value
def get_coefficient(self):
result = 1
for term in self.terms:
result *= term.get_coefficient()
return result
@staticmethod
def __multiply_alpha_bros(terms) -> 'AlphaTerm':
result = terms[0]
for term in terms[1:]:
result *= term
return result
def seperate_by_alphas(self) -> list:
terms_dict = dict()
for _term in self.terms:
if not terms_dict.get(_term.get_alpha()):
terms_dict[_term.get_alpha()] = list()
terms_dict[_term.get_alpha()].append(_term)
for alpha in terms_dict.keys():
if len(terms_dict[alpha]) > 1:
terms_dict[alpha] = [self.__multiply_alpha_bros(terms_dict[alpha])]
final_terms = []
for t in terms_dict.values():
final_terms.append(t[0])
return final_terms
def get_full_term(self) -> str:
alpha_exp = ""
for term in self.seperated_terms:
alpha_exp += term.get_alpha() + term.get_printable_exponent()
return str(self.get_coefficient()) + alpha_exp
def get_derivative(self, _d: str, degree=1):
terms = self.terms.copy()
for index, term in enumerate(terms):
if term.get_alpha() == _d:
derrived_term = term.get_derivative(degree)
if derrived_term == 0:
return 0
else:
terms[index] = derrived_term
continue
if self.terms == terms:
raise ValueError("You can't derive from a variable that doesn't exist.")
else:
return MultipleAlphaTerm(terms)
def turn_to_known(self, **values):
new_term = self.__copy__()
new_alpha_term = self.terms[0].__copy__()
for term in self.seperated_terms:
value = values.get(term.get_alpha())
if value:
try:
new_term.__coefficient /= term.get_coefficient()
except ZeroDivisionError:
pass
new_term.__coefficient *= term.turn_to_known(value)
new_term.seperated_terms.remove(term)
if len(new_term.seperated_terms) == 1:
if new_term.seperated_terms[0].get_alpha() in values.keys():
continue
else:
new_alpha_term.set_coefficient(new_term.__coefficient)
new_alpha_term.set_alpha(new_term.seperated_terms[0].get_alpha())
new_alpha_term.set_exponent(new_term.seperated_terms[0].get_exponent())
return new_alpha_term
elif len(new_term.seperated_terms) == 0:
return new_term.get_coefficient()
else:
if set(values).intersection(set([t.get_alpha() for t in new_term.seperated_terms])):
continue
else:
return new_term
def __str__(self) -> str:
return self.get_full_term()
def __mul__(self, other) -> 'MultipleAlphaTerm':
if isinstance(other, int) or isinstance(other, float):
new_terms = self.terms.copy()
new_terms[-1] = new_terms[-1].__copy__() * other
return MultipleAlphaTerm(new_terms)
elif isinstance(other, type(self.terms[-1])): # self.terms[-1] always will be an AlphaTerm object.
new_terms = self.terms + [other]
return MultipleAlphaTerm(new_terms)
elif isinstance(other, type(self)):
new_terms = self.terms + other.terms
return MultipleAlphaTerm(new_terms)
def __rmul__(self, other):
return self.__mul__(other)
def __copy__(self):
return MultipleAlphaTerm(self.terms)
def __pow__(self, power):
new_terms = []
for term in self.seperated_terms:
new_terms.append(term ** power)
return MultipleAlphaTerm(new_terms)
def __truediv__(self, other):
if type(other) in [int, float]:
if other == 0:
raise ValueError("MultipleAlphaTerm object cannot be divided by zero")
return self.__mul__(1 / other)
elif isinstance(other, type(self.terms[-1])):
if other.is_equal_zero:
raise ValueError("MultipleAlphaTerm object cannot be divided by zero")
else:
new_object = other.__copy__()
new_object.set_coefficient(1 / other.get_coefficient())
new_object.set_exponent(-other.get_exponent())
return MultipleAlphaTerm(self.terms + [new_object])
elif isinstance(other, type(self)):
other_terms = []
for term in other.terms:
new_term = term.__copy__()
new_term.set_coefficient(1 / term.get_coefficient())
new_term.set_exponent(-term.get_exponent)
other_terms.append(new_term)
return MultipleAlphaTerm(self.terms + [other_terms])
def __float__(self):
return float(self.__coefficient)
def __abs__(self):
new_term = self.__copy__()
for i in range(0, len(new_term.terms)):
new_term.terms[i] = abs(new_term.terms[i])
return new_term