Showing posts with label biology. Show all posts
Showing posts with label biology. Show all posts

Saturday, August 22, 2026

Python (TI-84 Evo) and HP 32S: Shannon-Wiener Diversity Index

Python (TI-84 Evo) and HP 32S: Shannon-Wiener Diversity Index



Introduction



The Shannon-Wiener Diversity Index measures a level of how diverse a group of populations are by the following formula:


H = -Σ( p_i * ln(p_i), i = 1 to n) = Σ( -p_i * ln(p_i), i = 1 to n)


where p_i = (population of segment i) ÷ (total population)


As stated by M. Robert F. (see Sources), there are three levels of diversity:


Low Diversity: H < 1.5

Medium Diversity: 1.5 ≤ H ≤ 2.5

High Diversity: H > 2.5




Python: diverse.py

This script was done with a TI-84 Evo. The script uses only the math module and should be able to use on any calculator with Python.


# Math Calculations

from math import *


print("Shannon-Wiener Diversity\nIndex")

print("Enter a list of populations.")

lpop=eval(input("list: "))

# sum

s=sum(lpop)

# proportions

lpro=[i/s for i in lpop]

# -p log p

llog=[-i*log(i) for i in lpro]

# diversity index

h=sum(llog)


# results

print("diversity index:\n{0:.5f}".format(h))

if h>2.5:

  print("high diversity")

elif h<1.5:

  print("low diversity")

else:

  print("medium diversity")


HP 32S Program: Shannon Wiener Diversity Index


This was programmed on an original HP 32S, and the code should be same for the later HP 32SII and Swiss Micros DM32.


Instructions:

If need be, clear the variables

Enter the population size in the variables A, B, C, D etc… up to 25.

Enter the number of populations, XEQ A


Four labels are used:

LBL A: program start, initialization

LBL Z: determine total population (pop = A + B + C + D….)

LBL Y: determine proportion of each population (A = A/pop, B = B/pop, C = C/pop, …)

LBL X: calculate the Shannon-Weiner Diversity Index: -Σ(p_i * ln(p_i) from i=1 to n)



Code:

A01 LBL A

A02 ENTER

A03 ENTER

A04 ENTER

A05 CL x

A06 STO Z

A07 R↓

A08 STO i


Z01 LBL Z

Z02 RCL(i)

Z03 STO+ Z

Z04 R↓

Z05 DSE i

Z06 GTO Z

Z07 STO i

Z08 RCL Z


Y01 LBL Y

Y02 STO÷ (i)

Y03 DSE i

Y04 GTO Y

Y05 R↓

Y06 STO i

Y07 CL x

Y08 STO Z


X01 LBL X

X02 RCL (i)

X03 ENTER

X04 LN

X05 ×

X06 +/-

X07 STO+ Z

X08 DSE i

X09 GTO X

X10 RCL Z

X11 RTN


Size:

LBL A: 12.0 bytes

LBL Z: 12.0 bytes

LBL Y: 12.0 bytes

LBL X: 16.5 bytes

Total: 52.5 bytes


This does not count the 9.5 bytes needed for each variable used (Z, i, A, B, C, etc.). This program takes up to 25 populations (A through Y).



Examples


Example 1:


4 populations (n = 4).


A = 40

B = 30

C = 70

D = 20


Index: 1.2820 (low diversity index)


Example 2:


9 populations (n = 9).


A = 41

B = 36

C = 40

D = 97

E = 64

F = 37

G = 16

H = 18

I = 36


Index: 2.06303 (medium diversity index)



Sources



M. Robert F. The Math Behind Biology Lightning Source4 LLC. LaVergne, TN. 2026. pp. 6-8


Nolan, K.A. and J.E. Callahan. 2006. Beachcomber biology: The Shannon-Weiner Species Diversity Index. Pages 334-338, in Tested Studies for Laboratory Teaching, Volume 27 (M.A. O’Donnell, Editor). Proceedings of the 27th Workshop/Conference of the Association for Biology Laboratory Education (ABLE), 383 pages. 2006. ISBN 1-890444-09-X. Article: https://www.ableweb.org/biologylabs/wp-content/uploads/volumes/vol-27/22_Nolan.pdf. Retrieved May 6, 2026.





