diff --git a/extra/math/derivatives/derivatives.factor b/extra/math/derivatives/derivatives.factor new file mode 100644 index 0000000000..b5bbfa7fbe --- /dev/null +++ b/extra/math/derivatives/derivatives.factor @@ -0,0 +1,8 @@ +! Copyright © 2008 Reginald Keith Ford II +! Tool for computing the derivative of a function at a point +USING: kernel math math.points math.function-tools ; +IN: derivatives + +: small-amount ( -- n ) 1.0e-12 ; +: near ( x -- y ) small-amount + ; +: derivative ( x function -- m ) 2dup [ near ] dip [ eval ] 2bi@ slope ; \ No newline at end of file diff --git a/extra/math/function-tools/function-tools.factor b/extra/math/function-tools/function-tools.factor new file mode 100644 index 0000000000..3f719767d3 --- /dev/null +++ b/extra/math/function-tools/function-tools.factor @@ -0,0 +1,8 @@ +! Copyright © 2008 Reginald Keith Ford II +! Tools for quickly comparing and evaluating mathematical Factor functions + +USING: kernel math arrays ; +IN: math.function-tools +: difference-func ( func func -- func ) [ bi - ] 2curry ; +: eval ( x func -- pt ) dupd call 2array ; +: eval3d ( x y func -- pt ) [ 2dup ] dip call 3array ; \ No newline at end of file diff --git a/extra/math/newtons-method/newtons-method.factor b/extra/math/newtons-method/newtons-method.factor new file mode 100644 index 0000000000..55241c523b --- /dev/null +++ b/extra/math/newtons-method/newtons-method.factor @@ -0,0 +1,11 @@ +! Copyright © 2008 Reginald Keith Ford II +! Newton's Method of approximating roots + +USING: kernel math math.derivatives derivatives ; +IN: newtons-method + + +: newton-method ( guess function -- x ) newton-precision [ [ newton-step ] keep ] times drop ; diff --git a/extra/math/points/points.factor b/extra/math/points/points.factor index 5efd6e07e0..c8654869e2 100644 --- a/extra/math/points/points.factor +++ b/extra/math/points/points.factor @@ -1,5 +1,4 @@ - -USING: kernel arrays math.vectors ; +USING: kernel arrays math.vectors sequences math ; IN: math.points @@ -20,3 +19,9 @@ PRIVATE> : v+z ( seq z -- seq ) Z v+ ; : v-z ( seq z -- seq ) Z v- ; +: rise ( pt2 pt1 -- n ) [ second ] bi@ - ; +: run ( pt2 pt1 -- n ) [ first ] bi@ - ; +: slope ( pt pt -- slope ) [ rise ] [ run ] 2bi / ; +: distance ( point point -- float ) v- norm ; +: midpoint ( point point -- point ) v+ 2 v/n ; +: linear-solution ( pt pt -- x ) [ drop first2 ] [ slope ] 2bi / - ; \ No newline at end of file diff --git a/extra/math/secant-method/secant-method.factor b/extra/math/secant-method/secant-method.factor new file mode 100644 index 0000000000..2089dde848 --- /dev/null +++ b/extra/math/secant-method/secant-method.factor @@ -0,0 +1,14 @@ +! Copyright © 2008 Reginald Keith Ford II +! Secant Method of approximating roots + +USING: kernel math math.function-tools math.points math.vectors ; +IN: math.secant-method + + +: secant-method ( left right function -- x ) secant-precision [ secant-step ] times drop v+ 2 v*n ; +! : close-enough? ( a b -- t/f ) - abs tiny-amount < ; +! : secant-method2 ( left right function -- x ) 2over close-enough? [ drop average ] [ secant-step secant-method ] if ; \ No newline at end of file