2012-02-17 04:48:57 +00:00
|
|
|
// run
|
2008-07-18 21:18:07 +00:00
|
|
|
|
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2012-02-19 06:44:02 +00:00
|
|
|
// Test concurrency primitives: power series.
|
|
|
|
|
2008-07-18 21:18:07 +00:00
|
|
|
// Power series package
|
|
|
|
// A power series is a channel, along which flow rational
|
|
|
|
// coefficients. A denominator of zero signifies the end.
|
|
|
|
// Original code in Newsqueak by Doug McIlroy.
|
|
|
|
// See Squinting at Power Series by Doug McIlroy,
|
|
|
|
// http://www.cs.bell-labs.com/who/rsc/thread/squint.pdf
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
2009-05-08 22:21:41 +00:00
|
|
|
import "os"
|
|
|
|
|
2008-07-18 21:18:07 +00:00
|
|
|
type rat struct {
|
2010-09-04 00:36:13 +00:00
|
|
|
num, den int64 // numerator, denominator
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func (u rat) pr() {
|
2009-12-10 03:27:08 +00:00
|
|
|
if u.den==1 {
|
|
|
|
print(u.num)
|
|
|
|
} else {
|
|
|
|
print(u.num, "/", u.den)
|
|
|
|
}
|
2008-07-18 21:18:07 +00:00
|
|
|
print(" ")
|
|
|
|
}
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func (u rat) eq(c rat) bool {
|
2008-07-18 21:18:07 +00:00
|
|
|
return u.num == c.num && u.den == c.den
|
|
|
|
}
|
|
|
|
|
|
|
|
type dch struct {
|
2010-09-04 00:36:13 +00:00
|
|
|
req chan int
|
|
|
|
dat chan rat
|
|
|
|
nam int
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type dch2 [2] *dch
|
|
|
|
|
|
|
|
var chnames string
|
|
|
|
var chnameserial int
|
|
|
|
var seqno int
|
|
|
|
|
|
|
|
func mkdch() *dch {
|
2010-09-04 00:36:13 +00:00
|
|
|
c := chnameserial % len(chnames)
|
|
|
|
chnameserial++
|
|
|
|
d := new(dch)
|
|
|
|
d.req = make(chan int)
|
|
|
|
d.dat = make(chan rat)
|
|
|
|
d.nam = c
|
|
|
|
return d
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func mkdch2() *dch2 {
|
2010-09-04 00:36:13 +00:00
|
|
|
d2 := new(dch2)
|
|
|
|
d2[0] = mkdch()
|
|
|
|
d2[1] = mkdch()
|
|
|
|
return d2
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// split reads a single demand channel and replicates its
|
|
|
|
// output onto two, which may be read at different rates.
|
2009-02-06 23:03:14 +00:00
|
|
|
// A process is created at first demand for a rat and dies
|
|
|
|
// after the rat has been sent to both outputs.
|
2008-07-18 21:18:07 +00:00
|
|
|
|
|
|
|
// When multiple generations of split exist, the newest
|
|
|
|
// will service requests on one channel, which is
|
|
|
|
// always renamed to be out[0]; the oldest will service
|
|
|
|
// requests on the other channel, out[1]. All generations but the
|
|
|
|
// newest hold queued data that has already been sent to
|
|
|
|
// out[0]. When data has finally been sent to out[1],
|
|
|
|
// a signal on the release-wait channel tells the next newer
|
|
|
|
// generation to begin servicing out[1].
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func dosplit(in *dch, out *dch2, wait chan int ) {
|
2010-09-04 00:36:13 +00:00
|
|
|
both := false // do not service both channels
|
2008-07-28 19:03:56 +00:00
|
|
|
|
2008-07-18 21:18:07 +00:00
|
|
|
select {
|
|
|
|
case <-out[0].req:
|
2010-09-04 00:36:13 +00:00
|
|
|
|
2008-07-18 21:18:07 +00:00
|
|
|
case <-wait:
|
2010-09-04 00:36:13 +00:00
|
|
|
both = true
|
2008-07-18 21:18:07 +00:00
|
|
|
select {
|
|
|
|
case <-out[0].req:
|
2010-09-04 00:36:13 +00:00
|
|
|
|
2008-07-18 21:18:07 +00:00
|
|
|
case <-out[1].req:
|
2010-09-04 00:36:13 +00:00
|
|
|
out[0], out[1] = out[1], out[0]
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-04 00:36:13 +00:00
|
|
|
seqno++
|
|
|
|
in.req <- seqno
|
|
|
|
release := make(chan int)
|
|
|
|
go dosplit(in, out, release)
|
|
|
|
dat := <-in.dat
|
|
|
|
out[0].dat <- dat
|
2008-07-18 21:18:07 +00:00
|
|
|
if !both {
|
|
|
|
<-wait
|
|
|
|
}
|
2010-09-04 00:36:13 +00:00
|
|
|
<-out[1].req
|
|
|
|
out[1].dat <- dat
|
|
|
|
release <- 0
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func split(in *dch, out *dch2) {
|
2010-09-04 00:36:13 +00:00
|
|
|
release := make(chan int)
|
|
|
|
go dosplit(in, out, release)
|
|
|
|
release <- 0
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func put(dat rat, out *dch) {
|
2010-09-04 00:36:13 +00:00
|
|
|
<-out.req
|
|
|
|
out.dat <- dat
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func get(in *dch) rat {
|
2010-09-04 00:36:13 +00:00
|
|
|
seqno++
|
|
|
|
in.req <- seqno
|
|
|
|
return <-in.dat
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
// Get one rat from each of n demand channels
|
2008-07-18 21:18:07 +00:00
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func getn(in []*dch) []rat {
|
2010-09-04 00:36:13 +00:00
|
|
|
n := len(in)
|
|
|
|
if n != 2 { panic("bad n in getn") }
|
|
|
|
req := new([2] chan int)
|
|
|
|
dat := new([2] chan rat)
|
|
|
|
out := make([]rat, 2)
|
|
|
|
var i int
|
|
|
|
var it rat
|
2008-07-18 21:18:07 +00:00
|
|
|
for i=0; i<n; i++ {
|
2010-09-04 00:36:13 +00:00
|
|
|
req[i] = in[i].req
|
|
|
|
dat[i] = nil
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
for n=2*n; n>0; n-- {
|
2010-09-04 00:36:13 +00:00
|
|
|
seqno++
|
2008-07-28 19:03:56 +00:00
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
select {
|
2008-09-17 02:33:40 +00:00
|
|
|
case req[0] <- seqno:
|
2010-09-04 00:36:13 +00:00
|
|
|
dat[0] = in[0].dat
|
|
|
|
req[0] = nil
|
2008-09-17 02:33:40 +00:00
|
|
|
case req[1] <- seqno:
|
2010-09-04 00:36:13 +00:00
|
|
|
dat[1] = in[1].dat
|
|
|
|
req[1] = nil
|
2008-09-17 02:33:40 +00:00
|
|
|
case it = <-dat[0]:
|
2010-09-04 00:36:13 +00:00
|
|
|
out[0] = it
|
|
|
|
dat[0] = nil
|
2008-09-17 02:33:40 +00:00
|
|
|
case it = <-dat[1]:
|
2010-09-04 00:36:13 +00:00
|
|
|
out[1] = it
|
|
|
|
dat[1] = nil
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
}
|
2010-09-04 00:36:13 +00:00
|
|
|
return out
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
// Get one rat from each of 2 demand channels
|
2008-07-18 21:18:07 +00:00
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func get2(in0 *dch, in1 *dch) []rat {
|
2010-09-04 00:36:13 +00:00
|
|
|
return getn([]*dch{in0, in1})
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func copy(in *dch, out *dch) {
|
2008-07-18 21:18:07 +00:00
|
|
|
for {
|
2010-09-04 00:36:13 +00:00
|
|
|
<-out.req
|
|
|
|
out.dat <- get(in)
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func repeat(dat rat, out *dch) {
|
2008-07-18 21:18:07 +00:00
|
|
|
for {
|
|
|
|
put(dat, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-04 00:36:13 +00:00
|
|
|
type PS *dch // power series
|
|
|
|
type PS2 *[2] PS // pair of power series
|
2008-07-18 21:18:07 +00:00
|
|
|
|
2009-01-20 22:40:40 +00:00
|
|
|
var Ones PS
|
|
|
|
var Twos PS
|
2008-07-18 21:18:07 +00:00
|
|
|
|
|
|
|
func mkPS() *dch {
|
|
|
|
return mkdch()
|
|
|
|
}
|
|
|
|
|
|
|
|
func mkPS2() *dch2 {
|
|
|
|
return mkdch2()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Conventions
|
|
|
|
// Upper-case for power series.
|
|
|
|
// Lower-case for rationals.
|
|
|
|
// Input variables: U,V,...
|
|
|
|
// Output variables: ...,Y,Z
|
|
|
|
|
|
|
|
// Integer gcd; needed for rational arithmetic
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func gcd (u, v int64) int64 {
|
2008-07-18 21:18:07 +00:00
|
|
|
if u < 0 { return gcd(-u, v) }
|
|
|
|
if u == 0 { return v }
|
|
|
|
return gcd(v%u, u)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a rational from two ints and from one int
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func i2tor(u, v int64) rat {
|
2010-09-04 00:36:13 +00:00
|
|
|
g := gcd(u,v)
|
|
|
|
var r rat
|
2008-07-18 21:18:07 +00:00
|
|
|
if v > 0 {
|
2010-09-04 00:36:13 +00:00
|
|
|
r.num = u/g
|
|
|
|
r.den = v/g
|
2008-07-18 21:18:07 +00:00
|
|
|
} else {
|
2010-09-04 00:36:13 +00:00
|
|
|
r.num = -u/g
|
|
|
|
r.den = -v/g
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
2010-09-04 00:36:13 +00:00
|
|
|
return r
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func itor(u int64) rat {
|
2010-09-04 00:36:13 +00:00
|
|
|
return i2tor(u, 1)
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
2010-09-04 00:36:13 +00:00
|
|
|
var zero rat
|
|
|
|
var one rat
|
2008-07-18 21:18:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
// End mark and end test
|
|
|
|
|
2010-09-04 00:36:13 +00:00
|
|
|
var finis rat
|
2008-07-18 21:18:07 +00:00
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func end(u rat) int64 {
|
2008-07-18 21:18:07 +00:00
|
|
|
if u.den==0 { return 1 }
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Operations on rationals
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func add(u, v rat) rat {
|
2010-09-04 00:36:13 +00:00
|
|
|
g := gcd(u.den,v.den)
|
|
|
|
return i2tor(u.num*(v.den/g)+v.num*(u.den/g),u.den*(v.den/g))
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func mul(u, v rat) rat {
|
2010-09-04 00:36:13 +00:00
|
|
|
g1 := gcd(u.num,v.den)
|
|
|
|
g2 := gcd(u.den,v.num)
|
|
|
|
var r rat
|
|
|
|
r.num = (u.num/g1)*(v.num/g2)
|
|
|
|
r.den = (u.den/g2)*(v.den/g1)
|
|
|
|
return r
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func neg(u rat) rat {
|
2010-09-04 00:36:13 +00:00
|
|
|
return i2tor(-u.num, u.den)
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func sub(u, v rat) rat {
|
2010-09-04 00:36:13 +00:00
|
|
|
return add(u, neg(v))
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func inv(u rat) rat { // invert a rat
|
2008-08-12 05:07:49 +00:00
|
|
|
if u.num == 0 { panic("zero divide in inv") }
|
2010-09-04 00:36:13 +00:00
|
|
|
return i2tor(u.den, u.num)
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
2008-07-21 03:13:07 +00:00
|
|
|
// print eval in floating point of PS at x=c to n terms
|
2009-12-10 03:27:08 +00:00
|
|
|
func evaln(c rat, U PS, n int) {
|
2010-09-04 00:36:13 +00:00
|
|
|
xn := float64(1)
|
|
|
|
x := float64(c.num)/float64(c.den)
|
|
|
|
val := float64(0)
|
2008-07-21 03:13:07 +00:00
|
|
|
for i:=0; i<n; i++ {
|
2010-09-04 00:36:13 +00:00
|
|
|
u := get(U)
|
2008-07-21 03:13:07 +00:00
|
|
|
if end(u) != 0 {
|
2010-09-04 00:36:13 +00:00
|
|
|
break
|
2008-07-21 03:13:07 +00:00
|
|
|
}
|
2010-09-04 00:36:13 +00:00
|
|
|
val = val + x * float64(u.num)/float64(u.den)
|
|
|
|
xn = xn*x
|
2008-07-21 03:13:07 +00:00
|
|
|
}
|
2010-09-04 00:36:13 +00:00
|
|
|
print(val, "\n")
|
2008-07-21 03:13:07 +00:00
|
|
|
}
|
2008-07-18 21:18:07 +00:00
|
|
|
|
2008-07-21 03:13:07 +00:00
|
|
|
// Print n terms of a power series
|
2009-02-06 23:03:14 +00:00
|
|
|
func printn(U PS, n int) {
|
2010-09-04 00:36:13 +00:00
|
|
|
done := false
|
2008-07-18 21:18:07 +00:00
|
|
|
for ; !done && n>0; n-- {
|
2010-09-04 00:36:13 +00:00
|
|
|
u := get(U)
|
2009-12-10 03:27:08 +00:00
|
|
|
if end(u) != 0 {
|
|
|
|
done = true
|
|
|
|
} else {
|
|
|
|
u.pr()
|
|
|
|
}
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
2010-09-04 00:36:13 +00:00
|
|
|
print(("\n"))
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Evaluate n terms of power series U at x=c
|
2009-02-06 23:03:14 +00:00
|
|
|
func eval(c rat, U PS, n int) rat {
|
2008-07-18 21:18:07 +00:00
|
|
|
if n==0 { return zero }
|
2010-09-04 00:36:13 +00:00
|
|
|
y := get(U)
|
2008-07-18 21:18:07 +00:00
|
|
|
if end(y) != 0 { return zero }
|
2010-09-04 00:36:13 +00:00
|
|
|
return add(y,mul(c,eval(c,U,n-1)))
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Power-series constructors return channels on which power
|
|
|
|
// series flow. They start an encapsulated generator that
|
|
|
|
// puts the terms of the series on the channel.
|
|
|
|
|
|
|
|
// Make a pair of power series identical to a given power series
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func Split(U PS) *dch2 {
|
2010-09-04 00:36:13 +00:00
|
|
|
UU := mkdch2()
|
|
|
|
go split(U,UU)
|
|
|
|
return UU
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add two power series
|
2009-02-06 23:03:14 +00:00
|
|
|
func Add(U, V PS) PS {
|
2010-09-04 00:36:13 +00:00
|
|
|
Z := mkPS()
|
2009-02-06 23:03:14 +00:00
|
|
|
go func() {
|
2010-09-04 00:36:13 +00:00
|
|
|
var uv []rat
|
2008-07-18 21:18:07 +00:00
|
|
|
for {
|
2010-09-04 00:36:13 +00:00
|
|
|
<-Z.req
|
|
|
|
uv = get2(U,V)
|
2008-07-18 21:18:07 +00:00
|
|
|
switch end(uv[0])+2*end(uv[1]) {
|
|
|
|
case 0:
|
2010-09-04 00:36:13 +00:00
|
|
|
Z.dat <- add(uv[0], uv[1])
|
2008-07-18 21:18:07 +00:00
|
|
|
case 1:
|
2010-09-04 00:36:13 +00:00
|
|
|
Z.dat <- uv[1]
|
|
|
|
copy(V,Z)
|
2008-07-18 21:18:07 +00:00
|
|
|
case 2:
|
2010-09-04 00:36:13 +00:00
|
|
|
Z.dat <- uv[0]
|
|
|
|
copy(U,Z)
|
2008-07-18 21:18:07 +00:00
|
|
|
case 3:
|
2010-09-04 00:36:13 +00:00
|
|
|
Z.dat <- finis
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
}
|
2010-09-04 00:36:13 +00:00
|
|
|
}()
|
|
|
|
return Z
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Multiply a power series by a constant
|
2009-02-06 23:03:14 +00:00
|
|
|
func Cmul(c rat,U PS) PS {
|
2010-09-04 00:36:13 +00:00
|
|
|
Z := mkPS()
|
2009-02-06 23:03:14 +00:00
|
|
|
go func() {
|
2010-09-04 00:36:13 +00:00
|
|
|
done := false
|
2008-07-18 21:18:07 +00:00
|
|
|
for !done {
|
2010-09-04 00:36:13 +00:00
|
|
|
<-Z.req
|
|
|
|
u := get(U)
|
2009-12-10 03:27:08 +00:00
|
|
|
if end(u) != 0 {
|
|
|
|
done = true
|
|
|
|
} else {
|
|
|
|
Z.dat <- mul(c,u)
|
|
|
|
}
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
2010-09-04 00:36:13 +00:00
|
|
|
Z.dat <- finis
|
|
|
|
}()
|
|
|
|
return Z
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Subtract
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func Sub(U, V PS) PS {
|
2010-09-04 00:36:13 +00:00
|
|
|
return Add(U, Cmul(neg(one), V))
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Multiply a power series by the monomial x^n
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func Monmul(U PS, n int) PS {
|
2010-09-04 00:36:13 +00:00
|
|
|
Z := mkPS()
|
2009-02-06 23:03:14 +00:00
|
|
|
go func() {
|
2008-07-18 21:18:07 +00:00
|
|
|
for ; n>0; n-- { put(zero,Z) }
|
2010-09-04 00:36:13 +00:00
|
|
|
copy(U,Z)
|
|
|
|
}()
|
|
|
|
return Z
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Multiply by x
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func Xmul(U PS) PS {
|
2010-09-04 00:36:13 +00:00
|
|
|
return Monmul(U,1)
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func Rep(c rat) PS {
|
2010-09-04 00:36:13 +00:00
|
|
|
Z := mkPS()
|
|
|
|
go repeat(c,Z)
|
|
|
|
return Z
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Monomial c*x^n
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func Mon(c rat, n int) PS {
|
2010-09-04 00:36:13 +00:00
|
|
|
Z:=mkPS()
|
2009-02-06 23:03:14 +00:00
|
|
|
go func() {
|
2008-07-18 21:18:07 +00:00
|
|
|
if(c.num!=0) {
|
|
|
|
for ; n>0; n=n-1 { put(zero,Z) }
|
2010-09-04 00:36:13 +00:00
|
|
|
put(c,Z)
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
2010-09-04 00:36:13 +00:00
|
|
|
put(finis,Z)
|
|
|
|
}()
|
|
|
|
return Z
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func Shift(c rat, U PS) PS {
|
2010-09-04 00:36:13 +00:00
|
|
|
Z := mkPS()
|
2009-02-06 23:03:14 +00:00
|
|
|
go func() {
|
2010-09-04 00:36:13 +00:00
|
|
|
put(c,Z)
|
|
|
|
copy(U,Z)
|
|
|
|
}()
|
|
|
|
return Z
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// simple pole at 1: 1/(1-x) = 1 1 1 1 1 ...
|
|
|
|
|
|
|
|
// Convert array of coefficients, constant term first
|
|
|
|
// to a (finite) power series
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
/*
|
|
|
|
func Poly(a []rat) PS {
|
2010-09-04 00:36:13 +00:00
|
|
|
Z:=mkPS()
|
2009-02-06 23:03:14 +00:00
|
|
|
begin func(a []rat, Z PS) {
|
2010-09-04 00:36:13 +00:00
|
|
|
j:=0
|
|
|
|
done:=0
|
2008-10-05 05:11:26 +00:00
|
|
|
for j=len(a); !done&&j>0; j=j-1)
|
2010-09-04 00:36:13 +00:00
|
|
|
if(a[j-1].num!=0) done=1
|
|
|
|
i:=0
|
|
|
|
for(; i<j; i=i+1) put(a[i],Z)
|
|
|
|
put(finis,Z)
|
|
|
|
}()
|
|
|
|
return Z
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
2008-08-28 00:28:30 +00:00
|
|
|
*/
|
2008-07-18 21:18:07 +00:00
|
|
|
|
|
|
|
// Multiply. The algorithm is
|
2009-08-12 20:18:54 +00:00
|
|
|
// let U = u + x*UU
|
2008-07-18 21:18:07 +00:00
|
|
|
// let V = v + x*VV
|
|
|
|
// then UV = u*v + x*(u*VV+v*UU) + x*x*UU*VV
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func Mul(U, V PS) PS {
|
2010-09-04 00:36:13 +00:00
|
|
|
Z:=mkPS()
|
2009-02-06 23:03:14 +00:00
|
|
|
go func() {
|
2010-09-04 00:36:13 +00:00
|
|
|
<-Z.req
|
|
|
|
uv := get2(U,V)
|
2008-07-18 21:18:07 +00:00
|
|
|
if end(uv[0])!=0 || end(uv[1]) != 0 {
|
2010-09-04 00:36:13 +00:00
|
|
|
Z.dat <- finis
|
2008-07-18 21:18:07 +00:00
|
|
|
} else {
|
2010-09-04 00:36:13 +00:00
|
|
|
Z.dat <- mul(uv[0],uv[1])
|
|
|
|
UU := Split(U)
|
|
|
|
VV := Split(V)
|
|
|
|
W := Add(Cmul(uv[0],VV[0]),Cmul(uv[1],UU[0]))
|
|
|
|
<-Z.req
|
|
|
|
Z.dat <- get(W)
|
|
|
|
copy(Add(W,Mul(UU[1],VV[1])),Z)
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
2010-09-04 00:36:13 +00:00
|
|
|
}()
|
|
|
|
return Z
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Differentiate
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func Diff(U PS) PS {
|
2010-09-04 00:36:13 +00:00
|
|
|
Z:=mkPS()
|
2009-02-06 23:03:14 +00:00
|
|
|
go func() {
|
2010-09-04 00:36:13 +00:00
|
|
|
<-Z.req
|
|
|
|
u := get(U)
|
2008-07-18 21:18:07 +00:00
|
|
|
if end(u) == 0 {
|
2010-09-04 00:36:13 +00:00
|
|
|
done:=false
|
2008-07-18 21:18:07 +00:00
|
|
|
for i:=1; !done; i++ {
|
2010-09-04 00:36:13 +00:00
|
|
|
u = get(U)
|
2009-12-10 03:27:08 +00:00
|
|
|
if end(u) != 0 {
|
|
|
|
done = true
|
|
|
|
} else {
|
2010-09-04 00:36:13 +00:00
|
|
|
Z.dat <- mul(itor(int64(i)),u)
|
|
|
|
<-Z.req
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-09-04 00:36:13 +00:00
|
|
|
Z.dat <- finis
|
|
|
|
}()
|
|
|
|
return Z
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Integrate, with const of integration
|
2009-02-06 23:03:14 +00:00
|
|
|
func Integ(c rat,U PS) PS {
|
2010-09-04 00:36:13 +00:00
|
|
|
Z:=mkPS()
|
2009-02-06 23:03:14 +00:00
|
|
|
go func() {
|
2010-09-04 00:36:13 +00:00
|
|
|
put(c,Z)
|
|
|
|
done:=false
|
2008-07-18 21:18:07 +00:00
|
|
|
for i:=1; !done; i++ {
|
2010-09-04 00:36:13 +00:00
|
|
|
<-Z.req
|
|
|
|
u := get(U)
|
2008-07-18 21:18:07 +00:00
|
|
|
if end(u) != 0 { done= true }
|
2010-09-04 00:36:13 +00:00
|
|
|
Z.dat <- mul(i2tor(1,int64(i)),u)
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
2010-09-04 00:36:13 +00:00
|
|
|
Z.dat <- finis
|
|
|
|
}()
|
|
|
|
return Z
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Binomial theorem (1+x)^c
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func Binom(c rat) PS {
|
2010-09-04 00:36:13 +00:00
|
|
|
Z:=mkPS()
|
2009-02-06 23:03:14 +00:00
|
|
|
go func() {
|
2010-09-04 00:36:13 +00:00
|
|
|
n := 1
|
|
|
|
t := itor(1)
|
2008-07-18 21:18:07 +00:00
|
|
|
for c.num!=0 {
|
2010-09-04 00:36:13 +00:00
|
|
|
put(t,Z)
|
|
|
|
t = mul(mul(t,c),i2tor(1,int64(n)))
|
|
|
|
c = sub(c,one)
|
|
|
|
n++
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
2010-09-04 00:36:13 +00:00
|
|
|
put(finis,Z)
|
|
|
|
}()
|
|
|
|
return Z
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reciprocal of a power series
|
|
|
|
// let U = u + x*UU
|
|
|
|
// let Z = z + x*ZZ
|
|
|
|
// (u+x*UU)*(z+x*ZZ) = 1
|
|
|
|
// z = 1/u
|
|
|
|
// u*ZZ + z*UU +x*UU*ZZ = 0
|
2010-09-04 00:36:13 +00:00
|
|
|
// ZZ = -UU*(z+x*ZZ)/u
|
2008-07-18 21:18:07 +00:00
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func Recip(U PS) PS {
|
2010-09-04 00:36:13 +00:00
|
|
|
Z:=mkPS()
|
2009-02-06 23:03:14 +00:00
|
|
|
go func() {
|
2010-09-04 00:36:13 +00:00
|
|
|
ZZ:=mkPS2()
|
|
|
|
<-Z.req
|
|
|
|
z := inv(get(U))
|
|
|
|
Z.dat <- z
|
|
|
|
split(Mul(Cmul(neg(z),U),Shift(z,ZZ[0])),ZZ)
|
|
|
|
copy(ZZ[1],Z)
|
|
|
|
}()
|
|
|
|
return Z
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Exponential of a power series with constant term 0
|
|
|
|
// (nonzero constant term would make nonrational coefficients)
|
|
|
|
// bug: the constant term is simply ignored
|
|
|
|
// Z = exp(U)
|
|
|
|
// DZ = Z*DU
|
|
|
|
// integrate to get Z
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func Exp(U PS) PS {
|
2010-09-04 00:36:13 +00:00
|
|
|
ZZ := mkPS2()
|
|
|
|
split(Integ(one,Mul(ZZ[0],Diff(U))),ZZ)
|
|
|
|
return ZZ[1]
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Substitute V for x in U, where the leading term of V is zero
|
|
|
|
// let U = u + x*UU
|
|
|
|
// let V = v + x*VV
|
2009-08-12 20:18:54 +00:00
|
|
|
// then S(U,V) = u + VV*S(V,UU)
|
2008-07-18 21:18:07 +00:00
|
|
|
// bug: a nonzero constant term is ignored
|
|
|
|
|
2009-01-20 22:40:40 +00:00
|
|
|
func Subst(U, V PS) PS {
|
2010-09-04 00:36:13 +00:00
|
|
|
Z:= mkPS()
|
2009-02-06 23:03:14 +00:00
|
|
|
go func() {
|
2010-09-04 00:36:13 +00:00
|
|
|
VV := Split(V)
|
|
|
|
<-Z.req
|
|
|
|
u := get(U)
|
|
|
|
Z.dat <- u
|
2008-07-18 21:18:07 +00:00
|
|
|
if end(u) == 0 {
|
2009-12-10 03:27:08 +00:00
|
|
|
if end(get(VV[0])) != 0 {
|
2010-09-04 00:36:13 +00:00
|
|
|
put(finis,Z)
|
2009-12-10 03:27:08 +00:00
|
|
|
} else {
|
2010-09-04 00:36:13 +00:00
|
|
|
copy(Mul(VV[0],Subst(U,VV[1])),Z)
|
2009-12-10 03:27:08 +00:00
|
|
|
}
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
2010-09-04 00:36:13 +00:00
|
|
|
}()
|
|
|
|
return Z
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Monomial Substition: U(c x^n)
|
|
|
|
// Each Ui is multiplied by c^i and followed by n-1 zeros
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func MonSubst(U PS, c0 rat, n int) PS {
|
2010-09-04 00:36:13 +00:00
|
|
|
Z:= mkPS()
|
2009-02-06 23:03:14 +00:00
|
|
|
go func() {
|
2010-09-04 00:36:13 +00:00
|
|
|
c := one
|
2008-07-18 21:18:07 +00:00
|
|
|
for {
|
2010-09-04 00:36:13 +00:00
|
|
|
<-Z.req
|
|
|
|
u := get(U)
|
|
|
|
Z.dat <- mul(u, c)
|
|
|
|
c = mul(c, c0)
|
2008-07-18 21:18:07 +00:00
|
|
|
if end(u) != 0 {
|
2010-09-04 00:36:13 +00:00
|
|
|
Z.dat <- finis
|
|
|
|
break
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
for i := 1; i < n; i++ {
|
2010-09-04 00:36:13 +00:00
|
|
|
<-Z.req
|
|
|
|
Z.dat <- zero
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
}
|
2010-09-04 00:36:13 +00:00
|
|
|
}()
|
|
|
|
return Z
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-20 22:40:40 +00:00
|
|
|
func Init() {
|
2010-09-04 00:36:13 +00:00
|
|
|
chnameserial = -1
|
|
|
|
seqno = 0
|
|
|
|
chnames = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
|
|
zero = itor(0)
|
|
|
|
one = itor(1)
|
|
|
|
finis = i2tor(1,0)
|
|
|
|
Ones = Rep(one)
|
|
|
|
Twos = Rep(itor(2))
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
|
2009-02-06 23:03:14 +00:00
|
|
|
func check(U PS, c rat, count int, str string) {
|
2008-07-18 21:18:07 +00:00
|
|
|
for i := 0; i < count; i++ {
|
2010-09-04 00:36:13 +00:00
|
|
|
r := get(U)
|
2008-07-18 21:18:07 +00:00
|
|
|
if !r.eq(c) {
|
2010-09-04 00:36:13 +00:00
|
|
|
print("got: ")
|
|
|
|
r.pr()
|
|
|
|
print("should get ")
|
|
|
|
c.pr()
|
|
|
|
print("\n")
|
2008-08-12 05:07:49 +00:00
|
|
|
panic(str)
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-20 22:40:40 +00:00
|
|
|
const N=10
|
2009-02-06 23:03:14 +00:00
|
|
|
func checka(U PS, a []rat, str string) {
|
2008-08-05 15:20:34 +00:00
|
|
|
for i := 0; i < N; i++ {
|
2010-09-04 00:36:13 +00:00
|
|
|
check(U, a[i], 1, str)
|
2008-07-18 21:18:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2010-09-04 00:36:13 +00:00
|
|
|
Init()
|
2009-05-08 22:21:41 +00:00
|
|
|
if len(os.Args) > 1 { // print
|
2010-09-04 00:36:13 +00:00
|
|
|
print("Ones: "); printn(Ones, 10)
|
|
|
|
print("Twos: "); printn(Twos, 10)
|
|
|
|
print("Add: "); printn(Add(Ones, Twos), 10)
|
|
|
|
print("Diff: "); printn(Diff(Ones), 10)
|
|
|
|
print("Integ: "); printn(Integ(zero, Ones), 10)
|
|
|
|
print("CMul: "); printn(Cmul(neg(one), Ones), 10)
|
|
|
|
print("Sub: "); printn(Sub(Ones, Twos), 10)
|
|
|
|
print("Mul: "); printn(Mul(Ones, Ones), 10)
|
|
|
|
print("Exp: "); printn(Exp(Ones), 15)
|
|
|
|
print("MonSubst: "); printn(MonSubst(Ones, neg(one), 2), 10)
|
|
|
|
print("ATan: "); printn(Integ(zero, MonSubst(Ones, neg(one), 2)), 10)
|
2008-07-18 21:18:07 +00:00
|
|
|
} else { // test
|
2010-09-04 00:36:13 +00:00
|
|
|
check(Ones, one, 5, "Ones")
|
|
|
|
check(Add(Ones, Ones), itor(2), 0, "Add Ones Ones") // 1 1 1 1 1
|
|
|
|
check(Add(Ones, Twos), itor(3), 0, "Add Ones Twos") // 3 3 3 3 3
|
|
|
|
a := make([]rat, N)
|
|
|
|
d := Diff(Ones)
|
2008-07-18 21:18:07 +00:00
|
|
|
for i:=0; i < N; i++ {
|
|
|
|
a[i] = itor(int64(i+1))
|
|
|
|
}
|
2010-09-04 00:36:13 +00:00
|
|
|
checka(d, a, "Diff") // 1 2 3 4 5
|
|
|
|
in := Integ(zero, Ones)
|
|
|
|
a[0] = zero // integration constant
|
2008-07-18 21:18:07 +00:00
|
|
|
for i:=1; i < N; i++ {
|
|
|
|
a[i] = i2tor(1, int64(i))
|
|
|
|
}
|
2010-09-04 00:36:13 +00:00
|
|
|
checka(in, a, "Integ") // 0 1 1/2 1/3 1/4 1/5
|
|
|
|
check(Cmul(neg(one), Twos), itor(-2), 10, "CMul") // -1 -1 -1 -1 -1
|
|
|
|
check(Sub(Ones, Twos), itor(-1), 0, "Sub Ones Twos") // -1 -1 -1 -1 -1
|
|
|
|
m := Mul(Ones, Ones)
|
2008-07-18 21:18:07 +00:00
|
|
|
for i:=0; i < N; i++ {
|
|
|
|
a[i] = itor(int64(i+1))
|
|
|
|
}
|
2010-09-04 00:36:13 +00:00
|
|
|
checka(m, a, "Mul") // 1 2 3 4 5
|
|
|
|
e := Exp(Ones)
|
|
|
|
a[0] = itor(1)
|
|
|
|
a[1] = itor(1)
|
|
|
|
a[2] = i2tor(3,2)
|
|
|
|
a[3] = i2tor(13,6)
|
|
|
|
a[4] = i2tor(73,24)
|
|
|
|
a[5] = i2tor(167,40)
|
|
|
|
a[6] = i2tor(4051,720)
|
|
|
|
a[7] = i2tor(37633,5040)
|
|
|
|
a[8] = i2tor(43817,4480)
|
|
|
|
a[9] = i2tor(4596553,362880)
|
|
|
|
checka(e, a, "Exp") // 1 1 3/2 13/6 73/24
|
|
|
|
at := Integ(zero, MonSubst(Ones, neg(one), 2))
|
2008-07-18 21:18:07 +00:00
|
|
|
for c, i := 1, 0; i < N; i++ {
|
|
|
|
if i%2 == 0 {
|
|
|
|
a[i] = zero
|
|
|
|
} else {
|
2010-09-04 00:36:13 +00:00
|
|
|
a[i] = i2tor(int64(c), int64(i))
|
2008-07-18 21:18:07 +00:00
|
|
|
c *= -1
|
|
|
|
}
|
|
|
|
}
|
2010-09-04 00:36:13 +00:00
|
|
|
checka(at, a, "ATan") // 0 -1 0 -1/3 0 -1/5
|
2008-07-18 21:18:07 +00:00
|
|
|
/*
|
2010-09-04 00:36:13 +00:00
|
|
|
t := Revert(Integ(zero, MonSubst(Ones, neg(one), 2)))
|
|
|
|
a[0] = zero
|
|
|
|
a[1] = itor(1)
|
|
|
|
a[2] = zero
|
|
|
|
a[3] = i2tor(1,3)
|
|
|
|
a[4] = zero
|
|
|
|
a[5] = i2tor(2,15)
|
|
|
|
a[6] = zero
|
|
|
|
a[7] = i2tor(17,315)
|
|
|
|
a[8] = zero
|
|
|
|
a[9] = i2tor(62,2835)
|
|
|
|
checka(t, a, "Tan") // 0 1 0 1/3 0 2/15
|
2008-07-18 21:18:07 +00:00
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|