Post Reply 
Recursive programs in XCAS.
02-12-2017, 12:22 AM (This post was last modified: 02-12-2017 12:24 AM by John P.)
Post: #1
Recursive programs in XCAS.
Hello,
Can I write recursive programs in XCAS? I know that I can do recursive programs in HP PRIME, but I am trying to write some in XCAS and they do not work.
Thank you,
John P
Find all posts by this user
Quote this message in a reply
02-12-2017, 01:41 AM (This post was last modified: 02-12-2017 01:43 AM by Han.)
Post: #2
RE: Recursive programs in XCAS.
Can you provide a bit more information on what you have tried? Perhaps a snippet of code might provide a better idea of what you are trying to achieve. Or even a better description of what you wan to do beyond a generic description of "recursive program."

Fibonnaci example
Code:

fibon(n):=
begin
  if (n == 1) then return(1); end;
  if (n == 2) then return(1); end;
  if (n > 2) then
    return( fibon(n-1) + fibon(n-2) );  
  end;
end;

In general, any CAS code on the HP Prime should port as easily as copy/paste -- WITH EXCEPTIONS FOR A FEW COMMANDS (e.g. idenmat() on xcas is identity() )

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
02-12-2017, 02:08 AM
Post: #3
RE: Recursive programs in XCAS.
(02-12-2017 01:41 AM)Han Wrote:  Can you provide a bit more information on what you have tried? Perhaps a snippet of code might provide a better idea of what you are trying to achieve. Or even a better description of what you wan to do beyond a generic description of "recursive program."

Fibonnaci example
Code:

fibon(n):=
begin
  if (n == 1) then return(1); end;
  if (n == 2) then return(1); end;
  if (n > 2) then
    return( fibon(n-1) + fibon(n-2) );  
  end;
end;

In general, any CAS code on the HP Prime should port as easily as copy/paste -- WITH EXCEPTIONS FOR A FEW COMMANDS (e.g. idenmat() on xcas is identity() )

Actually, I am trying to port the program, that you provided some time ago for HP Prime, to the PocketCAS on my iPhone 6+. On the HP Prime it works great, but on the iPhone 6+ I can't get it to work. Below I paste your original prgm. for the computation of the set of combinations from the set (list).

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;
Find all posts by this user
Quote this message in a reply
02-12-2017, 02:32 AM (This post was last modified: 02-12-2017 02:33 AM by John P.)
Post: #4
RE: Recursive programs in XCAS.
(02-12-2017 01:41 AM)Han Wrote:  Can you provide a bit more information on what you have tried? Perhaps a snippet of code might provide a better idea of what you are trying to achieve. Or even a better description of what you wan to do beyond a generic description of "recursive program."

Fibonnaci example
Code:

fibon(n):=
begin
  if (n == 1) then return(1); end;
  if (n == 2) then return(1); end;
  if (n > 2) then
    return( fibon(n-1) + fibon(n-2) );  
  end;
end;

In general, any CAS code on the HP Prime should port as easily as copy/paste -- WITH EXCEPTIONS FOR A FEW COMMANDS (e.g. idenmat() on xcas is identity() )

Yes, the example of fibon() you provided works on my iPhone 6+ so, recursive programs are possible in PocketCAS. I must be making some mistake(s) in porting the program.
Thank you very much.
John P
Find all posts by this user
Quote this message in a reply
02-12-2017, 02:52 AM
Post: #5
RE: Recursive programs in XCAS.
First, please understand that non-CAS programs do not behave the same way as CAS programs. The combo set program is a non-CAS program. So commands like makelist() are actually two different commands:

In "Home" programs (i.e. non-CAS programs), upper/lower case does not matter. So makelist() is actually parsed as MAKELIST(), which is a different command from the CAS makelist(). In CAS programs, upper/lower case DOES matter.

The CAS version of makelist() is:

makelist( cas-function, var, start, end )

