func check(problem int, tolf, tolg float64) { io.Pf("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> problem %d <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n", problem) opt := getfcn(problem) x := opt.RptXref f := make([]float64, opt.Nf) g := make([]float64, opt.Ng) h := make([]float64, opt.Nh) opt.MinProb(f, g, h, x, nil, 0) err := math.Abs(f[0] - opt.RptFref[0]) io.Pforan("nx=%d nf=%d ng=%d nh=%d\n", opt.Nflt, opt.Nf, opt.Ng, opt.Nh) io.Pforan("x = %v\n", x) io.Pforan("f = %v err = %v\n", f, err) io.Pforan("g = %v\n", g) io.Pforan("h = %v\n", h) for i := 0; i < opt.Ng; i++ { if g[i] < 0 { io.PfRed("unfeasible on g\n") } } for i := 0; i < opt.Nh; i++ { if math.Abs(h[i]) > opt.EpsH { io.PfRed("unfeasible on h\n") } } if err > tolf { io.PfRed("err is too big\n") } }
func Test_quad01(tst *testing.T) { //verbose() chk.PrintTitle("quad01") y := func(x float64) float64 { return math.Sqrt(1.0 + math.Pow(math.Sin(x), 3.0)) } var err error Acor := 1.08268158558 // trapezoidal rule var T Quadrature T = new(Trap) T.Init(y, 0, 1, 1e-11) A, err := T.Integrate() if err != nil { io.Pforan(err.Error()) } io.Pforan("A = %v\n", A) chk.Scalar(tst, "A", 1e-11, A, Acor) // Simpson's rule var S Quadrature S = new(Simp) S.Init(y, 0, 1, 1e-11) A, err = S.Integrate() if err != nil { io.Pforan(err.Error()) } io.Pforan("A = %v\n", A) chk.Scalar(tst, "A", 1e-11, A, Acor) }
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)) }
func Test_intordmut01(tst *testing.T) { //verbose() chk.PrintTitle("intordmut01") var ops OpsData ops.SetDefault() ops.Pm = 1 rnd.Init(0) a := []int{1, 2, 3, 4, 5, 6, 7, 8} io.Pforan("before: a = %v\n", a) ops.OrdSti = []int{2, 5, 4} IntOrdMutation(a, 0, &ops) io.Pfcyan("after: a = %v\n", a) chk.Ints(tst, "a", a, []int{1, 2, 6, 7, 3, 4, 5, 8}) nums := utl.IntRange2(1, 9) sort.Ints(a) chk.Ints(tst, "asorted = 12345678", a, nums) a = []int{1, 2, 3, 4, 5, 6, 7, 8} io.Pforan("\nbefore: a = %v\n", a) ops.OrdSti = nil IntOrdMutation(a, 0, &ops) io.Pfcyan("after: a = %v\n", a) sort.Ints(a) chk.Ints(tst, "asorted = 12345678", a, nums) }
func Test_binmut01(tst *testing.T) { //verbose() chk.PrintTitle("binmut01. mutation: binary") var ops OpsData ops.SetDefault() ops.Pm = 1.0 ops.Tmax = 10 ops.Nchanges = 3 rnd.Init(0) A := []int{0, 1, 1, 1, 0, 0, 1, 0, 1, 1} a := make([]int, len(A)) copy(a, A) io.Pforan("before: A = %v\n", A) IntBinMutation(A, 0, &ops) io.Pforan("after: A = %v\n", A) ndiff := 0 for i := 0; i < len(A); i++ { if A[i] != a[i] { ndiff++ } } io.Pforan("number of changes = %v\n", ndiff) if ndiff != ops.Nchanges { tst.Errorf("binary mutation failed\n") } }
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) }
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)) }
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) }
func Test_hash02(tst *testing.T) { //verbose() chk.PrintTitle("hash02") c1 := []float64{9.99999999, 9999.999999, 99.99998} c2 := []float64{9.99999998, 9999.999999, 99.99998} tol := 1e-15 xmin := []float64{0, 0, 0} xmax := []float64{10, 10000, 100} xdel := []float64{0, 0, 0} for i := 0; i < 3; i++ { xdel[i] = xmax[i] - xmin[i] } h1 := HashPoint(c1, xmin, xdel, tol) h2 := HashPoint(c2, xmin, xdel, tol) io.Pforan("h1 = %v\n", h1) io.Pforan("h2 = %v\n", h2) if h1 == h2 { chk.Panic("h1 must not be equal to h2") } }
func Test_2dinteg02(tst *testing.T) { //verbose() chk.PrintTitle("2dinteg02. bidimensional integral") // Γ(1/4, 1) gamma_1div4_1 := 0.2462555291934987088744974330686081384629028737277219 x := utl.LinSpace(0, 1, 11) y := utl.LinSpace(0, 1, 11) m, n := len(x), len(y) f := la.MatAlloc(m, n) for i := 0; i < m; i++ { for j := 0; j < n; j++ { f[i][j] = 8.0 * math.Exp(-math.Pow(x[i], 2)-math.Pow(y[j], 4)) } } dx, dy := x[1]-x[0], y[1]-y[0] Vt := Trapz2D(dx, dy, f) Vs := Simps2D(dx, dy, f) Vc := math.Sqrt(math.Pi) * math.Erf(1) * (math.Gamma(1.0/4.0) - gamma_1div4_1) io.Pforan("Vt = %v\n", Vt) io.Pforan("Vs = %v\n", Vs) io.Pfgreen("Vc = %v\n", Vc) chk.Scalar(tst, "Vt", 0.0114830435645548, Vt, Vc) chk.Scalar(tst, "Vs", 1e-4, Vs, Vc) }
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}) }
// run_rootsol_test runs root solution test // Note: xguess is the trial solution for Newton's method (not Brent's) func run_rootsol_test(tst *testing.T, xa, xb, xguess, tolcmp float64, ffcnA Cb_yxe, ffcnB Cb_f, JfcnB Cb_Jd, fname string, save, show bool) (xbrent float64) { // Brent io.Pfcyan("\n - - - - - - - using Brent's method - - -- - - - \n") var o Brent o.Init(ffcnA) var err error xbrent, err = o.Solve(xa, xb, false) if err != nil { chk.Panic("%v", err) } var ybrent float64 ybrent, err = ffcnA(xbrent) if err != nil { chk.Panic("%v", err) } io.Pforan("x = %v\n", xbrent) io.Pforan("f(x) = %v\n", ybrent) io.Pforan("nfeval = %v\n", o.NFeval) io.Pforan("nit = %v\n", o.It) if math.Abs(ybrent) > 1e-10 { chk.Panic("Brent failed: f(x) = %g > 1e-10\n", ybrent) } // Newton io.Pfcyan("\n - - - - - - - using Newton's method - - -- - - - \n") var p NlSolver p.Init(1, ffcnB, nil, JfcnB, true, false, nil) xnewt := []float64{xguess} var cnd float64 cnd, err = p.CheckJ(xnewt, 1e-6, true, !chk.Verbose) io.Pforan("cond(J) = %v\n", cnd) if err != nil { chk.Panic("%v", err.Error()) } err = p.Solve(xnewt, false) if err != nil { chk.Panic("%v", err.Error()) } var ynewt float64 ynewt, err = ffcnA(xnewt[0]) if err != nil { chk.Panic("%v", err) } io.Pforan("x = %v\n", xnewt[0]) io.Pforan("f(x) = %v\n", ynewt) io.Pforan("nfeval = %v\n", p.NFeval) io.Pforan("nJeval = %v\n", p.NJeval) io.Pforan("nit = %v\n", p.It) if math.Abs(ynewt) > 1e-9 { chk.Panic("Newton failed: f(x) = %g > 1e-10\n", ynewt) } // compare Brent's and Newton's solutions PlotYxe(ffcnA, "results", fname, xbrent, xa, xb, 101, "Brent", "'b-'", save, show, func() { plt.PlotOne(xnewt[0], ynewt, "'g+', ms=15, label='Newton'") }) chk.Scalar(tst, "xbrent - xnewt", tolcmp, xbrent, xnewt[0]) return }
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) }
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)) } }
func Test_ind02(tst *testing.T) { //verbose() chk.PrintTitle("ind02. copy into") rnd.Init(0) nbases := 1 A := get_individual(0, nbases) B := get_individual(1, nbases) fmts := map[string][]string{ "int": {"%2d", "%4d", "%5d"}, // ints "flt": {"%6g", "%6g", "%5g"}, // floats "str": {"%4s", "%2s", "%2s"}, // strings "key": {"%3x", "%3x", "%3x"}, // keys "byt": {"%4s", "%4s", "%4s"}, // bytes "fun": {"%3s", "%3s", "%3s"}, // funcs } io.Pfpink("A = %v\n", A.Output(fmts, false)) io.Pfcyan("B = %v\n", B.Output(fmts, false)) var ops OpsData ops.SetDefault() ops.Pc = 1.0 ops.Cuts = []int{1, 2} ops.Xrange = [][]float64{{0, 1}, {-20, 20}, {-300, 300}} a := A.GetCopy() b := A.GetCopy() IndCrossover(a, b, A, B, 0, &ops) io.Pforan("a = %v\n", a.Output(fmts, false)) io.Pfblue2("b = %v\n", b.Output(fmts, false)) chk.Ints(tst, "a.Ints ", a.Ints, []int{1, -20, 300}) chk.Ints(tst, "b.Ints ", b.Ints, []int{-1, 20, -300}) chk.Strings(tst, "a.Strings", a.Strings, []string{"abc", "Y", "c"}) chk.Strings(tst, "b.Strings", b.Strings, []string{"X", "b", "Z"}) // TODO: add other tests here io.Pf("\n") x := get_individual(0, nbases) x.Ovas = []float64{0, 0} x.Oors = []float64{0, 0, 0} io.Pfblue2("x = %v\n", x.Output(fmts, false)) B.CopyInto(x) chk.Scalar(tst, "ova0", 1e-17, x.Ovas[0], 200) chk.Scalar(tst, "ova1", 1e-17, x.Ovas[1], 100) chk.Scalar(tst, "oor0", 1e-17, x.Oors[0], 15) chk.Scalar(tst, "oor1", 1e-17, x.Oors[1], 25) chk.Scalar(tst, "oor2", 1e-17, x.Oors[2], 35) io.Pforan("x = %v\n", x.Output(fmts, false)) chk.String(tst, x.Output(fmts, false), B.Output(fmts, false)) }
func Test_igd01(tst *testing.T) { //verbose() chk.PrintTitle("igd. igd metric with star equal to trial => igd=0") // load star values prob := "UF1" fStar, err := io.ReadMatrix(io.Sf("./examples/mulobj-cec09/cec09/pf_data/%s.dat", prob)) if err != nil { tst.Errorf("cannot read fStar matrix:\n%v", err) return } npts := len(fStar) // optimiser var opt Optimiser opt.Default() opt.Nsol = npts opt.Ncpu = 1 opt.FltMin = []float64{0, 0} // used to store fStar opt.FltMax = []float64{1, 1} // used to store fStar nf, ng, nh := 2, 0, 0 // generator (store fStar into Flt) gen := func(sols []*Solution, prms *Parameters) { for i, sol := range sols { sol.Flt[0], sol.Flt[1] = fStar[i][0], fStar[i][1] } } // objective function (copy fStar from Flt into Ova) obj := func(f, g, h, x []float64, ξ []int, cpu int) { f[0], f[1] = x[0], x[1] } // initialise optimiser opt.Init(gen, nil, obj, nf, ng, nh) // compute igd igd := StatIgd(&opt, fStar) io.Pforan("igd = %v\n", igd) chk.Scalar(tst, "igd", 1e-15, igd, 0) // plot if chk.Verbose { fmt := &plt.Fmt{C: "red", M: ".", Ms: 1, Ls: "None", L: "solutions"} fS0 := utl.DblsGetColumn(0, fStar) fS1 := utl.DblsGetColumn(1, fStar) io.Pforan("len(fS0) = %v\n", len(fS0)) plt.SetForEps(0.75, 300) opt.PlotAddOvaOva(0, 1, opt.Solutions, true, fmt) plt.Plot(fS0, fS1, io.Sf("'b.', ms=2, label='star(%s)', clip_on=0", prob)) plt.Gll("$f_0$", "$f_1$", "") plt.SaveD("/tmp/goga", "igd01.eps") } }
func Test_fun05(tst *testing.T) { //verbose() chk.PrintTitle("fun05. zero and one") io.Pforan("Zero(666,nil) = %v\n", Zero.F(666, nil)) io.Pforan("One(666,nil) = %v\n", One.F(666, nil)) chk.Scalar(tst, "zero", 1e-17, Zero.F(666, nil), 0) chk.Scalar(tst, "one ", 1e-17, One.F(666, nil), 1) }
func Test_hist01(tst *testing.T) { //verbose() chk.PrintTitle("hist01") lims := []float64{0, 1, 2, 3, 4, 5} hist := Histogram{Stations: lims} idx := hist.FindBin(-3.3) chk.IntAssert(idx, -1) idx = hist.FindBin(7.0) chk.IntAssert(idx, -1) for i, x := range lims { idx = hist.FindBin(x) io.Pforan("x=%g idx=%d\n", x, idx) if i < len(lims)-1 { chk.IntAssert(idx, i) } else { chk.IntAssert(idx, -1) } } idx = hist.FindBin(0.5) chk.IntAssert(idx, 0) idx = hist.FindBin(1.5) chk.IntAssert(idx, 1) idx = hist.FindBin(2.5) chk.IntAssert(idx, 2) idx = hist.FindBin(3.99999999999999) chk.IntAssert(idx, 3) idx = hist.FindBin(4.999999) chk.IntAssert(idx, 4) hist.Count([]float64{ 0, 0.1, 0.2, 0.3, 0.9, // 5 1, 1, 1, 1.2, 1.3, 1.4, 1.5, 1.99, // 8 2, 2.5, // 2 3, 3.5, // 2 4.1, 4.5, 4.9, // 3 -3, -2, -1, 5, 6, 7, 8, }, true) io.Pforan("counts = %v\n", hist.Counts) chk.Ints(tst, "counts", hist.Counts, []int{5, 8, 2, 2, 3}) labels := hist.GenLabels("%g") io.Pforan("labels = %v\n", labels) }
func Test_fourlayers01(tst *testing.T) { //verbose() chk.PrintTitle("fourlayers01") analysis := NewFEM("data/fourlayers.sim", "", true, false, false, false, chk.Verbose, 0) doms := NewDomains(analysis.Sim, analysis.DynCfs, analysis.HydSta, 0, 1, false) if len(doms) == 0 { tst.Errorf("NewDomains failed\n") return } dom := doms[0] io.Pforan("stage # 0\n") err := dom.SetStage(0) if err != nil { tst.Errorf("SetStage # 0 failed\n%v", err) return } nids, eqs := get_nids_eqs(dom) chk.Ints(tst, "nids", nids, []int{1, 2, 14, 12, 0, 10}) chk.Ints(tst, "eqs", eqs, []int{0, 1, 12, 2, 3, 4, 5, 6, 7, 13, 8, 9, 10, 11}) io.Pforan("stage # 1\n") err = dom.SetStage(1) if err != nil { tst.Errorf("SetStage # 1 failed\n%v", err) return } nids, eqs = get_nids_eqs(dom) chk.Ints(tst, "nids", nids, []int{10, 12, 9, 6, 1, 2, 14, 0, 8}) chk.Ints(tst, "eqs", eqs, []int{0, 1, 2, 3, 19, 4, 5, 20, 6, 7, 8, 9, 18, 10, 11, 12, 13, 14, 15, 16, 17}) io.Pforan("stage # 2\n") err = dom.SetStage(2) if err != nil { tst.Errorf("SetStage # 2 failed\n%v", err) return } nids, eqs = get_nids_eqs(dom) chk.Ints(tst, "nids", nids, []int{10, 12, 9, 6, 1, 2, 14, 0, 7, 11, 8, 13}) chk.Ints(tst, "eqs", eqs, []int{0, 1, 2, 3, 25, 4, 5, 26, 6, 7, 8, 9, 24, 10, 11, 12, 13, 14, 15, 16, 17, 27, 18, 19, 20, 21, 22, 23}) io.Pforan("stage # 3\n") err = dom.SetStage(3) if err != nil { tst.Errorf("SetStage # 3 failed\n%v", err) return } nids, eqs = get_nids_eqs(dom) chk.Ints(tst, "nids", nids, []int{7, 13, 5, 4, 10, 12, 9, 6, 1, 2, 14, 11, 3, 0, 8}) chk.Ints(tst, "eqs", eqs, []int{0, 1, 33, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 31, 12, 13, 32, 14, 15, 16, 17, 30, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29}) }
func Test_mylab09(tst *testing.T) { //verbose() chk.PrintTitle("mylab09. arg min and max") u := []float64{1, 2, 3, -5, 60, -10, 8} imin, imax := DblArgMinMax(u) io.Pforan("imin = %v (5)\n", imin) io.Pforan("imax = %v (4)\n", imax) chk.IntAssert(imin, 5) chk.IntAssert(imax, 4) }
func Test_stat02(tst *testing.T) { //verbose() chk.PrintTitle("stat02") x := [][]float64{ {100, 100, 102, 98, 77, 99, 70, 105, 98}, {80, 101, 12, 58, 47, 80, 20, 111, 89}, {50, 130, 72, 38, 71, 15, 10, 12, 55}, } y, z := StatTable(x, true, true) la.PrintMat("x", x, "%5g", false) la.PrintMat("y", y, "%13.6f", false) la.PrintMat("z", z, "%13.6f", false) io.Pforan("\nmin\n") chk.Scalar(tst, "y00=min(x[0,:])", 1e-17, y[0][0], 70) chk.Scalar(tst, "y01=min(x[1,:])", 1e-17, y[0][1], 12) chk.Scalar(tst, "y02=min(x[2,:])", 1e-17, y[0][2], 10) io.Pforan("\nave\n") chk.Scalar(tst, "y10=ave(x[0,:])", 1e-17, y[1][0], 849.0/9.0) chk.Scalar(tst, "y11=ave(x[1,:])", 1e-17, y[1][1], 598.0/9.0) chk.Scalar(tst, "y12=ave(x[2,:])", 1e-17, y[1][2], 453.0/9.0) io.Pforan("\nmax\n") chk.Scalar(tst, "y20=max(x[0,:])", 1e-17, y[2][0], 105) chk.Scalar(tst, "y21=max(x[1,:])", 1e-17, y[2][1], 111) chk.Scalar(tst, "y22=max(x[2,:])", 1e-17, y[2][2], 130) io.Pforan("\ndev\n") chk.Scalar(tst, "y30=dev(x[0,:])", 1e-17, y[3][0], 12.134661099511597) chk.Scalar(tst, "y31=dev(x[1,:])", 1e-17, y[3][1], 34.688294535444918) chk.Scalar(tst, "y32=dev(x[2,:])", 1e-17, y[3][2], 38.343839140075687) io.Pfyel("\nmin\n") chk.Scalar(tst, "z00=min(y[0,:])=min(min)", 1e-17, z[0][0], 10) chk.Scalar(tst, "z01=min(y[1,:])=min(ave)", 1e-17, z[0][1], 453.0/9.0) chk.Scalar(tst, "z02=min(y[2,:])=min(max)", 1e-17, z[0][2], 105) chk.Scalar(tst, "z03=min(y[3,:])=min(dev)", 1e-17, z[0][3], 12.134661099511597) io.Pfyel("\nave\n") chk.Scalar(tst, "z10=ave(y[0,:])=ave(min)", 1e-17, z[1][0], 92.0/3.0) chk.Scalar(tst, "z11=ave(y[1,:])=ave(ave)", 1e-17, z[1][1], ((849.0+598.0+453.0)/9.0)/3.0) chk.Scalar(tst, "z12=ave(y[2,:])=ave(max)", 1e-17, z[1][2], 346.0/3.0) chk.Scalar(tst, "z13=ave(y[3,:])=ave(dev)", 1e-17, z[1][3], (12.134661099511597+34.688294535444918+38.343839140075687)/3.0) io.Pfyel("\nmax\n") chk.Scalar(tst, "z20=max(y[0,:])=max(min)", 1e-17, z[2][0], 70) chk.Scalar(tst, "z21=max(y[1,:])=max(ave)", 1e-17, z[2][1], 849.0/9.0) chk.Scalar(tst, "z22=max(y[2,:])=max(max)", 1e-17, z[2][2], 130) chk.Scalar(tst, "z23=max(y[3,:])=max(dev)", 1e-17, z[2][3], 38.343839140075687) io.Pfyel("\ndev\n") chk.Scalar(tst, "z30=dev(y[0,:])=dev(min)", 1e-17, z[3][0], 34.078341117685483) chk.Scalar(tst, "z31=dev(y[1,:])=dev(ave)", 1e-17, z[3][1], 22.261169573539771) chk.Scalar(tst, "z32=dev(y[2,:])=dev(max)", 1e-17, z[3][2], 13.051181300301263) chk.Scalar(tst, "z33=dev(y[3,:])=dev(dev)", 1e-17, z[3][3], 14.194778389023206) }
func Test_fourlayers01(tst *testing.T) { chk.PrintTitle("fourlayers01") if !Start("data/fourlayers.sim", true, chk.Verbose) { tst.Errorf("test failed\n") } defer End() distr := false dom := NewDomain(Global.Sim.Regions[0], distr) if dom == nil { tst.Errorf("test failed\n") return } io.Pforan("stage # 0\n") if !dom.SetStage(0, Global.Sim.Stages[0], distr) { tst.Errorf("test failed\n") return } nids, eqs := get_nids_eqs(dom) chk.Ints(tst, "nids", nids, []int{1, 2, 14, 12, 0, 10}) chk.Ints(tst, "eqs", eqs, []int{0, 1, 12, 2, 3, 4, 5, 6, 7, 13, 8, 9, 10, 11}) io.Pforan("stage # 1\n") if !dom.SetStage(1, Global.Sim.Stages[1], distr) { tst.Errorf("test failed\n") return } nids, eqs = get_nids_eqs(dom) chk.Ints(tst, "nids", nids, []int{10, 12, 9, 6, 1, 2, 14, 0, 8}) chk.Ints(tst, "eqs", eqs, []int{0, 1, 2, 3, 19, 4, 5, 20, 6, 7, 8, 9, 18, 10, 11, 12, 13, 14, 15, 16, 17}) io.Pforan("stage # 2\n") if !dom.SetStage(2, Global.Sim.Stages[2], distr) { tst.Errorf("test failed\n") return } nids, eqs = get_nids_eqs(dom) chk.Ints(tst, "nids", nids, []int{10, 12, 9, 6, 1, 2, 14, 0, 7, 11, 8, 13}) chk.Ints(tst, "eqs", eqs, []int{0, 1, 2, 3, 25, 4, 5, 26, 6, 7, 8, 9, 24, 10, 11, 12, 13, 14, 15, 16, 17, 27, 18, 19, 20, 21, 22, 23}) io.Pforan("stage # 3\n") if !dom.SetStage(3, Global.Sim.Stages[3], distr) { tst.Errorf("test failed\n") return } nids, eqs = get_nids_eqs(dom) chk.Ints(tst, "nids", nids, []int{7, 13, 5, 4, 10, 12, 9, 6, 1, 2, 14, 11, 3, 0, 8}) chk.Ints(tst, "eqs", eqs, []int{0, 1, 33, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 31, 12, 13, 32, 14, 15, 16, 17, 30, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29}) }
func Test_pareto01(tst *testing.T) { //verbose() chk.PrintTitle("pareto01. compare vectors: Pareto-optimal") u := []float64{1, 2, 3, 4, 5, 6} v := []float64{1, 2, 3, 4, 5, 6} io.Pforan("u = %v\n", u) io.Pfblue2("v = %v\n", v) u_dominates, v_dominates := DblsParetoMin(u, v) io.Pfpink("u_dominates = %v\n", u_dominates) io.Pfpink("v_dominates = %v\n", v_dominates) if u_dominates { tst.Errorf("test failed\n") return } if v_dominates { tst.Errorf("test failed\n") return } v = []float64{1, 1.8, 3, 4, 5, 6} io.Pforan("\nu = %v\n", u) io.Pfblue2("v = %v\n", v) u_dominates, v_dominates = DblsParetoMin(u, v) io.Pfpink("u_dominates = %v\n", u_dominates) io.Pfpink("v_dominates = %v\n", v_dominates) if u_dominates { tst.Errorf("test failed\n") return } if !v_dominates { tst.Errorf("test failed\n") return } v = []float64{1, 2.1, 3, 4, 5, 6} io.Pforan("\nu = %v\n", u) io.Pfblue2("v = %v\n", v) u_dominates, v_dominates = DblsParetoMin(u, v) io.Pfpink("u_dominates = %v\n", u_dominates) io.Pfpink("v_dominates = %v\n", v_dominates) if !u_dominates { tst.Errorf("test failed\n") return } if v_dominates { tst.Errorf("test failed\n") return } }
func Test_hash01(tst *testing.T) { //verbose() chk.PrintTitle("hash01") c1 := []float64{1.0000000001, 2.0000000002} c2 := []float64{1.00000000009, 2.0000000002} c3 := []float64{1.0000000000002, 2.0000000002, 3.0000000000003} c4 := []float64{1.0000000000003, 2.0000000002, 3.0000000000003} tol := 1e-11 xmin := []float64{-10, -1000, -100} xmax := []float64{10, 100, 1000} xdel := []float64{0, 0, 0} for i := 0; i < 3; i++ { xdel[i] = xmax[i] - xmin[i] } h1 := HashPoint(c1, xmin, xdel, tol) h2 := HashPoint(c2, xmin, xdel, tol) //tol = 1e-14 tol = 1e-15 h3 := HashPoint(c3, xmin, xdel, tol) h4 := HashPoint(c4, xmin, xdel, tol) io.Pforan("h1 = %v\n", h1) io.Pforan("h2 = %v\n", h2) io.Pforan("h3 = %v\n", h3) io.Pforan("h4 = %v\n", h4) if h1 == h2 { chk.Panic("h1 must not be equal to h2") } if h1 == h3 { chk.Panic("h1 must not be equal to h3") } if h1 == h4 { chk.Panic("h1 must not be equal to h4") } if h2 == h3 { chk.Panic("h2 must not be equal to h3") } if h2 == h4 { chk.Panic("h2 must not be equal to h4") } if h3 == h4 { chk.Panic("h3 must not be equal to h4") } }
func Test_sort02(tst *testing.T) { //verbose() chk.PrintTitle("sort02") i := []int{33, 0, 7, 8} x := []float64{1000.33, 0, -77.7, 88.8} y := []float64{1e-5, 1e-7, 1e-2, 1e-9} z := []float64{-8000, -7000, 0, -1} io.Pforan("by 'i'\n") I, X, Y, Z, err := SortQuadruples(i, x, y, z, "i") if err != nil { tst.Errorf("%v\n", err) } chk.Ints(tst, "i", I, []int{0, 7, 8, 33}) chk.Vector(tst, "x", 1e-16, X, []float64{0, -77.7, 88.8, 1000.33}) chk.Vector(tst, "y", 1e-16, Y, []float64{1e-7, 1e-2, 1e-9, 1e-5}) chk.Vector(tst, "z", 1e-16, Z, []float64{-7000, 0, -1, -8000}) io.Pforan("by 'x'\n") I, X, Y, Z, err = SortQuadruples(i, x, y, z, "x") if err != nil { tst.Errorf("%v\n", err) } chk.Ints(tst, "i", I, []int{7, 0, 8, 33}) chk.Vector(tst, "x", 1e-16, X, []float64{-77.7, 0.0, 88.8, 1000.33}) chk.Vector(tst, "y", 1e-16, Y, []float64{1e-2, 1e-7, 1e-9, 1e-5}) chk.Vector(tst, "z", 1e-16, Z, []float64{0, -7000, -1, -8000}) io.Pforan("by 'y'\n") I, X, Y, Z, err = SortQuadruples(i, x, y, z, "y") if err != nil { tst.Errorf("%v\n", err) } chk.Ints(tst, "i", I, []int{8, 0, 33, 7}) chk.Vector(tst, "x", 1e-16, X, []float64{88.8, 0, 1000.33, -77.7}) chk.Vector(tst, "y", 1e-16, Y, []float64{1e-9, 1e-7, 1e-5, 1e-2}) chk.Vector(tst, "z", 1e-16, Z, []float64{-1, -7000, -8000, 0}) io.Pforan("by 'z'\n") I, X, Y, Z, err = SortQuadruples(i, x, y, z, "z") if err != nil { tst.Errorf("%v\n", err) } chk.Ints(tst, "i", I, []int{33, 0, 8, 7}) chk.Vector(tst, "x", 1e-16, X, []float64{1000.33, 0, 88.8, -77.7}) chk.Vector(tst, "y", 1e-16, Y, []float64{1e-5, 1e-7, 1e-9, 1e-2}) chk.Vector(tst, "z", 1e-16, Z, []float64{-8000, -7000, -1, 0}) }
func Test_Mw02(tst *testing.T) { //verbose() chk.PrintTitle("Mw02") prms := []string{"φ", "Mfix"} vals := []float64{32, 0} var o NcteM o.Init(prms, vals) if SAVE_FIG { // rosette full, ref := false, true r := 1.1 * SQ2 * o.M(1) / 3.0 PlotRosette(r, full, ref, true, 7) // NcteM npts := 201 X := make([]float64, npts) Y := make([]float64, npts) W := utl.LinSpace(-1, 1, npts) for i, w := range W { θ := math.Asin(w) / 3.0 r := SQ2 * o.M(w) / 3.0 X[i] = -r * math.Sin(math.Pi/6.0-θ) Y[i] = r * math.Cos(math.Pi/6.0-θ) //plt.Text(X[i], Y[i], io.Sf("$\\\\theta=%.2f$", θ*180.0/math.Pi), "size=8, ha='center', color='red'") //plt.Text(X[i], Y[i], io.Sf("$w=%.2f$", w), "size=8, ha='center', color='red'") } plt.Plot(X, Y, "'b-'") // MC g := func(θ float64) float64 { return SQ2 * o.Sinφ / (SQ3*math.Cos(θ) - o.Sinφ*math.Sin(θ)) } io.Pforan("M( 1) = %v\n", SQ2*o.M(1)/3.0) io.Pforan("g(30) = %v\n", g(math.Pi/6.0)) for i, w := range W { θ := math.Asin(w) / 3.0 r := g(θ) X[i] = -r * math.Sin(math.Pi/6.0-θ) Y[i] = r * math.Cos(math.Pi/6.0-θ) } plt.Plot(X, Y, "'k-'") // save plt.Equal() plt.SaveD("/tmp/gosl", "mw02.eps") } }
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) }
func Test_beam01b(tst *testing.T) { //verbose() chk.PrintTitle("beam01b. simply supported") // start simulation analysis := NewFEM("data/beam01.sim", "", true, true, false, false, chk.Verbose, 0) // run simulation err := analysis.Run() if err != nil { tst.Errorf("Run failed:\n%v", err) return } // check dom := analysis.Domains[0] ele := dom.Elems[0].(*Beam) _, M := ele.CalcVandM(dom.Sol, 0.5, 1) qn, L := 15.0, 1.0 Mcentre := qn * L * L / 8.0 io.Pforan("M = %v (%v)\n", M, Mcentre) chk.Scalar(tst, "M @ centre", 1e-17, M[0], Mcentre) // check moment using OutIpsData idx_centre := 5 // considering 11 stations dat := ele.OutIpsData() res := dat[idx_centre].Calc(dom.Sol) io.Pfcyan("M @ centre (OutIpsData) = %v\n", res["M"]) chk.Scalar(tst, "M @ centre (OutIpsData)", 1e-17, res["M"], Mcentre) }
func Test_jacobi01(tst *testing.T) { //verbose() chk.PrintTitle("jacobi01") A := [][]float64{ {1, 2, 3}, {2, 3, 2}, {3, 2, 2}, } Q := MatAlloc(3, 3) v := make([]float64, 3) nit, err := Jacobi(Q, v, A) if err != nil { chk.Panic("Jacobi failed:\n%v", err) } io.Pforan("number of iterations = %v\n", nit) PrintMat("A", A, "%13.8f", false) PrintMat("Q", Q, "%13.8f", false) PrintVec("v", v, "%13.8f", false) chk.Matrix(tst, "Q", 1e-17, Q, [][]float64{ {7.81993314738381295e-01, 5.26633230856907386e-01, 3.33382506832158143e-01}, {-7.14394870018381645e-02, 6.07084171793832561e-01, -7.91419742017035133e-01}, {-6.19179178753124115e-01, 5.95068272145819699e-01, 5.12358171676802088e-01}, }) chk.Vector(tst, "v", 1e-17, v, []float64{-1.55809924785903786e+00, 6.69537390404459476e+00, 8.62725343814443657e-01}) }
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) }