Showing posts with label INPUT. Show all posts
Showing posts with label INPUT. Show all posts

Saturday, July 20, 2019

TI Nspire CX II and TI Nspire CX II CAS: User Input in Graphics Mode with getKey

TI Nspire CX II  and TI Nspire CX II CAS:  User Input in Graphics Mode with getKey

Introduction

In graphics mode, the TI Nspire CX II cannot use the Text, Request, or RequestStr.  However, with the use of getKey command, we can have the user interact with the program. 

TI Nspire CX II Program keydemo2

This program is a game where the user stops a spinner in hopes to win money or a car.  Good luck!

Define keydemo2()=
Prgm
:Clear 
:Local l,str,k,n
:l:={"$200","$300","$500","$1000","CAR","Nope."}
:While getKey(0)≠"enter"
:  Clear 
:  UseBuffer 
:© get key continous execution
:  n:=randInt(1,6)
:  SetColor 0,0,0
:  DrawText 50,25,"PRESS enter TO STOP"
:  If n≤5 Then
:    SetColor 0,128,128
:  Else
:    SetColor 255,0,0
:  EndIf
:© use square brackets for elements 
:© we can clear only dynamic areas
:© but it does not look good
:  DrawText 50,50,l[n]
:  PaintBuffer 
:© usebuffer and paintbuffer allows all objects to be displayed at once, ensuring a smooth transition, can also use wait
:EndWhile
:EndPrgm



TI Nspire CX II Program enterdemo

With the use of getKey, the user enters a number in graphics mode.  This can be used as a template. 

Define enterdemo()=
Prgm
:© goal: develop input in graphics mode
:© use float mode
:setMode(1,1)
:Local str,num,k
:str:=""
:© use initial text
:Clear 
:  SetColor 0,0,0
:  DrawText 0,50,"Press [enter] to stop."
:
:
:© main loop: enter the numbers
:Loop
:  Clear 
:  UseBuffer 
:  SetColor 0,0,0
:  DrawText 0,50,"Press [enter] to stop."
:
:  k:=getKey(1)
:  If k="." and inString(str,".")=0 Then
:    str:=str&"."
:  ElseIf k="0" Then
:    str:=str&"0"
:  ElseIf k="1" Then
:    str:=str&"1"
:  ElseIf k="2" Then
:    str:=str&"2"
:  ElseIf k="3" Then
:    str:=str&"3"
:  ElseIf k="4" Then
:    str:=str&"4"
:  ElseIf k="5" Then
:    str:=str&"5"
:  ElseIf k="6" Then
:    str:=str&"6"
:  ElseIf k="7" Then
:    str:=str&"7"
:  ElseIf k="8" Then
:    str:=str&"8"
:  ElseIf k="9" Then
:    str:=str&"9"
:  ElseIf k="−" Then
:    str:=string(−1*expr(str))
:  ElseIf k="del" and dim(str)>0 Then
:    str:=left(str,dim(str)-1)
:
:  ElseIf k="enter" Then
:© "lock" the number and leave
:  SetColor 0,0,0
:  DrawText 0,100,str
:  PaintBuffer 
:    Exit
:  EndIf
:
:SetColor 0,0,255
:DrawText 0,100,str
:PaintBuffer 
:
:EndLoop
:© return number to home
:num:=expr(str)
:Disp num
:EndPrgm



There are two ways the getKey command can be used in graphics mode.

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.

Tuesday, October 3, 2017

Adventures in Python: Input Requires Declaring Type of Input

Adventures in Python: Input Requires Declaring Type of Input

I work with calculators, and in calculator programming, the Input command usually defaults to numerical input.  That is not the case in Python.  Input, without declaration, defaults in strings.  Strings are nice, but don’t usually do well in mathematical calculation.

Ways to use the Input command:

input(‘prompt’)
Input accepted as a string.  We can also use string(input(‘prompt’)).
int(input(‘prompt’))
Input accepted as integers.
float(input(‘prompt’))
Input accepted as real (floating) numbers.
complex(input(‘prompt’))
Input accepted as complex numbers.  In Python, complex numbers are in the form x + yj (instead of x + yi). 

