Symbolic Combinations and Permutations question.
01-12-2015, 07:38 PM
Post: #1
 John P Member Posts: 191 Joined: Dec 2013
Symbolic Combinations and Permutations question.
Hello everybody,
Is there a function in xCAS or HPPL that outputs combinations (permutations) of all elements in a list. For example: when I have list {1,2,3} and I want combs. of all elements taken two at a time the functions outputs list {{1,2},{1,3},{2,3}} as combinations.
Cheers
01-13-2015, 01:17 AM
Post: #2
 Helge Gabert Senior Member Posts: 460 Joined: Dec 2013
RE: Symbolic Combinations and Permutations question.
I don't know of any one built-in command that would do that, but the Prime, being fully programmable, allows you do something along these lines

EXPORT YOURNAME(n)
BEGIN
LOCAL a,l:={},ll:={},j:=1,k:=1;
a:=SIZE(n);
FOR j FROM 1 TO a-1 DO
FOR k FROM j+1 TO a DO
l:={{n(j),n(k)}};
ll:=CONCAT(ll,l);
END;
END;
RETURN ll;
END;

This will give you your desired pairs of combinations, and the code can be extended to return triplets, quadruplets, etc.
01-13-2015, 02:09 AM
Post: #3
 John P Member Posts: 191 Joined: Dec 2013
RE: Symbolic Combinations and Permutations question.
(01-13-2015 01:17 AM)Helge Gabert Wrote:  I don't know of any one built-in command that would do that, but the Prime, being fully programmable, allows you do something along these lines

EXPORT YOURNAME(n)
BEGIN
LOCAL a,l:={},ll:={},j:=1,k:=1;
a:=SIZE(n);
FOR j FROM 1 TO a-1 DO
FOR k FROM j+1 TO a DO
l:={{n(j),n(k)}};
ll:=CONCAT(ll,l);
END;
END;
RETURN ll;
END;

This will give you your desired pairs of combinations, and the code can be extended to return triplets, quadruplets, etc.

Thank you Helge Gabert,

I will modify it to work as nchoosek i matlab so you can give two arguments; list of elements an the number elements to choose.

Cheers
01-13-2015, 02:53 AM (This post was last modified: 01-13-2015 03:53 AM by Han.)
Post: #4 Han Senior Member Posts: 1,811 Joined: Dec 2013
RE: Symbolic Combinations and Permutations question.
Here's my attempt at the general version via recursion. Usage:

COMBOSET(list,n)

Works for arbitrary sets, too. For example, try: COMBOSET({"a","b",1,[0,-1,0]},2)

Code:
EXPORT COMBOSET(s,n) BEGIN   local l:={};   local sl:={};   local tl:={};   local i,j;   local m:=size(s);   case     if n==m then l:={s}; end;     if n==1 then l:=makelist({s(X)},X,1,m); end;      if n>m then l:={}; end;     for i from 2 to m-n+2 do       sl:=sub(s,i,m);        tl:=COMBOSET(sl,n-1);       for j from 1 to size(tl) do         tl(j):=concat({s(i-1)},tl(j));       end;       l:=concat(l,tl);      end;   end;   return(l); END;

Graph 3D | QPI | SolveSys
01-13-2015, 03:57 AM
Post: #5
 John P Member Posts: 191 Joined: Dec 2013
RE: Symbolic Combinations and Permutations question.
(01-13-2015 02:53 AM)Han Wrote:  Here's my attempt at the general version via recursion. Usage:

COMBOSET(list,n)

Works for arbitrary sets, too. For example, try: COMBOSET({"a","b",1,[0,-1,0]},2)

Code:
EXPORT COMBOSET(s,n) BEGIN   local l:={};   local sl:={};   local tl:={};   local i,j;   local m:=size(s);   case     if n==m then l:={s}; end;     if n==1 then l:=makelist({s(X)},X,1,m); end;      if n>m then l:={}; end;     for i from 2 to m-n+2 do       sl:=sub(s,i,m);        tl:=COMBOSET(sl,n-1);       for j from 1 to size(tl) do         tl(j):=concat({s(i-1)},tl(j));       end;       l:=concat(l,tl);      end;   end; END;

Hello Han,

Thank you. I do not think I could make i better. I've written similar program for HP50 and recently was thinking to rewrite it for HP Prime. I was thinking about your resent post about solving system of eqs. You described case were there was more eqs. than unknowns and fsolve was giving some kind of error. If I remember correctly from linear algebra system like that is called overdetermined and theoretically could have only one solution but mostly has number of solutions for different combinations of eqs. for example if you have 3 eqs. and 2 unknowns you would have, in general, 3 solutions: sol. for eq1 and eq2, sol. for eq1 and eq3 and sol. for eq2 and eq3 (combinations of eqs.). but I have to find the book and do some reviewing. Thanks once again.

Cheers
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)