github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/num/dual/dual_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 dual_test 6 7 import ( 8 "fmt" 9 10 "github.com/jingcheng-WU/gonum/num/dual" 11 ) 12 13 func ExampleNumber_fike() { 14 // Calculate the value and derivative of the function 15 // e^x/(sqrt(sin(x)^3 + cos(x)^3)). 16 fn := func(x dual.Number) dual.Number { 17 return dual.Mul( 18 dual.Exp(x), 19 dual.Inv(dual.Sqrt( 20 dual.Add( 21 dual.PowReal(dual.Sin(x), 3), 22 dual.PowReal(dual.Cos(x), 3))))) 23 } 24 25 v := fn(dual.Number{Real: 1.5, Emag: 1}) 26 fmt.Printf("v=%.4f\n", v) 27 fmt.Printf("fn(1.5)=%.4f\nfn'(1.5)=%.4f\n", v.Real, v.Emag) 28 29 // Output: 30 // 31 // v=(4.4978+4.0534ϵ) 32 // fn(1.5)=4.4978 33 // fn'(1.5)=4.0534 34 }