Eddie


All original content copyright, © 2011-2026. 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, February 8, 2020

HP 42S and HP Prime: Rabbits vs Foxes

HP 42S and HP Prime: Rabbits vs Foxes

Introduction

The program presented today is based on the Rabbits vs. Foxes program for the HP 25 (see source below).  The program determines the population of rabbits and foxes over time as modeled by the differential equations:

Change in Rabbits:
dr/dt = 2 * r - α * r * f

Change of Foxes:
df/dt = -f + α * r  * f

where:
r = population of rabbits
f = population of foxes
α = probability of a rabbit encounters a fox
h = step 

This is approximated by Euler's method.

The HP Prime program RABBIT25 displays a print screen of results of the desired amount of iterations.  The HP 42S program RAB25 displays the results one step at time in the format rrrrr.fffff  (rabbits.foxes), like the original HP 25 program.


HP Prime Program RABBIT25

EXPORT RABBIT25()
BEGIN
// 2020-01-20 EWS
// Based on the Rabbits vs
// Foxes HP 25 program
LOCAL α,h,r,f,k,n,a;

// initialize and input
INPUT({α,h,r,f,n},
"Rabbits vs Foxes",
{"α: ","h: ","r0:","f0:","n: "},
{"α","h","inital # rabbits",
"initial # foxes",
"# iterations"});
L0:={r}; L1:={f};


// compute data
MSGBOX("L0 = rabbit population,
L1 = fox population;
(0,1,2,...,n), size n+1");
HFormat:=0;
PRINT();
PRINT("Rabbits vs Foxes");
FOR k FROM 0 TO n DO
IF k≠0 THEN
a:=α*r*f;
r:=r+h*(2*r-a);
f:=f+h*(−f+a);
END;
PRINT(k+" R: "+IP(r)+" F: "+
IP(f));
END;


// end of program
END;


HP 42S/Free42/DM42 Program RAB25

00 { 58-Byte Prgm }
01▸LBL "RAB25"
02 STO 02
03 R↓
04 STO 03
05 FIX 05
06▸LBL 05
07 RCL 02
08 ENTER
09 ENTER
10 RCL 03
11 RCL 00
12 ×
13 ×
14 STO 04
15 X<>Y
16 -
17 RCL 01
18 ×
19 +
20 X<0 font="">
21 0
22 STO 02
23 RCL 03
24 2
25 ×
26 RCL 04
27 -
28 RCL 01
29 ×
30 RCL 03
31 +
32 X<0 font="">
33 0
34 STO 03
35 IP
36 RCL 02
37 IP
38 1ᴇ5
39 ÷
40 +
41 STOP
42 GTO 05
43 .END.

Note:  You can replace STOP with PSE, as many as you like, if you don't want to press R/S after each step. 

Instructions:
Store α in R00, store h in R01, enter initial rabbit population

Example

Initial Populations:
Rabbits:  r = 300
Fixes:  f = 150
α = 0.01
h = 0.02

Iter:   Rabbits.Foxes  (rrrrr.ffff)
0:  300.00150
1:  303.00156
2:  305.00162
3:  307.00169
4:  309.00176
5:  311.00183
6:  312.00191
7:  312.00199
8:  312.00207
9:  312.00216
10: 311.00225
11: 309.00235
12: 307.00245
13: 304.00255
14: 301.00265
15: 297.00276


Source:

Randall B. Neff and Lynn Tilman "An Example of HP-25 Programming" Hewlett-Packard Journal: November 1975.  pg. 6
Eddie


All original content copyright, © 2011-2020.  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, November 30, 2019

HP 41C and DM41L: Basic EKG Calculations

HP 41C and DM41L: Basic EKG Calculations



Introduction

The program calculates the following:

*  Lead II magnification, in mm
*  The mean axis deviation
*  The mean axis magnitude 

Given:

*  Lead I positive deflection, in mm
*  Lead III negative deflection, in mm

The mean axis deviation and magnitude are calculated by a rectangular conversion by the following coordinates:

X: Lead I positive deflection - Lead I negative deflection
Y:  Net Lead I deflection * 0.5774 + Net Lead III deflection * 1.1547
Mean axis deviation:  θ - 57°

The program also:

* Converts between the heart rate (rpm) and the R-R interval
* Use either parameter to calculate the Q-T interval, in seconds

heart rate = 60/R-R
Q-T interval = √(R-R)*0.39

The program is a translation of HP 67/HP 97 Basic EKG Determination, which itself is a translation of Steven A. Conrad's HP-65 program (HP-65 Users' Library Program).  See the source listed below.

