### July 16 2013

## Kalkyl getting some help from Sage

*Kalkyl* is the module in Continuous that takes care of symbolic math operations. Why does Continuous need to handle symbolic math? Well, it would be possible to create visualizations with only numerical calculations, but the whole application would become much less flexible.

### How about numerical calculations using eval?

Let's say that we want the user to be able to fill in a function representing a surface of their own choice. Since JavaScript has an *eval*-function, we could simply substitute *x* and *y* in the user's expression by numerical values and then evaluate the string. Do this systematically with small intervals for *x* and *y* and *Voilà*, we have enough data to feed into our 3D engine to create a surface.

But then let's say that we want to visualize the partial *x* derivative by applying a color gradient to the surface, where red means large positive derivative and blue means large negative derivative. Of course we would need to calculate the derivative in every point. We *could* calculate a numerical approxiamation, but *come on!* The results would be inaccurate, and if we wanted to show a mathematical expression of the derivative to the user, it would be impossible.

Or let's say that we have a function that operates on matrices and outputs a vector. JavaScript does not natively handle matrix arithmetic, so the *eval*-idea fails.

### Hello, expression trees

What *Kalkyl* does is that it parses input from the user and builds up data structures called expression trees. Finding the derivative of an expression is just a matter of traversing the tree and applying differentiation rules from calculus. These trees also make some simplification of expressions possible by searching the trees for known patterns that can be rewritten in a simpler way.

### Symbolic integration and solving equations?

An expression tree and some differentiation rules is a good start, but thins get a bit more complex if you want the program to be able to solve equations or integrate arbitrary expressions Wolfram Alpha-style. Although it would most certainly be amazingly exciting to try to implement a full-fledged Computer Algebra System (like Mathematica or Maple) in JavaScript, I think I pass. At least for now. So what to do? Should we be satisfied with the limited set of symbolic operations that was feasible to implement in JavaScript without devoting an entire life to mathematics? No way!

### The open source community comes to the rescue, I hope.

I found an open source alternative to Mathematica and Maple, written in Python and published under the GPL license. Sage is capable of integrating expressions, solving equations and much more. My plan right now is to get Sage running on a web server, and let Continuous ask Sage for help when Kalkyl is not smart enough to do the math.

As some of the computation moves from the browser to a server, the application has to be designed to be much more async in its nature. It will be a real challenge to avoid callback soup. This article seems to explain some common ways to tackle this challenge, so I suppose reading it thoroughly will help me on my quest.