mirror of
https://github.com/gonum/gonum.git
synced 2025-10-05 23:26:52 +08:00

This is necessary because gofmt in go1.19 imposes smart quotes on comments that use pairs of single quotes. Doubled-up single tick, U+2032, is chosen over double tick, U+2033, since the latter is harder to distinguish in many fonts at normally used font sizes, sometimes being indistinguishable from other superscript marks such as asterisk. Comparison: ′ ″ *
69 lines
1.7 KiB
Go
69 lines
1.7 KiB
Go
// Copyright ©2016 The Gonum Authors. All rights reserved.
|
||
// Use of this source code is governed by a BSD-style
|
||
// license that can be found in the LICENSE file.
|
||
|
||
package fd_test
|
||
|
||
import (
|
||
"fmt"
|
||
"math"
|
||
|
||
"gonum.org/v1/gonum/diff/fd"
|
||
"gonum.org/v1/gonum/mat"
|
||
)
|
||
|
||
func ExampleDerivative() {
|
||
f := func(x float64) float64 {
|
||
return math.Sin(x)
|
||
}
|
||
// Compute the first derivative of f at 0 using the default settings.
|
||
fmt.Println("f′(0) ≈", fd.Derivative(f, 0, nil))
|
||
// Compute the first derivative of f at 0 using the forward approximation
|
||
// with a custom step size.
|
||
df := fd.Derivative(f, 0, &fd.Settings{
|
||
Formula: fd.Forward,
|
||
Step: 1e-3,
|
||
})
|
||
fmt.Println("f′(0) ≈", df)
|
||
|
||
f = func(x float64) float64 {
|
||
return math.Pow(math.Cos(x), 3)
|
||
}
|
||
// Compute the second derivative of f at 0 using
|
||
// the centered approximation, concurrent evaluation,
|
||
// and a known function value at x.
|
||
df = fd.Derivative(f, 0, &fd.Settings{
|
||
Formula: fd.Central2nd,
|
||
Concurrent: true,
|
||
OriginKnown: true,
|
||
OriginValue: f(0),
|
||
})
|
||
fmt.Println("f′′(0) ≈", df)
|
||
|
||
// Output:
|
||
// f′(0) ≈ 1
|
||
// f′(0) ≈ 0.9999998333333416
|
||
// f′′(0) ≈ -2.999999981767587
|
||
}
|
||
|
||
func ExampleJacobian() {
|
||
f := func(dst, x []float64) {
|
||
dst[0] = x[0] + 1
|
||
dst[1] = 5 * x[2]
|
||
dst[2] = 4*x[1]*x[1] - 2*x[2]
|
||
dst[3] = x[2] * math.Sin(x[0])
|
||
}
|
||
jac := mat.NewDense(4, 3, nil)
|
||
fd.Jacobian(jac, f, []float64{1, 2, 3}, &fd.JacobianSettings{
|
||
Formula: fd.Central,
|
||
Concurrent: true,
|
||
})
|
||
fmt.Printf("J ≈ %.6v\n", mat.Formatted(jac, mat.Prefix(" ")))
|
||
|
||
// Output:
|
||
// J ≈ ⎡ 1 0 0⎤
|
||
// ⎢ 0 0 5⎥
|
||
// ⎢ 0 16 -2⎥
|
||
// ⎣ 1.62091 0 0.841471⎦
|
||
}
|