Skip to content

DKay7/differentiator

Repository files navigation

Differentiator

Symbolic differentiator written in C++.

It reads a math expression from stdin, parses it with recursive descent, builds an expression tree, differentiates the tree, simplifies the result and generates a PDF report with intermediate steps. It also dumps expression trees, which is useful for debugging.

Features

  • parse expressions with recursive descent.
  • build expression trees.
  • dump expression trees to PNG through Graphviz.
  • differentiate expressions symbolically.
  • simplify obvious cases like constants, zero terms and one multipliers.
  • generate a LaTeX/PDF report with differentiation and simplification steps.
  • replace long subexpressions in the PDF report with short names.

Examples

Example tree dumps for expression: sin(x)^2+cos(x)^2

Original expression tree

Simplified derivative tree

A saved PDF report is available at examples/example_report.pdf.

Build and Run

Requirements:

  • C++14 compiler
  • Graphviz (dot) for tree pictures
  • pdflatex for PDF reports

Use make to build the project:

make

This builds the main executable.

To run the project, pass an expression through stdin:

./main

sin(x)^2+cos(x)^2

The program prints debug dumps to stdout, writes tree pictures to /tmp, and writes a PDF report to data/. The exact report filename is generated automatically and looks like data/diff_fileXXXXXX.pdf. Generated reports are ignored by git.

Supported syntax

Examples of accepted expressions:

x^2+2*x+1
sin(x)^2+cos(x)^2
ln(x)+sqrt(x)
(x+1)/(x-1)

The parser uses one-letter variable names. The main variable for differentiation is x.

Supported operations are defined in operators.h. The interesting files are:

  • read_tree.cpp - recursive descent parser
  • tree.cpp - expression tree utilities and Graphviz dumps
  • differentiator.cpp - differentiation and simplification logic
  • operators.h - rules for operators
  • to_latex.cpp - LaTeX report generation
  • data/latex_*.txt - report templates and phrases

About

Symbolic differentiator written in C++. It reads a math expression from stdin, parses it with recursive descent, builds an expression tree, differentiates the tree, simplifies the result and generates a PDF report with intermediate steps. It also dumps expression trees, which is useful for debugging.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages