-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelementaries.py
194 lines (132 loc) · 5.68 KB
/
elementaries.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
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
import numpy as np
import pytest
from node import Node
def _check_log_domain_restrictions(x):
if x.value <= 0:
raise ValueError(f"Value {x.value} not valid for a logarithmic function")
def _check_sqrt_domain_restrictions(x):
if x.value < 0:
raise ValueError("Square roots of negative numbers not supported")
def sqrt(x):
symbolic_representation = "sqrt({})".format(str(x))
if Node._check_node_exists(symbolic_representation):
return Node._get_existing_node(symbolic_representation)
x = Node._convert_to_node(x)
_check_sqrt_domain_restrictions(x)
forward_trace = np.sqrt(x.value)
tangent_trace = x.derivative / 2 * np.sqrt(x.value)
new_node = Node(symbolic_representation, forward_trace, tangent_trace)
Node._insert_node_to_registry(new_node)
return new_node
def ln(x):
symbolic_representation = "ln({})".format(str(x))
if Node._check_node_exists(symbolic_representation):
return Node._get_existing_node(symbolic_representation)
x = Node._convert_to_node(x)
_check_log_domain_restrictions(x)
forward_trace = np.log(x.value)
tangent_trace = 1 / x.value
new_node = Node(symbolic_representation, forward_trace, tangent_trace)
Node._insert_node_to_registry(new_node)
return new_node
def log10(x):
symbolic_representation = "log10({})".format(str(x))
if Node._check_node_exists(symbolic_representation):
return Node._get_existing_node(symbolic_representation)
x = Node._convert_to_node(x)
_check_log_domain_restrictions(x)
forward_trace = np.log10(x.value)
tangent_trace = 1 / (x.value * np.log(10))
new_node = Node(symbolic_representation, forward_trace, tangent_trace)
Node._insert_node_to_registry(new_node)
return new_node
def log2(x):
symbolic_representation = "log2({})".format(str(x))
if Node._check_node_exists(symbolic_representation):
return Node._get_existing_node(symbolic_representation)
x = Node._convert_to_node(x)
_check_log_domain_restrictions(x)
forward_trace = np.log2(x.value)
tangent_trace = 1 / (x.value * np.log(2))
new_node = Node(symbolic_representation, forward_trace, tangent_trace)
Node._insert_node_to_registry(new_node)
return new_node
def exp(x):
symbolic_representation = "exp({})".format(str(x))
if Node._check_node_exists(symbolic_representation):
return Node._get_existing_node(symbolic_representation)
x = Node._convert_to_node(x)
forward_trace = np.exp(x.value)
tangent_trace = x.derivative * forward_trace
new_node = Node(symbolic_representation, forward_trace, tangent_trace)
Node._insert_node_to_registry(new_node)
return new_node
def sin(x):
symbolic_representation = "sin({})".format(str(x))
if Node._check_node_exists(symbolic_representation):
return Node._get_existing_node(symbolic_representation)
x = Node._convert_to_node(x)
forward_trace = np.sin(x.value)
tangent_trace = np.cos(x.value) * x.derivative
new_node = Node(symbolic_representation, forward_trace, tangent_trace)
Node._insert_node_to_registry(new_node)
return new_node
def cos(x):
symbolic_representation = "cos({})".format(str(x))
if Node._check_node_exists(symbolic_representation):
return Node._get_existing_node(symbolic_representation)
x = Node._convert_to_node(x)
forward_trace = np.cos(x.value)
tangent_trace = -np.sin(x.value) * x.derivative
new_node = Node(symbolic_representation, forward_trace, tangent_trace)
Node._insert_node_to_registry(new_node)
return new_node
def tan(x):
symbolic_representation = "tan({})".format(str(x))
if Node._check_node_exists(symbolic_representation):
return Node._get_existing_node(symbolic_representation)
x = Node._convert_to_node(x)
forward_trace = np.tan(x.value)
tangent_trace = x.derivative / (np.cos(x.val) ** 2)
new_node = Node(symbolic_representation, forward_trace, tangent_trace)
Node._insert_node_to_registry(new_node)
return new_node
def arcsin(x):
symbolic_representation = "arcsin({})".format(str(x))
if Node._check_node_exists(symbolic_representation):
return Node._get_existing_node(symbolic_representation)
x = Node._convert_to_node(x)
forward_trace = np.arcsin(x.value)
tangent_trace = x.derivative / np.sqrt(1 - x.value**2)
new_node = Node(symbolic_representation, forward_trace, tangent_trace)
Node._insert_node_to_registry(new_node)
return new_node
def arccos(x):
symbolic_representation = "arccos({})".format(str(x))
if Node._check_node_exists(symbolic_representation):
return Node._get_existing_node(symbolic_representation)
x = Node._convert_to_node(x)
forward_trace = np.arccos(x.value)
tangent_trace = -x.derivative / np.sqrt(1 - x.value**2)
new_node = Node(symbolic_representation, forward_trace, tangent_trace)
Node._insert_node_to_registry(new_node)
return new_node
def arctan(x):
symbolic_representation = "arctan({})".format(str(x))
if Node._check_node_exists(symbolic_representation):
return Node._get_existing_node(symbolic_representation)
x = Node._convert_to_node(x)
forward_trace = np.arccos(x.value)
tangent_trace = -x.derivative / (1 + x.value**2)
new_node = Node(symbolic_representation, forward_trace, tangent_trace)
Node._insert_node_to_registry(new_node)
return new_node
if __name__ == "__main__":
x = Node("x", 5, 1)
z = log2(x)
print(z.value)
"""Testing the `_check_log_domain_restrictions` function above"""
def test_check_log_domain_restrictions(self):
node = Node('x', -15, 11)
with pytest.raises(ValueError):
_check_log_domain_restrictions(node)