It is time to close shop. With your help I have a practical and accurate J
model of finite set theory. Thank you for attention and contributions. /Kip
Examples are followed by the model. A "set" is 0$<1 or a non-empty list of
boxes without duplicates, and an "element" is the open of a box.
M =: pwrSet 0;1 NB. set of all subsets of set 0;1
N =: pwrSet 1;0
M ,: N
++---+---+-----+
||+-+|+-+|+-+-+|
|||1|||0|||0|1|| <-- set M
||+-+|+-+|+-+-+|
++---+---+-----+
||+-+|+-+|+-+-+|
|||0|||1|||1|0|| <-- set N
||+-+|+-+|+-+-+|
++---+---+-----+
M equalset N NB. i.e., equal as sets
1
(realset M),:(realset N) NB. The "real set" is "lexically" sorted
++---+-----+---+
||+-+|+-+-+|+-+|
|||0|||0|1|||1||
||+-+|+-+-+|+-+|
++---+-----+---+
||+-+|+-+-+|+-+|
|||0|||0|1|||1||
||+-+|+-+-+|+-+|
++---+-----+---+
S =: 1;2;3;4
T =: 6;5;4;3
S;T;<S u T
+---------+---------+-------------+
|+-+-+-+-+|+-+-+-+-+|+-+-+-+-+-+-+|
||1|2|3|4|||6|5|4|3|||1|2|6|5|4|3||
|+-+-+-+-+|+-+-+-+-+|+-+-+-+-+-+-+|
+---------+---------+-------------+
T;S;<T u S
+---------+---------+-------------+
|+-+-+-+-+|+-+-+-+-+|+-+-+-+-+-+-+|
||6|5|4|3|||1|2|3|4|||6|5|1|2|3|4||
|+-+-+-+-+|+-+-+-+-+|+-+-+-+-+-+-+|
+---------+---------+-------------+
(S u T) equalset T u S
1
N ,: realset N NB. set realset N is recursive "lexical" sort of set N
++---+-----+-----+
||+-+|+-+ |+-+-+|
|||0|||1| ||1|0|| <-- set N
||+-+|+-+ |+-+-+|
++---+-----+-----+
||+-+|+-+-+|+-+ |
|||0|||0|1|||1| | <-- set realset N, see recursive sort
||+-+|+-+-+|+-+ |
++---+-----+-----+
]e =: ,<0 NB. a non-empty list of boxes with no duplicates
+-+
|0|
+-+
e iselementof N NB. remember N is set of all subsets of set 1;0
1
]E =: 0$<1 NB. empty set
E iselementof N
1
The model:
NB. "Real sets" 12 July 2009
NB. A "set" is 0$<1 or a non-empty list of boxes with no duplicates.
NB. An "element" is the open of a box. As 0$<1 has no box it has no element.
NB. M =: set element;element;...;element creates a set with those elements.
NB. Use M =: set element;...;element;<element if the rightmost element is boxed.
NB. If you are CAREFUL TO HAVE NO DUPLICATES you can use
NB. M =: element;element;...;element when rightmost element is open
NB. M =: element;...;element;<element when rightmost element is boxed
NB. Verb set =: ~. is defined below.
load 'validate'
set =: ~. NB. creates a non-empty set when used as shown above
isset =: -:&(0$<1) +. isboxed *. isvector *. (-: ~.)
NB. 0$<1 or boxed non-empty list without duplicates
isrealset =: -: realset NB. see verb realset below
Empty =: 0$<1 NB. empty set
E =: Empty
sortset =: ( ] : [: )`( /:~ : [: )@.( (isset f.) *. -.@((0$<1) -: ]) )
NB. If argument is a set but not 0$<1 , sortset sorts the argument;
NB. sortset returns 0$<1 and nonsets unchanged.
realset =: 3 : 0 NB. recursive sortset; almost Fraser's sortSet
s =. y
for_k. i. >: L. y do. s =. sortset L:k s end.
)
NB. usage realset set produces the equivalent "real set":
NB. recursively "sortsetted" for easy comparison with other "real sets".
NB. This program of Fraser's is a real break-through.
sortSet =: 3 : 0 NB. Fraser's; the source of realset
s =. y
for_k. i. >: L. y do. s =. Set L:k s end.
)
equalset =: -:&realset NB. i.e., equal as sets
eqs =: equalset
iselementof =: <@[ e. ]
in =: iselementof
u =: -. , ] NB. union, R. E. Boss and Oleg Kobchenko
less =: -. NB. difference
n =: [ -. -. NB. intersection, R. E. Boss and Oleg Kobchenko
issubsetof =: [ equalset n
part =: issubsetof
NB. pwrSet below is Raul's powSet without the sort
pwrSet =: (<@#~) 2 (#"1~ {:)@#:@i...@^ #
NB. This program of Raul's is another mile-stone.
NB. powSet=: /:~@:(<@#~) 2 (#"1~ {:)@#:@i...@^ # NB. Raul's; the source of
pwrSet
NB. Commented out to avoid confusion with pwrSet
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm