Is there an easy way to generate combination k from n in PARI/GP?


Peter

Suppose I have a vector with n elements in PARI/GP.

I want to generate combination k from n elements.

For example, if the vector is [3,7,11,14,18] and k=3, the output should be

[3,7,11]
[3,7,14]
[3,7,18]
[3,11,14]
[3,11,18]
[3,14,18]
[7,11,14]
[7,11,18]
[7,14,18]
[11,14,18]

Is there a command in PARI/GP to do this or do I have to program the function?

Piotr Semenov

Unfortunately, PARI doesn't have built-in commands to do all the work you need.

Functions forvec(with flag=2) are great for combinatorial problems. So your function can be as follows:

subsets(A, k) = {
   my (lst = List());
   forvec(v = vector(k, i, [1, #A]), listput(lst, vecextract(A, v)), 2);

   Vec(lst)
};

subsets([3,7,11,14,18], 3)
gp> [[3,7,11], [3,7,14], [3,7,18], [3,11,14], [3,11,18], [3,14,18], [7,11,14], [7,11,18],
     [7,14,18], [11,14,18]]

Related


Quick and easy way to generate tarballs from bazaar checkout

Claudio I need to generate a tarball from a project checked out from the bazaar repository. Since I'm not a normal bazaar user, can someone tell me if there is such a command and its syntax? Reimer Behrends Simple answer: bzr export /path/to/myproj.tar.gz Baz

Quick and easy way to generate tarballs from bazaar checkout

Claudio I need to generate a tarball from a project checked out from the bazaar repository. Since I'm not a normal bazaar user, can someone tell me if there is such a command and its syntax? Reimer Behrends Simple answer: bzr export /path/to/myproj.tar.gz Baz

Generate k permutations from n in C

Siddharth Chabra I basically need the equivalent result of the following Python command itertoolsin C : a = itertools.permutations(range(4),2)) Currently my approach consists of first "selecting" from 10 and then generating an array of 5 elements for those 5 e

Generate k permutations from n in C

Siddharth Chabra I basically need the equivalent result of the following Python command itertoolsin C : a = itertools.permutations(range(4),2)) Currently my approach consists of first "selecting" from 10 and then generating an array of 5 elements for those 5 e

Generate k permutations from n in C

Siddharth Chabra I basically need the equivalent result of the following Python command itertoolsin C : a = itertools.permutations(range(4),2)) Currently my approach consists of first "selecting" from 10 and then generating an array of 5 elements for those 5 e

Generate k permutations from n in C

Siddharth Chabra I basically need the equivalent result of the following Python command itertoolsin C : a = itertools.permutations(range(4),2)) Currently my approach consists of first "selecting" from 10 and then generating an array of 5 elements for those 5 e

Generate k permutations from n in C

Siddharth Chabra I basically need the equivalent result of the following Python command itertoolsin C : a = itertools.permutations(range(4),2)) Currently my approach consists of first "selecting" from 10 and then generating an array of 5 elements for those 5 e

Generate k permutations from n in C

Siddharth Chabra I basically need the equivalent result of the following Python command itertoolsin C : a = itertools.permutations(range(4),2)) Currently my approach consists of first "selecting" from 10 and then generating an array of 5 elements for those 5 e

Generate k permutations from n in C

Siddharth Chabra I basically need the equivalent result of the following Python command itertoolsin C : a = itertools.permutations(range(4),2)) Currently my approach consists of first "selecting" from 10 and then generating an array of 5 elements for those 5 e

Is there an easy way to generate sample FHIR resources?

Mishkin Is there an easy way to generate sample FHIR resources without using commercial tools? I'm working on a project where FHIR messages are stored to Elasticsearch, and I need a tool to generate FHIR sample messages in real time for delivery over TCP/IP. M

Is there an easy way to generate sample FHIR resources?

Mishkin Is there an easy way to generate sample FHIR resources without using commercial tools? I'm working on a project where FHIR messages are stored to Elasticsearch, and I need a tool to generate FHIR sample messages in real time for delivery over TCP/IP. M