Skip to content

Commit 8fd923c

Browse files
authored
Merge pull request #80 from JuliaAlgebra/bl/import
Stop importing Base functions
2 parents edb4bde + 75da2f8 commit 8fd923c

File tree

7 files changed

+70
-75
lines changed

7 files changed

+70
-75
lines changed

src/MultivariatePolynomials.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ module MultivariatePolynomials
44

55
#using DocStringExtensions
66

7-
import Base: *, +, -, /, ^, ==,
8-
promote_rule, convert, show, isless, size, getindex,
9-
one, zero, isapprox, @pure, copy
107
using Compat
118
import Compat.LinearAlgebra: dot, norm
129

src/comparison.jl

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ Base.iszero(t::AbstractPolynomial) = iszero(nterms(t))
77

88
# See https://github.com/blegat/MultivariatePolynomials.jl/issues/22
99
# avoids the call to be transfered to eqconstant
10-
(==)(α::Nothing, x::APL) = false
11-
(==)(x::APL, α::Nothing) = false
12-
(==)(α::Dict, x::APL) = false
13-
(==)(x::APL, α::Dict) = false
14-
(==)(α::Nothing, x::RationalPoly) = false
15-
(==)(x::RationalPoly, α::Nothing) = false
16-
(==)(α::Dict, x::RationalPoly) = false
17-
(==)(x::RationalPoly, α::Dict) = false
10+
Base.:(==)(α::Nothing, x::APL) = false
11+
Base.:(==)(x::APL, α::Nothing) = false
12+
Base.:(==)(α::Dict, x::APL) = false
13+
Base.:(==)(x::APL, α::Dict) = false
14+
Base.:(==)(α::Nothing, x::RationalPoly) = false
15+
Base.:(==)(x::RationalPoly, α::Nothing) = false
16+
Base.:(==)(α::Dict, x::RationalPoly) = false
17+
Base.:(==)(x::RationalPoly, α::Dict) = false
1818

1919
function polyeqterm(p::AbstractPolynomial, t)
2020
if iszero(p)
@@ -40,7 +40,7 @@ eqconstant(t::AbstractTermLike, α) = _termeqconstant(t, α)
4040
eqconstant(α, p::APL) = polyeqterm(p, α)
4141
eqconstant(p::APL, α) = polyeqterm(p, α)
4242

43-
function (==)(t1::AbstractTerm, t2::AbstractTerm)
43+
function Base.:(==)(t1::AbstractTerm, t2::AbstractTerm)
4444
c1 = coefficient(t1)
4545
c2 = coefficient(t2)
4646
if iszero(c1)
@@ -49,8 +49,8 @@ function (==)(t1::AbstractTerm, t2::AbstractTerm)
4949
c1 == c2 && monomial(t1) == monomial(t2)
5050
end
5151
end
52-
(==)(p::AbstractPolynomial, t::AbstractTerm) = polyeqterm(p, t)
53-
(==)(t::AbstractTerm, p::AbstractPolynomial) = polyeqterm(p, t)
52+
Base.:(==)(p::AbstractPolynomial, t::AbstractTerm) = polyeqterm(p, t)
53+
Base.:(==)(t::AbstractTerm, p::AbstractPolynomial) = polyeqterm(p, t)
5454

5555
function compare_terms(p1::AbstractPolynomial, p2::AbstractPolynomial, isz, op)
5656
i1 = 1
@@ -89,14 +89,14 @@ end
8989
# end
9090
# return true
9191
#end
92-
(==)(p1::AbstractPolynomial, p2::AbstractPolynomial) = compare_terms(p1, p2, iszero, ==)
92+
Base.:(==)(p1::AbstractPolynomial, p2::AbstractPolynomial) = compare_terms(p1, p2, iszero, ==)
9393

94-
(==)(p::RationalPoly, q::RationalPoly) = p.num*q.den == q.num*p.den
94+
Base.:(==)(p::RationalPoly, q::RationalPoly) = p.num*q.den == q.num*p.den
9595
# Solve ambiguity with (::PolyType, ::Any)
96-
(==)(p::APL, q::RationalPoly) = p*q.den == q.num
97-
(==)(q::RationalPoly, p::APL) = p == q
98-
(==)(α, q::RationalPoly) = α*q.den == q.num
99-
(==)(q::RationalPoly, α) = α == q
96+
Base.:(==)(p::APL, q::RationalPoly) = p*q.den == q.num
97+
Base.:(==)(q::RationalPoly, p::APL) = p == q
98+
Base.:(==)(α, q::RationalPoly) = α*q.den == q.num
99+
Base.:(==)(q::RationalPoly, α) = α == q
100100

101101
# α could be a JuMP affine expression
102102
isapproxzero(α; ztol::Real=0.) = false

src/conversion.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export variable
22

