예제 #1
0
// Interpolation in two dimensions.
func ExampleAlgorithm_cube() {
	const (
		inputs   = 2
		outputs  = 1
		minLevel = 1
		maxLevel = 10
		accuracy = 1e-4
		order    = 1
	)

	grid := equidistant.NewClosed(inputs)
	basis := polynomial.NewClosed(inputs, order)
	algorithm := New(inputs, outputs, grid, basis)
	strategy := NewStrategy(inputs, outputs, grid, minLevel, maxLevel, accuracy)

	surrogate := algorithm.Compute(func(x, y []float64) {
		if math.Abs(2.0*x[0]-1.0) < 0.45 && math.Abs(2.0*x[1]-1.0) < 0.45 {
			y[0] = 1.0
		} else {
			y[0] = 0.0
		}
	}, strategy)

	fmt.Println(surrogate)

	// Output:
	// {inputs:2 outputs:1 nodes:477}
}
예제 #2
0
func New(ni, no uint, config *config.Solution) (*Solution, error) {
	power := config.Power
	if power == 0 {
		return nil, errors.New("the interpolation power should be positive")
	}

	var agrid interface {
		hybrid.Grid
		hybrid.Guide
		grid.Parenter
	}
	var abasis hybrid.Basis
	switch config.Rule {
	case "closed":
		agrid = equidistant.NewClosed(ni)
		abasis = polynomial.NewClosed(ni, power)
	case "open":
		agrid = equidistant.NewOpen(ni)
		abasis = polynomial.NewOpen(ni, power)
	default:
		return nil, errors.New("the interpolation rule is unknown")
	}

	return &Solution{
		Algorithm: *hybrid.New(ni, no, agrid, abasis),

		config: config,
		grid:   agrid,
	}, nil
}
예제 #3
0
func TestSolutionCompute(t *testing.T) {
	config, _ := config.New("fixtures/002_020.json")
	system, _ := system.New(&config.System)
	uncertainty, _ := uncertainty.NewEpistemic(system, &config.Uncertainty)

	quantity, _ := quantity.New(system, uncertainty, &config.Quantity)
	ni, no := quantity.Dimensions()

	solution, _ := New(ni, no, &config.Solution)
	surrogate := solution.Compute(quantity, quantity)

	nn := surrogate.Surrogate.Nodes

	assert.Equal(nn, uint(841), t)

	grid := grid.NewClosed(ni)
	nodes := grid.Compute(surrogate.Surrogate.Indices)

	values := make([]float64, nn*no)
	for i := uint(0); i < nn; i++ {
		quantity.Compute(nodes[i*ni:(i+1)*ni], values[i*no:(i+1)*no])
	}

	assert.EqualWithin(values, solution.Evaluate(surrogate, nodes), 1e-15, t)
}
예제 #4
0
func (self *fixture) initialize() {
	ni := self.surrogate.Inputs

	switch self.rule {
	case "closed":
		self.grid = equidistant.NewClosed(ni)
		self.basis = polynomial.NewClosed(ni, 1)
	default:
		panic("the rule is unknown")
	}
}
예제 #5
0
func benchmarkClosedCompute(power uint, b *testing.B) {
	const (
		nd = 10
		ns = 100000
	)

	basis := NewClosed(nd, power)
	indices := generateIndices(nd, ns, equidistant.NewClosed(nd).Refine)
	points := generatePoints(nd, ns, indices, basis.grid.Node)

	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		for j := 0; j < ns; j++ {
			basis.Compute(indices[j*nd:(j+1)*nd], points[j*nd:(j+1)*nd])
		}
	}
}
예제 #6
0
func TestValidate(t *testing.T) {
	const (
		ni = 2
	)

	grid := equidistant.NewClosed(1)

	cases := []struct {
		levels []uint64
		orders []uint64
		result bool
	}{
		{
			[]uint64{
				0, 0,
				0, 1,
				1, 0,
				1, 1,
				1, 2,
			},
			[]uint64{
				0, 0,
				0, 2,
				2, 0,
				2, 2,
				2, 3,
			},
			true,
		},
		{
			[]uint64{
				0, 0,
				0, 1,
				1, 0,
				1, 1,
				1, 1,
				1, 2,
			},
			[]uint64{
				0, 0,
				0, 2,
				2, 0,
				2, 2,
				2, 2,
				2, 3,
			},
			false,
		},
		{
			[]uint64{
				0, 0,
				0, 1,
				1, 0,
				1, 1,
				1, 2,
			},
			[]uint64{
				0, 0,
				0, 2,
				2, 0,
				2, 2,
				2, 1,
			},
			false,
		},
		{
			[]uint64{
				0, 0,
				0, 1,
				1, 0,
				1, 1,
				2, 2,
			},
			[]uint64{
				0, 0,
				0, 2,
				2, 0,
				2, 2,
				3, 3,
			},
			false,
		},
	}

	for _, c := range cases {
		indices := internal.Compose(c.levels, c.orders)
		assert.Equal(Validate(indices, ni, grid), c.result, t)
	}
}