3

I'm trying to figure out the way to properly use map in python so that I can multi-thread my program by Pool.map. Basically I'm running into issues trying to understand how functional python works. I have:

import numpy as np

def maptest(foo,bars):
   print foo * bars

main():
   matA = np.eye(2)
   matB = np.eye(2)

   print map((lambda foo: maptest(foo, matB)), matA) 

This gives me an output of:

[[ 1.  0.]
 [ 0.  0.]]
[[ 0.  0.]
 [ 0.  1.]]
[None, None]

when the output I want is simply:

[[1. 0.]
 [0. 1.]]

What's going on with the map call I can? This is my first time using map and lambda. I've used lambdify with sympy, but that's all for my functionals experience. Thanks!

1 Answer 1

5

The [None, None] is coming from printing the map call (note that your maptest function prints!).

Now, the reason that it prints those multiple arrays is that you are mapping your function across all of mapA. mapA is actually a two-element array, and map applies your function to each element of the array. Hence, you print [1,0][[1,0][0,1]], then [0,1][[1,0][0,1]]. Instead of multiplying the matrices, you have made two multiplications, one for each element of mapA.

Sign up to request clarification or add additional context in comments.

5 Comments

This make A LOT more sense now. Thanks! I've taken out the print so the [None,None] doesn't show anymore, but how would I alter the map call to do this multiplication normally as opposed to multiplying each elements of matA?
You'll have to wrap matA in another iterable. A one-element tuple for instance: map((lambda foo: maptest(foo, matB)), (matA,)) or list [matA].
@sebastian Ah! So now if I wrap matA it works! But now if I change, say matA to [[0,1],[1,0]], I get a solution of [[0,1],[1,0]], where it should be [[0,0],[0,0]]. What's going on now?
No, that's correct. The identity matrix multiplied by any second matrix is that second matrix.
You're totally right. This was posted late at night, so I was just being dumb -- really just in need of sleep. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.