Beispiel #1
0
func (b *Bayesian) ProbAWhenB(word string) *big.Rat {
	probA := big.NewRat(b.countA, b.totalCount)
	v := b.words[word]
	probBWhenA := big.NewRat(v.countAWithB, b.countA)
	probB := big.NewRat(v.countB, b.totalCount)
	br := &BayesRule{probBWhenA, probA, probB}
	probAWhenB := br.Calculate()
	return probAWhenB
}
func TestTweets(t *T) {
	b := makeTweets()

	assertProb(t, b, big.NewRat(1, 1), "grinnell")
	assertProb(t, b, big.NewRat(1, 1), "president")
	assertProb(t, b, big.NewRat(1, 9), "one")
	assertProb(t, b, big.NewRat(1, 3), "four")
	assertProb(t, b, big.NewRat(1, 2), "other")
}
Beispiel #3
0
func TestRatSub(t *testing.T) {
	fmt.Println("Testing RatSub")
	x := big.NewRat(0, 1).Sub(big.NewRat(1, 3), big.NewRat(1, 2))
	fmt.Print("x ")
	fmt.Println(x)
	if (x.Num().Cmp(big.NewInt(-1)) != 0) || (x.Denom().Cmp(big.NewInt(6)) != 0) {
		t.Error("Num and Denom don't match")
	}
}
Beispiel #4
0
func number_divide(x, y Obj) Obj {
	xfx := (uintptr(unsafe.Pointer(x)) & fixnum_mask) == fixnum_tag
	yfx := (uintptr(unsafe.Pointer(y)) & fixnum_mask) == fixnum_tag
	if xfx && yfx {
		i1 := int(uintptr(unsafe.Pointer(x))) >> fixnum_shift
		i2 := int(uintptr(unsafe.Pointer(y))) >> fixnum_shift
		// A good optimizer will combine the div and mod into
		// one instruction.
		r, m := i1/i2, i1%i2
		if m == 0 && r > fixnum_min && r < fixnum_max {
			return Make_fixnum(r)
		} else {
			return wrap(big.NewRat(int64(i1), int64(i2)))
		}
	}

	if (!xfx && (uintptr(unsafe.Pointer(x))&heap_mask) != heap_tag) ||
		(!yfx && (uintptr(unsafe.Pointer(y))&heap_mask) != heap_tag) {
		panic("bad type")
	}

	if xfx {
		x = wrap(big.NewInt(int64(fixnum_to_int(x))))
	}
	if yfx {
		y = wrap(big.NewInt(int64(fixnum_to_int(y))))
		//return wrap(z.Div(vx,vy))
	}

	switch vx := (*x).(type) {
	case *big.Int:
		var z *big.Int = big.NewInt(0)
		switch vy := (*y).(type) {
		case *big.Int:
			return simpBig(z.Div(vx, vy))
		case *big.Rat:
			z := big.NewRat(1, 1)
			z.SetInt(vx)
			return simpRat(z.Quo(z, vy))
		default:
			panic("bad type")
		}
	case *big.Rat:
		z := big.NewRat(1, 1)
		switch vy := (*y).(type) {
		case *big.Int:
			z.SetInt(vy)
			return simpRat(z.Quo(vx, z))
		case *big.Rat:
			return simpRat(z.Quo(vx, vy))
		}
	}
	panic("bad type")
}
Beispiel #5
0
func Euler33() string {
	prod := big.NewRat(1, 1)
	for a := 10; a < 100; a++ {
		for b := a + 1; b < 100; b++ {
			if hasProperty(a, b) {
				prod.Mul(prod, big.NewRat(int64(a), int64(b)))
			}
		}
	}
	den := prod.Denom()
	return fmt.Sprint(den.String())
}
func main() {

	current := big.NewRat(1, 1)
	result := big.NewRat(0, 1)

	for rounds := 1; rounds <= 15; rounds += 1 {

		result.SetFrac64(0, 1)

		split(1, rounds, 0, current, result)
		fmt.Println(result)
	}
}
Beispiel #7
0
func number_multiply(x, y Obj) Obj {
	xfx := (uintptr(unsafe.Pointer(x)) & fixnum_mask) == fixnum_tag
	yfx := (uintptr(unsafe.Pointer(y)) & fixnum_mask) == fixnum_tag
	if xfx && yfx {
		i1 := int64(int(uintptr(unsafe.Pointer(x))))
		i2 := int64(int(uintptr(unsafe.Pointer(y))))
		r := (i1 >> fixnum_shift) * (i2 >> fixnum_shift)
		if r > int64(fixnum_min) && r < int64(fixnum_max) {
			return Make_fixnum(int(r))
		} else {
			return wrap(big.NewInt(r))
		}
	}

	if (!xfx && (uintptr(unsafe.Pointer(x))&heap_mask) != heap_tag) ||
		(!yfx && (uintptr(unsafe.Pointer(y))&heap_mask) != heap_tag) {
		panic("bad type")
	}

	if xfx {
		x = wrap(big.NewInt(int64(fixnum_to_int(x))))
	}
	if yfx {
		y = wrap(big.NewInt(int64(fixnum_to_int(y))))
	}

	switch vx := (*x).(type) {
	case *big.Int:
		var z *big.Int = big.NewInt(0)
		switch vy := (*y).(type) {
		case *big.Int:
			return simpBig(z.Mul(vx, vy))
		case *big.Rat:
			z := big.NewRat(1, 1)
			z.SetInt(vx)
			return simpRat(z.Mul(z, vy))
		default:
			panic("bad type")
		}
	case *big.Rat:
		z := big.NewRat(1, 1)
		switch vy := (*y).(type) {
		case *big.Int:
			z.SetInt(vy)
			return simpRat(z.Mul(vx, z))
		case *big.Rat:
			return simpRat(z.Mul(vx, vy))
		}
	}
	panic("bad type")
}
Beispiel #8
0
func (rs *ResultSet) rat(ord int) (value *big.Rat, isNull bool) {
	if rs.conn.LogLevel >= LogVerbose {
		defer rs.conn.logExit(rs.conn.logEnter("*ResultSet.rat"))
	}

	isNull = rs.isNull(ord)
	if isNull {
		return
	}

	val := rs.values[ord]

	switch rs.fields[ord].format {
	case textFormat:
		x := big.NewRat(1, 1)
		if _, ok := x.SetString(string(val)); !ok {
			panic("*big.Rat.SetString failed")
		}
		value = x

	case binaryFormat:
		panicNotImplemented()
	}

	return
}
Beispiel #9
0
func Test_Numeric(t *testing.T) {
	strWant := "0." + strings.Repeat("0123456789", 100)[1:]
	numWant, _ := big.NewRat(1, 1).SetString(strWant)
	numParam := param("@num", Numeric, numWant)

	withStatementResultSet(t, "SELECT @num;", []*Parameter{numParam}, func(rs *ResultSet) {
		// Use interface{}, so *resultSet.Any will be tested as well.
		var numHaveInterface interface{}

		_, err := rs.ScanNext(&numHaveInterface)
		if err != nil {
			t.Error("failed to scan next:", err)
		}

		numHave, ok := numHaveInterface.(*big.Rat)
		if !ok {
			t.Errorf("unexpected type: %T", numHaveInterface)
			return
		}

		strHave := numHave.FloatString(999)
		if strHave != strWant {
			t.Errorf("have: %s, but want: %s", strHave, strWant)
		}
	})
}
Beispiel #10
0
// MakeConst makes an ideal constant from a literal
// token and the corresponding literal string.
func MakeConst(tok token.Token, lit string) Const {
	switch tok {
	case token.INT:
		var x big.Int
		_, ok := x.SetString(lit, 0)
		assert(ok)
		return Const{&x}
	case token.FLOAT:
		var y big.Rat
		_, ok := y.SetString(lit)
		assert(ok)
		return Const{&y}
	case token.IMAG:
		assert(lit[len(lit)-1] == 'i')
		var im big.Rat
		_, ok := im.SetString(lit[0 : len(lit)-1])
		assert(ok)
		return Const{cmplx{big.NewRat(0, 1), &im}}
	case token.CHAR:
		assert(lit[0] == '\'' && lit[len(lit)-1] == '\'')
		code, _, _, err := strconv.UnquoteChar(lit[1:len(lit)-1], '\'')
		assert(err == nil)
		return Const{big.NewInt(int64(code))}
	case token.STRING:
		s, err := strconv.Unquote(lit)
		assert(err == nil)
		return Const{s}
	}
	panic("unreachable")
}
Beispiel #11
0
func (b *Bayesian) Filter(data Data, threshold *big.Rat) bool {
	total := big.NewRat(1, 1)
	for _, word := range data.GetWords() {
		w := lowerTrim(word)
		_, has := b.words[w]
		if has {
			probAWhenB := b.ProbAWhenB(word)
			one := big.NewRat(1, 1)
			total = total.Mul(total, one.Sub(one, probAWhenB))
		}

	}
	one := big.NewRat(1, 1)
	inverseThreshold := one.Sub(one, threshold)
	return total.Cmp(inverseThreshold) <= 0
}
Beispiel #12
0
func (d *Deserializer) ReadObject() Obj {
	tag := d.readInt().Int64()
	length := d.readInt()
	switch tag {
	case Integer:
		if length.Cmp(fixnum_min_Int) < 0 || length.Cmp(fixnum_max_Int) > 0 {
			var vv interface{} = length
			return Obj(&vv)
		}
		return Make_fixnum(int(length.Int64()))
	case Rational:
		i := big.NewRat(1, 1)
		return wrap(i.SetFrac(length, d.readInt()))
	case Float64:
		var v float64
		binary.Read(d.r, binary.LittleEndian, &v)
		return wrap(v)
	case Complex128:
		var v complex128
		binary.Read(d.r, binary.LittleEndian, &v)
		return wrap(v)
	case Pair:
		o1 := d.ReadObject()
		o2 := d.ReadObject()
		return Cons(o1, o2)
	case Vector:
		l := length.Int64() // fix
		obj := Make_vector(Make_fixnum(int(l)), Void)
		var i int64 = 0
		for ; i < l; i++ {
			t := d.ReadObject()
			Vector_set_ex(obj, Make_fixnum(int(i)), t)
		}
		return obj
	case Null:
		return Eol
	case String:
		s := d.readString(length.Int64())
		return String_string(s)
	case Symbol:
		i := length.Int64()
		s := d.readString(i)
		return String_to_symbol(String_string(s))
	case Bytevector:
		b := make([]byte, length.Int64())
		d.r.Read(b)
		return wrap(b)
	case Boolean:
		if length.Int64() != 0 {
			return Make_boolean(true)
		}
		return Make_boolean(false)
	case Char:
		c := length.Int64()
		return Make_char(int(c))
	}
	s := fmt.Sprintf("unknown tag: %v length: %v", tag, length)
	panic(s)
}
Beispiel #13
0
// Match attempts to match the internal constant representations of x and y.
// If the attempt is successful, the result is the values of x and y,
// if necessary converted to have the same internal representation; otherwise
// the results are invalid.
func (x Const) Match(y Const) (u, v Const) {
	switch a := x.val.(type) {
	case bool:
		if _, ok := y.val.(bool); ok {
			u, v = x, y
		}
	case *big.Int:
		switch y.val.(type) {
		case *big.Int:
			u, v = x, y
		case *big.Rat:
			var z big.Rat
			z.SetInt(a)
			u, v = Const{&z}, y
		case cmplx:
			var z big.Rat
			z.SetInt(a)
			u, v = Const{cmplx{&z, big.NewRat(0, 1)}}, y
		}
	case *big.Rat:
		switch y.val.(type) {
		case *big.Int:
			v, u = y.Match(x)
		case *big.Rat:
			u, v = x, y
		case cmplx:
			u, v = Const{cmplx{a, big.NewRat(0, 0)}}, y
		}
	case cmplx:
		switch y.val.(type) {
		case *big.Int, *big.Rat:
			v, u = y.Match(x)
		case cmplx:
			u, v = x, y
		}
	case string:
		if _, ok := y.val.(string); ok {
			u, v = x, y
		}
	default:
		panic("unreachable")
	}
	return
}
Beispiel #14
0
func TestRatString(t *testing.T) {
	fmt.Println("Testing RatString")
	x := big.NewRat(0, 1)
	_, ok := x.SetString("5.23")
	fmt.Print("x ")
	fmt.Print(x)
	fmt.Print(" ")
	fmt.Println(x.FloatString(4))
	fmt.Print("ok ")
	fmt.Println(ok)
}
Beispiel #15
0
func split(turn, maxTurns int, blueDiscsDrawn int, current, result *big.Rat) {

	if blueDiscsDrawn > (maxTurns / 2) {
		result.Add(result, current)
		//fmt.Println(blueDiscsDrawn, current)
		return
	} else if turn > maxTurns {
		return
	}

	redCurrent := big.NewRat(1, 1)
	blueCurrent := big.NewRat(1, 1)

	redCurrent.Mul(current, big.NewRat(int64(turn), int64(turn+1)))
	blueCurrent.Mul(current, big.NewRat(1, int64(turn+1)))

	split(turn+1, maxTurns, blueDiscsDrawn, redCurrent, result)
	split(turn+1, maxTurns, blueDiscsDrawn+1, blueCurrent, result)

}
Beispiel #16
0
func TestRat(t *testing.T) {
	fmt.Println("Testing Rat")
	x := big.NewRat(34234212327, 5464523)
	fmt.Print("x ")
	fmt.Print(x)
	fmt.Print(" ")
	fmt.Println(x.FloatString(5))
	if x.FloatString(5) != "6264.81256" {
		t.Error("Float String rep not matching")
	}
}
Beispiel #17
0
func (t *idealFloatType) Zero() Value { return &idealFloatV{big.NewRat(0, 1)} }
Beispiel #18
0
func (t *uintType) minVal() *big.Rat { return big.NewRat(0, 1) }
Beispiel #19
0
	Val("i", 1),
	CErr("zzz", undefined),
	// TODO(austin) Test variable in constant context
	//CErr("t", typeAsExpr),

	Val("'a'", big.NewInt('a')),
	Val("'\\uffff'", big.NewInt('\uffff')),
	Val("'\\n'", big.NewInt('\n')),
	CErr("''+x", badCharLit),
	// Produces two parse errors
	//CErr("'''", ""),
	CErr("'\n'", badCharLit),
	CErr("'\\z'", unknownEscape),
	CErr("'ab'", badCharLit),

	Val("1.0", big.NewRat(1, 1)),
	Val("1.", big.NewRat(1, 1)),
	Val(".1", big.NewRat(1, 10)),
	Val("1e2", big.NewRat(100, 1)),

	Val("\"abc\"", "abc"),
	Val("\"\"", ""),
	Val("\"\\n\\\"\"", "\n\""),
	CErr("\"\\z\"", unknownEscape),
	CErr("\"abc", "string not terminated"),

	Val("(i)", 1),

	Val("ai[0]", 1),
	Val("(&ai)[0]", 1),
	Val("ai[1]", 2),
