knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

This vignette provides examples for the Exe() function in the yacR package.

library(Ryacas)
library(yacR)

We start with the matrix $\mathbf{A}$ whose elements are $\left{ a, b, c, d \right}$.

A <- matrix(c("a", "b", "c", "d"), ncol = 2, byrow = TRUE)
cat(
  "\\begin{align*}",
  "\\mathbf{A} &=",
  as.tex(A),
  "\\end{align*}",
  sep = ""
)

We need to convert the matrix A into a yac_symbol first. This can be done using the Ryacas function ysym(). The yacR package has an alternative function, as.ysym(), to convert an object into a yac_symbol. It is a safe conversion tool which checks if the object is of class yac_symbol before converting it to one. If the target object is already of class yac_symbol it simply returns the object. If the target is not of class yac_symbol, it uses the ysym() function from Ryacas to convert the target object into a yac_symbol.

A <- ysym(A)
A <- as.ysym(A)

Example 1

The Determinant of A can be obtained in Yacas with the expression Determinant(A).

expr1 <- paste0("Determinant(", A, ")")

The following returns a yac_symbol output.

yac_symbol <- Exe(expr1)
str(yac_symbol)

The following returns a string output.

string <- Exe(expr1, format = "str")
str(string)

The following returns a LaTeX mathematics output.

latex <- Exe(expr1, format = "tex")
str(latex)

LaTeX mathematics can be displayed inline by enclosing the code or object with $`r `$.

$r latex$

It can be displayed as a centered equation by enclosing the code or object with $$`r `$$.

$$r latex$$

It can also be displayed as an R code chuck. Remember to set results = "asis".

cat(
  "\\begin{align*}",
  "\\det \\mathbf{A} &=",
  latex,
  "\\end{align*}",
  sep = ""
)

The following returns an R expression output.

yacR::Exe(expr1, R = TRUE)

Example 2

In the following example we derive the Transpose of the matrix A with the expression Transpose(A).

expr2 <- paste0("Transpose(", A, ")")

The following returns a yac_symbol output.

yac_symbol <- Exe(expr2)
str(yac_symbol)

The following returns a string output.

string <- Exe(expr2, format = "str")
str(string)

The following returns a LaTeX mathematics output.

latex <- Exe(expr2, format = "tex")
str(latex)
cat(
  "\\begin{align*}",
  "\\mathbf{A}^{\\mathsf{T}} &=",
  latex,
  "\\end{align*}",
  sep = ""
)

The following returns an R expression output.

yacR::Exe(expr2, R = TRUE)

Numeric

The R expression outputs can be evaluated numerically using the eval function. The symbols a, b, c, and d should have values in the environment eval is using.

a <- 1
b <- 2
c <- 3
d <- 4

Below is the numerical result for example 1

eval(yacR::Exe(expr1, R = TRUE))

which is equal to the following.

det(matrix(1:4, ncol = 2, byrow = TRUE))

Below is the numerical result for example 2

eval(yacR::Exe(expr2, R = TRUE))

which is equal to the following.

t(matrix(1:4, ncol = 2, byrow = TRUE))


jeksterslab/yacR documentation built on Feb. 27, 2021, 3:45 a.m.