Commit 26a85211 authored by Josh Bleecher Snyder's avatar Josh Bleecher Snyder Committed by Brad Fitzpatrick

test: gofmt chan/*.go

These are functional tests, so it is safe to gofmt them.

Change-Id: I3067279c1d49809ac6a62054448ab8a6c3de9bda
Reviewed-on: https://go-review.googlesource.com/43623Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent a9bf3b2e
...@@ -54,4 +54,3 @@ func main() { ...@@ -54,4 +54,3 @@ func main() {
AsynchFifo() AsynchFifo()
SynchFifo() SynchFifo()
} }
...@@ -28,19 +28,19 @@ func main() { ...@@ -28,19 +28,19 @@ func main() {
<-n // ERROR "receive from non-chan" <-n // ERROR "receive from non-chan"
n <- 2 // ERROR "send to non-chan" n <- 2 // ERROR "send to non-chan"
c <- 0 // ok c <- 0 // ok
<-c // ok <-c // ok
x, ok := <-c // ok x, ok := <-c // ok
_, _ = x, ok _, _ = x, ok
cr <- 0 // ERROR "send" cr <- 0 // ERROR "send"
<-cr // ok <-cr // ok
x, ok = <-cr // ok x, ok = <-cr // ok
_, _ = x, ok _, _ = x, ok
cs <- 0 // ok cs <- 0 // ok
<-cs // ERROR "receive" <-cs // ERROR "receive"
x, ok = <-cs // ERROR "receive" x, ok = <-cs // ERROR "receive"
_, _ = x, ok _, _ = x, ok
select { select {
...@@ -57,14 +57,14 @@ func main() { ...@@ -57,14 +57,14 @@ func main() {
_ = x _ = x
} }
for _ = range cs {// ERROR "receive" for _ = range cs { // ERROR "receive"
} }
for range cs {// ERROR "receive" for range cs { // ERROR "receive"
} }
close(c) close(c)
close(cs) close(cs)
close(cr) // ERROR "receive" close(cr) // ERROR "receive"
close(n) // ERROR "invalid operation.*non-chan type" close(n) // ERROR "invalid operation.*non-chan type"
} }
...@@ -17,12 +17,12 @@ package main ...@@ -17,12 +17,12 @@ package main
import "os" import "os"
type rat struct { type rat struct {
num, den int64 // numerator, denominator num, den int64 // numerator, denominator
} }
func (u rat) pr() { func (u rat) pr() {
if u.den==1 { if u.den == 1 {
print(u.num) print(u.num)
} else { } else {
print(u.num, "/", u.den) print(u.num, "/", u.den)
...@@ -35,12 +35,12 @@ func (u rat) eq(c rat) bool { ...@@ -35,12 +35,12 @@ func (u rat) eq(c rat) bool {
} }
type dch struct { type dch struct {
req chan int req chan int
dat chan rat dat chan rat
nam int nam int
} }
type dch2 [2] *dch type dch2 [2]*dch
var chnames string var chnames string
var chnameserial int var chnameserial int
...@@ -77,17 +77,17 @@ func mkdch2() *dch2 { ...@@ -77,17 +77,17 @@ func mkdch2() *dch2 {
// a signal on the release-wait channel tells the next newer // a signal on the release-wait channel tells the next newer
// generation to begin servicing out[1]. // generation to begin servicing out[1].
func dosplit(in *dch, out *dch2, wait chan int ) { func dosplit(in *dch, out *dch2, wait chan int) {
both := false // do not service both channels both := false // do not service both channels
select { select {
case <-out[0].req: case <-out[0].req:
case <-wait: case <-wait:
both = true both = true
select { select {
case <-out[0].req: case <-out[0].req:
case <-out[1].req: case <-out[1].req:
out[0], out[1] = out[1], out[0] out[0], out[1] = out[1], out[0]
} }
...@@ -95,7 +95,7 @@ func dosplit(in *dch, out *dch2, wait chan int ) { ...@@ -95,7 +95,7 @@ func dosplit(in *dch, out *dch2, wait chan int ) {
seqno++ seqno++
in.req <- seqno in.req <- seqno
release := make(chan int) release := make(chan int)
go dosplit(in, out, release) go dosplit(in, out, release)
dat := <-in.dat dat := <-in.dat
out[0].dat <- dat out[0].dat <- dat
...@@ -128,17 +128,19 @@ func get(in *dch) rat { ...@@ -128,17 +128,19 @@ func get(in *dch) rat {
func getn(in []*dch) []rat { func getn(in []*dch) []rat {
n := len(in) n := len(in)
if n != 2 { panic("bad n in getn") } if n != 2 {
req := new([2] chan int) panic("bad n in getn")
dat := new([2] chan rat) }
req := new([2]chan int)
dat := new([2]chan rat)
out := make([]rat, 2) out := make([]rat, 2)
var i int var i int
var it rat var it rat
for i=0; i<n; i++ { for i = 0; i < n; i++ {
req[i] = in[i].req req[i] = in[i].req
dat[i] = nil dat[i] = nil
} }
for n=2*n; n>0; n-- { for n = 2 * n; n > 0; n-- {
seqno++ seqno++
select { select {
...@@ -178,8 +180,8 @@ func repeat(dat rat, out *dch) { ...@@ -178,8 +180,8 @@ func repeat(dat rat, out *dch) {
} }
} }
type PS *dch // power series type PS *dch // power series
type PS2 *[2] PS // pair of power series type PS2 *[2]PS // pair of power series
var Ones PS var Ones PS
var Twos PS var Twos PS
...@@ -200,23 +202,27 @@ func mkPS2() *dch2 { ...@@ -200,23 +202,27 @@ func mkPS2() *dch2 {
// Integer gcd; needed for rational arithmetic // Integer gcd; needed for rational arithmetic
func gcd (u, v int64) int64 { func gcd(u, v int64) int64 {
if u < 0 { return gcd(-u, v) } if u < 0 {
if u == 0 { return v } return gcd(-u, v)
}
if u == 0 {
return v
}
return gcd(v%u, u) return gcd(v%u, u)
} }
// Make a rational from two ints and from one int // Make a rational from two ints and from one int
func i2tor(u, v int64) rat { func i2tor(u, v int64) rat {
g := gcd(u,v) g := gcd(u, v)
var r rat var r rat
if v > 0 { if v > 0 {
r.num = u/g r.num = u / g
r.den = v/g r.den = v / g
} else { } else {
r.num = -u/g r.num = -u / g
r.den = -v/g r.den = -v / g
} }
return r return r
} }
...@@ -228,29 +234,30 @@ func itor(u int64) rat { ...@@ -228,29 +234,30 @@ func itor(u int64) rat {
var zero rat var zero rat
var one rat var one rat
// End mark and end test // End mark and end test
var finis rat var finis rat
func end(u rat) int64 { func end(u rat) int64 {
if u.den==0 { return 1 } if u.den == 0 {
return 1
}
return 0 return 0
} }
// Operations on rationals // Operations on rationals
func add(u, v rat) rat { func add(u, v rat) rat {
g := gcd(u.den,v.den) g := gcd(u.den, v.den)
return i2tor(u.num*(v.den/g)+v.num*(u.den/g),u.den*(v.den/g)) return i2tor(u.num*(v.den/g)+v.num*(u.den/g), u.den*(v.den/g))
} }
func mul(u, v rat) rat { func mul(u, v rat) rat {
g1 := gcd(u.num,v.den) g1 := gcd(u.num, v.den)
g2 := gcd(u.den,v.num) g2 := gcd(u.den, v.num)
var r rat var r rat
r.num = (u.num/g1)*(v.num/g2) r.num = (u.num / g1) * (v.num / g2)
r.den = (u.den/g2)*(v.den/g1) r.den = (u.den / g2) * (v.den / g1)
return r return r
} }
...@@ -262,23 +269,25 @@ func sub(u, v rat) rat { ...@@ -262,23 +269,25 @@ func sub(u, v rat) rat {
return add(u, neg(v)) return add(u, neg(v))
} }
func inv(u rat) rat { // invert a rat func inv(u rat) rat { // invert a rat
if u.num == 0 { panic("zero divide in inv") } if u.num == 0 {
panic("zero divide in inv")
}
return i2tor(u.den, u.num) return i2tor(u.den, u.num)
} }
// print eval in floating point of PS at x=c to n terms // print eval in floating point of PS at x=c to n terms
func evaln(c rat, U PS, n int) { func evaln(c rat, U PS, n int) {
xn := float64(1) xn := float64(1)
x := float64(c.num)/float64(c.den) x := float64(c.num) / float64(c.den)
val := float64(0) val := float64(0)
for i:=0; i<n; i++ { for i := 0; i < n; i++ {
u := get(U) u := get(U)
if end(u) != 0 { if end(u) != 0 {
break break
} }
val = val + x * float64(u.num)/float64(u.den) val = val + x*float64(u.num)/float64(u.den)
xn = xn*x xn = xn * x
} }
print(val, "\n") print(val, "\n")
} }
...@@ -286,7 +295,7 @@ func evaln(c rat, U PS, n int) { ...@@ -286,7 +295,7 @@ func evaln(c rat, U PS, n int) {
// Print n terms of a power series // Print n terms of a power series
func printn(U PS, n int) { func printn(U PS, n int) {
done := false done := false
for ; !done && n>0; n-- { for ; !done && n > 0; n-- {
u := get(U) u := get(U)
if end(u) != 0 { if end(u) != 0 {
done = true done = true
...@@ -299,10 +308,14 @@ func printn(U PS, n int) { ...@@ -299,10 +308,14 @@ func printn(U PS, n int) {
// Evaluate n terms of power series U at x=c // Evaluate n terms of power series U at x=c
func eval(c rat, U PS, n int) rat { func eval(c rat, U PS, n int) rat {
if n==0 { return zero } if n == 0 {
return zero
}
y := get(U) y := get(U)
if end(y) != 0 { return zero } if end(y) != 0 {
return add(y,mul(c,eval(c,U,n-1))) return zero
}
return add(y, mul(c, eval(c, U, n-1)))
} }
// Power-series constructors return channels on which power // Power-series constructors return channels on which power
...@@ -313,7 +326,7 @@ func eval(c rat, U PS, n int) rat { ...@@ -313,7 +326,7 @@ func eval(c rat, U PS, n int) rat {
func Split(U PS) *dch2 { func Split(U PS) *dch2 {
UU := mkdch2() UU := mkdch2()
go split(U,UU) go split(U, UU)
return UU return UU
} }
...@@ -324,16 +337,16 @@ func Add(U, V PS) PS { ...@@ -324,16 +337,16 @@ func Add(U, V PS) PS {
var uv []rat var uv []rat
for { for {
<-Z.req <-Z.req
uv = get2(U,V) uv = get2(U, V)
switch end(uv[0])+2*end(uv[1]) { switch end(uv[0]) + 2*end(uv[1]) {
case 0: case 0:
Z.dat <- add(uv[0], uv[1]) Z.dat <- add(uv[0], uv[1])
case 1: case 1:
Z.dat <- uv[1] Z.dat <- uv[1]
copy(V,Z) copy(V, Z)
case 2: case 2:
Z.dat <- uv[0] Z.dat <- uv[0]
copy(U,Z) copy(U, Z)
case 3: case 3:
Z.dat <- finis Z.dat <- finis
} }
...@@ -343,7 +356,7 @@ func Add(U, V PS) PS { ...@@ -343,7 +356,7 @@ func Add(U, V PS) PS {
} }
// Multiply a power series by a constant // Multiply a power series by a constant
func Cmul(c rat,U PS) PS { func Cmul(c rat, U PS) PS {
Z := mkPS() Z := mkPS()
go func() { go func() {
done := false done := false
...@@ -353,7 +366,7 @@ func Cmul(c rat,U PS) PS { ...@@ -353,7 +366,7 @@ func Cmul(c rat,U PS) PS {
if end(u) != 0 { if end(u) != 0 {
done = true done = true
} else { } else {
Z.dat <- mul(c,u) Z.dat <- mul(c, u)
} }
} }
Z.dat <- finis Z.dat <- finis
...@@ -372,8 +385,10 @@ func Sub(U, V PS) PS { ...@@ -372,8 +385,10 @@ func Sub(U, V PS) PS {
func Monmul(U PS, n int) PS { func Monmul(U PS, n int) PS {
Z := mkPS() Z := mkPS()
go func() { go func() {
for ; n>0; n-- { put(zero,Z) } for ; n > 0; n-- {
copy(U,Z) put(zero, Z)
}
copy(U, Z)
}() }()
return Z return Z
} }
...@@ -381,25 +396,27 @@ func Monmul(U PS, n int) PS { ...@@ -381,25 +396,27 @@ func Monmul(U PS, n int) PS {
// Multiply by x // Multiply by x
func Xmul(U PS) PS { func Xmul(U PS) PS {
return Monmul(U,1) return Monmul(U, 1)
} }
func Rep(c rat) PS { func Rep(c rat) PS {
Z := mkPS() Z := mkPS()
go repeat(c,Z) go repeat(c, Z)
return Z return Z
} }
// Monomial c*x^n // Monomial c*x^n
func Mon(c rat, n int) PS { func Mon(c rat, n int) PS {
Z:=mkPS() Z := mkPS()
go func() { go func() {
if(c.num!=0) { if c.num != 0 {
for ; n>0; n=n-1 { put(zero,Z) } for ; n > 0; n = n - 1 {
put(c,Z) put(zero, Z)
}
put(c, Z)
} }
put(finis,Z) put(finis, Z)
}() }()
return Z return Z
} }
...@@ -407,8 +424,8 @@ func Mon(c rat, n int) PS { ...@@ -407,8 +424,8 @@ func Mon(c rat, n int) PS {
func Shift(c rat, U PS) PS { func Shift(c rat, U PS) PS {
Z := mkPS() Z := mkPS()
go func() { go func() {
put(c,Z) put(c, Z)
copy(U,Z) copy(U, Z)
}() }()
return Z return Z
} }
...@@ -440,20 +457,20 @@ func Poly(a []rat) PS { ...@@ -440,20 +457,20 @@ func Poly(a []rat) PS {
// then UV = u*v + x*(u*VV+v*UU) + x*x*UU*VV // then UV = u*v + x*(u*VV+v*UU) + x*x*UU*VV
func Mul(U, V PS) PS { func Mul(U, V PS) PS {
Z:=mkPS() Z := mkPS()
go func() { go func() {
<-Z.req <-Z.req
uv := get2(U,V) uv := get2(U, V)
if end(uv[0])!=0 || end(uv[1]) != 0 { if end(uv[0]) != 0 || end(uv[1]) != 0 {
Z.dat <- finis Z.dat <- finis
} else { } else {
Z.dat <- mul(uv[0],uv[1]) Z.dat <- mul(uv[0], uv[1])
UU := Split(U) UU := Split(U)
VV := Split(V) VV := Split(V)
W := Add(Cmul(uv[0],VV[0]),Cmul(uv[1],UU[0])) W := Add(Cmul(uv[0], VV[0]), Cmul(uv[1], UU[0]))
<-Z.req <-Z.req
Z.dat <- get(W) Z.dat <- get(W)
copy(Add(W,Mul(UU[1],VV[1])),Z) copy(Add(W, Mul(UU[1], VV[1])), Z)
} }
}() }()
return Z return Z
...@@ -462,18 +479,18 @@ func Mul(U, V PS) PS { ...@@ -462,18 +479,18 @@ func Mul(U, V PS) PS {
// Differentiate // Differentiate
func Diff(U PS) PS { func Diff(U PS) PS {
Z:=mkPS() Z := mkPS()
go func() { go func() {
<-Z.req <-Z.req
u := get(U) u := get(U)
if end(u) == 0 { if end(u) == 0 {
done:=false done := false
for i:=1; !done; i++ { for i := 1; !done; i++ {
u = get(U) u = get(U)
if end(u) != 0 { if end(u) != 0 {
done = true done = true
} else { } else {
Z.dat <- mul(itor(int64(i)),u) Z.dat <- mul(itor(int64(i)), u)
<-Z.req <-Z.req
} }
} }
...@@ -484,16 +501,18 @@ func Diff(U PS) PS { ...@@ -484,16 +501,18 @@ func Diff(U PS) PS {
} }
// Integrate, with const of integration // Integrate, with const of integration
func Integ(c rat,U PS) PS { func Integ(c rat, U PS) PS {
Z:=mkPS() Z := mkPS()
go func() { go func() {
put(c,Z) put(c, Z)
done:=false done := false
for i:=1; !done; i++ { for i := 1; !done; i++ {
<-Z.req <-Z.req
u := get(U) u := get(U)
if end(u) != 0 { done= true } if end(u) != 0 {
Z.dat <- mul(i2tor(1,int64(i)),u) done = true
}
Z.dat <- mul(i2tor(1, int64(i)), u)
} }
Z.dat <- finis Z.dat <- finis
}() }()
...@@ -503,17 +522,17 @@ func Integ(c rat,U PS) PS { ...@@ -503,17 +522,17 @@ func Integ(c rat,U PS) PS {
// Binomial theorem (1+x)^c // Binomial theorem (1+x)^c
func Binom(c rat) PS { func Binom(c rat) PS {
Z:=mkPS() Z := mkPS()
go func() { go func() {
n := 1 n := 1
t := itor(1) t := itor(1)
for c.num!=0 { for c.num != 0 {
put(t,Z) put(t, Z)
t = mul(mul(t,c),i2tor(1,int64(n))) t = mul(mul(t, c), i2tor(1, int64(n)))
c = sub(c,one) c = sub(c, one)
n++ n++
} }
put(finis,Z) put(finis, Z)
}() }()
return Z return Z
} }
...@@ -527,14 +546,14 @@ func Binom(c rat) PS { ...@@ -527,14 +546,14 @@ func Binom(c rat) PS {
// ZZ = -UU*(z+x*ZZ)/u // ZZ = -UU*(z+x*ZZ)/u
func Recip(U PS) PS { func Recip(U PS) PS {
Z:=mkPS() Z := mkPS()
go func() { go func() {
ZZ:=mkPS2() ZZ := mkPS2()
<-Z.req <-Z.req
z := inv(get(U)) z := inv(get(U))
Z.dat <- z Z.dat <- z
split(Mul(Cmul(neg(z),U),Shift(z,ZZ[0])),ZZ) split(Mul(Cmul(neg(z), U), Shift(z, ZZ[0])), ZZ)
copy(ZZ[1],Z) copy(ZZ[1], Z)
}() }()
return Z return Z
} }
...@@ -548,7 +567,7 @@ func Recip(U PS) PS { ...@@ -548,7 +567,7 @@ func Recip(U PS) PS {
func Exp(U PS) PS { func Exp(U PS) PS {
ZZ := mkPS2() ZZ := mkPS2()
split(Integ(one,Mul(ZZ[0],Diff(U))),ZZ) split(Integ(one, Mul(ZZ[0], Diff(U))), ZZ)
return ZZ[1] return ZZ[1]
} }
...@@ -559,7 +578,7 @@ func Exp(U PS) PS { ...@@ -559,7 +578,7 @@ func Exp(U PS) PS {
// bug: a nonzero constant term is ignored // bug: a nonzero constant term is ignored
func Subst(U, V PS) PS { func Subst(U, V PS) PS {
Z:= mkPS() Z := mkPS()
go func() { go func() {
VV := Split(V) VV := Split(V)
<-Z.req <-Z.req
...@@ -567,9 +586,9 @@ func Subst(U, V PS) PS { ...@@ -567,9 +586,9 @@ func Subst(U, V PS) PS {
Z.dat <- u Z.dat <- u
if end(u) == 0 { if end(u) == 0 {
if end(get(VV[0])) != 0 { if end(get(VV[0])) != 0 {
put(finis,Z) put(finis, Z)
} else { } else {
copy(Mul(VV[0],Subst(U,VV[1])),Z) copy(Mul(VV[0], Subst(U, VV[1])), Z)
} }
} }
}() }()
...@@ -580,7 +599,7 @@ func Subst(U, V PS) PS { ...@@ -580,7 +599,7 @@ func Subst(U, V PS) PS {
// Each Ui is multiplied by c^i and followed by n-1 zeros // Each Ui is multiplied by c^i and followed by n-1 zeros
func MonSubst(U PS, c0 rat, n int) PS { func MonSubst(U PS, c0 rat, n int) PS {
Z:= mkPS() Z := mkPS()
go func() { go func() {
c := one c := one
for { for {
...@@ -601,14 +620,13 @@ func MonSubst(U PS, c0 rat, n int) PS { ...@@ -601,14 +620,13 @@ func MonSubst(U PS, c0 rat, n int) PS {
return Z return Z
} }
func Init() { func Init() {
chnameserial = -1 chnameserial = -1
seqno = 0 seqno = 0
chnames = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" chnames = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
zero = itor(0) zero = itor(0)
one = itor(1) one = itor(1)
finis = i2tor(1,0) finis = i2tor(1, 0)
Ones = Rep(one) Ones = Rep(one)
Twos = Rep(itor(2)) Twos = Rep(itor(2))
} }
...@@ -627,7 +645,8 @@ func check(U PS, c rat, count int, str string) { ...@@ -627,7 +645,8 @@ func check(U PS, c rat, count int, str string) {
} }
} }
const N=10 const N = 10
func checka(U PS, a []rat, str string) { func checka(U PS, a []rat, str string) {
for i := 0; i < N; i++ { for i := 0; i < N; i++ {
check(U, a[i], 1, str) check(U, a[i], 1, str)
...@@ -636,53 +655,64 @@ func checka(U PS, a []rat, str string) { ...@@ -636,53 +655,64 @@ func checka(U PS, a []rat, str string) {
func main() { func main() {
Init() Init()
if len(os.Args) > 1 { // print if len(os.Args) > 1 { // print
print("Ones: "); printn(Ones, 10) print("Ones: ")
print("Twos: "); printn(Twos, 10) printn(Ones, 10)
print("Add: "); printn(Add(Ones, Twos), 10) print("Twos: ")
print("Diff: "); printn(Diff(Ones), 10) printn(Twos, 10)
print("Integ: "); printn(Integ(zero, Ones), 10) print("Add: ")
print("CMul: "); printn(Cmul(neg(one), Ones), 10) printn(Add(Ones, Twos), 10)
print("Sub: "); printn(Sub(Ones, Twos), 10) print("Diff: ")
print("Mul: "); printn(Mul(Ones, Ones), 10) printn(Diff(Ones), 10)
print("Exp: "); printn(Exp(Ones), 15) print("Integ: ")
print("MonSubst: "); printn(MonSubst(Ones, neg(one), 2), 10) printn(Integ(zero, Ones), 10)
print("ATan: "); printn(Integ(zero, MonSubst(Ones, neg(one), 2)), 10) print("CMul: ")
} else { // test 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)
} else { // test
check(Ones, one, 5, "Ones") check(Ones, one, 5, "Ones")
check(Add(Ones, Ones), itor(2), 0, "Add Ones Ones") // 1 1 1 1 1 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 check(Add(Ones, Twos), itor(3), 0, "Add Ones Twos") // 3 3 3 3 3
a := make([]rat, N) a := make([]rat, N)
d := Diff(Ones) d := Diff(Ones)
for i:=0; i < N; i++ { for i := 0; i < N; i++ {
a[i] = itor(int64(i+1)) a[i] = itor(int64(i + 1))
} }
checka(d, a, "Diff") // 1 2 3 4 5 checka(d, a, "Diff") // 1 2 3 4 5
in := Integ(zero, Ones) in := Integ(zero, Ones)
a[0] = zero // integration constant a[0] = zero // integration constant
for i:=1; i < N; i++ { for i := 1; i < N; i++ {
a[i] = i2tor(1, int64(i)) a[i] = i2tor(1, int64(i))
} }
checka(in, a, "Integ") // 0 1 1/2 1/3 1/4 1/5 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(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 check(Sub(Ones, Twos), itor(-1), 0, "Sub Ones Twos") // -1 -1 -1 -1 -1
m := Mul(Ones, Ones) m := Mul(Ones, Ones)
for i:=0; i < N; i++ { for i := 0; i < N; i++ {
a[i] = itor(int64(i+1)) a[i] = itor(int64(i + 1))
} }
checka(m, a, "Mul") // 1 2 3 4 5 checka(m, a, "Mul") // 1 2 3 4 5
e := Exp(Ones) e := Exp(Ones)
a[0] = itor(1) a[0] = itor(1)
a[1] = itor(1) a[1] = itor(1)
a[2] = i2tor(3,2) a[2] = i2tor(3, 2)
a[3] = i2tor(13,6) a[3] = i2tor(13, 6)
a[4] = i2tor(73,24) a[4] = i2tor(73, 24)
a[5] = i2tor(167,40) a[5] = i2tor(167, 40)
a[6] = i2tor(4051,720) a[6] = i2tor(4051, 720)
a[7] = i2tor(37633,5040) a[7] = i2tor(37633, 5040)
a[8] = i2tor(43817,4480) a[8] = i2tor(43817, 4480)
a[9] = i2tor(4596553,362880) a[9] = i2tor(4596553, 362880)
checka(e, a, "Exp") // 1 1 3/2 13/6 73/24 checka(e, a, "Exp") // 1 1 3/2 13/6 73/24
at := Integ(zero, MonSubst(Ones, neg(one), 2)) at := Integ(zero, MonSubst(Ones, neg(one), 2))
for c, i := 1, 0; i < N; i++ { for c, i := 1, 0; i < N; i++ {
if i%2 == 0 { if i%2 == 0 {
...@@ -692,20 +722,20 @@ func main() { ...@@ -692,20 +722,20 @@ func main() {
c *= -1 c *= -1
} }
} }
checka(at, a, "ATan") // 0 -1 0 -1/3 0 -1/5 checka(at, a, "ATan") // 0 -1 0 -1/3 0 -1/5
/* /*
t := Revert(Integ(zero, MonSubst(Ones, neg(one), 2))) t := Revert(Integ(zero, MonSubst(Ones, neg(one), 2)))
a[0] = zero a[0] = zero
a[1] = itor(1) a[1] = itor(1)
a[2] = zero a[2] = zero
a[3] = i2tor(1,3) a[3] = i2tor(1,3)
a[4] = zero a[4] = zero
a[5] = i2tor(2,15) a[5] = i2tor(2,15)
a[6] = zero a[6] = zero
a[7] = i2tor(17,315) a[7] = i2tor(17,315)
a[8] = zero a[8] = zero
a[9] = i2tor(62,2835) a[9] = i2tor(62,2835)
checka(t, a, "Tan") // 0 1 0 1/3 0 2/15 checka(t, a, "Tan") // 0 1 0 1/3 0 2/15
*/ */
} }
} }
...@@ -21,8 +21,8 @@ package main ...@@ -21,8 +21,8 @@ package main
import "os" import "os"
type rat struct { type rat struct {
num, den int64 // numerator, denominator num, den int64 // numerator, denominator
} }
type item interface { type item interface {
...@@ -30,8 +30,8 @@ type item interface { ...@@ -30,8 +30,8 @@ type item interface {
eq(c item) bool eq(c item) bool
} }
func (u *rat) pr(){ func (u *rat) pr() {
if u.den==1 { if u.den == 1 {
print(u.num) print(u.num)
} else { } else {
print(u.num, "/", u.den) print(u.num, "/", u.den)
...@@ -45,12 +45,12 @@ func (u *rat) eq(c item) bool { ...@@ -45,12 +45,12 @@ func (u *rat) eq(c item) bool {
} }
type dch struct { type dch struct {
req chan int req chan int
dat chan item dat chan item
nam int nam int
} }
type dch2 [2] *dch type dch2 [2]*dch
var chnames string var chnames string
var chnameserial int var chnameserial int
...@@ -87,25 +87,25 @@ func mkdch2() *dch2 { ...@@ -87,25 +87,25 @@ func mkdch2() *dch2 {
// a signal on the release-wait channel tells the next newer // a signal on the release-wait channel tells the next newer
// generation to begin servicing out[1]. // generation to begin servicing out[1].
func dosplit(in *dch, out *dch2, wait chan int ){ func dosplit(in *dch, out *dch2, wait chan int) {
both := false // do not service both channels both := false // do not service both channels
select { select {
case <-out[0].req: case <-out[0].req:
case <-wait: case <-wait:
both = true both = true
select { select {
case <-out[0].req: case <-out[0].req:
case <-out[1].req: case <-out[1].req:
out[0],out[1] = out[1], out[0] out[0], out[1] = out[1], out[0]
} }
} }
seqno++ seqno++
in.req <- seqno in.req <- seqno
release := make(chan int) release := make(chan int)
go dosplit(in, out, release) go dosplit(in, out, release)
dat := <-in.dat dat := <-in.dat
out[0].dat <- dat out[0].dat <- dat
...@@ -117,13 +117,13 @@ func dosplit(in *dch, out *dch2, wait chan int ){ ...@@ -117,13 +117,13 @@ func dosplit(in *dch, out *dch2, wait chan int ){
release <- 0 release <- 0
} }
func split(in *dch, out *dch2){ func split(in *dch, out *dch2) {
release := make(chan int) release := make(chan int)
go dosplit(in, out, release) go dosplit(in, out, release)
release <- 0 release <- 0
} }
func put(dat item, out *dch){ func put(dat item, out *dch) {
<-out.req <-out.req
out.dat <- dat out.dat <- dat
} }
...@@ -137,21 +137,23 @@ func get(in *dch) *rat { ...@@ -137,21 +137,23 @@ func get(in *dch) *rat {
// Get one item from each of n demand channels // Get one item from each of n demand channels
func getn(in []*dch) []item { func getn(in []*dch) []item {
n:=len(in) n := len(in)
if n != 2 { panic("bad n in getn") } if n != 2 {
req := make([] chan int, 2) panic("bad n in getn")
dat := make([] chan item, 2) }
req := make([]chan int, 2)
dat := make([]chan item, 2)
out := make([]item, 2) out := make([]item, 2)
var i int var i int
var it item var it item
for i=0; i<n; i++ { for i = 0; i < n; i++ {
req[i] = in[i].req req[i] = in[i].req
dat[i] = nil dat[i] = nil
} }
for n=2*n; n>0; n-- { for n = 2 * n; n > 0; n-- {
seqno++ seqno++
select{ select {
case req[0] <- seqno: case req[0] <- seqno:
dat[0] = in[0].dat dat[0] = in[0].dat
req[0] = nil req[0] = nil
...@@ -171,25 +173,25 @@ func getn(in []*dch) []item { ...@@ -171,25 +173,25 @@ func getn(in []*dch) []item {
// Get one item from each of 2 demand channels // Get one item from each of 2 demand channels
func get2(in0 *dch, in1 *dch) []item { func get2(in0 *dch, in1 *dch) []item {
return getn([]*dch{in0, in1}) return getn([]*dch{in0, in1})
} }
func copy(in *dch, out *dch){ func copy(in *dch, out *dch) {
for { for {
<-out.req <-out.req
out.dat <- get(in) out.dat <- get(in)
} }
} }
func repeat(dat item, out *dch){ func repeat(dat item, out *dch) {
for { for {
put(dat, out) put(dat, out)
} }
} }
type PS *dch // power series type PS *dch // power series
type PS2 *[2] PS // pair of power series type PS2 *[2]PS // pair of power series
var Ones PS var Ones PS
var Twos PS var Twos PS
...@@ -210,93 +212,100 @@ func mkPS2() *dch2 { ...@@ -210,93 +212,100 @@ func mkPS2() *dch2 {
// Integer gcd; needed for rational arithmetic // Integer gcd; needed for rational arithmetic
func gcd (u, v int64) int64{ func gcd(u, v int64) int64 {
if u < 0 { return gcd(-u, v) } if u < 0 {
if u == 0 { return v } return gcd(-u, v)
}
if u == 0 {
return v
}
return gcd(v%u, u) return gcd(v%u, u)
} }
// Make a rational from two ints and from one int // Make a rational from two ints and from one int
func i2tor(u, v int64) *rat{ func i2tor(u, v int64) *rat {
g := gcd(u,v) g := gcd(u, v)
r := new(rat) r := new(rat)
if v > 0 { if v > 0 {
r.num = u/g r.num = u / g
r.den = v/g r.den = v / g
} else { } else {
r.num = -u/g r.num = -u / g
r.den = -v/g r.den = -v / g
} }
return r return r
} }
func itor(u int64) *rat{ func itor(u int64) *rat {
return i2tor(u, 1) return i2tor(u, 1)
} }
var zero *rat var zero *rat
var one *rat var one *rat
// End mark and end test // End mark and end test
var finis *rat var finis *rat
func end(u *rat) int64 { func end(u *rat) int64 {
if u.den==0 { return 1 } if u.den == 0 {
return 1
}
return 0 return 0
} }
// Operations on rationals // Operations on rationals
func add(u, v *rat) *rat { func add(u, v *rat) *rat {
g := gcd(u.den,v.den) g := gcd(u.den, v.den)
return i2tor(u.num*(v.den/g)+v.num*(u.den/g),u.den*(v.den/g)) return i2tor(u.num*(v.den/g)+v.num*(u.den/g), u.den*(v.den/g))
} }
func mul(u, v *rat) *rat{ func mul(u, v *rat) *rat {
g1 := gcd(u.num,v.den) g1 := gcd(u.num, v.den)
g2 := gcd(u.den,v.num) g2 := gcd(u.den, v.num)
r := new(rat) r := new(rat)
r.num =(u.num/g1)*(v.num/g2) r.num = (u.num / g1) * (v.num / g2)
r.den = (u.den/g2)*(v.den/g1) r.den = (u.den / g2) * (v.den / g1)
return r return r
} }
func neg(u *rat) *rat{ func neg(u *rat) *rat {
return i2tor(-u.num, u.den) return i2tor(-u.num, u.den)
} }
func sub(u, v *rat) *rat{ func sub(u, v *rat) *rat {
return add(u, neg(v)) return add(u, neg(v))
} }
func inv(u *rat) *rat{ // invert a rat func inv(u *rat) *rat { // invert a rat
if u.num == 0 { panic("zero divide in inv") } if u.num == 0 {
panic("zero divide in inv")
}
return i2tor(u.den, u.num) return i2tor(u.den, u.num)
} }
// print eval in floating point of PS at x=c to n terms // print eval in floating point of PS at x=c to n terms
func Evaln(c *rat, U PS, n int) { func Evaln(c *rat, U PS, n int) {
xn := float64(1) xn := float64(1)
x := float64(c.num)/float64(c.den) x := float64(c.num) / float64(c.den)
val := float64(0) val := float64(0)
for i:=0; i<n; i++ { for i := 0; i < n; i++ {
u := get(U) u := get(U)
if end(u) != 0 { if end(u) != 0 {
break break
} }
val = val + x * float64(u.num)/float64(u.den) val = val + x*float64(u.num)/float64(u.den)
xn = xn*x xn = xn * x
} }
print(val, "\n") print(val, "\n")
} }
// Print n terms of a power series // Print n terms of a power series
func Printn(U PS, n int){ func Printn(U PS, n int) {
done := false done := false
for ; !done && n>0; n-- { for ; !done && n > 0; n-- {
u := get(U) u := get(U)
if end(u) != 0 { if end(u) != 0 {
done = true done = true
...@@ -307,16 +316,20 @@ func Printn(U PS, n int){ ...@@ -307,16 +316,20 @@ func Printn(U PS, n int){
print(("\n")) print(("\n"))
} }
func Print(U PS){ func Print(U PS) {
Printn(U,1000000000) Printn(U, 1000000000)
} }
// Evaluate n terms of power series U at x=c // Evaluate n terms of power series U at x=c
func eval(c *rat, U PS, n int) *rat{ func eval(c *rat, U PS, n int) *rat {
if n==0 { return zero } if n == 0 {
return zero
}
y := get(U) y := get(U)
if end(y) != 0 { return zero } if end(y) != 0 {
return add(y,mul(c,eval(c,U,n-1))) return zero
}
return add(y, mul(c, eval(c, U, n-1)))
} }
// Power-series constructors return channels on which power // Power-series constructors return channels on which power
...@@ -325,29 +338,29 @@ func eval(c *rat, U PS, n int) *rat{ ...@@ -325,29 +338,29 @@ func eval(c *rat, U PS, n int) *rat{
// Make a pair of power series identical to a given power series // Make a pair of power series identical to a given power series
func Split(U PS) *dch2{ func Split(U PS) *dch2 {
UU := mkdch2() UU := mkdch2()
go split(U,UU) go split(U, UU)
return UU return UU
} }
// Add two power series // Add two power series
func Add(U, V PS) PS{ func Add(U, V PS) PS {
Z := mkPS() Z := mkPS()
go func(U, V, Z PS){ go func(U, V, Z PS) {
var uv [] item var uv []item
for { for {
<-Z.req <-Z.req
uv = get2(U,V) uv = get2(U, V)
switch end(uv[0].(*rat))+2*end(uv[1].(*rat)) { switch end(uv[0].(*rat)) + 2*end(uv[1].(*rat)) {
case 0: case 0:
Z.dat <- add(uv[0].(*rat), uv[1].(*rat)) Z.dat <- add(uv[0].(*rat), uv[1].(*rat))
case 1: case 1:
Z.dat <- uv[1] Z.dat <- uv[1]
copy(V,Z) copy(V, Z)
case 2: case 2:
Z.dat <- uv[0] Z.dat <- uv[0]
copy(U,Z) copy(U, Z)
case 3: case 3:
Z.dat <- finis Z.dat <- finis
} }
...@@ -357,9 +370,9 @@ func Add(U, V PS) PS{ ...@@ -357,9 +370,9 @@ func Add(U, V PS) PS{
} }
// Multiply a power series by a constant // Multiply a power series by a constant
func Cmul(c *rat,U PS) PS{ func Cmul(c *rat, U PS) PS {
Z := mkPS() Z := mkPS()
go func(c *rat, U, Z PS){ go func(c *rat, U, Z PS) {
done := false done := false
for !done { for !done {
<-Z.req <-Z.req
...@@ -367,7 +380,7 @@ func Cmul(c *rat,U PS) PS{ ...@@ -367,7 +380,7 @@ func Cmul(c *rat,U PS) PS{
if end(u) != 0 { if end(u) != 0 {
done = true done = true
} else { } else {
Z.dat <- mul(c,u) Z.dat <- mul(c, u)
} }
} }
Z.dat <- finis Z.dat <- finis
...@@ -377,52 +390,56 @@ func Cmul(c *rat,U PS) PS{ ...@@ -377,52 +390,56 @@ func Cmul(c *rat,U PS) PS{
// Subtract // Subtract
func Sub(U, V PS) PS{ func Sub(U, V PS) PS {
return Add(U, Cmul(neg(one), V)) return Add(U, Cmul(neg(one), V))
} }
// Multiply a power series by the monomial x^n // Multiply a power series by the monomial x^n
func Monmul(U PS, n int) PS{ func Monmul(U PS, n int) PS {
Z := mkPS() Z := mkPS()
go func(n int, U PS, Z PS){ go func(n int, U PS, Z PS) {
for ; n>0; n-- { put(zero,Z) } for ; n > 0; n-- {
copy(U,Z) put(zero, Z)
}
copy(U, Z)
}(n, U, Z) }(n, U, Z)
return Z return Z
} }
// Multiply by x // Multiply by x
func Xmul(U PS) PS{ func Xmul(U PS) PS {
return Monmul(U,1) return Monmul(U, 1)
} }
func Rep(c *rat) PS{ func Rep(c *rat) PS {
Z := mkPS() Z := mkPS()
go repeat(c,Z) go repeat(c, Z)
return Z return Z
} }
// Monomial c*x^n // Monomial c*x^n
func Mon(c *rat, n int) PS{ func Mon(c *rat, n int) PS {
Z:=mkPS() Z := mkPS()
go func(c *rat, n int, Z PS){ go func(c *rat, n int, Z PS) {
if(c.num!=0) { if c.num != 0 {
for ; n>0; n=n-1 { put(zero,Z) } for ; n > 0; n = n - 1 {
put(c,Z) put(zero, Z)
}
put(c, Z)
} }
put(finis,Z) put(finis, Z)
}(c, n, Z) }(c, n, Z)
return Z return Z
} }
func Shift(c *rat, U PS) PS{ func Shift(c *rat, U PS) PS {
Z := mkPS() Z := mkPS()
go func(c *rat, U, Z PS){ go func(c *rat, U, Z PS) {
put(c,Z) put(c, Z)
copy(U,Z) copy(U, Z)
}(c, U, Z) }(c, U, Z)
return Z return Z
} }
...@@ -453,21 +470,21 @@ func Poly(a [] *rat) PS{ ...@@ -453,21 +470,21 @@ func Poly(a [] *rat) PS{
// let V = v + x*VV // let V = v + x*VV
// then UV = u*v + x*(u*VV+v*UU) + x*x*UU*VV // then UV = u*v + x*(u*VV+v*UU) + x*x*UU*VV
func Mul(U, V PS) PS{ func Mul(U, V PS) PS {
Z:=mkPS() Z := mkPS()
go func(U, V, Z PS){ go func(U, V, Z PS) {
<-Z.req <-Z.req
uv := get2(U,V) uv := get2(U, V)
if end(uv[0].(*rat))!=0 || end(uv[1].(*rat)) != 0 { if end(uv[0].(*rat)) != 0 || end(uv[1].(*rat)) != 0 {
Z.dat <- finis Z.dat <- finis
} else { } else {
Z.dat <- mul(uv[0].(*rat),uv[1].(*rat)) Z.dat <- mul(uv[0].(*rat), uv[1].(*rat))
UU := Split(U) UU := Split(U)
VV := Split(V) VV := Split(V)
W := Add(Cmul(uv[0].(*rat),VV[0]),Cmul(uv[1].(*rat),UU[0])) W := Add(Cmul(uv[0].(*rat), VV[0]), Cmul(uv[1].(*rat), UU[0]))
<-Z.req <-Z.req
Z.dat <- get(W) Z.dat <- get(W)
copy(Add(W,Mul(UU[1],VV[1])),Z) copy(Add(W, Mul(UU[1], VV[1])), Z)
} }
}(U, V, Z) }(U, V, Z)
return Z return Z
...@@ -475,19 +492,19 @@ func Mul(U, V PS) PS{ ...@@ -475,19 +492,19 @@ func Mul(U, V PS) PS{
// Differentiate // Differentiate
func Diff(U PS) PS{ func Diff(U PS) PS {
Z:=mkPS() Z := mkPS()
go func(U, Z PS){ go func(U, Z PS) {
<-Z.req <-Z.req
u := get(U) u := get(U)
if end(u) == 0 { if end(u) == 0 {
done:=false done := false
for i:=1; !done; i++ { for i := 1; !done; i++ {
u = get(U) u = get(U)
if end(u) != 0 { if end(u) != 0 {
done=true done = true
} else { } else {
Z.dat <- mul(itor(int64(i)),u) Z.dat <- mul(itor(int64(i)), u)
<-Z.req <-Z.req
} }
} }
...@@ -498,16 +515,18 @@ func Diff(U PS) PS{ ...@@ -498,16 +515,18 @@ func Diff(U PS) PS{
} }
// Integrate, with const of integration // Integrate, with const of integration
func Integ(c *rat,U PS) PS{ func Integ(c *rat, U PS) PS {
Z:=mkPS() Z := mkPS()
go func(c *rat, U, Z PS){ go func(c *rat, U, Z PS) {
put(c,Z) put(c, Z)
done:=false done := false
for i:=1; !done; i++ { for i := 1; !done; i++ {
<-Z.req <-Z.req
u := get(U) u := get(U)
if end(u) != 0 { done= true } if end(u) != 0 {
Z.dat <- mul(i2tor(1,int64(i)),u) done = true
}
Z.dat <- mul(i2tor(1, int64(i)), u)
} }
Z.dat <- finis Z.dat <- finis
}(c, U, Z) }(c, U, Z)
...@@ -516,18 +535,18 @@ func Integ(c *rat,U PS) PS{ ...@@ -516,18 +535,18 @@ func Integ(c *rat,U PS) PS{
// Binomial theorem (1+x)^c // Binomial theorem (1+x)^c
func Binom(c *rat) PS{ func Binom(c *rat) PS {
Z:=mkPS() Z := mkPS()
go func(c *rat, Z PS){ go func(c *rat, Z PS) {
n := 1 n := 1
t := itor(1) t := itor(1)
for c.num!=0 { for c.num != 0 {
put(t,Z) put(t, Z)
t = mul(mul(t,c),i2tor(1,int64(n))) t = mul(mul(t, c), i2tor(1, int64(n)))
c = sub(c,one) c = sub(c, one)
n++ n++
} }
put(finis,Z) put(finis, Z)
}(c, Z) }(c, Z)
return Z return Z
} }
...@@ -540,15 +559,15 @@ func Binom(c *rat) PS{ ...@@ -540,15 +559,15 @@ func Binom(c *rat) PS{
// u*ZZ + z*UU +x*UU*ZZ = 0 // u*ZZ + z*UU +x*UU*ZZ = 0
// ZZ = -UU*(z+x*ZZ)/u // ZZ = -UU*(z+x*ZZ)/u
func Recip(U PS) PS{ func Recip(U PS) PS {
Z:=mkPS() Z := mkPS()
go func(U, Z PS){ go func(U, Z PS) {
ZZ:=mkPS2() ZZ := mkPS2()
<-Z.req <-Z.req
z := inv(get(U)) z := inv(get(U))
Z.dat <- z Z.dat <- z
split(Mul(Cmul(neg(z),U),Shift(z,ZZ[0])),ZZ) split(Mul(Cmul(neg(z), U), Shift(z, ZZ[0])), ZZ)
copy(ZZ[1],Z) copy(ZZ[1], Z)
}(U, Z) }(U, Z)
return Z return Z
} }
...@@ -560,9 +579,9 @@ func Recip(U PS) PS{ ...@@ -560,9 +579,9 @@ func Recip(U PS) PS{
// DZ = Z*DU // DZ = Z*DU
// integrate to get Z // integrate to get Z
func Exp(U PS) PS{ func Exp(U PS) PS {
ZZ := mkPS2() ZZ := mkPS2()
split(Integ(one,Mul(ZZ[0],Diff(U))),ZZ) split(Integ(one, Mul(ZZ[0], Diff(U))), ZZ)
return ZZ[1] return ZZ[1]
} }
...@@ -573,7 +592,7 @@ func Exp(U PS) PS{ ...@@ -573,7 +592,7 @@ func Exp(U PS) PS{
// bug: a nonzero constant term is ignored // bug: a nonzero constant term is ignored
func Subst(U, V PS) PS { func Subst(U, V PS) PS {
Z:= mkPS() Z := mkPS()
go func(U, V, Z PS) { go func(U, V, Z PS) {
VV := Split(V) VV := Split(V)
<-Z.req <-Z.req
...@@ -581,9 +600,9 @@ func Subst(U, V PS) PS { ...@@ -581,9 +600,9 @@ func Subst(U, V PS) PS {
Z.dat <- u Z.dat <- u
if end(u) == 0 { if end(u) == 0 {
if end(get(VV[0])) != 0 { if end(get(VV[0])) != 0 {
put(finis,Z) put(finis, Z)
} else { } else {
copy(Mul(VV[0],Subst(U,VV[1])),Z) copy(Mul(VV[0], Subst(U, VV[1])), Z)
} }
} }
}(U, V, Z) }(U, V, Z)
...@@ -594,7 +613,7 @@ func Subst(U, V PS) PS { ...@@ -594,7 +613,7 @@ func Subst(U, V PS) PS {
// Each Ui is multiplied by c^i and followed by n-1 zeros // Each Ui is multiplied by c^i and followed by n-1 zeros
func MonSubst(U PS, c0 *rat, n int) PS { func MonSubst(U PS, c0 *rat, n int) PS {
Z:= mkPS() Z := mkPS()
go func(U, Z PS, c0 *rat, n int) { go func(U, Z PS, c0 *rat, n int) {
c := one c := one
for { for {
...@@ -615,14 +634,13 @@ func MonSubst(U PS, c0 *rat, n int) PS { ...@@ -615,14 +634,13 @@ func MonSubst(U PS, c0 *rat, n int) PS {
return Z return Z
} }
func Init() { func Init() {
chnameserial = -1 chnameserial = -1
seqno = 0 seqno = 0
chnames = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" chnames = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
zero = itor(0) zero = itor(0)
one = itor(1) one = itor(1)
finis = i2tor(1,0) finis = i2tor(1, 0)
Ones = Rep(one) Ones = Rep(one)
Twos = Rep(itor(2)) Twos = Rep(itor(2))
} }
...@@ -641,7 +659,8 @@ func check(U PS, c *rat, count int, str string) { ...@@ -641,7 +659,8 @@ func check(U PS, c *rat, count int, str string) {
} }
} }
const N=10 const N = 10
func checka(U PS, a []*rat, str string) { func checka(U PS, a []*rat, str string) {
for i := 0; i < N; i++ { for i := 0; i < N; i++ {
check(U, a[i], 1, str) check(U, a[i], 1, str)
...@@ -650,53 +669,64 @@ func checka(U PS, a []*rat, str string) { ...@@ -650,53 +669,64 @@ func checka(U PS, a []*rat, str string) {
func main() { func main() {
Init() Init()
if len(os.Args) > 1 { // print if len(os.Args) > 1 { // print
print("Ones: "); Printn(Ones, 10) print("Ones: ")
print("Twos: "); Printn(Twos, 10) Printn(Ones, 10)
print("Add: "); Printn(Add(Ones, Twos), 10) print("Twos: ")
print("Diff: "); Printn(Diff(Ones), 10) Printn(Twos, 10)
print("Integ: "); Printn(Integ(zero, Ones), 10) print("Add: ")
print("CMul: "); Printn(Cmul(neg(one), Ones), 10) Printn(Add(Ones, Twos), 10)
print("Sub: "); Printn(Sub(Ones, Twos), 10) print("Diff: ")
print("Mul: "); Printn(Mul(Ones, Ones), 10) Printn(Diff(Ones), 10)
print("Exp: "); Printn(Exp(Ones), 15) print("Integ: ")
print("MonSubst: "); Printn(MonSubst(Ones, neg(one), 2), 10) Printn(Integ(zero, Ones), 10)
print("ATan: "); Printn(Integ(zero, MonSubst(Ones, neg(one), 2)), 10) print("CMul: ")
} else { // test 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)
} else { // test
check(Ones, one, 5, "Ones") check(Ones, one, 5, "Ones")
check(Add(Ones, Ones), itor(2), 0, "Add Ones Ones") // 1 1 1 1 1 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 check(Add(Ones, Twos), itor(3), 0, "Add Ones Twos") // 3 3 3 3 3
a := make([]*rat, N) a := make([]*rat, N)
d := Diff(Ones) d := Diff(Ones)
for i:=0; i < N; i++ { for i := 0; i < N; i++ {
a[i] = itor(int64(i+1)) a[i] = itor(int64(i + 1))
} }
checka(d, a, "Diff") // 1 2 3 4 5 checka(d, a, "Diff") // 1 2 3 4 5
in := Integ(zero, Ones) in := Integ(zero, Ones)
a[0] = zero // integration constant a[0] = zero // integration constant
for i:=1; i < N; i++ { for i := 1; i < N; i++ {
a[i] = i2tor(1, int64(i)) a[i] = i2tor(1, int64(i))
} }
checka(in, a, "Integ") // 0 1 1/2 1/3 1/4 1/5 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(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 check(Sub(Ones, Twos), itor(-1), 0, "Sub Ones Twos") // -1 -1 -1 -1 -1
m := Mul(Ones, Ones) m := Mul(Ones, Ones)
for i:=0; i < N; i++ { for i := 0; i < N; i++ {
a[i] = itor(int64(i+1)) a[i] = itor(int64(i + 1))
} }
checka(m, a, "Mul") // 1 2 3 4 5 checka(m, a, "Mul") // 1 2 3 4 5
e := Exp(Ones) e := Exp(Ones)
a[0] = itor(1) a[0] = itor(1)
a[1] = itor(1) a[1] = itor(1)
a[2] = i2tor(3,2) a[2] = i2tor(3, 2)
a[3] = i2tor(13,6) a[3] = i2tor(13, 6)
a[4] = i2tor(73,24) a[4] = i2tor(73, 24)
a[5] = i2tor(167,40) a[5] = i2tor(167, 40)
a[6] = i2tor(4051,720) a[6] = i2tor(4051, 720)
a[7] = i2tor(37633,5040) a[7] = i2tor(37633, 5040)
a[8] = i2tor(43817,4480) a[8] = i2tor(43817, 4480)
a[9] = i2tor(4596553,362880) a[9] = i2tor(4596553, 362880)
checka(e, a, "Exp") // 1 1 3/2 13/6 73/24 checka(e, a, "Exp") // 1 1 3/2 13/6 73/24
at := Integ(zero, MonSubst(Ones, neg(one), 2)) at := Integ(zero, MonSubst(Ones, neg(one), 2))
for c, i := 1, 0; i < N; i++ { for c, i := 1, 0; i < N; i++ {
if i%2 == 0 { if i%2 == 0 {
...@@ -706,20 +736,20 @@ func main() { ...@@ -706,20 +736,20 @@ func main() {
c *= -1 c *= -1
} }
} }
checka(at, a, "ATan"); // 0 -1 0 -1/3 0 -1/5 checka(at, a, "ATan") // 0 -1 0 -1/3 0 -1/5
/* /*
t := Revert(Integ(zero, MonSubst(Ones, neg(one), 2))) t := Revert(Integ(zero, MonSubst(Ones, neg(one), 2)))
a[0] = zero a[0] = zero
a[1] = itor(1) a[1] = itor(1)
a[2] = zero a[2] = zero
a[3] = i2tor(1,3) a[3] = i2tor(1,3)
a[4] = zero a[4] = zero
a[5] = i2tor(2,15) a[5] = i2tor(2,15)
a[6] = zero a[6] = zero
a[7] = i2tor(17,315) a[7] = i2tor(17,315)
a[8] = zero a[8] = zero
a[9] = i2tor(62,2835) a[9] = i2tor(62,2835)
checka(t, a, "Tan") // 0 1 0 1/3 0 2/15 checka(t, a, "Tan") // 0 1 0 1/3 0 2/15
*/ */
} }
} }
...@@ -14,12 +14,10 @@ import "time" ...@@ -14,12 +14,10 @@ import "time"
const always = "function did not" const always = "function did not"
const never = "function did" const never = "function did"
func unreachable() { func unreachable() {
panic("control flow shouldn't reach here") panic("control flow shouldn't reach here")
} }
// Calls f and verifies that f always/never panics depending on signal. // Calls f and verifies that f always/never panics depending on signal.
func testPanic(signal string, f func()) { func testPanic(signal string, f func()) {
defer func() { defer func() {
...@@ -34,7 +32,6 @@ func testPanic(signal string, f func()) { ...@@ -34,7 +32,6 @@ func testPanic(signal string, f func()) {
f() f()
} }
// Calls f and empirically verifies that f always/never blocks depending on signal. // Calls f and empirically verifies that f always/never blocks depending on signal.
func testBlock(signal string, f func()) { func testBlock(signal string, f func()) {
c := make(chan string) c := make(chan string)
...@@ -51,7 +48,6 @@ func testBlock(signal string, f func()) { ...@@ -51,7 +48,6 @@ func testBlock(signal string, f func()) {
} }
} }
func main() { func main() {
const async = 1 // asynchronous channels const async = 1 // asynchronous channels
var nilch chan int var nilch chan int
...@@ -114,8 +110,7 @@ func main() { ...@@ -114,8 +110,7 @@ func main() {
// empty selects always block // empty selects always block
testBlock(always, func() { testBlock(always, func() {
select { select {}
}
}) })
// selects with only nil channels always block // selects with only nil channels always block
......
...@@ -30,7 +30,7 @@ func chanchan() { ...@@ -30,7 +30,7 @@ func chanchan() {
func sendprec() { func sendprec() {
c := make(chan bool, 1) c := make(chan bool, 1)
c <- false || true // not a syntax error: same as c <- (false || true) c <- false || true // not a syntax error: same as c <- (false || true)
if !<-c { if !<-c {
panic("sent false") panic("sent false")
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment