Hello everybody,

Here is an interesting blog post from yosefk about Forth and stack machines:

http://yosefk.com/blog/my-history-with-forth-stack-machines.html

It contains a lot of interesting stuff but one part in particular got
my interest. He is trying to write a word to "compute the mean and the
standard deviation of a vector given the sum of its elements, the sum
of their squares, and the inverse of its length" and what he produces
to solve it is:

: mean_std ( sum2 sum inv_len -- mean std )
  \ precise_mean = sum * inv_len;
  tuck u* \ sum2 inv_len precise_mean
  \ mean = precise_mean >> FRAC;
  dup FRAC rshift -rot3 \ mean sum2 inv_len precise_mean
  \ var = (((unsigned long long)sum2 * inv_len) >> FRAC) -
(precise_mean * precise_mean >> (FRAC*2));
  dup um* nip FRAC 2 * 32 - rshift -rot \ mean precise_mean^2 sum2 inv_len
  um* 32 FRAC - lshift swap FRAC rshift or \ mean precise_mean^2 sum*inv_len
  swap - isqrt \ mean std ;

So his code is hideous and I'm wondering if we can do it better in
Factor? I'd love to take a stab at it myself but I have no idea what
mathematical formula he is implementing. Anyone knows? As far as I
understand, given three scalar values: the sum of all elements in a
vector, the sum of all elements squared and the inverse of the length,
you should be able to calculate the mean of the vector and the
standard deviation.


-- 
mvh/best regards Björn Lindqvist

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

Reply via email to