本文整理汇总了Golang中github.com/cpmech/gosl/io.Pforan函数的典型用法代码示例。如果您正苦于以下问题:Golang Pforan函数的具体用法?Golang Pforan怎么用?Golang Pforan使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Pforan函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: check
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")
}
}
示例2: Test_quad01
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)
}
示例3: Test_GOflt01
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))
}
示例4: Test_intordmut01
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)
}
示例5: Test_binmut01
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")
}
}
示例6: Test_invs07
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)
}
示例7: Test_MTint01
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))
}
示例8: Test_brent03
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)
}
示例9: Test_hash02
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")
}
}
示例10: Test_2dinteg02
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)
}
示例11: Test_state01
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})
}
示例12: run_rootsol_test
// 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
}
示例13: Test_munkres04
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)
}
示例14: Test_mtdeb01
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))
}
}
示例15: Test_ind02
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))
}