33
convertconstant(::Type{P}, α) where P<:APL = P(α)
4-
convert(::Type{P}, α) where P<:APL = convertconstant(P, α)
5-
convert(::Type{P}, p::APL) where P<:AbstractPolynomial = P(polynomial(p))
4+
Base.convert(::Type{P}, α) where P<:APL = convertconstant(P, α)
5+
Base.convert(::Type{P}, p::APL) where P<:AbstractPolynomial = P(polynomial(p))
66

77
Base.convert(::Type{Any}, p::APL) = p
88
# Conversion polynomial -> scalar

src/differentiation.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# I do not use it but I import the function to add a method
21
export differentiate
32

43
"""
@@ -104,4 +103,4 @@ else
104103
differentiate(differentiate(p, x), x, Val{N - 1}())
105104
end
106105
end
107-
end
106+
end

src/operators.jl

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# We reverse the order of comparisons here so that the result
22
# of x < y is equal to the result of Monomial(x) < Monomial(y)
3-
@pure isless(v1::AbstractVariable, v2::AbstractVariable) = name(v1) > name(v2)
4-
isless(m1::AbstractTermLike, m2::AbstractTermLike) = isless(promote(m1, m2)...)
3+
Base.@pure Base.isless(v1::AbstractVariable, v2::AbstractVariable) = name(v1) > name(v2)
4+
Base.isless(m1::AbstractTermLike, m2::AbstractTermLike) = isless(promote(m1, m2)...)
55

6-
function isless(t1::AbstractTerm, t2::AbstractTerm)
6+
function Base.isless(t1::AbstractTerm, t2::AbstractTerm)
77
if monomial(t1) < monomial(t2)
88
true
99
elseif monomial(t1) == monomial(t2)
@@ -18,22 +18,22 @@ end
1818
# is a polynomial of JuMP affine expression but if we promote it would be a
1919
# polynomial of quadratic expression
2020
for op in [:+, :-, :(==)]
21-
@eval $op(p1::APL, p2::APL) = $op(promote(p1, p2)...)
21+
@eval Base.$op(p1::APL, p2::APL) = $op(promote(p1, p2)...)
2222
end
23-
isapprox(p1::APL, p2::APL; kwargs...) = isapprox(promote(p1, p2)...; kwargs...)
23+
Base.isapprox(p1::APL, p2::APL; kwargs...) = isapprox(promote(p1, p2)...; kwargs...)
2424

2525
# @eval $op(p::APL, α) = $op(promote(p, α)...) would be less efficient
2626
for (op, fun) in [(:+, :plusconstant), (:-, :minusconstant), (:*, :multconstant), (:(==), :eqconstant)]
27-
@eval $op(p::APL, α) = $fun(p, α)
28-
@eval $op(α, p::APL) = $fun(α, p)
27+
@eval Base.$op(p::APL, α) = $fun(p, α)
28+
@eval Base.$op(α, p::APL) = $fun(α, p)
2929
end
30-
isapprox(p::APL, α; kwargs...) = isapprox(promote(p, α)...; kwargs...)
31-
isapprox(α, p::APL; kwargs...) = isapprox(promote(p, α)...; kwargs...)
30+
Base.isapprox(p::APL, α; kwargs...) = isapprox(promote(p, α)...; kwargs...)
31+
Base.isapprox(α, p::APL; kwargs...) = isapprox(promote(p, α)...; kwargs...)
3232

33-
(-)(m::AbstractMonomialLike) = (-1) * m
34-
(-)(t::AbstractTermLike) = (-coefficient(t)) * monomial(t)
35-
(-)(p::APL) = polynomial((-).(terms(p)))
36-
(+)(p::Union{APL, RationalPoly}) = p
33+
Base.:-(m::AbstractMonomialLike) = (-1) * m
34+
Base.:-(t::AbstractTermLike) = (-coefficient(t)) * monomial(t)
35+
Base.:-(p::APL) = polynomial((-).(terms(p)))
36+
Base.:+(p::Union{APL, RationalPoly}) = p
3737

3838
# Avoid adding a zero constant that might artificially increase the Newton polytope
3939
# Need to add polynomial conversion for type stability
@@ -59,16 +59,16 @@ _multconstant(α, f, p::AbstractPolynomialLike) = _multconstant(α, f, polynomia
5959
multconstant(α, p::AbstractPolynomialLike) = _multconstant(α, β -> α*β, p)
6060
multconstant(p::AbstractPolynomialLike, α) = _multconstant(α, β -> β*α, p)
6161

62-
(*)(m1::AbstractMonomialLike, m2::AbstractMonomialLike) = mapexponents(+, m1, m2)
63-
#(*)(m1::AbstractMonomialLike, m2::AbstractMonomialLike) = *(monomial(m1), monomial(m2))
62+
Base.:*(m1::AbstractMonomialLike, m2::AbstractMonomialLike) = mapexponents(+, m1, m2)
63+
#Base.:*(m1::AbstractMonomialLike, m2::AbstractMonomialLike) = *(monomial(m1), monomial(m2))
6464

65-
(*)(m::AbstractMonomialLike, t::AbstractTermLike) = coefficient(t) * (m * monomial(t))
66-
(*)(t::AbstractTermLike, m::AbstractMonomialLike) = coefficient(t) * (monomial(t) * m)
67-
(*)(t1::AbstractTermLike, t2::AbstractTermLike) = (coefficient(t1) * coefficient(t2)) * (monomial(t1) * monomial(t2))
65+
Base.:*(m::AbstractMonomialLike, t::AbstractTermLike) = coefficient(t) * (m * monomial(t))
66+
Base.:*(t::AbstractTermLike, m::AbstractMonomialLike) = coefficient(t) * (monomial(t) * m)
67+
Base.:*(t1::AbstractTermLike, t2::AbstractTermLike) = (coefficient(t1) * coefficient(t2)) * (monomial(t1) * monomial(t2))
6868

69-
(*)(t::AbstractTermLike, p::APL) = polynomial(map(te -> t * te, terms(p)))
70-
(*)(p::APL, t::AbstractTermLike) = polynomial(map(te -> te * t, terms(p)))
71-
(*)(p::APL, q::APL) = polynomial(p) * polynomial(q)
69+
Base.:*(t::AbstractTermLike, p::APL) = polynomial(map(te -> t * te, terms(p)))
70+
Base.:*(p::APL, t::AbstractTermLike) = polynomial(map(te -> te * t, terms(p)))
71+
Base.:*(p::APL, q::APL) = polynomial(p) * polynomial(q)
7272

7373
# guaranteed that monomial(t1) > monomial(t2)
7474
function _polynomial_2terms(t1::TT, t2::TT, ::Type{T}) where {TT<:AbstractTerm, T}
@@ -82,9 +82,9 @@ function _polynomial_2terms(t1::TT, t2::TT, ::Type{T}) where {TT<:AbstractTerm,
8282
end
8383
for op in [:+, :-]
8484
@eval begin
85-
$op(t1::AbstractTermLike, t2::AbstractTermLike) = $op(term(t1), term(t2))
86-
$op(t1::AbstractTerm, t2::AbstractTerm) = $op(promote(t1, t2)...)
87-
function $op(t1::TT, t2::TT) where {T, TT <: AbstractTerm{T}}
85+
Base.$op(t1::AbstractTermLike, t2::AbstractTermLike) = $op(term(t1), term(t2))
86+
Base.$op(t1::AbstractTerm, t2::AbstractTerm) = $op(promote(t1, t2)...)
87+
function Base.$op(t1::TT, t2::TT) where {T, TT <: AbstractTerm{T}}
8888
S = Base.promote_op($op, T, T)
8989
# t1 > t2 would compare the coefficient in case the monomials are equal
9090
# and it will throw a MethodError in case the coefficients are not comparable
@@ -104,9 +104,9 @@ adjoint_operator(m::AbstractMonomial) = m
104104
adjoint_operator(t::T) where {T <: AbstractTerm} = adjoint_operator(coefficient(t)) * monomial(t)
105105
adjoint_operator(p::AbstractPolynomialLike) = polynomial(map(adjoint_operator, terms(p)))
106106

107-
dot(p1::AbstractPolynomialLike, p2::AbstractPolynomialLike) = p1' * p2
108-
dot(x, p::AbstractPolynomialLike) = x' * p
109-
dot(p::AbstractPolynomialLike, x) = p' * x
107+
Base.dot(p1::AbstractPolynomialLike, p2::AbstractPolynomialLike) = p1' * p2
108+
Base.dot(x, p::AbstractPolynomialLike) = x' * p
109+
Base.dot(p::AbstractPolynomialLike, x) = p' * x
110110

111111
# Amazingly, this works! Thanks, StaticArrays.jl!
112112
"""
@@ -117,4 +117,4 @@ Base.vec(vars::Tuple{Vararg{AbstractVariable}}) = [vars...]
117117
# vec(vars::Tuple{Vararg{<:TypedVariable}}) = SVector(vars)
118118

