-
Notifications
You must be signed in to change notification settings - Fork 0
/
sinh.go
160 lines (141 loc) · 3.99 KB
/
sinh.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
// Copyright 2022 The Go 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 ivyshims
import (
"math/big"
)
func sinh(c Context, v Value) Value {
if u, ok := v.(Complex); ok {
if !isZero(u.imag) {
return complexSinh(c, u)
}
v = u.real
}
return evalFloatFunc(c, v, floatSinh)
}
func cosh(c Context, v Value) Value {
if u, ok := v.(Complex); ok {
if !isZero(u.imag) {
return complexCosh(c, u)
}
v = u.real
}
return evalFloatFunc(c, v, floatCosh)
}
func tanh(c Context, v Value) Value {
if u, ok := v.(Complex); ok {
if !isZero(u.imag) {
return complexTanh(c, u)
}
v = u.real
}
return evalFloatFunc(c, v, floatTanh)
}
// floatSinh computes sinh(x) = (e**x - e**-x)/2.
func floatSinh(c Context, x *big.Float) *big.Float {
// The Taylor series for sinh(x) is the odd terms of exp(x): x + x³/3! + x⁵/5!...
conf := c.Config()
xN := newF(conf).Set(x)
term := newF(conf)
n := newF(conf)
nFactorial := newF(conf).SetUint64(1)
z := newF(conf).SetInt64(0)
for loop := newLoop(conf, "sinh", x, 10); ; { // Big exponentials converge slowly.
term.Set(xN)
term.Quo(term, nFactorial)
z.Add(z, term)
if loop.done(z) {
break
}
// Advance x**index (multiply by x).
xN.Mul(xN, x)
xN.Mul(xN, x)
// Advance n, n!.
nFactorial.Mul(nFactorial, n.SetUint64(2*loop.i))
nFactorial.Mul(nFactorial, n.SetUint64(2*loop.i+1))
}
return z
}
// floatCosh computes sinh(x) = (e**x + e**-x)/2.
func floatCosh(c Context, x *big.Float) *big.Float {
// The Taylor series for cosh(x) is the even terms of exp(x): 1 + x²/2! + x⁴/4!...
conf := c.Config()
xN := newF(conf).Set(x)
xN.Mul(xN, x) // x²
term := newF(conf)
n := newF(conf)
nFactorial := newF(conf).SetUint64(2)
z := newF(conf).SetInt64(1)
for loop := newLoop(conf, "cosh", x, 10); ; { // Big exponentials converge slowly.
term.Set(xN)
term.Quo(term, nFactorial)
z.Add(z, term)
if loop.done(z) {
break
}
// Advance x**index (multiply by x).
xN.Mul(xN, x)
xN.Mul(xN, x)
// Advance n, n!.
nFactorial.Mul(nFactorial, n.SetUint64(2*loop.i+1))
nFactorial.Mul(nFactorial, n.SetUint64(2*loop.i+2))
}
return z
}
// floatTanh computes tanh(x) = sinh(x)/cosh(x)
func floatTanh(c Context, x *big.Float) *big.Float {
if x.IsInf() {
Errorf("tanh of infinity")
}
denom := floatCosh(c, x)
if denom.Cmp(floatZero) == 0 {
Errorf("tanh is infinite")
}
num := floatSinh(c, x)
return num.Quo(num, denom)
}
func complexSinh(c Context, v Complex) Value {
// Use the formula: sinh(x+yi) = sinh(x)cos(y) + i cosh(x)sin(y)
// First turn v into (a + bi) where a and b are big.Floats.
x := floatSelf(c, v.real).Float
y := floatSelf(c, v.imag).Float
sinhX := floatSinh(c, x)
cosY := floatCos(c, y)
coshX := floatCosh(c, x)
sinY := floatSin(c, y)
lhs := sinhX.Mul(sinhX, cosY)
rhs := coshX.Mul(coshX, sinY)
return newComplex(BigFloat{lhs}, BigFloat{rhs}).shrink()
}
func complexCosh(c Context, v Complex) Value {
// Use the formula: cosh(x+yi) = cosh(x)cos(y) + i sinh(x)sin(y)
// First turn v into (a + bi) where a and b are big.Floats.
x := floatSelf(c, v.real).Float
y := floatSelf(c, v.imag).Float
coshX := floatCosh(c, x)
cosY := floatCos(c, y)
sinhX := floatSinh(c, x)
sinY := floatSin(c, y)
lhs := coshX.Mul(coshX, cosY)
rhs := sinhX.Mul(sinhX, sinY)
return newComplex(BigFloat{lhs}, BigFloat{rhs}).shrink()
}
func complexTanh(c Context, v Complex) Value {
// Use the formula: tanh(x+yi) = (sinh(2x) + i sin(2y)/(cosh(2x) + cos(2y))
// First turn v into (a + bi) where a and b are big.Floats.
x := floatSelf(c, v.real).Float
y := floatSelf(c, v.imag).Float
// Double them - all the arguments are 2X.
x.Mul(x, floatTwo)
y.Mul(y, floatTwo)
sinh2X := floatSinh(c, x)
sin2Y := floatSin(c, y)
cosh2X := floatCosh(c, x)
cos2Y := floatCos(c, y)
den := cosh2X.Add(cosh2X, cos2Y)
if den.Sign() == 0 {
Errorf("tangent is infinite")
}
return newComplex(BigFloat{sinh2X.Quo(sinh2X, den)}, BigFloat{sin2Y.Quo(sin2Y, den)}).shrink()
}