@@ -66,6 +66,7 @@ struct TruncatedPoly{K,T,TO} <: Number
6666end
6767const Max2Poly{T,TO} = TruncatedPoly{2 ,T,TO}
6868Max2Poly (a, b, maxorder) = TruncatedPoly ((a, b), maxorder)
69+ Max2Poly {T,TO} (a, b, maxorder) where {T,TO} = TruncatedPoly {2,T,TO} ((a, b), maxorder)
6970
7071function Base.:+ (a:: Max2Poly , b:: Max2Poly )
7172 aa, ab = a. coeffs
@@ -83,40 +84,32 @@ function Base.:+(a::Max2Poly, b::Max2Poly)
8384 end
8485end
8586
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
9598 end
9699end
97100
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
114107end
115108
116109Base. zero (:: Type{TruncatedPoly{K,T,TO}} ) where {K,T,TO} = TruncatedPoly (ntuple (i-> zero (T), K), zero (Tropical{TO}). n)
117110Base. 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})
120113
121114Base. show (io:: IO , x:: TruncatedPoly ) = show (io, MIME " text/plain" (), x)
122115function Base. show (io:: IO , :: MIME"text/plain" , x:: TruncatedPoly{K} ) where K
0 commit comments