# Paste: dual numbers

Author: | Jason Merrill |

Mode: | factor |

Date: | Fri, 6 Feb 2009 00:16:35 |

Plain Text |
USING: kernel math math.functions math.derivatives accessors words
generalizations sequences generic.parser fry locals compiler.units
continuations quotations combinators macros ;
IN: math.dual
TUPLE: dual ordinary-part epsilon-part ;
C: <dual> dual
M: number ordinary-part>> ;
M: number epsilon-part>> drop 0 ;
: unpack-dual
[ ordinary-part>> ] [ epsilon-part>> ] bi ;
<PRIVATE
:: chain-rule
{ [ [ epsilon-part>> ] n napply ] [ [ ordinary-part>> ] n napply ] }
n ncleave
derivative-list [ n ncurry ] n nwith map
spread n narray sum ; inline
PRIVATE>
:: [dual-op]
word "derivative" word-prop :> derivative-list
derivative-list length :> n
[ [ [ ordinary-part>> ] n napply word execute ] n nkeep
derivative-list n chain-rule
<dual> ] ;
MACRO: dual-op [dual-op] ;
: define-dual-method
[ \ dual swap create-method-in ] keep [dual-op] define ;
<< { sqrt exp log sin cos tan sinh cosh tanh atan }
[ define-dual-method ] each >>
: d+ \ + dual-op ;
: d- \ - dual-op ;
: d* \ * dual-op ;
: d/ \ / dual-op ;
: d^ \ ^ dual-op ;

Author: | slava |

Mode: | factor |

Date: | Fri, 6 Feb 2009 00:28:47 |

Plain Text |
:: [dual-op]
word "derivative" word-prop :> derivative-list
derivative-list length :> n
n word '[ [ ordinary-part>> ] _ napply _ execute ] n
derivative-list n
'[
_ _ nkeep
_ _ chain-rule
<dual>
] ;

## New Annotation