-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconstructors.go
85 lines (71 loc) · 1.83 KB
/
constructors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2017 Andreas Pannewitz. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ps
// ===========================================================================
// Constructors - from coefficients.
// Conventions
// Upper-case for power series.
// Lower-case for coefficients.
// Input variables: U,V,...
// Output variables: ...,Y,Z
// 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.
// Monomial returns `c * x^n`.
func Monomial(c Coefficient, n int) PS {
Z := New()
go func(Z PS, c Coefficient, n int) {
defer Z.Close()
if IsZero(c) {
return
}
for ; n > 0; n-- { // n-1 times aZero
if !Z.Provide(aZero()) {
return
}
}
Z.Provide(c) // `c * x^n`
}(Z, c, n)
return Z
}
// Binomial returns `(1+x)^c`,
// a finite polynom iff `c` is a positive
// and an alternating infinite power series otherwise.
func Binomial(c Coefficient) PS {
Z := New()
go func(Z PS, c Coefficient) {
defer Z.Close()
i, iZ := 1, aOne() // `1`, `1/1`
for !IsZero(c) {
if !Z.Provide(iZ) {
return
}
iZ.Mul(iZ, cRat1byI(i)(c)) // `iZ = iZ * c * 1/i`
c.Sub(c, aOne()) // `c = c-1`
i++
}
}(Z, c)
return Z
}
// Polynom converts coefficients, constant term `c` first,
// to a (finite) power series, the polynom in the coefficients.
func Polynom(a ...Coefficient) PS {
Z := New()
go func(Z PS, a ...Coefficient) {
defer Z.Close()
j := 0
for j = len(a); j > 0; j-- {
if !IsZero(a[j-1]) { // remove trailing zeros
break
}
}
for i := 0; i < j; i++ {
if !Z.Provide(a[i]) {
return
}
}
}(Z, a...)
return Z
}
// ===========================================================================