Re: [Factor-talk] Let's fix yosefk's tricky code

2014-08-14 Thread Björn Lindqvist
2014-08-14 0:17 GMT+02:00 Jon Purdy evincarofaut...@gmail.com:
 Sigh. The formatting button in Gmail is directly next to the “Send” button.
 Apologies, list.


 It seems like most of the overhead comes from fixed-point arithmetic,
 actually. Here’s a translation to Kitten with locals:

 def meanStd (float float float → float float):

   → sum2 sum invLen;

   sum ×. invLen → μ;
   μ (sum2 ×. invLen −. μ square) sqrt

 You can write this in postfix:

 def meanStd (float float float → float float):

   → sum2 sum invLen;

   sum invLen (×.) → μ;
   μ sum2 invLen (×.) μ square (−.) sqrt

 Then easily eliminate μ:

 def meanStd (float float float → float float):

   → sum2 sum invLen;

   sum invLen (×.) dup
   { sum2 invLen (×.) } dip
   square (−.) sqrt

Interesting! But what does the (x.) and (-.) words do?


-- 
mvh/best regards Björn Lindqvist

--
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] Let's fix yosefk's tricky code

2014-08-14 Thread Jon Purdy
 Interesting! But what does the (x.) and (-.) words do?

Multiply and subtract in floating point. Factor obviates such
distinctions with dynamic typing.

--
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] Let's fix yosefk's tricky code

2014-08-14 Thread Björn Lindqvist
2014-08-14 9:44 GMT+02:00 Jon Purdy evincarofaut...@gmail.com:
 Interesting! But what does the (x.) and (-.) words do?

 Multiply and subtract in floating point. Factor obviates such
 distinctions with dynamic typing.

Now I get it. With locals your code translates to this in Factor:

:: mean-std-2 ( sum^2 sum inv-len -- mean std )
sum inv-len * dup [ sum^2 inv-len * ] dip sq - sqrt ;

Or without locals:

: mean-std-8 ( inv-len sum^2 sum -- mean std )
pick * -rot * over sq - sqrt ;

Here I rearranged the parameter order to make the stack shuffling more
convenient. Another variant using the tuck word:

: mean-std-6 ( sum^2 sum inv-len -- mean std )
tuck * -rot * over sq - sqrt ;

I must say that the algorithm makes a compelling argument in favor of
locals! Or perhaps in favor of rearranging the parameters to fit the
problem better:

! Fry and tuck
: mean-std-10 ( sum^2 sum inv-len -- std mean )
'[ _ * ] bi@ tuck sq - sqrt ;

I think the last one is fairly readable.


-- 
mvh/best regards Björn Lindqvist

--
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk