Showing posts with label logical list. Show all posts
Showing posts with label logical list. Show all posts

Sunday, October 18, 2015

HP Prime Programs Update: BOOLLIST and SAM

Two typos had to be corrected for the BOOLLIST (choose a list's elements by use of a boolean list) and SAM (random perumtation).  Both of the program listings are now correct and are the links are listed here:

BOOLLIST


EXPORT BOOLLIST(LA, LB)
BEGIN

LOCAL LC, n, s, k, j;

//  Initialization
LC≔{ };
j≔1;
s≔SIZE(LA);
n≔SIZE(LB);

// Process
FOR k FROM 1 TO s DO

IF LB(j)==1 THEN
LC≔CONCAT(LC,LA(k));
END;

j≔j+1;

IF j>n THEN
j≔1;
END;

END;

RETURN LC;

END;


SAM


Input:  SAM(n)

EXPORT SAM(n)
BEGIN
LOCAL s,L0,L1,I,c;

L0≔MAKELIST(X,X,1,n,1);
L1≔{ };
I≔0;

REPEAT
c≔RANDINT(1,n);
IF L0(c)≠0 THEN
L1≔CONCAT(L1,{L0(c)});
L0(c)≔0;
I≔I+1;
END;
UNTIL I==n;

RETURN L1;

END;


Eddie

This blog is property of Edward Shore.  2015

Monday, August 17, 2015

TI-84 Plus: Picking List Elements Using a Logical List and Matrix By-Element Multiplication

Picking List Elements Using a Logical List 

The program BOOLLIST picks out elements from a source list based on a logical list (a list that consists of 0s and 1s).  0 represents  FALSE (do not pick) and 1 represents TRUE (pick).  

For more information and examples, see my last blog entry:  http://edspi31415.blogspot.com/2015/08/hp-prime-picking-out-elements-using.html

Note, that weird looking L is not the L character, but is represents the small "L" character.  This is accessed from [2nd], [stat] (list), OPS sub-menu, select B for "L" (the last option in this sub-menu).  On this listing, I will bold the "L".  


TI-84 Plus:  BOOLLIST

Input "SOURCE LIST=",A
Input "LOGICAL LIST=",B
sum(B)→S
S→dim(C)
1→I
1→J
dim(A)→A
dim(B)→B
For(K,1,A)
If B(J)=1
Then
A(K)→C(I)
1+I→I
End
1+J→J
If J>B
1→J
End
Pause C

Matrix by Element Multiplication

In R, the multiplication operator (*) multiplies matrices element-by-element.  To get the proper linear-algebra multiplication of matrices, use the %*% operator.  The program MATMLEM implements the former method.  

Example:
[A] = [[ 1, 2 ] [ 3, 4 ]]
[B] = [[ 4, 3 ] [ 2, 1 ]]
Running MATMELM returns the matrix [[ 4, 6 ][ 6, 4 ]].

Variables [H], [I], and [J] are used for calculations.  Also, like BOOLLIST above,  the weird looking L is not the L character, but is represents the small "L" character.  This is accessed from [2nd], [stat] (list), OPS sub-menu, select B for "L" (the last option in this sub-menu).  On this listing, I will bold the "L". 

TI-84 Plus:  MATMELM

Disp "MATRIX MULTIPLY"
Disp "BY ELEMENT"
Input "[H]=",[H]
Input "[I]=",[I]
dim([H])→H
dim([I])→I
If H(1)≠I(1) or H(2)≠I(2)
Then
Disp "INVALID"
Stop
End
[H]→[J]
For(H,1,H(1))
For(I,1,H(2))
[H](H,I)*[I](H,I)→[J](H,I)
End
End
DelVar H
DelVar I
Disp "[J]="
Pause [J]


If the program is hard to read, please let me know in the comments.  


I am about half way through the R introduction programming course.  Time flies by when you are having fun. 

Eddie

This blog is property of Edward Shore.  2015.

Tuesday, August 11, 2015

HP Prime: Picking Out Elements Using a Logical List (Updated 10/18/2015)

HP Prime:  Picking Out Elements Using a Logical List

I am taking an online class in R Programming language (www.edx.org), having a great time. The program BOOLLIST is based on the ability in R to pick out elements using logical elements (TRUE, FALSE).


Example (in R):

vector <- [2, 3, 4, 5]
vector[ c(TRUE, TRUE, FALSE, TRUE) ]   returns [2, 3, 5]

The logical vector doesn’t have to be same length as the source vector.  If the logical vector has elements than the source vector.    

vector <- [1, 2, 3, 4, 5, 6]
vector[ c(TRUE, FALSE, TRUE, FALSE, TRUE, FALSE) ] returns [1, 3, 5]
vector[ c(TRUE, FALSE) ] returns [1, 3, 5]  (TRUE, FALSE pattern recycles)

Program BOOLLIST:

Input:  BOOLLIST(source list, logical list)

Notes:  Use list brackets { }.  For the logical list, use 1 for TRUE and 0 for FALSE.

EXPORT BOOLLIST(LA, LB)
BEGIN

LOCAL LC, n, s, k, j;

//  Initialization
LC≔{ };
j≔1;
s≔SIZE(LA);
n≔SIZE(LB);

// Process
FOR k FROM 1 TO s DO

IF LB(j)==1 THEN
LC≔CONCAT(LC,LA(k));
END;

j≔j+1;

IF j>n THEN
j≔1;
END;

END;

RETURN LC;

END;


Examples:

In addition to the examples above that can be tried with BOOLLIST:

BOOLLIST( {4,2,3,6}, {1,0,0,1} ) returns {4, 6}

BOOLLIST( {3,9,6,-1,6}, {1,0} ) returns {3, 6, 6}

See you next time, Eddie

Update:  There was an error in the program listing.  Previously I had b:=SIZE(LB); where it should be n:=SIZE(LB).  Eddie




This blog is property of Edward Shore – 2015.

The MU Key on a Four Function Calculator and Programs for the DM42/HP 42S

  The MU Key on a Four Function Calculator and Programs for the DM42/HP 42S Not too long ago, I purchased this very colorful, four funct...