Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"cross product" but raise to exponent instead of multiply

I have two vectors. I would like a "cross product"-esque function that will take each value from the first vector and raise it to the exponent of each value in a second vector, returning a matrix. Is there anything built in to numpy that does this? It could be done with loops but I'm looking for something efficient.

For example:

>>> cross_exp([1,2], [3,4]) 
[[1, 1],[8, 16]]
like image 892
user3439329 Avatar asked Sep 16 '15 15:09

user3439329


1 Answers

It sounds like you might want np.power.outer:

>>> np.power.outer([1,2], [3,4])
array([[ 1,  1],
       [ 8, 16]])

Most ufuncs have an outer method which computes the result of the operation on all pairs of values from two arrays (note this is different to the cross product).

like image 124
Alex Riley Avatar answered Oct 09 '22 00:10

Alex Riley