HP Forums
Symbolic Combinations and Permutations question. - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Symbolic Combinations and Permutations question. (/thread-2845.html)



Symbolic Combinations and Permutations question. - John P - 01-12-2015 07:38 PM

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


RE: Symbolic Combinations and Permutations question. - Helge Gabert - 01-13-2015 01:17 AM

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.


RE: Symbolic Combinations and Permutations question. - John P - 01-13-2015 02:09 AM

(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


RE: Symbolic Combinations and Permutations question. - Han - 01-13-2015 02:53 AM

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;



RE: Symbolic Combinations and Permutations question. - John P - 01-13-2015 03:57 AM

(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