Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to raise every element of a vector to the power of every element of another vector?

I would like to raise a vector by ascending powers form 0 to 5:

import numpy as np

a = np.array([1, 2, 3]) # list of 11 components
b = np.array([0, 1, 2, 3, 4]) # power
c = np.power(a,b)

desired results are:

c = [[1**0, 1**1, 1**2, 1**3, 1**4], [2**0, 2**1, ...], ...]

I keep getting this error:

ValueError: operands could not be broadcast together with shapes (3,) (5,)
like image 744
Hamza Avatar asked Feb 17 '21 13:02

Hamza


People also ask

How do you raise a vector to a power in R?

This can be done by using ^ sign as we do in Excel. For example, if we have a vector x then the square of all values in x can be found as x^2.

How do you raise to a power in Numpy?

power() in Python. numpy. power(arr1, arr2, out = None, where = True, casting = 'same_kind', order = 'K', dtype = None) : Array element from first array is raised to the power of element from second element(all happens element-wise).

What does NP power do?

The np. power() is a mathematical library function used to return an array containing elements of the first array raised to the power element of the second array. The numpy power() function refers elements in the first array as a base and returns it to the power of the corresponding component of the second array.

How do you cube every element of a numpy array?

To cube each element in an array., element-wise, use the numpy. power() method in Python. Here, the 1st parameter is the base and the 2nd exponents. Since, we want the cube, the exponent is 3.


3 Answers

One solution will be to add a new dimension to your array a

c = a[:,None]**b

# Using broadcasting : 
# (3,1)**(4,) --> (3,4)
#
#     [[1],
# c =  [2], ** [0,1,2,3,4]
#      [3]]

For more information check the numpy broadcasting documentation

like image 187
obchardon Avatar answered Oct 19 '22 18:10

obchardon


Here's a solution:

num_of_powers = 5
num_of_components = 11

a = []
for i in range(1,num_of_components + 1):
    a.append(np.repeat(i,num_of_powers))
b = list(range(num_of_powers))
c = np.power(a,b)

The output c would look like:

array([[    1,     1,     1,     1,     1],
       [    1,     2,     4,     8,    16],
       [    1,     3,     9,    27,    81],
       [    1,     4,    16,    64,   256],
       [    1,     5,    25,   125,   625],
       [    1,     6,    36,   216,  1296],
       [    1,     7,    49,   343,  2401],
       [    1,     8,    64,   512,  4096],
       [    1,     9,    81,   729,  6561],
       [    1,    10,   100,  1000, 10000],
       [    1,    11,   121,  1331, 14641]], dtype=int32)

Your solution shows a broadcast error because as per the documentation:

If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output).

like image 34
Ishwar Venugopal Avatar answered Oct 19 '22 17:10

Ishwar Venugopal


c = [[x**y for y in b] for x in a]
like image 29
Aven Desta Avatar answered Oct 19 '22 19:10

Aven Desta