119119
# https://github.com/JuliaLang/julia/pull/23332
120-
^(x::AbstractPolynomialLike, p::Integer) = Base.power_by_squaring(x, p)
120+
Base.:^(x::AbstractPolynomialLike, p::Integer) = Base.power_by_squaring(x, p)

src/rational.jl

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export RationalPoly
2-
import Base.+, Base.-, Base.*, Base./
32

43
# TODO We should take gcd between numerator and denominator
54
struct RationalPoly{NT <: APL, DT <: APL}
@@ -25,43 +24,43 @@ end
2524

2625
Base.inv(r::RationalPoly) = r.den / r.num
2726
Base.inv(p::APL{T}) where T = one(T) / p
28-
(/)(r::RationalPoly, p) = r.num / (r.den * p)
29-
function (/)(num::NT, den::DT) where {NT <: APL, DT <: APL}
27+
Base.:/(r::RationalPoly, p) = r.num / (r.den * p)
28+
function Base.:/(num::NT, den::DT) where {NT <: APL, DT <: APL}
3029
RationalPoly{NT, DT}(num, den)
3130
end
32-
function (/)(num, den::APL)
31+
function Base.:/(num, den::APL)
3332
constantterm(num, den) / den
3433
end
3534
# Polynomial divided by coefficient is a polynomial not a rational polynomial
3635
# (1/den) * num would not be correct in case of noncommutative coefficients
37-
(/)(num::APL, den) = mapcoefficientsnz-> α/den, num)
36+
Base.:/(num::APL, den) = mapcoefficientsnz-> α/den, num)
3837

