本文整理汇总了Golang中github.com/gonum/floats.Equal函数的典型用法代码示例。如果您正苦于以下问题:Golang Equal函数的具体用法?Golang Equal怎么用?Golang Equal使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Equal函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestFlattenTriangular
func TestFlattenTriangular(t *testing.T) {
for i, test := range []struct {
a [][]float64
ans []float64
ul blas.Uplo
}{
{
a: [][]float64{
{1, 2, 3},
{0, 4, 5},
{0, 0, 6},
},
ul: blas.Upper,
ans: []float64{1, 2, 3, 4, 5, 6},
},
{
a: [][]float64{
{1, 0, 0},
{2, 3, 0},
{4, 5, 6},
},
ul: blas.Lower,
ans: []float64{1, 2, 3, 4, 5, 6},
},
} {
a := flattenTriangular(test.a, test.ul)
if !floats.Equal(a, test.ans) {
t.Errorf("Case %v. Want %v, got %v.", i, test.ans, a)
}
}
}
示例2: Func
func (b *BatchGradient) Func(params []float64) float64 {
if floats.Equal(params, b.lastParams) {
return b.lastFunc
}
b.lastFunc = b.funcGrad(params, b.lastGrad)
return b.lastFunc
}
示例3: initNextLinesearch
func (ls *LinesearchMethod) initNextLinesearch(loc *Location, xNext []float64) (EvaluationType, IterationType, error) {
copy(ls.x, loc.X)
var stepSize float64
if ls.first {
stepSize = ls.NextDirectioner.InitDirection(loc, ls.dir)
ls.first = false
} else {
stepSize = ls.NextDirectioner.NextDirection(loc, ls.dir)
}
projGrad := floats.Dot(loc.Gradient, ls.dir)
if projGrad >= 0 {
ls.evalType = NoEvaluation
ls.iterType = NoIteration
return ls.evalType, ls.iterType, ErrNonNegativeStepDirection
}
ls.evalType = ls.Linesearcher.Init(loc.F, projGrad, stepSize)
floats.AddScaledTo(xNext, ls.x, stepSize, ls.dir)
// Compare the starting point for the current iteration with the next
// evaluation point to make sure that rounding errors do not prevent progress.
if floats.Equal(ls.x, xNext) {
ls.evalType = NoEvaluation
ls.iterType = NoIteration
return ls.evalType, ls.iterType, ErrNoProgress
}
ls.iterType = MinorIteration
return ls.evalType, ls.iterType, nil
}
示例4: initNextLinesearch
// initNextLinesearch initializes the next linesearch using the previous
// complete location stored in loc. It fills loc.X and returns an evaluation
// to be performed at loc.X.
func (ls *LinesearchMethod) initNextLinesearch(loc *Location) (Operation, error) {
copy(ls.x, loc.X)
var step float64
if ls.first {
ls.first = false
step = ls.NextDirectioner.InitDirection(loc, ls.dir)
} else {
step = ls.NextDirectioner.NextDirection(loc, ls.dir)
}
projGrad := floats.Dot(loc.Gradient, ls.dir)
if projGrad >= 0 {
return ls.error(ErrNonNegativeStepDirection)
}
op := ls.Linesearcher.Init(loc.F, projGrad, step)
if !op.isEvaluation() {
panic("linesearch: Linesearcher returned invalid operation")
}
floats.AddScaledTo(loc.X, ls.x, step, ls.dir)
if floats.Equal(ls.x, loc.X) {
// Step size is so small that the next evaluation point is
// indistinguishable from the starting point for the current iteration
// due to rounding errors.
return ls.error(ErrNoProgress)
}
ls.lastStep = step
ls.eval = NoOperation // Invalidate all fields of loc.
ls.lastOp = op
return ls.lastOp, nil
}
示例5: evaluate
// evaluate evaluates the problem given by p at xNext, stores the answer into
// loc and updates stats. If loc.X is not equal to xNext, then unused fields of
// loc are set to NaN.
// evaluate panics if the function does not support the requested evalType.
func evaluate(p *Problem, evalType EvaluationType, xNext []float64, loc *Location, stats *Stats) {
if !floats.Equal(loc.X, xNext) {
if evalType == NoEvaluation {
// Optimizers should not request NoEvaluation at a new location.
// The intent and therefore an appropriate action are both unclear.
panic("optimize: no evaluation requested at new location")
}
invalidate(loc)
copy(loc.X, xNext)
}
toEval := evalType
if evalType&FuncEvaluation != 0 {
loc.F = p.Func(loc.X)
stats.FuncEvaluations++
toEval &= ^FuncEvaluation
}
if evalType&GradEvaluation != 0 {
p.Grad(loc.X, loc.Gradient)
stats.GradEvaluations++
toEval &= ^GradEvaluation
}
if evalType&HessEvaluation != 0 {
p.Hess(loc.X, loc.Hessian)
stats.HessEvaluations++
toEval &= ^HessEvaluation
}
if toEval != NoEvaluation {
panic(fmt.Sprintf("optimize: unknown evaluation type %v", evalType))
}
}
示例6: DrsclTest
func DrsclTest(t *testing.T, impl Drscler) {
for _, test := range []struct {
x []float64
a float64
}{
{
x: []float64{1, 2, 3, 4, 5},
a: 4,
},
{
x: []float64{1, 2, 3, 4, 5},
a: math.MaxFloat64,
},
{
x: []float64{1, 2, 3, 4, 5},
a: 1e-307,
},
} {
xcopy := make([]float64, len(test.x))
copy(xcopy, test.x)
// Cannot test the scaling directly because of floating point scaling issues
// (the purpose of Drscl). Instead, check that scaling and scaling back
// yeilds approximately x. If overflow or underflow occurs then the scaling
// won't match.
impl.Drscl(len(test.x), test.a, xcopy, 1)
if floats.Equal(xcopy, test.x) {
t.Errorf("x unchanged during call to drscl. a = %v, x = %v.", test.a, test.x)
}
impl.Drscl(len(test.x), 1/test.a, xcopy, 1)
if !floats.EqualApprox(xcopy, test.x, 1e-14) {
t.Errorf("x not equal after scaling and unscaling. a = %v, x = %v.", test.a, test.x)
}
}
}
示例7: Grad
func (b *BatchGradient) Grad(params, deriv []float64) {
if floats.Equal(params, b.lastParams) {
copy(deriv, b.lastGrad)
return
}
b.lastFunc = b.funcGrad(params, b.lastGrad)
copy(deriv, b.lastGrad)
}
示例8: TestQuantile
func TestQuantile(t *testing.T) {
cumulantKinds := []CumulantKind{Empirical}
for i, test := range []struct {
p []float64
x []float64
w []float64
ans [][]float64
}{
{
p: []float64{0, 0.05, 0.1, 0.15, 0.45, 0.5, 0.55, 0.85, 0.9, 0.95, 1},
x: []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
w: nil,
ans: [][]float64{{1, 1, 1, 2, 5, 5, 6, 9, 9, 10, 10}},
},
{
p: []float64{0, 0.05, 0.1, 0.15, 0.45, 0.5, 0.55, 0.85, 0.9, 0.95, 1},
x: []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
w: []float64{3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
ans: [][]float64{{1, 1, 1, 2, 5, 5, 6, 9, 9, 10, 10}},
},
} {
copyX := make([]float64, len(test.x))
copy(copyX, test.x)
var copyW []float64
if test.w != nil {
copyW = make([]float64, len(test.w))
copy(copyW, test.w)
}
for j, p := range test.p {
for k, kind := range cumulantKinds {
v := Quantile(p, kind, test.x, test.w)
if !floats.Equal(copyX, test.x) {
t.Errorf("x changed for case %d kind %d percentile %v", i, k, p)
}
if !floats.Equal(copyW, test.w) {
t.Errorf("x changed for case %d kind %d percentile %v", i, k, p)
}
if v != test.ans[k][j] {
t.Errorf("mismatch case %d kind %d percentile %v. Expected: %v, found: %v", i, k, p, test.ans[k][j], v)
}
}
}
}
}
示例9: TestCDF
func TestCDF(t *testing.T) {
cumulantKinds := []CumulantKind{Empirical}
for i, test := range []struct {
q []float64
x []float64
weights []float64
ans [][]float64
}{
{},
{
q: []float64{0, 0.9, 1, 1.1, 2.9, 3, 3.1, 4.9, 5, 5.1},
x: []float64{1, 2, 3, 4, 5},
ans: [][]float64{{0, 0, 0.2, 0.2, 0.4, 0.6, 0.6, 0.8, 1, 1}},
},
{
q: []float64{0, 0.9, 1, 1.1, 2.9, 3, 3.1, 4.9, 5, 5.1},
x: []float64{1, 2, 3, 4, 5},
weights: []float64{1, 1, 1, 1, 1},
ans: [][]float64{{0, 0, 0.2, 0.2, 0.4, 0.6, 0.6, 0.8, 1, 1}},
},
} {
copyX := make([]float64, len(test.x))
copy(copyX, test.x)
var copyW []float64
if test.weights != nil {
copyW = make([]float64, len(test.weights))
copy(copyW, test.weights)
}
for j, q := range test.q {
for k, kind := range cumulantKinds {
v := CDF(q, kind, test.x, test.weights)
if !floats.Equal(copyX, test.x) {
t.Errorf("x changed for case %d kind %d percentile %v", i, k, q)
}
if !floats.Equal(copyW, test.weights) {
t.Errorf("x changed for case %d kind %d percentile %v", i, k, q)
}
if v != test.ans[k][j] {
t.Errorf("mismatch case %d kind %d percentile %v. Expected: %v, found: %v", i, k, q, test.ans[k][j], v)
}
}
}
}
}
示例10: TestFlatten2D
func TestFlatten2D(t *testing.T) {
s2d := [][]float64{{11, 22}, {33, 44}, {55, 66}}
expected := []float64{11, 22, 33, 44, 55, 66}
flatten := Flatten2D(s2d)
if !floats.Equal(flatten, expected) {
t.Fatalf("Flatten failed. expected %+v, got %+v", expected, flatten)
}
}
示例11: Iterate
func (ls *LinesearchMethod) Iterate(loc *Location, xNext []float64) (EvaluationType, IterationType, error) {
if ls.iterType == SubIteration {
// We needed to evaluate invalid fields of Location. Now we have them
// and can announce MajorIteration.
copy(xNext, loc.X)
ls.evalType = NoEvaluation
ls.iterType = MajorIteration
return ls.evalType, ls.iterType, nil
}
if ls.iterType == MajorIteration {
// The linesearch previously signaled MajorIteration. Since we're here,
// it means that the previous location is not good enough to converge,
// so start the next linesearch.
return ls.initNextLinesearch(loc, xNext)
}
projGrad := floats.Dot(loc.Gradient, ls.dir)
if ls.Linesearcher.Finished(loc.F, projGrad) {
copy(xNext, loc.X)
// Check if the last evaluation evaluated all fields of Location.
ls.evalType = complementEval(loc, ls.evalType)
if ls.evalType == NoEvaluation {
// Location is complete and MajorIteration can be announced directly.
ls.iterType = MajorIteration
} else {
// Location is not complete, evaluate its invalid fields in SubIteration.
ls.iterType = SubIteration
}
return ls.evalType, ls.iterType, nil
}
// Line search not done, just iterate.
stepSize, evalType, err := ls.Linesearcher.Iterate(loc.F, projGrad)
if err != nil {
ls.evalType = NoEvaluation
ls.iterType = NoIteration
return ls.evalType, ls.iterType, err
}
floats.AddScaledTo(xNext, ls.x, stepSize, ls.dir)
// Compare the starting point for the current iteration with the next
// evaluation point to make sure that rounding errors do not prevent progress.
if floats.Equal(ls.x, xNext) {
ls.evalType = NoEvaluation
ls.iterType = NoIteration
return ls.evalType, ls.iterType, ErrNoProgress
}
ls.evalType = evalType
ls.iterType = MinorIteration
return ls.evalType, ls.iterType, nil
}
示例12: TestLegendreSingle
func TestLegendreSingle(t *testing.T) {
for c, test := range []struct {
n int
min, max float64
}{
{
n: 100,
min: -1,
max: 1,
},
{
n: 50,
min: -3,
max: -1,
},
{
n: 1000,
min: 2,
max: 7,
},
} {
l := Legendre{}
n := test.n
xs := make([]float64, n)
weights := make([]float64, n)
l.FixedLocations(xs, weights, test.min, test.max)
xsSingle := make([]float64, n)
weightsSingle := make([]float64, n)
for i := range xsSingle {
xsSingle[i], weightsSingle[i] = l.FixedLocationSingle(n, i, test.min, test.max)
}
if !floats.Equal(xs, xsSingle) {
t.Errorf("Case %d: xs mismatch batch and single", c)
}
if !floats.Equal(weights, weightsSingle) {
t.Errorf("Case %d: weights mismatch batch and single", c)
}
}
}
示例13: denseEqual
func denseEqual(a *Dense, acomp matComp) bool {
ar2, ac2 := a.Dims()
if ar2 != acomp.r {
return false
}
if ac2 != acomp.c {
return false
}
if !floats.Equal(a.mat.Data, acomp.data) {
return false
}
return true
}
示例14: TestCopy2D
func TestCopy2D(t *testing.T) {
s1 := [][]float64{{11, 22}, {33, 44}, {55, 66}}
s2 := CopyFloat2D(s1)
for k, _ := range s1 {
if &s1[k] == &s2[k] {
t.Fatalf("Slices have the same address, not a copy.")
}
if !floats.Equal(s1[k], s2[k]) {
t.Fatalf("Copy failed. want: %+v, have: %+v", s1, s2)
}
}
}
示例15: TestPredictFeaturized
func TestPredictFeaturized(t *testing.T) {
for _, test := range []struct {
z []float64
featureWeights [][]float64
output []float64
Name string
}{
{
Name: "General",
z: []float64{1, 2, 3},
featureWeights: [][]float64{
{3, 4},
{1, 2},
{0.5, 0.4},
},
output: []float64{6.5, 9.2},
},
} {
zCopy := make([]float64, len(test.z))
copy(zCopy, test.z)
fwMat := flatten(test.featureWeights)
fwMatCopy := &mat64.Dense{}
fwMatCopy.Clone(fwMat)
output := make([]float64, len(test.output))
predictFeaturized(zCopy, fwMat, output)
// Test that z wasn't changed
if !floats.Equal(test.z, zCopy) {
t.Errorf("z changed during call")
}
if !floats.EqualApprox(output, test.output, 1e-14) {
t.Errorf("output doesn't match for test %v. Expected %v, found %v", test.Name, test.output, output)
}
}
}