File: errors.go

package info (click to toggle)
golang-gonum-v1-gonum 0.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,792 kB
  • sloc: asm: 6,252; fortran: 5,271; sh: 377; ruby: 211; makefile: 98
file content (78 lines) | stat: -rw-r--r-- 2,868 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright ©2014 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 optimize

import (
	"errors"
	"fmt"
	"math"
)

var (
	// ErrZeroDimensional signifies an optimization was called with an input of length 0.
	ErrZeroDimensional = errors.New("optimize: zero dimensional input")

	// ErrLinesearcherFailure signifies that a Linesearcher has iterated too
	// many times. This may occur if the gradient tolerance is set too low.
	ErrLinesearcherFailure = errors.New("linesearch: failed to converge")

	// ErrNonDescentDirection signifies that LinesearchMethod has received a
	// search direction from a NextDirectioner in which the function is not
	// decreasing.
	ErrNonDescentDirection = errors.New("linesearch: non-descent search direction")

	// ErrNoProgress signifies that LinesearchMethod cannot make further
	// progress because there is no change in location after Linesearcher step
	// due to floating-point arithmetic.
	ErrNoProgress = errors.New("linesearch: no change in location after Linesearcher step")

	// ErrLinesearcherBound signifies that a Linesearcher reached a step that
	// lies out of allowed bounds.
	ErrLinesearcherBound = errors.New("linesearch: step out of bounds")

	// ErrMissingGrad signifies that a Method requires a Gradient function that
	// is not supplied by Problem.
	ErrMissingGrad = errors.New("optimize: problem does not provide needed Grad function")

	// ErrMissingHess signifies that a Method requires a Hessian function that
	// is not supplied by Problem.
	ErrMissingHess = errors.New("optimize: problem does not provide needed Hess function")
)

// ErrFunc is returned when an initial function value is invalid. The error
// state may be either +Inf or NaN. ErrFunc satisfies the error interface.
type ErrFunc float64

func (err ErrFunc) Error() string {
	switch {
	case math.IsInf(float64(err), 1):
		return "optimize: initial function value is infinite"
	case math.IsNaN(float64(err)):
		return "optimize: initial function value is NaN"
	default:
		panic("optimize: bad ErrFunc")
	}
}

// ErrGrad is returned when an initial gradient is invalid. The error gradient
// may be either ±Inf or NaN. ErrGrad satisfies the error interface.
type ErrGrad struct {
	Grad  float64 // Grad is the invalid gradient value.
	Index int     // Index is the position at which the invalid gradient was found.
}

func (err ErrGrad) Error() string {
	switch {
	case math.IsInf(err.Grad, 0):
		return fmt.Sprintf("optimize: initial gradient is infinite at position %d", err.Index)
	case math.IsNaN(err.Grad):
		return fmt.Sprintf("optimize: initial gradient is NaN at position %d", err.Index)
	default:
		panic("optimize: bad ErrGrad")
	}
}

// List of shared panic strings
const badProblem = "optimize: objective function is undefined"