]> gitweb.factorcode.org Git - factor.git/blob - extra/math/secant-method/secant-method.factor
Merge branch 'experimental' into couchdb
[factor.git] / extra / math / secant-method / secant-method.factor
1 ! Copyright © 2008 Reginald Keith Ford II
2 ! See http://factorcode.org/license.txt for BSD license.
3 ! Secant Method of approximating roots
4 USING: kernel math math.function-tools math.points math.vectors ;
5 IN: math.secant-method
6
7 <PRIVATE
8
9 : secant-solution ( x1 x2 function -- solution )
10     [ eval ] curry bi@ linear-solution ;
11
12 : secant-step ( x1 x2 func -- x2 x3 func )
13     [ secant-solution ] 2keep swapd ;
14
15 : secant-precision ( -- n ) 15 ; inline
16
17 PRIVATE>
18
19 : secant-method ( left right function -- x )
20     secant-precision [ secant-step ] times drop + 2 / ;
21
22 ! : close-enough? ( a b -- t/f ) - abs tiny-amount < ;
23
24 ! : secant-method2 ( left right function -- x )
25     ! 2over close-enough?
26     ! [ drop average ] [ secant-step secant-method ] if  ;