---
title: "Matrix and vector `Sym` objects"
author: "Mikkel Meyer Andersen and Søren Højsgaard"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Matrix and vector `Sym` objects}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r, message=FALSE}
library(Ryacas0)
```

# Matrices

Define a character matrix (covariance matrix from a certain $AR(1)$):

```{r}
N <- 3
L1 <- diag(1, 1 + N)
L1[cbind(1+(1:N), 1:N)] <- "-alpha"
L1s <- as.Sym(L1)
L1s
```

Now, this can be converted to a `Sym` object:

```{r}
L1s <- as.Sym(L1)
L1s
```

Operations can be performed:

```{r}
L1s + 4
tmp <- L1s^4
tmp
Simplify(tmp)
```


Or the concentration matrix $K=L L'$ can be found:

```{r}
K1s <- Simplify(L1s * Transpose(L1s))
K1s
```

This can be converted to $\LaTeX$:

```{r}
TeXForm(K1s)
```

Which look like this:

```{r, results="asis"}
cat("\\[ K_1 = ", TeXForm(K1s), " \\]", sep = "")
```

# Vectors

Similar can be done for vectors:

```{r}
x <- paste0("x", 1:2)
xs <- as.Sym(x)
xs
```

And matrix-vector multiplication (or matrix-matrix multiplication):

```{r}
A <- matrix(paste0(paste0("a", 1:2), rep(1:2, each = 2)), 2, 2)
As <- as.Sym(A)
As
```

```{r}
As*xs
As*As
```

# Eval

```{r}
xs
Eval(xs, list(x1 = 2, x2 = 3))
```

```{r}
As
Eval(As, list(a11 = 11, a12 = 12, a21 = 21, a22 = 22))
```

# Disabling functionality

The functionality can be disabled as follows:

```{r}
Ryacas_options("module_matvec_enabled")
As
Ryacas_options(module_matvec_enabled = FALSE)
As
```


 