The prompt is an optional string. 

In the following program, thanks to complex(input()) format, inputs are accepted as complex numbers.  This solves the quadratic equation using the quadratic formula. 

One other note, I use the double asterisk (**) for powers.  Examples:  2**2 = 2^2 = 4,  16**0.5 = √16 = 4.  The double asterisk works on complex numbers, opposed to the math.sqrt function.

# Program 002: Quadratic Equation
# header
# note that all comments start with a hashtag

# header
print("a*x^2 + b*x + c = 0")

# the default format of input is string
# we must use complex to all for complex numbers
# complex numbers are in the format real + imag*j
# where j = sqrt(-1).  j is usally i but it is used
# for Python and electronic engineering
a = complex(input("a: "))
b = complex(input("b: "))
c = complex(input("c: "))

# discriminat
# note that powers y^x are used by double asterisk, like this y**x
d = b**2-4*a*c

# root calculation
root1 = (-b + d**(0.5))/(2*a)
root2 = (-b - d**(0.5))/(2*a)

# display results
print("Discriminant =  ",d)
print("Root 1 = ",root1)
print("Root 2 = ",root2)


Output (I give a = 1+6j, b = -9, c = 4 as inputs):

a*x^2 + b*x + c = 0
a: 1+6j
b: -9
c: 4
Discriminant =   (65-96j)
Root 1 =  (-0.1590249844353114-1.5691249198547j)
Root 2 =  (0.4022682276785546+0.10966546039524058j)

More adventures in Python to come! 

Eddie


This blog is property of Edward Shore, 2017.

Monday, July 3, 2017

Programming Languages: TI-85 vs. TI-84 CE Plus

Programming Languages:  TI-85 vs. TI-84 CE Plus

I purchased a 1995 edition of the TI-85 at a Pasadena City College swap meet.  The TI-85 was my go-to and favorite calculator through high school (1991 – 1995). 

Today, I am going to compare common programming commands between two of my favorite Texas Instrument calculators, the TI-85 and the current TI-84 CE Plus.  The manuals for each calculator were consulted to answer questions.

Arguments in vector brackets [ ] means the argument is optional.

Command
TI-85 (1992 – 1997)
(probably also applies to the TI-86)
Software Version 10.0
TI-84 CE Plus (2016-)
(most of the this applies to TI-84 Plus 2004-present)
Software Version 5.2.2
Variable Names
Up to 8 characters, must begin with a number, no spaces.
All variables are global.
Single letters A through Z, n, θ
All variables are global.
Complex Number display, Rectangular
(X, Y)
X + Yi
Complex Number display, Polar
(R θ)
R e^θi
Graphing Modes
Function, Polar, Parametric, Differential Equation
Function, Polar, Parametric, Recurring Sequences
Number of Colors
0 (monochrome display)
15: blue, red, black, magenta, green, orange, brown, navy, light blue, yellow, white, light gray, medium gray, gray, dark gray (backlit display)
**CE and C versions only
Input
Input by itself displays the graph screen

Input [“prompt string”], varname
Input by itself displays the graph screen

Input  [“prompt string”], variable
Prompt:  asking for and storing multiple values
Prompt var1, var2, var3 ,…
Prompt var1, var2, var3, …
Disp
Disp by itself displays the home screen

Disp var/string, [var/string …]
Disp by itself displays the home screen

