Showing posts with label HP 15C Limited Edition. Show all posts
Showing posts with label HP 15C Limited Edition. Show all posts

Sunday, October 22, 2023

HP 15C: Transition Matrix Test and Taking a Square Matrix to an Integer Power

HP 15C:   Transition Matrix Test and Taking a Square Matrix to an Integer Power



Transition Matrix Test - Markov Chain


Let A be a square matrix of probabilities (entries from 0 to 1).   Is the square matrix suitable to be used in Markov Chain calculations?   It would qualify if for each row, the elements of that row have a sum to 1.  


The program:  


*  Tests whether Matrix A is a square matrix.  Non-square matrices are not transition matrices qualified for Markov Chains.   (lines 005 through 007)

*  Sum the elements of each row.

*  Tests whether sum of each row is 1. (LBL 2)


If (I) and (III) are met, then Matrix A is qualified to be used as a transition matrix for Markov Chain calculations.   This test returns a 1 for yes and 0 for no.


Store the contents and dimensions of Matrix A before running the program. 



HP 15C Program - Transition Matrix Test


Code:


001 : 42,21,11 : f LBL A

002 : 45,16,11 : RCL MATRIX A

003 : 36 : ENTER

004 : 36 : ENTER

005 : 45,23,11 : RCL DIM A

006 : 43,30, 6 :  g TEST 6  (x≠y)

007 : 22, 1 : GTO 1

008 : 44, 2 : STO 2

009 : 43,35 : g CLx

010 : 1 : 1

011 : 42,23,12 :  f DIM B

012 : 44,16,12 : STO MATRIX B

013 : 33 :  R↓

014 : 33 :  R↓

015 : 45,16,12 : RCL MATRIX B

016 : 42,26,13 : f RESULT C

017 : 20 : ×

018 : 42,16, 1 : f MATRIX 1


019 : 42,21, 2 : LBL 2

020 : 45, 2 : RCL 2

021 : 44, 0 : STO 0

022 : 45,13 : RCL C

023 : 1 : 1

024 : 43,30, 6 : g TEST 6 (x≠y)

025 : 22, 1 : GTO 1

026 : 42, 5, 2 : DSE 2

027 : 22, 2 : GTO 2


028 : 43,32 : RTN   (test passes)


029 : 42,21, 1 : f LBL 1  

030 : 43,35 : g CLx

031 : 43,32 : RTN  (test fails)


Notes:


RCL C instead of RCL MATRIX C is used because we want to recall the element, not the entire matrix.  The element that is recalled depends on the row (stored in R0) and column (stored in R1). 


HP 15C:  Square Matrix to a Positive Integer


Using a loop is required.  We are not able to use the x^2 or the y^x function with matrices, an Error 1 condition occurs.  


 A square matrix is stored in Matrix A.   Enter the positive integer power (n > 1) on the X stack before running the program.  


Code:  


032 : 42, 21, 11 : f LBL B

033:  1 :  1

034: 30 : -

035 : 44, 2 : STO 2

036 : 45,16,11 : RCL MATRIX A

037 : 44,16,12 : STO MATRIX B


038 : 42,21, 3 : f LBL 3

039 : 42,16,11 : f RESULT C

040 : 45,16,11 : RCL MATRIX A

041 : 45,16,12 : RCL MATRIX B

042 : 20 : ×

043 : 44,16,12 : STO MATRIX B

044 : 42, 5, 2 : f DSE 2

045: 22, 3 : GTO 3


046 : 45,16,13 : RCL MATRIX C

047: 42,16, 1 : f MATRIX 1

048 :43,32 : g RTN


Notes:  


MATRIX 1:  Set the row and column pointers to 1  (R0 = 1, R1 = 1)



Example



A = [ [ 0.3, 0.7, 0 ] [ 0.3, 0.3, 0.4 ] [ 0.2, 0.5, 0.3 ] ] 


GTO A R/S:   1  (yes, Matrix A is qualified for as a transition matrix)


If we insert a high enough power (as n → ∞), the matrix settles into a steady state, where each column will have the same value.


25 GTO B R/S:   C 3 x 3 

MATRIX 1

RCL C...

[ [ 0.2736, 0.4623, 0.2642 ] [ 0.2736, 0.4623, 0.2642 ] [ 0.2736, 0.4623, 0.2642 ] ]



Eddie



All original content copyright, © 2011-2023.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Saturday, October 21, 2023

HP 15C: Vector Operations

HP 15C:   Vector Operations


Calculators covered:


* HP 15C

* HP 15C Limited Edition

* HP 15C Collector's Edition

* Apps, Swiss Micros DM 15



HP 15C:   Dot Product, Tensor Product, and Angle Between Vectors



Set vectors A and B as 3 x 1 vectors (3 rows, 1 column)


3 ENTER 1 dim A

3 ENTER 1 dim B


Dot Product  (LBL D)  


Dot product:  A ⋅ B = A^T B 

The result is a single number.


Code: 

001 : 42,21,14 : LBL D

002 : 45,16,11 : RCL MATRIX A

003 : 42,16, 4 : MATRIX 4

004 : 45, 16, 12 : RCL MATRIX B

005 : 42, 26, 13 : RESULT C

006 : 20 : ×

007 : 45,16,11 : RCL MATRIX A

008 : 42,16, 4 : MATRIX 4

009 : 45,13 : RCL C

010 : 43,32 : RTN


Tensor Product (LBL E)


Tensor product:  A ⊗ B = A B^T

The result is a 3 x 3 matrix


Code: 

011 : 42,21,15 : LBL E

012 : 45,16,11 : RCL MATRIX A

013 : 45,16,12 : RCL MATRIX B

014 : 42,16, 4 : MATRIX 4

015 : 42,26,13 : RESULT C

016 : 20 : ×

017 : 45,16,12 : RCL MATRIX B

018 : 41,16, 4 : MATRIX 4

019 : 42,16, 1 : MATRIX 1

020 : 45,16,13 : RCL MATRIX C

021 : 43, 32 : RTN


Angle Between Vectors (LBL 0)


θ = arccos( (A ⋅ B) ÷ (||A|| ||B||) )


Code:

022 : 42,21, 0 : LBL 0

023 : 32,14 : GSB D

024 : 45,16,11 : RCL MATRIX A

025 : 42,16, 8 : MATRIX 8

026 : 45,16,12 : RCL MATRIX B

027 : 42,16, 8 : MATRIX 8

028 : 20 : ×

029 : 10 : ÷

030 : 43,24 : COS^-1

031 : 43,32 : RTN


Notes:  


MATRIX 4:  Transposes a matrix, which it is stored in the original matrix slot.  


MATRIX 8:  The 2-norm of a matrix.   It is calculated by taking a square root of the sum of the square of each element.  √(Σ( A_r,c ^ 2,  r = 1 to n and c = 1 to m))


For the dot and tensor products, I transpose the appropriate matrix back to the 3 x 1 form after the calculation.



Example


A = [ [ 4 ], [ 5 ], [ -2 ] ]

B = [ [ -3 ], [ 9 ], [ 8 ] ]


Dot Product:  GTO D R/S

Result:  17


Tensor Product:   GTO E R/S

Result:

[ [ -12, 36, 32 ], [ -15, 45, 40 ], [ 6, -18, -16 ] ]


Angle Between Vectors:  GTO 0 R/S

Result:  78.2166°



Eddie


All original content copyright, © 2011-2023.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Python in Numworks: Duplicating and Grayscale

Python in Numworks: Duplicating and Grayscale All three scripts presented today use the math, random, and the Numworks specific ...