Mean and Deviant

Young Woman in Purple Velvet Dress at Ornate Entrance
94
0
  • Squishy Plushie's avatar Artist
    Squishy Pl...
  • Prompt
    Read prompt
  • DDG Model
    DaVinci2
  • Access
    Public
  • Created
    1d ago
  • Try

More about Mean and Deviant

The following ::SHE+ILA:: function computes the Mean and the Standard Deviation of N consecutive values of a user-supplied function Foo :
Function Stats(N, &Foo) {
i <:: 0; sum <:: 0; variance <:: 0; {* i.le.N : sum += Foo(i); i++; *}
i <:: 0; mean <:: sum.div.N; {* i.le.N : variance += (Foo(i)-mean)*(Foo(i) -mean); i++; *}
ttyout("Mean value is ", mean, " and standard deviation is ", .sqrt. variance, CRLF); }
The following ::SHE+ILA:: function uses FBI operators to do this for a user-supplied array A:
Function ArrayStats(A) {
sum <:: +/A; mean <:: sum.div.#A; stddev <:: .sqrt.((A-mean)*(A-mean) .div. #A);
ttyout(" the mean array element is", mean, " and the std.dev is ", stddev, CRLF) };
Here the hash operator gives the size of the array supplied as its argument, and +/ is "plus reduce", adding all the elements of the array.
Note also that the functions could have been defined using lambdas, e.g.
"Function ArrayStats(A) {" could have been written as "ArrayStats <:: [](A) {" .

Comments


Loading Dream Comments...

Discover more dreams from this artist