给定一个序列 (d1, d2, ..., dn),我想要计算所有 i != j 时的乘积 (1 - Dij),其中 Dij = 1 如果 di = dj,否则为 0。
The code I have only checks Dij when i
prod = 1;
for (int i=1; i<n; ++i) {
for (int j=i; j<=n; ++j) {
prod *= (1 - Dij);
}
}
I know I can stop when I get Dij=1, but what I'm trying to do is get a minimal expression of the Dij's to check. This way I have one expression and then I can use difference sequences and evaluate it. So I know that I can do i<j
instead of i != j
. So I want to expand out this product and get something like this for n=3:
(1 - D12) (1 - D13) (1 - D23) = 1 - D12 - D13 - D23 + D12*D13 + D12*D23 + D13*D23 - D12*D13*D23
But there is more that I can do. This expression is actually always equal to
1 - D12 - D13 - D23 + 3 * D12*D13 - D12*D13*D23
My questions are:
Why is D12 * D13 = D12 * D23? This is always true (meaning it doesn't matter what the d sequence is) but I don't really get why because it seems to me that this means D13 = D23 which isn't always true (it depends on the d sequence). This is the relation that helps make the expression smaller.
How can I find all the relations like this and get a minimal expression? Is the expression above minimal? I don't even know.