Showing posts with label matrix column sums. Show all posts
Showing posts with label matrix column sums. Show all posts

Saturday, July 9, 2016

HP 42S Programming Part I: Matrix Column Sum, GCD, Error Function

HP 42S Programming Part I:  Matrix Column Sum, GCD, Error Function

Continuing with calculators from the 1980s, this series will present one of the most beloved scientific calculators and one of the most sought after: the Hewlett Packard HP 42S from the late 1980s.  If you want one, you might want to save up because HP 42S calculators are valuable, and often cost over $100.

It is one of my most favorite calculators of all time. 

Free42

However, you can use an emulator for the HP 42S for free, check out the Free42 by Thomas Oakken.  It is a rewrite of the HP 42S code and the emulator is available on many platforms including Widows, Mac, iOS, and Android.  Link:  http://thomasokken.com/free42/   

Oakken accepts donations.





Part III hopefully to come next week.  

As usual, anything that comes after the double-backslash is a comment only.

HP 42S: Column Sums of a Matrix

This could potentially had been a medium to long sized programs involving loops, but thanks to the powerful function RSUM (found in CATALOG-FUNC or executed by XEQ RSUM) this program will be quite short.  RSUM is the row sum function, or the sum of each row of a matrix.

Just put a matrix on the stack and run CSUM.  When the program is finished, press [shift] [ 9 ] (MATRIX), {EDIT} to see the sums.

00 {12-Byte Prgm}
01 LBL “CSUM”
02 TRANS \\ transpose
03 RSUM
04 END

Test:  MAT1 = [ [4, 3, 1], [5, 8, 2], [7, 6, 3]]
Result:  [16, 17, 6]

HP 42S:  GCD (Greatest Common Divisor) by Euclid Division

Enter the two integers on the Y stack and X stack.  Order doesn’t matter.

00 {42-Byte Prgm}
01 LBL “GCD”
02 X<Y?
03 X<>Y
04 STO 01  \\ store maximum in R01
05 X<>Y
06 STO 00  \\ store minimum in R00
07 LBL 00 \\ main loop
08 RCL 01
09 ENTER
10 RCL÷ 00
11 IP
12 RCL* 00
13 –
14 X=0?  \\ is max – IP(min/max)*min = 0?
15 GTO 01
16 X<>Y
17 STO 01
18 X<>Y
19 STO 00
20 GTO 00
21 LBL 01 \\ display GCD
22 RCL 00
23 “GCD:”
24 ARCL ST X
25 AVIEW
26 END


Test:   Find the GCD of 485 and 175.
485 [ENTER] 175 [XEQ] {GCD}   Result:  5
Alternatively:
175 [ENTER] 485 [XEQ] {GCD}   Result:  5


HP 42S:  Error (erf) Function


ERF(X) = ∫ 2/√π * (e^(-T^2)) dT, from T = 0 to T = x

Integrand:
00 {22-Byte Prgm}
01 LBL “ERF”
02 MVAR “T”
03 RCL “T”
04 X^2
05 +/-
06 E^X
07 2
08 *
09 PI
10 SQRT
11 ÷
12 END

Instructions:

Press [(shift)] [ 8 ] (∫ f(x) )
Choose ERF at the “Select ∫f(x) Program”
Select T at Set Vars; Select ∫var
Store 0 in LLIM, X in ULIM, a suitable accuracy factor (10^-n) in ACC
Press the ∫ soft key.

Test: erf(1.2) with ACC of 1E-6  (LLIM = 0, ULIM = 1.2) 
Result:  0.9103


 This blog is property of Edward Shore, 2016.

Saturday, April 4, 2015

HP Prime and HP 50g: Matrices: Sum of the Rows, Columns, and Elements

HP Prime and HP 50g:  Matrices: Sum of the Rows, Columns, and Elements


Define the matrix M with R rows and C columns. 


HP Prime

Determine number of Rows and Columns:

L1* := SIZE(M)
R:=L1(1)   // number of rows
C:=L1(2)   // number of columns

*L1 or any list variable.

Sum of the Columns and Sum of all Elements

Sum of the Columns:   MAKEMAT(1,1,R)*M
Sum of all Elements:  result*MAKEMAT(1,C,1)

Sum of the Rows and Sum of all Elements

Sum of the Rows:  M*MAKEMAT(1,C,1)
Sum of all Elements:  TRN(result)*MAKEMAT(1,R,1)


HP 50g

Determine the number of Rows and Columns:

Number of Rows:  << M SIZE 1 GET ‘R’ STO >>
Number of Columns:  << M SIZE 2 GET ‘C’ STO >>

Sum of the Columns and Sum of all Elements

Sum of the Columns:  << M 1 C START 1 NEXT C ->ARRY * >>
Sum of all Elements:  << result   AXL ∑LIST >>

Sum of the Rows and Sum of all Elements

Sum of the Rows:  << M TRAN 1 R START 1 NEXT R ->ARRY * >>
Sum of all Elements:  << result AXL ∑LIST >>


