-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathADmath.py
More file actions
179 lines (162 loc) · 4.5 KB
/
ADmath.py
File metadata and controls
179 lines (162 loc) · 4.5 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
"""Module for performing special functions on ADnum objects.
Take an ADnum object as input and return an ADnum object as output.
For real number inputs, returns a real number.
"""
import numpy as np
from AD20.ADnum import ADnum
#TRIGONOMETRIC FUNCTIONS
def sin(X):
try:
y = ADnum(np.sin(X.val), der = np.cos(X.val)*X.der)
y.graph = X.graph
if X not in y.graph:
y.graph[X] = []
y.graph[X].append((y, 'sin'))
return y
except AttributeError:
return np.sin(X)
def cos(X):
try:
y = ADnum(np.cos(X.val), der = -np.sin(X.val)*X.der)
y.graph = X.graph
if X not in y.graph:
y.graph[X] = []
y.graph[X].append((y, 'cos'))
return y
except AttributeError:
return np.cos(X)
def tan(X):
try:
y = ADnum(np.tan(X.val), der = (1/np.cos(X.val)**2)*X.der)
y.graph = X.graph
if X not in y.graph:
y.graph[X] = []
y.graph[X].append((y, 'tan'))
return y
except AttributeError:
return np.tan(X)
def csc(X):
try:
y = ADnum(1/np.sin(X.val), der = (-1/np.tan(X.val))*(1/np.sin(X.val))*X.der)
y.graph = X.graph
if X not in y.graph:
y.graph[X] = []
y.graph[X].append((y, 'csc'))
return y
except:
return 1/np.sin(X)
def sec(X):
try:
y = ADnum(1/np.cos(X.val), der = np.tan(X.val)/np.cos(X.val)*X.der)
y.graph = X.graph
if X not in y.graph:
y.graph[X] = []
y.graph[X].append((y, 'sec'))
return y
except AttributeError:
return 1/np.cos(X)
def cot(X):
try:
y = ADnum(1/np.tan(X.val), der = -1/(np.sin(X.val)**2)*X.der)
y.graph = X.graph
if X not in y.graph:
y.graph[X] = []
y.graph[X].append((y, 'cot'))
return y
except AttributeError:
return 1/np.tan(X)
#INVERSE TRIGONOMETRIC FUNCTIONS
def arcsin(X):
try:
y = ADnum(np.arcsin(X.val), der = 1/np.sqrt(1-X.val**2)*X.der)
y.graph = X.graph
if X not in y.graph:
y.graph[X] = []
y.graph[X].append((y, 'arcsin'))
return y
except AttributeError:
return np.arcsin(X)
def arccos(X):
try:
y = ADnum(np.arccos(X.val), der = -1/np.sqrt(1-X.val**2)*X.der)
y.graph = X.graph
if X not in y.graph:
y.graph[X] = []
y.graph[X].append((y, 'arccos'))
return y
except AttributeError:
return np.arccos(X)
def arctan(X):
try:
y = ADnum(np.arctan(X.val), der = 1/(1+X.val**2)*X.der)
y.graph = X.graph
if X not in y.graph:
y.graph[X] = []
y.graph[X].append((y, 'arctan'))
return y
except AttributeError:
return np.arctan(X)
#HYPERBOLIC TRIG FUNCTIONS
def sinh(X):
try:
y = ADnum(np.sinh(X.val), der = np.cosh(X.val)*X.der)
y.graph = X.graph
if X not in y.graph:
y.graph[X] = []
y.graph[X].append((y, 'sinh'))
return y
except AttributeError:
return np.sinh(X)
def cosh(X):
try:
y = ADnum(np.cosh(X.val), der = np.sinh(X.val)*X.der)
y.graph = X.graph
if X not in y.graph:
y.graph[X] = []
y.graph[X].append((y, 'cosh'))
return y
except AttributeError:
return np.cosh(X)
def tanh(X):
try:
y = ADnum(np.tanh(X.val), der = 1/(np.cosh(X.val)**2)*X.der)
y.graph = X.graph
if X not in y.graph:
y.graph[X] = []
y.graph[X].append((y, 'tanh'))
return y
except AttributeError:
return np.tanh(X)
#NATURAL EXPONENTIAL AND NATURAL LOGARITHM
def exp(X):
try:
y = ADnum(np.exp(X.val), der = np.exp(X.val)*X.der)
y.graph = X.graph
if X not in y.graph:
y.graph[X] = []
y.graph[X].append((y, 'exp'))
return y
except AttributeError:
return np.exp(X)
def log(X):
try:
y = ADnum(np.log(X.val), der = 1/X.val*X.der)
y.graph = X.graph
if X not in y.graph:
y.graph[X] = []
y.graph[X].append((y, 'log'))
return y
except AttributeError:
return np.log(X)
def logistic(X):
return 1/(1+exp(-1*X))
def sqrt(X):
try:
y = ADnum(np.sqrt(X.val), der = X.der/(2*np.sqrt(X.val)))
y.graph = X.graph
if X not in y.graph:
y.graph[X] = []
y.graph[X].append((y, 'sqrt'))
return y
except AttributeError:
return np.sqrt(X)