返回

Chapter_02.R

6.4 KB · R · 2026-06-07 09:06
rm(list = ls(all = TRUE)) # 清空当前环境
graphics.off() # 清空绘图区域
################################ Inverse Matrix ################################
hilbert = function(n) { i = 1:n; 1 / outer(i - 1, i, `+`) }
h8 = hilbert(8)
h8
inverse_h8 = solve(h8)
inverse_h8
round(inverse_h8 %*% h8, 3)

################################ Generalized Inverse Matrix ################################
library(MASS)
g_inverse_h8 = ginv(h8)
g_inverse_h8
round(h8 %*% g_inverse_h8 %*% h8, 5)
round(h8, 5)

x = c(1, 0, 0, 0)
A = matrix(x, nrow=2)
A
g_inverse_A = ginv(A)
g_inverse_A
A %*% g_inverse_A %*% A

################################ Eigenvalue and Eigenvector ################################
x = c(1, 0, 0, 0, 1/2, 1/2, 0, 1/2, 1/2)
A = matrix(x, nrow=3, byrow = TRUE)
A
A %*% A
options(digits = 4)
eigen(A)

################################ Spectral Decmoposition ################################
x <- c(1, 0, 0, 0)
A <- matrix(x, nrow=2)
A
g_inverse_A <- ginv(A)
g_inverse_A
A %*% g_inverse_A %*% A

y <- c(1, 0, 0, 8)
B <- matrix(y, nrow=2)
B
A %*% B %*% A

################################ Distance ################################
library(latex2exp)
x <- c(-0.2, 2.5)
y <- c(-0.2, 2)
plot(x, y, type = 'n', frame.plot = FALSE, xlab = '', ylab = '', axes = FALSE, asp = 1)
arrows(0, 0, 2.5, 0, code = 2, lwd=2, length = 0.15)
arrows(0, 0, 0, 2, code = 2, lwd=2, length = 0.15)
arrows(0, 0, 1, 1.5, code = 2, lwd=2, length = 0.15)
arrows(0, 0, 2, 1, code = 2, lwd=2, length = 0.15)
lines(c(2, 2), c(0, 1), lty = 2)
lines(c(1, 1), c(0, 1.5), lty = 2)
lines(c(0, 1), c(1.5, 1.5), lty = 2)
lines(c(0, 2), c(1, 1), lty = 2)
lines(c(1, 2), c(1.5, 1), lwd = 1.5)
text(1, -0.1, TeX("$x_1$"), cex = 1.5)
text(2, -0.1, TeX("$y_1$"), cex = 1.5)
text(-0.1, 1.5, TeX("$x_2$"), cex = 1.5)
text(-0.1, 1, TeX("$y_2$"), cex = 1.5)
text(1, 1.55, TeX("$x$"), cex = 1.5)
text(2.05, 1, TeX("$y$"), cex = 1.5)
text(1.5, 1.35, TeX("$d$"), cex = 1.5)
text(2, 1.8, TeX("$p=2$"), cex = 1.5)

# ----------------------------- Identity Metric Iso-distance Curve -----------------------------
library(plotrix)
plot(0, 0, xlim = c(-0.2, 6), ylim = c(-0.2, 6), type = 'n', frame.plot = FALSE, asp = 1,
     xlab = '', ylab = '', axes = FALSE)
draw.circle(4, 3, 1)
arrows(0, 0, 6, 0, code = 2, lwd=2, length = 0.15)
arrows(0, 0, 0, 5, code = 2, lwd=2, length = 0.15)
lines(c(4, 4), c(0, 3), lty = 2)
lines(c(0, 4), c(3, 3), lty = 2)
arrows(4, 3, 4 - sqrt(2)/2, 3 + sqrt(2)/2, code = 2, lwd=2, length = 0.1)
text(4, -0.15, TeX("$x_{1_0}$"), cex = 1.3)
text(-0.2, 3, TeX("$x_{2_0}$"), cex = 1.3)
text(4.2, 3, TeX("$x_0$"), cex = 1.3)
text(4.6, 2, TeX("$E_d$"), cex = 1.3)
text(3.8, 3.4, TeX("$d$"), cex = 1.3)

# ----------------------------- General Metric Iso-distance Curve -----------------------------
plot(0, 0, xlim = c(-0.2, 6), ylim = c(-0.2, 6), type = 'n', frame.plot = FALSE, asp = 1,
     xlab = '', ylab = '', axes = FALSE)
draw.ellipse(4, 3, a = 1.2, b = 2, angle = 45, lwd = 1.5)
arrows(0, 0, 6, 0, code = 2, lwd=2, length = 0.15)
arrows(0, 0, 0, 5, code = 2, lwd=2, length = 0.15)
lines(c(4, 4), c(0, 3), lty = 2)
lines(c(0, 4), c(3, 3), lty = 2)
arrows(4, 3, 4.85, 3.85, code = 2, lwd = 1.5, length = 0.1)
arrows(4, 3, 4 - 1.4, 3 + 1.4, code = 2, lwd = 1.5, length = 0.1)
arrows(0, 0, 0.7, 0.7, code = 2, lwd = 1.5, length = 0.1)
arrows(0, 0, -0.7, 0.7, code = 2, lwd = 1.5, length = 0.1)
text(4, -0.15, TeX("$x_{1_0}$"))
text(-0.15, 3, TeX("$x_{2_0}$"))
text(4.2, 3, TeX("$x_0$"))
text(4.6, 3.3, TeX("$\\frac{d}{\\sqrt{\\lambda_1}}$"))
text(3.5, 3.9, TeX("$\\frac{d}{\\sqrt{\\lambda_2}}$"))
text(5.5, 1.4, TeX("$E_d$"))
text(0.8, 0.8, TeX("$\\gamma_1$"))
text(-0.8, 0.8, TeX("$\\gamma_2$"))

# ----------------------------- Rectangle Surrounding the Ellipsoid -----------------------------
plot(0, 0, xlim = c(-0.2, 6), ylim = c(-0.2, 6), type = 'n', frame.plot = FALSE, asp = 1,
     xlab = '', ylab = '', axes = FALSE)
draw.ellipse(4, 3, a = 1.2, b = 2, angle = 45, lwd = 1.5)
arrows(0, 0, 6, 0, code = 2, lwd=2, length = 0.15)
arrows(0, 0, 0, 5, code = 2, lwd=2, length = 0.15)
lines(c(4, 4), c(0, 3), lty = 2)
lines(c(0, 4), c(3, 3), lty = 2)
lines(c(2.33, 5.66), c(1.33, 1.33), col = 'red', lwd = 2)
lines(c(2.33, 5.66), c(4.66, 4.66), col = 'red', lwd = 2)
lines(c(2.33, 2.33), c(1.33, 4.66), col = 'red', lwd = 2)
lines(c(5.66, 5.66), c(1.33, 4.66), col = 'red', lwd = 2)
text(4.2, 3, TeX("$x_0$"))

################################ Fig 2.4 ################################
plot(0, 0, xlim = c(-0.2, 2), ylim = c(-0.2, 2), type = 'n', frame.plot = FALSE, asp = 1,
     xlab = '', ylab = '', axes = FALSE)
arrows(0, 0, 2, 0, code = 2, lwd=2, length = 0.15)
arrows(0, 0, 0, 2, code = 2, lwd=2, length = 0.15)
arrows(0, 0, 1.4, 0.6, code = 2, lwd=2, length = 0.15)
arrows(0, 0, 1, 1.3, code = 2, lwd=2, length = 0.15)
text(1.03, 1.33, TeX("$x$"))
text(1.43, 0.63, TeX("$y$"))
curve(sqrt(1-x^2), 0.61, 1, add=TRUE, lwd=2, col=2)
curve(sqrt(9/16-x^2), 2.75/4, 3/4, add=TRUE, lwd=2)
curve(sqrt(0.875^2-x^2), 0.53, 0.805, add=TRUE, lwd=2)
text(1.15, 0.25, TeX("$\\theta_1 = \\theta + \\theta_2$"))
text(0.65, 0.15, TeX("$\\theta_2$"))
text(0.65, 0.5, TeX("$\\theta$"))

################################ Fig 2.5 ################################
plot(0, 0, xlim = c(-0.1, 1.1), ylim = c(-0.1, 1.1), type = 'n', frame.plot = FALSE, asp = 1,
     xlab = '', ylab = '', axes = FALSE)
arrows(0, 0, 1, 0, code = 2, lwd = 2, length = 0.15)
arrows(0, 0, 0.65, 0.45, code = 2, lwd = 2, length = 0.15)
lines(c(0.65, 0.65), c(0, 0.45), lwd = 2, col = 'blue')
arrows(0, 0, 0.65, 0, code = 2, lwd=2, length = 0.15)
text(0.68, 0.45, TeX("$x$"))
text(1.02, 0, TeX("$y$"))
text(0.65, -0.03, TeX("$p_x$"))
text(0.1, 0.03, TeX("$\\theta$"))
lines(c(0.65, 0.6, 0.6), c(0.05, 0.05, 0))

################################ Rotation ################################
x <- c(-0.1, 2)
y <- c(-0.1, 2)
plot(x, y, type = 'n', frame.plot = FALSE, xlab = '', ylab = '', axes = FALSE, asp = 1)
arrows(0, 0, 1.5, 0, code = 2, lwd=2, length = 0.15)
arrows(0, 0, 0, 1.5, code = 2, lwd=2, length = 0.15)
arrows(0, 0, 1, 1, code = 2, lwd=2, length = 0.15)
text(1.05, 1.05, TeX("$x$"))
arrows(0, 0, 1.31, 0.54, code = 2, lwd=2, length = 0.15, col = 'red')
text(1.36, 0.55, TeX("$y$"))
curve(sqrt(1/16-x^2), 0.175, 0.23, add=TRUE, lwd=2, col=2)
text(0.24, 0.16, TeX("$\\theta$"))
arrows(0, 0, 0.54, 1.31, code = 2, lwd=2, length = 0.15, col = 'blue')
text(0.55, 1.36, TeX("$z$"))
curve(sqrt(9/100-x^2), 0.12, 0.21, add=TRUE, lwd=2, col = 'blue')
text(0.19, 0.28, TeX("$\\theta$"))