관리 메뉴

bright jazz music

선형대수 : 7. 행렬의 요소별 곱(아다마르 곱) 본문

Math/선형대수

선형대수 : 7. 행렬의 요소별 곱(아다마르 곱)

bright jazz music 2022. 7. 26. 08:39

행렬의 요소별 곱 (아다마르 곱)

  • 행렬의 요소별 곱은 행렬의 각 요소끼리 곱하는 것이다.
  • 아다마르 곱(Hadamard product)이라고도 불린다.
  • ○ 또는 ⊙로 표기한다.

 

두 행렬 A와 B가 아래와 같을 때

LaTeX 수식
$ \begin{align} A &=\begin{pmatrix} a_{11}&a_{12}&\cdots & a_{1n}\\ a_{21}&a_{22}&\cdots & a_{2n}\\ \vdots & \vdots & \ddots & \vdots \\ a_{m1}&a_{m2}&\cdots & a_{mn} \end{pmatrix}  \end{align}$
$ \begin{align} B &=\begin{pmatrix} b_{11} & b_{12}&\cdots & b_{1n}\\ b_{21}&b_{22}&\cdots & b_{2n}\\ \vdots & \vdots & \ddots & \vdots \\ b_{m1}&b_{m2}&\cdots & b_{mn} \end{pmatrix}\\ \end{align}$

 

두 행렬 A와 B의 아다마르 곱은 아래와 같다.

 

LaTeX 수식
$ \begin{align} A \circ B &=\begin{pmatrix} a_{11}b_{11} & a_{12}b_{12}&\cdots & a_{1n}b_{1n}\\ a_{21}b_{21}&a_{22}b_{22}&\cdots & a_{2n}b_{2n}\\ \vdots & \vdots & \ddots & \vdots \\ a_{m1}b_{m1} & a_{m2}b_{m2}&\cdots & a_{mn}b_{mn} \end{pmatrix}\\ \end{align}$
 

 

예시

 

 

LaTeX 수식
$ \begin{align} A &=\begin{pmatrix} 0 & 1& 2\\ 3 & 4 & 5 \\ 6 & 7 & 8 \end{pmatrix} \\ 
B &= \begin{pmatrix} 0 & 1& 2\\ 2 & 0 & 1 \\ 1 & 2 & 0 \end{pmatrix} \end{align}$
$ \begin{align} A \circ B &=\begin{pmatrix} 0 \times 0 & 1\times1& 2\times2\\ 3\times2 & 4\times0& 5\times1 \\ 6\times1 & 7\times2 & 8\times0 \end{pmatrix}\\ \\ &= \begin{pmatrix} 0 & 1 & 4\\6 & 0 & 5\\ 6 & 14 & 0 \end{pmatrix} \end{align}$

 

 

파이썬에서 요소별 곱(아다마르 곱) 구현하기

import numpy as np

a = np.array([[0, 1 , 2],
              [3, 4, 5],
              [6, 7, 8]])

b = np.array([[0, 1 , 2],
              [2, 0, 1],
              [1, 2, 0]])


print(a*b)

# [[ 0  1  4]
#  [ 6  0  5]
#  [ 6 14  0]]

 

 

 

실습:

#행렬 a와 b의 행렬곱, 행렬 c와 d의 요소별 곱을 구하라

import numpy as np

a = np.array([[0, 1, 2],
              [1, 2, 3]])

b = np.array([[0, 1],
              [1, 2],
              [2, 3]])

#행렬곱
print(np.dot(a, b))
print()

c = np.array([[0, 1, 2],
              [3, 4, 5],
              [6, 7, 8]])

d = np.array([[0, 2, 0],
              [2, 0, 2],
              [0, 2, 0]])

#요소별 곱
print(c*d)


# [[ 5  8]
#  [ 8 14]]
# 
# [[ 0  2  0]
#  [ 6  0 10]
#  [ 0 14  0]]

 

 

 

 

 

 

참고:

https://ko.wikipedia.org/wiki/%EC%95%84%EB%8B%A4%EB%A7%88%EB%A5%B4_%EA%B3%B1

 

 

 

 

 

 

 

 

 

 

Comments