Skip to content

Commit

Permalink
Support expectation with different basis (#82)
Browse files Browse the repository at this point in the history
* Support expectation with different basis

* Fix

* fix format

* Fix for Julia v1.6
  • Loading branch information
blegat authored Jun 18, 2024
1 parent c3180e1 commit 745c911
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/expectation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
μ,
Expand Down
5 changes: 5 additions & 0 deletions test/expectation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +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]),
)
@test (@inferred dot(a, m)) == 2 * 3
@test (@inferred dot(m, x[1] * x[3])) == 3
end

0 comments on commit 745c911

Please sign in to comment.