The Museum of HP Calculators

HP Forum Archive 19

[ Return to Index | Top of Index ]

i41CX+ gets another plus (CAS with editor) [micro review]
Message #1 Posted by Egan Ford on 14 Mar 2010, 3:36 p.m.

i41CX+ already has a lot of pluses, e.g.:

  • Direct HW access (e.g. GPS, compass, accelerometer, etc...)
  • Printer emulation
  • Overlays and skins
  • Module support including HEPAX
  • Many ways to get data in and out
  • ARM-based math module with advanced functions (e.g. LnGamma, LambertW, etc...)
  • 5 field stack display
And soon, with the next release, CAS and a text editor.

I am currently beta testing the next release of i41CX+ and I thought I'd share with you some of the new CAS and editor features.

The Editor

There is a new command (I41CXED) that when executed will edit and/or create a text file as defined in the alpha register. The text file unless too large will be saved into XM. Any XM text file can be edited. If the file is too large then it will be saved (and can be retrieved) from XXM (extended extended memory :-). IOW, the iPhone main flash memory. AFAIK, only the I41CXED and CAS commands can actually access XXM files.

The editor supports two fonts (monospace and not monospace) with variable sizes and colors. The colors mimic the 41CX LCD, but you can toggle to black on white. Your last font, font size, and color are retained between sessions. Tapping the name of the file will drop down the keyboard.

The alternative would be to email (cut and paste) or download text files created elsewhere (also supported by i41CX+). Having the editor built-in is much nicer since the iPhone still does not allow the multitasking of 3rd party apps. I like it so much that Sigma+ is now I41CXED.

CAS

