Skip to content

Commit c75277d

Browse files
blegatYingboMa
andauthored
Specialize on function type (#208)
* Specialize on function type Co-authored-by: Yingbo Ma <[email protected]> * Fix Co-authored-by: Yingbo Ma <[email protected]>
1 parent 8e19e99 commit c75277d

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

src/default_polynomial.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Base.:(-)(p1::Polynomial{<:LinearAlgebra.UniformScaling}, p2::Polynomial{<:Linea
105105

106106
LinearAlgebra.adjoint(x::Polynomial) = polynomial!(adjoint.(terms(x)))
107107

108-
function mapcoefficients(f::Function, p::Polynomial; nonzero = false)
108+
function mapcoefficients(f::F, p::Polynomial; nonzero = false) where {F<:Function}
109109
terms = map(p.terms) do term
110110
mapcoefficients(f, term)
111111
end
@@ -114,7 +114,7 @@ function mapcoefficients(f::Function, p::Polynomial; nonzero = false)
114114
end
115115
return polynomial!(terms)
116116
end
117-
function mapcoefficients!(f::Function, p::Polynomial; nonzero = false)
117+
function mapcoefficients!(f::F, p::Polynomial; nonzero = false) where {F<:Function}
118118
for i in eachindex(p.terms)
119119
t = p.terms[i]
120120
p.terms[i] = Term(f(coefficient(t)), monomial(t))
@@ -125,7 +125,7 @@ function mapcoefficients!(f::Function, p::Polynomial; nonzero = false)
125125
return p
126126
end
127127

128-
function mapcoefficients_to!(output::Polynomial, f::Function, p::Polynomial; nonzero = false)
128+
function mapcoefficients_to!(output::Polynomial, f::F, p::Polynomial; nonzero = false) where {F<:Function}
129129
resize!(output.terms, nterms(p))
130130
for i in eachindex(p.terms)
131131
t = p.terms[i]

src/operators.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ MA.operate_to!(output::AbstractPolynomial, op::typeof(*), p::APL, α) = MA.opera
7676
MA.operate_to!(output::APL, op::typeof(/), p::APL, α) = mapcoefficients_to!(output, Base.Fix2(op, α), p)
7777

7878
function polynomial_merge!(
79-
n1::Int, n2::Int, get1::Function, get2::Function,
80-
set::Function, push::Function, compare_monomials::Function,
81-
combine::Function, keep::Function, resize::Function)
79+
n1::Int, n2::Int, get1::F1, get2::F2,
80+
set::F3, push::F4, compare_monomials::F5,
81+
combine::F6, keep::F7, resize::F8) where {F1, F2, F3, F4, F5, F6, F7, F8}
8282
buffer = nothing
8383
i = j = k = 1
8484
# Invariant:

src/polynomial.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function polynomial(Q::AbstractMatrix, mv::AbstractVector, ::Type{T}) where T
5858
polynomial(polynomial(Q, mv), T)
5959
end
6060

61-
polynomial(f::Function, mv::AbstractVector{<:AbstractMonomialLike}) = polynomial!([term(f(i), mv[i]) for i in 1:length(mv)])
61+
polynomial(f::F, mv::AbstractVector{<:AbstractMonomialLike}) where {F<:Function} = polynomial!([term(f(i), mv[i]) for i in 1:length(mv)])
6262

6363
function polynomial(a::AbstractVector, x::AbstractVector, s::ListState=MessyState())
6464
# If `x` is e.g. `[v, 1]` then it will contains terms that are convertible to monomials.
@@ -404,8 +404,8 @@ function _divtoone(t::AbstractTermLike{T}, α::S) where {T, S}
404404
end
405405

406406
# TODO deprecate
407-
mapcoefficientsnz(f::Function, p::APL) = mapcoefficients(f, p, nonzero = true)
408-
mapcoefficientsnz_to!(output::APL, f::Function, p::APL) = mapcoefficients_to!(output, f, p, nonzero = true)
407+
mapcoefficientsnz(f::F, p::APL) where {F<:Function} = mapcoefficients(f, p, nonzero = true)
408+
mapcoefficientsnz_to!(output::APL, f::F, p::APL) where {F<:Function} = mapcoefficients_to!(output, f, p, nonzero = true)
409409

410410
"""
411411
mapcoefficients(f::Function, p::AbstractPolynomialLike, nonzero = false)
@@ -422,13 +422,13 @@ See also [`mapcoefficients!`](@ref) and [`mapcoefficients_to!`](@ref).
422422
Calling `mapcoefficients(α -> mod(3α, 6), 2x*y + 3x + 1)` should return `3x + 3`.
423423
"""
424424
function mapcoefficients end
425-
function mapcoefficients(f::Function, p::AbstractPolynomialLike; nonzero = false) # Not used by either TypedPolynomials or DynamicPolynomials but used by CustomPoly in tests. FIXME Remove in a breaking release
425+
function mapcoefficients(f::F, p::AbstractPolynomialLike; nonzero = false) where {F<:Function} # Not used by either TypedPolynomials or DynamicPolynomials but used by CustomPoly in tests. FIXME Remove in a breaking release
426426
# Invariant: p has only nonzero coefficient
427427
# therefore f(α) will be nonzero for every coefficient α of p
428428
# hence we can use Uniq
429429
polynomial!(mapcoefficients.(f, terms(p)), nonzero ? SortedUniqState() : SortedState())
430430
end
431-
function mapcoefficients(f::Function, t::AbstractTermLike; nonzero = false)
431+
function mapcoefficients(f::F, t::AbstractTermLike; nonzero = false) where {F<:Function}
432432
return term(f(coefficient(t)), monomial(t))
433433
end
434434

0 commit comments

Comments
 (0)