The following warnings occurred:
Warning [2] count(): Parameter must be an array or an object that implements Countable - Line: 795 - File: showthread.php PHP 7.4.33 (FreeBSD)
File Line Function
/showthread.php 795 errorHandler->error





Post Reply 
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
[enter] initializes the python syntax in the hp-prime and the ** operator to calculate powers.

The use of list comprehensions is to simplify a set of sentences for example

PHP Code:
squares := [];
for 
x in range10 ): 
  
squares.appendx**

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**for x in range10 )  ] 
[enter] returns [ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 ]


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
# The "range" function returns a list, starting at 0 and ending with the indicated number minus one, increase in 1
[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
def testPythonSyntax_listComp():
    
python_compat(True)
    
index := 0
    purge
(xjkct)

    
# Case 1, define a numerical sequence 
    
list1 := range# The "range" function returns a list, starting at 0 and ending with the indicated number minus one, increase in 1
    
print ( list1 )
    print ( [ 
seq0..4 ) ] ) # equivalent function
    
print ( " " )
        
    
list1a := range1# specifying start and end-1
    
print ( list1a )
    print ( [ 
seq1..4 ) ] ) # equivalent function  
    
print ( " " )
   
    
list1b := range181.9 # specifying start and end-1, more step
    
print ( list1b )    
    print ( [ 
seq1..71.9 ) ] ) # equivalent function
    
print ( " " )
   
    
list1c := range111, -1# negative ranges
    
print ( list1c )    
    print ( [ 
seq11..2, -) ] ) # equivalent function
    
print ( " " )
     
    
   
# Case 2, define a list, with an input function
    
list2 := [ k*for k in range) ] # create a list, for each element in the range, multiply it by itself, and add it as a new item in the list 
    
print ( list2 
    
# equivalent functions  
    
print ( seqj*jj0) ) # variable and domain separately
    
print ( seqj*jj, [ 0123] ) ) # explicit domain as a list
    
print ( [ seqj*jj=0..4 ) ] ) # domain as an expression
    
print ( " " )
     
    
    
# Case 3, define list with a symbolic sequence 
    
list3 := [ x^for j in range) ]
    print ( 
list3 )
    print ( 
seqx^jj0) ) # equivalent function      
    
print ( " " )
     

    
# Case 3a, define list with a symbolic constant
    
list3a := [ for c in range) ]  
    print ( 
list3a )    
    print ( 
seqt) ) # equivalent function 
    
print ( " " )
   
  
    
# Case 3b, define list with a numeric constant 
    
list3b := [ -for c in range) ]   
    print ( 
list3b )    
    print ( 
seq( -1) ) # equivalent function 
    
print ( " " )
     
    
    
# Case 4, define a list of list (matrix)    
    
list4 := [ [ k] for k in range ) ]
    print ( 
list4 )    
    print ( 
seq( [ k], k0) ) # equivalent function 
       
    
return Done
#end 

testPythonSyntax_listComp()[enter] returns

Quote:[0,1,2,3,4]
[0,1,2,3,4]

[1,2,3,4]
[1,2,3,4]

[1.0,2.9,4.8,6.7]
[1,2.9,4.8,6.7]

[11,10,9,8,7,6,5,4,3,2]
[11,10,9,8,7,6,5,4,3,2]

[0,1,4,9,16]
[0,1,4,9,16]
[0,1,4,9,16]
[0,1,4,9,16]

[1,x,x**2,x**3]
[1,x,x**2,x**3]

[t,t,t,t]
[t,t,t,t]

[-1,-1,-1,-1]
[-1,-1,-1,-1]

[[0,2],[1,3],[2,4],[3,5],[4,6]]
[[0,2],[1,3],[2,4],[3,5],[4,6]]


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):
  return 
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
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Testing Python syntax. HP-Prime Firmware Oct-2019 - compsystems - 10-25-2019 08:52 PM



User(s) browsing this thread: 1 Guest(s)