github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/num/quat/exp.go (about) 1 // Copyright ©2018 The Gonum Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Copyright 2017 The Go Authors. All rights reserved. 6 // Use of this source code is governed by a BSD-style 7 // license that can be found in the LICENSE file. 8 9 package quat 10 11 import "math" 12 13 // Exp returns e**q, the base-e exponential of q. 14 func Exp(q Number) Number { 15 w, uv := split(q) 16 if uv == zero { 17 return lift(math.Exp(w)) 18 } 19 v := Abs(uv) 20 e := math.Exp(w) 21 s, c := math.Sincos(v) 22 return join(e*c, Scale(e*s/v, uv)) 23 } 24 25 // Log returns the natural logarithm of q. 26 func Log(q Number) Number { 27 w, uv := split(q) 28 if uv == zero { 29 return lift(math.Log(w)) 30 } 31 v := Abs(uv) 32 return join(math.Log(Abs(q)), Scale(math.Atan2(v, w)/v, uv)) 33 } 34 35 // Pow return q**r, the base-q exponential of r. 36 // For generalized compatibility with math.Pow: 37 // Pow(0, ±0) returns 1+0i+0j+0k 38 // Pow(0, c) for real(c)<0 returns Inf+0i+0j+0k if imag(c), jmag(c), kmag(c) are zero, 39 // otherwise Inf+Inf i+Inf j+Inf k. 40 func Pow(q, r Number) Number { 41 if q == zero { 42 w, uv := split(r) 43 switch { 44 case w == 0: 45 return Number{Real: 1} 46 case w < 0: 47 if uv == zero { 48 return Number{Real: math.Inf(1)} 49 } 50 return Inf() 51 case w > 0: 52 return zero 53 } 54 } 55 return Exp(Mul(Log(q), r)) 56 } 57 58 // Sqrt returns the square root of q. 59 func Sqrt(q Number) Number { 60 if q == zero { 61 return zero 62 } 63 return Pow(q, Number{Real: 0.5}) 64 }