## MAP function

Messages in english

compsystems
Messages : 540
Inscription : sam. févr. 04, 2017 11:34 pm
Localisation : Colombia
Contact :

### MAP function

Hello, I am trying to apply a function of two parameters to a vector or matrices

Code : Tout sélectionner

``````def multiply2(x):
return x * 2``````
map(multiply2, [1, 2, 3, 4]) [enter] [2,4,6,8] # ok

but

Code : Tout sélectionner

``````def sumxy(x,y):
return x+y``````
map(sumxy, [1, 2, 3, 4]) [enter] Output expected: [3, 6, 10]

or

map(sumxy, [1, 2], [3, 4]) [enter] Output expected: [4, 6]

##

list_a = [1, 2, 3]
list_b = [10, 20, 30]

map(lambda x, y: x + y, list_a, list_b) [enter] Output expected: [11, 22, 33]

parisse
Messages : 5346
Inscription : mar. déc. 20, 2005 4:02 pm
Contact :

### Re: MAP function

map expects a unary function, not a binary function. The commandname is zip for binary function.

compsystems
Messages : 540
Inscription : sam. févr. 04, 2017 11:34 pm
Localisation : Colombia
Contact :

### Re: MAP function

Hi, BP.

for compatibility to python it is very important that map is expanded to two or more function arguments.

1 argument for the map.
#####
def multiply2(x):
return x * 2

objects_list = map(multiply2, [1, 2, 3, 4])
print( list( objects_list )) # Xcas and Python returns > [ 2, 4, 6, 8 ]
#####

#####
objects_list = map(lambda x : x*2, [1, 2, 3, 4])
print(list( objects_list )) # Xcas/Python returns > [ 2, 4, 6, 8 ]
#####

2 argument for the map.
#####
list_a = [ 1, 2, 3 ]
list_b = [ 10, 20, 30 ]

objects_list = map( lambda x, y: x + y, list_a, list_b )
print(list( objects_list)) # Python returns > [ 11, 22, 33 ]
#####

3:
#####
list_a = [ 1, 2, 3 ]
list_b = [ 10, 20, 30 ]
list_c = [ -1, 1, -1 ]

objects_list = map( lambda x, y, z: (x + y) * z, list_a, list_b, list_c )
print(list( objects_list)) # Python returns > [ -11, 22, -33 ]
XCAS returns > error

compsystems
Messages : 540
Inscription : sam. févr. 04, 2017 11:34 pm
Localisation : Colombia
Contact :

### Re: MAP function

Thanks BP, now map works with functions of more than two arguments, but when iterating over a set, the set prefix is lost, please improve this case.

Code : Tout sélectionner

``````map( normal, set( [ (x-1)*(x+1) , x² - 1 ] )  ) // returns [x^2-1,x^2-1] should return set[x^2-1, x^2-1]
eval( set[x^2-1, x^2-1]) -> set[x^2-1]
``````
also if the argument is a matrix of a row, work in pairs

Code : Tout sélectionner

``````sumxy( x, y ):= return x + y;
map( sumxy, [[ 1, 2, 3, 4 ]], matrix ) //-> [[ 1+2->3, 3+4 ]] -> [[3, 7]]
map( sumxy, matrix[[ 1, 2, 3, 4 ]] ) /
``````
Xcas session
https://www.eonicasys.com/CAS/xcas/casc ... as_eng.xws
/* map cmd */
autosimplify( false )
multiply2( x ):= return x * 2
map( multiply2, [ 1, 2, 3, 4 ] )
sumxy( x, y ):= return x + y
map( sumxy, [ 1, 2 ], [ 3, 4 ] )
map( sumxy, [[ 1, 2, 3, 4 ]], matrix ) //-> [[ 1+2->3, 3+4 ]] -> [[3, 7]]
list_a := [ 1, 2, 3 ];
list_b := [ 10, 20, 30 ]
map( lambda x, y: x + y, list_a, list_b )
list_c := [ -1, 1, -1 ]
map( lambda x, y, z: (x + y) * z, list_a, list_b, list_c )
solve( x^3-6*x^2+11*x = 6, x, '=' )
subst( x^3-6*x^2+11*x = 6, set[ x=1, x=2, x=3 ] )
convert( set[ x=1, x=2, x=3 ], list )
map( lambda x : subst( x^3-6*x^2+11*x = 6, x), [ x=1, x=2, x=3 ] )
map( lambda x : evalb( subst( x^3-6*x^2+11*x = 6, x)), [ x=1, x=2, x=3 ] )
map( x -> x ≥ -1 ,[ -5, -3, -1, 1 ] )
apply(x -> x >= -1,[ -5, -3, -1, 1 ] )
map(lambda x: x ≥ -1 ,[ -5, -3, -1, 1 ] )
map( propfrac, [5/3, 18229/94, 53/12 ] ) // mixed number