Skip to content

Commit 7d18643

Browse files
jverzanijishnub
andauthored
Jishnub aqua (#549)
* Test using Aqua v0.8 * scalar_mult speedup; zero polynomial --------- Co-authored-by: Jishnu Bhattacharya <[email protected]>
1 parent a1a6386 commit 7d18643

File tree

3 files changed

+79
-36
lines changed

3 files changed

+79
-36
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ PolynomialsMakieCoreExt = "MakieCore"
2424
PolynomialsMutableArithmeticsExt = "MutableArithmetics"
2525

2626
[compat]
27-
Aqua = "0.6, 0.7"
27+
Aqua = "0.8"
2828
ChainRulesCore = "1"
2929
ChainRulesTestUtils = "1"
3030
DualNumbers = "0.6"

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

Lines changed: 77 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,35 @@ Immutable is a bit of a misnomer, as using the `@set!` macro from `Setfield.jl`
1212
"""
1313
struct ImmutableDensePolynomial{B,T,X,N} <: AbstractDenseUnivariatePolynomial{B,T,X}
1414
coeffs::NTuple{N,T}
15-
function ImmutableDensePolynomial{B,T,X,N}(cs::NTuple{N}) where {B,N,T,X}
15+
function ImmutableDensePolynomial{B,T,X,N}(cs::NTuple{N,T}) where {B,N,T,X}
1616
new{B,T,Symbol(X),N}(cs)
1717
end
18+
19+
# tuple with mismatched size/type
20+
# need zero(T) defined if padding needed
21+
function ImmutableDensePolynomial{B,T,X,N}(xs::Tuple{Vararg}) where {B,T,X,N}
22+
M = length(xs)
23+
N′ = findlast(!iszero, xs)
24+
N < N′ && throw(ArgumentError("Too many coefficients for type"))
25+
if N == N′ == M
26+
cs = T.(xs)
27+
elseif isnothing(N′)
28+
cs = ntuple(i -> zero(T), Val(N))
29+
else
30+
cs = ntuple(i -> i <= N′ ? T(xs[i]) : zero(first(xs)), Val(N))
31+
end
32+
new{B,T,X,N}(cs)
33+
end
34+
35+
# zero polynomial
36+
function ImmutableDensePolynomial{B,T,X,N}(cs::Tuple{}) where {B,T,X,N}
37+
cs = ntuple(i->zero(T), Val(N))
38+
new{B,T,Symbol(X),N}(cs)
39+
end
40+
function ImmutableDensePolynomial{B,T,X,0}(cs::Tuple{}) where {B,T,X}
41+
new{B,T,Symbol(X),0}(())
42+
end
43+
1844
end
1945

2046
ImmutableDensePolynomial{B,T,X,N}(check::Type{Val{false}}, cs::NTuple{N,T}) where {B,N,T,X} =
@@ -23,33 +49,38 @@ ImmutableDensePolynomial{B,T,X,N}(check::Type{Val{false}}, cs::NTuple{N,T}) wher
2349
ImmutableDensePolynomial{B,T,X,N}(check::Type{Val{true}}, cs::NTuple{N,T}) where {B,N, T,X} =
2450
ImmutableDensePolynomial{B,T,X,N}(cs)
2551

26-
# tuple with mismatched size
27-
function ImmutableDensePolynomial{B,T,X,N}(xs::NTuple{M,S}) where {B,T,S,X,N,M}
28-
p = ImmutableDensePolynomial{B,S,X,M}(xs)
29-
convert(ImmutableDensePolynomial{B,T,X,N}, ImmutableDensePolynomial{B,T,X,M}(xs))
30-
end
31-
3252
# vector case with N
3353
function ImmutableDensePolynomial{B,T,X,N}(xs::AbstractVector{S}) where {B,T,S,X,N}
3454
ImmutableDensePolynomial{B,T,X,N}(ntuple(Base.Fix1(getindex, xs), Val(N)))
35-
end
55+
end
3656

3757
# constant
3858
function ImmutableDensePolynomial{B,T,X,N}(c::S) where {B,T,X,N,S<:Scalar}
39-
if N == 0
40-
if iszero(c)
41-
throw(ArgumentError("Can't create zero-length polynomial"))
42-
else
43-
return zero(ImmutableDensePolynomial{B,T,X})
44-
end
45-
end
46-
cs = ntuple(i -> i == 1 ? T(c) : zero(T), Val(N))
47-
return ImmutableDensePolynomial{B,T,X,N}(cs)
59+
return ImmutableDensePolynomial{B,T,X,N}((c,))
4860
end
49-
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))
50-
ImmutableDensePolynomial{B,T,X}(xs::NTuple{N}) where {B,T,X,N} = ImmutableDensePolynomial{B,T,X,N}(convert(NTuple{N,T}, xs))
51-
ImmutableDensePolynomial{B,T}(xs::NTuple{N}, var::SymbolLike=Var(:x)) where {B,T,N} = ImmutableDensePolynomial{B,T,Symbol(var),N}(xs)
52-
ImmutableDensePolynomial{B}(xs::NTuple{N,T}, var::SymbolLike=Var(:x)) where {B,T,N} = ImmutableDensePolynomial{B,T,Symbol(var),N}(xs)
61+
62+
function ImmutableDensePolynomial{B,T,X}(::Val{false}, xs::Tuple{S,Vararg{S}}) where {B,T,S,X}
63+
N = length(xs)
64+
ImmutableDensePolynomial{B,T,X,N}(convert(NTuple{N,T}, xs))
65+
end
66+
67+
ImmutableDensePolynomial{B,T,X}(xs::NTuple{N}) where {B,T,X,N} =
68+
ImmutableDensePolynomial{B,T,X,N}(convert(NTuple{N,T}, xs))
69+
70+
ImmutableDensePolynomial{B,T,X}(xs::Tuple{}) where {B,T,X} =
71+
ImmutableDensePolynomial{B,T,X,0}(())
72+
73+
ImmutableDensePolynomial{B,T}(xs::NTuple{N}, var::SymbolLike=Var(:x)) where {B,T,N} =
74+
ImmutableDensePolynomial{B,T,Symbol(var),N}(xs)
75+
76+
ImmutableDensePolynomial{B,T}(xs::Tuple{}, var::SymbolLike=Var(:x)) where {B,T} =
77+
ImmutableDensePolynomial{B,T,Symbol(var),0}(xs)
78+
79+
ImmutableDensePolynomial{B}(xs::Tuple{T,Vararg{T}}, var::SymbolLike=Var(:x)) where {B,T} =
80+
ImmutableDensePolynomial{B,T,Symbol(var),length(xs)}(xs)
81+
82+
ImmutableDensePolynomial{B}(xs::Tuple{}, var::SymbolLike=Var(:x)) where {B} =
83+
ImmutableDensePolynomial{B,Float64,Symbol(var),0}(xs)
5384

5485
# abstract vector. Must eat order
5586
ImmutableDensePolynomial{B,T,X}(::Val{false}, xs::AbstractVector, order::Int=0) where {B,T,X} =
@@ -80,18 +111,37 @@ Base.promote_rule(::Type{P}, ::Type{<:S}) where {S<:Number,B,T,X,N,P<:ImmutableD
80111

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

89-
function Base.map(fn, p::P, args...) where {B,T,X, P<:ImmutableDensePolynomial{B,T,X}}
117+
function Base.map(fn, p::P, args...) where {B,T,X,N, P<:ImmutableDensePolynomial{B,T,X,N}}
90118
xs = map(fn, p.coeffs, args...)
91119
R = eltype(xs)
92-
return ImmutableDensePolynomial{B,R,X}(xs)
120+
return ImmutableDensePolynomial{B,R,X,N}(xs)
121+
end
122+
123+
# map has a type instability
124+
function scalar_mult(p::P, c::S) where {B, S<:Scalar, T, X, N, P<:ImmutableDensePolynomial{B,T,X,N}}
125+
R = Base.promote_op(*, T, S)
126+
#cs = map(Base.Fix2(*,c), p.coeffs)
127+
cs = p.coeffs .* (c,)
128+
129+
ImmutableDensePolynomial{B,R,X,N}(cs)
130+
end
131+
132+
function scalar_mult(c::S, p::P) where {B, S<:Scalar, T, X, N, P<:ImmutableDensePolynomial{B,T,X,N}}
133+
R = Base.promote_op(*, T, S)
134+
#cs = map(Base.Fix1(*,c), p.coeffs)
135+
cs = (c,) .* p.coeffs
136+
ImmutableDensePolynomial{B,R,X,N}(cs)
93137
end
94138

139+
# scalar mult faster with 0 default
140+
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})
141+
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})
142+
143+
144+
95145

96146
Base.copy(p::ImmutableDensePolynomial) = p
97147
Base.similar(p::ImmutableDensePolynomial, args...) = p.coeffs
@@ -257,10 +307,6 @@ function _tuple_combine(op, p::ImmutableDensePolynomial{B,T,X,N}, q::ImmutableDe
257307
end
258308

259309

260-
# scalar mult faster with 0 default
261-
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})
262-
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})
263-
264310

265311
## ---
266312
# Padded vector combination of two homogeneous tuples assuming N ≥ M

test/aqua.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
using Aqua
22

3-
Aqua.test_all(Polynomials;
4-
unbound_args = false,
5-
stale_deps = false
6-
)
3+
Aqua.test_all(Polynomials)

0 commit comments

Comments
 (0)