Ejemplo n.º 1
0
func Mul(a, b string) string {
	ra, rb, f := twop(a, b)
	ar := bignum.Rat(ra, int64(f))
	br := bignum.Rat(rb, int64(f))
	i, n := ar.Mul(br).Value()
	nv := n.Value()
	d := uint64(1)
	for d%nv != 0 {
		d *= 10
	}
	i = i.Mul1(int64(d / nv))
	if uint64(f) < d {
		i = i.Div(bignum.Int(int64(d / uint64(f))))
		d = uint64(f)
	}
	return String(i.Value(), int(d))
}
Ejemplo n.º 2
0
func NewHilbert(n int) *Matrix {
	a := NewMatrix(n, n)
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			x := Big.Rat(1, int64(i+j+1))
			a.set(i, j, x)
		}
	}
	return a
}
Ejemplo n.º 3
0
func NewInverseHilbert(n int) *Matrix {
	a := NewMatrix(n, n)
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			x0 := One
			if (i+j)&1 != 0 {
				x0 = x0.Neg()
			}
			x1 := Big.Rat(int64(i+j+1), 1)
			x2 := MakeRat(Big.Binomial(uint(n+i), uint(n-j-1)))
			x3 := MakeRat(Big.Binomial(uint(n+j), uint(n-i-1)))
			x4 := MakeRat(Big.Binomial(uint(i+j), uint(i)))
			x4 = x4.Mul(x4)
			a.set(i, j, x0.Mul(x1).Mul(x2).Mul(x3).Mul(x4))
		}
	}
	return a
}
Ejemplo n.º 4
0
func (t *idealFloatType) Zero() Value { return &idealFloatV{bignum.Rat(1, 0)} }
Ejemplo n.º 5
0
func (t *uintType) minVal() *bignum.Rational { return bignum.Rat(0, 1) }
Ejemplo n.º 6
0
	Val("i", 1),
	CErr("zzz", undefined),
	// TODO(austin) Test variable in constant context
	//CErr("t", typeAsExpr),

	Val("'a'", bignum.Int('a')),
	Val("'\\uffff'", bignum.Int('\uffff')),
	Val("'\\n'", bignum.Int('\n')),
	CErr("''+x", badCharLit),
	// Produces two parse errors
	//CErr("'''", ""),
	CErr("'\n'", badCharLit),
	CErr("'\\z'", illegalEscape),
	CErr("'ab'", badCharLit),

	Val("1.0", bignum.Rat(1, 1)),
	Val("1.", bignum.Rat(1, 1)),
	Val(".1", bignum.Rat(1, 10)),
	Val("1e2", bignum.Rat(100, 1)),

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

	Val("(i)", 1),

	Val("ai[0]", 1),
	Val("(&ai)[0]", 1),
	Val("ai[1]", 2),
Ejemplo n.º 7
0
// Computes a Hilbert matrix, its inverse, multiplies them
// and verifies that the product is the identity matrix.

package main

import Big "bignum"
import Fmt "fmt"

func assert(p bool) {
	if !p {
		panic("assert failed")
	}
}

var (
	Zero = Big.Rat(0, 1)
	One  = Big.Rat(1, 1)
)

type Matrix struct {
	n, m int
	a    []*Big.Rational
}

func (a *Matrix) at(i, j int) *Big.Rational {
	assert(0 <= i && i < a.n && 0 <= j && j < a.m)
	return a.a[i*a.m+j]
}

func (a *Matrix) set(i, j int, x *Big.Rational) {
	assert(0 <= i && i < a.n && 0 <= j && j < a.m)