Esempio n. 1
0
func Test_prms02(tst *testing.T) {

	//verbose()
	chk.PrintTitle("prms02")

	var prms Prms
	prms = []*Prm{
		&Prm{N: "klx", V: 1.0},
		&Prm{N: "kly", V: 2.0},
		&Prm{N: "klz", V: 3.0},
	}
	io.Pforan("%v\n", prms)

	var klx, kly, klz float64
	err_msg := prms.ConnectSet([]*float64{&klx, &kly, &klz}, []string{"klx", "kly", "klz"}, "Test_prms02")
	if err_msg != "" {
		tst.Error("connect set failed: %v\n", err_msg)
		return
	}

	chk.Scalar(tst, "klx", 1e-15, klx, 1.0)
	chk.Scalar(tst, "kly", 1e-15, kly, 2.0)
	chk.Scalar(tst, "klz", 1e-15, klz, 3.0)

	prms[1].Set(2.2)
	chk.Scalar(tst, "kly", 1e-15, kly, 2.2)
}
Esempio n. 2
0
func Test_linsol02(tst *testing.T) {

	//verbose()
	chk.PrintTitle("linsol02. real")

	// input matrix data into Triplet
	var t Triplet
	t.Init(10, 10, 64)
	for i := 0; i < 10; i++ {
		j := i
		if i > 0 {
			j = i - 1
		}
		for ; j < 10; j++ {
			val := 10.0 - float64(j)
			if i > j {
				val -= 1.0
			}
			t.Put(i, j, val)
		}
	}

	// run test
	b := []float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}
	x_correct := []float64{-1, 8, -65, 454, -2725, 13624, -54497, 163490, -326981, 326991}
	tol := 1e-9 // TODO: check why tests fails with 1e-10 @ office but not @ home
	run_linsol_testR(tst, &t, 1e-4, tol, b, x_correct)
}
Esempio n. 3
0
func Test_linsol04(tst *testing.T) {

	//verbose()
	chk.PrintTitle("linsol04. complex (but real)")

	// input matrix data into Triplet
	var t TripletC
	t.Init(10, 10, 64, false)
	for i := 0; i < 10; i++ {
		j := i
		if i > 0 {
			j = i - 1
		}
		for ; j < 10; j++ {
			val := 10.0 - float64(j)
			if i > j {
				val -= 1.0
			}
			t.Put(i, j, val, 0)
		}
	}

	// run test
	b := []complex128{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}
	x_correct := []complex128{-1, 8, -65, 454, -2725, 13624, -54497, 163490, -326981, 326991}
	run_linsol_testC(tst, &t, 1e-4, 1e-9, b, x_correct)
}
Esempio n. 4
0
func Test_groups01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("groups01")

	Init(0)

	ng := 3            // number of groups
	nints := 12        // number of integers
	size := nints / ng // groups size
	ints := utl.IntRange(nints)
	groups := utl.IntsAlloc(ng, size)
	hists := make([]*IntHistogram, ng)
	for i := 0; i < ng; i++ {
		hists[i] = &IntHistogram{Stations: utl.IntRange(nints + 1)}
	}
	IntGetGroups(groups, ints)
	io.Pfcyan("groups = %v\n", groups)
	for i := 0; i < NSAMPLES; i++ {
		IntGetGroups(groups, ints)
		for j := 0; j < ng; j++ {
			check_repeated(groups[j])
			hists[j].Count(groups[j], false)
		}
	}
	for i := 0; i < ng; i++ {
		io.Pf("\n")
		io.Pf(TextHist(hists[i].GenLabels("%d"), hists[i].Counts, 60))
	}
}
Esempio n. 5
0
func Test_GOflt01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("GOflt01. float64")

	Init(1234)

	xmin := 10.0
	xmax := 20.0
	vals := make([]float64, NSAMPLES)

	// using Float64
	t0 := time.Now()
	for i := 0; i < NSAMPLES; i++ {
		vals[i] = Float64(xmin, xmax)
	}
	io.Pforan("time elapsed = %v\n", time.Now().Sub(t0))

	hist := Histogram{Stations: []float64{10, 12.5, 15, 17.5, 20}}
	hist.Count(vals, true)
	io.Pfpink(TextHist(hist.GenLabels("%4g"), hist.Counts, 60))

	// using Float64s
	t0 = time.Now()
	Float64s(vals, xmin, xmax)
	io.Pforan("time elapsed = %v\n", time.Now().Sub(t0))

	hist.Count(vals, true)
	io.Pfblue2(TextHist(hist.GenLabels("%4g"), hist.Counts, 60))
}
Esempio n. 6
0
func Test_mtdeb01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("mtdeb01. Deb's mutation")

	var ops OpsData
	ops.SetDefault()
	ops.Pm = 1.0
	ops.Xrange = [][]float64{{-3, 3}, {-4, 4}}
	ops.EnfRange = true

	rnd.Init(0)

	A := []float64{-1, 1}
	io.Pforan("before: A = %v\n", A)
	FltMutationDeb(A, 10, &ops)
	io.Pforan("after:  A = %v\n", A)

	ha0 := rnd.Histogram{Stations: utl.LinSpace(-3, 3, 11)}

	nsamples := 1000
	aa := make([]float64, len(A))
	a0s := make([]float64, nsamples)
	for _, t := range []int{0, 50, 100} {
		for i := 0; i < nsamples; i++ {
			copy(aa, A)
			FltMutationDeb(aa, t, &ops)
			a0s[i] = aa[0]
		}
		ha0.Count(a0s, true)
		io.Pf("\ntime = %d\n", t)
		io.Pf("%s", rnd.TextHist(ha0.GenLabels("%.1f"), ha0.Counts, 60))
	}
}
Esempio n. 7
0
func TestMyLab04(tst *testing.T) {

	//verbose()
	chk.PrintTitle("mylab04")

	n := 5
	a := LinSpace(2.0, 3.0, n)
	δ := (3.0 - 2.0) / float64(n-1)
	r := make([]float64, n)
	for i := 0; i < n; i++ {
		r[i] = 2.0 + float64(i)*δ
	}
	io.Pf("δ = %v\n", δ)
	io.Pf("a = %v\n", a)
	io.Pf("r = %v\n", r)
	chk.Vector(tst, "linspace(2,3,5)", 1e-17, a, []float64{2.0, 2.25, 2.5, 2.75, 3.0})

	b := LinSpaceOpen(2.0, 3.0, n)
	Δ := (3.0 - 2.0) / float64(n)
	R := make([]float64, n)
	for i := 0; i < n; i++ {
		R[i] = 2.0 + float64(i)*Δ
	}
	io.Pf("Δ = %v\n", Δ)
	io.Pf("b = %v\n", b)
	io.Pf("R = %v\n", R)
	chk.Vector(tst, "linspace(2,3,5,open)", 1e-17, b, []float64{2.0, 2.2, 2.4, 2.6, 2.8})

	c := LinSpace(2.0, 3.0, 1)
	io.Pf("c = %v\n", c)
	chk.Vector(tst, "linspace(2,3,1)", 1e-17, c, []float64{2.0})

}
Esempio n. 8
0
func Test_draw01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("draw01")

	P := [][]float64{
		{-2.5, 0.0},
		{-5.5, 4.0},
		{0.0, 10.0},
		{5.5, 4.0},
		{2.5, 0.0},
	}

	var sd Sty
	sd.Init()
	sd.Closed = true
	DrawPolyline(P, &sd, "")
	AutoScale(P)
	Equal()
	DrawLegend([]Fmt{
		Fmt{"red", "o", "-", 1, -1, "first", -1},
		Fmt{"green", "s", "-", 2, 0, "second", -1},
		Fmt{"blue", "+", "-", 3, 10, "third", -1},
	}, 10, "best", false, "")
	if chk.Verbose {
		SaveD("/tmp/gosl", "draw01.eps")
	}
}
Esempio n. 9
0
func Test_up01b(tst *testing.T) {

	// capture errors and flush log
	defer End()

	//verbose()
	chk.PrintTitle("up01b")

	// start simulation
	if !Start("data/up01.sim", true, chk.Verbose) {
		chk.Panic("cannot start simulation")
	}

	// for debugging Kb
	if true {
		defer up_DebugKb(&testKb{
			tst: tst, eid: 3, tol: 1e-8, verb: chk.Verbose,
			ni: 1, nj: 1, itmin: 1, itmax: -1, tmin: 800, tmax: 1000,
		})()
	}

	// run simulation
	if !Run() {
		chk.Panic("cannot run simulation\n")
	}
}
Esempio n. 10
0
func Test_mylab08(tst *testing.T) {

	//verbose()
	chk.PrintTitle("mylab08. dot and cross products")

	u := []float64{3, -3, 1}
	v := []float64{4, 9, 2}
	w := make([]float64, 3)
	s := Dot3d(u, v)
	Cross3d(w, u, v) // w := u cross v
	chk.Scalar(tst, "s = u dot v", 1e-17, s, -13)
	chk.Vector(tst, "w = u cross v", 1e-17, w, []float64{-15, -2, 39})

	u = []float64{3, -3, 1}
	v = []float64{-12, 12, -4}
	s = Dot3d(u, v)
	Cross3d(w, u, v) // w := u cross v
	chk.Scalar(tst, "s = u dot v", 1e-17, s, -76)
	chk.Vector(tst, "w = u cross v", 1e-17, w, []float64{0, 0, 0})

	u = []float64{3, 2, -2}
	v = []float64{1, 0, -5}
	s = Dot3d(u, v)
	Cross3d(w, u, v) // w := u cross v
	chk.Scalar(tst, "s = u dot v", 1e-17, s, 13)
	chk.Vector(tst, "w = u cross v", 1e-17, w, []float64{-10, 13, -2})
}
Esempio n. 11
0
func Test_mylab03a(tst *testing.T) {

	//verbose()
	chk.PrintTitle("mylab03a. ints: min and max. dbls: min and max")

	A := []int{1, 2, 3, -1, -2, 0, 8, -3}
	mi, ma := IntMinMax(A)
	io.Pf("A      = %v\n", A)
	io.Pf("min(A) = %v\n", mi)
	io.Pf("max(A) = %v\n", ma)
	if mi != -3 {
		chk.Panic("min(A) failed")
	}
	if ma != 8 {
		chk.Panic("max(A) failed")
	}

	if Imin(-1, 2) != -1 {
		chk.Panic("Imin(-1,2) failed")
	}
	if Imax(-1, 2) != 2 {
		chk.Panic("Imax(-1,2) failed")
	}
	if Min(-1, 2) != -1.0 {
		chk.Panic("Min(-1,2) failed")
	}
	if Max(-1, 2) != 2.0 {
		chk.Panic("Max(-1,2) failed")
	}
}
Esempio n. 12
0
func Test_mylab07(tst *testing.T) {

	//verbose()
	chk.PrintTitle("mylab07. get column")

	v := [][]float64{
		{1, 2, 3, 4},
		{-1, 2, 3, 0},
		{-1, 2, 1, 4},
		{1, -2, 3, 8},
		{1, 1, -3, 4},
		{0, 2, 9, -4},
	}

	x := DblsGetColumn(0, v)
	chk.Vector(tst, "v[:,0]", 1e-17, x, []float64{1, -1, -1, 1, 1, 0})

	x = DblsGetColumn(1, v)
	chk.Vector(tst, "v[:,1]", 1e-17, x, []float64{2, 2, 2, -2, 1, 2})

	x = DblsGetColumn(2, v)
	chk.Vector(tst, "v[:,2]", 1e-17, x, []float64{3, 3, 1, 3, -3, 9})

	x = DblsGetColumn(3, v)
	chk.Vector(tst, "v[:,3]", 1e-17, x, []float64{4, 0, 4, 8, 4, -4})
}
Esempio n. 13
0
func Test_state01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("state01")

	nsig, nalp, large, nle := 4, 1, false, true
	state0 := NewState(nsig, nalp, large, nle)
	io.Pforan("state0 = %+v\n", state0)
	chk.Vector(tst, "sig", 1.0e-17, state0.Sig, []float64{0, 0, 0, 0})
	chk.Vector(tst, "alp", 1.0e-17, state0.Alp, []float64{0})
	chk.Vector(tst, "epsE", 1.0e-17, state0.EpsE, []float64{0, 0, 0, 0})

	state0.Sig[0] = 10.0
	state0.Sig[1] = 11.0
	state0.Sig[2] = 12.0
	state0.Sig[3] = 13.0
	state0.Alp[0] = 20.0

	state1 := NewState(nsig, nalp, large, nle)
	state1.Set(state0)
	io.Pforan("state1 = %+v\n", state1)
	chk.Vector(tst, "sig", 1.0e-17, state1.Sig, []float64{10, 11, 12, 13})
	chk.Vector(tst, "alp", 1.0e-17, state1.Alp, []float64{20})

	state2 := state1.GetCopy()
	io.Pforan("state2 = %+v\n", state2)
	chk.Vector(tst, "sig", 1.0e-17, state2.Sig, []float64{10, 11, 12, 13})
	chk.Vector(tst, "alp", 1.0e-17, state2.Alp, []float64{20})
	chk.Vector(tst, "epsE", 1.0e-17, state2.EpsE, []float64{0, 0, 0, 0})
}
Esempio n. 14
0
func Test_munkres04(tst *testing.T) {

	//verbose()
	chk.PrintTitle("munkres04. row and column matrices")

	C := [][]float64{
		{1.0},
		{2.0},
		{0.5},
		{3.0},
		{4.0},
	}

	var mnk Munkres
	mnk.Init(len(C), len(C[0]))
	mnk.SetCostMatrix(C)
	mnk.Run()
	io.Pforan("links = %v\n", mnk.Links)
	io.Pforan("cost = %v  (13938)\n", mnk.Cost)
	chk.Ints(tst, "links", mnk.Links, []int{-1, -1, 0, -1, -1})
	chk.Scalar(tst, "cost", 1e-17, mnk.Cost, 0.5)

	C = [][]float64{
		{1.0, 2.0, 0.5, 3.0, 4.0},
	}
	mnk.Init(len(C), len(C[0]))
	mnk.SetCostMatrix(C)
	mnk.Run()
	io.Pforan("links = %v\n", mnk.Links)
	io.Pforan("cost = %v  (13938)\n", mnk.Cost)
	chk.Ints(tst, "links", mnk.Links, []int{2})
	chk.Scalar(tst, "cost", 1e-17, mnk.Cost, 0.5)
}
Esempio n. 15
0
func Test_pipe01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("runcmd01")

	Pforan("running pipe\n")

	// find $DIR -type f # Find all files
	dir := "."
	find := exec.Command("find", dir, "-type", "f")

	// | grep -v '/[._]' # Ignore hidden/temporary files
	egrep := exec.Command("egrep", "-v", `/[._]`)

	// | sort -t. -k2 # Sort by file extension
	sort := exec.Command("sort", "-t.", "-k2")

	output, stderr, err := Pipeline(find, egrep, sort)
	Pfblue2("\noutput:\n%v\n", string(output))
	Pfcyan("stderr:\n%v\n", string(stderr))
	if err != nil {
		tst.Errorf("error: %v\n", err)
		return
	}
}
Esempio n. 16
0
func Test_bins03(tst *testing.T) {

	//verbose()
	chk.PrintTitle("bins03. find along line (3D)")

	// bins
	var bins Bins
	bins.Init([]float64{0, 0, 0}, []float64{10, 10, 10}, 10)

	// fill bins structure
	maxit := 10 // number of entries
	ID := make([]int, maxit)
	var err error
	for k := 0; k < maxit; k++ {
		x := float64(k) / float64(maxit) * 10
		ID[k] = k * 11
		err = bins.Append([]float64{x, x, x}, ID[k])
		if err != nil {
			chk.Panic(err.Error())
		}
	}

	// find points along diagonal
	ids := bins.FindAlongSegment([]float64{0, 0, 0}, []float64{10, 10, 10}, 0.0000001)
	io.Pforan("ids = %v\n", ids)
	chk.Ints(tst, "ids", ID, ids)
}
Esempio n. 17
0
func Test_spo751b(tst *testing.T) {

	//verbose()
	chk.PrintTitle("spo751b")

	// run simulation
	analysis := NewFEM("data/spo751.sim", "", true, true, false, false, chk.Verbose, 0)

	// for debugging Kb
	if true {
		u_DebugKb(analysis, &testKb{
			tst: tst, eid: 3, tol: 1e-5, verb: chk.Verbose,
			ni: 1, nj: 1, itmin: 1, itmax: -1, tmin: 0.89, tmax: 0.96,
		})
	}

	// run simulation
	err := analysis.Run()
	if err != nil {
		tst.Errorf("Run failed:\n%v", err)
		return
	}

	// check
	if true {
		verb := false
		skipK := true
		tolK := 1e-17
		tolu := 1e-13
		tols := 1e-14
		TestingCompareResultsU(tst, "data/spo751.sim", "cmp/spo751.cmp", "", tolK, tolu, tols, skipK, verb)
	}
}
Esempio n. 18
0
func Test_deep01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("deep01")

	a := Deep3alloc(3, 2, 4)
	for i := 0; i < 3; i++ {
		for j := 0; j < 2; j++ {
			for k := 0; k < 4; k++ {
				if math.Abs(a[i][j][k]) > 1e-17 {
					tst.Errorf("a[i][j][k] failed")
				}
			}
		}
	}
	io.Pf("a = %v\n", a)

	b := Deep4alloc(3, 2, 1, 2)
	for i := 0; i < 3; i++ {
		for j := 0; j < 2; j++ {
			for k := 0; k < 1; k++ {
				for l := 0; l < 2; l++ {
					if math.Abs(b[i][j][k][l]) > 1e-17 {
						tst.Errorf("b[i][j][k][l] failed")
					}
				}
			}
		}
	}
	io.Pf("b = %v\n", b)
}
Esempio n. 19
0
func TestMyLab03b(tst *testing.T) {

	//verbose()
	chk.PrintTitle("mylab03b")

	a := []int{1, 2, 3, -1, -2, 0, 8, -3}
	b := IntFilter(a, func(i int) bool {
		if a[i] < 0 {
			return true
		}
		return false
	})
	c := IntNegOut(a)
	io.Pf("a = %v\n", a)
	io.Pf("b = %v\n", b)
	io.Pf("c = %v\n", c)
	chk.Ints(tst, "b", b, []int{1, 2, 3, 0, 8})
	chk.Ints(tst, "c", c, []int{1, 2, 3, 0, 8})

	A := []float64{1, 2, 3, -1, -2, 0, 8, -3}
	s := DblSum(A)
	mi, ma := DblMinMax(A)
	io.Pf("A      = %v\n", A)
	io.Pf("sum(A) = %v\n", s)
	io.Pf("min(A) = %v\n", mi)
	io.Pf("max(A) = %v\n", ma)
	chk.Scalar(tst, "sum(A)", 1e-17, s, 8)
	chk.Scalar(tst, "min(A)", 1e-17, mi, -3)
	chk.Scalar(tst, "max(A)", 1e-17, ma, 8)
}
Esempio n. 20
0
func TestSPDsolve02(tst *testing.T) {

	//verbose()
	chk.PrintTitle("TestSPDsolve 02")

	a := [][]float64{
		{2, 1, 1, 3, 2},
		{1, 2, 2, 1, 1},
		{1, 2, 9, 1, 5},
		{3, 1, 1, 7, 1},
		{2, 1, 5, 1, 8},
	}
	b := []float64{-2, 4, 3, -5, 1}
	B := []float64{24, 29, 110, 12, 102}
	x := make([]float64, 5)
	X := make([]float64, 5)
	SPDsolve2(x, X, a, b, B)
	CheckResidR(tst, 1e-14, a, x, b)
	CheckResidR(tst, 1e-14, a, X, B)
	chk.Vector(tst, "x = inv(a) * b", 1e-13, x, []float64{
		-629.0 / 98.0,
		237.0 / 49.0,
		-53.0 / 49.0,
		62.0 / 49.0,
		23.0 / 14.0,
	})
	chk.Vector(tst, "X = inv(a) * B", 1e-13, X, []float64{0, 4, 7, -1, 8})
}
Esempio n. 21
0
func Test_mylab02(tst *testing.T) {

	//verbose()
	chk.PrintTitle("mylab02")

	a := []string{"66", "644", "666", "653", "10", "0", "1", "1", "1"}
	idx := StrIndexSmall(a, "666")
	io.Pf("a = %v\n", a)
	io.Pf("idx of '666' = %v\n", idx)
	if idx != 2 {
		tst.Errorf("idx is wrong")
	}
	idx = StrIndexSmall(a, "1")
	io.Pf("idx of '1'   = %v\n", idx)
	if idx != 6 {
		tst.Errorf("idx is wrong")
	}

	b := []int{66, 644, 666, 653, 10, 0, 1, 1, 1}
	idx = IntIndexSmall(b, 666)
	io.Pf("b = %v\n", b)
	io.Pf("idx of 666 = %v\n", idx)
	if idx != 2 {
		tst.Errorf("idx is wrong")
	}
	idx = IntIndexSmall(b, 1)
	io.Pf("idx of 1   = %v\n", idx)
	if idx != 6 {
		tst.Errorf("idx is wrong")
	}
}
Esempio n. 22
0
func Test_Ts(tst *testing.T) {

	//verbose()
	chk.PrintTitle("Ts")

	nd := test_nd
	for m := 0; m < len(test_nd)-3; m++ {
		//for m := 0; m < 3; m++ {
		A := test_AA[m]
		a := M_Alloc2(nd[m])
		Ten2Man(a, A)
		s := M_Dev(a)
		Ts := M_Alloc4(nd[m])
		M_Ts(Ts, s)
		s2 := M_Alloc2(nd[m])
		ds2ds := M_Alloc4(nd[m])
		M_Sq(s2, s)
		M_SqDeriv(ds2ds, s)
		Ts_ := M_Alloc4(nd[m])
		for i := 0; i < len(a); i++ {
			for j := 0; j < len(a); j++ {
				for k := 0; k < len(a); k++ {
					for l := 0; l < len(a); l++ {
						Ts_[i][j] += Psd[i][k] * ds2ds[k][l] * Psd[l][j]
					}
				}
			}
		}
		chk.Matrix(tst, "Ts", 1e-13, Ts, Ts_)
	}
}
Esempio n. 23
0
func Test_MTint01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("MTint01. integers (Mersenne Twister)")

	Init(1234)

	nints := 10
	vals := make([]int, NSAMPLES)

	// using MTint
	t0 := time.Now()
	for i := 0; i < NSAMPLES; i++ {
		vals[i] = MTint(0, nints-1)
	}
	io.Pforan("time elapsed = %v\n", time.Now().Sub(t0))

	hist := IntHistogram{Stations: utl.IntRange(nints + 1)}
	hist.Count(vals, true)
	io.Pfyel(TextHist(hist.GenLabels("%d"), hist.Counts, 60))

	// using MTints
	t0 = time.Now()
	MTints(vals, 0, nints-1)
	io.Pforan("time elapsed = %v\n", time.Now().Sub(t0))

	hist.Count(vals, true)
	io.Pfcyan(TextHist(hist.GenLabels("%d"), hist.Counts, 60))
}
Esempio n. 24
0
func Test_brent02(tst *testing.T) {

	//verbose()
	chk.PrintTitle("brent02. root finding")

	ffcnA := func(x float64) (res float64, err error) {
		return x*x*x - 2.0*x - 5.0, nil
	}

	ffcnB := func(fx, x []float64) (err error) {
		fx[0], err = ffcnA(x[0])
		return
	}

	JfcnB := func(dfdx [][]float64, x []float64) (err error) {
		dfdx[0][0] = 3.0*x[0]*x[0] - 2.0
		return
	}

	xa, xb := 2.0, 3.0
	xguess := 2.1
	//save   := true
	save := false
	xbrent := run_rootsol_test(tst, xa, xb, xguess, 1e-7, ffcnA, ffcnB, JfcnB, "brent02.png", save, false)
	chk.Scalar(tst, "xsol", 1e-14, xbrent, 2.09455148154233)
}
Esempio n. 25
0
func Test_linsol01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("linsol01. real")

	// input matrix data into Triplet
	var t Triplet
	t.Init(5, 5, 13)
	t.Put(0, 0, 1.0)
	t.Put(0, 0, 1.0)
	t.Put(1, 0, 3.0)
	t.Put(0, 1, 3.0)
	t.Put(2, 1, -1.0)
	t.Put(4, 1, 4.0)
	t.Put(1, 2, 4.0)
	t.Put(2, 2, -3.0)
	t.Put(3, 2, 1.0)
	t.Put(4, 2, 2.0)
	t.Put(2, 3, 2.0)
	t.Put(1, 4, 6.0)
	t.Put(4, 4, 1.0)

	// run test
	b := []float64{8.0, 45.0, -3.0, 3.0, 19.0}
	x_correct := []float64{1, 2, 3, 4, 5}
	run_linsol_testR(tst, &t, 1e-14, 1e-13, b, x_correct)
}
Esempio n. 26
0
func Test_brent03(tst *testing.T) {

	//verbose()
	chk.PrintTitle("brent03. minimum finding")

	ffcn := func(x float64) (res float64, err error) {
		return x*x*x - 2.0*x - 5.0, nil
	}

	var o Brent
	o.Init(ffcn)
	xa, xb := 0.0, 1.0
	x, err := o.Min(xa, xb, false)
	if err != nil {
		chk.Panic("%v", err)
	}
	y, err := ffcn(x)
	if err != nil {
		chk.Panic("%v", err)
	}
	xcor := math.Sqrt(2.0 / 3.0)
	io.Pforan("x      = %v (correct=%g)\n", x, xcor)
	io.Pforan("f(x)   = %v\n", y)
	io.Pforan("nfeval = %v\n", o.NFeval)
	io.Pforan("nit    = %v\n", o.It)

	//save := true
	save := false
	PlotYxe(ffcn, "results", "brent03.png", x, -1, 3, 101, "Brent", "'b-'", save, false, nil)
	chk.Scalar(tst, "xcorrect", 1e-8, x, xcor)
}
Esempio n. 27
0
func Test_linsol03(tst *testing.T) {

	//verbose()
	chk.PrintTitle("linsol03. complex (but real)")

	// input matrix data into Triplet
	var t TripletC
	t.Init(5, 5, 13, false)
	t.Put(0, 0, 1.0, 0)
	t.Put(0, 0, 1.0, 0)
	t.Put(1, 0, 3.0, 0)
	t.Put(0, 1, 3.0, 0)
	t.Put(2, 1, -1.0, 0)
	t.Put(4, 1, 4.0, 0)
	t.Put(1, 2, 4.0, 0)
	t.Put(2, 2, -3.0, 0)
	t.Put(3, 2, 1.0, 0)
	t.Put(4, 2, 2.0, 0)
	t.Put(2, 3, 2.0, 0)
	t.Put(1, 4, 6.0, 0)
	t.Put(4, 4, 1.0, 0)

	// run test
	b := []complex128{8.0, 45.0, -3.0, 3.0, 19.0}
	x_correct := []complex128{1, 2, 3, 4, 5}
	run_linsol_testC(tst, &t, 1e-14, 1e-13, b, x_correct)
}
Esempio n. 28
0
func Test_brent01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("brent01. root finding")

	ffcnA := func(x float64) (res float64, err error) {
		res = math.Pow(x, 3.0) - 0.165*math.Pow(x, 2.0) + 3.993e-4
		return
	}

	ffcnB := func(fx, x []float64) (err error) {
		fx[0], err = ffcnA(x[0])
		return
	}

	JfcnB := func(dfdx [][]float64, x []float64) (err error) {
		dfdx[0][0] = 3.0*x[0]*x[0] - 2.0*0.165*x[0]
		return
	}

	xa, xb := 0.0, 0.11
	//xguess := 0.001 // ===> this one converges to the right-hand solution
	xguess := 0.03
	//save   := true
	save := false
	run_rootsol_test(tst, xa, xb, xguess, 1e-7, ffcnA, ffcnB, JfcnB, "brent01.png", save, false)
}
Esempio n. 29
0
func Test_linsol05(tst *testing.T) {

	//verbose()
	chk.PrintTitle("linsol05. complex (but real)")

	// data
	n := 10
	b := make([]complex128, n)
	x_correct := make([]complex128, n)

	// input matrix data into Triplet
	var t TripletC
	t.Init(n, n, n, false)
	for i := 0; i < n; i++ {

		// Some very fake diagonals. Should take exactly 20 GMRES steps
		ar := 10.0 + float64(i)/(float64(n)/10.0)
		ac := 10.0 - float64(i)/(float64(n)/10.0)
		t.Put(i, i, ar, ac)

		// Let exact solution = 1 + 0.5i
		x_correct[i] = complex(float64(i+1), float64(i+1)/10.0)

		// Generate RHS to match exact solution
		b[i] = complex(ar*real(x_correct[i])-ac*imag(x_correct[i]),
			ar*imag(x_correct[i])+ac*real(x_correct[i]))
	}

	// run text
	run_linsol_testC(tst, &t, 1e-14, 1e-13, b, x_correct)
}
Esempio n. 30
0
func Test_invs07(tst *testing.T) {

	//verbose()
	chk.PrintTitle("invs07")

	smp_a, smp_b, smp_β, smp_ϵ := -1.0, 0.0, 1.0, 1e-3

	σ := []float64{-1, -1, 0, 0}

	pcam, qcam, _ := M_pqw(σ)
	poct, qoct := pcam*SQ3, qcam*SQ2by3

	N := make([]float64, 3)
	n := make([]float64, 3)
	m := SmpDirector(N, σ, smp_a, smp_b, smp_β, smp_ϵ)
	SmpUnitDirector(n, m, N)
	psmp1, qsmp1, err := GenInvs(σ, n, smp_a)
	if err != nil {
		chk.Panic("M_GenInvs failed:\n%v", err)
	}

	psmp2, qsmp2, err := M_pq_smp(σ, smp_a, smp_b, smp_β, smp_ϵ)
	if err != nil {
		chk.Panic("M_pq_smp failed:\n%v", err)
	}
	io.Pforan("pcam,  qcam  = %v, %v\n", pcam, qcam)
	io.Pforan("poct,  qoct  = %v, %v\n", poct, qoct)
	io.Pforan("psmp1, qsmp1 = %v, %v\n", psmp1, qsmp1)
	io.Pforan("psmp2, qsmp2 = %v, %v\n", psmp2, qsmp2)
	chk.Scalar(tst, "p", 1e-15, psmp1, psmp2)
	chk.Scalar(tst, "q", 1e-15, qsmp1, qsmp2)
}