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

Return AlgebraElement from Poly #128

Merged
merged 3 commits into from
Jul 1, 2024
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
2 changes: 1 addition & 1 deletion src/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,5 @@ function JuMP.add_variable(
end
return vref
end
return MP.polynomial(_newvar, v.p.polynomial_basis)
return MB.algebra_element(_newvar, v.p.polynomial_basis)
end
45 changes: 24 additions & 21 deletions test/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module TestVariable
using Test

import MutableArithmetics as MA
import StarAlgebras as SA

using MultivariatePolynomials
const MP = MultivariatePolynomials
Expand Down Expand Up @@ -36,34 +37,36 @@ function test_variable_macro_with_Poly(var)
@test_throws ErrorException @variable(m, p3[2:3] >= 0, Poly(X))
end

function _algebra_element_type(B, mono)
M = MP.monomial_type(mono)
C = JuMP.VariableRef
return SA.AlgebraElement{
MB.Algebra{MB.SubBasis{B,M,MP.monomial_vector_type(M)},B,M},
C,
Vector{C},
}
end

function _test_variable(
m,
p,
monos,
binary = false,
integer = false,
vars = true,
use_y = true,
B = MB.Monomial,
)
PT = polynomial_type(
use_y ? first(monos) : first(variables(first(monos))),
vars ? JuMP.VariableRef : JuMP.AffExpr,
@test isa(
p,
_algebra_element_type(
B,
use_y ? first(monos) : first(variables(first(monos))),
),
)
@test isa(p, PT)
@test monomials(p) == monomial_vector(monos)
if vars
@test all(α -> JuMP.is_binary(α) == binary, coefficients(p))
@test all(α -> JuMP.is_integer(α) == integer, coefficients(p))
else
@test all(
α -> JuMP.is_binary(first(keys(α.terms))) == binary,
coefficients(p),
)
@test all(
α -> JuMP.is_integer(first(keys(α.terms))) == integer,
coefficients(p),
)
end
@test SA.basis(p).monomials == monomial_vector(monos)
coeffs = SA.coeffs(p, MB.explicit_basis(p))
@test all(α -> JuMP.is_binary(α) == binary, coeffs)
@test all(α -> JuMP.is_integer(α) == integer, coeffs)
end

function test_MonomialBasis(var)
Expand All @@ -72,7 +75,7 @@ function test_MonomialBasis(var)
X = [1, y, x^2, x, y^2]
m = Model()
@variable m p1[1:3] Poly(X)
PT = polynomial_type(x * y, JuMP.VariableRef)
PT = _algebra_element_type(MB.Monomial, x * y)
@test isa(p1, Vector{PT})
_test_variable(m, p1[1], X)
@variable(m, p2, Poly(X), integer = true)
Expand Down Expand Up @@ -102,7 +105,7 @@ function test_ScaledMonomialBasis(var)
false,
true,
false,
false,
MB.ScaledMonomial,
)
end

Expand Down
Loading