MultiSet

Why have a multiset? A multiset is a collection that supports order-independent equality, like Set, but may have duplicate elements. A multiset is also sometimes called a bag.

In JavaTools a multiset can simply be created with JMultiSet[]. It then has to be filled with elements afterwards.

In[62]:=

"multiset_1.gif"

Out[62]=

"multiset_2.gif"

However, it's also possible to create a new multiset and supply initial values during creation:

In[63]:=

"multiset_3.gif"

Out[63]=

"multiset_4.gif"

JMultiSetSize[] returns the number of elements in the multiset, and JMultiSetIsEmpty[] shows if the multiset is empty:

In[64]:=

"multiset_5.gif"

Out[64]=

"multiset_6.gif"

In[65]:=

"multiset_7.gif"

Out[65]=

"multiset_8.gif"

A new element is added to the multiset with JMultiSetAdd[]:

In[66]:=

"multiset_9.gif"

Out[66]=

"multiset_10.gif"

The whole list of elements in the multiset can be shown with JSetToArray[]:

In[67]:=

"multiset_11.gif"

Out[67]=

"multiset_12.gif"

The type of elements that can be put in a multiset can be anything: Integer, Real, String, or even List:

In[68]:=

"multiset_13.gif"

Out[68]=

"multiset_14.gif"

In[69]:=

"multiset_15.gif"

Out[69]=

"multiset_16.gif"

In[70]:=

"multiset_17.gif"

Out[70]=

"multiset_18.gif"

In[71]:=

"multiset_19.gif"

Out[71]=

"multiset_20.gif"

In[72]:=

"multiset_21.gif"

Out[72]=

"multiset_22.gif"

In[73]:=

"multiset_23.gif"

Out[73]=

"multiset_24.gif"

Note that with a multiset it IS possible to add an element that is already in the multiset. That is the crucial difference between a set and a multiset: There can be no duplicates in the set!

In[74]:=

"multiset_25.gif"

Out[74]=

"multiset_26.gif"

In[75]:=

"multiset_27.gif"

Out[75]=

"multiset_28.gif"

JMultiSetAdd[] also can be used to add a specified number of occurrences of an element to the multiset It retunrs the count of the element BEFORE the operation, possibly 0:

In[76]:=

"multiset_29.gif"

Out[76]=

"multiset_30.gif"

In[77]:=

"multiset_31.gif"

Out[77]=

"multiset_32.gif"

In[78]:=

"multiset_33.gif"

Out[78]=

"multiset_34.gif"

In[79]:=

"multiset_35.gif"

Out[79]=

"multiset_36.gif"

With MultiSetAddAll[] we can add a whole list of elements to the multiset element-wise:

In[80]:=

"multiset_37.gif"

Out[80]=

"multiset_38.gif"

In[81]:=

"multiset_39.gif"

Out[81]=

"multiset_40.gif"

In[82]:=

"multiset_41.gif"

Out[82]=

"multiset_42.gif"

In[83]:=

"multiset_43.gif"

Out[83]=

"multiset_44.gif"

JMultiSetCount[] returns the number of occurences of that element in the multiset ("multiplicity").

In[84]:=

"multiset_45.gif"

Out[84]=

"multiset_46.gif"

In[85]:=

"multiset_47.gif"

Out[85]=

"multiset_48.gif"

In[86]:=

"multiset_49.gif"

Out[86]=

"multiset_50.gif"

In[87]:=

"multiset_51.gif"

Out[87]=

"multiset_52.gif"

JMultiSetElementSet[] returns the set of DISTINCT elements in the multiset (unlike JMultiSetToArray[], which returns all elements of the multiset):

In[88]:=

"multiset_53.gif"

Out[88]=

"multiset_54.gif"

With JSetRemove[]we can remove an element that is in the multiset:

In[89]:=

"multiset_55.gif"

Out[89]=

"multiset_56.gif"

In[90]:=

"multiset_57.gif"

Out[90]=

"multiset_58.gif"

With JSetRemove[]we can also remove a (sub)set of elements that occur in the multiset a specified number of times. JMultiSetRemove[] returns the number of occurrences BEFORE removal (which could possibly be zero).

In[91]:=

"multiset_59.gif"

Out[91]=

"multiset_60.gif"

In[92]:=

"multiset_61.gif"

Out[92]=

"multiset_62.gif"

In[93]:=

"multiset_63.gif"

Out[93]=

"multiset_64.gif"

In[94]:=

"multiset_65.gif"

Out[94]=

"multiset_66.gif"

With JMultiSetContains[] we can see if the multiset contains a particular element:

In[95]:=

"multiset_67.gif"

Out[95]=

"multiset_68.gif"

In[96]:=

"multiset_69.gif"

Out[96]=

"multiset_70.gif"

In[97]:=

"multiset_71.gif"

Out[97]=

"multiset_72.gif"

In[98]:=

"multiset_73.gif"

Out[98]=

"multiset_74.gif"

Note that JMultiSetAddAll[]adds all elements in the list to the multiset individually, whereas JSetAdd[] adds all elements in the list to the multiset as one list element:

In[99]:=

"multiset_75.gif"

Out[99]=

"multiset_76.gif"

In[100]:=

"multiset_77.gif"

Out[100]=

"multiset_78.gif"

In[101]:=

"multiset_79.gif"

Out[101]=

"multiset_80.gif"

JMultiSetSetCount[]lets you set the count (number of occurrences for a given element in the multiset. JMultiSetSetCount[]will add or remove as many elements as is necessary to attain the desired count. It returns the count of the element before the operation:

In[102]:=

"multiset_81.gif"

Out[102]=

"multiset_82.gif"

In[103]:=

"multiset_83.gif"

Out[103]=

"multiset_84.gif"

In[104]:=

"multiset_85.gif"

Out[104]=

"multiset_86.gif"

JMultiSetClear[] completely clears the entire multiset, returning an empty multiset and resetting its size to 0.

In[105]:=

"multiset_87.gif"

In[106]:=

"multiset_88.gif"

Out[106]=

"multiset_89.gif"

In[107]:=

"multiset_90.gif"

Out[107]=

"multiset_91.gif"

In[108]:=

"multiset_92.gif"

Out[108]=

"multiset_93.gif"

Spikey Created with Wolfram Mathematica 7.0