9  Symbolic Computing

9.1 Introduction

In symbolic operations, mathematical computations are carried out analytically using symbols and expressions rather than numerical values. Symbolic computation provides exact results because it relies on the exact representation of mathematical symbols. In R, there are several packages available for symbolic mathematics:

  • Ryacas: This package implements the Yacas computer algebra system. It provides functionalities for algebraic manipulations, calculus, and solving equations symbolically.
  • caracas: An R interface to the SymPy library in Python. It allows users to perform symbolic mathematics using SymPy’s capabilities via reticulate. This approach requires Python and the SymPy library to be installed.
  • rSymPy: This package also provides an interface to the SymPy library in Python. However, it is less commonly used compared to caracas.

In this chapter, we use the Ryacas package to for symbolic computations due to its ease of installation and use directly within R.

# Load necessary libraries
library(Ryacas)
library(caracas)

9.2 Declaration of Symbols

In Ryacas, we can declare symbols using the ysym() function. In the following example, we declare two symbols, x and y.

# Declare symbols in Ryacas
x <- ysym("x")
y <- ysym("y")
z <- ysym("z")
class(x)
[1] "yac_symbol" "list"      

We can then use these symbols in mathematical expressions. For example, we can create an expression representing a polynomial:

# Create a symbolic expression
expr <- x^2 + 2*x*y + y^2+z
expr
y: x^2+2*x*y+y^2+z
class(expr)
[1] "yac_symbol" "list"      

9.3 Symbolic Operations

We can perform various symbolic operations using Ryacas. To simplify an expression, we can use the Simplify() function.