Merge branch 'master' of git://factorforge.org/git/drford

db4
Slava Pestov 2008-08-05 19:31:53 -05:00
commit a2c680484b
7 changed files with 58 additions and 9 deletions

View File

@ -0,0 +1,34 @@
USING: help.markup help.syntax ;
IN: extra.animations
HELP: animate ( quot duration -- )
{ $values
{ "quot" "a quot which uses " { $link progress } }
{ "duration" "a duration of time" }
}
{ $description { $link animate } " calls " { $link reset-progress } " , then continously calls the given quot until the duration of time has elapsed. The quot should use " { $link progress } " at least once." }
{ $example
"USING: extra.animations calendar threads prettyprint ;"
"[ 1 sleep progress unparse write \" ms elapsed\" print ] 1/20 seconds animate ;"
"46 ms elapsed\n17 ms elapsed"
} ;
HELP: reset-progress ( -- )
{ $description "Initiates the timer. Call this before using a loop which makes use of " { $link progress } "." } ;
HELP: progress ( -- time )
{ $values { "time" "an integer" } }
{ $description "Gives the time elapsed since the last time this word was called, in milliseconds." }
{ $example
"USING: extra.animations threads prettyprint ;"
"reset-progress 3 [ 1 sleep progress unparse write \"ms elapsed\" print ] times ;"
"31 ms elapsed\n18 ms elapsed\n16 ms elapsed"
} ;
ARTICLE: "extra.animations" "Animations"
"Provides a lightweight framework for properly simulating continuous functions of real time. This framework helps one create animations that use rates which do not change across platforms. The speed of the computer should correlate with the smoothness of the animation, not the speed of the animation!"
{ $subsection animate }
{ $subsection reset-progress }
{ $subsection progress }
{ $link progress } " specifically provides the length of time since " { $link reset-progress } " was called, and also calls " { $link reset-progress } " as its last action. This can be directly used when one's quote runs for a specific number of iterations, instead of a length of time. If the animation is like most, and is expected to run for a specific length of time, " { $link animate } " should be used." ;
ABOUT: "extra.animations"

View File

@ -0,0 +1,12 @@
! Small library for cross-platform continuous functions of real time
USING: kernel shuffle system locals
prettyprint math io namespaces threads calendar ;
IN: extra.animations
SYMBOL: last-loop
: reset-progress ( -- ) millis last-loop set ;
: progress ( -- progress ) millis last-loop get - reset-progress ;
: set-end ( duration -- end-time ) dt>milliseconds millis + ;
: loop ( quot end -- ) dup millis > [ [ dup call ] dip loop ] [ 2drop ] if ;
: animate ( quot duration -- ) reset-progress set-end loop ;

View File

@ -0,0 +1 @@
Reginald Keith Ford II

View File

@ -3,7 +3,8 @@
USING: kernel math math.points math.function-tools ;
IN: math.derivatives
: small-amount ( -- n ) 1.0e-12 ;
: near ( x -- y ) small-amount + ;
: derivative ( x function -- m ) 2dup [ near ] dip [ eval ] 2bi@ slope ;
: small-amount ( -- n ) 1.0e-14 ;
: some-more ( x -- y ) small-amount + ;
: some-less ( x -- y ) small-amount - ;
: derivative ( x function -- m ) [ [ some-more ] dip eval ] [ [ some-less ] dip eval ] 2bi slope ;
: derivative-func ( function -- function ) [ derivative ] curry ;

View File

@ -1,8 +1,9 @@
! Copyright © 2008 Reginald Keith Ford II
! Tools for quickly comparing, transforming, and evaluating mathematical Factor functions
USING: kernel math arrays ;
USING: kernel math arrays sequences sequences.lib ;
IN: math.function-tools
: difference-func ( func func -- func ) [ bi - ] 2curry ;
: eval ( x func -- pt ) dupd call 2array ;
: eval-inverse ( y func -- pt ) dupd call swap 2array ;
: eval3d ( x y func -- pt ) [ 2dup ] dip call 3array ;

View File

@ -1,4 +1,4 @@
! Copyright © 2008 Reginald Keith Ford II
! Copyright © 2008 Reginald Keith Ford II
! Newton's Method of approximating roots
USING: kernel math math.derivatives ;
@ -6,6 +6,6 @@ IN: math.newtons-method
<PRIVATE
: newton-step ( x function -- x2 ) dupd [ call ] [ derivative ] 2bi / - ;
: newton-precision ( -- n ) 7 ;
: newton-precision ( -- n ) 13 ;
PRIVATE>
: newton-method ( guess function -- x ) newton-precision [ [ newton-step ] keep ] times drop ;
: newtons-method ( guess function -- x ) newton-precision [ [ newton-step ] keep ] times drop ;

View File

@ -7,8 +7,8 @@ IN: math.secant-method
<PRIVATE
: secant-solution ( x1 x2 function -- solution ) [ eval ] curry bi@ linear-solution ;
: secant-step ( x1 x2 func -- x2 x3 func ) 2dup [ secant-solution ] 2dip swapd ;
: secant-precision ( -- n ) 11 ;
: secant-precision ( -- n ) 15 ;
PRIVATE>
: secant-method ( left right function -- x ) secant-precision [ secant-step ] times drop v+ 2 v*n ;
: secant-method ( left right function -- x ) secant-precision [ secant-step ] times drop + 2 / ;
! : 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 ;