Examples:

[[ 1, 2, 1  ] [ 2, 1, 1]]

Rows:  [[ 4 ] [ 4 ]]
Columns:  [[ 3, 3, 2 ]]
All Elements:  [[ 8 ]]


[[ 6, 0, 5 ] [ 0, -3, 0 ] [ 3, 2, 3 ]]

Rows: [[ 11 ] [ -3 ] [ 8 ]]
Columns: [[ 9, -1, 8 ]]
All Elements: [[ 16 ]]


* Eddie *


This blog is property of Edward Shore.  2015.

Thursday, February 26, 2015

Throwback Thursday: Programs for the HP 49g+/50g from 2003-2007

Throwback Thursday:  Programs for the HP 49g+/50g from 2003-2007

My first Hewlett Packard calculator was an HP 48G+, which I bought in 2000.  I really did not get serious into RPN and RPL until I bought an HP 49g+ in 2003.  Despite keyboard problems, the 49g+ was my go-to calculator.   The first 49g+ had a plus key that came loose, the second one I had trouble with the ON button.  Thankfully, by the time the HP 50g was released, the keyboard was reliable.  I bought my first 50g I think in 2003 – not sure.  It was the standard black 50g with orange and white text. Several years later, I ordered the blue 50g from Spain.  Recently, I traded the black 50g with a friend of mine (me getting a HP 42S in return). 

During 2003 to 2006 (I wish I noted the dates when I programmed these things back then), I made a library of programs that covered math, science, finance, and stats.  After finding them still on the SD card, today, I am going to list some of my favorite programs from that era. 

All the programs presented were done in User RPL and most of them, if not all of them, should be able to be used on any of the HP 48 series.  (no guarantees)

QCKPLOT – Simplified Plot Screen
(updated 2/19/2015)

QCKPLOT is probably my favorite out of the bunch.  QCKPLOT allows the user to plot a single function y(x) in a familiar graph set up screen input interface.  The plotting interface with the 49g+ and 50g took a while to get used to. 



