Skip to content

Commit f141d2f

Browse files
committed
optimized length for SparsePolynomial
1 parent 0deea6f commit f141d2f

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

src/common.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,8 @@ Base.length(p::AbstractPolynomial) = length(coeffs(p))
460460
461461
Returns the size of the polynomials coefficients, along axis `i` if provided.
462462
"""
463-
Base.size(p::AbstractPolynomial) = size(coeffs(p))
464-
Base.size(p::AbstractPolynomial, i::Integer) = size(coeffs(p), i)
463+
Base.size(p::AbstractPolynomial) = (length(p),)
464+
Base.size(p::AbstractPolynomial, i::Integer) = i <= 1 ? size(p)[i] : 1
465465
Base.eltype(p::AbstractPolynomial{T}) where {T} = T
466466
# in analogy with polynomial as a Vector{T} with different operations defined.
467467
Base.eltype(::Type{<:AbstractPolynomial}) = Float64
@@ -470,7 +470,7 @@ _eltype(::Type{<:AbstractPolynomial}) = nothing
470470
_eltype(::Type{<:AbstractPolynomial{T}}) where {T} = T
471471
function _eltype(P::Type{<:AbstractPolynomial}, p::AbstractPolynomial)
472472
T′ = _eltype(P)
473-
T = T′ == nothing ? eltype(p) : T′
473+
T = T′ === nothing ? eltype(p) : T′
474474
T
475475
end
476476
Base.iszero(p::AbstractPolynomial) = all(iszero, p)
@@ -706,7 +706,7 @@ Returns an iterator over the terms, `pᵢ⋅basis(p,i)`, of the polynomial for e
706706
monomials(p) = Monomials(p)
707707
function Base.iterate(v::Monomials, state...)
708708
y = iterate(pairs(v.p), state...)
709-
y == nothing && return nothing
709+
y === nothing && return nothing
710710
kv, s = y
711711
return (kv[2]*basis(v.p, kv[1]), s)
712712
end
@@ -723,18 +723,18 @@ _indeterminate(::Type{P}) where {P <: AbstractPolynomial} = nothing
723723
_indeterminate(::Type{P}) where {T, X, P <: AbstractPolynomial{T,X}} = X
724724
function indeterminate(::Type{P}) where {P <: AbstractPolynomial}
725725
X = _indeterminate(P)
726-
X == nothing ? :x : X
726+
X === nothing ? :x : X
727727
end
728728
indeterminate(p::P) where {P <: AbstractPolynomial} = _indeterminate(P)
729729
function indeterminate(PP::Type{P}, p::AbstractPolynomial{T,Y}) where {P <: AbstractPolynomial, T,Y}
730730
X = _indeterminate(PP)
731-
X == nothing && return Y
731+
X === nothing && return Y
732732
assert_same_variable(X,Y)
733733
return X
734-
#X = _indeterminate(PP) == nothing ? indeterminate(p) : _indeterminate(PP)
734+
#X = _indeterminate(PP) === nothing ? indeterminate(p) : _indeterminate(PP)
735735
end
736736
function indeterminate(PP::Type{P}, x::Symbol) where {P <: AbstractPolynomial}
737-
X = _indeterminate(PP) == nothing ? x : _indeterminate(PP)
737+
X = _indeterminate(PP) === nothing ? x : _indeterminate(PP)
738738
end
739739

740740
#=
@@ -761,7 +761,7 @@ Base.zero(p::P, var=indeterminate(p)) where {P <: AbstractPolynomial} = zero(P,
761761
Returns a representation of 1 as the given polynomial.
762762
"""
763763
Base.one(::Type{P}) where {P<:AbstractPolynomial} = throw(ArgumentError("No default method defined")) # no default method
764-
Base.one(::Type{P}, var::SymbolLike) where {P <: AbstractPolynomial} = one((P){eltype(P), Symbol(var == nothing ? :x : var)})
764+
Base.one(::Type{P}, var::SymbolLike) where {P <: AbstractPolynomial} = one((P){eltype(P), Symbol(var === nothing ? :x : var)})
765765
Base.one(p::P, var=indeterminate(p)) where {P <: AbstractPolynomial} = one(P, var)
766766

767767
Base.oneunit(::Type{P}, args...) where {P <: AbstractPolynomial} = one(P, args...)

src/polynomials/SparsePolynomial.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ function Base.iterate(v::PolynomialValues{SparsePolynomial{T,X}}, state...) wher
141141
return (y[1][2], y[2])
142142
end
143143

144-
144+
Base.length(S::SparsePolynomial) = isempty(S.coeffs) ? 0 : maximum(keys(S.coeffs)) + 1
145145

146146
##
147147
## ----

test/StandardBasis.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ isimmutable(::Type{<:ImmutablePolynomial}) = true
4545
P == Polynomial && @test size(p) == size(coeff)
4646
P == Polynomial && @test size(p, 1) == size(coeff, 1)
4747
P == Polynomial && @test typeof(p).parameters[1] == eltype(coeff)
48-
if P != FactoredPolynomial
48+
if !(eltype(coeff) <: Real && P == FactoredPolynomial) # roots may be complex
4949
@test eltype(p) == eltype(coeff)
5050
end
5151
@test all([-200, -0.3, 1, 48.2] .∈ Polynomials.domain(p))

0 commit comments

Comments
 (0)