Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SetAttributes[set, {Flat, Orderless}]
- set[args___] := Block[{union},
- union = Union[{args}];
- (
- set @@ union
- ) /; Length[union] != Length[{args}]
- ]
- set /: Union[s___set] := set[s]
- set /: Element[e_, s_set] := MemberQ[s, e]
- set /: Subset[s___set] := VectorQ[Partition[Reverse[{s}], 2, 1], SubsetQ @@ # &]
- cardinality[s_set] := Length[s]
- cardinality[_] = 0;
- set /: MakeBoxes[s : set[args__], fmt_] :=
- MakeBoxes[Interpretation[〈args〉, s], fmt]
- set /: MakeBoxes[set[], fmt_] := MakeBoxes[Interpretation["∅", set[]], fmt]
- set[1, 1, 2, 2, 1, 3]
- (* 〈1, 2, 3〉 *)
- set[1, 2, 3] ⋃ set[3, 4, 5] ⋃ set[4, 5, 6]
- (* 〈1, 2, 3, 4, 5, 6〉 *)
- set[1, 2, 3] ⋂ set[3, 4, 5]
- (* 〈3〉 *)
- set[1, 2, 3] ⋂ set[4, 5, 6]
- (* ∅ *)
- set[1, 2, 3] ⋂ set[3, 4, 5] ⋃ set[4, 8]
- (* 〈3, 4, 8〉 *)
- 5 ∈ set[1, 2, 3, 4, 5]
- (* True *)
- set[1, 2, 3] ⊂ set[1, 2, 3, 4] ⊂ set[1, 2, 3, 4, 5]
- (* True *)
- cardinality[set[1, 2, 3, 4, 5]]
- (* 5 *)
- MakeBoxes[EmptySet, StandardForm] ^= TemplateBox[
- {},
- "EmptySet",
- DisplayFunction -> ("[EmptySet]"&),
- InterpretationFunction -> ("EmptySet"&)
- ];
- MakeBoxes[UniversalSet, StandardForm] ^= TemplateBox[
- {},
- "UniversalSet",
- DisplayFunction -> (StyleBox["[DoubleStruckCapitalU]", FontFamily->"Times"]&),
- InterpretationFunction -> ("UniversalSet"&)
- ];
- CurrentValue[EvaluationNotebook[], InputAliases] = {
- "su" -> "⊔",
- "si" -> "⊓",
- "sb" -> "⊏",
- "es" -> TemplateBox[
- {},
- "EmptySet",
- DisplayFunction -> ("[EmptySet]"&),
- InterpretationFunction -> ("EmptySet"&)
- ],
- "us" -> TemplateBox[
- {},
- "UniversalSet",
- DisplayFunction -> (StyleBox["[DoubleStruckCapitalU]", FontFamily->"Times"]&),
- InterpretationFunction -> ("UniversalSet"&)
- ]
- };
- toBoolean[expr_] := ReplaceAll[
- expr,
- {
- SquareUnion -> Or,
- SquareIntersection -> And,
- SquareSubset -> Implies,
- Backslash -> (And[#1, !#2]&),
- OverBar -> Not,
- Equal -> Equivalent,
- EmptySet -> False,
- UniversalSet -> True
- }
- ]
- set = A ⊓ B ⊔ B;
- toBoolean[set]
- BooleanMinimize[toBoolean[set]]
- set = A ⊓ B;
- cond = A ⊏ B;
- BooleanMinimize[toBoolean[set], toBoolean[cond]]
- setQ[EmptySet] = True;
- setQ[UniversalSet] = True;
- setQ[_Symbol] = True;
- setQ[(Backslash | SquareUnion | SquareIntersection | OverBar)[a__]] := AllTrue[{a}, setQ]
- setQ[_] = False;
- setQ[A ⊔ B]
- setQ[A ⊏ B]
- setQ[A == B]
- fromBoolean[expr_] := ReplaceAll[
- expr,
- {
- Or -> SquareUnion,
- And -> SquareIntersection,
- Not -> OverBar,
- Equivalent -> Equal
- }
- ]
- fromBoolean[a_ && !b_Symbol] := Backslash[a, b]
- fromBoolean[!a_Symbol && b_] := Backslash[b, a]
- fromBoolean[a_ || !b_Symbol] := SquareSubset[b, a]
- fromBoolean[!a_Symbol || b_] := SquareSubset[a, b]
- Options[SetSimplify] = {Method -> Automatic};
- SetSimplify[set_, cond_:True, OptionsPattern[]] := Module[{res},
- res = fromBoolean @ BooleanMinimize[
- toBoolean[set],
- toBoolean[cond],
- Method->OptionValue[Method]
- ];
- If[setQ[set],
- res /. {False -> EmptySet, True -> UniversalSet},
- res
- ]
- ]
- SetSimplify[(A ⊓ B) ⊔ B]
- SetSimplify[A ⊓ B == A, A ⊏ B]
- SetSimplify[A ⊔ B == B, A ⊏ B]
- set = A ⊓ UniversalSet;
- set //TeXForm
- SetSimplify[set]
- set = A ⊔ OverBar[A];
- set //TeXForm
- SetSimplify[set] //TeXForm
- SetSimplify[A ⊔ A]
- SetSimplify[A ⊓ A]
- SetSimplify[A ⊔ (A ⊓ B)]
- SetSimplify[A ⊓ (A ⊔ B)]
- law = OverBar[A ⊔ B] == OverBar[A] ⊓ OverBar[B];
- law //TeXForm
- SetSimplify[law]
- set = OverBar[EmptySet];
- set //TeXForm
- SetSimplify[set] //TeXForm
- SetSimplify[A ⊏ A]
- SetSimplify[A == B, A ⊏ B && B ⊏ A]
- SetSimplify[A ⊏ C, A ⊏ B && B ⊏ C]
- SetSimplify[A ⊏ A ⊔ B]
- SetSimplify[A ⊔ B ⊏ C, A ⊏ C && B ⊏ C]
- SetSimplify[A ⊓ B ⊏ A]
- SetSimplify[C ⊏ A ⊓ B, C ⊏ A && C ⊏ B]
- SetSimplify[A ⊓ B == A, A ⊔ B == B]
- SetSimplify[A ∖ B, A ⊏ B] //TeXForm
- SetSimplify[A ∖ A] //TeXForm
- set = UniversalSet ∖ A;
- set //TeXForm
- SetSimplify[set] //TeXForm
- MakeBoxes[EmptySet, form_] ^= TemplateBox[
- {},
- "EmptySet",
- DisplayFunction -> ("[EmptySet]"&),
- InterpretationFunction -> ("EmptySet"&)
- ];
- MakeBoxes[UniversalSet, form_] ^= TemplateBox[
- {},
- "UniversalSet",
- DisplayFunction -> (StyleBox["[DoubleStruckCapitalU]", FontFamily->"Times"]&),
- InterpretationFunction -> ("UniversalSet"&)
- ];
- CurrentValue[EvaluationNotebook[], InputAliases] = {
- "su" -> "⊔",
- "si" -> "⊓",
- "sb" -> "⊏",
- "es" -> TemplateBox[
- {},
- "EmptySet",
- DisplayFunction -> ("[EmptySet]"&),
- InterpretationFunction -> ("EmptySet"&)
- ],
- "us" -> TemplateBox[
- {},
- "UniversalSet",
- DisplayFunction -> (StyleBox["[DoubleStruckCapitalU]", FontFamily->"Times"]&),
- InterpretationFunction -> ("UniversalSet"&)
- ]
- };
- toBoolean[expr_] := ReplaceAll[
- expr,
- {
- SquareUnion -> Or,
- SquareIntersection -> And,
- SquareSubset -> Implies,
- OverBar -> Not,
- Backslash -> (And[#1, !#2]&),
- Equal -> Equivalent,
- EmptySet -> False,
- UniversalSet -> True
- }
- ]
- setQ[EmptySet] = True;
- setQ[UniversalSet] = True;
- setQ[_Symbol] = True;
- setQ[(Backslash | SquareUnion | SquareIntersection | OverBar)[a__]] := AllTrue[{a}, setQ]
- setQ[_] = False;
- fromBoolean[expr_] := ReplaceAll[
- expr,
- {
- Or -> SquareUnion,
- And -> SquareIntersection,
- Not -> OverBar,
- Equivalent -> Equal
- }
- ]
- fromBoolean[a_ && !b_Symbol] := Backslash[a, b]
- fromBoolean[!a_Symbol && b_] := Backslash[b, a]
- fromBoolean[a_ || !b_Symbol] := SquareSubset[b, a]
- fromBoolean[!a_Symbol || b_] := SquareSubset[a, b]
- Options[SetSimplify] = {Method -> Automatic};
- SetSimplify[set_, cond_:True, OptionsPattern[]] := Module[{res},
- res = fromBoolean @ BooleanMinimize[
- toBoolean[set],
- toBoolean[cond],
- Method->OptionValue[Method]
- ];
- If[setQ[set],
- res /. {False -> EmptySet, True -> UniversalSet},
- res
- ]
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement