1 Problem

\[ \boldsymbol{A}=\begin{bmatrix}1&3\\2&4\end{bmatrix}, \boldsymbol{B}=\begin{bmatrix}1\\2\end{bmatrix} のとき,\boldsymbol{A}\cdot \boldsymbol{B}を求めよ。 \]

1.1 R

(A <- matrix(1:4, nrow = 2))
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
(B <- matrix(1:2, nrow = 2))
##      [,1]
## [1,]    1
## [2,]    2
A %*% B
##      [,1]
## [1,]    7
## [2,]   10

1.2 Python

import numpy as np

A = np.matrix([[1, 3], [2, 4]])
print(A)
## [[1 3]
##  [2 4]]
B = np.matrix([[1],[2]])
print(B)
## [[1]
##  [2]]
A.dot(B)
## matrix([[ 7],
##         [10]])

2 Problem

\[ \boldsymbol{A}=\begin{bmatrix}1&3\\2&4\end{bmatrix}, \boldsymbol{I}=\begin{bmatrix}1&0\\0&1\end{bmatrix} のとき,\boldsymbol{A}\cdot \boldsymbol{I}を求めよ。 \]

2.1 R

(A <- matrix(1:4, nrow = 2))
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
(I <- diag(nrow = 2))
##      [,1] [,2]
## [1,]    1    0
## [2,]    0    1
A %*% I
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4

2.2 Python

A = np.matrix([[1, 3], [2, 4]])
print(A)
## [[1 3]
##  [2 4]]
I = np.diag([1,1])
print(I)
## [[1 0]
##  [0 1]]
A.dot(I)
## matrix([[1, 3],
##         [2, 4]])

3 Problem

\[ \boldsymbol{A}=\begin{bmatrix}1&3\\2&4\end{bmatrix}, \boldsymbol{B}=\begin{bmatrix}1&3\\2&4\end{bmatrix} のとき,\boldsymbol{A}\cdot \boldsymbol{B}を求めよ。 \]

3.1 R

A <- matrix(1:4, nrow = 2)
B <- matrix(1:4, nrow = 2)
A %*% B
##      [,1] [,2]
## [1,]    7   15
## [2,]   10   22

次のように単に積記号「*」だと要素積(アダマール積)になるので注意する。 \[\boldsymbol{A}\otimes \boldsymbol{B}\]

A * B
##      [,1] [,2]
## [1,]    1    9
## [2,]    4   16

3.2 Python

A = np.matrix([[1, 3], [2, 4]])
B = np.matrix([[1, 3], [2, 4]])

A.dot(B)
## matrix([[ 7, 15],
##         [10, 22]])
#install.packages("matlib")
library(matlib)
# shows addition of vectors
theta <- seq(0, 2*pi, pi/10)
x <- cos(theta)
y <- sin(theta)
v <- rbind(x, y)
A <- matrix(c(2, 0, -2, 2), nrow = 2)
w <- A %*% v

# proper geometry requires asp=1
matplot(x, y, type = "o",
        xlim = c(-5, 5), ylim = c(-5, 5), 
        xlab="X", ylab = "Y")

matpoints(w[1, ], w[2, ], type = "o", 
          xlim = c(-5, 5), ylim = c(-5, 5), 
          xlab = "X", ylab = "Y")

abline(v = seq(-5, 5, 1),
       h = seq(-5, 5, 1), lty = 2, col = gray(0.2))