gonum.org/v1/gonum@v0.14.0/num/hyperdual/hyperdual_example_test.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 hyperdual_test
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"gonum.org/v1/gonum/num/hyperdual"
    11  )
    12  
    13  func ExampleNumber_fike() {
    14  	// Calculate the value and first and second derivatives
    15  	// of the function e^x/(sqrt(sin(x)^3 + cos(x)^3)).
    16  	fn := func(x hyperdual.Number) hyperdual.Number {
    17  		return hyperdual.Mul(
    18  			hyperdual.Exp(x),
    19  			hyperdual.Inv(hyperdual.Sqrt(
    20  				hyperdual.Add(
    21  					hyperdual.PowReal(hyperdual.Sin(x), 3),
    22  					hyperdual.PowReal(hyperdual.Cos(x), 3)))))
    23  	}
    24  
    25  	v := fn(hyperdual.Number{Real: 1.5, E1mag: 1, E2mag: 1})
    26  	fmt.Printf("v=%.4f\n", v)
    27  	fmt.Printf("fn(1.5)=%.4f\nfn′(1.5)=%.4f\nfn′′(1.5)=%.4f\n", v.Real, v.E1mag, v.E1E2mag)
    28  
    29  	// Output:
    30  	//
    31  	// v=(4.4978+4.0534ϵ₁+4.0534ϵ₂+9.4631ϵ₁ϵ₂)
    32  	// fn(1.5)=4.4978
    33  	// fn′(1.5)=4.0534
    34  	// fn′′(1.5)=9.4631
    35  }