gorgonia.org/gorgonia@v0.9.17/math.go (about) 1 package gorgonia 2 3 import ( 4 "math" 5 6 "github.com/chewxy/math32" 7 ) 8 9 // functions in this file are functions that do not have an optimized/hacked up version 10 // typically I'd have done some benchmark vs some hacked up version and determined that the default implementation is indeed superior 11 12 /* UNARY OPS */ 13 func _signf32(x float32) float32 { 14 if math32.Signbit(x) { 15 return float32(-1.0) 16 } 17 return 1 18 } 19 20 func _signf64(x float64) float64 { 21 if math.Signbit(x) { 22 return -1.0 23 } 24 return 1 25 } 26 27 func _squaref64(x float64) float64 { return x * x } 28 func _squaref32(x float32) float32 { return x * x } 29 30 func _cubef64(x float64) float64 { return x * x * x } 31 func _cubef32(x float32) float32 { return x * x * x } 32 33 func _negf32(x float32) float32 { return -x } 34 func _negf64(x float64) float64 { return -x } 35 36 /* TODO: write optimized versions of these */ 37 38 // bounds acquired with this: 39 /* 40 func main() { 41 var buf bytes.Buffer 42 for i := -1000; i < 1000; i++ { 43 res := math.Log1p(math.Exp(float64(i))) 44 fmt.Fprintf(&buf, "%d\t%v\n", i, res) 45 } 46 fmt.Println(buf.String()) 47 } 48 */ 49 // I chose 16 because from 17 onwards to 709, its pretty much returns x (with some stupid small decimals) 50 func _softplusf64(x float64) float64 { 51 if x < -708 { 52 return 0 53 } 54 if x > 16 { 55 return x 56 } 57 return math.Log1p(math.Exp(x)) 58 } 59 60 func _softplusf32(x float32) float32 { 61 if x < -103 { 62 return 0 63 } 64 if x > 14 { 65 return x 66 } 67 return float32(math.Log1p(math.Exp(float64(x)))) 68 }