From 3a958d4b7eceba52ff547efff28078f523febf4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Mon, 17 Jun 2024 23:25:00 +0200 Subject: [PATCH 1/4] Support expectation with different basis --- src/expectation.jl | 9 +++++++++ test/expectation.jl | 2 ++ 2 files changed, 11 insertions(+) diff --git a/src/expectation.jl b/src/expectation.jl index 06e0c11..31881e7 100644 --- a/src/expectation.jl +++ b/src/expectation.jl @@ -21,6 +21,15 @@ function _expectation( return s end +function _expectation( + μ::MomentVector{S,<:MB.SubBasis{B}}, + p::SA.AlgebraElement{<:MB.Algebra}, + f, +) where {S,B} + basis = MB.FullBasis{B,MP.monomial_type(typeof(p))}() + return _expectation(μ, MB.algebra_element(SA.coeffs(p, basis), basis), f) +end + function _expectation(μ::MomentVector, p::MP.AbstractPolynomialLike, f) return _expectation( μ, diff --git a/test/expectation.jl b/test/expectation.jl index 33d397d..26945f7 100644 --- a/test/expectation.jl +++ b/test/expectation.jl @@ -17,5 +17,7 @@ @test (@inferred dot(0.5 * x[1] * x[2]^2, m)) == 2.0 a = MB.algebra_element([0.5], MB.SubBasis{MB.Monomial}([x[1] * x[2]^2])) @test (@inferred dot(a, m)) == 2.0 + a = MB.algebra_element([0.5], MB.SubBasis{MB.ScaledMonomial}([x[1] * x[2]^2])) + @test (@inferred dot(a, m)) == 2.0 @test (@inferred dot(m, x[1] * x[3])) == 3 end From 6d50737331256c111a62357dc1883121ac60c2f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Tue, 18 Jun 2024 09:05:54 +0200 Subject: [PATCH 2/4] Fix --- test/expectation.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/expectation.jl b/test/expectation.jl index 26945f7..fefea1c 100644 --- a/test/expectation.jl +++ b/test/expectation.jl @@ -18,6 +18,6 @@ a = MB.algebra_element([0.5], MB.SubBasis{MB.Monomial}([x[1] * x[2]^2])) @test (@inferred dot(a, m)) == 2.0 a = MB.algebra_element([0.5], MB.SubBasis{MB.ScaledMonomial}([x[1] * x[2]^2])) - @test (@inferred dot(a, m)) == 2.0 + @test (@inferred dot(a, m)) == 2√3 @test (@inferred dot(m, x[1] * x[3])) == 3 end From 608c693b2e771a3db24d18ef2a7ead8ab188a581 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Tue, 18 Jun 2024 09:06:00 +0200 Subject: [PATCH 3/4] fix format --- test/expectation.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/expectation.jl b/test/expectation.jl index fefea1c..9a07ee6 100644 --- a/test/expectation.jl +++ b/test/expectation.jl @@ -17,7 +17,10 @@ @test (@inferred dot(0.5 * x[1] * x[2]^2, m)) == 2.0 a = MB.algebra_element([0.5], MB.SubBasis{MB.Monomial}([x[1] * x[2]^2])) @test (@inferred dot(a, m)) == 2.0 - a = MB.algebra_element([0.5], MB.SubBasis{MB.ScaledMonomial}([x[1] * x[2]^2])) + a = MB.algebra_element( + [0.5], + MB.SubBasis{MB.ScaledMonomial}([x[1] * x[2]^2]), + ) @test (@inferred dot(a, m)) == 2√3 @test (@inferred dot(m, x[1] * x[3])) == 3 end From a858382ee3eeb8d918f55761ee4dd2e92a603e5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Tue, 18 Jun 2024 09:19:05 +0200 Subject: [PATCH 4/4] Fix for Julia v1.6 --- test/expectation.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/expectation.jl b/test/expectation.jl index 9a07ee6..cba3964 100644 --- a/test/expectation.jl +++ b/test/expectation.jl @@ -21,6 +21,6 @@ [0.5], MB.SubBasis{MB.ScaledMonomial}([x[1] * x[2]^2]), ) - @test (@inferred dot(a, m)) == 2√3 + @test (@inferred dot(a, m)) == 2 * √3 @test (@inferred dot(m, x[1] * x[3])) == 3 end