]> gitweb.factorcode.org Git - factor.git/blob - extra/math/derivatives/derivatives.factor
ee30d534bb093d0c1bcc176033539138afc92b7d
[factor.git] / extra / math / derivatives / derivatives.factor
1 ! Copyright (C) 2009 Jason W. Merrill.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel math math.functions math.derivatives.syntax 
4     math.order math.parser summary accessors make combinators ;
5 IN: math.derivatives
6
7 ERROR: undefined-derivative point word ;
8 M: undefined-derivative summary
9     [ dup "Derivative of " % word>> name>> % 
10     " is undefined at " % point>> # "." % ]
11     "" make ;
12
13 DERIVATIVE: + [ 2drop ] [ 2drop ]
14 DERIVATIVE: - [ 2drop ] [ 2drop neg ]
15 DERIVATIVE: * [ nip * ] [ drop * ]
16 DERIVATIVE: / [ nip / ] [ sq / neg * ]
17 ! Conditional checks if the epsilon-part of the exponent is 
18 ! 0 to avoid getting float answers for integer powers.
19 DERIVATIVE: ^ [ [ 1 - ^ ] keep * * ] 
20     [ [ dup zero? ] 2dip [ 3drop 0 ] [ [ ^ ] keep log * * ] if ]
21
22 DERIVATIVE: abs 
23     [ 0 <=> 
24         { 
25             { +lt+ [ neg ] } 
26             { +eq+ [ 0 \ abs undefined-derivative ] } 
27             { +gt+ [ ] } 
28         } case
29     ]
30
31 DERIVATIVE: sqrt [ sqrt 2 * / ]
32
33 DERIVATIVE: e^ [ e^ * ]
34 DERIVATIVE: log [ / ]
35
36 DERIVATIVE: sin [ cos * ]
37 DERIVATIVE: cos [ sin neg * ]
38 DERIVATIVE: tan [ sec sq * ]
39
40 DERIVATIVE: sinh [ cosh * ]
41 DERIVATIVE: cosh [ sinh * ]
42 DERIVATIVE: tanh [ sech sq * ]
43
44 DERIVATIVE: asin [ sq neg 1 + sqrt / ]
45 DERIVATIVE: acos [ sq neg 1 + sqrt neg / ]
46 DERIVATIVE: atan [ sq 1 + / ]
47
48 DERIVATIVE: asinh [ sq 1 + sqrt / ]
49 DERIVATIVE: acosh [ [ 1 + sqrt ] [ 1 - sqrt ] bi * / ]
50 DERIVATIVE: atanh [ sq neg 1 + / ]
51
52 DERIVATIVE: neg [ drop neg ]
53 DERIVATIVE: recip [ sq recip neg * ]