Ejemplo n.º 1
0
func TestRect(t *testing.T) {
	const n = 10
	// testing with float64
	for i := 0; i < n; i++ {
		llx, lly := rand.NormFloat64(), rand.NormFloat64()
		urx, ury := rand.NormFloat64(), rand.NormFloat64()
		r := newRect(llx, lly, urx, ury)
		a := []float64{llx, lly, urx, ury}
		if bytes.Compare(r.output(), output(a)) != 0 {
			t.Errorf("newRect(%f, %f, %f, %f) doesn't work",
				llx, lly, urx, ury)
		}
	}
	// testing with int
	for i := 0; i < n; i++ {
		// random function that returns both positive and negative
		rnd := func() int {
			sign := 1
			if rand.Intn(2) == 0 {
				sign = -1
			}
			return sign * rand.Int()
		}

		llx, lly, urx, ury := rnd(), rnd(), rnd(), rnd()
		r := newRectInt(llx, lly, urx, ury)
		a := []int{llx, lly, urx, ury}
		if bytes.Compare(r.output(), output(a)) != 0 {
			t.Errorf("newRect(%d, %d, %d, %d) doesn't work",
				llx, lly, urx, ury)
		}
	}
}
Ejemplo n.º 2
0
func genString(stddev float64) string {
	n := int(math.Fabs(rand.NormFloat64()*stddev + stddev/2))
	c := make([]int, n)
	for i := range c {
		f := math.Fabs(rand.NormFloat64()*64 + 32)
		if f > 0x10ffff {
			f = 0x10ffff
		}
		c[i] = int(f)
	}
	return string(c)
}
Ejemplo n.º 3
0
/*
	randomly permute particle's position and strategy using evolution strategies method
*/
func (s *Swarm) mutate(p *Particle) {
	dim := float64(len(p.Position))
	tau := (1 / math.Sqrt(2*dim)) * (1.0 - float64(s.Generation)/float64(s.config.Generations))
	p.Strategy *= math.Exp(tau * rand.NormFloat64())
	for i := range p.Position {
		p.Position[i] += p.Strategy * rand.NormFloat64()
		if p.Position[i] > p.Max {
			p.Position[i] = p.Max
		} else if p.Position[i] < p.Min {
			p.Position[i] = p.Min
		}
	}
}
Ejemplo n.º 4
0
func main() {
	var (
		sum, variance, alpha float64 = 0.0, 0.0, 0.05
		path                         = make(chan packet, 100)
		endall                       = make(chan bool)
	)
	go func(k int) {
		for i := 0; i < k; i++ {
			path <- packet{math.Sin(theta*float64(i)) + alpha*rand.NormFloat64(), false}
		}
		//		fmt.Printf("%f\n", a);
		path <- packet{0.0, true}
	}(num)
	go func(k int) {
		for {
			a := <-path
			if a.end {
				endall <- true
				break
			}
			sum += a.x
			variance += a.x * a.x
		}
	}(num)
	<-endall
	fmt.Printf("mean value : %f\n", sum/float64(num))
	fmt.Printf("variance : %f\n", variance/float64(num))
}
Ejemplo n.º 5
0
func TestFloat64(t *testing.T) {
	base := []float64{
		0,
		math.Copysign(0, -1),
		-1,
		1,
		math.NaN(),
		math.Inf(+1),
		math.Inf(-1),
		0.1,
		1.5,
		1.9999999999999998,     // all 1s mantissa
		1.3333333333333333,     // 1.010101010101...
		1.1428571428571428,     // 1.001001001001...
		1.112536929253601e-308, // first normal
		2,
		4,
		8,
		16,
		32,
		64,
		128,
		256,
		3,
		12,
		1234,
		123456,
		-0.1,
		-1.5,
		-1.9999999999999998,
		-1.3333333333333333,
		-1.1428571428571428,
		-2,
		-3,
		1e-200,
		1e-300,
		1e-310,
		5e-324,
		1e-105,
		1e-305,
		1e+200,
		1e+306,
		1e+307,
		1e+308,
	}
	all := make([]float64, 200)
	copy(all, base)
	for i := len(base); i < len(all); i++ {
		all[i] = rand.NormFloat64()
	}

	test(t, "+", add, fop(Fadd64), all)
	test(t, "-", sub, fop(Fsub64), all)
	if GOARCH != "386" { // 386 is not precise!
		test(t, "*", mul, fop(Fmul64), all)
		test(t, "/", div, fop(Fdiv64), all)
	}
}
Ejemplo n.º 6
0
func genArray(n int) []interface{} {
	f := int(math.Fabs(rand.NormFloat64()) * math.Fmin(10, float64(n/2)))
	if f > n {
		f = n
	}
	x := make([]interface{}, int(f))
	for i := range x {
		x[i] = genValue(((i+1)*n)/f - (i*n)/f)
	}
	return x
}
Ejemplo n.º 7
0
func Flux(in FluxInput) (out chan *Datapoint) {
	out = make(chan *Datapoint)
	fc := make(chan *FluxComponent) // Channel to receive flux components from each gridpoint
	id := make(chan int)
	fc_ret := make(chan float64)
	go func() {
		// This goroutine is to accept flux components, store it in a slice
		// and return the transfer fluxes
		flux := make([]float64, in.NumGridpoints)
		for {
			select {
			case f := <-fc:
				flux[f.Idx] = f.Fc
			case idx := <-id:
				ft := in.FluxTransfer[in.FluxIndex[idx*2]:in.FluxIndex[idx*2+1]]
				for _, ftIdx := range ft {
					fc_ret <- flux[ftIdx-1] - flux[idx]
				}
			}
		}
	}()
	go func() {
		S_Var := float64(S_Start)
		for dt := range in.chFlux1 {
			fc <- &FluxComponent{dt.K * dt.Temp[0], dt.Idx}
			S_Var = S_Var + 5*rand.NormFloat64()
			dt.F[0] = S_Var * 0.25 * (1 - dt.A) //solar effect
			dt.F[1] = Sigma * math.Pow(dt.Temp[0], 4)
			dt.F[2] = Sigma * math.Pow(dt.Temp[1], 4)
			dt.F[3] = dt.F[2]
			dt.F[4] = Sigma * math.Pow(dt.Temp[2], 4)
			dt.F[5] = dt.F[4]
			out <- dt
		}
	}()
	go func() {
		for dt := range in.chFlux2 {
			id <- dt.Idx
			dt.F[6] = <-fc_ret
			dt.F[7] = <-fc_ret
			dt.F[8] = <-fc_ret
			if !(dt.NPole || dt.SPole) {
				fmt.Printf("Shouldn't be here\n")
				dt.F[9] = <-fc_ret
			}
			out <- dt
		}
		close(out)
	}()
	return out
}
Ejemplo n.º 8
0
func genMap(n int) map[string]interface{} {
	f := int(math.Fabs(rand.NormFloat64()) * math.Fmin(10, float64(n/2)))
	if f > n {
		f = n
	}
	if n > 0 && f == 0 {
		f = 1
	}
	x := make(map[string]interface{})
	for i := 0; i < f; i++ {
		x[genString(10)] = genValue(((i+1)*n)/f - (i*n)/f)
	}
	return x
}
Ejemplo n.º 9
0
func genArray(n int) []interface{} {
	f := int(math.Abs(rand.NormFloat64()) * math.Min(10, float64(n/2)))
	if f > n {
		f = n
	}
	if n > 0 && f == 0 {
		f = 1
	}
	x := make([]interface{}, f)
	for i := range x {
		x[i] = genValue(((i+1)*n)/f - (i*n)/f)
	}
	return x
}
Ejemplo n.º 10
0
Archivo: solar.go Proyecto: Arrow/GoGCM
func Solar() (out chan float64) {
	out = make(chan float64)
	ts := timestep.TimeStep()
	go func() {
		S_Var := float64(S_Start)
		for {
			select {
			case <-ts:
				S_Var = S_Var + 5*rand.NormFloat64()
			case out <- S_Var:
			}
		}
		close(out)
	}()
	return out
}
Ejemplo n.º 11
0
func normalDistributionDemo() {
	maxTrials := 100
	printEvery := 10
	fmt.Printf("\nGenerating %v normal samples. The descriptive statistics ", maxTrials)
	fmt.Printf("converge to the appropriate values: mean→0, variance→1, skew→0, kurtosis→0. \n")
	var d stats.Stats
	for i := 0; i <= maxTrials; i++ {
		y := rand.NormFloat64()
		d.Update(y)
		if i != 0 && i%printEvery == 0 {
			mean := d.Mean()
			variance := d.PopulationVariance()
			skew := d.PopulationSkew()
			kurtosis := d.PopulationKurtosis()
			fmt.Printf("itr %v: mean (→0.0) = %0.5f, variance (→1.0) = %0.5f, skew (→0.0) = %0.5f, kurtosis (→0.0) = %0.5f\n",
				i, mean, variance, skew, kurtosis)
		}
	}
}
Ejemplo n.º 12
0
func genValue(n int) interface{} {
	if n > 1 {
		switch rand.Intn(2) {
		case 0:
			return genArray(n)
		case 1:
			return genMap(n)
		}
	}
	switch rand.Intn(3) {
	case 0:
		return rand.Intn(2) == 0
	case 1:
		return rand.NormFloat64()
	case 2:
		return genString(30)
	}
	panic("unreachable")
}
Ejemplo n.º 13
0
func solar(ch chan *datapoint) (out chan *datapoint) {
	out = make(chan *datapoint)
	go func() {
		S_Var := float64(S_Start)
		for dt := range ch {
			switch dt.F {
			case 0.0:
				S_Var = S_Var + 5*rand.NormFloat64()
				dt.S, dt.F = S_Var, S_Var*0.25*(1-A)
			default:
				S_Var = dt.F * 4 / (1 - A)
				dt.S = S_Var
			}
			out <- dt
		}
		close(out)
	}()
	return out
}
Ejemplo n.º 14
0
func gcm(ch chan *datapoint) (out chan *datapoint) {
	out = make(chan *datapoint)
	go func() {
		Temp := float64(T_Start)
		for dt := range ch {
			switch dt.F {
			case 0.0:
				Temp = Temp + 0.05 + 0.08*rand.NormFloat64()
				dt.temp, dt.F = Temp, math.Pow(dt.temp, 4)/Sigma_Inv
			default:
				Temp = math.Pow(dt.F*Sigma_Inv, 0.25)
				dt.temp = Temp
			}
			out <- dt
		}
		close(out)
	}()
	return out
}
Ejemplo n.º 15
0
func gcm(ch chan *datapoint) (out chan *datapoint) {
	out = make(chan *datapoint)
	go func() {
		TempS := float64(T_Start)
		TempA := float64(T_Start)
		for dt := range ch {
			switch dt.F {
			case 0.0:
				TempS = TempS + 0.05 + 0.08*rand.NormFloat64()
				TempA = TempS / math.Pow(2, 0.25)
				dt.temp[0], dt.temp[1], dt.F = TempS, TempA, math.Pow(TempA, 4)/Sigma_Inv
			default:
				TempA = math.Pow(dt.F*Sigma_Inv, 0.25)
				TempS = math.Pow(2, 0.25) * TempA
				dt.temp[0], dt.temp[1] = TempS, TempA
			}
			out <- dt
		}
		close(out)
	}()
	return out
}