This section is devoted to very important practical techniques how to extract submatrices and how to build matrices from smaller matrices. Although all presented examples and codes are applied to matrices from space 𝔽m×n with entries from field 𝔽 (which is either ℤ or ℚ or ℝ or ℂ), all these matrix operations can be extended for abstract matrices where entries could be arbitrary algebraic objects including lists.
,p>
Vectors play a fundamental roe in linear algebra since they are used to represent quantities of different algebraic structure. In R, vectors are essential data structures that facilitate various
mathematical operations. This section will cover basic to advanced manipulations with vectors,
providing clear explanations and R code examples.
Manipulations with vectors
Usually, an n-tuple (x₁, x₂, … , xn), written in parenthesis and a row vector [x₁, x₂, … , xn], written in brackets, look as the same object to human eyes. One of the pedagogical virtues of any software package is its requirement to pay close attention to the types of objects used in specific contexts. In particular, the computer algebra system Mathematica treats these two versions of a vector differently because
where x [bold] is a matrix and 𝔽m×n
is read “m by n” indicating dimensions of a (rows by columns) matrix, not m multiplied by n. This denotes the space of m × n matrices with coefficients from field 𝔽.
To create a vector in R, use the c() function to combine elements.
You can find the length (number of elements) of a vector using the length() function
In R, you can create column vectors and row vectors using the matrix() function.
Similar operations lead to a row vector:
Example 1:
You can convert a row vector into a column vector or vice versa using the t()
function. This operation is called transposition.
■
End of Example 1
You can scale a vector by multiplying it by a scalar
Adding two vectors element-wise is straightforward in R.
A zero vector has all elements as zero, and a one vector has all elements as one.
or
Two vectors are orthogonal if their dot product is zero.
Normalization is the process of scaling a vector so that it has a length of 1. This is
done by dividing each element of the vector by its magnitude (or norm).
You can extract specific elements or subvectors from a vector using indexing.
Now we present some examples with randomly generated entries.
Example 2: R provides functions to create vectors with randomly generated entities. Here’s an example
using runif() to generate random numbers between 0 and 1.
Random complex numbers are also available
Real and Imaginary may be separately accessed
■
End of Example 2
Another practical example:
Example 3:
For three given vectors,
Clear[x, y, z]
x = {1, 2, 3};
y = {{1, 2, 3}};
z = {{1}, {2}, {3}};
Extracting entries from vectors:
x[[1]] (* extracting the first component *)
1
y[[1]]
{1, 2}
y[[1, 2]] (* extracting the first component *)
2
y[[1, 1 ;; 2]]
{1, 2}
z[[1]]
{1}
z[[2, 1]]
2
Let us randomly generate a 9-tuple:
x = RandomInteger[{0, 9}, 9]
{6, 8, 0, 5, 6, 2, 8, 3, 5}
Extracting a subvector with even indices may be created in several ways
building subvector with even or odd entries and vector with third values as [1, 3, 6, 9, ..]
extract subvesto of the first left part
extract subvector of right part
■
End of Example 3
reverse order of entries in a matrix
Example 4:
■
End of Example 4
Example 5:
Vectors can represent stock prices in a portfolio. You can calculate the overall value of a stock
portfolio and analyze the contribution of each stock.
■
End of Example 5
Example 6:
Vectors represent forces acting on an object. Calculate the net force acting on an object by
summing up individual force vectors.
■
End of Example 6
We can visualize two dimensional vectors with R:
We can multiply vectors, but the output depends on the order of the vectors and their dimensions. There are known three (actually, there are more, but we consider only three the most popular ones) types of products. We start with the outer product of two vectors of arbitrary dimensions. Since the outer product of two vectors is a rectangular matrix, it does not matter to what space these vectors belong. However, it is convenient to consider one vector as a row, but another as a column.
The outer product (also known as tensor product) of two vectors x ∈ ℝm×1 and y ∈ ℝ1×n is their matrix product whose entries are all products of an element in the first vector with an element from the second vector:
We can ask for a particular entry using the convention {r,c} to identify the row and column where the entry resides. Here is the second entry in the second row of our matrix
m[[2, 2]]
6
Going the other way, we can ask Mathematica to tell us where that element is in our matrix
Position[m, 6]
(2, 2)
We often need the elements in the diagonal of a matrix. First, we build a square matrix by using only the first three columns (matching the number of rows) of our sample matrix
Notice this is the same as the diagonal of our original matrix because Mathematica starts at the upper left and ignores columns beyond what it needs for a Diagonal of the leftmost square portion of the matrix
Multiply row 2 with 3:
To multiply row 2 of the original matrix by 3 multiply the matrix by a vector with 1 as the first and third element and 2 as the second element
Multiply column 1 with 4:
Similarly, if you wish to multiply a single column by a scalar, leaving all other columns unchanged, here is column 1 of the original matrix times 4
A sparse matrix, or sparse array, is a matrix in which most elements are zero. A Band-Diagonal System generalizes the Tri-Diagonal condition, having non zero elements in a diagonal band and zero elements elsewhere.
Just as diagonals need not be in the absolute middle, triangular matrices need not be square and may be strictly or partially triangular, sometimes known as tri-diagonal
In linear algebra, the Crout matrix decomposition is an LU decomposition which decomposes a matrix into a lower triangular matrix (L), an upper triangular matrix (U), and, although not always needed, a permutation matrix (P). It was developed by the American mathematician Prescott Durand Crout (1907--1984).
The Crout matrix decomposition algorithm differs slightly from the Doolittle method. Doolittle's method returns a unit lower triangular matrix and an upper triangular matrix, while the Crout method returns a lower triangular matrix and a unit upper triangular matrix.
The built-in Mathematic function LUDecomposition interchanges rows prior to decomposition to provide numerical stability; we demonstrate this function in a special section.
■
End of Example 9
Query
Axier, S., Linear Algebra Done Right. Undergraduate Texts in Mathematics (3rd ed.). Springer. 2015, ISBN 978-3-319-11079-0.