Search code examples
pythonnumpymatrixcross-product

"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]]

Solution

  • 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).