Skip to main content

A Symbolic Differentiator

Project description

delta-dx

PYPI Version Publish to PYPI Run Python Tests

Author : MrRhuezzler
Project Language : Python
Project Year : 2021

How to Install

This currently avaiable on PYPI, from where it can be installed using the following command.
(Note : pip and python must be added to the PATH for this command to work)

pip install delta-dx

Usage

from delta import Expression
expression = Expression("x * e ^ x")
print(Expression.differentiate(expression, nth_derivative=1))

Approach

This Project uses all the basic rules of Mathematical Differentiation and converts them into a computer algorithm, which provides the power to differentiate various equations. One main advantage of this project is that it can differentiate between variables ( x, y, z, w, u, v ) and numbers (5, 0.56, e, pi, tau ).

Thinking of devising an algorithm to handle such a task is the goal, luckily Dynamic Programming can be used to devise such an algorithm. Let's take an example to explain.

Example:

d(sin(2x)) / dx = cos(2x) * 2

In the equation sin(2x), first we differentiate based on the rule d(sin(u)) / dx =cos(u) and then followed by differentiating u = 2x based on the rule d(cv) / dx = c dv/dx we get 2.

From the above example, we can see that the process of differentiating sin(2x)is being broken down to first differentiating the outer sin(u) function and differentiating the inner 2x function and then combining both results to get the answer to the given problem.

The Next major problem is that, how is an equation being represented in the computer algorithm. To Tackle this problem, we have to see the various ways that an expression can be expressed. They are infix, prefix, postfix notations.

Example:

(a+b) * (c + d) - Infix
*+ab+cd         - Prefix
ab+cd+*         - Postfix

Given the input expression, it can be converted to the prefix notation, with can be easily converted into a prefix expression tree, which can be used as an input to the algorithm and also devise the algorithm in such a way that it outputs a prefix expression tree, which makes it complete cycle (Enabling the algorithm to find multiorder derivatives).

Example:

log 10(cos(5 * x))

Expression Tree :

        log
      /     \
    10        cos
                \ 
                 *
                /  \ 
              5     x

By traversing the tree layer by layer and calling the algorithm, to solve for each layer, the problem of differentiating becomes a lot simpler. Let's take an example of differentiating a mathematical expression.

Example:

sin(2*x)

Expression Tree

    sin
      |
      *
    /   \
  2       x
-> differentiate(sin) // Calling the algorithm to solve
-> if input == sin // If the given head node of the tree represents sin function
->    cos = cos // Creating a node representing cos function
->    cos.child = input.child // Assigning the input for cos function
->     multiply = * // Creating a multiplication node
->    multiply.left = cos
->    multiply.right = differentiate(input.child) // Recursively calling the algorithm to solve for 2*x
->      return multiply // returning the result
-> if input == * // If the input is a multiplication operation
->     // Statements to handle this case and return the result.
-> done

Now we are done with the differentiating algorithm.

The Next steps may be to simplify the output of the differentiating algorithm, so that it looks neat and simple, rather than having more additional terms in the final output expression. Such an expression folding algorithm is also implemented to fold into smaller expressions. And with some basic mathematics an algorithm was devised. It is not the most optimal solution for the problem. I learnt many things in making this package.

Resources

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

delta-dx-0.1.2.tar.gz (11.7 kB view hashes)

Uploaded Source

Built Distribution

delta_dx-0.1.2-py3-none-any.whl (13.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page