Skip to content

Commit aa0f5b0

Browse files
authored
Improve type stability of expectation (#75)
1 parent 442c962 commit aa0f5b0

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

src/expectation.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
function _expectation::Measure, p::_APL, f)
1+
function _expectation::Measure{S}, p::_APL{T}, f) where {S,T}
22
i = 1
3-
s = 0
3+
s = zero(MA.promote_operation(*, S, T))
44
for t in MP.terms(p)
55
while i <= length.x) && MP.monomial(t) != μ.x[i]
66
i += 1

test/expectation.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
p = x[3] - 2x[1] * x[2]^2 + 3x[3] * x[1] - 5x[1]^3
44
v = (1, 2, 3)
55
m = dirac(monomials(p), x => v)
6-
@test MultivariateMoments.expectation(m, p) ==
6+
@test (@inferred MultivariateMoments.expectation(m, p)) ==
77
p(x => v) ==
8-
MultivariateMoments.expectation(p, m)
8+
(@inferred MultivariateMoments.expectation(p, m))
99
@test_throws ErrorException dot(x[1] * x[2] * x[3], m)
10-
@test dot(0.5 * x[1] * x[2]^2, m) == 2.0
11-
@test dot(m, x[1] * x[3]) == 3
10+
@test (@inferred dot(0.5 * x[1] * x[2]^2, m)) == 2.0
11+
@test (@inferred dot(m, x[1] * x[3])) == 3
1212
end

0 commit comments

Comments
 (0)