Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jishnub aqua #549

Merged
merged 2 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ PolynomialsMakieCoreExt = "MakieCore"
PolynomialsMutableArithmeticsExt = "MutableArithmetics"

[compat]
Aqua = "0.6, 0.7"
Aqua = "0.8"
ChainRulesCore = "1"
ChainRulesTestUtils = "1"
DualNumbers = "0.6"
Expand Down
108 changes: 77 additions & 31 deletions src/polynomial-container-types/immutable-dense-polynomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,35 @@
"""
struct ImmutableDensePolynomial{B,T,X,N} <: AbstractDenseUnivariatePolynomial{B,T,X}
coeffs::NTuple{N,T}
function ImmutableDensePolynomial{B,T,X,N}(cs::NTuple{N}) where {B,N,T,X}
function ImmutableDensePolynomial{B,T,X,N}(cs::NTuple{N,T}) where {B,N,T,X}
new{B,T,Symbol(X),N}(cs)
end

# tuple with mismatched size/type
# need zero(T) defined if padding needed
function ImmutableDensePolynomial{B,T,X,N}(xs::Tuple{Vararg}) where {B,T,X,N}
M = length(xs)
N′ = findlast(!iszero, xs)
N < N′ && throw(ArgumentError("Too many coefficients for type"))
if N == N′ == M
cs = T.(xs)
elseif isnothing(N′)
cs = ntuple(i -> zero(T), Val(N))

Check warning on line 28 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#L28

Added line #L28 was not covered by tests
else
cs = ntuple(i -> i <= N′ ? T(xs[i]) : zero(first(xs)), Val(N))
end
new{B,T,X,N}(cs)
end

# zero polynomial
function ImmutableDensePolynomial{B,T,X,N}(cs::Tuple{}) where {B,T,X,N}
cs = ntuple(i->zero(T), Val(N))
new{B,T,Symbol(X),N}(cs)
end
function ImmutableDensePolynomial{B,T,X,0}(cs::Tuple{}) where {B,T,X}
new{B,T,Symbol(X),0}(())
end

end

ImmutableDensePolynomial{B,T,X,N}(check::Type{Val{false}}, cs::NTuple{N,T}) where {B,N,T,X} =
Expand All @@ -23,33 +49,38 @@
ImmutableDensePolynomial{B,T,X,N}(check::Type{Val{true}}, cs::NTuple{N,T}) where {B,N, T,X} =
ImmutableDensePolynomial{B,T,X,N}(cs)

# tuple with mismatched size
function ImmutableDensePolynomial{B,T,X,N}(xs::NTuple{M,S}) where {B,T,S,X,N,M}
p = ImmutableDensePolynomial{B,S,X,M}(xs)
convert(ImmutableDensePolynomial{B,T,X,N}, ImmutableDensePolynomial{B,T,X,M}(xs))
end

# vector case with N
function ImmutableDensePolynomial{B,T,X,N}(xs::AbstractVector{S}) where {B,T,S,X,N}
ImmutableDensePolynomial{B,T,X,N}(ntuple(Base.Fix1(getindex, xs), Val(N)))
end
end

# constant
function ImmutableDensePolynomial{B,T,X,N}(c::S) where {B,T,X,N,S<:Scalar}
if N == 0
if iszero(c)
throw(ArgumentError("Can't create zero-length polynomial"))
else
return zero(ImmutableDensePolynomial{B,T,X})
end
end
cs = ntuple(i -> i == 1 ? T(c) : zero(T), Val(N))
return ImmutableDensePolynomial{B,T,X,N}(cs)
return ImmutableDensePolynomial{B,T,X,N}((c,))
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))
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)

function ImmutableDensePolynomial{B,T,X}(::Val{false}, xs::Tuple{S,Vararg{S}}) where {B,T,S,X}
N = length(xs)
ImmutableDensePolynomial{B,T,X,N}(convert(NTuple{N,T}, xs))

Check warning on line 64 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#L62-L64

Added lines #L62 - L64 were not covered by tests
end

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,X}(xs::Tuple{}) where {B,T,X} =

Check warning on line 70 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#L70

Added line #L70 was not covered by tests
ImmutableDensePolynomial{B,T,X,0}(())

ImmutableDensePolynomial{B,T}(xs::NTuple{N}, var::SymbolLike=Var(:x)) where {B,T,N} =
ImmutableDensePolynomial{B,T,Symbol(var),N}(xs)

ImmutableDensePolynomial{B,T}(xs::Tuple{}, var::SymbolLike=Var(:x)) where {B,T} =

Check warning on line 76 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#L76

Added line #L76 was not covered by tests
ImmutableDensePolynomial{B,T,Symbol(var),0}(xs)

ImmutableDensePolynomial{B}(xs::Tuple{T,Vararg{T}}, var::SymbolLike=Var(:x)) where {B,T} =
ImmutableDensePolynomial{B,T,Symbol(var),length(xs)}(xs)

ImmutableDensePolynomial{B}(xs::Tuple{}, var::SymbolLike=Var(:x)) where {B} =

Check warning on line 82 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#L82

Added line #L82 was not covered by tests
ImmutableDensePolynomial{B,Float64,Symbol(var),0}(xs)

# abstract vector. Must eat order
ImmutableDensePolynomial{B,T,X}(::Val{false}, xs::AbstractVector, order::Int=0) where {B,T,X} =
Expand Down Expand Up @@ -80,18 +111,37 @@

function Base.convert(::Type{<:ImmutableDensePolynomial{B,T,X,N}},
p::ImmutableDensePolynomial{B,T′,X,N′}) where {B,T,T′,X,N,N′}
N′′ = findlast(!iszero, p)
isnothing(N′′) && return zero(ImmutableDensePolynomial{B,T,X,N})
N < N′′ && throw(ArgumentError("Wrong size"))
ImmutableDensePolynomial{B,T,X,N}(ntuple(i -> T(p[i-1]), Val(N)))
ImmutableDensePolynomial{B,T,X,N}(p.coeffs)
end

function Base.map(fn, p::P, args...) where {B,T,X, P<:ImmutableDensePolynomial{B,T,X}}
function Base.map(fn, p::P, args...) where {B,T,X,N, P<:ImmutableDensePolynomial{B,T,X,N}}
xs = map(fn, p.coeffs, args...)
R = eltype(xs)
return ImmutableDensePolynomial{B,R,X}(xs)
return ImmutableDensePolynomial{B,R,X,N}(xs)
end

# map has a type instability
function scalar_mult(p::P, c::S) where {B, S<:Scalar, T, X, N, P<:ImmutableDensePolynomial{B,T,X,N}}
R = Base.promote_op(*, T, S)
#cs = map(Base.Fix2(*,c), p.coeffs)
cs = p.coeffs .* (c,)

ImmutableDensePolynomial{B,R,X,N}(cs)
end

function scalar_mult(c::S, p::P) where {B, S<:Scalar, T, X, N, P<:ImmutableDensePolynomial{B,T,X,N}}
R = Base.promote_op(*, T, S)
#cs = map(Base.Fix1(*,c), p.coeffs)
cs = (c,) .* p.coeffs
ImmutableDensePolynomial{B,R,X,N}(cs)
end

# scalar mult faster with 0 default
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})




Base.copy(p::ImmutableDensePolynomial) = p
Base.similar(p::ImmutableDensePolynomial, args...) = p.coeffs
Expand Down Expand Up @@ -257,10 +307,6 @@
end


# scalar mult faster with 0 default
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
Expand Down
5 changes: 1 addition & 4 deletions test/aqua.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using Aqua

Aqua.test_all(Polynomials;
unbound_args = false,
stale_deps = false
)
Aqua.test_all(Polynomials)
Loading