Vectors in Linear Algebra

Michael Taylor

2018/08/15

knitr::opts_chunk$set(cache=TRUE)

A two dimensional arrays in an algebra class.

\[A = \left( \begin{array}{c c c} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{array} \right)\]

(A = matrix(c(1,2,3,4,5,6,7,8,9), nrow = 3, byrow = T))
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9

Now, a (column) vector can be entered as:

(a = c(1, 4, 7))
## [1] 1 4 7

The vector a is in R printed “in row format” but can really be regarded as a column vector.

laff_copy <- function(x, y){
  m_x <- dim(x)[1]
  n_x <- dim(x)[2]
  m_y <- dim(y)[1]
  n_y <- dim(y)[2]
  
  if ((m_x != 1 && n_x != 1) | (m_y != 1 && n_y !=1))
  {
    return(print('FAILED'))
    } 
  
  if (length(x) !=length(y)  )
  {
    return(print('FAILED2'))
    }
  
  y_out = matrix(rep(0,m_x))
  
  if (n_x == 1) # if x is a column vector
    {
    if (n_y == 1) # if y is a column vector
      {
      # Copy the elements of x into the elements of y
      for (i in 1:m_x )
        y_out[i, 1] = x[i, 1]
      return(y_out)
      }
    else # y is row vector
      {
        for (i in 1:m_x )
          y_out[1, i] = x[i, 1]
        return(y_out)
      }
    } 
  else # x is a row vector
    {
    if (n_y == 1) # y is a column vector
      # Copy the elements of x into the elements of y
      {
      for (i in 1:n_x )
        y_out[i, 1] = x[1, i]
      return(y_out)
    } 
      else
      {
        for (i in 1:n_x)
          y_out[1, i] = x[1, i]  
      }
  }
  return(y_out)
  }
dump("laff_copy", file = "laff_copy.R")
x <- matrix(c(1, 2, 3, 4))
y <- matrix(c(0, -1, -2, -3))
x_old <- x
y_old <- y
laff_scale <- function(alpha, x){
  m_x <- dim(x)[1]
  n_x <- dim(x)[2]
  
  is.scalar <- function(x) length(x) == 1L && is.vector(x, mode = "numeric")
  
  if (!is.scalar(alpha) )
  {
    return(print('FAILED'))
    } 
  
  x_out = matrix(rep(0,m_x))
  
  if (n_x == 1) # if x is a column vector
  {
    for (i in 1:m_x)
      x_out[i, 1] = alpha * x[i, 1]
  }else{
    for (i in 1:n_x)
      x_out[i, 1] = alpha * x[i, 1]
  }
  return(x_out)
  }
dump("laff_scale", file = "laff_scale.R")
laff_scale(10,y)
##      [,1]
## [1,]    0
## [2,]  -10
## [3,]  -20
## [4,]  -30
sessionInfo()
## R version 3.5.1 (2018-07-02)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 18.04.1 LTS
## 
## Matrix products: default
## BLAS: /home/michael/anaconda3/lib/R/lib/libRblas.so
## LAPACK: /home/michael/anaconda3/lib/R/lib/libRlapack.so
## 
## locale:
##  [1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_CA.UTF-8        LC_COLLATE=en_CA.UTF-8    
##  [5] LC_MONETARY=en_CA.UTF-8    LC_MESSAGES=en_CA.UTF-8   
##  [7] LC_PAPER=en_CA.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] RevoUtils_11.0.1     RevoUtilsMath_11.0.0
## 
## loaded via a namespace (and not attached):
##  [1] bookdown_0.7     Rcpp_0.12.18     codetools_0.2-15 digest_0.6.15   
##  [5] rprojroot_1.3-2  backports_1.1.2  magrittr_1.5     evaluate_0.11   
##  [9] blogdown_0.9.8   stringi_1.2.4    rmarkdown_1.10   tools_3.5.1     
## [13] stringr_1.3.1    xfun_0.4.11      yaml_2.2.0       compiler_3.5.1  
## [17] htmltools_0.3.6  knitr_1.20

References

knitr::write_bib(.packages(), "packages.bib") 

R Core Team. 2018. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.