From 63545e60fe93e1626085a6dcb917dbc32de5379d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Wed, 11 Mar 2026 07:42:24 +0100 Subject: [PATCH] Define monomial_type for tuple of variables --- src/promotion.jl | 9 +++++++++ test/promotion.jl | 17 +++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/promotion.jl b/src/promotion.jl index 16fa23a..f00b381 100644 --- a/src/promotion.jl +++ b/src/promotion.jl @@ -50,3 +50,12 @@ end vars = merge(V1, V2...) Monomial{vars, length(vars)} end + +function MP.monomial_type(vars::Tuple{V,Vararg{Variable}}) where {V<:Variable} + sorted = merge((first(vars),), Base.tail(vars)...) + return Monomial{sorted, length(sorted)} +end + +function MP.monomial_type(::Type{T}) where {T<:Tuple{Variable,Vararg{Variable}}} + return monomial_type(tuple((V() for V in fieldtypes(T))...)) +end diff --git a/test/promotion.jl b/test/promotion.jl index b48db2e..6987feb 100644 --- a/test/promotion.jl +++ b/test/promotion.jl @@ -41,6 +41,23 @@ @test typeof(@inferred(promote(x, Monomial{tuple(), 0}()))) == NTuple{2, Monomial{(x,), 1}} end +@testset "monomial_type for variable tuples" begin + @polyvar x y z + + @test @inferred(monomial_type((x,))) == Monomial{(x,), 1} + @test @inferred(monomial_type((x,))) == monomial_type(x) + @test @inferred(monomial_type((x, y))) == Monomial{(x, y), 2} + @test @inferred(monomial_type((x, y))) == typeof(x * y) + @test @inferred(monomial_type((y, x))) == Monomial{(x, y), 2} + @test @inferred(monomial_type((x, y, z))) == Monomial{(x, y, z), 3} + @test @inferred(monomial_type((x, y, z))) == typeof(x * y * z) + @test @inferred(monomial_type((z, x, y))) == Monomial{(x, y, z), 3} + + @test @inferred(monomial_type(typeof((x,)))) == Monomial{(x,), 1} + @test @inferred(monomial_type(typeof((x, y)))) == Monomial{(x, y), 2} + @test @inferred(monomial_type(typeof((x, y, z)))) == Monomial{(x, y, z), 3} +end + @testset "conversion" begin @polyvar x y z