library(dplyr)
##
## Attaching package: 'dplyr'
##
## The following objects are masked from 'package:stats':
##
## filter, lag
##
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(MASS)
##
## Attaching package: 'MASS'
##
## The following object is masked from 'package:dplyr':
##
## select
### I think your cross-product is just fine.
### I am going to code an alternate just for fun.
### Doing it not because it's better, but because it algorithmically
### follows how determinants are calculated, and follows the trick
### where you can calculate the vector product by symbolically evaluating the
### following expression:
# [ihat jhat khat]
# a x b = det( [ a1 a2 a3])
# [ b1 b2 b3]
#
### There is also this from stackoverflow:
### http://stackoverflow.com/questions/15162741/what-is-rs-crossproduct-function
vprod <- function(x, y) {
### Stop if we don't have 3D vectors
if(!(length(x) == 3 & length(y) == 3)) stop("Argument vectors must both be of length 3.")
### Will do computation by expansion along minors with cofactors.
### Make a matrix that I can use to do this computation
mlower <- matrix(c(x, y), nrow=2, byrow=TRUE) # 2x3 matrix of the argument vectors, used in calculating the minors
matrix_minors <- lapply(1:3, function(i) det(mlower[,-i])) # list of the minors, expanding across the first row
cofactor_multiplier <- c(1, -1, 1) # Used to multiply by the minors to get the cofactor
crossp <- cofactor_multiplier * unlist(matrix_minors)
### Return the computation
return(crossp)
}
#### Vector norm (real and complex)
#### Note that technically 'Re' isn't necesarry, as v * Conj(v) should be sufficient
#### but once it thinks we are dealing with complex numbers, it carrys along the imaginary vector component,
#### type, but with 0i as the imaginary part. This just explicitly gets rid of the 0i part.
#vnorm <- function(v) {norm(as.matrix(v), "f")} # First attempt using core functions, but doesn't handle complex well.
#vnorm <- function(v) {sqrt(Re(sum(v * Conj(v))))} # Second attemp, works
vnorm <- function(v) {as.vector(sqrt(Re(crossprod(v, Conj(v)))))} # Use inner product definition to compute
#### Vector distance (Euclidian)
#### Euclidian distance between two vectors
#vdist <- function(u, v) {as.vector(dist(matrix(c(u, v), nrow=2, byrow=TRUE)))} # First attempt, forgetting the easy dist definition as norm of vec diff
vdist <- function(u, v) {vnorm(u - v)}
#### Vector projection of u onto v
vproj <- function(u, v) {crossprod(u, v) / vnorm(v)^2 * v}
#### Setup
u <- c(1, -2, 4)
v <- c(3, 5, 1)
w <- c(2, 1, -3)
#### a)
3 * u - 2 * v
## [1] -3 -16 10
#### b)
5 * u + 3 * v - 4 * w
## [1] 6 1 35
#### c)
crossprod(u, v)
## [,1]
## [1,] -3
crossprod(u, w)
## [,1]
## [1,] -12
crossprod(v, w)
## [,1]
## [1,] 8
#### d)
vnorm(u)
## [1] 4.582576
vnorm(v)
## [1] 5.91608
#### d)
costheta <- crossprod(u, v) / vnorm(u) / vnorm(v)
costheta
## [,1]
## [1,] -0.1106567
#### e)
vdist(u, v)
## [1] 7.874008
Equation: \[\left[ \begin{array}{c} x \\ y + 1 \\ y + z \end{array} \right] = \left[ \begin{array}{c} 2 x + y \\ 4 \\ 3 z \end{array} \right]\]
Easy to see from row 2 that \(y + 1 = 4\) implies \(y = 3\). Then, from row 1, \(x = -y\), so \(x = -3\), and in row 3, \(z = \frac{y}{2}\), or \(z = \frac{3}{2}\).
Write \(v\) as a linear combination of \(u_1\), \(u_2\), and \(u_3\).
Note that book has error. They say answer is: \(\left[ \begin{array}{c} 3 & -1 & 2 \end{array} \right]\), but that only works if \(v = \left[ \begin{array}{c} 9 & 0 & 16 \end{array} \right]\)
Doing this as a demonstration of linear solver and mapping the problem to that domain.
See: http://stackoverflow.com/questions/8145694/solving-simultaneous-equations-with-r
### Answer with values given in book
v <- c(9, -3, 16)
u1 <- c(1, 3, 3)
u2 <- c(2, 5, -1)
u3 <- c(4, -2, 3)
m <- matrix(c(u1, u2, u3), ncol=3)
#solve(m, v)
ans <- fractions(solve(m, v))
ans
## [1] 237/89 -116/89 199/89
m %*% ans
## [,1]
## [1,] 9
## [2,] -3
## [3,] 16
### Answer with changed values for v
v <- c(9, 0, 16)
u1 <- c(1, 3, 3)
u2 <- c(2, 5, -1)
u3 <- c(4, -2, 3)
m <- matrix(c(u1, u2, u3), ncol=3)
#solve(m, v)
ans <- fractions(solve(m, v))
ans
## [1] 3 -1 2
m %*% ans
## [,1]
## [1,] 9
## [2,] 0
## [3,] 16