Skip to content

Commit 6def899

Browse files
authored
Merge pull request #238 from jverzani/issue_237
use conversion for P(n); closes #237
2 parents ee2e948 + ea1efb3 commit 6def899

File tree

5 files changed

+12
-8
lines changed

5 files changed

+12
-8
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "Polynomials"
33
uuid = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
44
license = "MIT"
55
author = "JuliaMath"
6-
version = "1.1.1"
6+
version = "1.1.2"
77

88
[deps]
99
Intervals = "d8418881-c3e1-53bb-8760-2df7ec849ed5"

src/abstract.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ macro register(name)
4949
$poly{T}(x::AbstractVector{S}, var::SymbolLike = :x) where {T,S<:Number} =
5050
$poly(T.(x), Symbol(var))
5151
$poly{T}(n::S, var::SymbolLike = :x) where {T, S<:Number} =
52-
$poly(T[n], Symbol(var))
53-
$poly(n::Number, var::SymbolLike = :x) = $poly([n], Symbol(var))
52+
n * one($poly{T}, Symbol(var))
53+
$poly(n::S, var::SymbolLike = :x) where {S <: Number} = n * one($poly{S}, Symbol(var))
5454
$poly{T}(var::SymbolLike=:x) where {T} = variable($poly{T}, Symbol(var))
5555
$poly(var::SymbolLike=:x) = variable($poly, Symbol(var))
5656
end

src/common.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ Base.zero(p::P) where {P <: AbstractPolynomial} = zero(P, p.var)
430430
431431
Returns a representation of 1 as the given polynomial.
432432
"""
433-
Base.one(::Type{P}, var=:x) where {P <: AbstractPolynomial} = (P)(ones(eltype(P),1), var)
433+
Base.one(::Type{P}, var=:x) where {P <: AbstractPolynomial} = (P)(ones(eltype(P),1), var) # assumes p₀ = 1
434434
Base.one(p::P) where {P <: AbstractPolynomial} = one(P, p.var)
435435

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

src/polynomials/Poly.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ Polynomials.@register Poly
3939
Base.convert(P::Type{<:Polynomial}, p::Poly{T}) where {T} = P(p.coeffs, p.var)
4040

4141
Base.eltype(P::Type{<:Poly}) = P
42-
Base.zero(::Type{<:Poly{T}},var=:x) where {T} = Poly{T}(zeros(T,0), var)
43-
Base.zero(::Type{<:Poly},var=:x) where {T} = Poly(zeros(Float64,0), var)
44-
Base.one(::Type{<:Poly{T}},var=:x) where {T} = Poly{T}(ones(T,1), var)
45-
Base.one(::Type{<:Poly},var=:x) where {T} = Poly(ones(Float64,1), var)
42+
_eltype(::Type{<:Poly{T}}) where {T} = T
43+
_eltype(::Type{Poly}) = Float64
44+
Base.zero(P::Type{<:Poly},var=:x) = Poly(zeros(_eltype(P),0), var)
45+
Base.one(P::Type{<:Poly},var=:x) = Poly(ones(_eltype(P),1), var)
4646
function Polynomials.basis(P::Type{<:Poly}, k::Int, _var::Polynomials.SymbolLike=:x; var=_var)
4747
zs = zeros(Int, k+1)
4848
zs[end] = 1

test/StandardBasis.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ end
8383
@test iszero(p0)
8484
P != LaurentPolynomial && @test degree(p0) == -1
8585

86+
# P(2) is 2 (not 2p₀) connvert(Polynomial, P(s::Number)) = Polynomial(s)
87+
@test convert(Polynomial, P(2)) Polynomial(2)
88+
@test P(2) 2*one(P)
89+
8690
# variable(), P() to generate `x` in given basis
8791
@test degree(variable(P)) == 1
8892
@test variable(P)(1) == 1

0 commit comments

Comments
 (0)