-
Notifications
You must be signed in to change notification settings - Fork 523
/
exp.go
64 lines (57 loc) · 1.46 KB
/
exp.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
// 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.
// Copyright 2017 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 quat
import "math"
// Exp returns e**q, the base-e exponential of q.
func Exp(q Number) Number {
w, uv := split(q)
if uv == zero {
return lift(math.Exp(w))
}
v := Abs(uv)
e := math.Exp(w)
s, c := math.Sincos(v)
return join(e*c, Scale(e*s/v, uv))
}
// Log returns the natural logarithm of q.
func Log(q Number) Number {
w, uv := split(q)
if uv == zero {
return lift(math.Log(w))
}
v := Abs(uv)
return join(math.Log(Abs(q)), Scale(math.Atan2(v, w)/v, uv))
}
// Pow return q**r, the base-q exponential of r.
// For generalized compatibility with math.Pow:
// Pow(0, ±0) returns 1+0i+0j+0k
// Pow(0, c) for real(c)<0 returns Inf+0i+0j+0k if imag(c), jmag(c), kmag(c) are zero,
// otherwise Inf+Inf i+Inf j+Inf k.
func Pow(q, r Number) Number {
if q == zero {
w, uv := split(r)
switch {
case w == 0:
return Number{Real: 1}
case w < 0:
if uv == zero {
return Number{Real: math.Inf(1)}
}
return Inf()
case w > 0:
return zero
}
}
return Exp(Mul(Log(q), r))
}
// Sqrt returns the square root of q.
func Sqrt(q Number) Number {
if q == zero {
return zero
}
return Pow(q, Number{Real: 0.5})
}