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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file delta-dx-0.1.2.tar.gz.

File metadata

  • Download URL: delta-dx-0.1.2.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for delta-dx-0.1.2.tar.gz
Algorithm Hash digest
SHA256 f4b9a834e6bb77cb20ac81a75f77158e4752742c621c731e9e41dec66bdec775
MD5 621388636f82100489018d5ffb54883c
BLAKE2b-256 66e7137b1828e8b48593250924d4f1f17c94ee11ccfb012931a0a9d304f41810

See more details on using hashes here.

Provenance

File details

Details for the file delta_dx-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: delta_dx-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 13.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for delta_dx-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6ce7fed1ea7faa698fafe2909dcfbf5ca5397980b1fd63b6e176b621b7735a00
MD5 6eaaf5b1a55010626dd44c97072c171a
BLAKE2b-256 ddab1470e057e934e5589982e87fd0f6eb90e8ad24bd4c0bbaa16440930f7859

See more details on using hashes here.

Provenance

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