where cas-function is an actual function. For example, when you define a function f(x):=x^2 in the CAS view, you are actually creating the variable f whose content is the CAS-object (x)->x^2. The point here is that f(x):=x^2 is the same as f:=(x)->x^2. So it is this type of function (the right hand side being an example) that is the first argument in makelist(). In your case,

makelist( (X)->s[X], X, 1, m )

is what you would want. Notice the s[X] as opposed to s(X) -- this is to remove any ambiguity in the CAS side whether s is a function vs. an indexed object (i.e. list).

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
02-12-2017, 04:06 AM (This post was last modified: 02-12-2017 04:07 AM by Han.)
Post: #6
RE: Recursive programs in XCAS.
Just for fun, create the program below. Then in the CAS view, type:

purge(x);
f:=x^2-3*x+4;
CRPN(f);

Recursive Program Example:

Code:
#pragma mode( separator(.,;) integer(h32) )
#cas
CRPN(f):=
begin
  local rpn, op, par, n, j, g;

  rpn:="";
  op:="";
  par:="";
  if (type(f) <> DOM_SYMBOLIC) then
    rpn:=string(f);
    return(rpn);
  end;

  op:=string(f[1]);
  n:=size(op);
  op:=MID(op,2,n-2);
  n:=dim(f) + 1;

  for j from 2 to n do
    g:=f[j];
    if (type(g) <> DOM_SYMBOLIC) then
      par:=string(g);
    else
      par:=CRPN(g);
    end;
    if (j > 2) then rpn:=rpn + " "; end;
    rpn:=rpn + par;
    if ((j > 2) or (n == 2)) then
      rpn:=rpn + " " + op;
    end;
  end;

  return(rpn);
end;
#end

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
03-01-2017, 04:46 AM (This post was last modified: 03-01-2017 05:28 AM by Han.)
Post: #7
RE: Recursive programs in XCAS.
Here's a CAS version that works on the Prime. Notice the upper-case MAKELIST command (which is different from the lower-case makelist command). MAKELIST is a non-CAS command. This was a quick conversion from non-CAS to CAS (which does not support CASE as far as I can tell; it does have CASES, though, which seems to be the same as piecewise). You can make the code much cleaner using several return() calls.

PHP Code:
#cas
comboset(s,n):=
BEGIN
local l
;
local sl;
local tl;
local k;
local j;
local m;
local t;

m:=size(s);
l:={};
sl:={};
tl:={};

if 
n==m then l:={s}; else
  if 
n==1 then l:=MAKELIST({s(X)},X,1,m); else 
    if 
n>m then l:={}; else
      for 
k from 2 to m-n+do
        
sl:=sub(s,k,m); 
        
tl:=comboset(sl,n-1);
        for 
j from 1 to size(tl) do
          
tl[j]:=concat({s[k-1]},tl[j]);
        
end;
        
l:=concat(l,tl); 
      
end;
    
end;
  
end;
end;

return(
l);

END;
#end 

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
03-01-2017, 04:49 AM (This post was last modified: 03-01-2017 05:12 AM by Han.)
Post: #8
RE: Recursive programs in XCAS.
Slightly more readable version.

EDIT: Also removed MAKELIST() call since PocketCAS probably does not have that version of the command.

PHP Code:
#cas
comboset(s,n):=
BEGIN
local l
;
local sl;
local tl;
local k;
local j;
local m;
local t;

m:=size(s);
l:={};
sl:={};
tl:={};

if 
n==m then l:={s}; return(l); end;
if 
n==1 then
    
for k from 1 to m do
        
l[k]:={s[k]};
    
end;
    return(
l);
end;
if 
n>m then l:={}; return(l); end;

for 
k from 2 to m-n+do
    
sl:=sub(s,k,m); 
    
tl:=comboset(sl,n-1);
    for 
j from 1 to size(tl) do
        
tl[j]:=concat({s[k-1]},tl[j]);
    
end;
    
l:=concat(l,tl); 
end;

return(
l);

END;
#end 

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
Post Reply 




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