Source file
test/chan/powser2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package main
21
22 import "os"
23
24 type rat struct {
25 num, den int64
26 }
27
28 type item interface {
29 pr()
30 eq(c item) bool
31 }
32
33 func (u *rat) pr() {
34 if u.den == 1 {
35 print(u.num)
36 } else {
37 print(u.num, "/", u.den)
38 }
39 print(" ")
40 }
41
42 func (u *rat) eq(c item) bool {
43 c1 := c.(*rat)
44 return u.num == c1.num && u.den == c1.den
45 }
46
47 type dch struct {
48 req chan int
49 dat chan item
50 nam int
51 }
52
53 type dch2 [2]*dch
54
55 var chnames string
56 var chnameserial int
57 var seqno int
58
59 func mkdch() *dch {
60 c := chnameserial % len(chnames)
61 chnameserial++
62 d := new(dch)
63 d.req = make(chan int)
64 d.dat = make(chan item)
65 d.nam = c
66 return d
67 }
68
69 func mkdch2() *dch2 {
70 d2 := new(dch2)
71 d2[0] = mkdch()
72 d2[1] = mkdch()
73 return d2
74 }
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 func dosplit(in *dch, out *dch2, wait chan int) {
91 both := false
92
93 select {
94 case <-out[0].req:
95
96 case <-wait:
97 both = true
98 select {
99 case <-out[0].req:
100
101 case <-out[1].req:
102 out[0], out[1] = out[1], out[0]
103 }
104 }
105
106 seqno++
107 in.req <- seqno
108 release := make(chan int)
109 go dosplit(in, out, release)
110 dat := <-in.dat
111 out[0].dat <- dat
112 if !both {
113 <-wait
114 }
115 <-out[1].req
116 out[1].dat <- dat
117 release <- 0
118 }
119
120 func split(in *dch, out *dch2) {
121 release := make(chan int)
122 go dosplit(in, out, release)
123 release <- 0
124 }
125
126 func put(dat item, out *dch) {
127 <-out.req
128 out.dat <- dat
129 }
130
131 func get(in *dch) *rat {
132 seqno++
133 in.req <- seqno
134 return (<-in.dat).(*rat)
135 }
136
137
138
139 func getn(in []*dch) []item {
140 n := len(in)
141 if n != 2 {
142 panic("bad n in getn")
143 }
144 req := make([]chan int, 2)
145 dat := make([]chan item, 2)
146 out := make([]item, 2)
147 var i int
148 var it item
149 for i = 0; i < n; i++ {
150 req[i] = in[i].req
151 dat[i] = nil
152 }
153 for n = 2 * n; n > 0; n-- {
154 seqno++
155
156 select {
157 case req[0] <- seqno:
158 dat[0] = in[0].dat
159 req[0] = nil
160 case req[1] <- seqno:
161 dat[1] = in[1].dat
162 req[1] = nil
163 case it = <-dat[0]:
164 out[0] = it
165 dat[0] = nil
166 case it = <-dat[1]:
167 out[1] = it
168 dat[1] = nil
169 }
170 }
171 return out
172 }
173
174
175
176 func get2(in0 *dch, in1 *dch) []item {
177 return getn([]*dch{in0, in1})
178 }
179
180 func copy(in *dch, out *dch) {
181 for {
182 <-out.req
183 out.dat <- get(in)
184 }
185 }
186
187 func repeat(dat item, out *dch) {
188 for {
189 put(dat, out)
190 }
191 }
192
193 type PS *dch
194 type PS2 *[2]PS
195
196 var Ones PS
197 var Twos PS
198
199 func mkPS() *dch {
200 return mkdch()
201 }
202
203 func mkPS2() *dch2 {
204 return mkdch2()
205 }
206
207
208
209
210
211
212
213
214
215 func gcd(u, v int64) int64 {
216 if u < 0 {
217 return gcd(-u, v)
218 }
219 if u == 0 {
220 return v
221 }
222 return gcd(v%u, u)
223 }
224
225
226
227 func i2tor(u, v int64) *rat {
228 g := gcd(u, v)
229 r := new(rat)
230 if v > 0 {
231 r.num = u / g
232 r.den = v / g
233 } else {
234 r.num = -u / g
235 r.den = -v / g
236 }
237 return r
238 }
239
240 func itor(u int64) *rat {
241 return i2tor(u, 1)
242 }
243
244 var zero *rat
245 var one *rat
246
247
248
249 var finis *rat
250
251 func end(u *rat) int64 {
252 if u.den == 0 {
253 return 1
254 }
255 return 0
256 }
257
258
259
260 func add(u, v *rat) *rat {
261 g := gcd(u.den, v.den)
262 return i2tor(u.num*(v.den/g)+v.num*(u.den/g), u.den*(v.den/g))
263 }
264
265 func mul(u, v *rat) *rat {
266 g1 := gcd(u.num, v.den)
267 g2 := gcd(u.den, v.num)
268 r := new(rat)
269 r.num = (u.num / g1) * (v.num / g2)
270 r.den = (u.den / g2) * (v.den / g1)
271 return r
272 }
273
274 func neg(u *rat) *rat {
275 return i2tor(-u.num, u.den)
276 }
277
278 func sub(u, v *rat) *rat {
279 return add(u, neg(v))
280 }
281
282 func inv(u *rat) *rat {
283 if u.num == 0 {
284 panic("zero divide in inv")
285 }
286 return i2tor(u.den, u.num)
287 }
288
289
290 func Evaln(c *rat, U PS, n int) {
291 xn := float64(1)
292 x := float64(c.num) / float64(c.den)
293 val := float64(0)
294 for i := 0; i < n; i++ {
295 u := get(U)
296 if end(u) != 0 {
297 break
298 }
299 val = val + x*float64(u.num)/float64(u.den)
300 xn = xn * x
301 }
302 print(val, "\n")
303 }
304
305
306 func Printn(U PS, n int) {
307 done := false
308 for ; !done && n > 0; n-- {
309 u := get(U)
310 if end(u) != 0 {
311 done = true
312 } else {
313 u.pr()
314 }
315 }
316 print(("\n"))
317 }
318
319 func Print(U PS) {
320 Printn(U, 1000000000)
321 }
322
323
324 func eval(c *rat, U PS, n int) *rat {
325 if n == 0 {
326 return zero
327 }
328 y := get(U)
329 if end(y) != 0 {
330 return zero
331 }
332 return add(y, mul(c, eval(c, U, n-1)))
333 }
334
335
336
337
338
339
340
341 func Split(U PS) *dch2 {
342 UU := mkdch2()
343 go split(U, UU)
344 return UU
345 }
346
347
348 func Add(U, V PS) PS {
349 Z := mkPS()
350 go func(U, V, Z PS) {
351 var uv []item
352 for {
353 <-Z.req
354 uv = get2(U, V)
355 switch end(uv[0].(*rat)) + 2*end(uv[1].(*rat)) {
356 case 0:
357 Z.dat <- add(uv[0].(*rat), uv[1].(*rat))
358 case 1:
359 Z.dat <- uv[1]
360 copy(V, Z)
361 case 2:
362 Z.dat <- uv[0]
363 copy(U, Z)
364 case 3:
365 Z.dat <- finis
366 }
367 }
368 }(U, V, Z)
369 return Z
370 }
371
372
373 func Cmul(c *rat, U PS) PS {
374 Z := mkPS()
375 go func(c *rat, U, Z PS) {
376 done := false
377 for !done {
378 <-Z.req
379 u := get(U)
380 if end(u) != 0 {
381 done = true
382 } else {
383 Z.dat <- mul(c, u)
384 }
385 }
386 Z.dat <- finis
387 }(c, U, Z)
388 return Z
389 }
390
391
392
393 func Sub(U, V PS) PS {
394 return Add(U, Cmul(neg(one), V))
395 }
396
397
398
399 func Monmul(U PS, n int) PS {
400 Z := mkPS()
401 go func(n int, U PS, Z PS) {
402 for ; n > 0; n-- {
403 put(zero, Z)
404 }
405 copy(U, Z)
406 }(n, U, Z)
407 return Z
408 }
409
410
411
412 func Xmul(U PS) PS {
413 return Monmul(U, 1)
414 }
415
416 func Rep(c *rat) PS {
417 Z := mkPS()
418 go repeat(c, Z)
419 return Z
420 }
421
422
423
424 func Mon(c *rat, n int) PS {
425 Z := mkPS()
426 go func(c *rat, n int, Z PS) {
427 if c.num != 0 {
428 for ; n > 0; n = n - 1 {
429 put(zero, Z)
430 }
431 put(c, Z)
432 }
433 put(finis, Z)
434 }(c, n, Z)
435 return Z
436 }
437
438 func Shift(c *rat, U PS) PS {
439 Z := mkPS()
440 go func(c *rat, U, Z PS) {
441 put(c, Z)
442 copy(U, Z)
443 }(c, U, Z)
444 return Z
445 }
446
447
448
449
450
451
452
467
468
469
470
471
472
473 func Mul(U, V PS) PS {
474 Z := mkPS()
475 go func(U, V, Z PS) {
476 <-Z.req
477 uv := get2(U, V)
478 if end(uv[0].(*rat)) != 0 || end(uv[1].(*rat)) != 0 {
479 Z.dat <- finis
480 } else {
481 Z.dat <- mul(uv[0].(*rat), uv[1].(*rat))
482 UU := Split(U)
483 VV := Split(V)
484 W := Add(Cmul(uv[0].(*rat), VV[0]), Cmul(uv[1].(*rat), UU[0]))
485 <-Z.req
486 Z.dat <- get(W)
487 copy(Add(W, Mul(UU[1], VV[1])), Z)
488 }
489 }(U, V, Z)
490 return Z
491 }
492
493
494
495 func Diff(U PS) PS {
496 Z := mkPS()
497 go func(U, Z PS) {
498 <-Z.req
499 u := get(U)
500 if end(u) == 0 {
501 done := false
502 for i := 1; !done; i++ {
503 u = get(U)
504 if end(u) != 0 {
505 done = true
506 } else {
507 Z.dat <- mul(itor(int64(i)), u)
508 <-Z.req
509 }
510 }
511 }
512 Z.dat <- finis
513 }(U, Z)
514 return Z
515 }
516
517
518 func Integ(c *rat, U PS) PS {
519 Z := mkPS()
520 go func(c *rat, U, Z PS) {
521 put(c, Z)
522 done := false
523 for i := 1; !done; i++ {
524 <-Z.req
525 u := get(U)
526 if end(u) != 0 {
527 done = true
528 }
529 Z.dat <- mul(i2tor(1, int64(i)), u)
530 }
531 Z.dat <- finis
532 }(c, U, Z)
533 return Z
534 }
535
536
537
538 func Binom(c *rat) PS {
539 Z := mkPS()
540 go func(c *rat, Z PS) {
541 n := 1
542 t := itor(1)
543 for c.num != 0 {
544 put(t, Z)
545 t = mul(mul(t, c), i2tor(1, int64(n)))
546 c = sub(c, one)
547 n++
548 }
549 put(finis, Z)
550 }(c, Z)
551 return Z
552 }
553
554
555
556
557
558
559
560
561
562 func Recip(U PS) PS {
563 Z := mkPS()
564 go func(U, Z PS) {
565 ZZ := mkPS2()
566 <-Z.req
567 z := inv(get(U))
568 Z.dat <- z
569 split(Mul(Cmul(neg(z), U), Shift(z, ZZ[0])), ZZ)
570 copy(ZZ[1], Z)
571 }(U, Z)
572 return Z
573 }
574
575
576
577
578
579
580
581
582 func Exp(U PS) PS {
583 ZZ := mkPS2()
584 split(Integ(one, Mul(ZZ[0], Diff(U))), ZZ)
585 return ZZ[1]
586 }
587
588
589
590
591
592
593
594 func Subst(U, V PS) PS {
595 Z := mkPS()
596 go func(U, V, Z PS) {
597 VV := Split(V)
598 <-Z.req
599 u := get(U)
600 Z.dat <- u
601 if end(u) == 0 {
602 if end(get(VV[0])) != 0 {
603 put(finis, Z)
604 } else {
605 copy(Mul(VV[0], Subst(U, VV[1])), Z)
606 }
607 }
608 }(U, V, Z)
609 return Z
610 }
611
612
613
614
615 func MonSubst(U PS, c0 *rat, n int) PS {
616 Z := mkPS()
617 go func(U, Z PS, c0 *rat, n int) {
618 c := one
619 for {
620 <-Z.req
621 u := get(U)
622 Z.dat <- mul(u, c)
623 c = mul(c, c0)
624 if end(u) != 0 {
625 Z.dat <- finis
626 break
627 }
628 for i := 1; i < n; i++ {
629 <-Z.req
630 Z.dat <- zero
631 }
632 }
633 }(U, Z, c0, n)
634 return Z
635 }
636
637 func Init() {
638 chnameserial = -1
639 seqno = 0
640 chnames = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
641 zero = itor(0)
642 one = itor(1)
643 finis = i2tor(1, 0)
644 Ones = Rep(one)
645 Twos = Rep(itor(2))
646 }
647
648 func check(U PS, c *rat, count int, str string) {
649 for i := 0; i < count; i++ {
650 r := get(U)
651 if !r.eq(c) {
652 print("got: ")
653 r.pr()
654 print("should get ")
655 c.pr()
656 print("\n")
657 panic(str)
658 }
659 }
660 }
661
662 const N = 10
663
664 func checka(U PS, a []*rat, str string) {
665 for i := 0; i < N; i++ {
666 check(U, a[i], 1, str)
667 }
668 }
669
670 func main() {
671 Init()
672 if len(os.Args) > 1 {
673 print("Ones: ")
674 Printn(Ones, 10)
675 print("Twos: ")
676 Printn(Twos, 10)
677 print("Add: ")
678 Printn(Add(Ones, Twos), 10)
679 print("Diff: ")
680 Printn(Diff(Ones), 10)
681 print("Integ: ")
682 Printn(Integ(zero, Ones), 10)
683 print("CMul: ")
684 Printn(Cmul(neg(one), Ones), 10)
685 print("Sub: ")
686 Printn(Sub(Ones, Twos), 10)
687 print("Mul: ")
688 Printn(Mul(Ones, Ones), 10)
689 print("Exp: ")
690 Printn(Exp(Ones), 15)
691 print("MonSubst: ")
692 Printn(MonSubst(Ones, neg(one), 2), 10)
693 print("ATan: ")
694 Printn(Integ(zero, MonSubst(Ones, neg(one), 2)), 10)
695 } else {
696 check(Ones, one, 5, "Ones")
697 check(Add(Ones, Ones), itor(2), 0, "Add Ones Ones")
698 check(Add(Ones, Twos), itor(3), 0, "Add Ones Twos")
699 a := make([]*rat, N)
700 d := Diff(Ones)
701 for i := 0; i < N; i++ {
702 a[i] = itor(int64(i + 1))
703 }
704 checka(d, a, "Diff")
705 in := Integ(zero, Ones)
706 a[0] = zero
707 for i := 1; i < N; i++ {
708 a[i] = i2tor(1, int64(i))
709 }
710 checka(in, a, "Integ")
711 check(Cmul(neg(one), Twos), itor(-2), 10, "CMul")
712 check(Sub(Ones, Twos), itor(-1), 0, "Sub Ones Twos")
713 m := Mul(Ones, Ones)
714 for i := 0; i < N; i++ {
715 a[i] = itor(int64(i + 1))
716 }
717 checka(m, a, "Mul")
718 e := Exp(Ones)
719 a[0] = itor(1)
720 a[1] = itor(1)
721 a[2] = i2tor(3, 2)
722 a[3] = i2tor(13, 6)
723 a[4] = i2tor(73, 24)
724 a[5] = i2tor(167, 40)
725 a[6] = i2tor(4051, 720)
726 a[7] = i2tor(37633, 5040)
727 a[8] = i2tor(43817, 4480)
728 a[9] = i2tor(4596553, 362880)
729 checka(e, a, "Exp")
730 at := Integ(zero, MonSubst(Ones, neg(one), 2))
731 for c, i := 1, 0; i < N; i++ {
732 if i%2 == 0 {
733 a[i] = zero
734 } else {
735 a[i] = i2tor(int64(c), int64(i))
736 c *= -1
737 }
738 }
739 checka(at, a, "ATan")
740
754 }
755 }
756
View as plain text