Skip to content

Commit

Permalink
Improve type stability of expectation
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Nov 28, 2023
1 parent 442c962 commit 6e2df03
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/expectation.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function _expectation::Measure, p::_APL, f)
function _expectation::Measure{S}, p::_APL{T}, f) where {S,T}
i = 1
s = 0
s = zero(MA.promote_operation(*, S, T))
for t in MP.terms(p)
while i <= length.x) && MP.monomial(t) != μ.x[i]
i += 1
Expand Down
8 changes: 4 additions & 4 deletions test/expectation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
p = x[3] - 2x[1] * x[2]^2 + 3x[3] * x[1] - 5x[1]^3
v = (1, 2, 3)
m = dirac(monomials(p), x => v)
@test MultivariateMoments.expectation(m, p) ==
@test (@inferred MultivariateMoments.expectation(m, p)) ==
p(x => v) ==
MultivariateMoments.expectation(p, m)
(@inferred MultivariateMoments.expectation(p, m))
@test_throws ErrorException dot(x[1] * x[2] * x[3], m)
@test dot(0.5 * x[1] * x[2]^2, m) == 2.0
@test dot(m, x[1] * x[3]) == 3
@test (@inferred dot(0.5 * x[1] * x[2]^2, m)) == 2.0
@test (@inferred dot(m, x[1] * x[3])) == 3
end

0 comments on commit 6e2df03

Please sign in to comment.