@@ -460,8 +460,8 @@ Base.length(p::AbstractPolynomial) = length(coeffs(p))
460
460
461
461
Returns the size of the polynomials coefficients, along axis `i` if provided.
462
462
"""
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
465
465
Base. eltype (p:: AbstractPolynomial{T} ) where {T} = T
466
466
# in analogy with polynomial as a Vector{T} with different operations defined.
467
467
Base. eltype (:: Type{<:AbstractPolynomial} ) = Float64
@@ -470,7 +470,7 @@ _eltype(::Type{<:AbstractPolynomial}) = nothing
470
470
_eltype (:: Type{<:AbstractPolynomial{T}} ) where {T} = T
471
471
function _eltype (P:: Type{<:AbstractPolynomial} , p:: AbstractPolynomial )
472
472
T′ = _eltype (P)
473
- T = T′ == nothing ? eltype (p) : T′
473
+ T = T′ === nothing ? eltype (p) : T′
474
474
T
475
475
end
476
476
Base. iszero (p:: AbstractPolynomial ) = all (iszero, p)
@@ -664,47 +664,34 @@ Iteration =#
664
664
# `pairs(p)`: `i => pᵢ` possibly skipping over values of `i` with `pᵢ == 0` (SparsePolynomial)
665
665
# and possibly non ordered (SparsePolynomial)
666
666
# `monomials(p)`: iterates over pᵢ ⋅ basis(p, i) i ∈ keys(p)
667
- function Base. iterate (p:: AbstractPolynomial , state= nothing )
668
- i = firstindex (p)
669
- if state == nothing
670
- return (p[i], i)
671
- else
672
- j = lastindex (p)
673
- if i <= state < j
674
- return (p[state+ 1 ], state+ 1 )
675
- end
676
- return nothing
677
- end
667
+ function _iterate (p, state)
668
+ firstindex (p) <= state <= lastindex (p) || return nothing
669
+ return p[state], state+ 1
678
670
end
671
+ Base. iterate (p:: AbstractPolynomial , state = firstindex (p)) = _iterate (p, state)
679
672
680
673
# pairs map i -> aᵢ *possibly* skipping over ai == 0
681
674
# cf. abstractdict.jl
682
- struct PolynomialKeys{P}
675
+ struct PolynomialKeys{P} <: AbstractSet{Int}
683
676
p:: P
684
677
end
685
- struct PolynomialValues{P}
678
+ struct PolynomialValues{P, T} <: AbstractSet{T }
686
679
p:: P
680
+
681
+ PolynomialValues {P} (p:: P ) where {P} = new {P, eltype(p)} (p)
682
+ PolynomialValues (p:: P ) where {P} = new {P, eltype(p)} (p)
687
683
end
688
684
Base. keys (p:: AbstractPolynomial ) = PolynomialKeys (p)
689
685
Base. values (p:: AbstractPolynomial ) = PolynomialValues (p)
690
686
Base. length (p:: PolynomialValues ) = length (p. p. coeffs)
691
687
Base. length (p:: PolynomialKeys ) = length (p. p. coeffs)
692
688
Base. size (p:: Union{PolynomialValues, PolynomialKeys} ) = (length (p),)
693
- function Base. iterate (v:: PolynomialKeys , state= nothing )
694
- i = firstindex (v. p)
695
- state== nothing && return (i, i)
696
- j = lastindex (v. p)
697
- i <= state < j && return (state+ 1 , state+ 1 )
698
- return nothing
689
+ function Base. iterate (v:: PolynomialKeys , state = firstindex (v. p))
690
+ firstindex (v. p) <= state <= lastindex (v. p) || return nothing
691
+ return state, state+ 1
699
692
end
700
693
701
- function Base. iterate (v:: PolynomialValues , state= nothing )
702
- i = firstindex (v. p)
703
- state== nothing && return (v. p[i], i)
704
- j = lastindex (v. p)
705
- i <= state < j && return (v. p[state+ 1 ], state+ 1 )
706
- return nothing
707
- end
694
+ Base. iterate (v:: PolynomialValues , state = firstindex (v. p)) = _iterate (v. p, state)
708
695
709
696
710
697
# iterate over monomials of the polynomial
@@ -719,7 +706,7 @@ Returns an iterator over the terms, `pᵢ⋅basis(p,i)`, of the polynomial for e
719
706
monomials (p) = Monomials (p)
720
707
function Base. iterate (v:: Monomials , state... )
721
708
y = iterate (pairs (v. p), state... )
722
- y == nothing && return nothing
709
+ y === nothing && return nothing
723
710
kv, s = y
724
711
return (kv[2 ]* basis (v. p, kv[1 ]), s)
725
712
end
@@ -736,18 +723,18 @@ _indeterminate(::Type{P}) where {P <: AbstractPolynomial} = nothing
736
723
_indeterminate (:: Type{P} ) where {T, X, P <: AbstractPolynomial{T,X} } = X
737
724
function indeterminate (:: Type{P} ) where {P <: AbstractPolynomial }
738
725
X = _indeterminate (P)
739
- X == nothing ? :x : X
726
+ X === nothing ? :x : X
740
727
end
741
728
indeterminate (p:: P ) where {P <: AbstractPolynomial } = _indeterminate (P)
742
729
function indeterminate (PP:: Type{P} , p:: AbstractPolynomial{T,Y} ) where {P <: AbstractPolynomial , T,Y}
743
730
X = _indeterminate (PP)
744
- X == nothing && return Y
731
+ X === nothing && return Y
745
732
assert_same_variable (X,Y)
746
733
return X
747
- # X = _indeterminate(PP) == nothing ? indeterminate(p) : _indeterminate(PP)
734
+ # X = _indeterminate(PP) === nothing ? indeterminate(p) : _indeterminate(PP)
748
735
end
749
736
function indeterminate (PP:: Type{P} , x:: Symbol ) where {P <: AbstractPolynomial }
750
- X = _indeterminate (PP) == nothing ? x : _indeterminate (PP)
737
+ X = _indeterminate (PP) === nothing ? x : _indeterminate (PP)
751
738
end
752
739
753
740
#=
@@ -774,7 +761,7 @@ Base.zero(p::P, var=indeterminate(p)) where {P <: AbstractPolynomial} = zero(P,
774
761
Returns a representation of 1 as the given polynomial.
775
762
"""
776
763
Base. one (:: Type{P} ) where {P<: AbstractPolynomial } = throw (ArgumentError (" No default method defined" )) # no default method
777
- 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)})
778
765
Base. one (p:: P , var= indeterminate (p)) where {P <: AbstractPolynomial } = one (P, var)
779
766
780
767
Base. oneunit (:: Type{P} , args... ) where {P <: AbstractPolynomial } = one (P, args... )
0 commit comments