From 8785b24e0493e45893f13b07ececc08b1a2f46fe Mon Sep 17 00:00:00 2001 From: Rex Ford Date: Sun, 10 Aug 2008 16:44:17 -0500 Subject: [PATCH 01/20] Now with arbitrary accuracy --- .../math/derivatives/derivatives-docs.factor | 50 +++++++++++- extra/math/derivatives/derivatives.factor | 80 +++++++++++++++++-- 2 files changed, 119 insertions(+), 11 deletions(-) diff --git a/extra/math/derivatives/derivatives-docs.factor b/extra/math/derivatives/derivatives-docs.factor index 23847e82f7..70389f18ad 100644 --- a/extra/math/derivatives/derivatives-docs.factor +++ b/extra/math/derivatives/derivatives-docs.factor @@ -3,7 +3,51 @@ USING: help.markup help.syntax ; IN: math.derivatives HELP: derivative ( x function -- m ) -{ $values { "x" "the x-position on the function" } { "function" "a differentiable function" } } -{ $description "Finds the slope of the tangent line at the given x-position on the given function." } ; +{ $values { "x" "a position on the function" } { "function" "a differentiable function" } } +{ $description + "Approximates the slope of the tangent line by using Ridders' " + "method of computing derivatives, from the chapter \"Accurate computation " + "of F'(x) and F'(x)F''(x)\", from \"Advances in Engineering Software, Vol. 4, pp. 75-76 ." +} +{ $examples + { $example + "USING: math.derivatives prettyprint ;" + "[ sq ] 4 derivative ." + "8" + } + { $notes + "For applied scientists, you may play with the settings " + "in the source file to achieve arbitrary accuracy." + } +} ; -{ derivative-func } related-words +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 ) +{ $values { "func" "a differentiable function" } { "der" "the derivative" } } +{ $description + "Provides the derivative of the function. The implementation simply " + "attaches the " { $link derivative } " word to the end of the function." +} +{ $examples + { $example + "USING: math.derivatives prettyprint ;" + "[ sq ] derivative-func ." + "[ [ sq ] derivative ]" + } +} ; + +ARTICLE: "derivatives" "The Derivative Toolkit" +"A toolkit for computing the derivative of functions." +{ $subsection derivative } +{ $subsection derivative-func } +{ $subsection fast-derivative } ; +ABOUT: "derivatives" diff --git a/extra/math/derivatives/derivatives.factor b/extra/math/derivatives/derivatives.factor index d92066efaf..f77748d0b5 100644 --- a/extra/math/derivatives/derivatives.factor +++ b/extra/math/derivatives/derivatives.factor @@ -1,10 +1,74 @@ -! 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 ; +! Copyright (c) 2008 Reginald Ford +! Tools for approximating derivatives + +USING: kernel math math.functions locals generalizations float-arrays sequences +math.constants namespaces math.function-tools math.points math.ranges math.order ; IN: math.derivatives -: 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 ; \ No newline at end of file +! 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 +: ntab 10 ; ! max size of tableau (main accuracy setting) +: con 1.41 ; ! stepsize is decreased by this per-iteration +: con2 1.9881 ; ! con^2 +: 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 probably should not be changed +SYMBOL: i +SYMBOL: j +SYMBOL: err +SYMBOL: errt +SYMBOL: fac +SYMBOL: h +SYMBOL: ans +SYMBOL: matrix + +: (derivative) ( x function -- m ) + [ [ h get + ] dip eval ] + [ [ h get - ] dip eval ] + 2bi slope ; inline +: fast-derivative ( x function -- m ) + over epsilon sqrt * h set + (derivative) ; inline +: init-matrix ( -- ) + ntab [ ntab ] replicate + matrix set ; +: m-set ( value j i -- ) matrix get nth set-nth ; +: m-get ( j i -- n ) matrix get nth nth ; +:: derivative ( x func -- m ) + init-matrix + initial-h h set + x func (derivative) 0 0 m-set + largest-float err set + ntab 1 - [1,b] [| i | + h [ con / ] change + x func (derivative) 0 i m-set + con2 fac set + i [1,b] [| j | + j 1 - i m-get fac get * + j 1 - i 1 - m-get + - + fac get 1 - + / j i m-set + fac [ con2 * ] change + j i m-get j 1 - i m-get - abs + j i m-get j 1 - i 1 - m-get - abs + max errt set + errt get err get <= + [ + errt get err set + j i m-get ans set + ] [ ] + if + ] each + i i m-get i 1 - dup m-get - abs + err get safe * + < + ] all? drop + ans get ; inline +: derivative-func ( function -- function ) [ derivative ] curry ; inline +: fast-derivative-func ( function -- function ) [ fast-derivative ] curry ; inline + From aee8dbdba45abf06eab2e974b2985bc4e9c41ed5 Mon Sep 17 00:00:00 2001 From: Rex Ford Date: Sun, 10 Aug 2008 16:45:13 -0500 Subject: [PATCH 02/20] peer review by myself --- extra/24-game/24-game-docs.factor | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extra/24-game/24-game-docs.factor b/extra/24-game/24-game-docs.factor index 12a558b2d2..cd82f335d8 100644 --- a/extra/24-game/24-game-docs.factor +++ b/extra/24-game/24-game-docs.factor @@ -31,12 +31,12 @@ HELP: 24-able ( -- vector ) "just using the provided commands and the 4 numbers. The Following are the " "provided commands: " { $link + } ", " { $link - } ", " { $link * } ", " - { $link / } ", and " { $link swap } "." + { $link / } ", " { $link swap } ", and " { $link rot } "." } { $examples { $example "USE: 24-game" - "24-able vector-24-able?" + "24-able vector-24-able? ." "t" } { $notes { $link 24-able? } " is used in " { $link 24-able } "." } From 9d0acc555d51b1aba9918b8fd73269f6a5bd40cb Mon Sep 17 00:00:00 2001 From: Rex Ford Date: Sun, 10 Aug 2008 16:47:52 -0500 Subject: [PATCH 03/20] peer review by myself --- extra/animations/animations-docs.factor | 63 ++++++++++++++++++------- 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/extra/animations/animations-docs.factor b/extra/animations/animations-docs.factor index 6a1e89a28e..000c0ce4cc 100644 --- a/extra/animations/animations-docs.factor +++ b/extra/animations/animations-docs.factor @@ -1,34 +1,65 @@ USING: help.markup help.syntax ; -IN: extra.animations +IN: 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" +{ $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." +} +{ $examples + { $unchecked-example + "USING: animations calendar threads prettyprint ;" + "[ 1 sleep progress unparse write \" ms elapsed\" print ] " + "1/20 seconds animate ;" + "46 ms elapsed\n17 ms elapsed" + } + { $notes "The amount of time elapsed between these iterations will very." } } ; HELP: reset-progress ( -- ) -{ $description "Initiates the timer. Call this before using a loop which makes use of " { $link 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" +{ $description + "Gives the time elapsed since the last time" + " this word was called, in milliseconds." +} +{ $examples + { $unchecked-example + "USING: animations threads prettyprint ;" + "reset-progress 3 " + "[ 1 sleep progress unparse write \"ms elapsed\" print ] " + "times ;" + "31 ms elapsed\n18 ms elapsed\n16 ms elapsed" + } + { $notes "The amount of time elapsed between these iterations will very." } } ; -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!" +ARTICLE: "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" \ No newline at end of file +! A little talk about when to use progress and when to use animate + { $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: "animations" \ No newline at end of file From 6ab0f6b09c633f6543b1feee16babefd8aab287a Mon Sep 17 00:00:00 2001 From: Rex Ford Date: Sun, 10 Aug 2008 16:48:54 -0500 Subject: [PATCH 04/20] No one else used middle name --- extra/animations/authors.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extra/animations/authors.txt b/extra/animations/authors.txt index dac0cb42fe..137b1605da 100644 --- a/extra/animations/authors.txt +++ b/extra/animations/authors.txt @@ -1 +1 @@ -Reginald Keith Ford II \ No newline at end of file +Reginald Ford \ No newline at end of file From a44097af93cebbef61d8bee80da94c93618b3b49 Mon Sep 17 00:00:00 2001 From: Rex Ford Date: Sun, 10 Aug 2008 16:49:40 -0500 Subject: [PATCH 05/20] combinators should inline --- extra/math/function-tools/function-tools.factor | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/extra/math/function-tools/function-tools.factor b/extra/math/function-tools/function-tools.factor index 802bf9e14e..ec93a0891a 100644 --- a/extra/math/function-tools/function-tools.factor +++ b/extra/math/function-tools/function-tools.factor @@ -3,7 +3,7 @@ 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 ; +: difference-func ( func func -- func ) [ bi - ] 2curry ; inline +: eval ( x func -- pt ) dupd call 2array ; inline +: eval-inverse ( y func -- pt ) dupd call swap 2array ; inline +: eval3d ( x y func -- pt ) [ 2dup ] dip call 3array ; inline From 6df077805d5a89972aa1679a78565645c3a525ad Mon Sep 17 00:00:00 2001 From: Rex Ford Date: Sun, 10 Aug 2008 18:20:14 -0500 Subject: [PATCH 06/20] minor fixes --- extra/math/derivatives/derivatives-docs.factor | 15 ++------------- extra/math/derivatives/derivatives.factor | 16 +++------------- 2 files changed, 5 insertions(+), 26 deletions(-) diff --git a/extra/math/derivatives/derivatives-docs.factor b/extra/math/derivatives/derivatives-docs.factor index 70389f18ad..0db52adfa5 100644 --- a/extra/math/derivatives/derivatives-docs.factor +++ b/extra/math/derivatives/derivatives-docs.factor @@ -17,20 +17,10 @@ HELP: derivative ( x function -- m ) } { $notes "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 ) { $values { "func" "a differentiable function" } { "der" "the derivative" } } { $description @@ -48,6 +38,5 @@ HELP: derivative-func ( function -- der ) ARTICLE: "derivatives" "The Derivative Toolkit" "A toolkit for computing the derivative of functions." { $subsection derivative } -{ $subsection derivative-func } -{ $subsection fast-derivative } ; +{ $subsection derivative-func } ; ABOUT: "derivatives" diff --git a/extra/math/derivatives/derivatives.factor b/extra/math/derivatives/derivatives.factor index f77748d0b5..96d0fc3a81 100644 --- a/extra/math/derivatives/derivatives.factor +++ b/extra/math/derivatives/derivatives.factor @@ -1,19 +1,13 @@ -! Copyright (c) 2008 Reginald Ford ! Tools for approximating derivatives USING: kernel math math.functions locals generalizations float-arrays sequences math.constants namespaces math.function-tools math.points math.ranges math.order ; 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 -: ntab 10 ; ! max size of tableau (main accuracy setting) -: con 1.41 ; ! stepsize is decreased by this per-iteration -: con2 1.9881 ; ! con^2 +: ntab 10 ; ! max size of tableau (main accuracy setting) +: con 1.41 ; ! stepsize is decreased by this per-iteration +: con2 1.9881 ; ! con^2 : 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 probably should not be changed @@ -30,9 +24,6 @@ SYMBOL: matrix [ [ h get + ] dip eval ] [ [ h get - ] dip eval ] 2bi slope ; inline -: fast-derivative ( x function -- m ) - over epsilon sqrt * h set - (derivative) ; inline : init-matrix ( -- ) ntab [ ntab ] replicate matrix set ; @@ -70,5 +61,4 @@ SYMBOL: matrix ] all? drop ans get ; inline : derivative-func ( function -- function ) [ derivative ] curry ; inline -: fast-derivative-func ( function -- function ) [ fast-derivative ] curry ; inline From 6060b12ccb2d8e6a9ab4aafbe7d24e5e77cc75bf Mon Sep 17 00:00:00 2001 From: Rex Ford Date: Sun, 10 Aug 2008 18:22:32 -0500 Subject: [PATCH 07/20] minor additions --- extra/animations/animations.factor | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/extra/animations/animations.factor b/extra/animations/animations.factor index 7efd618bbf..db5b3448c1 100644 --- a/extra/animations/animations.factor +++ b/extra/animations/animations.factor @@ -2,11 +2,14 @@ USING: kernel shuffle system locals prettyprint math io namespaces threads calendar ; -IN: extra.animations +IN: animations SYMBOL: last-loop +SYMBOL: sleep-period + : 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 ; \ No newline at end of file +: loop ( quot end -- ) dup millis > [ [ dup call ] dip loop ] [ 2drop ] if ; inline +: animate ( quot duration -- ) reset-progress set-end loop ; inline +: sample ( revs quot -- avg ) reset-progress dupd times progress swap / ; inline \ No newline at end of file From bd168d06f258860a2d8c074d1e0335b02d6e1ec5 Mon Sep 17 00:00:00 2001 From: Rex Ford Date: Tue, 12 Aug 2008 00:28:22 -0400 Subject: [PATCH 08/20] now with progress-peek --- extra/animations/animations.factor | 2 ++ 1 file changed, 2 insertions(+) diff --git a/extra/animations/animations.factor b/extra/animations/animations.factor index db5b3448c1..803536a51c 100644 --- a/extra/animations/animations.factor +++ b/extra/animations/animations.factor @@ -8,7 +8,9 @@ SYMBOL: last-loop SYMBOL: sleep-period : reset-progress ( -- ) millis last-loop set ; +! : my-progress ( -- progress ) millis : progress ( -- progress ) millis last-loop get - reset-progress ; +: progress-peek ( -- progress ) millis last-loop get - ; : set-end ( duration -- end-time ) dt>milliseconds millis + ; : loop ( quot end -- ) dup millis > [ [ dup call ] dip loop ] [ 2drop ] if ; inline : animate ( quot duration -- ) reset-progress set-end loop ; inline From 2271aae7f070b5c373547f122d07861d239e74d1 Mon Sep 17 00:00:00 2001 From: Rex Ford Date: Tue, 12 Aug 2008 02:42:23 -0400 Subject: [PATCH 09/20] compatible with demos menu --- extra/24-game/24-game.factor | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/extra/24-game/24-game.factor b/extra/24-game/24-game.factor index 52f0cd6833..126215ab13 100644 --- a/extra/24-game/24-game.factor +++ b/extra/24-game/24-game.factor @@ -59,4 +59,5 @@ DEFER: check-status : 24-able? ( vector -- t/f ) [ makes-24? ] with-datastack first ; : 24-able ( -- vector ) build-quad dup 24-able? [ drop build-quad ] unless ; : set-commands ( -- ) { + - * / rot swap q } commands set ; -: play-game ( -- ) set-commands 24-able repeat ; \ No newline at end of file +: play-game ( -- ) set-commands 24-able repeat ; +MAIN: play-game \ No newline at end of file From 359bff5f154caaaaa9ed5037e78d43033cdf48b3 Mon Sep 17 00:00:00 2001 From: Rex Ford Date: Tue, 12 Aug 2008 11:24:00 -0400 Subject: [PATCH 10/20] Derivatives without dynamics OR locals --- extra/math/derivatives/authors.txt | 3 +- .../math/derivatives/derivatives-docs.factor | 66 ++++++- extra/math/derivatives/derivatives.factor | 175 ++++++++++++------ 3 files changed, 181 insertions(+), 63 deletions(-) diff --git a/extra/math/derivatives/authors.txt b/extra/math/derivatives/authors.txt index 137b1605da..3be8a6d4d3 100644 --- a/extra/math/derivatives/authors.txt +++ b/extra/math/derivatives/authors.txt @@ -1 +1,2 @@ -Reginald Ford \ No newline at end of file +Reginald Ford +Eduardo Cavazos \ No newline at end of file diff --git a/extra/math/derivatives/derivatives-docs.factor b/extra/math/derivatives/derivatives-docs.factor index 0db52adfa5..a78a697b76 100644 --- a/extra/math/derivatives/derivatives-docs.factor +++ b/extra/math/derivatives/derivatives-docs.factor @@ -1,5 +1,4 @@ -USING: help.markup help.syntax ; - +USING: help.markup help.syntax math.functions ; IN: math.derivatives HELP: derivative ( x function -- m ) @@ -21,6 +20,46 @@ HELP: derivative ( x function -- m ) } } ; +HELP: (derivative) ( x function h err -- m ) +{ $values + { "x" "a position on the function" } + { "function" "a differentiable function" } + { + "h" "distance between the points of the first secant line used for " + "approximation of the tangent. This distance will be divided " + "constantly, by " { $link con } ". See " { $link init-hh } + " for the code which enforces this. H should be .001 to .5 -- too " + "small can cause bad convergence. Also, h should be small enough " + "to give the correct sgn(f'(x)). In other words, if you're expecting " + "a positive derivative, make h small enough to give the same " + "when plugged into the academic limit definition of a derivative. " + "See " { $link update-hh } " for the code which performs this task." + } + { + "err" "maximum tolerance of increase in error. For example, if this " + "is set to 2.0, the program will terminate with its nearest answer " + "when the error multiplies by 2. See " { $link check-safe } " for " + "the enforcing code." + } +} +{ $description + "Approximates the slope of the tangent line by using Ridders' " + "method of computing derivatives, from the chapter \"Accurate computation " + "of F'(x) and F'(x)F''(x)\", from \"Advances in Engineering Software, " + "Vol. 4, pp. 75-76 ." +} +{ $examples + { $example + "USING: math.derivatives prettyprint ;" + "[ sq ] 4 derivative ." + "8" + } + { $notes + "For applied scientists, you may play with the settings " + "in the source file to achieve arbitrary accuracy. " + } +} ; + HELP: derivative-func ( function -- der ) { $values { "func" "a differentiable function" } { "der" "the derivative" } } { $description @@ -30,8 +69,27 @@ HELP: derivative-func ( function -- der ) { $examples { $example "USING: math.derivatives prettyprint ;" - "[ sq ] derivative-func ." - "[ [ sq ] derivative ]" + "60 deg>rad [ sin ] derivative-func call ." + "0.5000000000000173" + } + { $notes + "Without a heavy algebraic system, derivatives must be " + "approximated. With the current settings, there is a fair trade of " + "speed and accuracy; the first 12 digits " + "will always be correct with " { $link sin } " and " { $link cos } + ". The following code performs a minumum and maximum error test." + { $code + "USING: kernel math math.functions math.trig sequences sequences.lib ;" + "360" + "[" + " deg>rad" + " [ [ sin ] derivative-func call ]" + " ! Note: the derivative of sin is cos" + " [ cos ]" + " bi - abs" + "] map minmax" + + } } } ; diff --git a/extra/math/derivatives/derivatives.factor b/extra/math/derivatives/derivatives.factor index 96d0fc3a81..ad8d944bfe 100644 --- a/extra/math/derivatives/derivatives.factor +++ b/extra/math/derivatives/derivatives.factor @@ -1,64 +1,123 @@ -! Tools for approximating derivatives -USING: kernel math math.functions locals generalizations float-arrays sequences -math.constants namespaces math.function-tools math.points math.ranges math.order ; +USING: kernel continuations combinators sequences math + math.order math.ranges accessors float-arrays ; + IN: math.derivatives +TUPLE: state x func h err i j errt fac hh ans a done ; + : largest-float ( -- x ) HEX: 7fefffffffffffff bits>double ; foldable -: ntab 10 ; ! max size of tableau (main accuracy setting) -: con 1.41 ; ! stepsize is decreased by this per-iteration -: con2 1.9881 ; ! con^2 -: 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 probably should not be changed -SYMBOL: i -SYMBOL: j -SYMBOL: err -SYMBOL: errt -SYMBOL: fac -SYMBOL: h -SYMBOL: ans -SYMBOL: matrix +: ntab ( -- val ) 8 ; +: con ( -- val ) 1.6 ; +: con2 ( -- val ) con con * ; +: big ( -- val ) largest-float ; +: safe ( -- val ) 2.0 ; -: (derivative) ( x function -- m ) - [ [ h get + ] dip eval ] - [ [ h get - ] dip eval ] - 2bi slope ; inline -: init-matrix ( -- ) - ntab [ ntab ] replicate - matrix set ; -: m-set ( value j i -- ) matrix get nth set-nth ; -: m-get ( j i -- n ) matrix get nth nth ; -:: derivative ( x func -- m ) - init-matrix - initial-h h set - x func (derivative) 0 0 m-set - largest-float err set - ntab 1 - [1,b] [| i | - h [ con / ] change - x func (derivative) 0 i m-set - con2 fac set - i [1,b] [| j | - j 1 - i m-get fac get * - j 1 - i 1 - m-get - - - fac get 1 - - / j i m-set - fac [ con2 * ] change - j i m-get j 1 - i m-get - abs - j i m-get j 1 - i 1 - m-get - abs - max errt set - errt get err get <= - [ - errt get err set - j i m-get ans set - ] [ ] - if - ] each - i i m-get i 1 - dup m-get - abs - err get safe * - < - ] all? drop - ans get ; inline -: derivative-func ( function -- function ) [ derivative ] curry ; inline +! Yes, this was ported from C code. +: a[i][i] ( state -- elt ) [ i>> ] [ i>> ] [ a>> ] tri nth nth ; +: a[j][i] ( state -- elt ) [ i>> ] [ j>> ] [ a>> ] tri nth nth ; +: a[j-1][i] ( state -- elt ) [ i>> ] [ j>> 1 - ] [ a>> ] tri nth nth ; +: a[j-1][i-1] ( state -- elt ) [ i>> 1 - ] [ j>> 1 - ] [ a>> ] tri nth nth ; +: a[i-1][i-1] ( state -- elt ) [ i>> 1 - ] [ i>> 1 - ] [ a>> ] tri nth nth ; +: check-h ( state -- state ) + dup h>> 0 = [ "h must be nonzero in dfridr" throw ] when ; +: init-a ( state -- state ) ntab [ ntab ] replicate >>a ; +: init-hh ( state -- state ) dup h>> >>hh ; +: init-err ( state -- state ) big >>err ; +: update-hh ( state -- state ) dup hh>> con / >>hh ; +: reset-fac ( state -- state ) con2 >>fac ; +: update-fac ( state -- state ) dup fac>> con2 * >>fac ; + +! If error is decreased, save the improved answer +: error-decreased? ( state -- state ? ) [ ] [ errt>> ] [ err>> ] tri <= ; +: save-improved-answer ( state -- state ) + dup err>> >>errt + dup a[j][i] >>ans ; + +! If higher order is worse by a significant factor SAFE, then quit early. +: check-safe ( state -- state ) + dup + [ [ a[i][i] ] [ a[i-1][i-1] ] bi - abs ] [ err>> safe * ] bi >= + [ t >>done ] + when ; +: x+hh ( state -- val ) [ x>> ] [ hh>> ] bi + ; +: x-hh ( state -- val ) [ x>> ] [ hh>> ] bi - ; +: limit-approx ( state -- val ) + [ + [ [ x+hh ] [ func>> ] bi call ] + [ [ x-hh ] [ func>> ] bi call ] + bi - + ] + [ hh>> 2.0 * ] + bi / ; +: a[0][0]! ( state -- state ) + { [ ] [ limit-approx ] [ drop 0 ] [ drop 0 ] [ a>> ] } cleave nth set-nth ; +: a[0][i]! ( state -- state ) + { [ ] [ limit-approx ] [ i>> ] [ drop 0 ] [ a>> ] } cleave nth set-nth ; +: a[j-1][i]*fac ( state -- val ) [ a[j-1][i] ] [ fac>> ] bi * ; +: new-a[j][i] ( state -- val ) + [ [ a[j-1][i]*fac ] [ a[j-1][i-1] ] bi - ] + [ fac>> 1.0 - ] + bi / ; +: a[j][i]! ( state -- state ) + { [ ] [ new-a[j][i] ] [ i>> ] [ j>> ] [ a>> ] } cleave nth set-nth ; + +: update-errt ( state -- state ) + dup + [ [ a[j][i] ] [ a[j-1][i] ] bi - abs ] + [ [ a[j][i] ] [ a[j-1][i-1] ] bi - abs ] + bi max + >>errt ; + +: not-done? ( state -- state ? ) dup done>> not ; + +: derive ( state -- state ) + init-a + check-h + init-hh + a[0][0]! + init-err + 1 ntab [a,b) + [ + >>i + not-done? + [ + update-hh + a[0][i]! + reset-fac + 1 over i>> [a,b] + [ + >>j + a[j][i]! + update-fac + update-errt + error-decreased? [ save-improved-answer ] when + ] + each + check-safe + ] + when + ] + each ; + +: derivative-state ( x func h err -- state ) + state new + swap >>err + swap >>h + swap >>func + swap >>x ; + +! For scientists: +! h should be .001 to .5 -- too small can cause bad convergence, +! h should be small enough to give the correct sgn(f'(x)) +! err is the max tolerance of gain in error for a single iteration- +: (derivative) ( x func h err -- ans error ) + derivative-state + derive + [ ans>> ] + [ errt>> ] + bi ; + +: derivative ( x func -- m ) 0.01 2.0 (derivative) drop ; +: derivative-func ( func -- der ) [ derivative ] curry ; \ No newline at end of file From 6f767add2c4fada948553e2639888a6539d7ae1c Mon Sep 17 00:00:00 2001 From: Rex Ford Date: Tue, 12 Aug 2008 12:00:54 -0400 Subject: [PATCH 11/20] documentation for scientists --- extra/math/derivatives/derivatives-docs.factor | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/extra/math/derivatives/derivatives-docs.factor b/extra/math/derivatives/derivatives-docs.factor index a78a697b76..15dd954b1c 100644 --- a/extra/math/derivatives/derivatives-docs.factor +++ b/extra/math/derivatives/derivatives-docs.factor @@ -96,5 +96,6 @@ HELP: derivative-func ( function -- der ) ARTICLE: "derivatives" "The Derivative Toolkit" "A toolkit for computing the derivative of functions." { $subsection derivative } -{ $subsection derivative-func } ; +{ $subsection derivative-func } +{ $subsection (derivative) } ; ABOUT: "derivatives" From 8b3ce1ee841c78832671726066588d075226e008 Mon Sep 17 00:00:00 2001 From: William Schlieper Date: Wed, 13 Aug 2008 01:00:26 -0400 Subject: [PATCH 12/20] irc:Added whois command, fixed bug in parting --- extra/irc/messages/messages.factor | 2 +- extra/irc/ui/commands/commands.factor | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) mode change 100644 => 100755 extra/irc/messages/messages.factor diff --git a/extra/irc/messages/messages.factor b/extra/irc/messages/messages.factor old mode 100644 new mode 100755 index 3b9cf0af2c..fb56dd3a45 --- a/extra/irc/messages/messages.factor +++ b/extra/irc/messages/messages.factor @@ -46,7 +46,7 @@ GENERIC: irc-command-parameters ( irc-message -- seq ) M: irc-message irc-command-parameters ( irc-message -- seq ) parameters>> ; M: ping irc-command-parameters ( ping -- seq ) drop { } ; M: join irc-command-parameters ( join -- seq ) drop { } ; -M: part irc-command-parameters ( part -- seq ) name>> 1array ; +M: part irc-command-parameters ( part -- seq ) channel>> 1array ; M: quit irc-command-parameters ( quit -- seq ) drop { } ; M: nick irc-command-parameters ( nick -- seq ) drop { } ; M: privmsg irc-command-parameters ( privmsg -- seq ) name>> 1array ; diff --git a/extra/irc/ui/commands/commands.factor b/extra/irc/ui/commands/commands.factor index ddae783f06..184a2b4de8 100755 --- a/extra/irc/ui/commands/commands.factor +++ b/extra/irc/ui/commands/commands.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2008 William Schlieper ! See http://factorcode.org/license.txt for BSD license. -USING: accessors kernel irc.client irc.messages irc.ui namespaces ; +USING: accessors kernel arrays irc.client irc.messages irc.ui namespaces ; IN: irc.ui.commands @@ -16,5 +16,9 @@ IN: irc.ui.commands : query ( string -- ) irc-tab get window>> query-nick ; +: whois ( string -- ) + "WHOIS" swap { } clone swap + irc-tab get listener>> write-message ; + : quote ( string -- ) drop ; ! THIS WILL CHANGE From 6f78e38ab4c3dd474c3b16f747fd88e1056043a8 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 13 Aug 2008 14:19:48 -0500 Subject: [PATCH 13/20] Add a couple of words to disjoint-sets --- basis/disjoint-sets/disjoint-sets.factor | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/basis/disjoint-sets/disjoint-sets.factor b/basis/disjoint-sets/disjoint-sets.factor index f48129fbd4..77e4a53f7b 100644 --- a/basis/disjoint-sets/disjoint-sets.factor +++ b/basis/disjoint-sets/disjoint-sets.factor @@ -88,6 +88,14 @@ M:: disjoint-set equate ( a b disjoint-set -- ) disjoint-set link-sets ] if ; +: equate-all-with ( seq a disjoint-set -- ) + '[ , , equate ] each ; + +: equate-all ( seq disjoint-set -- ) + over dup empty? [ 2drop ] [ + [ unclip-slice ] dip equate-all-with + ] if ; + M: disjoint-set clone [ parents>> ] [ ranks>> ] [ counts>> ] tri [ clone ] tri@ disjoint-set boa ; From 35a1ca3201a6af3b9354ea6a4e852e38c337ef69 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 13 Aug 2008 14:19:58 -0500 Subject: [PATCH 14/20] Improve ranges docs --- basis/math/ranges/ranges-docs.factor | 40 ++++++++++++++++------------ 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/basis/math/ranges/ranges-docs.factor b/basis/math/ranges/ranges-docs.factor index 714fc67c9f..f3c65e51a4 100644 --- a/basis/math/ranges/ranges-docs.factor +++ b/basis/math/ranges/ranges-docs.factor @@ -1,21 +1,27 @@ -USING: help.syntax help.markup ; +USING: help.syntax help.markup arrays sequences ; IN: math.ranges ARTICLE: "ranges" "Ranges" - - "A " { $emphasis "range" } " is a virtual sequence with real elements " - "ranging from " { $emphasis "a" } " to " { $emphasis "b" } " by " { $emphasis "step" } "." - - $nl - - "Creating ranges:" - - { $subsection } - { $subsection [a,b] } - { $subsection (a,b] } - { $subsection [a,b) } - { $subsection (a,b) } - { $subsection [0,b] } - { $subsection [1,b] } - { $subsection [0,b) } ; \ No newline at end of file +"A " { $emphasis "range" } " is a virtual sequence with real number elements " +"ranging from " { $emphasis "a" } " to " { $emphasis "b" } " by " { $emphasis "step" } "." +$nl +"The class of ranges:" +{ $subsection range } +"Creating ranges with integer end-points. The standard mathematical convention is used, where " { $snippet "(" } " or " { $snippet ")" } " denotes that the end-point itself " { $emphasis "is not" } " part of the range; " { $snippet "[" } " or " { $snippet "]" } " denotes that the end-point " { $emphasis "is" } " part of the range:" +{ $subsection [a,b] } +{ $subsection (a,b] } +{ $subsection [a,b) } +{ $subsection (a,b) } +{ $subsection [0,b] } +{ $subsection [1,b] } +{ $subsection [0,b) } +"Creating general ranges:" +{ $subsection } +"Ranges are most frequently used with sequence combinators as a means of iterating over integers. For example," +{ $code + "3 10 [a,b] [ sqrt ] map" +} +"A range can be converted into a concrete sequence using a word such as " { $link >array } ". In most cases this is unnecessary since ranges implement the sequence protocol already. It is necessary if a mutable sequence is needed, for use with words such as " { $link set-nth } " or " { $link change-each } "." ; + +ABOUT: "ranges" \ No newline at end of file From f683c63da1c5a66a9e4a1ebc08bad7d7be79c9fe Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 13 Aug 2008 14:20:09 -0500 Subject: [PATCH 15/20] Fix code duplication --- extra/math/combinatorics/combinatorics.factor | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/extra/math/combinatorics/combinatorics.factor b/extra/math/combinatorics/combinatorics.factor index f7d7b76fa4..6193edfb91 100644 --- a/extra/math/combinatorics/combinatorics.factor +++ b/extra/math/combinatorics/combinatorics.factor @@ -1,7 +1,7 @@ ! Copyright (c) 2007, 2008 Slava Pestov, Doug Coleman, Aaron Schaefer. ! See http://factorcode.org/license.txt for BSD license. USING: assocs kernel math math.order math.ranges mirrors -namespaces sequences sorting ; +namespaces sequences sequences.lib sorting ; IN: math.combinatorics permutation ; -: reorder ( seq indices -- seq ) - [ [ over nth , ] each drop ] { } make ; - PRIVATE> : factorial ( n -- n! ) @@ -42,7 +39,7 @@ PRIVATE> twiddle [ nPk ] keep factorial / ; : permutation ( n seq -- seq ) - tuck permutation-indices reorder ; + tuck permutation-indices nths ; : all-permutations ( seq -- seq ) [ From bbb3597ac2a45207d33455f4cc165607d859f25e Mon Sep 17 00:00:00 2001 From: sheeple Date: Wed, 13 Aug 2008 15:13:55 -0500 Subject: [PATCH 16/20] add a couple constants for unix file access --- basis/unix/bsd/bsd.factor | 17 +++++++++-------- basis/unix/linux/linux.factor | 17 +++++++++-------- basis/unix/unix.factor | 2 ++ 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/basis/unix/bsd/bsd.factor b/basis/unix/bsd/bsd.factor index 0c669d2258..68444de85f 100755 --- a/basis/unix/bsd/bsd.factor +++ b/basis/unix/bsd/bsd.factor @@ -7,13 +7,15 @@ IN: unix : MAXPATHLEN 1024 ; inline -: O_RDONLY HEX: 0000 ; inline -: O_WRONLY HEX: 0001 ; inline -: O_RDWR HEX: 0002 ; inline -: O_APPEND HEX: 0008 ; inline -: O_CREAT HEX: 0200 ; inline -: O_TRUNC HEX: 0400 ; inline -: O_EXCL HEX: 0800 ; inline +: O_RDONLY HEX: 0000 ; inline +: O_WRONLY HEX: 0001 ; inline +: O_RDWR HEX: 0002 ; inline +: O_NONBLOCK HEX: 0004 ; inline +: O_APPEND HEX: 0008 ; inline +: O_CREAT HEX: 0200 ; inline +: O_TRUNC HEX: 0400 ; inline +: O_EXCL HEX: 0800 ; inline +: O_NOCTTY HEX: 20000 ; inline : SOL_SOCKET HEX: ffff ; inline : SO_REUSEADDR HEX: 4 ; inline @@ -24,7 +26,6 @@ IN: unix : F_SETFD 2 ; inline : F_SETFL 4 ; inline : FD_CLOEXEC 1 ; inline -: O_NONBLOCK 4 ; inline C-STRUCT: sockaddr-in { "uchar" "len" } diff --git a/basis/unix/linux/linux.factor b/basis/unix/linux/linux.factor index 0efacee294..cc1e056b8b 100755 --- a/basis/unix/linux/linux.factor +++ b/basis/unix/linux/linux.factor @@ -7,13 +7,15 @@ USING: alien.syntax ; : MAXPATHLEN 1024 ; inline -: O_RDONLY HEX: 0000 ; inline -: O_WRONLY HEX: 0001 ; inline -: O_RDWR HEX: 0002 ; inline -: O_CREAT HEX: 0040 ; inline -: O_EXCL HEX: 0080 ; inline -: O_TRUNC HEX: 0200 ; inline -: O_APPEND HEX: 0400 ; inline +: O_RDONLY HEX: 0000 ; inline +: O_WRONLY HEX: 0001 ; inline +: O_RDWR HEX: 0002 ; inline +: O_CREAT HEX: 0040 ; inline +: O_EXCL HEX: 0080 ; inline +: O_NOCTTY HEX: 0100 ; inline +: O_TRUNC HEX: 0200 ; inline +: O_APPEND HEX: 0400 ; inline +: O_NONBLOCK HEX: 0800 ; inline : SOL_SOCKET 1 ; inline @@ -28,7 +30,6 @@ USING: alien.syntax ; : FD_CLOEXEC 1 ; inline : F_SETFL 4 ; inline -: O_NONBLOCK HEX: 800 ; inline C-STRUCT: addrinfo { "int" "flags" } diff --git a/basis/unix/unix.factor b/basis/unix/unix.factor index 083700493d..065087fa59 100755 --- a/basis/unix/unix.factor +++ b/basis/unix/unix.factor @@ -25,6 +25,8 @@ TYPEDEF: uint socklen_t : NGROUPS_MAX 16 ; inline +: O_NDELAY O_NONBLOCK ; inline + C-STRUCT: group { "char*" "gr_name" } { "char*" "gr_passwd" } From 21fb13f4504aa5e8070c19d9a18c4b904f0135b9 Mon Sep 17 00:00:00 2001 From: sheeple Date: Wed, 13 Aug 2008 15:18:50 -0500 Subject: [PATCH 17/20] initial chicken of serial --- extra/serial/authors.txt | 1 + extra/serial/serial.factor | 23 ++++ extra/serial/summary.txt | 1 + extra/serial/tags.txt | 1 + extra/serial/unix/bsd/bsd.factor | 11 ++ extra/serial/unix/bsd/tags.txt | 1 + extra/serial/unix/linux/linux.factor | 130 +++++++++++++++++++ extra/serial/unix/linux/tags.txt | 1 + extra/serial/unix/tags.txt | 1 + extra/serial/unix/termios/bsd/bsd.factor | 19 +++ extra/serial/unix/termios/bsd/tags.txt | 1 + extra/serial/unix/termios/linux/linux.factor | 20 +++ extra/serial/unix/termios/linux/tags.txt | 1 + extra/serial/unix/termios/tags.txt | 1 + extra/serial/unix/termios/termios.factor | 9 ++ extra/serial/unix/unix-tests.factor | 21 +++ extra/serial/unix/unix.factor | 63 +++++++++ 17 files changed, 305 insertions(+) create mode 100644 extra/serial/authors.txt create mode 100644 extra/serial/serial.factor create mode 100644 extra/serial/summary.txt create mode 100644 extra/serial/tags.txt create mode 100644 extra/serial/unix/bsd/bsd.factor create mode 100644 extra/serial/unix/bsd/tags.txt create mode 100644 extra/serial/unix/linux/linux.factor create mode 100644 extra/serial/unix/linux/tags.txt create mode 100644 extra/serial/unix/tags.txt create mode 100644 extra/serial/unix/termios/bsd/bsd.factor create mode 100644 extra/serial/unix/termios/bsd/tags.txt create mode 100644 extra/serial/unix/termios/linux/linux.factor create mode 100644 extra/serial/unix/termios/linux/tags.txt create mode 100644 extra/serial/unix/termios/tags.txt create mode 100644 extra/serial/unix/termios/termios.factor create mode 100644 extra/serial/unix/unix-tests.factor create mode 100644 extra/serial/unix/unix.factor diff --git a/extra/serial/authors.txt b/extra/serial/authors.txt new file mode 100644 index 0000000000..7c1b2f2279 --- /dev/null +++ b/extra/serial/authors.txt @@ -0,0 +1 @@ +Doug Coleman diff --git a/extra/serial/serial.factor b/extra/serial/serial.factor new file mode 100644 index 0000000000..39a63927da --- /dev/null +++ b/extra/serial/serial.factor @@ -0,0 +1,23 @@ +! Copyright (C) 2008 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: accessors alien.c-types assocs combinators destructors +kernel math math.bitfields math.parser sequences summary system +vocabs.loader ; +IN: serial + +TUPLE: serial stream path baud + termios iflag oflag cflag lflag ; + +ERROR: invalid-baud baud ; +M: invalid-baud summary ( invalid-baud -- string ) + "Baud rate " + swap baud>> number>string + " not supported" 3append ; + +HOOK: lookup-baud os ( m -- n ) +HOOK: open-serial os ( serial -- serial' ) +M: serial dispose ( serial -- ) stream>> dispose ; + +{ + { [ os unix? ] [ "serial.unix" ] } +} cond require diff --git a/extra/serial/summary.txt b/extra/serial/summary.txt new file mode 100644 index 0000000000..5ccd99dbaa --- /dev/null +++ b/extra/serial/summary.txt @@ -0,0 +1 @@ +Serial port library diff --git a/extra/serial/tags.txt b/extra/serial/tags.txt new file mode 100644 index 0000000000..6bf68304bb --- /dev/null +++ b/extra/serial/tags.txt @@ -0,0 +1 @@ +unportable diff --git a/extra/serial/unix/bsd/bsd.factor b/extra/serial/unix/bsd/bsd.factor new file mode 100644 index 0000000000..68aaa03a23 --- /dev/null +++ b/extra/serial/unix/bsd/bsd.factor @@ -0,0 +1,11 @@ +! Copyright (C) 2008 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: kernel sequences system serial ; +IN: serial.unix + +M: bsd lookup-baud ( m -- n ) + dup { + 0 50 75 110 134 150 200 300 600 1200 1800 2400 4800 + 7200 9600 14400 19200 28800 38400 57600 76800 115200 + 230400 460800 921600 + } member? [ invalid-baud ] unless ; diff --git a/extra/serial/unix/bsd/tags.txt b/extra/serial/unix/bsd/tags.txt new file mode 100644 index 0000000000..6bf68304bb --- /dev/null +++ b/extra/serial/unix/bsd/tags.txt @@ -0,0 +1 @@ +unportable diff --git a/extra/serial/unix/linux/linux.factor b/extra/serial/unix/linux/linux.factor new file mode 100644 index 0000000000..3ad5088fc8 --- /dev/null +++ b/extra/serial/unix/linux/linux.factor @@ -0,0 +1,130 @@ +! Copyright (C) 2008 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: assocs alien.syntax kernel serial system unix ; +IN: serial.unix + +: TCSANOW 0 ; inline +: TCSADRAIN 1 ; inline +: TCSAFLUSH 2 ; inline + +: TCIFLUSH 0 ; inline +: TCOFLUSH 1 ; inline +: TCIOFLUSH 2 ; inline + +: TCOOFF 0 ; inline +: TCOON 1 ; inline +: TCIOFF 2 ; inline +: TCION 3 ; inline + +! iflag +: IGNBRK OCT: 0000001 ; inline +: BRKINT OCT: 0000002 ; inline +: IGNPAR OCT: 0000004 ; inline +: PARMRK OCT: 0000010 ; inline +: INPCK OCT: 0000020 ; inline +: ISTRIP OCT: 0000040 ; inline +: INLCR OCT: 0000100 ; inline +: IGNCR OCT: 0000200 ; inline +: ICRNL OCT: 0000400 ; inline +: IUCLC OCT: 0001000 ; inline +: IXON OCT: 0002000 ; inline +: IXANY OCT: 0004000 ; inline +: IXOFF OCT: 0010000 ; inline +: IMAXBEL OCT: 0020000 ; inline +: IUTF8 OCT: 0040000 ; inline + +! oflag +: OPOST OCT: 0000001 ; inline +: OLCUC OCT: 0000002 ; inline +: ONLCR OCT: 0000004 ; inline +: OCRNL OCT: 0000010 ; inline +: ONOCR OCT: 0000020 ; inline +: ONLRET OCT: 0000040 ; inline +: OFILL OCT: 0000100 ; inline +: OFDEL OCT: 0000200 ; inline +: NLDLY OCT: 0000400 ; inline +: NL0 OCT: 0000000 ; inline +: NL1 OCT: 0000400 ; inline +: CRDLY OCT: 0003000 ; inline +: CR0 OCT: 0000000 ; inline +: CR1 OCT: 0001000 ; inline +: CR2 OCT: 0002000 ; inline +: CR3 OCT: 0003000 ; inline +: TABDLY OCT: 0014000 ; inline +: TAB0 OCT: 0000000 ; inline +: TAB1 OCT: 0004000 ; inline +: TAB2 OCT: 0010000 ; inline +: TAB3 OCT: 0014000 ; inline +: BSDLY OCT: 0020000 ; inline +: BS0 OCT: 0000000 ; inline +: BS1 OCT: 0020000 ; inline +: FFDLY OCT: 0100000 ; inline +: FF0 OCT: 0000000 ; inline +: FF1 OCT: 0100000 ; inline + +! cflags +: CSIZE OCT: 0000060 ; inline +: CS5 OCT: 0000000 ; inline +: CS6 OCT: 0000020 ; inline +: CS7 OCT: 0000040 ; inline +: CS8 OCT: 0000060 ; inline +: CSTOPB OCT: 0000100 ; inline +: CREAD OCT: 0000200 ; inline +: PARENB OCT: 0000400 ; inline +: PARODD OCT: 0001000 ; inline +: HUPCL OCT: 0002000 ; inline +: CLOCAL OCT: 0004000 ; inline +: CIBAUD OCT: 002003600000 ; inline +: CRTSCTS OCT: 020000000000 ; inline + +! lflags +: ISIG OCT: 0000001 ; inline +: ICANON OCT: 0000002 ; inline +: XCASE OCT: 0000004 ; inline +: ECHO OCT: 0000010 ; inline +: ECHOE OCT: 0000020 ; inline +: ECHOK OCT: 0000040 ; inline +: ECHONL OCT: 0000100 ; inline +: NOFLSH OCT: 0000200 ; inline +: TOSTOP OCT: 0000400 ; inline +: ECHOCTL OCT: 0001000 ; inline +: ECHOPRT OCT: 0002000 ; inline +: ECHOKE OCT: 0004000 ; inline +: FLUSHO OCT: 0010000 ; inline +: PENDIN OCT: 0040000 ; inline +: IEXTEN OCT: 0100000 ; inline + +M: linux lookup-baud ( n -- n ) + dup H{ + { 0 OCT: 0000000 } + { 50 OCT: 0000001 } + { 75 OCT: 0000002 } + { 110 OCT: 0000003 } + { 134 OCT: 0000004 } + { 150 OCT: 0000005 } + { 200 OCT: 0000006 } + { 300 OCT: 0000007 } + { 600 OCT: 0000010 } + { 1200 OCT: 0000011 } + { 1800 OCT: 0000012 } + { 2400 OCT: 0000013 } + { 4800 OCT: 0000014 } + { 9600 OCT: 0000015 } + { 19200 OCT: 0000016 } + { 38400 OCT: 0000017 } + { 57600 OCT: 0010001 } + { 115200 OCT: 0010002 } + { 230400 OCT: 0010003 } + { 460800 OCT: 0010004 } + { 500000 OCT: 0010005 } + { 576000 OCT: 0010006 } + { 921600 OCT: 0010007 } + { 1000000 OCT: 0010010 } + { 1152000 OCT: 0010011 } + { 1500000 OCT: 0010012 } + { 2000000 OCT: 0010013 } + { 2500000 OCT: 0010014 } + { 3000000 OCT: 0010015 } + { 3500000 OCT: 0010016 } + { 4000000 OCT: 0010017 } + } at* [ nip ] [ drop invalid-baud ] if ; diff --git a/extra/serial/unix/linux/tags.txt b/extra/serial/unix/linux/tags.txt new file mode 100644 index 0000000000..6bf68304bb --- /dev/null +++ b/extra/serial/unix/linux/tags.txt @@ -0,0 +1 @@ +unportable diff --git a/extra/serial/unix/tags.txt b/extra/serial/unix/tags.txt new file mode 100644 index 0000000000..6bf68304bb --- /dev/null +++ b/extra/serial/unix/tags.txt @@ -0,0 +1 @@ +unportable diff --git a/extra/serial/unix/termios/bsd/bsd.factor b/extra/serial/unix/termios/bsd/bsd.factor new file mode 100644 index 0000000000..c8f1e8be54 --- /dev/null +++ b/extra/serial/unix/termios/bsd/bsd.factor @@ -0,0 +1,19 @@ +! Copyright (C) 2008 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: kernel sequences system ; +IN: serial.unix.termios + +: NCCS 20 ; inline + +TYPEDEF: uint tcflag_t +TYPEDEF: uchar cc_t +TYPEDEF: uint speed_t + +C-STRUCT: termios + { "tcflag_t" "iflag" } ! input mode flags + { "tcflag_t" "oflag" } ! output mode flags + { "tcflag_t" "cflag" } ! control mode flags + { "tcflag_t" "lflag" } ! local mode flags + { { "cc_t" NCCS } "cc" } ! control characters + { "speed_t" "ispeed" } ! input speed + { "speed_t" "ospeed" } ; ! output speed diff --git a/extra/serial/unix/termios/bsd/tags.txt b/extra/serial/unix/termios/bsd/tags.txt new file mode 100644 index 0000000000..6bf68304bb --- /dev/null +++ b/extra/serial/unix/termios/bsd/tags.txt @@ -0,0 +1 @@ +unportable diff --git a/extra/serial/unix/termios/linux/linux.factor b/extra/serial/unix/termios/linux/linux.factor new file mode 100644 index 0000000000..de9906e2b9 --- /dev/null +++ b/extra/serial/unix/termios/linux/linux.factor @@ -0,0 +1,20 @@ +! Copyright (C) 2008 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: alien.syntax kernel system unix ; +IN: serial.unix.termios + +: NCCS 32 ; inline + +TYPEDEF: uchar cc_t +TYPEDEF: uint speed_t +TYPEDEF: uint tcflag_t + +C-STRUCT: termios + { "tcflag_t" "iflag" } ! input mode flags + { "tcflag_t" "oflag" } ! output mode flags + { "tcflag_t" "cflag" } ! control mode flags + { "tcflag_t" "lflag" } ! local mode flags + { "cc_t" "line" } ! line discipline + { { "cc_t" NCCS } "cc" } ! control characters + { "speed_t" "ispeed" } ! input speed + { "speed_t" "ospeed" } ; ! output speed diff --git a/extra/serial/unix/termios/linux/tags.txt b/extra/serial/unix/termios/linux/tags.txt new file mode 100644 index 0000000000..6bf68304bb --- /dev/null +++ b/extra/serial/unix/termios/linux/tags.txt @@ -0,0 +1 @@ +unportable diff --git a/extra/serial/unix/termios/tags.txt b/extra/serial/unix/termios/tags.txt new file mode 100644 index 0000000000..6bf68304bb --- /dev/null +++ b/extra/serial/unix/termios/tags.txt @@ -0,0 +1 @@ +unportable diff --git a/extra/serial/unix/termios/termios.factor b/extra/serial/unix/termios/termios.factor new file mode 100644 index 0000000000..901416d62c --- /dev/null +++ b/extra/serial/unix/termios/termios.factor @@ -0,0 +1,9 @@ +! Copyright (C) 2008 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: combinators system vocabs.loader ; +IN: serial.unix.termios + +{ + { [ os linux? ] [ "serial.unix.termios.linux" ] } + { [ os bsd? ] [ "serial.unix.termios.bsd" ] } +} cond require diff --git a/extra/serial/unix/unix-tests.factor b/extra/serial/unix/unix-tests.factor new file mode 100644 index 0000000000..300cacf83e --- /dev/null +++ b/extra/serial/unix/unix-tests.factor @@ -0,0 +1,21 @@ +! Copyright (C) 2008 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: accessors kernel math.bitfields serial.unix ; +IN: serial.unix + +: serial-obj ( -- obj ) + serial new + "/dev/ttyS0" >>path + 19200 >>baud + { IGNPAR ICRNL } flags >>iflag + { } flags >>oflag + { CS8 CLOCAL CREAD } flags >>cflag + { ICANON } flags >>lflag ; + +: serial-test ( -- serial ) + serial-obj + open-serial + dup get-termios >>termios + dup configure-termios + dup tciflush + dup apply-termios ; diff --git a/extra/serial/unix/unix.factor b/extra/serial/unix/unix.factor new file mode 100644 index 0000000000..6b48c758cc --- /dev/null +++ b/extra/serial/unix/unix.factor @@ -0,0 +1,63 @@ +! Copyright (C) 2008 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: accessors alien.c-types alien.syntax combinators io.ports +io.streams.duplex io.unix.backend system kernel math math.bitfields +vocabs.loader unix serial serial.unix.termios ; +IN: serial.unix + +{ + { [ os linux? ] [ "serial.unix.linux" ] } + { [ os bsd? ] [ "serial.unix.bsd" ] } +} cond require + +FUNCTION: speed_t cfgetispeed ( termios* t ) ; +FUNCTION: speed_t cfgetospeed ( termios* t ) ; +FUNCTION: int cfsetispeed ( termios* t, speed_t s ) ; +FUNCTION: int cfsetospeed ( termios* t, speed_t s ) ; +FUNCTION: int tcgetattr ( int i1, termios* t ) ; +FUNCTION: int tcsetattr ( int i1, int i2, termios* t ) ; +FUNCTION: int tcdrain ( int i1 ) ; +FUNCTION: int tcflow ( int i1, int i2 ) ; +FUNCTION: int tcflush ( int i1, int i2 ) ; +FUNCTION: int tcsendbreak ( int i1, int i2 ) ; +FUNCTION: void cfmakeraw ( termios* t ) ; +FUNCTION: int cfsetspeed ( termios* t, speed_t s ) ; + +: fd>duplex-stream ( fd -- duplex-stream ) + init-fd + [ ] [ ] bi ; + +: open-rw ( path -- fd ) O_RDWR file-mode open-file ; +: ( path -- stream ) open-rw fd>duplex-stream ; + +M: unix open-serial ( serial -- serial' ) + dup + path>> { O_RDWR O_NOCTTY O_NDELAY } flags file-mode open-file + fd>duplex-stream >>stream ; + +: serial-fd ( serial -- fd ) + stream>> in>> handle>> fd>> ; + +: get-termios ( serial -- termios ) + serial-fd + "termios" [ tcgetattr io-error ] keep ; + +: configure-termios ( serial -- ) + dup termios>> + { + [ [ iflag>> ] dip over [ set-termios-iflag ] [ 2drop ] if ] + [ [ oflag>> ] dip over [ set-termios-oflag ] [ 2drop ] if ] + [ + [ + [ cflag>> 0 or ] [ baud>> lookup-baud ] bi bitor + ] dip set-termios-cflag + ] + [ [ lflag>> ] dip over [ set-termios-lflag ] [ 2drop ] if ] + } 2cleave ; + +: tciflush ( serial -- ) + serial-fd TCIFLUSH tcflush io-error ; + +: apply-termios ( serial -- ) + [ serial-fd TCSANOW ] + [ termios>> ] bi tcsetattr io-error ; From bb76f2f61707364c41a4a47e5704c8047d873d98 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Wed, 13 Aug 2008 18:57:57 -0500 Subject: [PATCH 18/20] Fix combinatorics tests --- extra/math/combinatorics/combinatorics-tests.factor | 5 ----- 1 file changed, 5 deletions(-) diff --git a/extra/math/combinatorics/combinatorics-tests.factor b/extra/math/combinatorics/combinatorics-tests.factor index e6a2824433..5ef435a4e0 100644 --- a/extra/math/combinatorics/combinatorics-tests.factor +++ b/extra/math/combinatorics/combinatorics-tests.factor @@ -13,11 +13,6 @@ IN: math.combinatorics.tests [ { 0 1 3 2 } ] [ 1 4 permutation-indices ] unit-test [ { 1 2 0 6 3 5 4 } ] [ 859 7 permutation-indices ] unit-test -[ { "b" "d" } ] [ { "a" "b" "c" "d" } { 1 3 } reorder ] unit-test -[ { "a" "b" "c" "d" } ] [ { "a" "b" "c" "d" } { 0 1 2 3 } reorder ] unit-test -[ { "d" "c" "b" "a" } ] [ { "a" "b" "c" "d" } { 3 2 1 0 } reorder ] unit-test -[ { "d" "a" "b" "c" } ] [ { "a" "b" "c" "d" } { 3 0 1 2 } reorder ] unit-test - [ 1 ] [ 0 factorial ] unit-test [ 1 ] [ 1 factorial ] unit-test [ 3628800 ] [ 10 factorial ] unit-test From c5e3bdf66842e54e3a791eb2f28d3d0eb6832956 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Wed, 13 Aug 2008 21:51:25 -0500 Subject: [PATCH 19/20] add constants for bsd serial --- extra/serial/unix/bsd/bsd.factor | 15 ++++++++++++++- extra/serial/unix/termios/bsd/bsd.factor | 2 +- extra/serial/unix/unix.factor | 4 ++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/extra/serial/unix/bsd/bsd.factor b/extra/serial/unix/bsd/bsd.factor index 68aaa03a23..7dac47193a 100644 --- a/extra/serial/unix/bsd/bsd.factor +++ b/extra/serial/unix/bsd/bsd.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2008 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: kernel sequences system serial ; +USING: alien.syntax kernel sequences system serial ; IN: serial.unix M: bsd lookup-baud ( m -- n ) @@ -9,3 +9,16 @@ M: bsd lookup-baud ( m -- n ) 7200 9600 14400 19200 28800 38400 57600 76800 115200 230400 460800 921600 } member? [ invalid-baud ] unless ; + +: TCSANOW 0 ; inline +: TCSADRAIN 1 ; inline +: TCSAFLUSH 2 ; inline +: TCSASOFT HEX: 10 ; inline + +: TCIFLUSH 1 ; inline +: TCOFLUSH 2 ; inline +: TCIOFLUSH 3 ; inline +: TCOOFF 1 ; inline +: TCOON 2 ; inline +: TCIOFF 3 ; inline +: TCION 4 ; inline diff --git a/extra/serial/unix/termios/bsd/bsd.factor b/extra/serial/unix/termios/bsd/bsd.factor index c8f1e8be54..5fbc571519 100644 --- a/extra/serial/unix/termios/bsd/bsd.factor +++ b/extra/serial/unix/termios/bsd/bsd.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2008 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: kernel sequences system ; +USING: alien.syntax kernel sequences system ; IN: serial.unix.termios : NCCS 20 ; inline diff --git a/extra/serial/unix/unix.factor b/extra/serial/unix/unix.factor index 6b48c758cc..7ed5bced37 100644 --- a/extra/serial/unix/unix.factor +++ b/extra/serial/unix/unix.factor @@ -5,10 +5,10 @@ io.streams.duplex io.unix.backend system kernel math math.bitfields vocabs.loader unix serial serial.unix.termios ; IN: serial.unix -{ +<< { { [ os linux? ] [ "serial.unix.linux" ] } { [ os bsd? ] [ "serial.unix.bsd" ] } -} cond require +} cond require >> FUNCTION: speed_t cfgetispeed ( termios* t ) ; FUNCTION: speed_t cfgetospeed ( termios* t ) ; From f98729eb91d6fe6187edd8fadd5f53915c03e18a Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Wed, 13 Aug 2008 22:04:12 -0500 Subject: [PATCH 20/20] more bsd flags --- extra/serial/unix/bsd/bsd.factor | 64 ++++++++++++++++++++++++++++- extra/serial/unix/unix-tests.factor | 2 +- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/extra/serial/unix/bsd/bsd.factor b/extra/serial/unix/bsd/bsd.factor index 7dac47193a..feed85348b 100644 --- a/extra/serial/unix/bsd/bsd.factor +++ b/extra/serial/unix/bsd/bsd.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2008 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: alien.syntax kernel sequences system serial ; +USING: alien.syntax kernel math.bitfields sequences system serial ; IN: serial.unix M: bsd lookup-baud ( m -- n ) @@ -22,3 +22,65 @@ M: bsd lookup-baud ( m -- n ) : TCOON 2 ; inline : TCIOFF 3 ; inline : TCION 4 ; inline + +! iflags +: IGNBRK HEX: 00000001 ; inline +: BRKINT HEX: 00000002 ; inline +: IGNPAR HEX: 00000004 ; inline +: PARMRK HEX: 00000008 ; inline +: INPCK HEX: 00000010 ; inline +: ISTRIP HEX: 00000020 ; inline +: INLCR HEX: 00000040 ; inline +: IGNCR HEX: 00000080 ; inline +: ICRNL HEX: 00000100 ; inline +: IXON HEX: 00000200 ; inline +: IXOFF HEX: 00000400 ; inline +: IXANY HEX: 00000800 ; inline +: IMAXBEL HEX: 00002000 ; inline +: IUTF8 HEX: 00004000 ; inline + +! oflags +: OPOST HEX: 00000001 ; inline +: ONLCR HEX: 00000002 ; inline +: OXTABS HEX: 00000004 ; inline +: ONOEOT HEX: 00000008 ; inline + +! cflags +: CIGNORE HEX: 00000001 ; inline +: CSIZE HEX: 00000300 ; inline +: CS5 HEX: 00000000 ; inline +: CS6 HEX: 00000100 ; inline +: CS7 HEX: 00000200 ; inline +: CS8 HEX: 00000300 ; inline +: CSTOPB HEX: 00000400 ; inline +: CREAD HEX: 00000800 ; inline +: PARENB HEX: 00001000 ; inline +: PARODD HEX: 00002000 ; inline +: HUPCL HEX: 00004000 ; inline +: CLOCAL HEX: 00008000 ; inline +: CCTS_OFLOW HEX: 00010000 ; inline +: CRTS_IFLOW HEX: 00020000 ; inline +: CRTSCTS { CCTS_OFLOW CRTS_IFLOW } flags ; inline +: CDTR_IFLOW HEX: 00040000 ; inline +: CDSR_OFLOW HEX: 00080000 ; inline +: CCAR_OFLOW HEX: 00100000 ; inline +: MDMBUF HEX: 00100000 ; inline + +! lflags +: ECHOKE HEX: 00000001 ; inline +: ECHOE HEX: 00000002 ; inline +: ECHOK HEX: 00000004 ; inline +: ECHO HEX: 00000008 ; inline +: ECHONL HEX: 00000010 ; inline +: ECHOPRT HEX: 00000020 ; inline +: ECHOCTL HEX: 00000040 ; inline +: ISIG HEX: 00000080 ; inline +: ICANON HEX: 00000100 ; inline +: ALTWERASE HEX: 00000200 ; inline +: IEXTEN HEX: 00000400 ; inline +: EXTPROC HEX: 00000800 ; inline +: TOSTOP HEX: 00400000 ; inline +: FLUSHO HEX: 00800000 ; inline +: NOKERNINFO HEX: 02000000 ; inline +: PENDIN HEX: 20000000 ; inline +: NOFLSH HEX: 80000000 ; inline diff --git a/extra/serial/unix/unix-tests.factor b/extra/serial/unix/unix-tests.factor index 300cacf83e..bab6c3f4f1 100644 --- a/extra/serial/unix/unix-tests.factor +++ b/extra/serial/unix/unix-tests.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2008 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: accessors kernel math.bitfields serial.unix ; +USING: accessors kernel math.bitfields serial serial.unix ; IN: serial.unix : serial-obj ( -- obj )