From 7a9bafc84114746178080df6633038961c858aa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Fri, 12 Jan 2018 10:01:31 +0100 Subject: [PATCH] Add Base.round --- src/polynomial.jl | 6 ++++++ test/polynomial.jl | 3 +++ 2 files changed, 9 insertions(+) diff --git a/src/polynomial.jl b/src/polynomial.jl index ca627123..504e7097 100644 --- a/src/polynomial.jl +++ b/src/polynomial.jl @@ -367,3 +367,9 @@ function mapcoefficientsnz(f::Function, p::AbstractPolynomialLike) polynomial(mapcoefficientsnz.(f, terms(p)), SortedUniqState()) end mapcoefficientsnz(f::Function, t::AbstractTermLike) = f(coefficient(t)) * monomial(t) + +Base.round(t::AbstractTermLike) = round(coefficient(t)) * monomial(t) +function Base.round(p::AbstractPolynomialLike) + # round(0.1) is zero so we cannot use SortedUniqState + polynomial(round.(terms(p)), SortedState()) +end diff --git a/test/polynomial.jl b/test/polynomial.jl index 9ad6196f..c62da82a 100644 --- a/test/polynomial.jl +++ b/test/polynomial.jl @@ -115,6 +115,9 @@ @test polynomial([1 2; 3 4], monovec([y, x^2])) == x^4 + 5x^2*y + 4y^2 @test polynomial([1 2; 3 4], monovec([y, x^2]), Float64) isa AbstractPolynomial{Float64} + @test (@inferred round(2.6x + 1.001x^2)) == 3x + 1x^2 + @test (@inferred round(3.1x*y)) == 3x*y + @testset "Graded Lex Order" begin Mod.@polyvar x y z p = 3*y^2 + 2*y*x