JuliaDiff / DualNumbers.jl

Julia package for representing dual numbers and for performing dual algebra
Other
80 stars 30 forks source link

Implement mod(z::Dual, n::Number) #25

Closed tomasaschan closed 9 years ago

tomasaschan commented 9 years ago

I just made this work on the real part and leave the dual part as-is, because that's what I need in my application. I guess that should be OK at least in some aspect, since anywhere mod is applicable, periodicity is implied which should make the derivative the same, but if you have better ideas, let me know :)

tomasaschan commented 9 years ago

Oh, I should add a couple of tests. Brb... Tests added and squashed.

mlubin commented 9 years ago

Could you give an example where mod of a dual number makes sense?

tomasaschan commented 9 years ago

Sure: right here.

It might not be so straightforward just from the code, but the application is interpolation of data, in this case with periodic extrapolation (we use mod for reflecting extrapolation too). We've taken care to allow for indexing with anything that isa(x, Number), and dual numbers as indices have a very real use case for evaluating both the interpolant and its approximate derivative (i.e. if the interpolant is a quadratic spline, the derivative is a piece-wise linear function) simultaneously.

Right before we actually forward the indices to the wrapped arrays, we use convert real(x) to an integer to avoid InexactErrors, but there can be quite a few things happening before we reach that point. DualNumbers have been sort-of our guinea pig for trying out that this all works (mainly because we can then also use it to test our implementation of the gradient function...) so there might be lots of other Number types that we think we support but don't - currently, the spec for what works is this:

tomasaschan commented 9 years ago

@mlubin Any further comments on this?

mlubin commented 9 years ago

Squashing would be appreciated

tomasaschan commented 9 years ago

Done.

tomasaschan commented 9 years ago

Thanks a lot for merging! It would be extra awesome if you were able to tag a new version as well, to allow the tests in tlycken/Interpolations.jl#50 to pass without me having to modify my build script to check out the master branch of DualNumbers.jl :)

mlubin commented 9 years ago

Done. CC @jrevels for port to ForwardDiff