-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
76 lines (56 loc) · 2.01 KB
/
functions.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
import math
from abc import ABC, abstractmethod
from utils import *
class Function (ABC):
def __init__(self, name) -> None:
self.name = name
@abstractmethod
def function(self, x: float) -> float:
pass
@abstractmethod
def integral(self, a: float, b: float) -> float:
pass
class CubicFunction (Function):
def __init__(self) -> None:
super().__init__(CUBIC_FUNCTION)
# override
def function(self, x: float) -> float:
return x**3
# override
def integral(self, a: float, b: float) -> float:
primitive = lambda x: x**4/4
return primitive(b) - primitive(a)
class ArctanDerivativeFunction (Function):
def __init__(self) -> None:
super().__init__(ARCTAN_DER_FUNCTION)
# override
def function(self, x: float) -> float:
return 1/(x**2 + 1)
# override
def integral(self, a: float, b: float) -> float:
primitive = lambda x: math.atan(x)
return primitive(b) - primitive(a)
class LogDerivativeFunction (Function):
def __init__(self) -> None:
super().__init__(LOG_DER_FUNCTION)
# override
def function(self, x: float) -> float:
return 1/x
# override
def integral(self, a: float, b: float) -> float:
primitive = lambda x: math.log(x)
return primitive(b) - primitive(a)
class CustomFunction (Function):
def __init__(self) -> None:
super().__init__(CUSTOM_FUNCTION_1)
# override
def function(self, x: float) -> float:
return abs(x)*pow(x,2)*pow(math.e,3*pow(x,2))
# override
def integral(self, a: float, b: float) -> float:
primitive = lambda x : (math.pow(x,2) * math.pow(math.e, 3 * math.pow(x, 2)) - math.pow(math.e, 3 * math.pow(x,2)) / 3) / 6
if (a < 0.0) & (b < 0.0):
return primitive (a) - primitive(b)
elif a < 0.0:
return (primitive(0) - primitive(a)) + (primitive(b) - primitive(0))
return primitive(b) - primitive(a)