}
{ $description "Reciprocal of a dual number." } ;
-HELP: define-dual-method
+HELP: define-dual
{ $values
{ "word" word }
}
-{ $description "Defines a method on the dual numbers for generic word." }
+{ $description "Defines a word " { $snippet "d[word]" } " in the " { $vocab-link "math.dual" } " vocabulary that operates on dual numbers." }
{ $notes "Uses the derivative word-prop, which holds a list of quotations giving the partial derivatives of the word with respect to each of its arguments. This can be set using " { $link POSTPONE: DERIVATIVE: } "." } ;
-{ define-dual-method dual-op POSTPONE: DERIVATIVE: } related-words
+{ define-dual dual-op POSTPONE: DERIVATIVE: } related-words
HELP: dual
{ $class-description "The class of dual numbers with non-zero epsilon part." } ;
math.constants ;
IN: math.dual.tests
-[ 0.0 1.0 ] [ 0 1 <dual> sin unpack-dual ] unit-test
-[ 1.0 0.0 ] [ 0 1 <dual> cos unpack-dual ] unit-test
+[ 0.0 1.0 ] [ 0 1 <dual> dsin unpack-dual ] unit-test
+[ 1.0 0.0 ] [ 0 1 <dual> dcos unpack-dual ] unit-test
[ 3 5 ] [ 1 5 <dual> 2 d+ unpack-dual ] unit-test
[ 0 -1 ] [ 1 5 <dual> 1 6 <dual> d- unpack-dual ] unit-test
[ 2 1 ] [ 2 3 <dual> 1 -1 <dual> d* unpack-dual ] unit-test
[ 1/2 -1/4 ] [ 2 1 <dual> 1 swap d/ unpack-dual ] unit-test
[ 2 ] [ 1 1 <dual> 2 d^ epsilon-part>> ] unit-test
-[ 2.0 .25 ] [ 4 1 <dual> sqrt unpack-dual ] unit-test
+[ 2.0 .25 ] [ 4 1 <dual> dsqrt unpack-dual ] unit-test
[ 2 -1 ] [ -2 1 <dual> dabs unpack-dual ] unit-test
[ -2 -1 ] [ 2 1 <dual> dneg unpack-dual ] unit-test
\ No newline at end of file
! Copyright (C) 2009 Jason W. Merrill.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel math math.functions math.derivatives accessors
- macros words effects sequences generalizations fry
+ macros words effects vocabs sequences generalizations fry
combinators.smart generic compiler.units ;
IN: math.dual
tri
'[ _ @ @ <dual> ] ;
-: define-dual-method ( word -- )
- [ \ dual swap create-method ] keep '[ _ dual-op ] define ;
+: define-dual ( word -- )
+ [
+ [ stack-effect ]
+ [ name>> "d" prepend "math.dual" create ]
+ bi [ set-stack-effect ] keep
+ ]
+ keep
+ '[ _ dual-op ] define ;
! Specialize math functions to operate on dual numbers.
-[ { sqrt exp log sin cos tan sinh cosh tanh acos asin atan }
- [ define-dual-method ] each ] with-compilation-unit
-
-! Inverse methods { asinh, acosh, atanh } are not generic, so
-! there is no way to specialize them for dual numbers. However,
-! they are defined in terms of functions that can operate on
-! dual numbers and arithmetic methods, so if it becomes
-! possible to make arithmetic operators work directly on dual
-! numbers, we will get these for free.
-
-! Arithmetic words are not generic (yet?), so we have to
-! define special versions of them to operate on dual numbers.
-: 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 ;
-
-: dabs ( x -- |x| ) \ abs dual-op ;
-
-! The following words are also not generic, but are defined in
-! terms of words that can operate on dual numbers and
-! arithmetic. If it becomes possible to implement arithmetic on
-! dual numbers directly, these functions can be deleted.
-: dneg ( x -- -x ) \ neg dual-op ;
-: drecip ( x -- 1/x ) \ recip dual-op ;
-: dasinh ( x -- y ) \ asinh dual-op ;
-: dacosh ( x -- y ) \ acosh dual-op ;
-: datanh ( x -- y ) \ atanh dual-op ;
\ No newline at end of file
+[ all-words [ "derivative" word-prop ] filter
+ [ define-dual ] each ] with-compilation-unit
\ No newline at end of file