comparison toolboxes/FullBNT-1.0.7/bnt/potentials/@scgpot/direct_combine_pots.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function pot = direct_combine_pots(pot1, pot2)
2 % DIRECTED_COMBINE_POTS The combination operation corresponds to ordinary composition of conditional distributions.
3 % In some sense is similar to that of forming disjoint union of set.
4 % pot = direct_combine_pots(pot1, pot2)
5
6 % directed combine can be performed under the conditon that the head node set of pot1 is disjoint from the domain of
7 % pot2 or vice versa. if the last conditon was satisfied we exchange the pot1 and pot2 firstly then perform the operation.
8 % If neither of them was satified the directed combine is undifined.
9
10
11 if isempty( myintersect(pot1.domain, pot2.cheaddom) )
12 pot1 = pot1;
13 pot2 = pot2;
14 elseif isempty( myintersect(pot2.domain, pot1.cheaddom))
15 temppot = pot1;
16 pot1 = pot2;
17 pot2 = temppot;
18 else
19 assert(0);
20 return;
21 end
22
23 domain = myunion(pot1.domain, pot2.domain);
24 nodesizes = zeros(1,max(domain));
25 nodesizes(pot2.ctaildom) = pot2.ctailsizes;
26 nodesizes(pot2.cheaddom) = pot2.cheadsizes;
27 nodesizes(pot2.ddom) = pot2.dsizes;
28 nodesizes(pot1.ctaildom) = pot1.ctailsizes;
29 nodesizes(pot1.cheaddom) = pot1.cheadsizes;
30 nodesizes(pot1.ddom) = pot1.dsizes;
31
32 dom_u = mysetdiff(pot2.ctaildom, pot1.cheaddom);
33 if ~isempty(dom_u) & ~mysubset(dom_u, pot1.ctaildom)
34 pot1 = extension_pot(pot1, [], [], dom_u, nodesizes(dom_u));
35 end
36
37 dom_u = myunion(pot1.cheaddom, pot1.ctaildom);
38 if ~isempty(dom_u) & ~mysubset(dom_u, pot2.ctaildom)
39 pot2 = extension_pot(pot2, [], [], dom_u, nodesizes(dom_u));
40 end
41
42
43 cheaddom = myunion(pot1.cheaddom, pot2.cheaddom);
44 ctaildom = mysetdiff(myunion(pot1.ctaildom, pot2.ctaildom), cheaddom);
45 cdom = myunion(cheaddom, ctaildom);
46 ddom = mysetdiff(domain, cdom);
47 dsizes = nodesizes(ddom);
48 dsize = prod(nodesizes(ddom));
49 cheadsizes = nodesizes(cheaddom);
50 cheadsize = sum(nodesizes(cheaddom));
51 ctailsizes = nodesizes(ctaildom);
52 ctailsize = sum(nodesizes(ctaildom));
53
54 r1 = pot1.cheadsize;
55 s1 = pot1.ctailsize;
56 scpot = cell(1, dsize);
57 mask1 = [];
58 mask2 = [];
59 if ~isempty(pot1.ddom)
60 mask1 = find_equiv_posns(pot1.ddom, ddom);
61 end
62 if ~isempty(pot2.ddom)
63 mask2 = find_equiv_posns(pot2.ddom, ddom);
64 end
65 cmask1 = [];
66 cmask2 = [];
67 if ~isempty(pot1.cheaddom)
68 cmask1 = find_equiv_posns(pot1.cheaddom, cheaddom);
69 end
70 if ~isempty(pot2.cheaddom)
71 cmask2 = find_equiv_posns(pot2.cheaddom, cheaddom);
72 end
73
74 u1 = block(cmask1, cheadsizes);
75 u2 = block(cmask2, cheadsizes);
76
77 fmaskh = find_equiv_posns(pot1.cheaddom, pot2.ctaildom);
78 fmaskt = find_equiv_posns(pot1.ctaildom, pot2.ctaildom);
79
80 fh = block(fmaskh, pot2.ctailsizes);
81 ft = block(fmaskt, pot2.ctailsizes);
82
83 for i=1:dsize
84 sub = ind2subv(dsizes, i);
85 sub1 = sub(mask1);
86 sub2 = sub(mask2);
87 ind1 = subv2ind(pot1.dsizes, sub1);
88 ind2 = subv2ind(pot2.dsizes, sub2);
89
90 if isempty(ind1)
91 ind1 = 1;
92 end
93 if isempty(ind2)
94 ind2 = 1;
95 end
96 potc1 = struct(pot1.scgpotc{ind1});
97 potc2 = struct(pot2.scgpotc{ind2});
98 p = potc1.p;
99 q = potc2.p;
100 ro = p*q;
101
102 A = potc1.A;
103 B = potc1.B;
104 C = potc1.C;
105
106 E = potc2.A;
107 F = potc2.B;
108 G = potc2.C;
109
110 F1 = F(:, fh);
111 F2 = F(:, ft);
112
113 if ~isempty(F1)
114 K1 = F1*A;
115 K2 = F1*B;
116 FCF = F1*C*F1';
117 FC = F1*C;
118 CFT = C*F1';
119 else
120 K1 = zeros(size(E));
121 K2 = zeros(size(F2));
122 FCF = zeros(size(G));
123 FC = zeros(size(C, 1), size(G, 2));
124 CFT = zeros(size(G, 2), size(C, 1));
125 end
126
127
128 U = zeros(cheadsize,1);
129 W = zeros(cheadsize,cheadsize);
130 V = zeros(cheadsize,ctailsize);
131
132 if cheadsize > 0
133 U(u1) = A;
134 U(u2) = E + K1;
135 W(u1, u1) = C;
136 W(u2, u2) = G + FCF;
137 W(u1, u2) = CFT;
138 W(u2, u1) = FC;
139 else
140 U = zeros(cheadsize,1);
141 W = zeros(cheadsize,cheadsize);
142 end
143 if cheadsize > 0 | ctailsize > 0
144 if ~isempty(u1)
145 V(u1, :) = B;
146 else
147 V(u1, :) = zeros(potc1.cheadsize, ctailsize);
148 end
149 if ~isempty(u2)
150 V(u2, :) = F2 + K2;
151 else
152 V(u2, :) = zeros(potc2.cheadsize, ctailsize);
153 end
154 else
155 V = zeros(cheadsize,ctailsize);
156 end
157
158 scpot{i} = scgcpot(cheadsize, ctailsize, ro, U, V, W);
159 end
160
161 pot = scgpot(ddom, cheaddom, ctaildom, nodesizes, scpot);