Beispiel #20
0
func number_cmp(x, y Obj) Obj {
	xfx := (uintptr(unsafe.Pointer(x)) & fixnum_mask) == fixnum_tag
	yfx := (uintptr(unsafe.Pointer(y)) & fixnum_mask) == fixnum_tag
	if xfx && yfx {
		i1 := int(uintptr(unsafe.Pointer(x))) >> fixnum_shift
		i2 := int(uintptr(unsafe.Pointer(y))) >> fixnum_shift
		switch {
		case i1 > i2:
			return Make_fixnum(1)
		case i1 < i2:
			return Make_fixnum(-1)
		default:
			return Make_fixnum(0)
		}
	}

	if (!xfx && (uintptr(unsafe.Pointer(x))&heap_mask) != heap_tag) ||
		(!yfx && (uintptr(unsafe.Pointer(y))&heap_mask) != heap_tag) {
		panic("bad type")
	}

	if xfx {
		return number_subtract(y, x)
	}

	switch vx := (*x).(type) {
	case *big.Int:
		if yfx {
			vy := big.NewInt(int64(fixnum_to_int(y)))
			return Make_fixnum(vx.Cmp(vy))
		}
		switch vy := (*y).(type) {
		case *big.Int:
			return Make_fixnum(vx.Cmp(vy))
		case *big.Rat:
			r := big.NewRat(1, 1).SetInt(vx)
			return Make_fixnum(r.Cmp(vy))
		case complex128:
			panic("comparison on complex numbers is undefined")
		default:
			panic("bad type")
		}
	case *big.Rat:
		if yfx {
			vy := big.NewRat(int64(fixnum_to_int(y)), 1)
			return Make_fixnum(vx.Cmp(vy))
		}
		switch vy := (*y).(type) {
		case *big.Int:
			r := big.NewRat(1, 1).SetInt(vy)
			return Make_fixnum(vx.Cmp(r))
		case *big.Rat:
			return Make_fixnum(vx.Cmp(vy))
		case complex128:
			panic("comparison on complex numbers is undefined")
		default:
			panic("bad type")
		}
	case complex128:
		panic("comparison on complex numbers is undefined")
	}
	panic("bad type")
}
	b.Add(Tweet{[]string{"president", "grinnell", "three"}, true})

	return b
}

func TestTweets(t *T) {
	b := makeTweets()

	assertProb(t, b, big.NewRat(1, 1), "grinnell")
	assertProb(t, b, big.NewRat(1, 1), "president")
	assertProb(t, b, big.NewRat(1, 9), "one")
	assertProb(t, b, big.NewRat(1, 3), "four")
	assertProb(t, b, big.NewRat(1, 2), "other")
}

var always = big.NewRat(1, 1)
var usually = big.NewRat(2, 3)
var mostly = big.NewRat(1, 2)
var sometimes = big.NewRat(1, 3)
var never = big.NewRat(0, 1)

func makeTweet(text string) Tweet {
	return Tweet{strings.Fields(text), false}
}

func TestFilter(t *T) {
	b := makeTweets()

	assertFilters(t, b, makeTweet("grinnell has a president"), always)
	assertNotFilters(t, b, makeTweet("five six seven"), sometimes)
	assertFilters(t, b, makeTweet("one four"), sometimes)