gonum.org/v1/gonum@v0.14.0/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 // 38 // Pow(0, ±0) returns 1+0i+0j+0k 39 // Pow(0, c) for real(c)<0 returns Inf+0i+0j+0k if imag(c), jmag(c), kmag(c) are zero, 40 // otherwise Inf+Inf i+Inf j+Inf k. 41 func Pow(q, r Number) Number { 42 if q == zero { 43 w, uv := split(r) 44 switch { 45 case w == 0: 46 return Number{Real: 1} 47 case w < 0: 48 if uv == zero { 49 return Number{Real: math.Inf(1)} 50 } 51 return Inf() 52 case w > 0: 53 return zero 54 } 55 } 56 return Exp(Mul(Log(q), r)) 57 } 58 59 // PowReal return q**r, the base-q exponential of r. 60 // For generalized compatibility with math.Pow: 61 // 62 // PowReal(0, ±0) returns 1+0i+0j+0k 63 // PowReal(0, c) for c<0 returns Inf+0i+0j+0k. 64 func PowReal(q Number, r float64) Number { 65 if q == zero { 66 switch { 67 case r == 0: 68 return Number{Real: 1} 69 case r < 0: 70 return Inf() 71 case r > 0: 72 return zero 73 } 74 } 75 return Exp(Scale(r, Log(q))) 76 } 77 78 // Sqrt returns the square root of q. 79 func Sqrt(q Number) Number { 80 if q == zero { 81 return zero 82 } 83 return PowReal(q, 0.5) 84 }