Introduction to SymCalc in C++

Not here for C++? 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:

```
#include <cmath>
double fx(double x){
return pow(x, 2) / 3.0;
}
```

With SymCalc becomes:

`Equation fx = pow(x, 2) / 3.0;`

Contents

Download

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

Now you can include SymCalc with

```
#include <symcalc/symcalc.hpp>
using namespace symcalc; // Include the SymCalc's namespace
```

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:

```
#include <symcalc/symcalc.hpp>
using namespace symcalc;
int main(){
Equation x("x");
std::cout << x << std::endl; // => x
return 0;
}
```

The C++ 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):

```
#include <symcalc/symcalc.hpp>
using namespace symcalc;
int main(){
Equation symcalc_pi("pi", 3.14159);
std::cout << symcalc_pi << std::endl; // => pi
return 0;
}
```

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:

```
#include <symcalc/symcalc.hpp>
using namespace symcalc;
int main(){
Equation x("x");
Equation f = 5 * x;
std::cout << f << std::endl; // => 5 * x
return 0;
}
```

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

```
#include <symcalc/symcalc.hpp>
using namespace symcalc;
int main(){
Equation x("x");
Equation f = 3 * pow(x, 2) / 2 + 4;
std::cout << f << std::endl; // => 3 * x^2 / 2 + 4
return 0;
}
```

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:

```
#include <symcalc/symcalc.hpp>
using namespace symcalc;
int main(){
Equation x("x");
Equation sinx = sin(x);
Equation cosx = cos(x);
Equation lnx = ln(x);
Equation log2x = log(x, 2);
Equation expx = exp(x);
Equation fx = sinx + cosx + lnx + log2x + expx;
Equation gx = sin(x * 3) - ln(cos(pow(x, 3) / 2) - 2) - exp(2 * x);
std::cout << gx << std::endl; // => sin(x * 3) - ln(cos(x^3 / 2) - 2) - exp(2 * x);
return 0;
}
```

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 named .eval - let's see how we can use it!

```
#include <symcalc/symcalc.hpp>
using namespace symcalc;
int main(){
Equation x("x"); // Declare the variable
Equation fx = pow(x, 2) / 4; // Declare the function
// Find the value of f at x = 3, .eval accepts map<variable, double>
double value = fx.eval({{x, 3}});
std::cout << value << std::endl; // => 2.25
return 0;
}
```

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:

```
#include <symcalc/symcalc.hpp>
using namespace symcalc;
int main(){
Equation x("x"); // Declare the variable
Equation fx = pow(x, 2); // Declare the function
// Take the derivative of f
Equation dfdx = fx.derivative();
std::cout << dfdx << std::endl; // => 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!

```
#include <symcalc/symcalc.hpp>
using namespace symcalc;
int main(){
Equation x("x"); // Declaring a variable
Equation y("y"); // Declaring a second variable!
// Declaring a multi-variable function
Equation fxy = pow(x, 2) + pow(y, 2);
std::cout << fxy << std::endl; // => x^2 + y^2
return 0;
}
```

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

```
#include <symcalc/symcalc.hpp>
using namespace symcalc;
int main(){
Equation x("x"); // Declaring a variable
Equation y("y"); // Declaring a second variable!
// Declaring a multi-variable function
Equation fxy = pow(x, 2) + pow(y, 2);
// Evaluating at x = 2 and y = 3
double value = fxy.eval({{x, 2}, {y, 3}});
std::cout << value << std::endl; // => 13
return 0;
}
```

Or differentiation:

```
#include <symcalc/symcalc.hpp>
using namespace symcalc;
int main(){
Equation x("x"); // Declaring a variable
Equation y("y"); // Declaring a second variable!
// Declaring a multi-variable function
Equation fxy = pow(x, 2) + pow(y, 2);
// Taking the derivative with respect to x
Equation dfdx = fxy.derivative(x);
// Taking the derivative with respect to y
Equation dfdy = fxy.derivative(y);
std::cout << dfdx << std::endl; // => 2 * x
std::cout << dfdy << std::endl; // => 2 * y
return 0;
}
```

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 our variable as the first argument to the .derivative() function.

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:

```
#include <symcalc/symcalc.hpp>
using namespace symcalc;
int main(){
// Declare a variable
Equation x("x");
// Declare the function pi * x
Equation fx = Constants::Pi * x;
// Take the derivative, should be just pi
Equation dfdx = fx.derivative();
std::cout << dfdx << std::endl; // => pi
// Evaluate the derivative (no variable values needed, since x is not in the derivative)
std::cout << dfdx.eval() << std::endl; // => 3.14159
return 0;
}
```

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 Ruby 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.