Skip to content

Commit

Permalink
aqua test
Browse files Browse the repository at this point in the history
  • Loading branch information
jverzani committed Aug 5, 2023
1 parent a92bf2f commit 77a8798
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 32 deletions.
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Setfield = "1"
julia = "1.6"

[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a"
DualNumbers = "fa6b7ba4-c1ee-5f82-b5fc-ecf0adba8f74"
Expand All @@ -43,4 +44,4 @@ SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["ChainRulesCore", "DualNumbers", "LinearAlgebra", "SparseArrays", "OffsetArrays", "SpecialFunctions", "Test"]
test = ["Aqua", "ChainRulesCore", "DualNumbers", "LinearAlgebra", "SparseArrays", "OffsetArrays", "SpecialFunctions", "Test"]
19 changes: 15 additions & 4 deletions src/abstract-polynomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ Base.:-(p::AbstractUnivariatePolynomial) = map(-, p) #scalar_mult(-1, p)

Base.:+(c::Scalar, p::AbstractUnivariatePolynomial) = scalar_add(c, p)
Base.:+(p::AbstractUnivariatePolynomial, c::Scalar) = scalar_add(c, p)
scalar_add(p::AbstractUnivariatePolynomial, c) = scalar_add(c, p) # scalar addition is commutative

Base.:+(p::AbstractUnivariatePolynomial) = p
Base.:+(p::AbstractUnivariatePolynomial{B, T, X},
Expand Down Expand Up @@ -249,14 +248,26 @@ Base.:/(p::AbstractUnivariatePolynomial, c::Scalar) = scalar_div(p, c)

Base.:^(p::AbstractUnivariatePolynomial, n::Integer) = Base.power_by_squaring(p, n)

scalar_mult(p::AbstractUnivariatePolynomial{B,T,X}, c) where {B,T,X} = map(Base.Fix2(*,c), p)
scalar_mult(c, p::AbstractUnivariatePolynomial{B,T,X}) where {B,T,X} = map(Base.Fix1(*,c), p)
scalar_div(p::AbstractUnivariatePolynomial{B,T,X}, c) where {B,T,X} = map(Base.Fix2(*,one(T)/c), p) # much better than Fix2(/,c)
scalar_mult(p::AbstractUnivariatePolynomial{B,T,X}, c::Scalar) where {B,T,X} = map(Base.Fix2(*,c), p)
scalar_mult(c::Scalar, p::AbstractUnivariatePolynomial{B,T,X}) where {B,T,X} = map(Base.Fix1(*,c), p)
scalar_div(p::AbstractUnivariatePolynomial{B,T,X}, c::Scalar) where {B,T,X} = map(Base.Fix2(*,one(T)/c), p) # much better than Fix2(/,c)

# treat constant polynomials as constants when symbols mixed
scalar_op(::typeof(*)) = scalar_mult
scalar_op(::typeof(+)) = scalar_add
scalar_op(::typeof(/)) = scalar_div

Check warning on line 258 in src/abstract-polynomial.jl

View check run for this annotation

Codecov / codecov/patch

src/abstract-polynomial.jl#L257-L258

Added lines #L257 - L258 were not covered by tests

function _mixed_symbol_op(::typeof(+),
p::P,
q::Q) where {B,T,S,X,Y,
P<:AbstractUnivariatePolynomial{B, T, X},
Q<:AbstractUnivariatePolynomial{B, S, Y}}
X == Y && throw(ArgumentError("dispatch should catch this case"))
isconstant(p) && return scalar_add(constantterm(p), q)
isconstant(q) && return scalar_add(constantterm(q), p) # only the one (c,p) signature
assert_same_variable(X,Y)
end

function _mixed_symbol_op(op,
p::P,
q::Q) where {B,T,S,X,Y,
Expand Down
23 changes: 15 additions & 8 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,10 @@ truncate!(ps::NTuple; kwargs...) = throw(ArgumentError("`truncate!` not defined.

_truncate(ps::NTuple{0}; kwargs...) = ps
function _truncate(ps::NTuple{N,T};
rtol::Real = Base.rtoldefault(real(T)),
atol::Real = 0,) where {N,T}
rtol::Real = Base.rtoldefault(real(T)),
atol::Real = 0,) where {N,T}


thresh = norm(ps, Inf) * rtol + atol
return NTuple{N,T}(abs(pᵢ) <= thresh ? zero(T) : pᵢ for pᵢ values(ps))
end
Expand Down Expand Up @@ -432,9 +434,14 @@ end
chop!(ps::NTuple; kwargs...) = throw(ArgumentError("chop! not defined"))

_chop(ps::NTuple{0}; kwargs...) = ps
function _chop(ps::NTuple{N,T};
rtol::Real = Base.rtoldefault(real(T)),
atol::Real = 0,) where {N,T}
function _chop(ps::NTuple{N};

Check warning on line 437 in src/common.jl

View check run for this annotation

Codecov / codecov/patch

src/common.jl#L437

Added line #L437 was not covered by tests
rtol = nothing,
atol = nothing) where {N}

T = real(eltype(ps))
rtol = something(rtol, Base.rtoldefault(T))
atol = somtething(atol, zero(T))

Check warning on line 443 in src/common.jl

View check run for this annotation

Codecov / codecov/patch

src/common.jl#L441-L443

Added lines #L441 - L443 were not covered by tests

thresh = norm(ps, Inf) * rtol + atol
for i in N:-1:1
if abs(ps[i]) > thresh
Expand Down Expand Up @@ -990,10 +997,10 @@ Base.:-(p1::AbstractPolynomial, p2::AbstractPolynomial) = +(p1, -p2)
Base.:+(p::AbstractPolynomial) = p

# polynomial + scalar; implicit identification of c with c*one(p)
Base.:+(p::P, c::T) where {T,X, P<:AbstractPolynomial{T,X}} = scalar_add(p, c)
Base.:+(p::P, c::T) where {T,X, P<:AbstractPolynomial{T,X}} = scalar_add(c, p)

# default scalar add can be problematic as addition of contant polynomial might circle back
scalar_add(p::AbstractPolynomial, c) = p + c * one(p)
scalar_add(c::S, p::AbstractPolynomial) where {S} = p + c * one(p)

## ----

Expand Down Expand Up @@ -1040,7 +1047,7 @@ function ⊕(P::Type{<:AbstractPolynomial}, p1::Vector{T}, p2::Vector{S}) where
end

# Padded vector sum of two tuples assuming N ≥ M
@generated function (P::Type{<:AbstractPolynomial}, p1::NTuple{N,T}, p2::NTuple{M,S}) where {T,N,S,M}
@generated function (P::Type{<:AbstractPolynomial}, p1::NTuple{N}, p2::NTuple{M}) where {N,M}

Check warning on line 1050 in src/common.jl

View check run for this annotation

Codecov / codecov/patch

src/common.jl#L1050

Added line #L1050 was not covered by tests
exprs = Any[nothing for i = 1:N]
for i in 1:M
exprs[i] = :(p1[$i] + p2[$i])
Expand Down
18 changes: 9 additions & 9 deletions src/polynomial-container-types/immutable-dense-polynomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Immutable is a bit of a misnomer, as using the `@set!` macro from `Setfield.jl`
"""
struct ImmutableDensePolynomial{B,T,X,N} <: AbstractDenseUnivariatePolynomial{B,T,X}
coeffs::NTuple{N,T}
function ImmutableDensePolynomial{B,T,X,N}(cs::NTuple{N,S}) where {B,N,T,X,S}
function ImmutableDensePolynomial{B,T,X,N}(cs::NTuple{N}) where {B,N,T,X}
new{B,T,Symbol(X),N}(cs)
end
end
Expand Down Expand Up @@ -42,15 +42,15 @@ function ImmutableDensePolynomial{B,T,X,N}(c::S) where {B,T,X,N,S<:Scalar}
return ImmutableDensePolynomial{B,T,X,N}(cs)
end
ImmutableDensePolynomial{B,T,X}(::Val{false}, xs::NTuple{N,S}) where {B,T,S,X,N} = ImmutableDensePolynomial{B,T,X,N}(convert(NTuple{N,T}, xs))

Check warning on line 44 in src/polynomial-container-types/immutable-dense-polynomial.jl

View check run for this annotation

Codecov / codecov/patch

src/polynomial-container-types/immutable-dense-polynomial.jl#L44

Added line #L44 was not covered by tests
ImmutableDensePolynomial{B,T,X}(xs::NTuple{N,S}) where {B,T,S,X,N} = ImmutableDensePolynomial{B,T,X,N}(convert(NTuple{N,T}, xs))
ImmutableDensePolynomial{B,T}(xs::NTuple{N,S}, var::SymbolLike=Var(:x)) where {B,T,S,N} = ImmutableDensePolynomial{B,T,Symbol(var),N}(xs)
ImmutableDensePolynomial{B,T,X}(xs::NTuple{N}) where {B,T,X,N} = ImmutableDensePolynomial{B,T,X,N}(convert(NTuple{N,T}, xs))
ImmutableDensePolynomial{B,T}(xs::NTuple{N}, var::SymbolLike=Var(:x)) where {B,T,N} = ImmutableDensePolynomial{B,T,Symbol(var),N}(xs)
ImmutableDensePolynomial{B}(xs::NTuple{N,T}, var::SymbolLike=Var(:x)) where {B,T,N} = ImmutableDensePolynomial{B,T,Symbol(var),N}(xs)

# abstract vector. Must eat order
ImmutableDensePolynomial{B,T,X}(::Val{false}, xs::AbstractVector{S}, order::Int=0) where {B,T,X,S} =
ImmutableDensePolynomial{B,T,X}(::Val{false}, xs::AbstractVector, order::Int=0) where {B,T,X} =

Check warning on line 50 in src/polynomial-container-types/immutable-dense-polynomial.jl

View check run for this annotation

Codecov / codecov/patch

src/polynomial-container-types/immutable-dense-polynomial.jl#L50

Added line #L50 was not covered by tests
ImmutableDensePolynomial{B,T,X}(xs, order)

function ImmutableDensePolynomial{B,T,X}(xs::AbstractVector{S}, order::Int=0) where {B,T,X,S}
function ImmutableDensePolynomial{B,T,X}(xs::AbstractVector, order::Int=0) where {B,T,X}
if Base.has_offset_axes(xs)
@warn "ignoring the axis offset of the coefficient vector"
xs = parent(xs)
Expand Down Expand Up @@ -255,13 +255,13 @@ end


# scalar mult faster with 0 default
scalar_mult(p::ImmutableDensePolynomial{B,T,X,0}, c::S) where {B,T,X,S} = zero(ImmutableDensePolynomial{B,promote_type(T,S),X,0})
scalar_mult(c::S, p::ImmutableDensePolynomial{B,T,X,0}) where {B,T,X,S} = zero(ImmutableDensePolynomial{B,promote_type(T,S),X,0})
scalar_mult(p::ImmutableDensePolynomial{B,T,X,0}, c::S) where {B,T,X,S<:Scalar} = zero(ImmutableDensePolynomial{B,promote_type(T,S),X,0})
scalar_mult(c::S, p::ImmutableDensePolynomial{B,T,X,0}) where {B,T,X,S<:Scalar} = zero(ImmutableDensePolynomial{B,promote_type(T,S),X,0})


## ---
# Padded vector combination of two homogeneous tuples assuming N ≥ M
@generated function _tuple_combine(op, p1::NTuple{N,T}, p2::NTuple{M,S}) where {T,N,S,M}
@generated function _tuple_combine(op, p1::NTuple{N}, p2::NTuple{M}) where {N,M}

exprs = Any[nothing for i = 1:N]
for i in 1:M
Expand All @@ -282,7 +282,7 @@ end
## Static size of product makes generated functions a good choice
## from https://github.com/tkoolen/StaticUnivariatePolynomials.jl/blob/master/src/monomial_basis.jl
## convolution of two tuples
@generated function fastconv(p1::NTuple{N,T}, p2::NTuple{M,S}) where {T,N,S,M}
@generated function fastconv(p1::NTuple{N}, p2::NTuple{M}) where {N,M}
P = M + N - 1
exprs = Any[nothing for i = 1 : P]
for i in 1 : N
Expand Down
13 changes: 3 additions & 10 deletions src/polynomials/ngcd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,10 @@ The main entry point for this function is `gcd(p, q, method=:numerical)`, but `n
In the case `degree(p) ≫ degree(q)`, a heuristic is employed to first call one step of the Euclidean gcd approach, and then call `ngcd` with relaxed tolerances.
"""
function ngcd(p::P, q::Q, args...; kwargs...) where {T,X,P<:StandardBasisPolynomial{T,X},
S,Y,Q<:StandardBasisPolynomial{S,Y}}
isconstant(p) && return (u=one(1), v=p,w=q, θ=NaN, κ=NaN)
isconstant(q) && return (u=one(p), v=p,w=q, θ=NaN, κ=NaN)
assert_same_variable(X,Y)
ngcd(promote(p,q)..., args...; kwargs...)
end

function ngcd(p::P, q::P,
function ngcd(p::P, q::Q,
args...;
kwargs...) where {T,X,P<:StandardBasisPolynomial{T,X}}
kwargs...) where {T,X,P<:StandardBasisPolynomial{T,X},
S,Y,Q<:StandardBasisPolynomial{S,Y}}
if (degree(q) > degree(p))
u,w,v,Θ,κ = ngcd(q,p,args...; kwargs...)
return (u=u,v=v,w=w, Θ=Θ, κ=κ)
Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[deps]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MutableArithmetics = "d8a4904e-b15c-11e9-3269-09a3773c0cb0"
Expand Down
6 changes: 6 additions & 0 deletions test/aqua.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using Aqua

Aqua.test_all(Polynomials;
unbound_args = false,
stale_deps = false
)
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ using OffsetArrays
if VERSION >= v"1.9.0-"
@testset "MutableArithmetics" begin include("mutable-arithmetics.jl") end
end
@testset "Aqua" begin include("aqua.jl") end

0 comments on commit 77a8798

Please sign in to comment.