From bb9c9b1c8b8e51e9673e5d53000211ad95483187 Mon Sep 17 00:00:00 2001 From: jverzani Date: Thu, 21 May 2020 11:48:05 -0400 Subject: [PATCH 1/2] closes issue #214; close issue #215; adjust regisiter macros --- src/abstract.jl | 28 ++++++++++++++++++++++++++++ src/common.jl | 15 ++++++++++++--- test/StandardBasis.jl | 5 +++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/abstract.jl b/src/abstract.jl index b115c4e9..71e5677a 100644 --- a/src/abstract.jl +++ b/src/abstract.jl @@ -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 @@ -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 diff --git a/src/common.jl b/src/common.jl index 5cc7a93a..d69000ef 100644 --- a/src/common.jl +++ b/src/common.jl @@ -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; @@ -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 diff --git a/test/StandardBasis.jl b/test/StandardBasis.jl index 900abddb..f116dc3e 100644 --- a/test/StandardBasis.jl +++ b/test/StandardBasis.jl @@ -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 @@ -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) From c8aa44f5b0b253c70d478eee219015a1c42bc3f7 Mon Sep 17 00:00:00 2001 From: jverzani Date: Thu, 21 May 2020 18:17:37 -0400 Subject: [PATCH 2/2] fix version number for registrator --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 6417a39f..70071b11 100644 --- a/Project.toml +++ b/Project.toml @@ -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"