Skip to content

Commit 9a6d167

Browse files
authored
close issue #395; allow symbol to be passed as Val(:x) (#398)
* close issue #395; allow symbol to be passed as Val(:x) * varsymbol approach * take II * keep bad formatting, fix later
1 parent c6c3e6c commit 9a6d167

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name = "Polynomials"
22
uuid = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
33
license = "MIT"
44
author = "JuliaMath"
5-
version = "2.0.24"
5+
version = "2.0.25"
66

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

src/abstract.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
export AbstractPolynomial
22

33

4+
# *internal* means to pass variable symbol to constructor through 2nd position and keep type stability
5+
struct Var{T} end
6+
Var(x::Symbol) = Var{x}()
7+
Symbol(::Var{T}) where {T} = T
48

5-
const SymbolLike = Union{AbstractString,Char,Symbol}
9+
const SymbolLike = Union{AbstractString,Char,Symbol, Var{T} where T}
610

711
"""
812
AbstractPolynomial{T,X}
@@ -51,7 +55,7 @@ abstract type AbstractPolynomial{T,X} end
5155

5256
# convert `as` into polynomial of type P based on instance, inheriting variable
5357
# (and for LaurentPolynomial the offset)
54-
_convert(p::P, as) where {T,X,P <: AbstractPolynomial{T,X}} = (P)(as, X)
58+
_convert(p::P, as) where {T,X,P <: AbstractPolynomial{T,X}} = (P)(as, Var(X))
5559

5660

5761
"""
@@ -122,7 +126,7 @@ macro registerN(name, params...)
122126
$poly{$(αs...),promote_type(T,S),X}
123127

124128
function $poly{$(αs...),T}(x::AbstractVector{S}, var::SymbolLike = :x) where {$(αs...),T,S}
125-
$poly{$(αs...),T, Symbol(var)}(T.(x))
129+
$poly{$(αs...),T,Symbol(var)}(T.(x))
126130
end
127131
$poly{$(αs...)}(coeffs::AbstractVector{T}, var::SymbolLike=:x) where {$(αs...),T} =
128132
$poly{$(αs...),T,Symbol(var)}(coeffs)

src/polynomials/Poly.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module PolyCompat
33
using ..Polynomials
44
indeterminate = Polynomials.indeterminate
55

6+
67
#=
78
Compat support for old code. This will be opt-in by v1.0, through "using Polynomials.PolyCompat"
89
=#

test/StandardBasis.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ isimmutable(::Type{<:ImmutablePolynomial}) = true
5757
@test_throws InexactError P{Int}([1+im, 1], :x)
5858
@test_throws InexactError P{Int,:x}(1+im)
5959
@test_throws InexactError P{Int}(1+im)
60+
61+
## issue #395
62+
v = [1,2,3]
63+
@test P(v) == P(v,:x) == P(v,'x') == P(v,"x") == P(v, Polynomials.Var(:x))
6064
end
6165

6266
end
@@ -393,9 +397,10 @@ end
393397
pN = P([276,3,87,15,24,0])
394398
pR = P([3 // 4, -2 // 1, 1 // 1])
395399

396-
# type stability of the default constructor without variable name
400+
# type stability of the default constructor with/without variable name
397401
if P !== ImmutablePolynomial
398402
@inferred P([1, 2, 3])
403+
@inferred P([1,2,3], Polynomials.Var(:x))
399404
end
400405

401406
@test p3 == P([1,2,1])
@@ -450,6 +455,11 @@ end
450455
x = variable(LaurentPolynomial)
451456
@test Polynomials.isconstant(x * inv(x))
452457
@test_throws ArgumentError inv(x + x^2)
458+
459+
# issue #395
460+
p = Polynomial([2,1], :s)
461+
@inferred -p # issue #395
462+
453463
end
454464

455465
@testset "Divrem" begin

0 commit comments

Comments
 (0)