| Name | dual-numbers JSON |
| Version |
0.0.11
JSON |
| download |
| home_page | None |
| Summary | 2D dual numbers for python, with support for complex scalars or arbitrary domains |
| upload_time | 2024-08-06 00:52:48 |
| maintainer | None |
| docs_url | None |
| author | None |
| requires_python | >=3.7 |
| license | None |
| keywords |
|
| VCS |
 |
| bugtrack_url |
|
| requirements |
No requirements were recorded.
|
| Travis-CI |
No Travis.
|
| coveralls test coverage |
No coveralls.
|
# 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) = Img(f(x+i*dx))/dx`. See [this](https://www.hedonisticlearning.com/posts/complex-step-differentiation.html)
for an explanation.
More info in [wikipedia](https://en.wikipedia.org/wiki/Dual_number).
## How to use:
To declare a dual number:
```
a=dual(1,2)
a
>>> (1+2d)
b=3 .d + 1
b
>>> (1+3d)
b=3.0.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,abs,arg` 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`, `a/b`, `a**b` work as expected.
* `a.exp()` and `a.log()` return their respective values.
* `a.abs()` returns the real part, and `a.arg()` returns `a.dl/a.real`
See docs or code for extending to other algebras ex.: `exp(self,f=mathmodule)`.
In case there is a math domain error, python handles them as usual, ex.:
```
a=1 .d # pure dual numbers do not have inverse
a**-1
>>> ValueError: math domain error
```
## Requirements
Install these with pip install as usual:
* custom_literals (for easy dual declaration with `.d`)
* forbiddenfruit (dependency for the former)
### Use case example: Dual quaternions
Lets import a sane quaternion lib, like `pip install quaternions`.
In order to extend all operations, all "scalars" of a dual must be
quaternions, and we can also [define](https://en.wikipedia.org/wiki/Quaternion#Functions_of_a_quaternion_variable)
our own exp() and log() functions for them,
and provide them to the library for use:
```
import quaternions.quaternion
from dual_numbers import *
import math as m
qt=quaternions.quaternion.Quaternion
def qtexp(q):
if type(q) in [type(1),type(1.0)]:
return qt(m.exp(q),0,0,0)
v=qt(0,q.x,q.y,q.z)
norm=v.norm()
v_u=v.unit()
real=m.cos(norm)
return (v_u*m.sin(norm)+qt(real,0,0,0))*m.exp(q.w)
def qtlog(q):
v=qt(0,q.x,q.y,q.z)
v_u=v.unit()
return v_u*m.acos(q.w/q.norm())+qt(m.log(q.norm),0,0,0)
```
Then, we can extend quaternions easily to dual quaternions:
```
q1=qt(1,1,0,0)
q2=qt(1,1,0,0)
unit=qt(1,0,0,0)
dual(q1,q2).exp(fexp=qtexp)
>>> (<1.469, 2.287, 0.0, 0.0>+<-0.819, 3.756, 0.0, 0.0>d)
dual(q1,q2)+dual(q1,unit)
>>> (<2, 2, 0, 0>+<2, 1, 0, 0>d)
```
## TODO
- [ ] Test the library
- [X] Add wheel
- [ ] Make docs
- [X] Put more use cases
- [ ] Give more links
Raw data
{
"_id": null,
"home_page": null,
"name": "dual-numbers",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": null,
"author": null,
"author_email": "Vicente Carrasco \u00c1lvarez <vidacaal@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/6a/08/d860a418335dc7726e59f149f997465e12efdd53c967dd98ea6fa0f6a5cd/dual_numbers-0.0.11.tar.gz",
"platform": null,
"description": "# dual_numbers\n2D Dual numbers for python, can trivially use complex numbers as scalars.\n\n## What are dual numbers?\nPython uses ```j``` for the imaginary unit, here we use ```d``` for\nthe dual unit, in order to differentiate it from scientific notation.\n \n```d^2 = 0```\n\n```exp(a*d)=1+a*d```\n\nSince complex numbers can make rotations be done as multiplication,\ndual numbers can make posible translation as multiplication. As the dual\npart squares to 0, they can also be used for automatic differentiation:\n\n```f(x+d) = f(x) + f'(x)d```\n\nNote: in complex numbers, `f'(x) = Img(f(x+i*dx))/dx`. See [this](https://www.hedonisticlearning.com/posts/complex-step-differentiation.html)\nfor an explanation.\n\nMore info in [wikipedia](https://en.wikipedia.org/wiki/Dual_number).\n## How to use:\nTo declare a dual number:\n```\na=dual(1,2)\na\n>>> (1+2d)\nb=3 .d + 1\nb\n>>> (1+3d)\nb=3.0.d+1\nb\n>>> (1+3d)\nc=dual(1) # same as complex(1)\nc\n>>> (1+0d)\nc.real\n>>> 1\nc.dl\n>>> 0\ndual(1+1j,2).real\n>>> (1+1j) # This is not a bug, but a feature.\n```\n\nTo add them, make sure the dual number comes first, since we cannot modify\npython's integer and float methods for adding:\n\n```\na=1 .d + 2\nb=1\na+b\n>>> (3+1d) # CORRECT!!\nb+a\n>>> Error: \"+\" not defined for int with dual\n```\n\nSo make sure to declare all your numbers as dual if you dont \nwant to deal with these.\n\nBasic arithmetic operations `+,-,*,/,exp,log,abs,arg` work as intended if both\noperands are dual. See docs or code for the cases when numbers are promoted.\n\n* `a+b`,`a-b`,`a*b`, `a/b`, `a**b` work as expected.\n* `a.exp()` and `a.log()` return their respective values. \n* `a.abs()` returns the real part, and `a.arg()` returns `a.dl/a.real`\nSee docs or code for extending to other algebras ex.: `exp(self,f=mathmodule)`.\n\nIn case there is a math domain error, python handles them as usual, ex.:\n```\na=1 .d # pure dual numbers do not have inverse\na**-1\n>>> ValueError: math domain error\n```\n## Requirements\nInstall these with pip install as usual:\n\n* custom_literals (for easy dual declaration with `.d`)\n* forbiddenfruit (dependency for the former)\n\n### Use case example: Dual quaternions\n\nLets import a sane quaternion lib, like `pip install quaternions`.\n\nIn order to extend all operations, all \"scalars\" of a dual must be\nquaternions, and we can also [define](https://en.wikipedia.org/wiki/Quaternion#Functions_of_a_quaternion_variable) \nour own exp() and log() functions for them, \nand provide them to the library for use:\n\n```\nimport quaternions.quaternion\nfrom dual_numbers import *\nimport math as m\n\nqt=quaternions.quaternion.Quaternion\n\ndef qtexp(q):\n if type(q) in [type(1),type(1.0)]:\n return qt(m.exp(q),0,0,0)\n v=qt(0,q.x,q.y,q.z)\n norm=v.norm()\n v_u=v.unit()\n real=m.cos(norm)\n return (v_u*m.sin(norm)+qt(real,0,0,0))*m.exp(q.w)\n\ndef qtlog(q):\n v=qt(0,q.x,q.y,q.z)\n v_u=v.unit()\n return v_u*m.acos(q.w/q.norm())+qt(m.log(q.norm),0,0,0)\n```\n\nThen, we can extend quaternions easily to dual quaternions:\n\n```\nq1=qt(1,1,0,0)\nq2=qt(1,1,0,0)\nunit=qt(1,0,0,0)\ndual(q1,q2).exp(fexp=qtexp)\n>>> (<1.469, 2.287, 0.0, 0.0>+<-0.819, 3.756, 0.0, 0.0>d)\ndual(q1,q2)+dual(q1,unit)\n>>> (<2, 2, 0, 0>+<2, 1, 0, 0>d)\n```\n\n## TODO\n- [ ] Test the library\n- [X] Add wheel\n- [ ] Make docs\n- [X] Put more use cases\n- [ ] Give more links\n",
"bugtrack_url": null,
"license": null,
"summary": "2D dual numbers for python, with support for complex scalars or arbitrary domains",
"version": "0.0.11",
"project_urls": {
"Homepage": "https://github.com/vichoca9/dual_numbers",
"Issues": "https://github.com/vichoca9/dual_numbers/issues"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "467efe71ddee7a414c7224a057b6010f60ad65cde4459aa847dc2dcc6929afeb",
"md5": "b424b0eb617553fd1087099df2251e29",
"sha256": "dc046dc76d2a51a94e48bb73c084091dd078e16d5578df81d061b465165e6a0d"
},
"downloads": -1,
"filename": "dual_numbers-0.0.11-py3-none-any.whl",
"has_sig": false,
"md5_digest": "b424b0eb617553fd1087099df2251e29",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 4525,
"upload_time": "2024-08-06T00:52:46",
"upload_time_iso_8601": "2024-08-06T00:52:46.549574Z",
"url": "https://files.pythonhosted.org/packages/46/7e/fe71ddee7a414c7224a057b6010f60ad65cde4459aa847dc2dcc6929afeb/dual_numbers-0.0.11-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "6a08d860a418335dc7726e59f149f997465e12efdd53c967dd98ea6fa0f6a5cd",
"md5": "a09c40f19311c1fcfffae5f2b4362b6f",
"sha256": "3b3b84bc697bb94de12076371e29446d434528275bea3186246ac2d1b77c2d1d"
},
"downloads": -1,
"filename": "dual_numbers-0.0.11.tar.gz",
"has_sig": false,
"md5_digest": "a09c40f19311c1fcfffae5f2b4362b6f",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 4481,
"upload_time": "2024-08-06T00:52:48",
"upload_time_iso_8601": "2024-08-06T00:52:48.323002Z",
"url": "https://files.pythonhosted.org/packages/6a/08/d860a418335dc7726e59f149f997465e12efdd53c967dd98ea6fa0f6a5cd/dual_numbers-0.0.11.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-06 00:52:48",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "vichoca9",
"github_project": "dual_numbers",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "dual-numbers"
}