power() #
np.power(x, y)は \(x\) と \(y\)をブロードキャストして,要素ごとのべき乗を計算する.
np.power(2, 3) # 2の3乗出力
8x = np.array([1, 2, 3])
np.power(x, 2)出力
array([1, 4, 9])- スカラーの
2は \(x\)に合わせてブロードキャストされて,要素ごとに2乗される
x = np.array([1, 2, 3])
np.power(x, x) # 1**1, 2**2, 3**3出力
array([ 1, 4, 27])x = np.array([1, 2, 3])
np.power(2, x) # 2**1, 2**2, 2**3出力
array([2, 4, 8])- 第1引数のスカラーがブロードキャストされる
np.power(2, 3) # 2の3乗出力
8A = np.array([[1], [2], [3]]) # 3行1列の2次元配列
p = np.array([1, 2, 3]) # 1次元配列
np.power(A, p)出力
array([[ 1, 1, 1],
[ 2, 4, 8],
[ 3, 9, 27]])\(A\) のshapeは(3, 1),\(p\) のshapeは(3, )なのでブロードキャストによって
- A.shpae: \( (3, 1) \to (3, 1) \to (3, 3)\)
- p.shape: \( (3,) \to (1, 3) \to (3, 3) \)
のように形状が揃えられる.
$$ A= \begin{pmatrix} 1\\ 2\\ 3 \end{pmatrix} \rightarrow $$$$ A= \begin{pmatrix} 1\\ 2\\ 3 \end{pmatrix} \rightarrow \begin{pmatrix} 1 & 1 & 1\\ 2 & 2 & 2\\ 3 & 3 & 3 \end{pmatrix} $$$$ p= \begin{pmatrix} 1 & 2 & 3 \end{pmatrix} \rightarrow \begin{pmatrix} 1 & 2 & 3\\ 1 & 2 & 3\\ 1 & 2 & 3 \end{pmatrix} $$出力結果は,\(3 \times 3\) 行列の各要素ごとのべき乗になっている.