The CAS is based on REDUCE--"A portable general-purpose computer algebra system" (http://reduce-algebra.sourceforge.net/). The version of REDUCE used in i41CX+ is optimized for embedded systems (http://reduce-algebra.svn.sourceforge.net/viewvc/reduce-algebra/branches/asau-bsd-port/csl/embedded/). I use the embedded version on my MacBook to quickly prototype REDUCE programs for i41CX+. The documentation for both versions is the same and can be obtained from the first URL. (I have a PDF of the docs for any that are interested).

REDUCE is a worksheet style CAS environment similar to Mathematica, Sage, etc... Basically, you enter some expressions, statements, programs, etc... execute CAS and then view the output, e.g.:

Unless specified the output will be appended to the input. Most useful for a quick CAS session. Unlike Mathematica, Sage, and many others the input and output on the worksheet is not separated. Because of this a "re-CAS" would produce the same initial results, but then could produce a lot of errors as it parses the old output. The solution for this is to specify two files in the alpha register, e.g. FOO,BAR. Where FOO would be the input file and BAR the output file. When the editor is invoked it will stop parsing the alpha register when either a comma or end of line is encountered. If the output file does not exist then CAS will create it with the output of the input file. If the output file exists then the output will be appended. If the output file is not specified but a comma still terminates the line (e.g. FOO,), then the output is sent to /dev/null, IOW there will be no output. There is actually a use for no output when writing programs that directly manipulate the 41CX registers (more on this later).

Given all the CAS programs available for the iPhone, including HP 49g emulation in m48+, you may be asking yourself, "why?"

There are a few distinct advantages of having this CAS built into i41CX+:

  1. You can write an RPN program to programmatically create a CAS input file, execute CAS, then parse the output. Large matrix operations perhaps?
  2. You can write powerful CAS programs that can use any 41CX register as input and/or output.
  3. Because it's CAS in a 41CX. :-)

CAS Program Examples

REDUCE programs are not much different that other structured programming languages (smells a bit like Pascal). What is unique with REDUCE for i41CX+ is that you can get and put data to any RPN register. The following table illustrates this relationship:

41CX          REDUCE
---------     -------------------------
X,Y,Z,T,L     x_reg, y_reg, etc...
ALPHA         a_reg
R000-Rnnn     data_reg(0)-data_reg(nnn)

Some of the following examples have been submitted for inclusion into the on-board i41CX+ examples.

FACTOR:

define x=x_reg;
p:=factorize(fix(x));
x:=0;
for i:=1:length(p) do <<
 q:=part(p,i);
 for j:=1:part(q,2) do
  data_reg(x:=x+1):=part(q,1);
>>;
FACTOR will store all the prime factors of an integer into the 41CX registers starting with R001 with the number of factors stored in X. The fix function converts any non-integer input to integer--input conditioning as apposed to proper error handling. If the number of registers required is not available the CAS system will exit with a DATA ERROR message. I am unsure if a flag is set on error.

FACTOR can be easily modified to only return distinct primes by replacing this block:

for i:=1:length(p) do <<
 q:=part(p,i);
 for j:=1:part(q,2) do
  data_reg(x:=x+1):=part(q,1);
>>;
with:
for i:=1:length(p) do
 data_reg(x:=x+1):=first part(p,i);
FACTOR relies on the CAS factorize function. factorize returns a list of pairs (lists), e.g. {{2,2},{5,2}}. The part function is one way to manipulate lists.

The define statement can be use to assign an alias. If you find your code using a lot of *_reg variables you can reduce your byte count by redefining some of the *_reg variables. This strategy was important with the early CAS betas, but now that XXM is supported it is less important.

REDUCE can use << >> or begin end to delimit blocks.

DIVIS:

define x=x_reg;
define r=data_reg;
r(0):=0;
procedure err(a); <<
 a_reg:=a;
 r(0):=-1;
 update_regs_and_quit();
>>;
if not fixp x then
 err """NOT INTEGER""";
if x<1 then err """X < 1""";
p:=factorize(x);
q:={1};
if x=1 then p:={};
while p neq {} do <<
 t:=first p;
 s:=first t;
 p:=rest p;
 l:=length q;
 for i:=1:second t do <<
  k:=s^i;
  for j:=1:l do
   q:=append(q,{part(q,j)*k});
 >>;
>>;
x:=length q;
if x>part(length(r),1)-1 then
 err """OUT OF REGS""";
for i:=1:x do r(i):=part(q,i);

DIVIS will return all the divisors of an integer into registers R001-Rnumber-of-divisors with the number of divisors stored in X unless the initial X is not an integer or less than 1. In that case X is left on the stack and an error message returned to the ALPHA register. If the number of registers required is less than the number of divisors then DIVIS will exit with an error, however X will contain the number of divisors (and therefore the number of registers needed (remember to add one for R000)).

DIVIS does a better job of error handling than FACTOR. To do this I defined a procedure (err) to accept an encapsulated string as an argument that then stores that to the ALPHA register as well as stores a -1 to R000 (the default is R000=0). The last statement update_regs_and_quit() updates the registers then exits. If quit or bye had been used then none of the *_reg variables would have been returned to the 41CX environment.

Below is an RPN wrapper around DIVIS (also called DIVIS). R000 is used as a return code; 0 for OK, and -1 for error. If error, then AVIEW, otherwise print the divisors.

Example output of 90 DIVIS:

If you have a 49/50 handy you will notice both output in the same order (I'll assume they use the same basic algorithm). To address the order issue I created a small straight insertion sort program.

SORT:

define r=data_reg;
for j:=2:x_reg do <<
 a:=r(j);
 k:=j-1;
 while k>0 and r(k)>a do <<
  r(k+1):=r(k);
  k:=k-1;
 >>;
 r(k+1):=a;
>>;
All that is needed is to call this sort in my DIVIS RPN program. Since the X register is left with the number of divisors and the sort program is expecting X to have the last register used for sorting then all that is needed is:
SORT,
CAS
New RPN DIVIS:

New output of 90 DIVIS:

Easy!

Summary

A 41CX with CAS? What's next? Graphics? :-)

I am not an expert with REDUCE. I have only quickly scanned the documentation. I cannot tell you if REDUCE is the most complete or best CAS for the iPhone. Without a complete survey its hard to say. However, I've tried just about every iPhone CAS. For programming, flexibility, and fun, this has to be my favorite (CAS is now assigned to Sigma-).

Edited: uploaded images locally

Edited: 15 Mar 2010, 11:20 a.m.

      
Re: i41CX+ gets another plus (CAS with editor) [micro review]
Message #2 Posted by hugh steers on 15 Mar 2010, 1:17 p.m.,
in response to message #1 by Egan Ford

Looks cool, is there a PC or non-iphone version. i dont have an iphone, but i'd like to try it.

thanks,

            
Re: i41CX+ gets another plus (CAS with editor) [micro review]
Message #3 Posted by Egan Ford on 15 Mar 2010, 1:35 p.m.,
in response to message #2 by hugh steers

There is the Windows PC version of REDUCE here: http://reduce-algebra.sourceforge.net/.

      
Re: i41CX+ gets another plus (CAS with editor) [micro review]
Message #4 Posted by Timo on 15 Mar 2010, 2:02 p.m.,
in response to message #1 by Egan Ford

Very nice work, congratulations!

I already have the emulator on my ipod Touch. I'm really looking forward to the this release...

      
Re: i41CX+ gets another plus (CAS with editor) [micro review]
Message #5 Posted by Eddie W. Shore on 18 Mar 2010, 12:05 a.m.,
in response to message #1 by Egan Ford

Great job, Egan.


[ Return to Index | Top of Index ]

Go back to the main exhibit hall