Disp var/string, [var/string…]
Pause
Pause [var/string]
Pause [var/string], [time in seconds]
Wait
N/A
Wait time in seconds up to 100
Display the graph screen
DispG
displays the graph screen
DispGraph
displays the graph screen
Display the function table
N/A
DispTable
Output
Outpt(line, col, var/string)
8 lines, 21 columns
Output(row, col, var/string)
10 rows, 26 columns
InpSt
InpSt stores entered text or equations as strings
N/A.  Use Input
If – Then – Else structure
If condition
Then
do if true
[Else
do if false]
End
If condition
Then
do if true
[Else
do if false]
End
For structure
For(var, begin, end, [step])
commands
End
For(var, begin, end, [step])
commands
End
While structure
While condition
do while condition is true
End
While condition
do while condition is true
End
Repeat – Until structure
Repeat condition
do while condition is false
End
Repeat condition
do while condition is false
End
Menu
Menu(nn, string, label…)
Where nn is from 1 to 15
Menu(“title”, “text”, label…)
Up to 9 options
Labels (Lbl/Goto)
Label names can have up to 8 characters.  All labels are local.
A-Z, 0 -99, θ.  All labels are local.
Increment (by 1) and Skip (if greater to value)
IS>(variable, value)
IS>(variable, value)
Decrement (by 1) and Skip (if less than value)
DS<(variable, value)
DS<(variable, value)
Stop program execution
Stop
Stop
Return from a subroutine
Return
Return
Execute a subroutine
Type the name of the program
Call program by pressing [prgm], choosing it from the Program submenu during editing
Clear the graph screen
ClDrw (clear all drawings)
ClrDraw (clear all drawings)
Display text on a graph screen
N/A
TextColor(color) sets the color.
Text([-1], row, col, string)
0-164 row pixel,
0-264 column pixel
-1 is for large text
Draw a temporary function
DrawF f(x)
DrawF f(x), [color]

Draw a temporary inverse function (f^-1(x) = y, x and y are swapped)
DrInv f(x)
DrawInv f(x), [color]
Shading
Shade (lower, upper, left, right)
Shade (lower, upper, [left, right, pattern, pattern resolution, color])
Draw a line
Line(x1, y1, x2, y2)
Line(x1, y1, x2, y2, [0/1, color, line style])
0/1: 0 to erase, 1 to draw
Draw a circle
Circle(x, y, radius)
Circle(x, y, radius, [color, line style])
Draw a Point
Only the TI-84 Plus CE has similar commands for pixels
PtOn(x,y)
PtOff(x,y)
PtChg(x,y)
Pt-On(x,y, [mark, color])
Pt-Off(x,y,[mark])
Pt-Change(x,y,[color])
Convert a value to a string
N/A
eval(expression) → Str#
No complex results
Convert a string to a value
N/A
expr(string)
Convert a string to a graph variable
St>Eq(string, graph variable)
String>Equ(string, graph variable)
Length of a string
lngth string
length(string)
(call from the catalog)
Extract part of a string
sub(string, begin point, length)
sub(string, begin point, length)
(call from the catalog)
Execute Linear Regression analysis (other regressions are similar)
LinR xlist, ylist
LinReg(ax+b) xlist, ylist, [freqlist, Y= variable]
Turn a stat plot on
N/A (stat plots are not available on the TI-85)
PlotsOn [1,2,3]
Turn a stat plot off
N/A (stat plots are not available on the TI-85)
PlotsOff [1,2,3]
Turn graph functions on
FnOn [1 – 99]
FnOn [1-9, 0]
Turn graph functions off
FnOff [1 – 99]
FnOff [1-9, 0]

Notes

As we can see, the programming language of the TI-85/86 family is similar to the TI-84 Plus family, translating the programs should not be difficult.  I believe that stat plots are available on the TI-86, but not the TI-85.

