Project: Symbolic Differentiator
I wanted to create a symbolic differentiator for polynomials of one variable. I had no idea what this meant when I began this project.
Latest
Click the button below to run the latest code. The output will be available in the DevTools console.
Code
<script></script>
Logbook
Thu Sep 28 04:19:18 PM PDT 2023
I had no clue where to start. So I searched "symbolic differentiator" and started with the Wikipedia article which came up. The article was full of math jargon which I hadn't reflected on in many years. I haven't done a lot of math since school. I did recently take the first few days of a Calculus I course, but we didn't get to differentiation. After reading the article, my best understanding was that a symbolic differentiator program would read in a mathematical expression as a string, parse it into an abstract syntax tree, then apply rewrite rules to reshape it into its differentiation.
When I searched, I also saw a lot of text books which I was sure would have more tutorial-esque walkthroughs of what this program meant. I meant for this project to challenge myself so I didn't want to look at any straightforward answer. Instead I decided to look up the mathematical definition of "differentiating polynomials of one variable" and attempt to implement something based on the math and the understanding I gained from the Wikipedia article.
I found a nice description of the "Power Rule" for differentiating polynomials on this page under the label "Theorem 3.1.3: The Power Rule (General Version)".
I described it in English, since I don't have a good way to write advanced mathematical formulas in my website yet:
The differentiation of an expression of variable
x
to the power ofn
isn
multiplied byx
to the power ofn - 1
.
So if x^n
is x
to the n
power, then the differentiation of, let's say, 6x^30
, is (30*6*x)^29
. Such an example would be a good test case.
Future: I explored a way to write in LaTex and translate it to pretty HTML so that I could write advanced mathematical formulas on my site.
My first task would be to parse an arbitrary polynomial into a syntax tree. That felt complicated.
Wow, I forgot how difficult parsers could be! That took a while, but I had something I was proud of as a starting place.
My next step would be to successively apply some rewrite rules to each item in this list of terms. First, if the coefficient was 0, that would be a constant term, which means it wouldn't appear in the differentiation. So I could filter out such terms. Next, if the coefficient was 1, then the differentiation would just be the term without the variable. Finally, if the coefficient was greater than 1, then I would have to multiply the coefficient by the exponent, and reduce the exponent by 1.
That seemed to work, so I considered this good enough for the task I set out on. I took a moment to quickly imagine enhancements I could make in the future.
Future: Instead of removing terms that resolve to 0, I wanted to replace those with a 0. Especially if it's the only remaining term, it should be 0 instead of no terms.
Future: I wanted to pretty-print the results using fancy text, like LaTex (but HTML & CSS).
Future: I wanted to show the steps of my symbolic differentiation, step-by-step with a fancy animation.
I put my code into a Gist to share it.
Tue Oct 31 05:21:14 PM PDT 2023
I worked with a pair programming partner to implement negative terms and subtraction. I took some time to rework this page to include that new code and put the running example at the top.
Future: I wanted to implement an input box for website users to input their own custom expression.