github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/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  //	Inv(±Inf) = ±0-0ϵ
   101  //	Inv(±0) = ±Inf-Infϵ
   102  func Inv(d Number) Number {
   103  	d2 := d.Real * d.Real
   104  	return Number{
   105  		Real: 1 / d.Real,
   106  		Emag: -d.Emag / d2,
   107  	}
   108  }
   109  
   110  // Scale returns d scaled by f.
   111  func Scale(f float64, d Number) Number {
   112  	return Number{Real: f * d.Real, Emag: f * d.Emag}
   113  }
   114  
   115  // Abs returns the absolute value of d.
   116  func Abs(d Number) Number {
   117  	if !math.Signbit(d.Real) {
   118  		return d
   119  	}
   120  	return Scale(-1, d)
   121  }