Testing Python syntax. HP-Prime Firmware Oct-2019
|
10-25-2019, 08:52 PM
(This post was last modified: 10-30-2019 05:06 PM by compsystems.)
Post: #1
|
|||
|
|||
Testing Python syntax. HP-Prime Firmware Oct-2019
Hello.
The numworks calculator is the first calculator in history with Python Language (numeric calculation engine), but the hp-prime is the first calculator with python syntax language (numerical / symbolic calculation engine) thanks to Bernard Parisse for GIAC/XCAS kernel https://www-fourier.ujf-grenoble.fr/~parisse/giac.html Python syntax for code program and entry line (CAS Mode). First topic, list comprehension. List comprehensions provide a concise way or compact expression to define lists. It consists of brackets containing expressions. Syntax: result := [ transform_expression iteration_expression filter_expression ] On the entry line and cas mode PHP Code: python_compat(True) The use of list comprehensions is to simplify a set of sentences for example PHP Code: squares := []; This will give you the output: [ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 ] Or you can use list comprehensions to get the same result: PHP Code: squares := [ x**2 for x in range( 10 ) ] Please try the following examples and report any anomaly. 1: List comprehension. purge( x, j, k, c, t ) # Remove content from variables # Case 1, define a numerical sequence on entry line PHP Code: list1 := range( 5 ) [enter] returns [ 0, 1, 2, 3, 4 ] range( 5 ) is equivalent to [ seq( 0..4 ) ] [ seq( 0..4 ) ] [enter] returns [ 0, 1, 2, 3, 4 ] list1a := range( 1, 5 ) # specifying start and end-1 [enter] returns [ 1, 2, 3, 4 ] range( 1, 5 ) is equivalent to [ seq( 1..4 ) ] [ seq( 1..4 ) ] [enter] returns [ 1, 2, 3, 4 ] list1b := range( 1, 8, 1.9 ) # specifying start and end-1, more step [enter] returns [ 1.0, 2.9, 4.8, 6.7 ] range( 1, 8, 1.9 ) is equivalent to [ seq( 1..7, 1.9 ) ] [ seq( 1..7, 1.9 ) ] [enter] returns [ 1.0, 2.9, 4.8, 6.7 ] range( 11, 1, -1 ) # negative ranges [enter] returns [ 11, 10, 9, 8, 7, 6, 5, 4, 3, 2 ] range( 11, 1, -1) is equivalent to [ seq( 11..2, -1 ) ] [ seq( 11..2, -1 ) ] [enter] returns [ 11, 10, 9, 8, 7, 6, 5, 4, 3, 2 ] # Case 2, define a list, with an input function list2 := [ k*k for k in range( 5 ) ] # create a list, for each element in the range, multiply it by itself, and add it as a new item in the list [enter] returns [ 0, 1, 4, 9, 16 ] [ k*k for k in range( 5 ) ] is equivalent to seq( j*j, j, 0, 4 ) # variable and domain separately or seq( j*j, j, [ 0, 1, 2, 3, 4 ] ) # explicit domain as a list or [ seq( j*j, j=0..4 ) ] # domain as an expression seq( j*j, j, 0, 4 ) [enter] returns [0,1,4,9,16] seq( j*j, j, [ 0, 1, 2, 3, 4 ] ) [enter] returns [0,1,4,9,16] [ seq( j*j, j=0..4 ) ] [enter] returns [0,1,4,9,16] # Case 3, define list with a symbolic sequence list2 := [ x^j for j in range( 4 ) ] [enter] returns [ 1, x, x^2, x^3 ] [ x^j for j in range( 4 ) ] is equivalent to seq( x^j, j, 0, 3 ) seq( x^j, j, 0, 3 ) [enter] returns [ 1, x, x^2, x^3 ] # Case 3a, define list with a symbolic constant list3a := [ t for c in range( 4 ) ] [enter] returns [ t, t, t, t ] [ t for c in range( 4 ) ] is equivalent to seq( t, 4 ) seq( t, 4 ) [enter] returns [ t, t, t, t ] # Case 3b, define list with a numeric constant list3b := [ -1 for c in range( 4 ) ] [enter] returns [ -1, -1, -1, -1 ] [ 1 for c in range( 4 ) ] is equivalent to seq( -1, 4 ) seq( -1, 4 ) [enter] returns [ -1, -1, -1, -1 ] # Case 4, define a list of list (matrix) Warning, to execute this instruction you must switch to linear input mode or not pretty print (known as algebraic mode) list4 := [ [ k, k + 2 ] for k in range ( 5 ) ] [enter] returns [[ 0, 2 ],[ 1, 3 ],[ 2, 4 ],[ 3, 5 ],[ 4, 6 ]] [ [k, k + 2] for k in range ( 5 ) ] is equivalent to seq( [ k, k + 2 ], k, 0, 4 ), or [ seq( [ k, k + 2 ], k=0..4 ) ] seq( [ k, k + 2 ], k, 0, 4 ) [enter] returns [[ 0,2 ], [ 1,3 ], [ 2,4 ], [ 3,5 ], [ 4,6 ]] Summary Code PHP Code: #cas testPythonSyntax_listComp()[enter] returns Quote:[0,1,2,3,4] Another Examples Multiply every part of a list by α and assign it to a new list. multiplied := [item*α for item in [X_, Y_, Z_] ] [enter] returns [X_*α,Y_*α,Z_*α] Let's show how easy you can convert lower case / upper case letters. [ j.lower() for j in [ "X", "Y", "Z" ] ] [enter] returns ["x","y","z"] [ j.upper() for j in [ "x", "y", "z" ] ] [enter] returns [ "X", "Y", "Z" ] Create a function and name it double: PHP Code: def double(x): If you now just print that function with a value in it, it should look like this: double(10) [enter] returns 20 We can easily use list comprehension on that function. [double(x) for x in range(10)] [enter] returns [ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 ] You can put in conditions (filter): [double(x) for x in range(10) if (x mod 2) == 0] [enter] returns [ 0, 4, 8, 12, 16 ] A practical example. A := set[ 2, 4, 5, 6 ]; B := set[ 1, 2, 3, 5 ] Find the following relation R = { ( b, a ) ∈ BxA / a ≤ 6 - 2*b } A_ := set[ 2, 4, 5, 6]; B_ := set[ 1, 2, 3, 5]; set[ j for j in B_*A_ if j[1]<=6-2*j[0] ]; [enter] returns [[1,2],[1,4],[2,2]] ^ Sorry for my bad English |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)