Message Boards Message Boards

GROUPS:

Does a Multisets[] function exist?

Posted 2 months ago
785 Views
|
6 Replies
|
6 Total Likes
|

With Permutations(), Subsets(), Tuples() i can build nice lists without further editing/programming, they're convenient to use. With an awkward combination of three functions i can build a Mma list which is a k-multiset. I can't find a Multisets() function in the Documentation and Google points at various Mma-related posts on the topic, so multisets are used by people, people need such a function. I can understand why n multichoose k is not implemented as function because it is easily calculated as Binomial(k+n-1,k). I also cannot load the Combinatorica` package into my 11.3 session.

So basically i am just wondering and asking. What do we know about the implementation of a multisets function in Mma, was it ever considered and then dropped (when, why)?

6 Replies
Posted 2 months ago

Have you checked out the function repository? There seems to be a few multiset functions there. Though, I do not know if they meet your need.

Great stuff there, thank you! None of these functions do what i am looking for but they are interesting on their own, i'll study them in more detail.

Looking at that page, it seems like a Multisets() function was not implemented in the original mma release. I use a combination of Map(), Sort(), DeleteDuplicate(), and Tuples() to build a multiset.

Hi Raspi, Are you using the desktop version? If so, you could create a package file containing definitions and functions, and then incorporate it into notebook files.

Here is an example notebook (.nb) of package definitions. Note the first line -- each time you save it, a package (.m) file will automatically be created. Note also that all definitions are in Initialization cells (Ctrl-8).

SetOptions[InputNotebook[], AutoGeneratedPackage -> Automatic]

SpatialDomainName := "Spatial";
AllelesFreqDomainName := "Alleles Frequency";
LociFreqDomainName := "Loci Frequency";
PopFreqDomainName := "Population Frequency";
HybridDomainName := "Spatial+Spectral";

stringScramble[string_] := 
  StringJoin[
   Sort[{RandomReal[], #} & /@ Characters[string]][[;; , 2]]];

To use the generated package, reference it from a .nb file:

Get["C:\\Users\\Authorized User\\Documents\\Mathematica\\community\\Example package.m"];

Print["alleles name = ", AllelesFreqDomainName];
Print["scrambled name = ", stringScramble[AllelesFreqDomainName]];

alleles name = Alleles Frequency
scrambled name = leqeleclFAusen ry
Posted 2 months ago

Hi Richard,

Is there a reason for using SetDelayed rather than Set in the expressions e.g.?

SpatialDomainName := "Spatial";

A more efficient implementation of stringScramble

stringScramble2[string_] := Characters[string] // RandomSample // StringJoin

w = StringJoin@RandomWord[100000];

stringScramble[w] // Timing // First
(* 2.03568 *)

stringScramble2[w] // Timing // First
(* 0.348845 *)

Needs["GeneralUtilities`"]

BenchmarkPlot[{stringScramble, stringScramble2}, StringTake[w, #] &, 
 10^Range[5], "IncludeFits" -> False]

enter image description here

Rohit, Thank you for the tips. I have been misunderstanding ':=' as 'define', and also was unaware of RandomSample[] - which I'm reading has the desired properties.

Several multiset functions are available in the Wolfram Function Repository: https://resources.wolframcloud.com/FunctionRepository/search/?i=multiset

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract