Multinomial coefficient
02-15-2015, 11:59 AM (This post was last modified: 02-15-2015 05:49 PM by salvomic.)
Post: #1
 salvomic Senior Member Posts: 1,396 Joined: Jan 2015
Multinomial coefficient
hi all,
I would write a little program to calculate "multinomial coefficient" (see here), as I cannot find it in the Prime's command list...

So, I need to input n (total number of objects) and a list of groups items (k_i) which sum is n.
Any hints to control user's input?
For now I tried this wrong program:
Code:

EXPORT Multinomial()
BEGIN
local n, k, x, list, multi;
input ({n, {k}}, "Multinomial Coefficient", {"n", "k_i list"}, {"Total n", "input {k_i list} with comma"}, {0,0});

list := apply(x-> x!, k);
multi := n!/∏LIST(list);
return multi;

END;

but this code give "Error: Bad argument type". More: the user must insert {} in the input (i.e. {1,4,4,2}) for the list.

Any hints to give user the possibility to input list without brackets?

About that, generally speaking, how to insert in Windows with HP Connectivity Kit, ∏LIST(), ∑LIST, ∆LIST? Connectivity Kit should have a way to insert special characters easily...
Also with the arrow -> used in apply(), map() and others commands: I insert it via shift-9 list but I get syntax error: I'm wrong, why?...

Thank you for help!

Salvo

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
02-15-2015, 05:48 PM
Post: #2
 salvomic Senior Member Posts: 1,396 Joined: Jan 2015
RE: Multinomial coefficient
ok,
after some tries, I simplified all with this:
Code:

EXPORT Multinomial()
BEGIN
local n,k;
input ({n, {k}}, "Multinomial Coefficient", {"n", "k_i list"}, {"Total n", "input {k_i list} with comma"}, {0,0});
// we need integers (also for list)
n:=IP(n); k:=IP(k);

return n!/∏List(k!);
END;

This program accept an integer for "n" and a list with {} for the list of k (like {1,4,4,2}) and give a integer that represent permutations in a multi set...

There are some (ugly) problems:
1. a way to avoid user to input also the {} for the list (it would better to have only something like 1,4,4,2)
2. a control because the sum of k must be equal to n

Any hints are welcome!

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
02-15-2015, 07:44 PM
Post: #3
 parisse Senior Member Posts: 1,313 Joined: Dec 2013
RE: Multinomial coefficient
I would recommend not to use input, but pass arguments to your function.
FYI, there is a multinomial command in Xcas for probability, it has an additional 2nd argument, the list of probabilities. It would perhaps be interesting to include in the Prime.
02-15-2015, 08:20 PM
Post: #4
 salvomic Senior Member Posts: 1,396 Joined: Jan 2015
RE: Multinomial coefficient
(02-15-2015 07:44 PM)parisse Wrote:  I would recommend not to use input, but pass arguments to your function.
FYI, there is a multinomial command in Xcas for probability, it has an additional 2nd argument, the list of probabilities. It would perhaps be interesting to include in the Prime.

ok, I'll try to re-write the program without input, in effects...

Yes, it would be interesting to include multinomial in the Prime, I hope the Team would seriously consider the idea to do that!

About combinatorics, in the CAS there are also functions for combination with repetition and permutation with repetition?
I defined to function to have them in the Prime (simply -> comb(x+k-1,k) and x^k)...

I'm thinking also to write something to treat Stirling numbers.

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
02-15-2015, 09:25 PM (This post was last modified: 02-21-2015 11:17 AM by salvomic.)
Post: #5
 salvomic Senior Member Posts: 1,396 Joined: Jan 2015
RE: Multinomial coefficient
And then (thank you Parisse for hint), the version without input.
Use: multinom(n,{list of k_i})

Code:

#cas
multinom(args):=
// Multinomial by Salvo M. multinom(n,{list k_i})
BEGIN
local n, k, argv,argc;
argv:=[args];
argc:=size(argv);
IF argc !=2 THEN
return "Input: integer n, {list k}";
ELSE
n:=argv(1);
k:=argv(2);
IF (type(k) != DOM_LIST) THEN return "Second argument must be a list"; ELSE
// controllo interi
n:= ip(n); k:= ip(k);
return  n!/∏LIST(k!);
END; // if inner

END; // if out

END;
#end

The program has a control if user doesn't input 2 arguments (no more, no less), if the second argument is not a list and rounds both arguments, to get integers.

Caveat: with this version (#cas program) integer part command -ip()- must be lowercase or ip(k) doesn't works for the list giving wrong results (I wonder why...)