Keep in mind if you have complex numbers, that the complex mode on the TI-84 Plus (a+bi or re^(θi) has to be turned on.  Furthermore, logarithmic, power, exponential, and trigonometric functions for complex numbers return error on the TI-84 Plus (substitute expressions must be used).

(use radians mode)
With z = X + Yi = R*e^(θ*i), X = real(z), Y = imag(z), R = abs(z), θ = arg(z)

z^n = R^n * (cos(n*θ) + sin(n*θ)*i), n is a real number
ln (z) = ln R + θ*i
e^(z) = e^X * cos Y + e^X * sin Y * i
sin(z) = sin X * cosh Y + cos X * sinh Y * i
cos(z) = cos X * cosh Y – sin X * sinh Y * i
sin¯¹ (z) = asin(z) = -i * ln (zi ± √ (1 – z^2)
z1 ^ z2 = e^(z2 * ln z1)

Eddie


This blog is property of Edward Shore, 2017

Sunday, July 2, 2017

App Review: Programmable Calculator (Jeff Glenn)

App Review:  Programmable Calculator (Jeff Glenn)

Programmable Calculator app screen shots

Author:  Jeff Glenn
Date: 2014
Cost:  99 cents
Version Reviewed: 1.2.0, 3/20/2015
Type: Programmable, Basic
Platform:  Android

Mathematical Programming From Scratch

The Programmable Calculator app lives up to its name.  It gives you barebones as far as features are concerned: just the four arithmetic functions (addition, subtraction, multiplication, and division) and nothing else.  Not even square root or π. 

Their website, http://igram.org/progcalc/user_manual.html, has a program to calculate square roots.

Before there were scientific calculators and scientific functions arrived on our computers, they had to all be programmed using just arithmetic functions. 

If you want to try scientific programming on this app, you may want to consult the 1975 book Scientific Analysis on the Pocket Calculator by Jon M. Smith (John Wiley & Sons), or this website by Ted Muller:  http://tedmuller.us/Math/Calculator-1'Introduction.htm 

Needless to the say, the Programmable Calculator operates on Chain logic.  That is, it doesn’t follow the algebraic order of operations ( 2, [ + ], 3,  [ * ], 4, [ = ] returns 20 instead of 14).

Press [ f ], [R/S] to access the options and user manual. Options include turning sound and vibration on for key presses, which I highly recommend. 

Programming

The programming features on this app are laid directly on the keyboard.  There are 10 memory registers (R0 through R9), 10 labels (again, 0 – 9), and the accumulator (number on the display) to use.  Programs can be saved and loaded with file names.  The limit seems to be the amount of memory on your Android device (phone).

What is weird is that certain commands, such as the comparison, store, and recall, will prompt you for either the accumulator (display, by pressing the [ . ]), a specific memory register (0 – 9), or a constant, which will require a press of the equals key [ = ] first.  This takes getting used to.

Here are some commands. 

[ IN ].  Input.  (in).   Stores the prompted value to a designated memory register.  This command also acts as a prompt.  If a print string proceeds the input command, the prompt gets attached to the end of the screen.

[ OUT ].  Output.  (out)   Displays the contents of a designated memory register.  Execution doesn’t stop on output, so if this command is the last command before eof (end of file), you’ll need either an R/S (run/stop) or sleep.  If a print string proceeds the output command, the value gets attached to the end of the print string.

[ PRT ].   Print  (prt).  Prints a string.  Without a string, this command would clear the display instead.

[ LBL] and [ GTO ].   Label (lbl) and go to (gto), respectively.

[ DSZ ].  Decrement and skip.  (dsz)  Designates a register to decrease by 1.  If the result is zero, the next command is stepped.

[ < ], [ = ], [ > ], ≤, ≠, ≥.  Comparisons.  Compares the accumulator (display) to a designated register.  If the test is true, the next command is executed.  Otherwise, the next command is skipped. Code names: lt, eq, gt, le, ne, and ge respectively.

[ SET ].  Sets the accumulator (display) to a designated value.

[ CLR ].  Clears the accumulator (display). 

[ STO ] and [ RCL ].  Store and recall.

The arithmetic keys: [ + ], [ - ], [ * ], [ ÷ ].  These keys work quite differently in program mode.  You are prompted to designate a register for the operation to work on (like recall arithmetic).  You can also choose the accumulator by pressing [ . ].  Pressing the equals key [ = ] allows for a numeric constant to be entered.  All numeric constants are shown as floating point numbers.  For example, add 1.0 would add 1, but add 1 would add the value of register 1.  Code names are add, sub, mul, and div, respectively.

[SLEEP]  This command pauses execution.  A value of 1,000 amounts to 1 second. 2,000 for 2 seconds, etc. Code name: slp.  This works similarly to the comparison and arithmetic commands.

[R/S]  Run/Stop.  Code name: stp.

[REM]  Remark, comment.

If I understand [TIME] correctly, this records the time value onto the calculator app.  I haven’t tried this yet.


Sample Programs

I suggest trying the sample programs listed below or listed online manual ( http://igram.org/progcalc/user_manual.html  ) before programing on your own.  I really got stuck with the input instruction and how to get the R/S key to work out.

Square Plus 1

This program calculates the function f(x) = x^2 + 1

Step
Line
Comment
00
prt “Number: “
Prompt
01
in 0
Input to R0
02
prt  (blank string)
Keys: [PRT], [DONE]
Clears the display
03
rcl 0

04
mul 0
R0 * R0
05
add 1.0
Keys: [ + ], [ = ], 1, [ = ]
06
sto 1
Store result in R1
07
out 1
Display R1
08
sleep 1000.0
Keys:  [ f ], [OUT] (SLEEP), [ = ], 1000, [ = ]
09
eof
End of Program

Example:
Input: 6, Result:  37
Input: 11, Result: 122

Area of a Circle

The value of π must be manually entered.  I use 10 digits.

Step
Line
Comment
00
rem “Area: Circle”
Remark
01
prt ([DONE])
Clear the display
02
prt “Radius: “
Prompt: “Radius”
03
in 0
Input to R0
04
prt

05
rcl 0

06
mul 0
R0 * R0
07
mul (=) 3.1415926535
Use [ = ] to enter the constant π
08
sto 1
Store result in R1
09
out 1
Display R1
10
eof
End of Program

Example:
Input:  2.5, Result:  19.634954084375

Power:  x^y

Conditions:  x > 0, y is an integer.  Neither condition is tested.  Since only arithmetic functions are available, a loop is used.

Step
Line
Comment
00
prt
Clear the display
01
prt “x = “
Prompt for x
02
in 0
Input R0
03
rcl 0

04
sto 2
Store R0 in R2 (copy x)
05
prt

06
prt “y (integer) = “
Prompt for y, with reminder
07
in 1
Input R1
08
lbl 0
Label 0, begin the loop
09
rcl 0

10
mul 2

11
sto 0
R0 * R2 → R0
12
rcl 1

13
sub 1.0
Keys: [ - ], [ = ], 1, [ = ]
R1 – 1
14
sto 1
R1 – 1 → R1
15
rcl 1
Put R1 in the display (accumulator)
16
gt 1.0
Keys: [ > ], [ = ], 1, [ = ]
Is R1 > 1?
17
gto 0
Goto Label 0 if R1 > 1
18
prt

19
prt “x^y = “
Start result string
20
out 0
Attach R0 (result) to string
21
eof


Examples:
Input:  x = 3, y = 5.  Result:  243
Input:  x = 2.7, y = 8.  Result: 2824.2953648100015

Modulus

This program calculates x mod y, where x and y are both positive and x > y.

Step
Line
Comment
00
prt
Clear the display
01
prt “x (>0) = “
Prompt for x
02
in 0
Input R0
03
prt

04
prt “y (>0) = “
Prompt for y
05
in 1
Input R1
06
lbl 0
Label 0, begin the loop
07
rcl 0

08
sub 1
Subtract R1
09
sto 0
R0 – R1 → R0
10
rcl 0

11
sub 1

12
ge 0.0
Keys: [ ≥ ], [ = ], 0 , [ = ]
R0 – R1 ≥ 0?
13
gto 0
Goto label 0, repeat loop if R0 ≥ R1
14
prt

15
prt “x mod y = “
Begin result string
16
out 0
Attach R0 to the result string
17
eof


Examples:
Input:  x = 44, y = 3.  Result:  44 mod 3 = 2
Input:  x = 76, y = 20.  Result:  76 mod 20 = 6

Final Verdict

The programming language seems to be clumsy at first and it will take some getting used to.  Turning on the sound and vibration made operating the app much better because the touch has to be precise.  I can see how this app can easily frustrate people. 

Also it is very unusual that the equals key is at the top row of the keyboard instead of the usual location at the bottom. 

With a lack of scientific functions, I recommend this app if:

(1) You want to engage mathematical programming from scratch.  Remember that all the scientific, financial, and advanced features had to be originally programmed using the four arithmetic functions themselves!
(2)  You want a challenge.

Eddie



This blog is property of Edward Shore, 2017.

RPN HP 12C: Fibonacci and Lucas Sequences

  RPN HP 12C: Fibonacci and Lucas Sequences Golden Ratio, Formulas, and Sequences Let φ be the Golden Ratio: φ = (1 + √5) ÷ 2...