public Varset Add(Varset varset) { Varset cp = new Varset(this); Varset zero = new Varset(0); cp.AlignLength(varset); while (!varset.Equals(zero)) { Varset tmp = cp.And(varset).LeftShift(1); cp = cp.Xor(varset); varset = tmp; } return cp; }
private ContingencyTableNode MakeContabLeafList(Varset variables, BitArray records) { Varset variablesCp = new Varset(variables); if (variablesCp.Equals(zero)) { int count = 0; for (int i = 0; i < records.Count; i++) { if (records[i]) { count += 1; } } return new ContingencyTableNode(count, 0, 1); } int firstIndex = variables.FindFirst(); int cardinality = network.GetCardinality(firstIndex); ContingencyTableNode ct = new ContingencyTableNode(0, cardinality, 0); variablesCp.Set(firstIndex, false); Varset remainingVariables = new Varset(variablesCp); for (int k = 0; k < cardinality; k++) { BitArray r = new BitArray(recordCount); r = r.Or(records); r = r.And(consistentRecords[firstIndex][k]); int count = 0; for (int i = 0; i < r.Count; i++) { if (r[i]) { count += 1; } } if (count > 0) { ContingencyTableNode child = MakeContabLeafList(remainingVariables, r); ct.SetChild(k, child); ct.LeafCount += child.LeafCount; } } return ct; }
private ContingencyTableNode MakeContab(Varset remainingVariables, ADNode node, int nodeIndex) { // check base case if (remainingVariables.Equals(zero)) { ContingencyTableNode ctn = new ContingencyTableNode(node.Count, 0, 1); return ctn; } int firstIndex = remainingVariables.FindFirst(); int n = network.GetCardinality(firstIndex); VaryNode vn = node.GetChild(firstIndex - nodeIndex - 1); ContingencyTableNode ct = new ContingencyTableNode(0, n, 0); Varset newVariables = Varset.ClearCopy(remainingVariables, firstIndex); ContingencyTableNode ctMcv = MakeContab(newVariables, node, nodeIndex); for (int k = 0; k < n; k++) { if (vn.GetChild(k) == null) { continue; } ADNode adn = vn.GetChild(k); ContingencyTableNode child = null; if (adn.LeafList.Count == 0) // これ注意 { child = MakeContab(newVariables, adn, firstIndex); } else { child = MakeContabLeafList(newVariables, adn.LeafList); } ct.SetChild(k, child); ct.LeafCount += child.LeafCount; ctMcv.Subtract(ct.GetChild(k)); } ct.SetChild(vn.Mcv, ctMcv); ct.LeafCount += ctMcv.LeafCount; return ct; }
public Varset Divide(Varset varset) { int length = this.item.Count; Varset n = new Varset(this); Varset d = new Varset(varset); //Console.Write("n: "); //n.Print(); //Console.Write("d: "); //d.Print(); Varset m = new Varset(n.item.Length); m.Set(0, true); Varset q = new Varset(n.item.Count); Varset zero = new Varset(n.item.Count); if (n.Equals(zero)) { return zero; } else if (d.Equals(zero)) { throw new ArgumentException("Zero Division."); } while (d.LessThan(n) || d.Equals(n)) { d = d.LeftShift(1); m = m.LeftShift(1); } Varset one = new Varset(n.item.Length); one.Set(0, true); while (one.LessThan(m)) { d = d.RightShift(1); m = m.RightShift(1); if (d.LessThan(n) || d.Equals(n)) { n = n.Subtract(d); q = q.Or(m); } } q = q.SubVarset(length); n = n.SubVarset(length); //Console.Write("q: "); //q.Print(); //Console.Write("n: "); //n.Print(); return q; }