Paste: dual numbers
Author: | Jason Merrill |
Mode: | factor |
Date: | Thu, 5 Feb 2009 01:01:24 |
Plain Text |
USING: kernel math math.functions math.derivatives accessors words
generalizations sequences generic.parser fry locals compiler.units
continuations quotations ;
IN: math.dual
TUPLE: dual ordinary-part epsilon-part ;
C: <dual> dual
M: number ordinary-part>> ;
M: number epsilon-part>> drop 0 ;
: unpack-dual ( dual -- ordinary-part epsilon-part )
[ ordinary-part>> ] [ epsilon-part>> ] bi ;
<PRIVATE
:: chain-rule ( epsilon-list ordinary-list derivative-list -- d )
epsilon-list derivative-list
[ '[ ordinary-list _ prefix _ with-datastack first ] call ]
2map sum ; inline
:: (dual-op) ( epsilon-list ordinary-list word derivative-list -- dual )
ordinary-list word 1quotation with-datastack first
epsilon-list ordinary-list derivative-list chain-rule
<dual> ;
PRIVATE>
: dual-op ( duals word -- dual )
dup "derivative" word-prop dup length
'[ _ narray [ [ epsilon-part>> ] map ] [ [ ordinary-part>> ] map ] bi
] 2dip (dual-op) ; inline
: define-dual ( word -- )
[ \ dual swap create-method-in ] keep [ dual-op ] curry define ;
<< { sqrt exp log sin cos tan sinh cosh tanh atan } [ define-dual ] each >>
: d+ ( x y -- x+y ) \ + dual-op ;
: d- ( x y -- x-y ) \ - dual-op ;
: d* ( x y -- x*y ) \ * dual-op ;
: d/ ( x y -- x/y ) \ / dual-op ;
: d^ ( x y -- x^y ) \ ^ dual-op ;
Author: | Jason Merrill |
Mode: | factor |
Date: | Thu, 5 Feb 2009 01:02:43 |
Plain Text |
USING: kernel math math.functions math.derivatives.parser ;
IN: math.derivatives
DERIVATIVE: + [ 2drop ] [ 2drop ]
DERIVATIVE: - [ 2drop ] [ 2drop neg ]
DERIVATIVE: * [ nip * ] [ drop * ]
DERIVATIVE: / [ nip / ] [ sq / neg * ]
DERIVATIVE: ^ [ [ 1 - ^ ] keep * * ]
[ [ dup zero? ] 2dip [ 3drop 0 ] [ [ ^ ] keep log * * ] if ]
DERIVATIVE: sqrt [ 2 * / ]
DERIVATIVE: exp [ exp * ]
DERIVATIVE: log [ / ]
DERIVATIVE: sin [ cos * ]
DERIVATIVE: cos [ sin neg * ]
DERIVATIVE: tan [ sec sq * ]
DERIVATIVE: sinh [ cosh * ]
DERIVATIVE: cosh [ sinh * ]
DERIVATIVE: tanh [ sech sq * ]
DERIVATIVE: asin [ sq neg 1 + sqrt / ]
DERIVATIVE: acos [ sq neg 1 + sqrt neg / ]
DERIVATIVE: atan [ sq 1 + / ]
DERIVATIVE: asinh [ sq 1 + sqrt / ]
DERIVATIVE: acosh [ [ 1 + sqrt ] [ 1 - sqrt ] bi * / ]
DERIVATIVE: atanh [ sq neg 1 + / ]
Author: | Jason Merrill |
Mode: | factor |
Date: | Thu, 5 Feb 2009 01:03:34 |
Plain Text |
USING: kernel parser words effects accessors sequences math.ranges ;
IN: math.derivatives.parser
: DERIVATIVE: scan-object dup stack-effect in>> length [1,b]
[ drop scan-object ] map "derivative" set-word-prop ; parsing
New Annotation