In this post, I’ll continue writing R code to accompany linear algebra equations found in Linear Algebra: Step by Step, by Kuldeep Singh. For more information on the origins of these posts, see the first companion post

Thank to you everyone who hung out throughout the brief hiatus, while life got in the way of linear algebra.

## Section 2.1 – Properties of Vectors

A brief review of how dot products work, in the form of example 2.1

u <- c(-3,1,7,-5) v <- c(9,2,-4,1) u %*% v

To continue, we’re able to determine the norm (or length) of a vector using a simple chain of square root and sum. Example 2.4 is as follows

u <- c(-7,-2) v <- c(8,3) sqrt(sum(u^2)) sqrt(sum(v^2))

Example 2.5 shows more real-world applicability, by showing the distance between two points.

s1 <- c(1,2,3) s2 <- c(7,4,3) sqrt(sum((s1-s2)^2))

## Section 2.2 – Further Properties of Vectors

Let’s simplify the distance function. From here forward, use the following function as distance:

`d <- function(u) {sqrt(sum(u^2))}`

To show the use of d, here is example 2.6

u <- c(1,5) v <- c(4,1) u %*% v d(u)*d(v) d(u)+d(v) d(u+v)

For example 2.7, let’s first create an extension of the d function that helps find angles. **Remember** – R trigonometric functions use radians and not degrees!

costheta <- function(vec1,vec2) {vec1%*%vec2 / (d(vec1)*d(vec2))} rad.to.deg <- function(rad) { 180*rad / pi }

Then we can perform example 2.7 by embedding multiple functions. Feel free to break out as appropriate for your own understanding and simplicity if using elsewhere.

u <- c(-5,-1) v <- c(4,2) rad.to.deg(acos(costheta(u,v)))