Skip to main content

2D dual numbers for python, with support for complex scalars

Project description

dual_numbers

2D Dual numbers for python, can trivially use complex numbers as scalars.

What are dual numbers?

Python uses j for the imaginary unit, here we use d for the dual unit, in order to differentiate it from scientific notation.

d^2 = 0

exp(a*d)=1+a*d

Since complex numbers can make rotations be done as multiplication, dual numbers can make posible translation as multiplication. As the dual part squares to 0, they can also be used for automatic differentiation:

f(x+d) = f(x) + f'(x)d Note: in complex numbers, f(x+i)=f(x)+f'(x)i

More info in wikipedia.

How to use:

To declare a dual number:

a=dual(1,2)
a
>>> (1+2d)
b=3 .d + 1
b
>>> (1+3d)
c=dual(1) # same as complex(1)
c
>>> (1+0d)
c.real
>>> 1
c.dl
>>> 0
dual(1+1j,2).real
>>> (1+1j) # This is not a bug, but a feature.

To add them, make sure the dual number comes first, since we cannot modify python's integer and float methods for adding:

a=1 .d + 2
b=1
a+b
>>> (3+1d) # CORRECT!!
b+a
>>> Error: "+" not defined for int with dual

So make sure to declare all your numbers as dual if you dont want to deal with these.

Basic arithmetic operations +,-,*,/,exp,log work as intended if both operands are dual. See docs or code for the cases when numbers are promoted.

  • a+b,a-b,a*b, etc
  • a.exp() and a.log() return their respective values. See docs or code for extending to other algebras ex.: exp(self,f=mathmodule).

Requirements

Install these with pip install as usual:

  • custom_literals (for easy dual declaration with .d)
  • forbiddenfruit (dependency for the former)

TODO

  • Test the library
  • Make literals optional
  • Add wheel
  • Make docs
  • Put more use cases
  • Give more links

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

dual_numbers-0.0.2a0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

dual_numbers-0.0.2a0-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file dual_numbers-0.0.2a0.tar.gz.

File metadata

  • Download URL: dual_numbers-0.0.2a0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for dual_numbers-0.0.2a0.tar.gz
Algorithm Hash digest
SHA256 377e83a652a5aaeb89790e813169d5201b385653d176f41a0357146f01ea7841
MD5 d48fe1e15b3fb941d4e08e6b46fc8f97
BLAKE2b-256 a26e5f868a17302099d05000e7299031382bb05164164ebe729e4dd9ac63b00a

See more details on using hashes here.

File details

Details for the file dual_numbers-0.0.2a0-py3-none-any.whl.

File metadata

File hashes

Hashes for dual_numbers-0.0.2a0-py3-none-any.whl
Algorithm Hash digest
SHA256 cf7e636478ee4cb4a4f1259619b2438eff10d3733bf8ceddf12bc18c1194f23e
MD5 20c2b55f3c58384035d77972616f3cfa
BLAKE2b-256 8e4da5a8cd10c70f862f1db9effb8e5e146438921c0ea67e7ae486bb03c4eb7a

See more details on using hashes here.

Supported by

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