Sym
objectsvignettes/sym-matrix-vector.Rmd
sym-matrix-vector.Rmd
Define a character matrix (covariance matrix from a certain \(AR(1)\)):
## Yacas matrix:
## [,1] [,2] [,3] [,4]
## [1,] 1 0 0 0
## [2,] -alpha 1 0 0
## [3,] 0 -alpha 1 0
## [4,] 0 0 -alpha 1
Now, this can be converted to a Sym
object:
L1s <- as.Sym(L1)
L1s
## Yacas matrix:
## [,1] [,2] [,3] [,4]
## [1,] 1 0 0 0
## [2,] -alpha 1 0 0
## [3,] 0 -alpha 1 0
## [4,] 0 0 -alpha 1
Operations can be performed:
L1s + 4
## Yacas matrix:
## [,1] [,2] [,3] [,4]
## [1,] 5 4 4 4
## [2,] 4 - alpha 5 4 4
## [3,] 4 4 - alpha 5 4
## [4,] 4 4 4 - alpha 5
tmp <- L1s^4
tmp
## Yacas matrix:
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] -4 * alpha 1 0
## [3,] 3 * alpha^2 - -3 * alpha^2 -4 * alpha 1
## [4,] -(alpha^3 + alpha * (3 * alpha^2)) 3 * alpha^2 - -3 * alpha^2 -4 * alpha
## [,4]
## [1,] 0
## [2,] 0
## [3,] 0
## [4,] 1
Simplify(tmp)
## Yacas matrix:
## [,1] [,2] [,3] [,4]
## [1,] 1 0 0 0
## [2,] -4 * alpha 1 0 0
## [3,] 6 * alpha^2 -4 * alpha 1 0
## [4,] -4 * alpha^3 6 * alpha^2 -4 * alpha 1
Or the concentration matrix \(K=L L'\) can be found:
## Yacas matrix:
## [,1] [,2] [,3] [,4]
## [1,] 1 -alpha 0 0
## [2,] -alpha alpha^2 + 1 -alpha 0
## [3,] 0 -alpha alpha^2 + 1 -alpha
## [4,] 0 0 -alpha alpha^2 + 1
This can be converted to \(\LaTeX\):
TeXForm(K1s)
## [1] "\\left( \\begin{array}{cccc} 1 & - \\alpha & 0 & 0 \\\\ - \\alpha & \\alpha ^{2} + 1 & - \\alpha & 0 \\\\ 0 & - \\alpha & \\alpha ^{2} + 1 & - \\alpha \\\\ 0 & 0 & - \\alpha & \\alpha ^{2} + 1 \\end{array} \\right)"
Which look like this:
\[ K_1 = \left( \begin{array}{cccc} 1 & - \alpha & 0 & 0 \\ - \alpha & \alpha ^{2} + 1 & - \alpha & 0 \\ 0 & - \alpha & \alpha ^{2} + 1 & - \alpha \\ 0 & 0 & - \alpha & \alpha ^{2} + 1 \end{array} \right) \]
Similar can be done for vectors:
## Yacas vector:
## [1] x1 x2
And matrix-vector multiplication (or matrix-matrix multiplication):
## Yacas matrix:
## [,1] [,2]
## [1,] a11 a12
## [2,] a21 a22
As*xs
## Yacas vector:
## [1] a11 * x1 + a12 * x2 a21 * x1 + a22 * x2
As*As
## Yacas matrix:
## [,1] [,2]
## [1,] a11^2 + a12 * a21 a11 * a12 + a12 * a22
## [2,] a21 * a11 + a22 * a21 a21 * a12 + a22^2
xs
## Yacas vector:
## [1] x1 x2
## [1] 2 3
As
## Yacas matrix:
## [,1] [,2]
## [1,] a11 a12
## [2,] a21 a22
## [,1] [,2]
## [1,] 11 12
## [2,] 21 22
The functionality can be disabled as follows:
Ryacas_options("module_matvec_enabled")
## [1] TRUE
As
## Yacas matrix:
## [,1] [,2]
## [1,] a11 a12
## [2,] a21 a22
Ryacas_options(module_matvec_enabled = FALSE)
As
## yacas_expression(list(list(a11, a12), list(a21, a22)))