hpPrime, with Python Syntax, beyond the Python numeric language
|
03-08-2019, 02:35 AM
(This post was last modified: 10-25-2019 08:36 PM by compsystems.)
Post: #5
|
|||
|
|||
RE: hpPrime, with Python Syntax, beyond the Python numeric language
List comprehension.
Is a compact expression to define lists. purge(x,j,k) # Remove content from variables python_compat(1) [enter] initializes the python syntax in xCAS and ** operator to calculate powers # Case 1, define a numerical sequence list1 := range( 5 ) # 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) 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 ]] PHP Code: #cas testPythonSyntax_listComp()[enter] returns [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]] ################################# Functions Functions are defined with the keyword def, followed by the name of the function and its parameters in (). Another way to define functions is with the keyword lambda (only for an expression). The value returned in the functions with def will be the one given with the return statement. using def: PHP Code: def sumxy(x, y = 2): "Done" Above function name is sumxy, it expects two arguments x and/or y and returns their sum. # Call the function sumxy(4) # The variable "y" takes the default value: 2 [enter] returns 6 sumxy(4, 10) # The variable "y" takes the value entered: 10 [enter] returns 14 Let’s see how we can convert the above function into a lambda function: PHP Code: sumxy = lambda x, y=2: x + y sumxy(4) [enter] returns 6 sumxy(4, 10) [enter] returns 14 PHP Code: #cas If we check type of add, it is a function. type(sumxy) [enter] returns func The functions can be passed as parameters to a new function which expects a function name as parameter like map cmd def multiply2(x): return x * 2 map(pow2, [1, 2, 3, 4]) [enter] returns [1,4,9,16] ############# Another example PHP Code: #cas calculator( "addition", 3, 4 ) [enter] returns 7 calculator( "subtraction", 3, 4 ) [enter] returns -1 calculator( "multiplication", 3, 4 ) [enter] returns 12 calculator( "division", 3, 4 ) [enter] returns 3/4~=0.75 to rewrite the code use python(name of the program) python( calculator ) [enter] returns "lambda op_rep,a,b: if ((""addition"")==op_rep) : return(a+b) else : if ((""subtraction"")==op_rep) : return(a-b) else : if ((""multiplication"")==op_rep) : return(a*b) else : if ((""division"")==op_rep) : return(a/b) else : return(None)" ########### ZIP vector1 = [1,2,3,4] vector2 = [7,8,3,2] DispG ClrIO printf("vector1 vector2") for array2d in zip(vector1,vector2): printf("%gen \t %gen", array2d[0], array2d[1]) vector1 := [1,2,3,4]; vector2 := [7,8,3,2] dict1 = table(zip(vector1,vector2)) all(iterable) Return True if in evaluating all elements of the iterable list/string are true (or if the iterable is empty). If not, it returns False. all function equivalent to: #cas def all( iterable ): for element_ in iterable: if not element_: return False return True #end True - If all elements in an iterable are true False - If any element in an iterable is false Truth table for all() When ----------------------------------- Return Value All values are true -------------------- True All values are false ------------------- False One value is true (others are false) --- False One value is false (others are true) --- False Empty Iterable ------------------------- True # Examples for list # all values true lst1 := [ -5.8, -1, 1, 3.5, 4, 10 ]; print( all( lst1 ) ) When executing the program, the output on the I/O line will be: 1 and in the console will be: True # all values false lst1 = [ 0, False, 0=-1 ] ; print( all( lst1 ) ) [enter] returns 1 False # one false value lst1 := [-1, 1, 0, abs(-1)==1 ] ; print( all( lst1 ) ) [enter] returns 1 False # one true value lst1 := [ 0, False, abs(-1)==1 ] ; print( all( lst1 ) ) [enter] returns 1 False # empty iterable lst1 = [] ; print( all( lst1 ) ) [enter] returns 1 True lst1:= [ 1, True, "×÷" ] print( all( lst1 ) ) [enter] Error, can not be determined if the element "×÷" is true or false # Examples on strings (NOT SUPPORTED) str1 := "This is string"; print( all( str1 ) ) [enter] returns Error # Examples on tables In case of tables data, if all the left part are numeric and true or the table is empty, all() returns True. Else, it returns false for all other cases.. tbl1 := table( 0: "", 65: "A", 97: "a", 215: "×", 247: "÷" ); print( all( tbl1 ) ) [enter] returns 1 False tbl1 := table( 65: "A", 97: "a", 215: "×", 247: "÷" ); print( all( tbl1 ) ) [enter] returns 1 True tbl1 := table() print( all( tbl1 ) ) [enter] returns 1 True tbl1 := table("1": one, "2": two) tbl1 := table("65": "A", "97": "a") tbl1 := { -1: False, 0: False }; print( all( tbl1 ) ) [enter] returns 1 Print False tbl1 := { -1: True, 1: True }; print( all( tbl1 ) ) [enter] returns 1 True s = {"000": 'True'} print(all(s)) Error lst1= [ 1, True, "×÷" ] print( all( lst1 ) ) |
|||
« Next Oldest | Next Newest »
|
Messages In This Thread |
hpPrime, with Python Syntax, beyond the Python numeric language - compsystems - 02-28-2019, 02:32 AM
RE: hpPrime with Python syntax - Giancarlo - 02-28-2019, 08:11 PM
RE: hpPrime with Python syntax, Beyond the Python language - compsystems - 03-03-2019, 05:31 PM
RE: hpPrime with Python syntax, Beyond the Python language - Giancarlo - 03-05-2019, 12:23 PM
RE: hpPrime, with Python Syntax, beyond the Python numeric language - compsystems - 03-08-2019 02:35 AM
RE: hpPrime, with Python Syntax, beyond the Python numeric language - compsystems - 04-04-2019, 01:33 AM
|
User(s) browsing this thread: 1 Guest(s)