Skip to content

Commit 1978d1d

Browse files
authored
Return AlgebraElement from Poly (#128)
* Return AlgebraElement from Poly * Fixes * Fix format
1 parent 25246dc commit 1978d1d

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

src/variable.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,5 @@ function JuMP.add_variable(
107107
end
108108
return vref
109109
end
110-
return MP.polynomial(_newvar, v.p.polynomial_basis)
110+
return MB.algebra_element(_newvar, v.p.polynomial_basis)
111111
end

test/variable.jl

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module TestVariable
33
using Test
44

55
import MutableArithmetics as MA
6+
import StarAlgebras as SA
67

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

40+
function _algebra_element_type(B, mono)
41+
M = MP.monomial_type(mono)
42+
C = JuMP.VariableRef
43+
return SA.AlgebraElement{
44+
MB.Algebra{MB.SubBasis{B,M,MP.monomial_vector_type(M)},B,M},
45+
C,
46+
Vector{C},
47+
}
48+
end
49+
3950
function _test_variable(
4051
m,
4152
p,
4253
monos,
4354
binary = false,
4455
integer = false,
45-
vars = true,
4656
use_y = true,
57+
B = MB.Monomial,
4758
)
48-
PT = polynomial_type(
49-
use_y ? first(monos) : first(variables(first(monos))),
50-
vars ? JuMP.VariableRef : JuMP.AffExpr,
59+
@test isa(
60+
p,
61+
_algebra_element_type(
62+
B,
63+
use_y ? first(monos) : first(variables(first(monos))),
64+
),
5165
)
52-
@test isa(p, PT)
53-
@test monomials(p) == monomial_vector(monos)
54-
if vars
55-
@test all-> JuMP.is_binary(α) == binary, coefficients(p))
56-
@test all-> JuMP.is_integer(α) == integer, coefficients(p))
57-
else
58-
@test all(
59-
α -> JuMP.is_binary(first(keys.terms))) == binary,
60-
coefficients(p),
61-
)
62-
@test all(
63-
α -> JuMP.is_integer(first(keys.terms))) == integer,
64-
coefficients(p),
65-
)
66-
end
66+
@test SA.basis(p).monomials == monomial_vector(monos)
67+
coeffs = SA.coeffs(p, MB.explicit_basis(p))
68+
@test all-> JuMP.is_binary(α) == binary, coeffs)
69+
@test all-> JuMP.is_integer(α) == integer, coeffs)
6770
end
6871

6972
function test_MonomialBasis(var)
@@ -72,7 +75,7 @@ function test_MonomialBasis(var)
7275
X = [1, y, x^2, x, y^2]
7376
m = Model()
7477
@variable m p1[1:3] Poly(X)
75-
PT = polynomial_type(x * y, JuMP.VariableRef)
78+
PT = _algebra_element_type(MB.Monomial, x * y)
7679
@test isa(p1, Vector{PT})
7780
_test_variable(m, p1[1], X)
7881
@variable(m, p2, Poly(X), integer = true)
@@ -102,7 +105,7 @@ function test_ScaledMonomialBasis(var)
102105
false,
103106
true,
104107
false,
105-
false,
108+
MB.ScaledMonomial,
106109
)
107110
end
108111

0 commit comments

Comments
 (0)