SHARE
TWEET

Untitled

a guest Mar 18th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SetAttributes[set, {Flat, Orderless}]
  2.  
  3. set[args___] := Block[{union},
  4.   union = Union[{args}];
  5.   (
  6.     set @@ union
  7.   ) /; Length[union] != Length[{args}]
  8. ]
  9.      
  10. set /: Union[s___set] := set[s]
  11. set /: Element[e_, s_set] := MemberQ[s, e]
  12. set /: Subset[s___set] := VectorQ[Partition[Reverse[{s}], 2, 1], SubsetQ @@ # &]
  13. cardinality[s_set] := Length[s]
  14. cardinality[_] = 0;
  15.      
  16. set /: MakeBoxes[s : set[args__], fmt_] :=
  17.   MakeBoxes[Interpretation[〈args〉, s], fmt]
  18.  
  19. set /: MakeBoxes[set[], fmt_] := MakeBoxes[Interpretation["∅", set[]], fmt]
  20.      
  21. set[1, 1, 2, 2, 1, 3]
  22. (* 〈1, 2, 3〉 *)
  23.  
  24. set[1, 2, 3] ⋃ set[3, 4, 5] ⋃ set[4, 5, 6]
  25. (* 〈1, 2, 3, 4, 5, 6〉 *)
  26.  
  27. set[1, 2, 3] ⋂ set[3, 4, 5]
  28. (* 〈3〉 *)
  29.  
  30. set[1, 2, 3] ⋂ set[4, 5, 6]
  31. (* ∅ *)
  32.  
  33. set[1, 2, 3] ⋂ set[3, 4, 5] ⋃ set[4, 8]
  34. (* 〈3, 4, 8〉 *)
  35.  
  36. 5 ∈ set[1, 2, 3, 4, 5]
  37. (* True *)
  38.  
  39. set[1, 2, 3] ⊂ set[1, 2, 3, 4] ⊂ set[1, 2, 3, 4, 5]
  40. (* True *)
  41.  
  42. cardinality[set[1, 2, 3, 4, 5]]
  43. (* 5 *)
  44.      
  45. MakeBoxes[EmptySet, StandardForm] ^= TemplateBox[
  46.     {},
  47.     "EmptySet",
  48.     DisplayFunction -> ("[EmptySet]"&),
  49.     InterpretationFunction -> ("EmptySet"&)
  50. ];
  51. MakeBoxes[UniversalSet, StandardForm] ^= TemplateBox[
  52.     {},
  53.     "UniversalSet",
  54.     DisplayFunction -> (StyleBox["[DoubleStruckCapitalU]", FontFamily->"Times"]&),
  55.     InterpretationFunction -> ("UniversalSet"&)
  56. ];
  57.      
  58. CurrentValue[EvaluationNotebook[], InputAliases] = {
  59.     "su" -> "⊔",
  60.     "si" -> "⊓",
  61.     "sb" -> "⊏",
  62.     "es" -> TemplateBox[
  63.         {},
  64.         "EmptySet",
  65.         DisplayFunction -> ("[EmptySet]"&),
  66.         InterpretationFunction -> ("EmptySet"&)
  67.     ],
  68.     "us" -> TemplateBox[
  69.         {},
  70.         "UniversalSet",
  71.         DisplayFunction -> (StyleBox["[DoubleStruckCapitalU]", FontFamily->"Times"]&),
  72.         InterpretationFunction -> ("UniversalSet"&)
  73.     ]
  74. };
  75.      
  76. toBoolean[expr_] := ReplaceAll[
  77.     expr,
  78.     {
  79.         SquareUnion -> Or,
  80.         SquareIntersection -> And,
  81.         SquareSubset -> Implies,
  82.         Backslash -> (And[#1, !#2]&),
  83.         OverBar -> Not,
  84.         Equal -> Equivalent,
  85.         EmptySet -> False,
  86.         UniversalSet -> True
  87.     }
  88. ]
  89.      
  90. set = A ⊓ B ⊔ B;
  91.      
  92. toBoolean[set]
  93.      
  94. BooleanMinimize[toBoolean[set]]
  95.      
  96. set = A ⊓ B;
  97. cond = A ⊏ B;
  98.      
  99. BooleanMinimize[toBoolean[set], toBoolean[cond]]
  100.      
  101. setQ[EmptySet] = True;
  102. setQ[UniversalSet] = True;
  103. setQ[_Symbol] = True;
  104.  
  105. setQ[(Backslash | SquareUnion | SquareIntersection | OverBar)[a__]] := AllTrue[{a}, setQ]
  106.  
  107. setQ[_] = False;
  108.      
  109. setQ[A ⊔ B]
  110. setQ[A ⊏ B]
  111. setQ[A == B]
  112.      
  113. fromBoolean[expr_] := ReplaceAll[
  114.     expr,
  115.     {
  116.     Or -> SquareUnion,
  117.     And -> SquareIntersection,
  118.     Not -> OverBar,
  119.     Equivalent -> Equal
  120.     }
  121. ]
  122. fromBoolean[a_ && !b_Symbol] := Backslash[a, b]
  123. fromBoolean[!a_Symbol && b_] := Backslash[b, a]
  124. fromBoolean[a_ || !b_Symbol] := SquareSubset[b, a]
  125. fromBoolean[!a_Symbol || b_] := SquareSubset[a, b]
  126.      
  127. Options[SetSimplify] = {Method -> Automatic};
  128.  
  129. SetSimplify[set_, cond_:True, OptionsPattern[]] := Module[{res},
  130.     res = fromBoolean @ BooleanMinimize[
  131.         toBoolean[set],
  132.         toBoolean[cond],
  133.         Method->OptionValue[Method]
  134.     ];
  135.     If[setQ[set],
  136.         res /. {False -> EmptySet, True -> UniversalSet},
  137.         res
  138.     ]
  139. ]
  140.      
  141. SetSimplify[(A ⊓ B) ⊔ B]
  142. SetSimplify[A ⊓ B == A, A ⊏ B]
  143. SetSimplify[A ⊔ B == B, A ⊏ B]
  144.      
  145. set = A ⊓ UniversalSet;
  146. set //TeXForm
  147.      
  148. SetSimplify[set]
  149.      
  150. set = A ⊔ OverBar[A];
  151. set //TeXForm
  152.      
  153. SetSimplify[set] //TeXForm
  154.      
  155. SetSimplify[A ⊔ A]
  156. SetSimplify[A ⊓ A]
  157.      
  158. SetSimplify[A ⊔ (A ⊓ B)]
  159. SetSimplify[A ⊓ (A ⊔ B)]
  160.      
  161. law = OverBar[A ⊔ B] == OverBar[A] ⊓ OverBar[B];
  162. law //TeXForm
  163.      
  164. SetSimplify[law]
  165.      
  166. set = OverBar[EmptySet];
  167. set //TeXForm
  168.      
  169. SetSimplify[set] //TeXForm
  170.      
  171. SetSimplify[A ⊏ A]
  172.      
  173. SetSimplify[A == B, A ⊏ B && B ⊏ A]
  174.      
  175. SetSimplify[A ⊏ C, A ⊏ B && B ⊏ C]
  176.      
  177. SetSimplify[A ⊏ A ⊔ B]
  178. SetSimplify[A ⊔ B ⊏ C, A ⊏ C && B ⊏ C]
  179.      
  180. SetSimplify[A ⊓ B ⊏ A]
  181. SetSimplify[C ⊏ A ⊓ B, C ⊏ A && C ⊏ B]
  182.      
  183. SetSimplify[A ⊓ B == A, A ⊔ B == B]
  184.      
  185. SetSimplify[A ∖ B, A ⊏ B] //TeXForm
  186.      
  187. SetSimplify[A ∖ A] //TeXForm
  188.      
  189. set = UniversalSet ∖ A;
  190. set //TeXForm
  191.      
  192. SetSimplify[set] //TeXForm
  193.      
  194. MakeBoxes[EmptySet, form_] ^= TemplateBox[
  195.     {},
  196.     "EmptySet",
  197.     DisplayFunction -> ("[EmptySet]"&),
  198.     InterpretationFunction -> ("EmptySet"&)
  199. ];
  200. MakeBoxes[UniversalSet, form_] ^= TemplateBox[
  201.     {},
  202.     "UniversalSet",
  203.     DisplayFunction -> (StyleBox["[DoubleStruckCapitalU]", FontFamily->"Times"]&),
  204.     InterpretationFunction -> ("UniversalSet"&)
  205. ];
  206.  
  207. CurrentValue[EvaluationNotebook[], InputAliases] = {
  208.     "su" -> "⊔",
  209.     "si" -> "⊓",
  210.     "sb" -> "⊏",
  211.     "es" -> TemplateBox[
  212.         {},
  213.         "EmptySet",
  214.         DisplayFunction -> ("[EmptySet]"&),
  215.         InterpretationFunction -> ("EmptySet"&)
  216.     ],
  217.     "us" -> TemplateBox[
  218.         {},
  219.         "UniversalSet",
  220.         DisplayFunction -> (StyleBox["[DoubleStruckCapitalU]", FontFamily->"Times"]&),
  221.         InterpretationFunction -> ("UniversalSet"&)
  222.     ]
  223. };
  224.  
  225. toBoolean[expr_] := ReplaceAll[
  226.     expr,
  227.     {
  228.         SquareUnion -> Or,
  229.         SquareIntersection -> And,
  230.         SquareSubset -> Implies,
  231.         OverBar -> Not,
  232.         Backslash -> (And[#1, !#2]&),
  233.         Equal -> Equivalent,
  234.         EmptySet -> False,
  235.         UniversalSet -> True
  236.     }
  237. ]
  238.  
  239. setQ[EmptySet] = True;
  240. setQ[UniversalSet] = True;
  241. setQ[_Symbol] = True;
  242.  
  243. setQ[(Backslash | SquareUnion | SquareIntersection | OverBar)[a__]] := AllTrue[{a}, setQ]
  244.  
  245. setQ[_] = False;
  246.  
  247. fromBoolean[expr_] := ReplaceAll[
  248.     expr,
  249.     {
  250.     Or -> SquareUnion,
  251.     And -> SquareIntersection,
  252.     Not -> OverBar,
  253.     Equivalent -> Equal
  254.     }
  255. ]
  256. fromBoolean[a_ && !b_Symbol] := Backslash[a, b]
  257. fromBoolean[!a_Symbol && b_] := Backslash[b, a]
  258. fromBoolean[a_ || !b_Symbol] := SquareSubset[b, a]
  259. fromBoolean[!a_Symbol || b_] := SquareSubset[a, b]
  260.  
  261. Options[SetSimplify] = {Method -> Automatic};
  262.  
  263. SetSimplify[set_, cond_:True, OptionsPattern[]] := Module[{res},
  264.     res = fromBoolean @ BooleanMinimize[
  265.         toBoolean[set],
  266.         toBoolean[cond],
  267.         Method->OptionValue[Method]
  268.     ];
  269.     If[setQ[set],
  270.         res /. {False -> EmptySet, True -> UniversalSet},
  271.         res
  272.     ]
  273. ]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top