Introduction to SymCalc in Ruby

Not here for Ruby? You can choose another language

First, let's understand what SymCalc is for. SymCalc is a programming library that:

Implements mathematical functions in their abstract form

Gives developers the ability to abstract from hard-coded equations

Automates the operations on these functions, e.g. evaluating or differentiating them

So, basically, a hard-coded function like this:

```
def fx(x)
x ** 2 / 3.0
end
```

With SymCalc becomes:

`fx = x ** 2 / 3.0`

Contents

Download

To install SymCalc, you can follow the installation procedure here.

Now you can include SymCalc with

`require 'symcalc'`

Declaring SymCalc variables

What do mathematical functions depend on? That's right - variables.

This is why, first we need to tell SymCalc, which variables we are going to use and what is their name.

Creating a SymCalc variable is straight-forward, like this:

```
require 'symcalc'
x = SymCalc.var 'x'
puts x # => x
```

The Ruby variable name does not matter, because SymCalc uses the string passed as the first argument to work with.

It is also possible to create a constant, by providing a name and a value as arguments, like this (you can learn more about constants here):

```
require 'symcalc'
symcalc_pi = SymCalc.const 'pi', 3.14159
puts symcalc_pi # => pi
```

Declaring SymCalc functions

Now that we have our variable, we can create a custom function to experiment further!

Let's create a simple function, f(x) = 5 * x with SymCalc:

```
require 'symcalc'
x = SymCalc.var 'x'
f = 5 * x
puts f # => 5 * x
```

This works, but what about functions with operations other than multiplication? Let's try creating a function, f(x) = 3 * x^2 / 2 + 4:

```
require 'symcalc'
x = SymCalc.var 'x'
f = 3 * x**2 / 2 + 4
puts f # => 3 * x^2 / 2 + 4
```

This means, we can define functions using our variable and most of the ruby numerical operators.

Using built-in complex SymCalc functions

Creating functions with numerical operators is cool and all, but what about incorporating functions like sine, cosine, logarithms and exponents?

Turns out, SymCalc has a wide variety of pre-built functions for this purpose.

And we can access them from the SymCalc module like this:

```
require 'symcalc'
x = SymCalc.var 'x' # Declare our variable
# Use different built-in functions
sinx = SymCalc.sin(x)
cosx = SymCalc.cos(x)
lnx = SymCalc.ln(x)
expx = SymCalc.exp(x)
fx = sinx + cosx + lnx + expx # Combine them together!
# Or create something fun!
gx = SymCalc.sin(x * 3) - SymCalc.ln(SymCalc.cos(x ** 3 / 2) - 2) - SymCalc.exp(2 * x);
puts gx # => sin(x * 3) - ln(cos(x^3 / 2) - 2) - exp(2 * x)
```

Evaluating SymCalc functions

Declaring complex functions is really cool, but to find any use out of it, we need to evaluate them at any value of x or other variables.

For this, SymCalc has a function method named .eval - let's see how we can use it!

```
require 'symcalc'
x = SymCalc.var 'x' # Declare the variable
fx = x ** 2 / 4 # Declare the function
# Find the value of f at x = 3
value = fx.eval x: 3
puts value # => 2.25
```

This code creates a function f(x) = x^2 / 4, and finds its value at x = 3, which is equal to 2.25.

Note: .eval will always return a numerical value, not a SymCalc expression, which is very important to remember!

Differentiating SymCalc functions

Now that we know so much about SymCalc functions, let's test our knowledge of Calculus and try taking their derivatives!

The general idea is that a derivative of f(x), is a function that shows the slope of f(x) at point x. This idea is crucial in many places, just like basic numerical operations that everyone uses, but on a higher level.

In SymCalc, differentiating a function is straight-forward, like this:

```
require 'symcalc'
x = SymCalc.var 'x' # Declare the variable
fx = x ** 2 # Declare the function
# Take the derivative of f
dfdx = fx.derivative
puts dfdx # => 2 * x
```

This code declares the function of f(x) = x^2, and takes its derivative, which you may know fom high school, is equal to 2x.

This variable, "dfdx", is the same kind of expression as "fx" and all other functions we've created. This means that we can apply the same set of operations to the derivative expression, for example, evaluate it, or take another derivative!

Multivariable functions in SymCalc

Now we know a lot about SymCalc and its abilities, so it's time to explore the multi-dimensional world!

Creating multi-dimensional functions in SymCalc is as straight-forward as creating one-dimensional ones, you just need more variables!

```
require 'symcalc'
x = SymCalc.var 'x' # Declaring a variable
y = SymCalc.var 'y' # Declaring a second variable!
# Declaring a multi-variable function
fxy = x ** 2 + y ** 2
puts fxy # => x^2 + y^2
```

We can perform the same set of operations on multi-dimensional functions, as on the one-dimensional ones. For example, evaluating:

```
require 'symcalc'
x = SymCalc.var 'x' # Declaring a variable
y = SymCalc.var 'y' # Declaring a second variable!
# Declaring a multi-variable function
fxy = x ** 2 + y ** 2
# Evaluating at x = 2 and y = 3
value = fxy.eval x: 2, y: 3
puts value # => 13
```

Or differentiation:

```
require 'symcalc'
x = SymCalc.var 'x' # Declaring a variable
y = SymCalc.var 'y' # Declaring a second variable!
# Declaring a multi-variable function
fxy = x ** 2 + y ** 2
# Taking the derivative with respect to x
dfdx = fxy.derivative variable: x
# Taking the derivative with respect to y
dfdy = fxy.derivative variable: y
puts dfdx # => 2 * x
puts dfdy # => 2 * y
```

Important! Don't forget, we can only differentiate a function with repsect to a single variable, not all of them! That's why we need to specify the argument "variable: x" in the ".derivative" method.

Constants in SymCalc

As you previously saw, you can create a constant in Symcalc.

They behave like a numerical value when evaluating, but as a variable when displaying the equation.

With this, SymCalc also has a few pre-existing constants. Here's the list of them:

`Constants::E`

Euler's number, the base of the natural log, or the limit of (1 + 1/n)^n as n tends to infinity.
`Constants::Pi`

Pi, the trigonometrical constant, that is equal to the ratio of a circle's circumference to its diameter.
Let's try using them:

```
require 'symcalc'
# Declare a variable
x = SymCalc.var("x)
# Declare the function pi * x
fx = SymCalc::Constants::Pi * x
# Take the derivative, should be just pi
dfdx = fx.derivative
puts dfdx # => pi
# Evaluate the derivative (no variable values needed, since x is not in the derivative)
puts dfdx.eval() # => 3.14159...
```

Conclusion

Well that's the end of SymCalc's intro for now!

Some features could be added in the future, from the smallest ones, to game-changing methods, but right now, that's what SymCalc has to offer!

If you'd like to know more of the "insides" of SymCalc, you can view the documentation.

Or explore SymCalc in C++ here.

If you have any questions or suggestions, you can contact me by email at kyryloshy@gmail.com, or on the portfolio website, kyrylshyshko.me in the "Let's talk" section.

SymCalc is licensed under the Apache 2.0 license. You can read more on the about page.