Skip to content

Commit

Permalink
rewrite of DualNumbers to support Dual{Complex}
Browse files Browse the repository at this point in the history
- remove dual() constructor (float() is deprecated in 0.4 anyways)

- replace real & epsilon with value and dual

- change du to ɛ addressing #24

- add Base.sinpi and Base.cospi as a patch addressing #23

- add complex function tests

add 2-norm(Vector{Dual})

add cis (cos+im*sin)

update readme to reflect more general support

fixes

dual => epsilon

re-add vectorized constructor dual

deprecate_binding du

release => 0.4 and deprecate real(::Dual)

dual => epsilon in readme

un-export value

incorporate some changes of #28:

__precompile()__

function => constructor in readme

import value in test

add definitions for non-complex differentiable functions

and tests

all generic norms work with isinf() and float()

fix print

so that you can copy paste and it will make a new Dual

add limit definition in readme

fix typo:

float is a no-op on Union{Dual{T},Dual{Complex{T}}}, not
Union{Dual{T},Complex{T}}

add no-op real{T<:Real}

add angle, realpart, and dualpart

deprecate real{T<:Real}(z::Dual{T}).

also, because sign := z/|z| in base, it works automatically and so #22
is no longer necessary.

remove value(z::Number) and epsilon(z::Number)

- add the constant imɛ
- add special dual_show methods for Dual{Bool}’s and
Dual{Complex{Bool}}’s

- replace real with DualNumbers.value

- change du to ɛ addressing #24

- add imɛ

- add Base.sinpi and Base.cospi as a patch addressing #23

- add cis (cos+im*sin)

- deprecate_binding du

- remove 0.3 from Travis build (and therefore support)

- deprecate real(::Dual)

- (real,epsilon) => (realpart,dualpart) in readme

- incorporate some changes of #28:

-- __precompile()__

- function => constructor in readme

- add isinf() and float() so that all generic norms work

- add show_dual{T<:Complex} so that you can copy paste and it will make a new Dual

- add show_dual{T<:Bool} and show_dual{T<:Complex{Bool}}

- add limit definition in readme

- add angle, realpart, and dualpart

- deprecate real{T<:Real}(z::Dual{T})

- sign := z/|z| in base, it works automatically and so #22
is no longer necessary

- add tests
  • Loading branch information
MikaelSlevinsky committed Dec 2, 2015
1 parent ca3a9ec commit 5bc6760
Show file tree
Hide file tree
Showing 6 changed files with 328 additions and 177 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Documentation: http://docs.travis-ci.com/user/languages/julia/
language: julia
os:
- linux
- osx
julia:
- 0.3
- 0.4
- nightly
notifications:
Expand Down
75 changes: 41 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@

### Scope of DualNumbers.jl

The `DualNumbers` package defines the `Dual` type to represent dual numbers and
supports standard mathematical operations on them. Conversions and promotions
are defined to allow performing operations on combinations of dual numbers with
The `DualNumbers` package defines the `Dual` type to represent dual numbers and
supports standard mathematical operations on them. Conversions and promotions
are defined to allow performing operations on combinations of dual numbers with
predefined Julia numeric types.

Dual numbers extend the real numbers, similar to complex numbers. They adjoin a
new element `ϵ` such that `ϵ*ϵ=0`, in a similar way that complex numbers
adjoin the imaginary unit `i` with the property `i*i=-1`. So the typical
representation of a dual number takes the form `x+y*ϵ`, where `x` and `y` are
real numbers.

Apart from their mathematical role in algebraic and differential geometry (they
are mainly interpreted as angles between lines), they also find applications in
physics (the real part of a dual represents the bosonic direction, while the
epsilon part represents the fermionic direction), in screw theory, in motor
and spatial vector algebra, and in computer science due to its relation with the
Dual numbers extend the real numbers, similar to complex numbers. They adjoin a
new element `ɛ` such that `ɛ*ɛ=0`, in a similar way that complex numbers
adjoin the imaginary unit `i` with the property `i*i=-1`. So the typical
representation of a dual number takes the form `x+y*ɛ`, where `x` and `y` are
real numbers. Duality can further extend complex numbers by adjoining one new
element to each of the real and imaginary parts.

Apart from their mathematical role in algebraic and differential geometry (they
are mainly interpreted as angles between lines), they also find applications in
physics (the real part of a dual represents the bosonic direction, while the
epsilon part represents the fermionic direction), in screw theory, in motor
and spatial vector algebra, and in computer science due to its relation with the
forward mode of automatic differentiation.

