Repository navigation
Expand file tree
/
Copy pathcalc.py
More file actions
219 lines (158 loc) · 5.56 KB
/
Copy pathcalc.py
File metadata and controls
219 lines (158 loc) · 5.56 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
from abc import ABC, abstractmethod
from math import exp
class Func(ABC):
def __add__(self, other: 'Func') -> 'Func':
return Add(self, other)
def __sub__(self, other: 'Func') -> 'Func':
return Sub(self, other)
def __mul__(self, other: 'Func') -> 'Func':
return Mul(self, other)
def __truediv__(self, other: 'Func') -> 'Func':
return Div(self, other)
def __pow__(self, other: 'Func') -> 'Func':
return Pow(self, other)
def __neg__(self) -> 'Func':
return Mul(Con(-1), self)
@abstractmethod
def eval(self, x: float) -> float:
pass
@abstractmethod
def der(self) -> 'Func': # derivative
pass
@abstractmethod
def prim(self) -> 'Func': # antiderivative, primitive function
pass
def integral(self, a: float, b: float) -> float:
p = self.prim()
return p.eval(b) - p.eval(a)
class Unary(Func):
def __init__(self, f: Func) -> None:
self.f = f
class Binary(Func):
def __init__(self, f1: Func, f2: Func) -> None:
self.f1 = f1
self.f2 = f2
class Add(Binary):
def __repr__(self) -> str:
return f'({self.f1} + {self.f2})'
def eval(self, x: float) -> float:
return self.f1.eval(x) + self.f2.eval(x)
def der(self) -> Func:
return Add(self.f1.der(), self.f2.der())
def prim(self) -> Func:
return Add(self.f1.prim(), self.f2.prim())
class Sub(Binary):
def __repr__(self) -> str:
return f'({self.f1} - {self.f2})'
def eval(self, x: float) -> float:
return self.f1.eval(x) - self.f2.eval(x)
def der(self) -> Func:
return Sub(self.f1.der(), self.f2.der())
def prim(self) -> Func:
return Sub(self.f1.prim(), self.f2.prim())
class Mul(Binary):
def __repr__(self) -> str:
return f'({self.f1} * {self.f2})'
def eval(self, x: float) -> float:
return self.f1.eval(x) * self.f2.eval(x)
def der(self) -> Func:
return Add(Mul(self.f1.der(), self.f2), Mul(self.f1, self.f2.der()))
def prim(self) -> Func:
raise NotImplementedError
class Div(Binary):
def __repr__(self) -> str:
return f'({self.f1} / {self.f2})'
def eval(self, x: float) -> float:
return self.f1.eval(x) / self.f2.eval(x)
def der(self) -> Func:
return Div(Sub(Mul(self.f1.der(), self.f2), Mul(self.f1, self.f2.der())), Mul(self.f2, self.f2))
def prim(self) -> Func:
raise NotImplementedError
class Pow(Binary):
def __repr__(self) -> str:
return f'({self.f1} ** {self.f2})'
def eval(self, x: float) -> float:
return self.f1.eval(x) ** self.f2.eval(x)
def der(self) -> Func:
raise NotImplementedError
def prim(self) -> Func:
raise NotImplementedError
class Exp(Unary):
def __repr__(self) -> str:
return f'Exp({self.f})'
def eval(self, x: float) -> float:
return exp(self.f.eval(x))
def der(self) -> Func:
return Mul(Exp(self.f), self.f.der())
def prim(self) -> Func:
raise NotImplementedError
class Pol(Func):
def __init__(self, pol: dict[float, float]) -> None:
self.pol = pol # key = exponent, value = coefficient
def coef(self, exp: int) -> float: # if not sure if exp exists in dict
if exp in self.pol: return self.pol[exp]
return 0
def is_null(self) -> bool:
return all(c == 0 for c in self.pol.values())
def der(self) -> 'Pol':
result = {}
for e, c in self.pol.items():
if e == 0: continue
result[e - 1] = c * e
return Pol(result)
def prim(self) -> 'Pol':
result = {}
for e, c in self.pol.items():
result[e + 1] = c / (e + 1)
return Pol(result)
def eval(self, x: float) -> float:
return sum(self.pol[e] * x**e for e in self.pol)
def __str__(self) -> str:
result = ''
s = dict(sorted(self.pol.items(), key=lambda item: item[0], reverse=True))
for e, c in s.items():
if c == 0: continue
if len(result) > 0 or c < 0: result += ' + ' if c > 0 else ' - '
if abs(c) != 1 or e == 0: result += str(abs(c))
if e != 0: result += 'x' if e == 1 else f'x^{e}'
return result
def __mul__(self, other):
if not isinstance(other, Pol):
return super().__mul__(other)
result = {}
for e1, c1 in self.pol.items():
for e2, c2 in other.pol.items():
e = e1 + e2
c = c1 * c2
if e not in result: result[e] = 0
result[e] += c
return Pol(result)
def __add__(self, other):
if not isinstance(other, Pol):
return super().__add__(other)
result = self.pol.copy()
for e, c in other.pol.items():
if e not in result: result[e] = 0
result[e] += c
return Pol(result)
def __sub__(self, other):
if not isinstance(other, Pol):
return super().__sub__(other)
result = self.pol.copy()
for e, c in other.pol.items():
if e not in result: result[e] = 0
result[e] -= c
return Pol(result)
def __pow__(self, other):
if not isinstance(other, int):
return super().__pow__(other)
result = Pol({0: 1})
for _ in range(other):
result *= self
return result
class Con(Pol):
def __init__(self, c: float) -> None:
super().__init__({0: c})
class Var(Pol):
def __init__(self) -> None:
super().__init__({1: 1})