-
Notifications
You must be signed in to change notification settings - Fork 525
/
hyperdual_hyperbolic.go
202 lines (194 loc) · 4.12 KB
/
hyperdual_hyperbolic.go
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
// Copyright ©2018 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package hyperdual
import "math"
// Sinh returns the hyperbolic sine of d.
//
// Special cases are:
// Sinh(±0) = (±0+Nϵ₁+Nϵ₂±0ϵ₁ϵ₂)
// Sinh(±Inf) = ±Inf
// Sinh(NaN) = NaN
func Sinh(d Number) Number {
if d.Real == 0 {
return Number{
Real: d.Real,
E1mag: d.E1mag,
E2mag: d.E1mag,
E1E2mag: d.Real,
}
}
if math.IsInf(d.Real, 0) {
return Number{
Real: d.Real,
E1mag: math.Inf(1),
E2mag: math.Inf(1),
E1E2mag: d.Real,
}
}
fn := math.Sinh(d.Real)
deriv := math.Cosh(d.Real)
return Number{
Real: fn,
E1mag: deriv * d.E1mag,
E2mag: deriv * d.E2mag,
E1E2mag: deriv*d.E1E2mag + fn*d.E1mag*d.E2mag,
}
}
// Cosh returns the hyperbolic cosine of d.
//
// Special cases are:
// Cosh(±0) = 1
// Cosh(±Inf) = +Inf
// Cosh(NaN) = NaN
func Cosh(d Number) Number {
if math.IsInf(d.Real, 0) {
return Number{
Real: math.Inf(1),
E1mag: d.Real,
E2mag: d.Real,
E1E2mag: math.Inf(1),
}
}
fn := math.Cosh(d.Real)
deriv := math.Sinh(d.Real)
return Number{
Real: fn,
E1mag: deriv * d.E1mag,
E2mag: deriv * d.E2mag,
E1E2mag: deriv*d.E1E2mag + fn*d.E1mag*d.E2mag,
}
}
// Tanh returns the hyperbolic tangent of d.
//
// Special cases are:
// Tanh(±0) = (±0+Nϵ₁+Nϵ₂∓0ϵ₁ϵ₂)
// Tanh(±Inf) = (±1+0ϵ₁+0ϵ₂∓0ϵ₁ϵ₂)
// Tanh(NaN) = NaN
func Tanh(d Number) Number {
switch d.Real {
case 0:
return Number{
Real: d.Real,
E1mag: d.E1mag,
E2mag: d.E2mag,
E1E2mag: -d.Real,
}
case math.Inf(1):
return Number{
Real: 1,
E1mag: 0,
E2mag: 0,
E1E2mag: negZero,
}
case math.Inf(-1):
return Number{
Real: -1,
E1mag: 0,
E2mag: 0,
E1E2mag: 0,
}
}
fn := math.Tanh(d.Real)
deriv := 1 - fn*fn
return Number{
Real: fn,
E1mag: deriv * d.E1mag,
E2mag: deriv * d.E2mag,
E1E2mag: deriv*d.E1E2mag - d.E1mag*d.E2mag*(2*fn*deriv),
}
}
// Asinh returns the inverse hyperbolic sine of d.
//
// Special cases are:
// Asinh(±0) = (±0+Nϵ₁+Nϵ₂∓0ϵ₁ϵ₂)
// Asinh(±Inf) = ±Inf
// Asinh(NaN) = NaN
func Asinh(d Number) Number {
if d.Real == 0 {
return Number{
Real: d.Real,
E1mag: d.E1mag,
E2mag: d.E2mag,
E1E2mag: -d.Real,
}
}
fn := math.Asinh(d.Real)
deriv1 := d.Real*d.Real + 1
deriv := 1 / math.Sqrt(deriv1)
return Number{
Real: fn,
E1mag: deriv * d.E1mag,
E2mag: deriv * d.E2mag,
E1E2mag: deriv*d.E1E2mag + d.E1mag*d.E2mag*(-d.Real*(deriv/deriv1)),
}
}
// Acosh returns the inverse hyperbolic cosine of d.
//
// Special cases are:
// Acosh(+Inf) = +Inf
// Acosh(1) = (0+Infϵ₁+Infϵ₂-Infϵ₁ϵ₂)
// Acosh(x) = NaN if x < 1
// Acosh(NaN) = NaN
func Acosh(d Number) Number {
if d.Real <= 1 {
if d.Real == 1 {
return Number{
Real: 0,
E1mag: math.Inf(1),
E2mag: math.Inf(1),
E1E2mag: math.Inf(-1),
}
}
return Number{
Real: math.NaN(),
E1mag: math.NaN(),
E2mag: math.NaN(),
E1E2mag: math.NaN(),
}
}
fn := math.Acosh(d.Real)
deriv1 := d.Real*d.Real - 1
deriv := 1 / math.Sqrt(deriv1)
return Number{
Real: fn,
E1mag: deriv * d.E1mag,
E2mag: deriv * d.E2mag,
E1E2mag: deriv*d.E1E2mag + d.E1mag*d.E2mag*(-d.Real*(deriv/deriv1)),
}
}
// Atanh returns the inverse hyperbolic tangent of d.
//
// Special cases are:
// Atanh(1) = +Inf
// Atanh(±0) = (±0+Nϵ₁+Nϵ₂±0ϵ₁ϵ₂)
// Atanh(-1) = -Inf
// Atanh(x) = NaN if x < -1 or x > 1
// Atanh(NaN) = NaN
func Atanh(d Number) Number {
if d.Real == 0 {
return Number{
Real: d.Real,
E1mag: d.E1mag,
E2mag: d.E2mag,
E1E2mag: d.Real,
}
}
if math.Abs(d.Real) == 1 {
return Number{
Real: math.Inf(int(d.Real)),
E1mag: math.NaN(),
E2mag: math.NaN(),
E1E2mag: math.Inf(int(d.Real)),
}
}
fn := math.Atanh(d.Real)
deriv1 := 1 - d.Real*d.Real
deriv := 1 / deriv1
return Number{
Real: fn,
E1mag: deriv * d.E1mag,
E2mag: deriv * d.E2mag,
E1E2mag: deriv*d.E1E2mag + d.E1mag*d.E2mag*(2*d.Real/(deriv1*deriv1)),
}
}