The [ForwardDiff](https://github.com/scidom/ForwardDiff.jl) package implements forward mode automatic differentiation in Julia using several approaches. One
Expand All @@ -27,65 +28,71 @@ dual numbers in Julia.

## Supported functions

We aim for complete support for `Dual` types for numerical functions within Julia's
We aim for complete support for `Dual` types for numerical functions within Julia's
`Base`. Currently, basic mathematical operations and trigonometric functions are
supported.


The following functions are specific to dual numbers:
* `dual`,
* `dual128`,
* `dual64`,
* `realpart`,
* `dualpart`,
* `epsilon`,
* `isdual`,
* `dual_show`,
* `conjdual`,
* `absdual`,
* `abs2dual`.

In some cases the mathematical definition of functions of ``Dual`` numbers
is in conflict with their use as a drop-in replacement for calculating
numerical derivatives, for example, ``conj``, ``abs`` and ``abs2``. In these
cases, we choose to follow the rule ``f(x::Dual) = Dual(f(real(x)),epsilon(x)*f'(real(x)))``,
where ``f'`` is the derivative of ``f``. The mathematical definitions are
available using the functions with the suffix ``dual``.
Similarly, comparison operators ``<``, ``>``, and ``==`` are overloaded to compare only real
components.
The dual number `f(a+bɛ)` is defined by the limit:

f(a+bɛ) := f(a) + lim_{h→0} (f(a + bɛh) - f(a))/h .

For complex differentiable functions, this is equivalent to differentiation:

f(a+bɛ) := f(a) + b f'(a) ɛ.

For functions that are not complex differentiable, the dual part returns the limit
and can be identified with a directional derivative in ``.

In some cases the mathematical definition of functions of ``Dual`` numbers
is in conflict with their use as a drop-in replacement for calculating
numerical derivatives, for example, ``conj``, ``abs`` and ``abs2``. The mathematical
definitions are available using the functions with the suffix ``dual``.
Similarly, comparison operators ``<``, ``>``, and ``==`` are overloaded to compare only value
components.

### A walk-through example

The example below demonstrates basic usage of dual numbers by employing them to
perform automatic differentiation. The code for this example can be found in
The example below demonstrates basic usage of dual numbers by employing them to
perform automatic differentiation. The code for this example can be found in
`test/automatic_differentiation_test.jl`.

First install the package by using the Julia package manager:

Pkg.update()
Pkg.add("DualNumbers")

Then make the package available via

using DualNumbers

Use the `dual()` function to define the dual number `2+1*du`:
Use the `Dual()` constructor to define the dual number `2+1*ɛ`:

x = dual(2, 1)
x = Dual(2, 1)

Define a function that will be differentiated, say

f(x) = x^3

Perform automatic differentiation by passing the dual number `x` as argument to
Perform automatic differentiation by passing the dual number `x` as argument to
`f`:

y = f(x)

Use the functions `real()` and `epsilon()` to get the real and imaginary (dual)
Use the functions `realpart` and `dualpart` to get the concrete and dual
parts of `x`, respectively:

println("f(x) = x^3")
println("f(2) = ", real(y))
println("f'(2) = ", epsilon(y))
println("f(2) = ", realpart(y))
println("f'(2) = ", dualpart(y))
3 changes: 1 addition & 2 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
julia 0.3
julia 0.4
Calculus
NaNMath
Compat
22 changes: 15 additions & 7 deletions src/DualNumbers.jl
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
isdefined(Base, :__precompile__) && __precompile__()
__precompile__()

module DualNumbers
importall Base

using Compat
import NaNMath
import Calculus

include("dual.jl")
include("dual_n.jl")

Base.@deprecate_binding du ɛ
@deprecate inf{T}(::Type{Dual{T}}) convert(Dual{T}, Inf)
@deprecate nan{T}(::Type{Dual{T}}) convert(Dual{T}, NaN)
@deprecate real{T<:Real}(z::Dual{T}) realpart(z)

export
Dual,
Dual128,
Dual64,
DualPair,
Dual32,
DualComplex256,
DualComplex128,
DualComplex64,
dual,
dual128,
dual64,
epsilon,
realpart,
dualpart,
isdual,
dual_show,
epsilon,
conjdual,
absdual,
abs2dual,
du
ɛ,
imɛ
end
Loading

0 comments on commit 5bc6760

Please sign in to comment.