예제 #1
0
파일: ode_test.go 프로젝트: dtromb/gogsl
func TestOde(test *testing.T) {
	var mu float64 = 10
	sys := &ode.GslOdeiv2System{
		Function:  F,
		Jacobian:  Df,
		Dimension: 2,
		Params:    &mu,
	}
	ode.InitializeGslFOdeiv2System(sys)

	d := ode.DriverAllocYNew(sys, ode.ODEIV2_STEP_TYPE_RK8PD, 1e-6, 1e-6, 0.0)

	var t float64 = 0.0
	var t1 float64 = 100.0
	var y []float64 = []float64{1.0, 0.0}

	for i := 1; i <= 100; i++ {
		ti := float64(i) * t1 / 100.0
		status := ode.DriverApply(d, &t, ti, y)
		if status != int32(gogsl.GSL_SUCCESS) {
			test.Error("error, return " + gogsl.GslError(status).String())
			break
		}
		fmt.Printf("%.5e %.5e %.5e\n", t, y[0], y[1])
	}
}
예제 #2
0
func TestMultirootFdf(t *testing.T) {

	var n int = 2

	rp := &Rparams{
		A: 1.0,
		B: 10.0,
	}

	f := &multiroot.GslMultirootFunctionFdf{
		Function:   Rosenbrock,
		Derivative: RosenbrockDf,
		Fdf:        RosenbrockFdf,
		Dimension:  n,
		Params:     rp,
	}
	multiroot.InitializeGslMultirootFunctionFdf(f)

	xInit := []float64{-10.0, -5.0}
	x := vector.VectorAlloc(n)

	vector.Set(x, 0, xInit[0])
	vector.Set(x, 1, xInit[1])

	T := multiroot.GSL_MULTIROOT_FDFSOLVER_GNEWTON
	s := multiroot.FdfsolverAlloc(T, 2)
	multiroot.FdfsolverSet(s, f, x)

	var iter int

	PrintStateFdf(iter, s)

	for {
		iter += 1
		status := gogsl.GslError(multiroot.FdfsolverIterate(s))
		PrintStateFdf(iter, s)
		if status != gogsl.GSL_SUCCESS {
			break
		}
		status = gogsl.GslError(multiroot.TestResidual(s.F_(), 1e-7))
		if status != gogsl.GSL_CONTINUE || iter >= 1000 {
			break
		}
		fmt.Printf("status = %s\n", status.String())
	}
}
예제 #3
0
func TestMultimin(t *testing.T) {

	par := []float64{1.0, 2.0, 10.0, 20.0, 30.0}
	f := &multimin.GslMultiminFunction{
		Function:  MyF,
		Dimension: 2,
		Params:    par,
	}
	multimin.InitializeGslMultiminFunction(f)

	/* Starting point */
	x := vector.VectorAlloc(2)
	vector.Set(x, 0, 5.0)
	vector.Set(x, 1, 7.0)

	/* Set initial step sizes to 1 */
	ss := vector.VectorAlloc(2)
	vector.SetAll(ss, 1.0)

	T := multimin.GSL_MULTIMIN_FMINIMIZER_NSIMPLEX2
	s := multimin.FminimizerAlloc(T, 2)
	multimin.FminimizerSet(s, f, x, ss)

	iter := 0

	for {
		iter += 1
		status := gogsl.GslError(multimin.FminimizerIterate(s))
		if status != gogsl.GSL_SUCCESS {
			break
		}
		size := multimin.FminimizerSize(s)
		status = gogsl.GslError(multimin.TestSize(size, 1e-2))
		if status == gogsl.GSL_SUCCESS {
			fmt.Printf("converged to minimum at\n")
		}
		fmt.Printf("%5d %10.3e %10.3e f() = %7.3f size = %.3f\n",
			iter, vector.Get(s.X_(), 0), vector.Get(s.X_(), 1), s.Fval(), size)
		if status != gogsl.GSL_CONTINUE || iter >= 100 {
			break
		}
	}
}
예제 #4
0
func TestMultiminFdf(t *testing.T) {

	par := []float64{1.0, 2.0, 10.0, 20.0, 30.0}
	f := &multimin.GslMultiminFunctionFdf{
		Function:   MyF,
		Derivative: MyDf,
		Fdf:        MyFdf,
		Dimension:  2,
		Params:     par,
	}
	multimin.InitializeGslMultiminFunctionFdf(f)

	/* Starting point */
	x := vector.VectorAlloc(2)
	vector.Set(x, 0, 5.0)
	vector.Set(x, 1, 7.0)

	T := multimin.GSL_MULTIMIN_FDFMINIMIZER_CONJUGATE_FR
	s := multimin.FdfminimizerAlloc(T, 2)
	multimin.FdfminimizerSet(s, f, x, 0.01, 1e-4)

	iter := 0

	for {
		iter += 1
		status := gogsl.GslError(multimin.FdfminimizerIterate(s))
		if status != gogsl.GSL_SUCCESS {
			break
		}
		status = gogsl.GslError(multimin.TestGradient(s.Gradient_(), 1e-3))
		if status == gogsl.GSL_SUCCESS {
			fmt.Printf("Minimum found at:\n")
		}
		fmt.Printf("%5d %10.3e %10.3e f() = %7.3f\n",
			iter, vector.Get(s.X_(), 0), vector.Get(s.X_(), 1), s.Fval())
		if status != gogsl.GSL_CONTINUE || iter >= 100 {
			break
		}
	}
}
예제 #5
0
파일: sf_test.go 프로젝트: postfix/gsl-1
func TestSf(t *testing.T) {
	var x float64 = 5.0
	var result sf.GslSfResult
	expected := -0.17759677131433830434739701
	status := bessel.J0E(x, &result)
	fmt.Printf("status  = %s\n", gogsl.GslError(status).String())
	fmt.Printf("J0(5.0) = %.18f\n"+
		"      +/- % .18f\n", result.Val(), result.Err())
	fmt.Printf("exact   = %.18f\n", expected)
	if math.Abs(result.Val()-expected) > result.Err() {
		t.Error("incorrect value")
	}
}
예제 #6
0
func TestMultiset(t *testing.T) {
	fmt.Println("All multisets of {0,1,2,3} by size:\n")
	for i := 0; i <= 4; i++ {
		c := multiset.MultisetCalloc(4, i)
		for {
			fmt.Printf("{")
			multiset.Fprintf(os.Stdout, c, " %u")
			fmt.Printf(" }\n")
			r := gogsl.GslError(multiset.Next(c))
			if r != gogsl.GSL_SUCCESS {
				break
			}
		}
	}
}
예제 #7
0
func TestNumint(t *testing.T) {
	gogsl.SetErrorHandler(&gogsl.GslErrorHandler{
		Handler: func(reason string, file string, line int, gslError gogsl.GslError) {
			fmt.Println(reason + " : " + gslError.String())
		},
	})
	expected := -4.0
	alpha := 1.0
	F := gogsl.GslFunction{Function: F, Params: alpha}
	status, result, err := numint.Qags(&F, 0, 1, 0, 0.0001, 1000, w)
	fmt.Printf("status          =  %s\n", gogsl.GslError(status).String())
	fmt.Printf("result          = % .18f\n", result)
	fmt.Printf("exact result    = % .18f\n", expected)
	fmt.Printf("estimated error = % .18f\n", err)
	fmt.Printf("actual error    = % .18f\n", result-expected)
	fmt.Printf("intervals =  %d\n", w.IntervalCount())
}
예제 #8
0
파일: min_test.go 프로젝트: dtromb/gogsl
func TestMin(t *testing.T) {
	var maxIter int = 100
	var m float64 = 2.0
	var mExpected float64 = math.Pi
	var a float64 = 0.0
	var b float64 = 6.0

	var iter int

	f := &gogsl.GslFunction{
		Function: Fn1,
	}
	gogsl.InitializeGslFunction(f)

	T := min.GSL_MIN_FMINIMIZER_BRENT
	s := min.FminimizerAlloc(T)
	min.FminimizerSet(s, f, m, a, b)

	fmt.Printf("using %s method\n", min.FminimizerName(s))
	fmt.Printf("%5s [%9s, %9s] %9s %10s %9s\n", "iter", "lower", "upper", "min", "err", "err(est)")
	fmt.Printf("%5d [%.7f, %.7f] %.7f %+.7f %.7f\n", iter, a, b, m, m-mExpected, b-a)

	for {

		iter += 1
		min.FminimizerIterate(s)

		m = min.FminimizerXMinimum(s)
		a = min.FminimizerXLower(s)
		b = min.FminimizerXUpper(s)

		status := gogsl.GslError(min.TestInterval(a, b, 0.001, 0.0))
		if status == gogsl.GSL_SUCCESS {
			fmt.Printf("Converged:\n")
		}

		fmt.Printf("%5d [%.7f, %.7f] %.7f %+.7f %.7f\n", iter, a, b, m, m-mExpected, b-a)

		if status != gogsl.GSL_CONTINUE || iter >= maxIter {
			break
		}
	}
}
예제 #9
0
파일: root_test.go 프로젝트: dtromb/gogsl
func TestRootFdf(t *testing.T) {

	var maxIter int = 100
	var x float64 = 5.0
	var rExpected float64 = math.Sqrt(5.0)

	qp := &QuadraticParams{
		A: 1.0,
		B: 0.0,
		C: -5.0,
	}

	f := &gogsl.GslFunctionFdf{
		Function:   Quadratic,
		Derivative: QuadraticDeriv,
		Fdf:        QuadraticFdf,
		Params:     qp,
	}
	gogsl.InitializeGslFunctionFdf(f)

	T := root.GSL_ROOT_FDFSOLVER_NEWTON
	s := root.FdfsolverAlloc(T)
	root.FdfsolverSet(s, f, x)

	fmt.Printf("using %s method\n", root.FdfsolverName(s))
	fmt.Printf("%-5s %10s %10s %10s\n", "iter", "root", "err", "err(est)")

	iter := 0
	for {
		iter += 1
		root.FdfsolverIterate(s)
		x0 := x
		x = root.FdfsolverRoot(s)
		status := gogsl.GslError(root.TestDelta(x, x0, 0, 1e-3))
		if status == gogsl.GSL_SUCCESS {
			fmt.Printf("Converged:\n")
		}
		fmt.Printf("%5d %10.7f %+10.7f %10.7f\n", iter, x, x-rExpected, x-x0)
		if status != gogsl.GSL_CONTINUE || iter >= maxIter {
			break
		}
	}
}
예제 #10
0
파일: root_test.go 프로젝트: dtromb/gogsl
func TestRoot(t *testing.T) {
	var maxIter int = 100
	var rExpected float64 = math.Sqrt(5.0)
	var xLo float64 = 0.0
	var xHi float64 = 5.0

	qp := &QuadraticParams{
		A: 1.0,
		B: 0.0,
		C: -5.0,
	}

	f := &gogsl.GslFunction{
		Function: Quadratic,
		Params:   qp,
	}
	gogsl.InitializeGslFunction(f)

	T := root.GSL_ROOT_FSOLVER_BRENT
	s := root.FsolverAlloc(T)
	root.FsolverSet(s, f, xLo, xHi)
	fmt.Printf("using %s method\n", root.FsolverName(s))
	fmt.Printf("%5s [%9s, %9s] %9s %10s %9s\n",
		"iter", "lower", "upper", "root",
		"err", "err(est)")
	iter := 0
	for {
		iter += 1
		root.FsolverIterate(s)
		r := root.FsolverRoot(s)
		xLo = root.FsolverXLower(s)
		xHi = root.FsolverXUpper(s)
		status := gogsl.GslError(root.TestInterval(xLo, xHi, 0, 0.001))
		if status == gogsl.GSL_SUCCESS {
			fmt.Printf("Converged:\n")
		}
		fmt.Printf("%5d [%.7f, %.7f] %.7f %+.7f %.7f\n",
			iter, xLo, xHi, r, r-rExpected, xHi-xLo)
		if status != gogsl.GSL_CONTINUE || iter >= maxIter {
			break
		}
	}
}
예제 #11
0
func TestCombination(t *testing.T) {
	maxiter := 25
	var iter int
	fmt.Printf("All subsets of {0,1,2,3} by size:\n")
	for i := 0; i <= 4; i++ {
		c := combination.CombinationCalloc(4, i)
		for {
			fmt.Printf("{")
			combination.Fprintf(os.Stdout, c, " %u")
			fmt.Printf(" }\n")
			r := gogsl.GslError(combination.Next(c))
			if r != gogsl.GSL_SUCCESS {
				break
			}
			if iter > maxiter {
				t.Error("iteration limit reached")
				break
			}
			iter += 1
		}
	}
}