go-hep.org/x/hep@v0.38.1/fit/curve_nd_test.go (about)

     1  // Copyright ©2020 The go-hep 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 fit_test
     6  
     7  import (
     8  	"math/rand/v2"
     9  	"testing"
    10  )
    11  
    12  func TestCurve2D(t *testing.T) {
    13  	checkPlot(ExampleCurveND_plane, t, "2d-plane-plot.png")
    14  }
    15  
    16  func genData2D(n0, n1 int, f func(x, ps []float64) float64, ps []float64, x0min, x0max, x1min, x1max float64) ([][]float64, []float64) {
    17  	var (
    18  		xdata  = make([][]float64, n0*n1)
    19  		ydata  = make([]float64, n0*n1)
    20  		rnd    = rand.New(rand.NewPCG(1234, 1234))
    21  		x0step = (x0max - x0min) / float64(n0)
    22  		x1step = (x1max - x1min) / float64(n1)
    23  		p      = make([]float64, len(ps))
    24  	)
    25  	for i := range n0 {
    26  		for j := range n1 {
    27  			x := []float64{x0min + x0step*float64(i), x1min + x1step*float64(j)}
    28  			for k := range p {
    29  				v := rnd.NormFloat64()
    30  				p[k] = ps[k] + v*0.01
    31  			}
    32  			xdata[(i%n0)*n0+j] = x
    33  			ydata[(i%n0)*n0+j] = f(x, p)
    34  		}
    35  
    36  	}
    37  	return xdata, ydata
    38  }