|
1 | 1 | export RationalPoly
|
2 | 2 | import Base.+, Base.-, Base.*, Base./
|
3 | 3 |
|
4 |
| -immutable RationalPoly{C, S, T} <: PolyType{C} |
5 |
| - num::TermContainer{C, S} |
6 |
| - den::TermContainer{C, T} |
| 4 | +immutable RationalPoly{NT <: APL, DT <: APL} |
| 5 | + num::NT |
| 6 | + den::DT |
7 | 7 | end
|
8 |
| -iscomm{C, S, T}(r::Type{RationalPoly{C, S, T}}) = C |
9 | 8 |
|
10 |
| -Base.convert{C, S, T}(::Type{RationalPoly{C, S, T}}, q::RationalPoly{C, S, T}) = q |
11 |
| -Base.convert{C, S, T, U, V}(::Type{RationalPoly{C, S, T}}, q::RationalPoly{C, U, V}) = TermContainer{C, S}(q.num) / TermContainer{C, T}(q.den) |
12 |
| -function Base.convert{C, S, T}(::Type{RationalPoly{C, S, T}}, p::TermContainer{C, S}) |
13 |
| - p / one(TermContainer{C, T}) |
| 9 | +Base.convert{NT, DT}(::Type{RationalPoly{NT, DT}}, q::RationalPoly{NT, DT}) = q |
| 10 | +Base.convert{NTout, DTout, NTin, DTin}(::Type{RationalPoly{NTout, DTout}}, q::RationalPoly{NTin, DTin}) = convert(NTout, q.num) / convert(DTout, q.den) |
| 11 | +function Base.convert{NT, DT}(::Type{RationalPoly{NT, DT}}, p::NT) |
| 12 | + p / one(DT) |
14 | 13 | end
|
15 |
| -function Base.convert{C, S, T}(::Type{RationalPoly{C, S, T}}, p::TermContainer) |
16 |
| - convert(RationalPoly{C, S, T}, TermContainer{C, S}(p)) |
17 |
| -end |
18 |
| -function Base.convert{C, S, T}(::Type{RationalPoly{C, S, T}}, p) |
19 |
| - Base.convert(RationalPoly{C, S, T}, TermContainer{C, S}(p)) |
| 14 | +function Base.convert{NT, DT}(::Type{RationalPoly{NT, DT}}, p) |
| 15 | + convert(RationalPoly{NT, DT}, convert(NT, p)) |
20 | 16 | end
|
21 | 17 |
|
22 |
| -(/)(r::RationalPoly, p::TermContainer) = r.num / (r.den * p) |
23 |
| -function (/){C, S, T}(num::TermContainer{C, S}, den::TermContainer{C, T}) |
24 |
| - RationalPoly{C, S, T}(num, den) |
| 18 | +(/)(r::RationalPoly, p) = r.num / (r.den * p) |
| 19 | +function (/){NT <: APL, DT <: APL}(num::NT, den::DT) |
| 20 | + RationalPoly{NT, DT}(num, den) |
25 | 21 | end
|
26 |
| -function (/){C}(num, den::PolyType{C}) |
27 |
| - TermContainer{C}(num) / den |
| 22 | +function (/)(num, den::APL) |
| 23 | + term(num) / den |
28 | 24 | end
|
29 |
| -(/){C}(num::PolyType{C}, den::PolyType{C}) = TermContainer{C}(num) / TermContainer{C}(den) |
30 |
| - |
31 | 25 | # Polynomial divided by coefficient is a polynomial not a rational polynomial
|
32 |
| -(/){C}(num::PolyType{C}, den) = num * (1 / den) |
| 26 | +# (1/den) * num would not be correct in case of noncommutative coefficients |
| 27 | +(/)(num::APL, den) = num * (1 / den) |
33 | 28 |
|
34 | 29 | function (+)(r::RationalPoly, s::RationalPoly)
|
35 | 30 | (r.num*s.den + r.den*s.num) / (r.den * s.den)
|
36 | 31 | end
|
37 |
| -function (+)(p::TermContainer, r::RationalPoly) |
| 32 | +function (+)(p, r::RationalPoly) |
38 | 33 | (p*r.den + r.num) / r.den
|
39 | 34 | end
|
40 |
| -(+)(r::RationalPoly, p::Polynomial) = p + r |
41 |
| -(+)(r::RationalPoly, t::Term) = t + r |
| 35 | +(+)(r::RationalPoly, p) = p + r |
42 | 36 | function (-)(r::RationalPoly, s::RationalPoly)
|
43 | 37 | (r.num*s.den - r.den*s.num) / (r.den * s.den)
|
44 | 38 | end
|
45 |
| -(-)(p::PolyType, s::RationalPoly) = (p * s.den - s.num) / s.den |
46 |
| -(-)(s::RationalPoly, p::PolyType) = (s.num - p * s.den) / s.den |
| 39 | +(-)(p, s::RationalPoly) = (p * s.den - s.num) / s.den |
| 40 | +(-)(s::RationalPoly, p) = (s.num - p * s.den) / s.den |
47 | 41 |
|
48 | 42 | (*)(r::RationalPoly, s::RationalPoly) = (r.num*s.num) / (r.den*s.den)
|
49 |
| -(*)(p::TermContainer, r::RationalPoly) = p == r.den ? r.num : (p * r.num) / r.den |
50 |
| -(*)(r::RationalPoly, p::Polynomial) = p == r.den ? r.num : (r.num * p) / r.den |
51 |
| -(*)(r::RationalPoly, t::Term) = t == r.den ? r.num : (r.num * t) / r.den |
52 |
| -(*)(p::PolyType, r::RationalPoly) = TermContainer(p) * r |
53 |
| -(*)(r::RationalPoly, p::Monomial) = r * TermContainer(p) |
54 |
| -(*)(r::RationalPoly, p::PolyVar) = r * TermContainer(p) |
55 |
| -(*){C}(α, r::RationalPoly{C}) = TermContainer{C}(α) * r |
56 |
| -(*){C}(r::RationalPoly{C}, α) = r * TermContainer{C}(α) |
| 43 | +# Not type stable, currently it is a hack for SumOfSquares/test/SOSdemo2.jl:line 22 |
| 44 | +# We should take gcd between numerator and denominator instead and in sosdemo2, we should cast to polynomial manually |
| 45 | +(*)(p, r::RationalPoly) = p == r.den ? r.num : (p * r.num) / r.den |
| 46 | +(*)(r::RationalPoly, p) = p == r.den ? r.num : (r.num * p) / r.den |
57 | 47 |
|
58 |
| -zero(r::RationalPoly) = zero(r.num) |
59 |
| -zero{C, S, T}(::Type{RationalPoly{C, S, T}}) = zero(Polynomial{C, S}) |
| 48 | +zero{NT}(::RationalPoly{NT}) = zero(NT) |
| 49 | +zero{NT, DT}(::Type{RationalPoly{NT, DT}}) = zero(NT) |
0 commit comments