-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalpha_term.py
More file actions
122 lines (99 loc) · 4.33 KB
/
alpha_term.py
File metadata and controls
122 lines (99 loc) · 4.33 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
from multiple_alpha_term import MultipleAlphaTerm
from printer import TermPrinter
class AlphaTerm:
def __init__(self, _coe=1.0, _alpha="x", _exp=1):
self.__coefficient = float(_coe)
self.__alpha = _alpha
self.__exponent = _exp
self.is_equal_zero = True if self.__coefficient == 0 else False
def get_coefficient(self) -> float:
return self.__coefficient
def get_alpha(self) -> str:
return self.__alpha
def get_exponent(self) -> int:
return self.__exponent
def get_printable_exponent(self):
# This function uses TermPrinter but still returns irregular result
return TermPrinter.get_printable_exponent(self.__exponent, False)
def get_printable_coefficient(self) -> str:
# Returns regular coefficient
return TermPrinter.get_printable_coefficient(self.__coefficient)
def get_full_term(self) -> str:
# Irregular term
return str(self.__coefficient) + self.__alpha + self.get_printable_exponent()
def get_derivative(self, degree=1):
if degree == 0:
return self.__copy__()
if self.__exponent == 0:
return 0
else:
derrived_term = self.__copy__()
derrived_term.set_coefficient(self.__coefficient * self.__exponent)
derrived_term.set_exponent(self.__exponent - 1)
if degree == 1:
return derrived_term
else:
return derrived_term.get_derivative(degree - 1)
def set_coefficient(self, _coe: float) -> None:
self.__coefficient = _coe
def set_alpha(self, alpha: str) -> None:
self.__alpha = alpha
def set_exponent(self, exponent: int):
self.__exponent = exponent
def turn_to_known(self, value: float) -> float:
return self.__coefficient * (value ** self.__exponent)
def __str__(self):
return self.get_full_term()
def __mul__(self, other):
if type(other) in [float, int]:
new_coefficient = self.__coefficient * other
return AlphaTerm(new_coefficient, self.__alpha, self.__exponent)
elif isinstance(other, type(self)):
other: AlphaTerm
if other.get_alpha() == self.__alpha:
new_coefficient = other.get_coefficient() * self.__coefficient
new_exponent = other.get_exponent() + self.__exponent
return AlphaTerm(new_coefficient, self.__alpha, new_exponent)
else:
return MultipleAlphaTerm([self, other])
elif isinstance(other, MultipleAlphaTerm):
new_terms = other.terms.copy()
new_terms.extend([self])
return MultipleAlphaTerm(new_terms)
def __rmul__(self, other):
return self.__mul__(other)
def __copy__(self):
return AlphaTerm(self.__coefficient, self.__alpha, self.__exponent)
def __pow__(self, power):
if isinstance(power, int):
new_coefficient = self.__coefficient ** power
new_exponent = self.__exponent * power
return AlphaTerm(new_coefficient, self.__alpha, new_exponent)
else:
raise ValueError("Power had to be an integer")
def __truediv__(self, other):
if type(other) in [float, int]:
if other == 0:
raise ValueError("AlphaTerm object cannot be divided by zero")
return self.__mul__(1 / other)
elif isinstance(other, type(self)):
if other.is_equal_zero:
raise ValueError("AlphaTerm object cannot be divided by zero")
new_term = other.__copy__()
new_term.set_exponent(-other.get_exponent())
new_term.set_coefficient(1 / other.get_coefficient())
return self * new_term
elif isinstance(other, MultipleAlphaTerm):
other_terms = []
for term in other.seperated_terms:
new_term = term.__copy__()
new_term.set_exponent(-term.get_exponent())
new_term.set_coefficient(1 / term.get_coefficient())
other_terms.append(new_term)
return MultipleAlphaTerm([self] + other_terms)
def __float__(self):
return float(self.__coefficient)
def __abs__(self):
new_term = self.__copy__()
new_term.set_coefficient(abs(self.__coefficient))
return new_term