Subject: fundamental principles of counting - 2nd try
From: Wolfgang Lindner
Date: Fri, 6 Aug 2010 12:58:32 +0200
dear group,
coming back from a break I found no reaction to my query:
|But I don't find functions to list (not only count) all
|. arrangements of s (whose number is n^r )
||. combinations of s (whose number is C(n,r) )
|. variations of s (:= all combs with repetitons,
| there are C(n+r-1,r) many of them and we want to see them all)
Maybe I was not explicit enough ..
but I would like to do elementary discrete math with Maxima.
So I will elaborate a bit, give a partial answer
and ask for a solution to concrete question.
Q1: If S is a set, how to produce a sequence of k instances
of S e.g. to call cartesian_product(S,S,..S)?
makelist(S,i,1,k) don't work ..
(%i223) S : {1,2,3,4}$
S2 : cartesian_product (S, S);
Q2: do you see possibilities to 'optimise' my code
for the 4 cases of counting? see below
Q3: how to write a better version of Events_nOwR(n,k), see below?
HERE is my code.
Any hints are very welcome,
hth Wolfgang
------------------------------------------------------------
Fundamental principles of Counting with MAXIMA: (W. Lindner)
------------------------------------------------------------
1. LISTING OF ALL ELEMENTAY EVENTS WITH MAXIMA.
According to the attachment we have 4 cases.
case 1: nOnR (no order, no repetitions)
(%i269) Events_nOnR(n,r) := full_listify(powerset
(setify(makelist(i,i,1,n)), n-r))$
Events_nOnR(4,2);
(%o270) [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
case 2: wOwR (with order, with repetitions)
Problem: how to write function Events_wOwR(n,r),
e.g. how to produce a sequence of k instances of S
to call cartesian_product(S,S,..,S) ??
(%i223) S : {1,2,3,4}$
S2 : cartesian_product (S, S);
listify(S2);
cardinality(S2);
(%o224)
{[1,1],[1,2],[1,3],[1,4],[2,1],[2,2],[2,3],[2,4],[3,1],[3,2],[3,3],[3,4],[4,
1],[4,2],[4,3],[4,4]}
(%o225)
[[1,1],[1,2],[1,3],[1,4],[2,1],[2,2],[2,3],[2,4],[3,1],[3,2],[3,3],[3,4],[4,
1],[4,2],[4,3],[4,4]]
(%o226) 16
case 3: wOnR (with order, no repetitions)
problem: .. using result of case 2 ..
(%i273) Events_wOnR(n,r) := listify(subset (S2, lambda ([x], is(
cardinality(setify(x))=k))))$
Events_nOnR(4,2);
(%o274) [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
case 4: nOwR (no order, with repetitions)
we have 10 elementary events:
ElEr: 1 1 [1,1] (ElEr = elementary event)
1 2
1 3
1 4
2 2
..
3 4
4 4 -> length=10
(%i281) Events_nOwR (n,k) := block(
ElEr : makelist([i],i,1,n),
for j:2 thru k do (
ElErNew: [],
for m:1 thru length(ElEr) do
for r : ElEr[m][j-1] thru n do
ElErNew: append( ElErNew, [append(ElEr[m],[r])] ),
ElEr : ElErNew
),
sublist(ElEr, lambda ([x], x[1]));
(%i282) Events_nOwR(4,2);
(%o282) [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
2. URN-MODELLS:
Urnmodell nOwR, one draw (with repetions and no order)
Comment: 'sort' destroys the distinguish steps of the drawing!
(%i254) nOwR(n,r) := sort(makelist(random(n)+1, k,1,n-r))$
nOwR(4,2);
(%o248) [1,4]
Urnmodell nOnR, one draw:
(%i255) nOnR(n,r) := sort(rest (random_permutation (makelist(i,i,1,n)),
n-r))$
nOnR(4,2);
(%o248) [1,4]
Urnmodell wOwR, one draw:
(%i263) wOwR(n,r) := makelist(random(n)+1, k,1,r)$
wOwR(4,2);
(%o256) [2,3]
Urnmodell wOnR, one draw:
(%i265) wOnR(n,r) := rest (random_permutation (makelist(i,i,1,n)), n-r)$
wOnR(4,2);
(%o264) [3,1]