Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Nov 28, 2023
1 parent 640a9d3 commit 551c841
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 13 deletions.
7 changes: 0 additions & 7 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
3 changes: 2 additions & 1 deletion src/MultivariateBases.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 6 additions & 2 deletions src/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -61,7 +64,8 @@ function MP.polynomial(
MA.add!!,
1:n;
init = MA.Zero(),
); end,
)
end,
MA.add!!,
1:n;
init = MA.Zero(),
Expand Down
12 changes: 10 additions & 2 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
4 changes: 4 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[deps]
DynamicPolynomials = "7c1d4256-1411-5781-91ec-d7bc3513ac07"
MultivariateBases = "be282fd4-ad43-11e9-1d11-8bd9d7e43378"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
18 changes: 17 additions & 1 deletion test/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
24 changes: 24 additions & 0 deletions test/monomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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])"
Expand All @@ -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]
Expand All @@ -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

0 comments on commit 551c841

Please sign in to comment.