39-
function (+)(r::RationalPoly, s::RationalPoly)
38+
function Base.:+(r::RationalPoly, s::RationalPoly)
4039
(r.num*s.den + r.den*s.num) / (r.den * s.den)
4140
end
4241
function _plus(r::RationalPoly, p)
4342
(p*r.den + r.num) / r.den
4443
end
45-
(+)(p::APL, r::RationalPoly) = _plus(r, p)
46-
(+)(r::RationalPoly, p::APL) = _plus(r, p)
47-
(+)(r::RationalPoly, α) = _plus(r, α)
48-
(+)(α, r::RationalPoly) = _plus(r, α)
49-
function (-)(r::RationalPoly, s::RationalPoly)
44+
Base.:+(p::APL, r::RationalPoly) = _plus(r, p)
45+
Base.:+(r::RationalPoly, p::APL) = _plus(r, p)
46+
Base.:+(r::RationalPoly, α) = _plus(r, α)
47+
Base.:+(α, r::RationalPoly) = _plus(r, α)
48+
function Base.:-(r::RationalPoly, s::RationalPoly)
5049
(r.num*s.den - r.den*s.num) / (r.den * s.den)
5150
end
5251
_minus(p, s::RationalPoly) = (p * s.den - s.num) / s.den
5352
_minus(s::RationalPoly, p) = (s.num - p * s.den) / s.den
54-
(-)(p::APL, r::RationalPoly) = _minus(p, r)
55-
(-)(r::RationalPoly, p::APL) = _minus(r, p)
56-
(-)(r::RationalPoly, α) = _minus(r, α)
57-
(-)(α, r::RationalPoly) = _minus(α, r)
58-
(-)(r::RationalPoly) = (-r.num) / r.den
53+
Base.:-(p::APL, r::RationalPoly) = _minus(p, r)
54+
Base.:-(r::RationalPoly, p::APL) = _minus(r, p)
55+
Base.:-(r::RationalPoly, α) = _minus(r, α)
56+
Base.:-(α, r::RationalPoly) = _minus(α, r)
57+
Base.:-(r::RationalPoly) = (-r.num) / r.den
5958

60-
(*)(r::RationalPoly, s::RationalPoly) = (r.num*s.num) / (r.den*s.den)
61-
(*)(p::APL, r::RationalPoly) = (p * r.num) / r.den
62-
(*)(r::RationalPoly, p::APL) = (r.num * p) / r.den
63-
(*)(α, r::RationalPoly) =* r.num) / r.den
64-
(*)(r::RationalPoly, α) = (r.num * α) / r.den
59+
Base.:*(r::RationalPoly, s::RationalPoly) = (r.num*s.num) / (r.den*s.den)
60+
Base.:*(p::APL, r::RationalPoly) = (p * r.num) / r.den
61+
Base.:*(r::RationalPoly, p::APL) = (r.num * p) / r.den
62+
Base.:*(α, r::RationalPoly) =* r.num) / r.den
63+
Base.:*(r::RationalPoly, α) = (r.num * α) / r.den
6564

6665
Base.zero(::RationalPoly{NT}) where {NT} = zero(NT)
6766
Base.zero(::Type{RationalPoly{NT, DT}}) where {NT, DT} = zero(NT)

src/show.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
function show(io::IO, v::AbstractVariable)
1+
function Base.show(io::IO, v::AbstractVariable)
22
print(io, name(v))
33
end
44

55
if VERSION < v"0.7.0-DEV.1144" # Define `isone` for base types JuliaLang/julia#22846
66
isone(x::T) where T = x == one(T)
77
end
88

9-
function show(io::IO, m::AbstractMonomial)
9+
function Base.show(io::IO, m::AbstractMonomial)
1010
if isconstant(m)
1111
print(io, '1')
1212
else

0 commit comments

Comments
 (0)