I wonder how can I implement dual numbers in Mathematica, so that all functions work well with them (as with complex numbers).
Particularly, for each function $f$, $f(\varepsilon)=f^\prime(0)\varepsilon$
|
I wonder how can I implement dual numbers in Mathematica, so that all functions work well with them (as with complex numbers). Particularly, for each function $f$, $f(\varepsilon)=f^\prime(0)\varepsilon$ |
|||||
|
|
If you use the matrix representation, addition and multiplication works by default, but you need to use matrix multiplication and not just space (which is element-wise multiplication), for instance If you just want to have the rule enforce that you mention, you could just write that rule out:
You can add other rules to make your other algebra work out aswell for intance for multiplication:
Update If you want the full function application rule, then there is a problem with just defining it, lets assume you wanted to write down:
Then you can write the definition for function application with respect to
And if you would like the numbers to be printed like
Now then this works together to allow:
Edited to correct error caught by Rahul Narain, also to improve incorrect output formating in symbolic expressions. |
|||||||||||||
|
|
Possibly a function that applies functions, by expanding to first order in the dualE part.
This has the inconvenience of being awkward, since it would need to be used with basic arithmetic (but as noted in other responses, UpValues on dualE could perhaps address this). An advantage is that it should work in a reasonably consistent manner, and (I hope) not require much beyond what I did here; might need some special fault-handling if you encounter the likes of 1/dualE. |
|||
|
|
|
Here's a tiny piece of advice I follow: any time I want to implement a new, exotic number system in Mathematica, the first thing I do is to look within the implementation of the Having said this, here's a bunch of rules for doing basic arithmetic with dual numbers, as well as a rule for evaluating functions with dual number arguments (notice the heavy use of
Samples:
The last rule, while fairly general, will give erroneous results for functions like |
|||
|
|
|
You can map any dual number $a + b \epsilon$ into a $2 \times 2$ matrix: $$ a + b \epsilon \mapsto \begin{pmatrix} a & b \cr 0 & a \end{pmatrix} $$ |
|||||||
|