Skip to content

Commit 29c2260

Browse files
authored
Change default of algebraic structure to field (#192)
* Change default of algebraic structure to field * Fix gcd use of algebraic_structure
1 parent df08067 commit 29c2260

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/division.jl

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,19 @@ struct Field end
3535
struct UniqueFactorizationDomain end
3636
const UFD = UniqueFactorizationDomain
3737

38-
algebraic_structure(::Type{<:Union{Rational, AbstractFloat}}, ::Type{<:Union{Rational, AbstractFloat}}) = Field()
39-
algebraic_structure(::Type{<:Union{Rational, AbstractFloat}}, ::Type) = Field()
40-
algebraic_structure(::Type, ::Type{<:Union{Rational, AbstractFloat}}) = Field()
41-
algebraic_structure(::Type, ::Type) = UFD()
38+
algebraic_structure(::Type{<:Integer}) = UFD()
39+
algebraic_structure(::Type{<:AbstractPolynomialLike}) = UFD()
40+
# `Rational`, `AbstractFloat`, JuMP expressions, etc... are fields
41+
algebraic_structure(::Type) = Field()
42+
_field_absorb(::UFD, ::UFD) = UFD()
43+
_field_absorb(::UFD, ::Field) = Field()
44+
_field_absorb(::Field, ::UFD) = Field()
45+
_field_absorb(::Field, ::Field) = Field()
4246

4347
# _div(a, b) assumes that b divides a
4448
_div(::Field, a, b) = a / b
4549
_div(::UFD, a, b) = div(a, b)
46-
_div(a, b) = _div(algebraic_structure(typeof(a), typeof(b)), a, b)
50+
_div(a, b) = _div(algebraic_structure(promote_type(typeof(a), typeof(b))), a, b)
4751
_div(m1::AbstractMonomialLike, m2::AbstractMonomialLike) = mapexponents(-, m1, m2)
4852
function _div(t::AbstractTerm, m::AbstractMonomial)
4953
term(coefficient(t), _div(monomial(t), m))
@@ -77,7 +81,7 @@ Base.div(f::APL, g::Union{APL, AbstractVector{<:APL}}; kwargs...) = divrem(f, g;
7781
Base.rem(f::APL, g::Union{APL, AbstractVector{<:APL}}; kwargs...) = divrem(f, g; kwargs...)[2]
7882

7983
function pseudo_divrem(f::APL{S}, g::APL{T}, algo) where {S,T}
80-
return _pseudo_divrem(algebraic_structure(S, T), f, g, algo)
84+
return _pseudo_divrem(algebraic_structure(MA.promote_operation(-, S, T)), f, g, algo)
8185
end
8286

8387
function _pseudo_divrem(::Field, f::APL, g::APL, algo)
@@ -102,7 +106,7 @@ function _pseudo_divrem(::UFD, f::APL, g::APL, algo)
102106
end
103107

104108
function pseudo_rem(f::APL{S}, g::APL{T}, algo) where {S,T}
105-
return _pseudo_rem(algebraic_structure(S, T), f, g, algo)
109+
return _pseudo_rem(algebraic_structure(MA.promote_operation(-, S, T)), f, g, algo)
106110
end
107111

108112
function _pseudo_rem(::Field, f::APL, g::APL, algo)
@@ -137,7 +141,7 @@ function MA.promote_operation(
137141
::Type{P},
138142
::Type{Q},
139143
) where {T,S,P<:APL{T},Q<:APL{S}}
140-
return _promote_operation(algebraic_structure(T, S), pseudo_rem, P, Q)
144+
return _promote_operation(algebraic_structure(MA.promote_operation(-, S, T)), pseudo_rem, P, Q)
141145
end
142146
function _promote_operation(
143147
::Field,

src/gcd.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ If the coefficients are not `AbstractFloat`, this
462462
Addison-Wesley Professional. Third edition.
463463
"""
464464
function univariate_gcd(p1::APL{S}, p2::APL{T}, algo::AbstractUnivariateGCDAlgorithm) where {S,T}
465-
return univariate_gcd(algebraic_structure(S, T), p1, p2, algo)
465+
return univariate_gcd(_field_absorb(algebraic_structure(S), algebraic_structure(T)), p1, p2, algo)
466466
end
467467
function univariate_gcd(::UFD, p1::APL, p2::APL, algo::AbstractUnivariateGCDAlgorithm)
468468
f1, g1 = primitive_part_content(p1, algo)
@@ -478,7 +478,7 @@ function univariate_gcd(::Field, p1::APL, p2::APL, algo::AbstractUnivariateGCDAl
478478
end
479479

480480
function univariate_gcdx(p1::APL{S}, p2::APL{T}, algo::AbstractUnivariateGCDAlgorithm) where {S,T}
481-
return univariate_gcdx(algebraic_structure(S, T), p1, p2, algo)
481+
return univariate_gcdx(_field_absorb(algebraic_structure(S), algebraic_structure(T)), p1, p2, algo)
482482
end
483483
function univariate_gcdx(::UFD, p1::APL, p2::APL, algo::AbstractUnivariateGCDAlgorithm)
484484
f1, g1 = primitive_part_content(p1, algo)

0 commit comments

Comments
 (0)