func TestEquivalent(t *testing.T) { t.Parallel() elements := []set.Element{"A", "B", "C", "D", "E", "F"} A := set.WithElements(elements...) B := set.WithElements(elements...) if !set.Equivalent(A, B) { t.Fatalf("%s and %s should be equivalent", A, B) } if set.Equivalent(A, set.Empty) { t.Fatalf("%s and %s should not be equivalent", A, set.Empty) } if !set.Equivalent(set.Empty, set.Empty) { t.Fatalf("%s and %s should be equivalent", set.Empty, set.Empty) } }
func TestString(t *testing.T) { t.Parallel() A := set.WithElements(1, 2, 3) B := set.Clone(A) if !set.Equivalent(A, B) { t.Fatalf("Clone of %s: %s should be equivalent", A, B) } B.Remove(1) if set.Equivalent(A, B) { t.Fatalf("%s should no longer be equivalent to %s", B, A) } if !A.Contains(1) { t.Fatalf("%s should still contain 1", A) } }
func TestSetComplement(t *testing.T) { t.Parallel() A := set.WithElements(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) B := set.WithElements(2, 4, 6, 8, 10) Odds := set.Complement(A, B) expectedOdds := set.WithElements(1, 3, 5, 7, 9) if !set.Equivalent(Odds, expectedOdds) { t.Fatalf("Expected %s\\%s to be %s, but got %s", A, B, expectedOdds, Odds) } }
// ComposableRelations indicates whether the list of relations can be // composed. That is to say whether they are defined over equivalent Universes. func ComposableRelations(relations []AbstractInterface) bool { if len(relations) == 0 { return true } u := relations[0].Universe() for _, b := range relations { if !set.Equivalent(u, b.Universe()) { return false } } return true }
func TestSetIntersection(t *testing.T) { t.Parallel() A := set.WithElements("one", "two") B := set.WithElements("two", "three") I := set.Intersection(A, B) if I.Cardinality() != 1 { t.Fatalf("Intersection of %s and %s should have cardinality 1", A, B) } expected := set.WithElements("two") if !set.Equivalent(I, expected) { t.Fatalf("Expected %s intersect %s to be %s, but got %s", A, B, expected, I) } }
// Compose takes two distributions, p and q, and creates a third lottery, // n which takes on each outcome in p with a probability alpha times that // events previous probability and each event in q with a probability 1-alpha // times that events previous probability. // // for o ∈ p.Domain() intersect q.Domain(); P(x in n) is alpha*P(x in p) + (1-alpha)P(x in q) func Compose(p, q DiscreteDistribution, alpha Probability) DiscreteDistribution { assert(FullySupported(p), "first distribution is not fully supported") assert(FullySupported(q), "second distribution is not fully supported") assert(set.Equivalent(p.Domain(), q.Domain()), "domains of both distributions must be equivalent") n := NewDiscreteDistribution(p.Domain()) for o := range n.Domain().Iter() { cp := alpha*p.ProbabilityOf(o) + (1-alpha)*q.ProbabilityOf(o) if cp == Impossible { continue // don't bother supporting } n.AddOutcome(o, cp) } return n }