@@ -66,6 +66,7 @@ struct TruncatedPoly{K,T,TO} <: Number
66
66
end
67
67
const Max2Poly{T,TO} = TruncatedPoly{2 ,T,TO}
68
68
Max2Poly (a, b, maxorder) = TruncatedPoly ((a, b), maxorder)
69
+ Max2Poly {T,TO} (a, b, maxorder) where {T,TO} = TruncatedPoly {2,T,TO} ((a, b), maxorder)
69
70
70
71
function Base.:+ (a:: Max2Poly , b:: Max2Poly )
71
72
aa, ab = a. coeffs
@@ -83,40 +84,32 @@ function Base.:+(a::Max2Poly, b::Max2Poly)
83
84
end
84
85
end
85
86
86
- function Base.:+ (a:: TruncatedPoly{K} , b:: TruncatedPoly{K} ) where K
87
- if a. maxorder == b. maxorder
88
- return TruncatedPoly (a. coeffs .+ b. coeffs, a. maxorder)
89
- elseif a. maxorder > b. maxorder
90
- offset = a. maxorder - b. maxorder
91
- return TruncatedPoly (ntuple (i-> i+ offset <= K ? a. coeffs[i] + b. coeffs[i+ offset] : a. coeffs[i], K), a. maxorder)
92
- else
93
- offset = b. maxorder - a. maxorder
94
- return TruncatedPoly (ntuple (i-> i+ offset <= K ? b. coeffs[i] + a. coeffs[i+ offset] : b. coeffs[i], K), b. maxorder)
87
+ @generated function Base.:+ (a:: TruncatedPoly{K} , b:: TruncatedPoly{K} ) where K
88
+ quote
89
+ if a. maxorder == b. maxorder
90
+ return TruncatedPoly (a. coeffs .+ b. coeffs, a. maxorder)
91
+ elseif a. maxorder > b. maxorder
92
+ offset = a. maxorder - b. maxorder
93
+ return TruncatedPoly ((@ntuple $ K i-> i+ offset <= $ K ? a. coeffs[i] + b. coeffs[i+ offset] : a. coeffs[i]), a. maxorder)
94
+ else
95
+ offset = b. maxorder - a. maxorder
96
+ return TruncatedPoly ((@ntuple $ K i-> i+ offset <= $ K ? b. coeffs[i] + a. coeffs[i+ offset] : b. coeffs[i]), b. maxorder)
97
+ end
95
98
end
96
99
end
97
100
98
- function Base.:* (a:: Max2Poly , b:: Max2Poly )
99
- maxorder = a. maxorder + b. maxorder
100
- aa, ab = a. coeffs
101
- ba, bb = b. coeffs
102
- Max2Poly (aa* bb + ab* ba, ab * bb, maxorder)
103
- end
104
-
105
- function Base.:* (a:: TruncatedPoly{K,T} , b:: TruncatedPoly{K,T} ) where {K,T}
106
- maxorder = a. maxorder + b. maxorder
107
- TruncatedPoly (ntuple (K) do k
108
- r = zero (T)
109
- for i= 1 : K- k+ 1
110
- r += a. coeffs[i+ k- 1 ]* b. coeffs[K- i+ 1 ]
111
- end
112
- return r
113
- end , maxorder)
101
+ @generated function Base.:* (a:: TruncatedPoly{K,T} , b:: TruncatedPoly{K,T} ) where {K,T}
102
+ tupleexpr = Expr (:tuple , [K- k+ 1 > 1 ? Expr (:call , :+ , [:(a. coeffs[$ (i+ k- 1 )]* b. coeffs[$ (K- i+ 1 )]) for i= 1 : K- k+ 1 ]. .. ) : :(a. coeffs[$ k]* b. coeffs[$ K]) for k= 1 : K]. .. )
103
+ quote
104
+ maxorder = a. maxorder + b. maxorder
105
+ TruncatedPoly ($ tupleexpr, maxorder)
106
+ end
114
107
end
115
108
116
109
Base. zero (:: Type{TruncatedPoly{K,T,TO}} ) where {K,T,TO} = TruncatedPoly (ntuple (i-> zero (T), K), zero (Tropical{TO}). n)
117
110
Base. one (:: Type{TruncatedPoly{K,T,TO}} ) where {K,T,TO} = TruncatedPoly (ntuple (i-> i== K ? one (T) : zero (T), K), zero (TO))
118
- Base. zero (:: TruncatedPoly{K,T,TO} ) where {K,T,TO} = zero (TruncatedPoly{T,TO})
119
- Base. one (:: TruncatedPoly{K,T,TO} ) where {K,T,TO} = one (TruncatedPoly{T,TO})
111
+ Base. zero (:: TruncatedPoly{K,T,TO} ) where {K,T,TO} = zero (TruncatedPoly{K, T,TO})
112
+ Base. one (:: TruncatedPoly{K,T,TO} ) where {K,T,TO} = one (TruncatedPoly{K, T,TO})
120
113
121
114
Base. show (io:: IO , x:: TruncatedPoly ) = show (io, MIME " text/plain" (), x)
122
115
function Base. show (io:: IO , :: MIME"text/plain" , x:: TruncatedPoly{K} ) where K
0 commit comments