@@ -6,19 +6,14 @@ const ERR_DIFFERENT_SAMPLE_SPACES = ArgumentError(
6
6
7
7
import Base: + , * , / , -
8
8
9
- function _plus (d1, d2, T)
10
- classes (d1) == classes (d2) || throw (ERR_DIFFERENT_SAMPLE_SPACES)
11
- S = d1. scitype
12
- decoder = d1. decoder
13
- prob_given_ref = copy (d1. prob_given_ref)
14
- for ref in keys (prob_given_ref)
15
- prob_given_ref[ref] += d2. prob_given_ref[ref]
16
- end
17
- return T (S, decoder, prob_given_ref)
9
+ pdf_matrix (d:: UnivariateFinite , L) = pdf .(d, L)
10
+ pdf_matrix (d:: AbstractArray{<:UnivariateFinite} , L) = pdf (d, L)
11
+
12
+ function + (d1:: U , d2:: U ) where U <: SingletonOrArray
13
+ L = classes (d1)
14
+ L == classes (d2) || throw (ERR_DIFFERENT_SAMPLE_SPACES)
15
+ return UnivariateFinite (L, pdf_matrix (d1, L) + pdf_matrix (d2, L))
18
16
end
19
- + (d1:: U , d2:: U ) where U <: UnivariateFinite = _plus (d1, d2, UnivariateFinite)
20
- + (d1:: U , d2:: U ) where U <: UnivariateFiniteArray =
21
- _plus (d1, d2, UnivariateFiniteArray)
22
17
23
18
function _minus (d, T)
24
19
S = d. scitype
32
27
- (d:: UnivariateFinite ) = _minus (d, UnivariateFinite)
33
28
- (d:: UnivariateFiniteArray ) = _minus (d, UnivariateFiniteArray)
34
29
35
- function _minus (d1, d2, T)
36
- classes (d1) == classes (d2) || throw (ERR_DIFFERENT_SAMPLE_SPACES)
37
- S = d1. scitype
38
- decoder = d1. decoder
39
- prob_given_ref = copy (d1. prob_given_ref)
40
- for ref in keys (prob_given_ref)
41
- prob_given_ref[ref] -= d2. prob_given_ref[ref]
42
- end
43
- return T (S, decoder, prob_given_ref)
30
+ function - (d1:: U , d2:: U ) where U <: SingletonOrArray
31
+ L = classes (d1)
32
+ L == classes (d2) || throw (ERR_DIFFERENT_SAMPLE_SPACES)
33
+ return UnivariateFinite (L, pdf_matrix (d1, L) - pdf_matrix (d2, L))
44
34
end
45
- - (d1:: U , d2:: U ) where U <: UnivariateFinite = _minus (d1, d2, UnivariateFinite)
46
- - (d1:: U , d2:: U ) where U <: UnivariateFiniteArray =
47
- _minus (d1, d2, UnivariateFiniteArray)
48
35
49
36
# It seems that the restriction `x::Number` below (applying only to the
50
37
# array case) is unavoidable because of a method ambiguity with
0 commit comments