Notes:

1.  Clear the assignments of keys A (Σ+) through E (LN) before running the program.  This must be done outside the programming environment.  Clear assignments to keys by ASN (blank) (designated key).  

Example:  Clear assignment from A: [ shift ] (ASN)  [ALPHA] [ALPHA] [ Σ+ ]

2.  Running EKG will turn the User Keyboard on.

3.  The program will set the calculator to degrees mode. 

4.  This program was entered on an HP 41C, and it should work on any simulator and Swiss Micros DM41.  

Instructions

1.  Run EKG.

2.  Determine Lead I net deviation and store it to register 01:
(in User's Mode) positive deviation Lead I [ENTER] negative deviation Lead I ( A )  

3.  Determine Lead II net deviation, storing net Lead III net deviation to register 03 and Lead II net deviation to register 02:
(in User's Mode)  positive deviation Lead III [ENTER] negative deviation Lead III ( B )
Result:  Lead II net deviation

4.  Compute Mean Axis:
(in User's Mode)   ( C )  deviation is displayed [ R/S ] magnitude is displayed

5a.  Convert heart rate to R-R:   
(in User's Mode) heart rate (bpm) ( D )
-or-
5b.  Convert R-R to heart rate:
(in User's Mode)  R-R ( D )

6.  Compute Q-T.  
(in User's Mode)  ( E )

HP 41C/DM41 Program:  EKG

01 LBL T^EKG
02 SF 27
03 GTO 00
04 LBL A
05 - 
06 STO 01
07 ^T III:_ 
08 ARCL X
09 AVIEW
10 RTN
11 LBL B
12 -
13 STO 03
14 RCL 01
15 +
16 STO 02
17 ^T II:_ 
18 ARCL X
19 AVIEW
20 RTN
21 LBL C
22 RCL 01
23 ENTER↑
24 0.5774
25 *
26 RCL 03
27 ENTER↑
28 1.1547
29 *
30 +
31 ENTER↑
32 RCL 01
33 R-P
34 X<>Y
35 57
36 -
37 ^T DEV=
38 ARCL X
39 AVIEW 
40 STOP
41 X<>Y
42 ABS
43 ^T MAG=
44 ARCL X
45 AVIEW
46 RTN
47 LBL D
48 ENTER↑
49 ENTER↑
50 60
51 X<>Y
52 /
53 X<=Y?
54 GTO 05
55 ^T RATE=
56 ARCL X
57 AVIEW
58 RTN
59 LBL 05
60 ^T R-R=
61 ARCL X
62 AVIEW
63 RTN
64 LBL E
65 X>Y?
66 X<>Y
67 SQRT
68 0.39
69 *
70 ^T Q-T=
71 ARCL X
72 AVIEW 
73 RTN
74 LBL 00
75 RTN
76 END

Example

I+ = 2.8 mm,  I- = 1.1 mm
III+ = 2.5 mm, III- = 1.4 mm
Heart Rate = 86 bpm

Keystrokes:

XEQ "EKG"
(User Mode is On)
2.8 [ENTER] 1.1 ( A ) 
Display:  III: 1.7000

2.5 [ENTER] 1.4 ( B )
Display:  II:  2.8000

( C )
Display:  DEV=-4.0516
[ R/S ]
Display:  MAG=2.8214

86 (D)
Display:  R-R=0.6977

(E)
Display:  Q-T=0.3258

Source:

"Basic EKG Determinations"  HP-67/97 User's Library Solutions:  Cardiac.  Hewlett Packard.  Corvallis, OR.  (no date given, but I estimate this to be circa 1974)

Eddie

All original content copyright, © 2011-2019.  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. 

Monday, April 10, 2017

HP Prime: Fick Cardiac Output (The Blog Turns Six!)



HP Prime:  Fick Cardiac Output  (The Blog Turns Six!)

A Special Note

Happy sixth birthday to my mathematics blog!   And a big reason why blog has turned six on April 11, 2017 is because of all of you readers!  Words can’t explain how appreciated I am. 

Thank you!  Cheers to everyone!

Fun fact:  The most popular blog entry is my TI-36X Pro review (my fourth blog entry, 4/27/2011).  Link:  http://edspi31415.blogspot.com/2011/04/ti-36-pro-review.html

Now let’s get to today’s subject. 

Fick Cardiac Output Equations

The program FICK will calculate:

* CO: Cardiac Output (L/min)
* SV: Stroke Volume (ml)
* CI:  Cardiac Index (L/(min/m^2))
* SI:  Stroke Index (ml/m^2)

Given the following inputs:

* Ca:  Arterial blood oxygen (O2) content percentage
* CvO2:  Venous blood oxygen (O2) content percentage
* VO2:  Oxygen compulsion (in ml per pure oxygen gas per minute, STPD)
* HR:  Heart Rate in beats per minute (BPM)
* Height in cm
* Weight in kg

STPD: Standard Temperature Pressure Density

Conversion factors:  1 in = 2.54 cm, 12 in = 1 ft, 1 lb = 0.45359237 kg

Formulas: 

CO = VO2 / (10 * (Ca - CvO2))
SV = CO * 1000/HR
CI = CO/BSA
SI = SV/BSA

BSA:  body surface area, measured in cm^3.  There are several formulas, but the Fick program uses the simple approximation formula:

BSA = √(weight * height)/60

HP Prime Program FICK

EXPORT FICK()
BEGIN
// Fick Cardiac Output
// HP 65, 1972
// EWS 2017-04-10

// inputs
LOCAL ca,cv,vo,hr,h,w;
INPUT({ca,cv,vo,hr,h,w},
"Fick Cardiac Output",
{"Ca O_2:","C_v O_2:","VO_2:","HR:",
"Height:","Weight:"},
{"Arterial blood (%)",
"Venous blood (%)",
"O_2 compulsion (ml/min)",
"Heart Rate (BPM)",
"Height (cm)",
"Weight (kg)"});

// calculation
LOCAL co:=vo/(10*(ca-cv));
LOCAL sv:=co*1000/hr;
// simple BSA
LOCAL bsa:=√(w*h)/60;
LOCAL ci:=co/bsa;
LOCAL si:=sv/bsa;

// output
PRINT();
PRINT("Cardiac Output (l/min)");
PRINT(co);
PRINT("Stroke Volume (ml)");
PRINT(sv);
PRINT("Cardiac Index (L/(min/m^2))");
PRINT(ci);
PRINT("Stroke Index (ml/m^2)");
PRINT(si);

END;

Example:

Inputs:



* Ca:  19%
* CvO2:  15%
* VO2:  250 ml/min
* HR:  64
* Height: 5 ft 11 in, 180.34 cm
* Weight 196 lb, 88.90410452 kg

Results:

* CO: 6.25 L/min
* SV: 97.65625 ml
* CI:  2.96158586825 L/(min/m^2)
* SI:  46.2747791914 ml/m^2

Source:

Hewlett Packard.  HP-67/97 User’s Library Solutions: Cardiac Corvallis, OR 1976  pg. 20

Here’s to many more years!

Eddie

This blog is property of Edward Shore, 2017.

Python – Earth’s Radius and Gravity in US Units

Python – Earth’s Radius and Gravity in US Units Introduction The following script, gravus2.py, estimates the Earth’s gravity i...