Skip to content

Commit

Permalink
Merge pull request #226 from jverzani/issues_214_215
Browse files Browse the repository at this point in the history
closes issue #214; close issue #215; adjust register macros
  • Loading branch information
jverzani authored May 22, 2020
2 parents 1d6cb14 + c8aa44f commit 6ff4b25
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "Polynomials"
uuid = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
license = "MIT"
author = "JuliaMath"
version = "1.1.1"
version = "1.1.0"

[deps]
Intervals = "d8418881-c3e1-53bb-8760-2df7ec849ed5"
Expand Down
28 changes: 28 additions & 0 deletions src/abstract.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,35 @@ macro register(name)
end


macro registerN(name, params...)
poly = esc(name)
αs = tuple(esc.(params)...)
quote
Base.convert(::Type{P}, q::Q) where {$(αs...),T, P<:$poly{$(αs...),T}, Q <: $poly{$(αs...),T}} = q
Base.convert(::Type{$poly{$(αs...)}}, q::Q) where {$(αs...),T, Q <: $poly{$(αs...),T}} = q
Base.promote(p::P, q::Q) where {$(αs...),T, P <:$poly{$(αs...),T}, Q <: $poly{$(αs...),T}} = p,q
Base.promote_rule(::Type{<:$poly{$(αs...),T}}, ::Type{<:$poly{$(αs...),S}}) where {$(αs...),T,S} =
$poly{$(αs...),promote_type(T, S)}
Base.promote_rule(::Type{<:$poly{$(αs...),T}}, ::Type{S}) where {$(αs...),T,S<:Number} =
$poly{$(αs...),promote_type(T,S)}

function $poly{$(αs...),T}(x::AbstractVector{S}, var::SymbolLike = :x) where {$(αs...),T,S}
$poly{$(αs...),T}(T.(x), Symbol(var))
end
$poly{$(αs...)}(coeffs::AbstractVector{T}, var::SymbolLike=:x) where {$(αs...),T} =
$poly{$(αs...),T}(coeffs, Symbol(var))
$poly{$(αs...),T}(n::Number, var::SymbolLike = :x) where {$(αs...),T} = n*one($poly{$(αs...),T}, Symbol(var))
$poly{$(αs...)}(n::Number, var::SymbolLike = :x) where {$(αs...)} = n*one($poly{$(αs...)}, Symbol(var))
$poly{$(αs...),T}(var::SymbolLike=:x) where {$(αs...), T} = variable($poly{$(αs...),T}, Symbol(var))
$poly{$(αs...)}(var::SymbolLike=:x) where {$(αs...)} = variable($poly{$(αs...)}, Symbol(var))
end
end


# deprecated. If desired, replace with @registerN type parameters... macro
# Macros to register POLY{α, T} and POLY{α, β, T}
macro register1(name)
@warn "@register1 is deprecated use @registerN"
poly = esc(name)
quote
Base.convert(::Type{P}, p::P) where {P<:$poly} = p
Expand All @@ -82,6 +109,7 @@ end

# Macro to register POLY{α, β, T}
macro register2(name)
@warn "@register2 is deprecated use @registerN"
poly = esc(name)
quote
Base.convert(::Type{P}, p::P) where {P<:$poly} = p
Expand Down
15 changes: 12 additions & 3 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,16 @@ fit(P::Type{<:AbstractPolynomial},
y,
deg::Integer = length(x) - 1;
weights = nothing,
var = :x,) = fit(P, promote(collect(x), collect(y))..., deg; weights = weights, var = var)

var = :x,) = fit'(P, promote(collect(x), collect(y))..., deg; weights = weights, var = var)

# avoid issue 214
fit′(P::Type{<:AbstractPolynomial}, x, y, args...;kwargs...) = throw(MethodError("x and y do not produce abstract vectors"))
fit′(P::Type{<:AbstractPolynomial},
x::AbstractVector{T},
y::AbstractVector{T},
args...; kwargs...) where {T} = fit(P,x,y,args...; kwargs...)


fit(x::AbstractVector,
y::AbstractVector,
deg::Integer = length(x) - 1;
Expand Down Expand Up @@ -249,7 +257,8 @@ LinearAlgebra.norm(q::AbstractPolynomial, p::Real = 2) = norm(coeffs(q), p)
Returns the complex conjugate of the polynomial
"""
LinearAlgebra.conj(p::P) where {P <: AbstractPolynomial} = P(conj(coeffs(p)))
LinearAlgebra.conj(p::P) where {P <: AbstractPolynomial} = (P)(conj(coeffs(p)), p.var)
LinearAlgebra.adjoint(p::P) where {P <: AbstractPolynomial} = (P)(adjoint.(coeffs(p)), p.var)
LinearAlgebra.transpose(p::AbstractPolynomial) = p
LinearAlgebra.transpose!(p::AbstractPolynomial) = p

Expand Down
5 changes: 5 additions & 0 deletions test/StandardBasis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ end
yy = Real[15.7696, 21.4851, 28.2463]
fit(P, xx, yy, 2)

# issue #214 -- should error
@test_throws MethodError fit(Polynomial, rand(2,2), rand(2,2))

end
end

Expand Down Expand Up @@ -574,6 +577,8 @@ end
@test coeffs(p2) ==ᵗ⁰ [1 + 1im, 2 + 3im]
@test transpose(p) == p
P != ImmutablePolynomial && @test transpose!(p) == p
@test adjoint(Polynomial(im)) == Polynomial(-im) # issue 215
@test conj(Polynomial(im)) == Polynomial(-im) # issue 215

@test norm(P([1., 2.])) == norm([1., 2.])
@test norm(P([1., 2.]), 1) == norm([1., 2.], 1)
Expand Down

2 comments on commit 6ff4b25

@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/15168

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 v1.1.0 -m "<description of version>" 6ff4b25df720358130e2e32747ccb3998732b985
git push origin v1.1.0

Please sign in to comment.