예제 #1
0
파일: example_test.go 프로젝트: ncw/pslq
func Example() {
	in := make([]big.Float, 4)
	in[0].SetFloat64(10.978081862745977) // Unknown number
	in[1].SetFloat64(math.Pi)            // Some constants it might be
	in[2].SetFloat64(math.E)
	in[3].SetFloat64(math.Log(2))
	pslq := pslq.New(64).SetMaxSteps(1000)
	out, err := pslq.Run(in)
	if err != nil {
		log.Fatal(err)
	}
	for i := range out {
		d := &out[i]
		if d.Sign() != 0 {
			fmt.Printf("%+d * %.10f\n", d, &in[i])
		}
	}
	fmt.Printf("= 0\n")
	// The output shows that
	// 7 * in[0] - 21 * pi - 4 * e = 0
	// => in[0] = 3 * pi + 4 * e / 7

	// Output: +7 * 10.9780818627
	// -21 * 3.1415926536
	// -4 * 2.7182818285
	// = 0
}
예제 #2
0
파일: main.go 프로젝트: ncw/pslq
func main() {
	flag.Usage = syntaxError
	flag.Parse()
	args := flag.Args()
	if len(args) < 1 {
		fatalf("No input supplied\n")
	}
	var xs []big.Float
	for _, arg := range args {
		xs = readFile(arg, xs)
	}
	// Set the max precision
	if *prec == 0 {
		for i := range xs {
			x := &xs[i]
			if x.Prec() > *prec {
				*prec = x.Prec()
			}
		}
	}
	digits := int(math.Log10(2)*float64(*prec) + 1)
	fmt.Fprintf(stdout, "Using precision %d\n", *prec)
	for i := range xs {
		x := &xs[i]
		fmt.Fprintf(stdout, "x[%d] = %*.*f\n", i, digits+6, digits, x)
	}

	pslq := pslq.New(*prec).SetMaxSteps(*iterations).SetVerbose(*verbose)
	result, err := pslq.Run(xs)
	if err != nil {
		fmt.Fprintf(os.Stderr, "PSLQ failed: %v\n", err)
		os.Exit(1)
	}
	fmt.Fprintf(stdout, "Result is\n")
	for i := range result {
		d := &result[i]
		if d.Sign() == 0 {
			continue
		}
		fmt.Fprintf(stdout, "%d * %.*f\n", d, digits, &xs[i])
	}
}