gonum.org/v1/gonum@v0.14.0/num/dual/dual.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 package dual 6 7 import ( 8 "fmt" 9 "math" 10 "strings" 11 ) 12 13 // Number is a float64 precision dual number. 14 type Number struct { 15 Real, Emag float64 16 } 17 18 // Format implements fmt.Formatter. 19 func (d Number) Format(fs fmt.State, c rune) { 20 prec, pOk := fs.Precision() 21 if !pOk { 22 prec = -1 23 } 24 width, wOk := fs.Width() 25 if !wOk { 26 width = -1 27 } 28 switch c { 29 case 'v': 30 if fs.Flag('#') { 31 fmt.Fprintf(fs, "%T{Real:%#v, Emag:%#v}", d, d.Real, d.Emag) 32 return 33 } 34 if fs.Flag('+') { 35 fmt.Fprintf(fs, "{Real:%+v, Emag:%+v}", d.Real, d.Emag) 36 return 37 } 38 c = 'g' 39 prec = -1 40 fallthrough 41 case 'e', 'E', 'f', 'F', 'g', 'G': 42 fre := fmtString(fs, c, prec, width, false) 43 fim := fmtString(fs, c, prec, width, true) 44 fmt.Fprintf(fs, fmt.Sprintf("(%s%[2]sϵ)", fre, fim), d.Real, d.Emag) 45 default: 46 fmt.Fprintf(fs, "%%!%c(%T=%[2]v)", c, d) 47 return 48 } 49 } 50 51 // This is horrible, but it's what we have. 52 func fmtString(fs fmt.State, c rune, prec, width int, wantPlus bool) string { 53 var b strings.Builder 54 b.WriteByte('%') 55 for _, f := range "0+- " { 56 if fs.Flag(int(f)) || (f == '+' && wantPlus) { 57 b.WriteByte(byte(f)) 58 } 59 } 60 if width >= 0 { 61 fmt.Fprint(&b, width) 62 } 63 if prec >= 0 { 64 b.WriteByte('.') 65 if prec > 0 { 66 fmt.Fprint(&b, prec) 67 } 68 } 69 b.WriteRune(c) 70 return b.String() 71 } 72 73 // Add returns the sum of x and y. 74 func Add(x, y Number) Number { 75 return Number{ 76 Real: x.Real + y.Real, 77 Emag: x.Emag + y.Emag, 78 } 79 } 80 81 // Sub returns the difference of x and y, x-y. 82 func Sub(x, y Number) Number { 83 return Number{ 84 Real: x.Real - y.Real, 85 Emag: x.Emag - y.Emag, 86 } 87 } 88 89 // Mul returns the dual product of x and y. 90 func Mul(x, y Number) Number { 91 return Number{ 92 Real: x.Real * y.Real, 93 Emag: x.Real*y.Emag + x.Emag*y.Real, 94 } 95 } 96 97 // Inv returns the dual inverse of d. 98 // 99 // Special cases are: 100 // 101 // Inv(±Inf) = ±0-0ϵ 102 // Inv(±0) = ±Inf-Infϵ 103 func Inv(d Number) Number { 104 d2 := d.Real * d.Real 105 return Number{ 106 Real: 1 / d.Real, 107 Emag: -d.Emag / d2, 108 } 109 } 110 111 // Scale returns d scaled by f. 112 func Scale(f float64, d Number) Number { 113 return Number{Real: f * d.Real, Emag: f * d.Emag} 114 } 115 116 // Abs returns the absolute value of d. 117 func Abs(d Number) Number { 118 if !math.Signbit(d.Real) { 119 return d 120 } 121 return Scale(-1, d) 122 }