func SubTo(dst, s, t []float64) []float64 {
	if len(s) != len(t) {
		panic(badLength)
	}
	if len(dst) != len(s) {
		panic(badDstLength)
	}
	f64.AxpyUnitaryTo(dst, -1, t, s)
	return dst
}
SubTo subtracts, element-wise, the elements of t from s and
stores the result in dst.
It panics if the argument lengths do not match.
package floats
import (
	"errors"
	"math"
	"sort"
	"gonum.org/v1/gonum/floats/scalar"
	"gonum.org/v1/gonum/internal/asm/f64"
)
const (
	zeroLength   = "floats: zero length slice"
	shortSpan    = "floats: slice length less than 2"
	badLength    = "floats: slice lengths do not match"
	badDstLength = "floats: destination slice length does not match input"
)
func Add(dst, s []float64) {
	if len(dst) != len(s) {
		panic(badDstLength)
	}
	f64.AxpyUnitaryTo(dst, 1, s, dst)
}
func AddTo(dst, s, t []float64) []float64 {
	if len(s) != len(t) {
		panic(badLength)
	}
	if len(dst) != len(s) {
		panic(badDstLength)
	}
	f64.AxpyUnitaryTo(dst, 1, s, t)
	return dst
}
func AddConst(c float64, dst []float64) {
	f64.AddConst(c, dst)
}
func AddScaled(dst []float64, alpha float64, s []float64) {
	if len(dst) != len(s) {
		panic(badLength)
	}
	f64.AxpyUnitaryTo(dst, alpha, s, dst)
}
func AddScaledTo(dst, y []float64, alpha float64, s []float64) []float64 {
	if len(s) != len(y) {
		panic(badLength)
	}
	if len(dst) != len(y) {
		panic(badDstLength)
	}
	f64.AxpyUnitaryTo(dst, alpha, s, y)
	return dst
}
type argsort struct {
	s    []float64
	inds []int
}
func (a argsort) Len() int {
	return len(a.s)
}
func (a argsort) Less(i, j int) bool {
	return a.s[i] < a.s[j]
}
func (a argsort) Swap(i, j int) {
	a.s[i], a.s[j] = a.s[j], a.s[i]
	a.inds[i], a.inds[j] = a.inds[j], a.inds[i]
}
func Argsort(dst []float64, inds []int) {
	if len(dst) != len(inds) {
		panic(badDstLength)
	}
	for i := range dst {
		inds[i] = i
	}
	a := argsort{s: dst, inds: inds}
	sort.Sort(a)
}
func ArgsortStable(dst []float64, inds []int) {
	if len(dst) != len(inds) {
		panic(badDstLength)
	}
	for i := range dst {
		inds[i] = i
	}
	a := argsort{s: dst, inds: inds}
	sort.Stable(a)
}
func Count(f func(float64) bool, s []float64) int {
	var n int
	for _, val := range s {
		if f(val) {
			n++
		}
	}
	return n
}
func CumProd(dst, s []float64) []float64 {
	if len(dst) != len(s) {
		panic(badDstLength)
	}
	if len(dst) == 0 {
		return dst
	}
	return f64.CumProd(dst, s)
}
func CumSum(dst, s []float64) []float64 {
	if len(dst) != len(s) {
		panic(badDstLength)
	}
	if len(dst) == 0 {
		return dst
	}
	return f64.CumSum(dst, s)
}
func Distance(s, t []float64, L float64) float64 {
	if len(s) != len(t) {
		panic(badLength)
	}
	if len(s) == 0 {
		return 0
	}
	if L == 2 {
		return f64.L2DistanceUnitary(s, t)
	}
	var norm float64
	if L == 1 {
		for i, v := range s {
			norm += math.Abs(t[i] - v)
		}
		return norm
	}
	if math.IsInf(L, 1) {
		for i, v := range s {
			absDiff := math.Abs(t[i] - v)
			if absDiff > norm {
				norm = absDiff
			}
		}
		return norm
	}
	for i, v := range s {
		norm += math.Pow(math.Abs(t[i]-v), L)
	}
	return math.Pow(norm, 1/L)
}
func Div(dst, s []float64) {
	if len(dst) != len(s) {
		panic(badLength)
	}
	f64.Div(dst, s)
}
func DivTo(dst, s, t []float64) []float64 {
	if len(s) != len(t) {
		panic(badLength)
	}
	if len(dst) != len(s) {
		panic(badDstLength)
	}
	return f64.DivTo(dst, s, t)
}
func Dot(s1, s2 []float64) float64 {
	if len(s1) != len(s2) {
		panic(badLength)
	}
	return f64.DotUnitary(s1, s2)
}
func Equal(s1, s2 []float64) bool {
	if len(s1) != len(s2) {
		return false
	}
	for i, val := range s1 {
		if s2[i] != val {
			return false
		}
	}
	return true
}
func EqualApprox(s1, s2 []float64, tol float64) bool {
	if len(s1) != len(s2) {
		return false
	}
	for i, a := range s1 {
		if !scalar.EqualWithinAbsOrRel(a, s2[i], tol, tol) {
			return false
		}
	}
	return true
}
func EqualFunc(s1, s2 []float64, f func(float64, float64) bool) bool {
	if len(s1) != len(s2) {
		return false
	}
	for i, val := range s1 {
		if !f(val, s2[i]) {
			return false
		}
	}
	return true
}
func EqualLengths(slices ...[]float64) bool {
	if len(slices) == 0 {
		return true
	}
	l := len(slices[0])
	for i := 1; i < len(slices); i++ {
		if len(slices[i]) != l {
			return false
		}
	}
	return true
}
func Find(inds []int, f func(float64) bool, s []float64, k int) ([]int, error) {
	inds = inds[:0]
	if k == 0 {
		return inds, nil
	}
	if k < 0 {
		for i, val := range s {
			if f(val) {
				inds = append(inds, i)
			}
		}
		return inds, nil
	}
	nFound := 0
	for i, val := range s {
		if f(val) {
			inds = append(inds, i)
			nFound++
			if nFound == k {
				return inds, nil
			}
		}
	}
	return inds, errors.New("floats: insufficient elements found")
}
func HasNaN(s []float64) bool {
	for _, v := range s {
		if math.IsNaN(v) {
			return true
		}
	}
	return false
}
func LogSpan(dst []float64, l, u float64) []float64 {
	Span(dst, math.Log(l), math.Log(u))
	for i := range dst {
		dst[i] = math.Exp(dst[i])
	}
	return dst
}
func LogSumExp(s []float64) float64 {
	maxval := Max(s)
	if math.IsInf(maxval, 0) {
		return maxval
	}
	var lse float64
	for _, val := range s {
		lse += math.Exp(val - maxval)
	}
	return math.Log(lse) + maxval
}
func Max(s []float64) float64 {
	return s[MaxIdx(s)]
}
func MaxIdx(s []float64) int {
	if len(s) == 0 {
		panic(zeroLength)
	}
	max := math.NaN()
	var ind int
	for i, v := range s {
		if math.IsNaN(v) {
			continue
		}
		if v > max || math.IsNaN(max) {
			max = v
			ind = i
		}
	}
	return ind
}
func Min(s []float64) float64 {
	return s[MinIdx(s)]
}
func MinIdx(s []float64) int {
	if len(s) == 0 {
		panic(zeroLength)
	}
	min := math.NaN()
	var ind int
	for i, v := range s {
		if math.IsNaN(v) {
			continue
		}
		if v < min || math.IsNaN(min) {
			min = v
			ind = i
		}
	}
	return ind
}
func Mul(dst, s []float64) {
	if len(dst) != len(s) {
		panic(badLength)
	}
	for i, val := range s {
		dst[i] *= val
	}
}
func MulTo(dst, s, t []float64) []float64 {
	if len(s) != len(t) {
		panic(badLength)
	}
	if len(dst) != len(s) {
		panic(badDstLength)
	}
	for i, val := range t {
		dst[i] = val * s[i]
	}
	return dst
}
func NearestIdx(s []float64, v float64) int {
	if len(s) == 0 {
		panic(zeroLength)
	}
	switch {
	case math.IsNaN(v):
		return 0
	case math.IsInf(v, 1):
		return MaxIdx(s)
	case math.IsInf(v, -1):
		return MinIdx(s)
	}
	var ind int
	dist := math.NaN()
	for i, val := range s {
		newDist := math.Abs(v - val)
		if math.IsNaN(newDist) {
			continue
		}
		if newDist < dist || math.IsNaN(dist) {
			dist = newDist
			ind = i
		}
	}
	return ind
}
func NearestIdxForSpan(n int, l, u float64, v float64) int {
	if n < 2 {
		panic(shortSpan)
	}
	if math.IsNaN(v) {
		return 0
	}
	switch {
	case math.IsNaN(l) && !math.IsNaN(u):
		return n - 1
	case math.IsNaN(u):
		return 0
	case math.IsInf(l, 0) && math.IsInf(u, 0):
		if l == u {
			return 0
		}
		if n%2 == 1 {
			if !math.IsInf(v, 0) {
				return n / 2
			}
			if math.Copysign(1, v) == math.Copysign(1, l) {
				return 0
			}
			return n/2 + 1
		}
		if math.Copysign(1, v) == math.Copysign(1, l) {
			return 0
		}
		return n / 2
	case math.IsInf(l, 0):
		if v == l {
			return 0
		}
		return n - 1
	case math.IsInf(u, 0):
		if v == u {
			return n - 1
		}
		return 0
	case math.IsInf(v, -1):
		if l <= u {
			return 0
		}
		return n - 1
	case math.IsInf(v, 1):
		if u <= l {
			return 0
		}
		return n - 1
	}
	switch {
	case l < u:
		if v <= l {
			return 0
		}
		if v >= u {
			return n - 1
		}
	case l > u:
		if v >= l {
			return 0
		}
		if v <= u {
			return n - 1
		}
	default:
		return 0
	}
	return int((float64(n)-1)/(u-l)*(v-l) + 0.5)
}
func Norm(s []float64, L float64) float64 {
	if len(s) == 0 {
		return 0
	}
	if L == 2 {
		return f64.L2NormUnitary(s)
	}
	var norm float64
	if L == 1 {
		for _, val := range s {
			norm += math.Abs(val)
		}
		return norm
	}
	if math.IsInf(L, 1) {
		for _, val := range s {
			norm = math.Max(norm, math.Abs(val))
		}
		return norm
	}
	for _, val := range s {
		norm += math.Pow(math.Abs(val), L)
	}
	return math.Pow(norm, 1/L)
}
func Prod(s []float64) float64 {
	prod := 1.0
	for _, val := range s {
		prod *= val
	}
	return prod
}
func Reverse(s []float64) {
	for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
		s[i], s[j] = s[j], s[i]
	}
}
func Same(s, t []float64) bool {
	if len(s) != len(t) {
		return false
	}
	for i, v := range s {
		w := t[i]
		if v != w && !(math.IsNaN(v) && math.IsNaN(w)) {
			return false
		}
	}
	return true
}
func Scale(c float64, dst []float64) {
	if len(dst) > 0 {
		f64.ScalUnitary(c, dst)
	}
}
func ScaleTo(dst []float64, c float64, s []float64) []float64 {
	if len(dst) != len(s) {
		panic(badDstLength)
	}
	if len(dst) > 0 {
		f64.ScalUnitaryTo(dst, c, s)
	}
	return dst
}
func Span(dst []float64, l, u float64) []float64 {
	n := len(dst)
	if n < 2 {
		panic(shortSpan)
	}
	switch {
	case math.IsNaN(l):
		for i := range dst[:len(dst)-1] {
			dst[i] = math.NaN()
		}
		dst[len(dst)-1] = u
		return dst
	case math.IsNaN(u):
		for i := range dst[1:] {
			dst[i+1] = math.NaN()
		}
		dst[0] = l
		return dst
	case math.IsInf(l, 0) && math.IsInf(u, 0):
		for i := range dst[:len(dst)/2] {
			dst[i] = l
			dst[len(dst)-i-1] = u
		}
		if len(dst)%2 == 1 {
			if l != u {
				dst[len(dst)/2] = 0
			} else {
				dst[len(dst)/2] = l
			}
		}
		return dst
	case math.IsInf(l, 0):
		for i := range dst[:len(dst)-1] {
			dst[i] = l
		}
		dst[len(dst)-1] = u
		return dst
	case math.IsInf(u, 0):
		for i := range dst[1:] {
			dst[i+1] = u
		}
		dst[0] = l
		return dst
	}
	step := (u - l) / float64(n-1)
	for i := range dst {
		dst[i] = l + step*float64(i)
	}
	return dst
}
func Sub(dst, s []float64) {
	if len(dst) != len(s) {
		panic(badLength)
	}
	f64.AxpyUnitaryTo(dst, -1, s, dst)
}