QCKPLOT:
<< ‘X’ PURGE
“Quick Function Plot” 
{ { “y(x)=” “Enter y(x)” }
{ “xmin=” “x minimum” }
{ “xmax=” “x maximum” }
{ “ymin=” “y minimum” }
{ “ymax=” “y maximum” } }
{ 2 2 } { } { } INFORM
IF 0 == THEN KILL END
EVAL YRNG XRNG STEQ ‘X’ INDEP FUNCTION (0,0) AXES
ERASE DRAX DRAW PICT {#0d #0d} “Y=” EQ →STR + 1
→GROB GOR PICTURE >>

You can use RPN to build expressions.  However, everything for the window variables must evaluate to numbers.  For example, if you want to set xmax to 2π, use 2 π * →NUM [ENTER].  Which brings me to my next favorite…

PCTXT:  Puts text on the graph screen.

PCTXT inserts a text screen to any Cartesian point on the graph screen.  The program does not immediately show the graph screen upon completion, which allows for use as a subroutine.  If PCTXT is used a standalone program, press the left arrow button [ ← ] or hold [left shift] while pressing [ F3 ] to see the results. (I am assuming you are in RPN mode).

Input:
2:  coordinates
1:  string

PCTXT:
<< 1 →GROB PICT ROT C→PX ROT GXOR >>

ZETA41:  Zeta Function Approximation

The HP 49g+/50g does not have a built in Reimann Zeta function.  Using the standard definition of the zeta function takes forever.

Σ(I=1, infinity, 1/X^Z)

 What is presented here is a series that converges faster than the standard definition. The upper limit of 250 is arbitrary – you can increase the upper limit for better accuracy, but keep in mind it there is a cost in calculation time. The accuracy increases as X increases.

Input:
1:  X

ZETA41:
<< → X ‘∑(I=1,250,(-1)^(I+1)/I^X)/(1-2^(1-X))’ >>

FIB:  nth Fibonacci Number

FIB uses an explicit formula to calculate the nth Fibonacci number, rather than use a sequence loop. 

Input:
1:  N

FIB:
<< → N
<< 1 5 √ + 2 / N ^ 1 5 √ - 2 / N ^
- 5 √ / EVAL 0 RND >> >>

Test:  FIB(8) = 21,  FIB(12) = 144

CRDTRN Subroutine:  (Coordinate Transformation)

CRDTRN transforms a coordinate (x, y) to a new coordinate (x’, y’) with new center (xc, yc) and rotation angle θ.  CRDTRN is both a good standalone program and a useful subroutine.

Input:
5:  XOLD
4:  YOLD
3:  XCTR
2:  YCTR
1:  θ

CRDTRN
<< → XOLD YOLD XCTR YCTR θ
<< XOLD YOLD R→C XCTR YCTR R→C – θ DUP COS →NUM SWAP NEG SIN →NUM R→C * >> >>

Example:  Let the original coordinate be (1,2)  What would be the coordinates if the center was moved to (-1, 3) and the plane was rotated 60⁰ (π/3 radians)?

In Raidian Mode:
5:  1
4:  2
3:  -1
2:  3
1:  π/3

New coordinate:  (.13397456215, -2.23205080757)

QUADRES:  Quadratic Regression (uses ∑DAT)
(Update 2/19/2015)

QUADRES aims to fulfill a missing statistical regression for the HP 49g+/50g series:  quadratic regression.

Y = a*X^2 + b*X + c

The program uses the standard system statistics matrix ΣDAT.  The coefficients are returned in a matrix:

[[ c ]
[ b ]
[ a ]]

Example:

ΣDAT =
[[ 2, .23 ]
[ 3, .58 ]
[ 4, .96 ]
[ 8 1.85 ]
[ 16 3.66 ]]

Results:
[[ -.302930140195 ]
[ .304114954514 ]
[ -3.55628308881E-3 ]]

Or Y = -3.55628308881E-3*X^2 + .304114954514*X - .302930140195

QUADRES:
<< ∑DAT 1 COL- VANDERMONDE DUP SIZE OBJ→ DROP DROP 3 2 →LIST {1,1}
SWAP SUB LSQ >>

CSUM:  Sum of each of the matrix’s columns
RSUM:  Sum of each of the matrix’s rows

An example for CSUM and RSUM:

Matrix:
[[ 2, 3, -7 ]
[ 1, 8, 1 ]
[ -3, 4, 6 ]]

CSUM: [0, 15, 0]
RSUM: [-2, 10, 7]

Input:  Matrix

CSUM:
<< →COL → S
<< 1 S START AXL ∑LIST S ROLL NEXT
S →LIST AXL >> >>

RSUM:
<< →ROW → S
<< 1 S START AXL ∑LIST S ROLL NEXT
S →LIST AXL >> >>

LVEVAL:  Evaluate (simplify) every element in a list or vector

LVEVAL evaluates and simplifies each component of a given list or vector.  For example, LVEVAL turns a list consisting of:

{‘e^2’, ‘π/2’, ‘5.273*LOG(3.44)’} to:

In exact mode:
{‘e^2’, ‘π/2’, 2.82927266768}

Or in approximate mode:
{7.38905609893, 1.5707963268, 2.82927266768}

Input:
1:  list/vector

LVEVAL:
<< → LV
<< LV TYPE 5 IF ≠
THEN LV AXL ‘LV’ STO 1 SF END 1 LV SIZE
FOR I LV I GET EVAL LV I 3 ROLL PUT ‘LV’
STO NEXT IF 1 FS? THEN LV AXL ‘LV’ STO 1
CF END LV >> >>



Bezier Curve

This program draws a Bezier curve.  I honestly don’t quite remember what α0, β0, α1, and β1 represent – I think they are points that are not on the curve be affect the path of the curve. (X0, Y0) and (X1, Y1) are end points. 


« 'T' PURGE PARAMETRIC { T 0. 1. } INDEP "Bézier Curve" { "X0" "Y0" "α0" "ß0" "X1" "Y1" "α1" "ß1" } { 2. 4. } { } { } INFORM 0.
  IF ==
  THEN KILL
  END OBJ→ DROP → X0 Y0 α0 ß0 X1 Y1 α1 ß1
  « X0 X1 - 2. * α0 α1 + 3. * + 'T' 3. ^ * X1 X0 - 3. * α1 α0 2. * + 3. * - 'T' 2. ^ * + α0 3. * 'T' * + X0 + Y0 Y1 - 2. * ß0 ß1 + 3. * + 'T' 3. ^ * Y1 Y0 - 3. * ß1 ß0 2. * + 3. * - 'T' 2. ^ * + ß0 3. * 'T' * + Y0 + i * + 'XY1(T)' SWAP = DEFINE 'T' XY1 STEQ 'T' XY1 ERASE AUTO DRAX DRAW PICTURE  »  »


One Minute Marvels

The document “One Minute Marvels” by Richard Nelson and Wlodek Mier-Jedrzejowicz is a collection of short, very effective RPL programs for the HP 48 series.  Programs include list operations (including rotate a list’s elements, fill a list with zeroes, randomize a list), calendar functions (including day of the week, stopwatch programs), and math and utility programs (LCD, GCM, Ulad Principle, quick quadratic equation, and flag toggles just to name a few).  For owners of the HP 48 series, including HP 49g+/50g, I consider this document an excellent addition to your mathematics library.

Link to download “One Minute Marvels”:  http://www.hpcalc.org/details.php?id=1691


Cheers to the HP 48 series and my trusty blue HP 50g!

Eddie



This blog is property of Edward Shore – 2015.

Earth's Radius by Latitude

Earth's Radius by Latitude Introduction: Calculating the Earth’s Radius In quick, general calculations, we assume that the...