From 184fc60de5d0ef51995fb6e2a061d846a4b76afa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Tue, 10 Nov 2026 15:55:26 +0100 Subject: [PATCH 1/3] Add LazyMatrix and make QuotientBasis use a matrix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move the Macaulay matrix LazyMatrix from MacaulayMatrix.jl so that it can be used by SumOfSquares to reduce modulo equality constraints without depending on MacaulayMatrix.jl. The monomial bases are now SA.SubBasis. QuotientBasis(basis, matrix) is now the quotient of the span of `basis` by the kernel of `matrix`. The coefficients are `matrix * a` where `a` are the coefficients in `basis` and the adjoint map is `transpose(matrix) * c`. It used to store a divisor and call `rem` but this is the same linear map with `matrix` the matrix of the remainders of the elements of `basis`. With the matrix, the adjoint map is explicit and the quotient can also be given by equations that do not come from a Gröbner basis, e.g., the null space of a Macaulay matrix. Sparse matrices are supported and their zeros are skipped. --- Project.toml | 2 + docs/src/index.md | 14 ++ src/MultivariateBases.jl | 2 + src/macaulay.jl | 324 +++++++++++++++++++++++++++++++++++++++ src/quotient.jl | 139 +++++++++++++++-- test/Project.toml | 1 + test/macaulay.jl | 59 +++++++ test/quotient.jl | 62 +++++--- 8 files changed, 570 insertions(+), 33 deletions(-) create mode 100644 src/macaulay.jl create mode 100644 test/macaulay.jl diff --git a/Project.toml b/Project.toml index 5d42821..2c21490 100644 --- a/Project.toml +++ b/Project.toml @@ -7,6 +7,7 @@ version = "0.4.1" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" MultivariatePolynomials = "102ac46a-7ee4-5c85-9060-abc95bfdeaa3" MutableArithmetics = "d8a4904e-b15c-11e9-3269-09a3773c0cb0" +SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" StarAlgebras = "0c0c59c1-dc5f-42e9-9a8b-b5dc384a6cd1" [weakdeps] @@ -22,5 +23,6 @@ JuMP = "1.15" MathOptInterface = "1" MultivariatePolynomials = "0.6" MutableArithmetics = "1.9" +SparseArrays = "1.10" StarAlgebras = "0.4.4" julia = "1.10" diff --git a/docs/src/index.md b/docs/src/index.md index 6493395..1f96159 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -47,3 +47,17 @@ Trigonometric SimpleBasis SemisimpleBasis ``` + +## Quotient basis + +```@docs +QuotientBasis +``` + +## Macaulay matrix + +```@docs +LazyMatrix +macaulay +expand! +``` diff --git a/src/MultivariateBases.jl b/src/MultivariateBases.jl index edcddc9..b0c62f5 100644 --- a/src/MultivariateBases.jl +++ b/src/MultivariateBases.jl @@ -60,7 +60,9 @@ include("legendre.jl") include("chebyshev.jl") include("trigonometric.jl") include("lagrange.jl") +import SparseArrays include("quotient.jl") +include("macaulay.jl") function algebra(basis::QuotientBasis{<:BasisElement{B,V}}) where {B,V} mstruct = if MP.is_commutative(V) diff --git a/src/macaulay.jl b/src/macaulay.jl new file mode 100644 index 0000000..803f0ff --- /dev/null +++ b/src/macaulay.jl @@ -0,0 +1,324 @@ +export LazyMatrix, macaulay, expand! + +""" + @enum(ShiftStatus, NOT_INCLUDED, INCLUDED, NOT_REDUNDANT, REDUNDANT) + +* `NOT_INCLUDED` : Not included yet in the rows of the Macaulay matrix +* `INCLUDED` : Already included in the rows of the Macaulay matrix but redundancy unknown +* `NOT_REDUNDANT` : Already included in the rows of the Macaulay matrix and no redundancy with the other rows included +* `REDUNDANT` : Redundant with another row already included in the Macaulay matrix +""" +@enum(ShiftStatus, NOT_INCLUDED, INCLUDED, NOT_REDUNDANT, REDUNDANT) + +""" + mutable struct LazyMatrix{T,P,V,B} + polynomials::V + row_shifts::B + shift_statuses::Vector{Vector{ShiftStatus}} + column_basis::B + end + +Macaulay matrix of the polynomials `polynomials` in the variables +`MP.variables(column_basis)`. The rows are the +polynomials `shift * polynomials[j]` for each `shift` in `row_shifts` +and `j` such that `shift_statuses[i][j]` is `INCLUDED` or `NOT_REDUNDANT` +where `i` is the index of `shift` in `row_shifts`. +The columns are indexed by the monomials of `column_basis`. +The rows can be added with [`expand!`](@ref) and the matrix can be built with +`SparseArrays.sparse`. +""" +mutable struct LazyMatrix{ + T, + P<:MP.AbstractPolynomialLike, + V<:AbstractVector{P}, + B<:SubBasis{Monomial}, +} + polynomials::V + row_shifts::B + shift_statuses::Vector{Vector{ShiftStatus}} + column_basis::B +end + +function LazyMatrix( + polynomials::V, + variables = MP.variables(polynomials), +) where {T,P<:MP.AbstractPolynomialLike{T},V<:AbstractVector{P}} + full = FullBasis{Monomial}(variables) + empty = SA.SubBasis(full, SA.key_type(full)[]) + return LazyMatrix{T,P,V,typeof(empty)}( + polynomials, + empty, + Vector{ShiftStatus}[], + empty, + ) +end + +function MP.monomial_type(::Type{<:LazyMatrix{T,P}}) where {T,P} + return MP.monomial_type(P) +end + +MP.variables(M::LazyMatrix) = MP.variables(M.column_basis) + +function Base.show(io::IO, M::LazyMatrix) + num_rows, num_cols = size(M) + println(io, "$(num_rows)×$(num_cols) Macaulay matrix for polynomials:") + for poly in M.polynomials + println(io, " ", poly) + end + println(io, "The row shifts are:") + println(io, M.row_shifts) + println(io, "The column basis is:") + println(io, M.column_basis) + return +end + +function Base.size(M::LazyMatrix, i::Int) + if i == 1 + return sum(M.shift_statuses; init = 0) do statuses + return count(statuses) do s + return s == INCLUDED || s == NOT_REDUNDANT + end + end + elseif i == 2 + return length(M.column_basis) + else + return 1 + end +end + +function Base.size(M::LazyMatrix) + return (size(M, 1), size(M, 2)) +end + +function _sub_basis(M::LazyMatrix, monos) + return parent(M.column_basis)[MP.monomial_vector(collect(monos))] +end + +abstract type AbstractShiftsSelector end + +""" + struct ColumnDegrees{V} <: AbstractShiftsSelector + degrees::V + end + +Select the shifts such that the maxdegree of the shifted polynomial is in +`degrees`. +""" +struct ColumnDegrees{V} <: AbstractShiftsSelector + degrees::V +end + +function select_shifts(vars, poly, d::ColumnDegrees) + deg = MP.maxdegree(poly) + if deg > maximum(d.degrees) + return nothing + else + return MP.monomials(vars, max.(0, d.degrees .- deg)) + end +end + +""" + struct LeadingTargetColumns{M} <: AbstractShiftsSelector + targets::Vector{M} + end + +Select the shifts such that the leading monomial of the shifted polynomial is +in `targets`. +""" +struct LeadingTargetColumns{M} <: AbstractShiftsSelector + targets::Vector{M} +end + +function select_shifts(vars, poly, d::LeadingTargetColumns{M}) where {M} + mono = MP.leading_monomial(poly) + return M[ + MP.div_multiple(target, mono) for + target in d.targets if MP.divides(mono, target) + ] +end + +""" + struct TargetColumns{M} <: AbstractShiftsSelector + targets::Vector{M} + end + +Select the shifts such that one of the monomials of the shifted polynomial is +in `targets`. +""" +struct TargetColumns{M} <: AbstractShiftsSelector + targets::Vector{M} +end + +function select_shifts(vars, poly, d::TargetColumns{M}) where {M} + return promote_type(M, MP.monomial_type(poly))[ + MP.div_multiple(target, mono) for mono in MP.monomials(poly) for + target in d.targets if MP.divides(mono, target) + ] +end + +""" + struct FixedShifts{M} <: AbstractShiftsSelector + shifts::Vector{M} + end + +Select the shifts `shifts` for every polynomial. +""" +struct FixedShifts{M} <: AbstractShiftsSelector + shifts::Vector{M} +end + +function select_shifts(vars, poly, d::FixedShifts) + return d.shifts +end + +""" + is_forever_trivial(M::LazyMatrix, col::MP.AbstractMonomialLike) + +Return `true` if there does not exists any shift (even outside of +`M.row_shifts`) that such that the shift of any of the polynomial +has `col` as one of its monomial. +""" +function is_forever_trivial(M::LazyMatrix, col::MP.AbstractMonomialLike) + return !any(M.polynomials) do poly + return any(Base.Fix2(MP.divides, col), MP.monomials(poly)) + end +end + +""" + is_saturated(M::LazyMatrix, col::MP.AbstractMonomialLike) + +Return `true` if all possible shift of a polynomial such that +`col` is one of the shifted monomial have been included. +""" +function is_saturated(M::LazyMatrix, col::MP.AbstractMonomialLike) + for (j, poly) in enumerate(M.polynomials) + for mono in MP.monomials(poly) + if MP.divides(mono, col) + shift = MP.div_multiple(col, mono) + i = _key_index(M.row_shifts, shift) + if isnothing(i) || M.shift_statuses[i][j] == NOT_INCLUDED + return false + end + end + end + end + return true +end + +""" + expand!(M::LazyMatrix, shifts_selector::AbstractShiftsSelector; sparse_columns::Bool = true) + +Add the row shifts selected by `shifts_selector`. +If `sparse_columns` is `false` then all monomials of degree up to the maximal +degree of the columns will be added to the columns. Otherwise, only the columns +that correspond to the monomial of one of the shifted polynomials will be added. +Return the number of rows added. +""" +function expand!(M::LazyMatrix, shifts_selector; sparse_columns::Bool = true) + vars = MP.variables(M) + MT = MP.monomial_type(typeof(M)) + row_monos_to_add = Dict{MT,Vector{ShiftStatus}}() + if sparse_columns + col_monos_to_add = Set{MT}() + else + col_maxdeg = isempty(M.column_basis) ? 0 : MP.maxdegree(M.column_basis) + end + added = 0 + for (j, poly) in enumerate(M.polynomials) + selected_shifts = select_shifts(vars, poly, shifts_selector) + if isnothing(selected_shifts) + continue + end + for shift in selected_shifts + i = _key_index(M.row_shifts, shift) + if isnothing(i) + if !haskey(row_monos_to_add, shift) + row_monos_to_add[shift] = + fill(NOT_INCLUDED, length(M.polynomials)) + end + statuses = row_monos_to_add[shift] + else + statuses = M.shift_statuses[i] + end + if statuses[j] == NOT_INCLUDED + added += 1 + statuses[j] = INCLUDED + for mono in MP.monomials(poly) + col = shift * mono + if isnothing(_key_index(M.column_basis, col)) + if sparse_columns + push!(col_monos_to_add, col) + else + col_maxdeg = max(col_maxdeg, MP.degree(col)) + end + end + end + end + end + end + if sparse_columns + if !isempty(col_monos_to_add) + M.column_basis = + SA.merge_bases(M.column_basis, _sub_basis(M, col_monos_to_add)) + end + else + M.column_basis = maxdegree_basis(parent(M.column_basis), col_maxdeg) + end + if !isempty(row_monos_to_add) + old_shifts = M.row_shifts + M.row_shifts = + SA.merge_bases(M.row_shifts, _sub_basis(M, keys(row_monos_to_add))) + old_statuses = M.shift_statuses + M.shift_statuses = + Vector{Vector{ShiftStatus}}(undef, length(M.row_shifts)) + for (shift, statuses) in zip(old_shifts.keys, old_statuses) + i = SA.key_index(M.row_shifts, shift) + M.shift_statuses[i] = statuses + end + for (shift, statuses) in row_monos_to_add + i = _key_index(M.row_shifts, shift) + M.shift_statuses[i] = statuses + end + end + return added +end + +""" + macaulay(polynomials, maxdegree, variables = MP.variables(polynomials); kws...) + +Return the [`LazyMatrix`](@ref) of the Macaulay matrix of degree `maxdegree` +of `polynomials` in the variables `variables`. +The keyword arguments are passed to [`expand!`](@ref). +""" +function macaulay( + polynomials, + maxdegree, + variables = MP.variables(polynomials); + kws..., +) + M = LazyMatrix(polynomials, variables) + expand!(M, ColumnDegrees(0:maxdegree); kws...) + return M +end + +function SparseArrays.sparse(M::LazyMatrix{T}) where {T} + row = 0 + I = Int[] + J = Int[] + K = T[] + for (shift, statuses) in + zip(keys_as_monomials(M.row_shifts), M.shift_statuses) + for (j, status) in enumerate(statuses) + if status == INCLUDED || status == NOT_REDUNDANT + row += 1 + for t in MP.terms(M.polynomials[j]) + push!(I, row) + push!(J, _key_index(M.column_basis, shift * MP.monomial(t))) + push!(K, MP.coefficient(t)) + end + end + end + end + @assert row == size(M, 1) + return SparseArrays.sparse(I, J, K, row, size(M, 2)) +end diff --git a/src/quotient.jl b/src/quotient.jl index e04aabb..e64009c 100644 --- a/src/quotient.jl +++ b/src/quotient.jl @@ -1,32 +1,146 @@ -struct QuotientBasis{T,I,B<:SA.AbstractBasis{T,I},D} <: SA.ExplicitBasis{T,I} +""" + struct QuotientBasis{T,I,B<:SA.AbstractBasis{T,I},M<:AbstractMatrix} <: + SA.ExplicitBasis{T,I} + basis::B + matrix::M + end + +Basis of the quotient of the span of `basis` by the kernel of `matrix`. The +coefficients of a polynomial `p` in this basis are `matrix * a` where `a` is +the vector of coefficients of `p` in `basis`. In particular, `p` is zero in the +quotient if and only if `matrix * a` is zero. The adjoint map sends the +coefficients `c` in this basis to the coefficients `transpose(matrix) * c` in +`basis`. + +To represent the quotient of the span `V` of `basis` modulo an ideal `J`, the +only requirement on `matrix` is that its kernel is `J ∩ V`, i.e., `matrix * a` +is zero if and only if the polynomial with coefficients `a` belongs to `J`. +Recombining the rows of `matrix` with an invertible matrix does not change its +kernel so it represents the same quotient, only its coordinates change. If the +rows of `matrix` are linearly independent, the length of the basis is the +dimension of the quotient. Two typical choices are: + +* If a Gröbner basis of `J` is known, `matrix[i, j]` is the coefficient of the + `i`th standard monomial in the remainder of the `j`th element of `basis` + modulo the Gröbner basis. For instance, modulo `x^2 + 1`, the basis + `[1, x, x^2, x^3]` gives `matrix = [1 0 -1 0; 0 1 0 -1]`. +* Otherwise, `J` can be replaced by its truncation `J_d` where `d` is the + maximal degree of `basis`. `J_d` is the row space of the Macaulay matrix of + degree `d`, i.e., the span of `m * h` for each generator `h` of `J` and + monomial `m` such that the degree of `m * h` is at most `d`. The rows of + `matrix` can be the reduced row echelon form of a basis of the linear + equations defining `J_d ∩ V`. Then `matrix * a` gives the coefficients of + the *truncated normal form*: the unique polynomial supported on the pivot + monomials that is congruent to `p` modulo `J_d`. Note that `J_d ∩ V` may be + strictly contained in `J ∩ V` if a combination of generators has a degree + smaller than the generators that are combined (a degree fall). +""" +struct QuotientBasis{T,I,B<:SA.AbstractBasis{T,I},M<:AbstractMatrix} <: + SA.ExplicitBasis{T,I} basis::B - divisor::D + matrix::M + function QuotientBasis( + basis::SA.AbstractBasis{T,I}, + matrix::AbstractMatrix, + ) where {T,I} + if size(matrix, 2) != length(basis) + throw( + DimensionMismatch( + "The number of columns `$(size(matrix, 2))` of the matrix does not match the length `$(length(basis))` of the basis", + ), + ) + end + return new{T,I,typeof(basis),typeof(matrix)}(basis, matrix) + end end implicit_basis(basis::QuotientBasis) = implicit_basis(basis.basis) MP.variables(basis::QuotientBasis) = MP.variables(basis.basis) -Base.iterate(basis::QuotientBasis) = iterate(basis.basis) -Base.iterate(basis::QuotientBasis, s) = iterate(basis.basis, s) -Base.length(basis::QuotientBasis) = length(basis.basis) +Base.length(basis::QuotientBasis) = size(basis.matrix, 1) + +function Base.show(io::IO, basis::QuotientBasis) + print( + io, + "Quotient of dimension $(length(basis)) of the span of ", + basis.basis, + ) + return +end function SA.algebra_element(coeffs, basis::QuotientBasis) return SA.AlgebraElement(coeffs, algebra(basis)) end +function _key_index(basis::SubBasis{Monomial}, mono::MP.AbstractMonomialLike) + return SA.key_index( + basis, + MP.exponents(MP.monomial(mono), MP.variables(basis)), + ) +end + +function _quotient_coeffs(p, basis::QuotientBasis) + sub = basis.basis + for mono in MP.monomials(p) + if isnothing(_key_index(sub, mono)) + throw( + ArgumentError( + "The monomial `$mono` is not in the basis `$sub` of which `$basis` is a quotient.", + ), + ) + end + end + return _mul(basis.matrix, MP.coefficients(p, sub)) +end + +# Computes `A * a` skipping the zero entries of `A`. This matters when the +# elements of `a` are, e.g., affine functions of decision variables as the +# product with zero would still create a term with zero coefficient. +function _mul(A::AbstractMatrix, a::AbstractVector) + U = MA.promote_operation(*, eltype(A), eltype(a)) + c = [zero(U) for _ in axes(A, 1)] + for j in axes(A, 2) + for i in axes(A, 1) + if !iszero(A[i, j]) + c[i] = MA.operate!!(MA.add_mul, c[i], A[i, j], a[j]) + end + end + end + return c +end + +function _mul(A::SparseArrays.AbstractSparseMatrixCSC, a::AbstractVector) + U = MA.promote_operation(*, eltype(A), eltype(a)) + c = [zero(U) for _ in axes(A, 1)] + rows = SparseArrays.rowvals(A) + vals = SparseArrays.nonzeros(A) + for j in axes(A, 2) + for k in SparseArrays.nzrange(A, j) + if !iszero(vals[k]) + i = rows[k] + c[i] = MA.operate!!(MA.add_mul, c[i], vals[k], a[j]) + end + end + end + return c +end + function MP.coefficients(p, basis::QuotientBasis) - return MP.coefficients(rem(p, basis.divisor), basis.basis) + return _quotient_coeffs(p, basis) end function SA.coeffs(coeffs, b::FullBasis{Monomial}, basis::QuotientBasis) - return MP.coefficients( + return _quotient_coeffs( MP.polynomial(values(coeffs), keys_as_monomials(keys(coeffs), b)), basis, ) end function SA.coeffs(coeffs, sub::SubBasis{Monomial}, basis::QuotientBasis) - return MP.coefficients(MP.polynomial(coeffs, keys_as_monomials(sub)), basis) + return _quotient_coeffs( + MP.polynomial(coeffs, keys_as_monomials(sub)), + basis, + ) end function SA.adjoint_coeffs( @@ -34,10 +148,9 @@ function SA.adjoint_coeffs( src::SubBasis{Monomial}, dest::QuotientBasis{<:BasisElement{Monomial}}, ) - return map(src) do poly - return sum(MP.terms(rem(MP.polynomial(poly), dest.divisor))) do t - return MP.coefficient(t) * - coeffs[SA.key_index(dest.basis, MP.exponents(t))] - end + a = transpose(dest.matrix) * coeffs + return map(keys_as_monomials(src)) do mono + i = _key_index(dest.basis, mono) + return isnothing(i) ? zero(eltype(a)) : a[i] end end diff --git a/test/Project.toml b/test/Project.toml index 3b3a785..a34c53d 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -7,6 +7,7 @@ MultivariateBases = "be282fd4-ad43-11e9-1d11-8bd9d7e43378" MultivariatePolynomials = "102ac46a-7ee4-5c85-9060-abc95bfdeaa3" MutableArithmetics = "d8a4904e-b15c-11e9-3269-09a3773c0cb0" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" +SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" StarAlgebras = "0c0c59c1-dc5f-42e9-9a8b-b5dc384a6cd1" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" TypedPolynomials = "afbbf031-7a57-5f58-a1b9-b774a0fad08d" diff --git a/test/macaulay.jl b/test/macaulay.jl new file mode 100644 index 0000000..34179eb --- /dev/null +++ b/test/macaulay.jl @@ -0,0 +1,59 @@ +using Test +using DynamicPolynomials +import MultivariateBases as MB +import SparseArrays + +function _test_rows(M::MB.LazyMatrix, expected_rows) + A = Matrix(SparseArrays.sparse(M)) + @test size(A) == size(M) + columns = MB.keys_as_monomials(M.column_basis) + rows = [polynomial(A[i, :], columns) for i in axes(A, 1)] + @test length(rows) == length(expected_rows) + @test Set(rows) == Set(expected_rows) +end + +@testset "Macaulay matrix" begin + @polyvar x y + p = x * y - 1 + q = x^2 - y + M = MB.macaulay([p, q], 2) + @test size(M) == (2, 4) + @test size(M, 3) == 1 + _test_rows(M, [p, q]) + @test MB.keys_as_monomials(M.column_basis) == [1, y, x * y, x^2] + @test MB.is_saturated(M, constant_monomial(p)) + @test !MB.is_saturated(M, x * y) + @test !MB.is_forever_trivial(M, x^2 * y) + @test MB.expand!(M, MB.ColumnDegrees(0:3)) == 4 + @test size(M) == (6, 9) + _test_rows(M, [p, x * p, y * p, q, x * q, y * q]) + @test MB.is_saturated(M, y) + @test MB.expand!(M, MB.ColumnDegrees(0:3)) == 0 + @test sprint(show, M) isa String + M = MB.macaulay([p, q], 3; sparse_columns = false) + @test size(M) == (6, 10) + @test MB.keys_as_monomials(M.column_basis) == monomials([x, y], 0:3) + @test isnothing(MB.select_shifts([x, y], p, MB.ColumnDegrees(0:1))) + M = MB.macaulay([p, x^3 - 1], 2) + _test_rows(M, [p]) + @polyvar z + M = MB.macaulay([q], 3, [x, y, z]) + _test_rows(M, [q, x * q, y * q, z * q]) +end + +@testset "Shift selectors" begin + @polyvar x y z + p = x * y - 1 + q = x^2 - y + @test !MB.is_forever_trivial(MB.LazyMatrix([p, q]), z) + @test MB.is_forever_trivial(MB.LazyMatrix([q]), z) + M = MB.LazyMatrix([p, q]) + @test MB.expand!(M, MB.LeadingTargetColumns([x^2 * y])) == 2 + _test_rows(M, [x * p, y * q]) + M = MB.LazyMatrix([p, q]) + @test MB.expand!(M, MB.TargetColumns([x^2 * y])) == 4 + _test_rows(M, [x * p, x^2 * y * p, y * q, x^2 * q]) + M = MB.LazyMatrix([p, q]) + @test MB.expand!(M, MB.FixedShifts([x])) == 2 + _test_rows(M, [x * p, x * q]) +end diff --git a/test/quotient.jl b/test/quotient.jl index 44d369e..dc187ab 100644 --- a/test/quotient.jl +++ b/test/quotient.jl @@ -3,30 +3,52 @@ using DynamicPolynomials import MultivariateBases as MB import StarAlgebras as SA -struct ImRem # Reduce modulo `x^2 = -1` -end -function Base.rem(m::AbstractMonomial, ::ImRem) - return prod((-1)^rem(e, 2) * v^mod(e, 2) for (v, e) in powers(m)) -end -function Base.rem(t::AbstractTerm, d::ImRem) - return coefficient(t) * rem(monomial(t), d) +import SparseArrays + +function _test_quotient(matrix) + @polyvar x y + sub = MB.SubBasis{MB.Monomial}([1, y, x]) + @test_throws DimensionMismatch MB.QuotientBasis(sub, ones(2, 2)) + # Quotient by the span of `x - y` + basis = MB.QuotientBasis(sub, matrix) + @test length(basis) == 2 + @test sprint(show, basis) == "Quotient of dimension 2 of the span of $sub" + @test coefficients(2 + 3x + y, basis) == [2, 4] + @test iszero(coefficients(x - y, basis)) + @test_throws ArgumentError coefficients(x^2, basis) + a = MB.algebra_element(2 + 3x + y) + @test SA.coeffs(a, basis) == [2, 4] + explicit = MB.explicit_basis(a) + exp = MB.algebra_element(SA.coeffs(a, explicit), explicit) + @test SA.coeffs(exp, basis) == [2, 4] + @test SA.adjoint_coeffs( + [5, 7], + MB.SubBasis{MB.Monomial}([y, x^2]), + basis, + ) == [7, 0] + q = MB.algebra_element([5, 7], basis) + @test SA.coeffs(q) == [5, 7] + return end -function Base.rem(p::AbstractPolynomial, d::ImRem) - return sum(rem(t, d) for t in terms(p)) + +@testset "QuotientBasis" begin + _test_quotient([1 0 0; 0 1 1]) + _test_quotient(SparseArrays.sparse([1 0 0; 0 1 1])) end -@testset "ImRem" begin +@testset "QuotientBasis modulo x^2 + 1" begin @polyvar x y - basis = MB.QuotientBasis(MB.SubBasis{MB.Monomial}([1, y, x]), ImRem()) + sub = MB.SubBasis{MB.Monomial}([1, y, x, x^2, x^2 * y, x^3]) + # Remainders `-1`, `-y` and `-x` of `x^2`, `x^2 * y` and `x^3` + R = [ + 1 0 0 -1 0 0 + 0 1 0 0 -1 0 + 0 0 1 0 0 -1 + ] + basis = MB.QuotientBasis(sub, SparseArrays.sparse(R)) @test length(basis) == 3 p = x^3 - 2x^2 * y + 3x^2 - coeffs = [3, 2, -1] - @test coefficients(p, basis) == coeffs - a = MB.algebra_element(p) - @test SA.coeffs(a, basis) == coeffs - explicit = MB.explicit_basis(a) - exp = MB.algebra_element(SA.coeffs(a, explicit), explicit) - @test SA.coeffs(exp, basis) == coeffs - q = MB.algebra_element(coeffs, basis) - @test SA.adjoint_coeffs(q, explicit) == [3, -2, 1] + @test MB.keys_as_monomials(sub) == [1, y, x, x^2, x^2 * y, x^3] + @test coefficients(p, basis) == [-3, 2, -1] + @test SA.adjoint_coeffs([1, 2, 3], sub, basis) == [1, 2, 3, -1, -2, -3] end From df7259fc172ef4e01af5251422e0ec091ca55083 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Tue, 17 Nov 2026 17:34:12 +0100 Subject: [PATCH 2/3] Use the monomial type of the columns for the shifts of LazyMatrix The monomial type of the polynomials may not contain all the variables of the columns, e.g., with TypedPolynomials, so the shifts could not be stored. --- src/macaulay.jl | 5 +++-- test/macaulay.jl | 11 +++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/macaulay.jl b/src/macaulay.jl index 803f0ff..8d6983c 100644 --- a/src/macaulay.jl +++ b/src/macaulay.jl @@ -53,8 +53,9 @@ function LazyMatrix( ) end -function MP.monomial_type(::Type{<:LazyMatrix{T,P}}) where {T,P} - return MP.monomial_type(P) +# The variables of the columns may not all be in the polynomials +function MP.monomial_type(::Type{<:LazyMatrix{T,P,V,B}}) where {T,P,V,B} + return MP.monomial_type(B) end MP.variables(M::LazyMatrix) = MP.variables(M.column_basis) diff --git a/test/macaulay.jl b/test/macaulay.jl index 34179eb..bbe4f81 100644 --- a/test/macaulay.jl +++ b/test/macaulay.jl @@ -2,6 +2,7 @@ using Test using DynamicPolynomials import MultivariateBases as MB import SparseArrays +import TypedPolynomials function _test_rows(M::MB.LazyMatrix, expected_rows) A = Matrix(SparseArrays.sparse(M)) @@ -57,3 +58,13 @@ end @test MB.expand!(M, MB.FixedShifts([x])) == 2 _test_rows(M, [x * p, x * q]) end + +@testset "Shifts in variables not in the polynomials" begin + # The type of the monomials of `q` does not contain `y` with + # `TypedPolynomials` so the shifts need the monomial type of the columns + TypedPolynomials.@polyvar x y + q = x^2 + 1 + M = MB.macaulay([q], 3, (x, y)) + @test MB.keys_as_monomials(M.row_shifts) == [1, y, x] + _test_rows(M, [q, x * q, y * q]) +end From af6b7a5528a8fee115971e15c3b893164051500e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Wed, 18 Nov 2026 19:16:05 +0100 Subject: [PATCH 3/3] Compare basis elements in monomial coordinates and document Macaulay selectors --- docs/src/index.md | 7 +++++++ test/utils.jl | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index 1f96159..21b7f0c 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -60,4 +60,11 @@ QuotientBasis LazyMatrix macaulay expand! +ColumnDegrees +FixedShifts +TargetColumns +LeadingTargetColumns +ShiftStatus +is_saturated +is_forever_trivial ``` diff --git a/test/utils.jl b/test/utils.jl index f46c4b5..09bf8c3 100644 --- a/test/utils.jl +++ b/test/utils.jl @@ -132,8 +132,8 @@ function api_test(B::Type{<:MB.AbstractMonomialIndexed}, degree) @test a ≈ monomial(p) @test monomial(p) ≈ a else - @test !(a ≈ monomial(p)) - @test !(monomial(p) ≈ a) + @test !(expanded ≈ monomial(p)) + @test !(monomial(p) ≈ expanded) end _wrap(s) = (B == MB.Monomial ? s : "$B($s)") @test sprint(show, p) == _wrap(sprint(show, monomial(p)))