forked from gonum/gonum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dual_hyperbolic.go
171 lines (163 loc) · 2.83 KB
/
dual_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
// 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 dual
import "math"
// Sinh returns the hyperbolic sine of d.
//
// Special cases are:
//
// Sinh(±0) = (±0+Nϵ)
// Sinh(±Inf) = ±Inf
// Sinh(NaN) = NaN
func Sinh(d Number) Number {
if d.Real == 0 {
return Number{
Real: d.Real,
Emag: d.Emag,
}
}
if math.IsInf(d.Real, 0) {
return Number{
Real: d.Real,
Emag: math.Inf(1),
}
}
fn := math.Sinh(d.Real)
deriv := math.Cosh(d.Real)
return Number{
Real: fn,
Emag: deriv * d.Emag,
}
}
// 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),
Emag: d.Real,
}
}
fn := math.Cosh(d.Real)
deriv := math.Sinh(d.Real)
return Number{
Real: fn,
Emag: deriv * d.Emag,
}
}
// Tanh returns the hyperbolic tangent of d.
//
// Special cases are:
//
// Tanh(±0) = (±0+Nϵ)
// Tanh(±Inf) = (±1+0ϵ)
// Tanh(NaN) = NaN
func Tanh(d Number) Number {
switch d.Real {
case 0:
return Number{
Real: d.Real,
Emag: d.Emag,
}
case math.Inf(1):
return Number{
Real: 1,
Emag: 0,
}
case math.Inf(-1):
return Number{
Real: -1,
Emag: 0,
}
}
fn := math.Tanh(d.Real)
deriv := 1 - fn*fn
return Number{
Real: fn,
Emag: deriv * d.Emag,
}
}
// Asinh returns the inverse hyperbolic sine of d.
//
// Special cases are:
//
// Asinh(±0) = (±0+Nϵ)
// Asinh(±Inf) = ±Inf
// Asinh(NaN) = NaN
func Asinh(d Number) Number {
if d.Real == 0 {
return Number{
Real: d.Real,
Emag: d.Emag,
}
}
fn := math.Asinh(d.Real)
deriv := 1 / math.Sqrt(d.Real*d.Real+1)
return Number{
Real: fn,
Emag: deriv * d.Emag,
}
}
// Acosh returns the inverse hyperbolic cosine of d.
//
// Special cases are:
//
// Acosh(+Inf) = +Inf
// Acosh(1) = (0+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,
Emag: math.Inf(1),
}
}
return Number{
Real: math.NaN(),
Emag: math.NaN(),
}
}
fn := math.Acosh(d.Real)
deriv := 1 / math.Sqrt(d.Real*d.Real-1)
return Number{
Real: fn,
Emag: deriv * d.Emag,
}
}
// Atanh returns the inverse hyperbolic tangent of d.
//
// Special cases are:
//
// Atanh(1) = +Inf
// Atanh(±0) = (±0+Nϵ)
// 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,
Emag: d.Emag,
}
}
if math.Abs(d.Real) == 1 {
return Number{
Real: math.Inf(int(d.Real)),
Emag: math.NaN(),
}
}
fn := math.Atanh(d.Real)
deriv := 1 / (1 - d.Real*d.Real)
return Number{
Real: fn,
Emag: deriv * d.Emag,
}
}