From 551c841fb1e390ddc07fe367f1fde20af3fde22b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Tue, 28 Nov 2023 13:57:00 +0100 Subject: [PATCH] Add tests --- Project.toml | 7 ------- src/MultivariateBases.jl | 3 ++- src/fixed.jl | 8 ++++++-- src/interface.jl | 12 ++++++++++-- test/Project.toml | 4 ++++ test/fixed.jl | 18 +++++++++++++++++- test/monomial.jl | 24 ++++++++++++++++++++++++ 7 files changed, 63 insertions(+), 13 deletions(-) create mode 100644 test/Project.toml diff --git a/Project.toml b/Project.toml index 617d156..c51ef7b 100644 --- a/Project.toml +++ b/Project.toml @@ -11,10 +11,3 @@ MutableArithmetics = "d8a4904e-b15c-11e9-3269-09a3773c0cb0" MultivariatePolynomials = "0.5.3" MutableArithmetics = "1" julia = "1.6" - -[extras] -DynamicPolynomials = "7c1d4256-1411-5781-91ec-d7bc3513ac07" -Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" - -[targets] -test = ["Test", "DynamicPolynomials"] diff --git a/src/MultivariateBases.jl b/src/MultivariateBases.jl index 826b7d0..f22122b 100644 --- a/src/MultivariateBases.jl +++ b/src/MultivariateBases.jl @@ -22,7 +22,8 @@ export AbstractGegenbauerBasis, ChebyshevBasis, ChebyshevBasisFirstKind, ChebyshevBasisSecondKind -export univariate_orthogonal_basis, +export generators, + univariate_orthogonal_basis, reccurence_first_coef, reccurence_second_coef, reccurence_third_coef, diff --git a/src/fixed.jl b/src/fixed.jl index 86c4942..d7a735b 100644 --- a/src/fixed.jl +++ b/src/fixed.jl @@ -23,7 +23,10 @@ function MP.polynomial_type( V = MA.promote_operation(+, U, U) return MP.polynomial_type(PT, V) end -function MP.polynomial(f::Function, basis::AbstractPolynomialVectorBasis{P}) where {P} +function MP.polynomial( + f::Function, + basis::AbstractPolynomialVectorBasis{P}, +) where {P} if isempty(generators(basis)) return zero(P) else @@ -61,7 +64,8 @@ function MP.polynomial( MA.add!!, 1:n; init = MA.Zero(), - ); end, + ) + end, MA.add!!, 1:n; init = MA.Zero(), diff --git a/src/interface.jl b/src/interface.jl index 8bfe8b9..da6548f 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -34,8 +34,16 @@ Base.getindex(basis::AbstractPolynomialBasis, i::Int) = generators(basis)[i] # Overload some of the `MP` interface for convenience MP.mindegree(basis::AbstractPolynomialBasis) = MP.mindegree(generators(basis)) MP.maxdegree(basis::AbstractPolynomialBasis) = MP.maxdegree(generators(basis)) -MP.mindegree(basis::AbstractPolynomialBasis, v) = MP.mindegree(generators(basis), v) -MP.maxdegree(basis::AbstractPolynomialBasis, v) = MP.maxdegree(generators(basis), v) +MP.extdegree(basis::AbstractPolynomialBasis) = MP.extdegree(generators(basis)) +function MP.mindegree(basis::AbstractPolynomialBasis, v) + return MP.mindegree(generators(basis), v) +end +function MP.maxdegree(basis::AbstractPolynomialBasis, v) + return MP.maxdegree(generators(basis), v) +end +function MP.extdegree(basis::AbstractPolynomialBasis, v) + return MP.extdegree(generators(basis), v) +end MP.nvariables(basis::AbstractPolynomialBasis) = MP.nvariables(generators(basis)) MP.variables(basis::AbstractPolynomialBasis) = MP.variables(generators(basis)) diff --git a/test/Project.toml b/test/Project.toml new file mode 100644 index 0000000..97b28c0 --- /dev/null +++ b/test/Project.toml @@ -0,0 +1,4 @@ +[deps] +DynamicPolynomials = "7c1d4256-1411-5781-91ec-d7bc3513ac07" +MultivariateBases = "be282fd4-ad43-11e9-1d11-8bd9d7e43378" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/test/fixed.jl b/test/fixed.jl index d2a5e1d..d63c574 100644 --- a/test/fixed.jl +++ b/test/fixed.jl @@ -5,11 +5,25 @@ using DynamicPolynomials @polyvar x y @testset "Polynomials" begin - basis = FixedPolynomialBasis([1 - x^2, x^2 + 2]) + gens = [1 - x^2, x^2 + 2] + basis = FixedPolynomialBasis(gens) @test polynomial_type(basis, Int) == polynomial_type(x, Int) @test polynomial(one, basis) == 3 @test basis[1] == 1 - x^2 @test basis[2] == x^2 + 2 + @test collect(basis) == gens + @test generators(basis) == gens + @test length(basis) == 2 + @test firstindex(basis) == 1 + @test lastindex(basis) == 2 + @test mindegree(basis) == 0 + @test mindegree(basis, x) == 0 + @test maxdegree(basis) == 2 + @test maxdegree(basis, x) == 2 + @test extdegree(basis) == (0, 2) + @test extdegree(basis, x) == (0, 2) + @test variables(basis) == [x] + @test nvariables(basis) == 1 @test sprint(show, basis) == "FixedPolynomialBasis([1 - x², 2 + x²])" @test sprint(print, basis) == "FixedPolynomialBasis([1 - x^2, 2 + x^2])" b2 = basis[2:2] @@ -53,6 +67,8 @@ end end @testset "Empty" begin basis = FixedPolynomialBasis(typeof(x + 1)[]) + @test isempty(basis) + @test isempty(eachindex(basis)) p = @inferred polynomial(zeros(Int, 0, 0), basis, Int) @test iszero(p) end diff --git a/test/monomial.jl b/test/monomial.jl index 3a52b2e..a0e6f2a 100644 --- a/test/monomial.jl +++ b/test/monomial.jl @@ -10,6 +10,13 @@ using DynamicPolynomials @test coefficients(x + 4y, MonomialBasis) == [4, 1] @test basis[1] == y @test basis[2] == x + @test generators(basis) == [y, x] + @test collect(basis) == [y, x] + @test length(basis) == 2 + @test firstindex(basis) == 1 + @test lastindex(basis) == 2 + @test variables(basis) == [x, y] + @test nvariables(basis) == 2 @test sprint(show, basis) == "MonomialBasis([y, x])" @test sprint(show, MIME"text/print"(), basis) == "MonomialBasis([y, x])" @test sprint(show, MIME"text/plain"(), basis) == "MonomialBasis([y, x])" @@ -33,6 +40,15 @@ end @testset "merge_bases" begin basis1 = MonomialBasis([x^2, y^2]) basis2 = MonomialBasis([x * y, y^2]) + @test mindegree(basis2) == 2 + @test mindegree(basis2, x) == 0 + @test mindegree(basis2, y) == 1 + @test maxdegree(basis2) == 2 + @test maxdegree(basis2, x) == 1 + @test maxdegree(basis2, y) == 2 + @test extdegree(basis2) == (2, 2) + @test extdegree(basis2, x) == (0, 1) + @test extdegree(basis2, y) == (1, 2) basis, I1, I2 = MultivariateBases.merge_bases(basis1, basis2) @test basis.monomials == [y^2, x * y, x^2] @test I1 == [1, 0, 2] @@ -42,3 +58,11 @@ end @testset "API degree = $degree" for degree in 0:3 api_test(MonomialBasis, degree) end + +@testset "Empty" begin + basis = MonomialBasis(typeof(x^2)[]) + @test isempty(basis) + @test isempty(eachindex(basis)) + p = @inferred polynomial(zeros(Int, 0, 0), basis, Int) + @test iszero(p) +end