minor fixes

db4
Rex Ford 2008-08-10 18:20:14 -05:00
parent a44097af93
commit 6df077805d
2 changed files with 5 additions and 26 deletions

View File

@ -17,20 +17,10 @@ HELP: derivative ( x function -- m )
} }
{ $notes { $notes
"For applied scientists, you may play with the settings " "For applied scientists, you may play with the settings "
"in the source file to achieve arbitrary accuracy." "in the source file to achieve arbitrary accuracy. "
} }
} ; } ;
HELP: fast-derivative ( x function -- m )
{ $values { "x" "a x-position on the function" } { "function" "a differentiable function" } }
{ $description
"Approximates the slope of the tangent line of the provided function "
"by using a secant line with very near points. This implementation is "
"naive and is only provided because it is used in the much more "
"accurate " { $link derivative } " word. Use this word if accuracy "
"is of no importance."
} ;
HELP: derivative-func ( function -- der ) HELP: derivative-func ( function -- der )
{ $values { "func" "a differentiable function" } { "der" "the derivative" } } { $values { "func" "a differentiable function" } { "der" "the derivative" } }
{ $description { $description
@ -48,6 +38,5 @@ HELP: derivative-func ( function -- der )
ARTICLE: "derivatives" "The Derivative Toolkit" ARTICLE: "derivatives" "The Derivative Toolkit"
"A toolkit for computing the derivative of functions." "A toolkit for computing the derivative of functions."
{ $subsection derivative } { $subsection derivative }
{ $subsection derivative-func } { $subsection derivative-func } ;
{ $subsection fast-derivative } ;
ABOUT: "derivatives" ABOUT: "derivatives"

View File

@ -1,19 +1,13 @@
! Copyright (c) 2008 Reginald Ford
! Tools for approximating derivatives ! Tools for approximating derivatives
USING: kernel math math.functions locals generalizations float-arrays sequences USING: kernel math math.functions locals generalizations float-arrays sequences
math.constants namespaces math.function-tools math.points math.ranges math.order ; math.constants namespaces math.function-tools math.points math.ranges math.order ;
IN: math.derivatives IN: math.derivatives
! Ridders' method of a derivative, from the chapter
! "Accurate computation of F'(x) and F'(x)F''(x)",
! From "Advances in Engineering Software, Vol. 4, pp. 75-76
! \ fast-derivative has been factored out for use by children
: largest-float ( -- x ) HEX: 7fefffffffffffff bits>double ; foldable : largest-float ( -- x ) HEX: 7fefffffffffffff bits>double ; foldable
: ntab 10 ; ! max size of tableau (main accuracy setting) : ntab 10 ; ! max size of tableau (main accuracy setting)
: con 1.41 ; ! stepsize is decreased by this per-iteration : con 1.41 ; ! stepsize is decreased by this per-iteration
: con2 1.9881 ; ! con^2 : con2 1.9881 ; ! con^2
: initial-h 0.02 ; ! distance of the 2 points of the first secant line : initial-h 0.02 ; ! distance of the 2 points of the first secant line
: safe 2.0 ; ! return when current err is SAFE worse than the best : safe 2.0 ; ! return when current err is SAFE worse than the best
! \ safe probably should not be changed ! \ safe probably should not be changed
@ -30,9 +24,6 @@ SYMBOL: matrix
[ [ h get + ] dip eval ] [ [ h get + ] dip eval ]
[ [ h get - ] dip eval ] [ [ h get - ] dip eval ]
2bi slope ; inline 2bi slope ; inline
: fast-derivative ( x function -- m )
over epsilon sqrt * h set
(derivative) ; inline
: init-matrix ( -- ) : init-matrix ( -- )
ntab [ ntab <float-array> ] replicate ntab [ ntab <float-array> ] replicate
matrix set ; matrix set ;
@ -70,5 +61,4 @@ SYMBOL: matrix
] all? drop ] all? drop
ans get ; inline ans get ; inline
: derivative-func ( function -- function ) [ derivative ] curry ; inline : derivative-func ( function -- function ) [ derivative ] curry ; inline
: fast-derivative-func ( function -- function ) [ fast-derivative ] curry ; inline