Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the new mapcoefficients API #76

Merged
merged 2 commits into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TypedPolynomials"
uuid = "afbbf031-7a57-5f58-a1b9-b774a0fad08d"
repo = "https://github.com/JuliaAlgebra/TypedPolynomials.jl.git"
version = "0.3.0"
version = "0.3.1"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand All @@ -13,7 +13,7 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[compat]
MacroTools = "0.5"
MultivariatePolynomials = "0.4"
MultivariatePolynomials = "0.4.1"
MutableArithmetics = "0.3"
NBInclude = "2"
julia = "1"
Expand Down
29 changes: 26 additions & 3 deletions src/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,39 @@ adjoint(m::Monomial) = m
adjoint(t::Term) = Term(adjoint(coefficient(t)), monomial(t))
adjoint(x::Polynomial) = Polynomial(adjoint.(terms(x)))

function MP.mapcoefficientsnz_to!(output::Polynomial, f::Function, p::Polynomial)
function MP.mapcoefficients(f::Function, p::Polynomial; nonzero = false)
terms = map(p.terms) do term
MP.mapcoefficients(f, term)
end
if !nonzero
filter!(!iszero, terms)
end
return Polynomial(terms)
end
function MP.mapcoefficients!(f::Function, p::Polynomial; nonzero = false)
for i in eachindex(p.terms)
t = p.terms[i]
p.terms[i] = Term(f(coefficient(t)), monomial(t))
end
if !nonzero
filter!(!iszero, p.terms)
end
return p
end

function MP.mapcoefficients_to!(output::Polynomial, f::Function, p::Polynomial; nonzero = false)
resize!(output.terms, nterms(p))
for i in eachindex(p.terms)
t = p.terms[i]
output.terms[i] = Term(f(coefficient(t)), monomial(t))
end
if !nonzero
filter!(!iszero, output.terms)
end
return output
end
function MP.mapcoefficientsnz_to!(output::Polynomial, f::Function, p::AbstractPolynomialLike)
return MP.mapcoefficientsnz_to!(output, f, polynomial(p))
function MP.mapcoefficients_to!(output::Polynomial, f::Function, p::AbstractPolynomialLike; nonzero = false)
return MP.mapcoefficients_to!(output, f, polynomial(p); nonzero = false)
end

function MA.operate!(::typeof(MP.removeleadingterm), p::Polynomial)
Expand Down