Skip to content

Commit

Permalink
close issue #395; allow symbol to be passed as Val(:x) (#398)
Browse files Browse the repository at this point in the history
* close issue #395; allow symbol to be passed as Val(:x)

* varsymbol approach

* take II

* keep bad formatting, fix later
  • Loading branch information
jverzani authored Feb 18, 2022
1 parent c6c3e6c commit 9a6d167
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name = "Polynomials"
uuid = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
license = "MIT"
author = "JuliaMath"
version = "2.0.24"
version = "2.0.25"

[deps]
Intervals = "d8418881-c3e1-53bb-8760-2df7ec849ed5"
Expand Down
10 changes: 7 additions & 3 deletions src/abstract.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
export AbstractPolynomial


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

const SymbolLike = Union{AbstractString,Char,Symbol}
const SymbolLike = Union{AbstractString,Char,Symbol, Var{T} where T}

"""
AbstractPolynomial{T,X}
Expand Down Expand Up @@ -51,7 +55,7 @@ abstract type AbstractPolynomial{T,X} end

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


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

function $poly{$(αs...),T}(x::AbstractVector{S}, var::SymbolLike = :x) where {$(αs...),T,S}
$poly{$(αs...),T, Symbol(var)}(T.(x))
$poly{$(αs...),T,Symbol(var)}(T.(x))
end
$poly{$(αs...)}(coeffs::AbstractVector{T}, var::SymbolLike=:x) where {$(αs...),T} =
$poly{$(αs...),T,Symbol(var)}(coeffs)
Expand Down
1 change: 1 addition & 0 deletions src/polynomials/Poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module PolyCompat
using ..Polynomials
indeterminate = Polynomials.indeterminate


#=
Compat support for old code. This will be opt-in by v1.0, through "using Polynomials.PolyCompat"
=#
Expand Down
12 changes: 11 additions & 1 deletion test/StandardBasis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ isimmutable(::Type{<:ImmutablePolynomial}) = true
@test_throws InexactError P{Int}([1+im, 1], :x)
@test_throws InexactError P{Int,:x}(1+im)
@test_throws InexactError P{Int}(1+im)

## issue #395
v = [1,2,3]
@test P(v) == P(v,:x) == P(v,'x') == P(v,"x") == P(v, Polynomials.Var(:x))
end

end
Expand Down Expand Up @@ -393,9 +397,10 @@ end
pN = P([276,3,87,15,24,0])
pR = P([3 // 4, -2 // 1, 1 // 1])

# type stability of the default constructor without variable name
# type stability of the default constructor with/without variable name
if P !== ImmutablePolynomial
@inferred P([1, 2, 3])
@inferred P([1,2,3], Polynomials.Var(:x))
end

@test p3 == P([1,2,1])
Expand Down Expand Up @@ -450,6 +455,11 @@ end
x = variable(LaurentPolynomial)
@test Polynomials.isconstant(x * inv(x))
@test_throws ArgumentError inv(x + x^2)

# issue #395
p = Polynomial([2,1], :s)
@inferred -p # issue #395

end

@testset "Divrem" begin
Expand Down

2 comments on commit 9a6d167

@jverzani
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/54925

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v2.0.25 -m "<description of version>" 9a6d167f74fd778a749847fe964ea3d06fa35bf7
git push origin v2.0.25

Please sign in to comment.