Shorter version of root-mean-square using &.:
   rms=.(+/%#)&.:*:

   rms 1 2 1 2
1.58114




Den 16:23 søndag den 17. november 2013 skrev Raul Miller 
<[email protected]>:
 
Here's another definition of rms
>
>   Rms=: +/@:*: %:@% #
>   Rms 1 2 1 2
>1.58114
>
>Explanation:
>
>We do not need to square the numbers in the argument to #, we only
>need to square them in the argument to +/
>
>We only need the square root on the result of %
>
>Makes sense?
>
>Also, here's a partial explanation for the (+/%#)&.:*: definition of RMS
>
>&.: means "under" much like &. except that the derived verb has
>infinite rank - the verb on the left gets the entire array which
>resulted from the verb on the right, regardless of the rank of the
>verb on the right. In other words, it is equivalent to (+/ % #) &.
>(*:"_)  In other words: square the numbers, add them up, divide by
>their sum, then do the inverse of squaring on the result.
>
>Thanks,
>
>-- 
>Raul
>
>
>On Sun, Nov 17, 2013 at 12:43 AM, km <[email protected]> wrote:
>> Focus on what you want to DO.  You want to do "the square root of the mean 
>> of the square".
>>
>> A novice's tip for using  [:  is to read [: f g as "the f of the g" .  What 
>> you want is
>>
>> [:  %:  (  [:  (+/%#)  *:  )
>>
>> which you read as "the square root of ( the mean of the square )" .  
>> (Omitted one "the".)
>>
>> Because of J's right-to-left processing you can omit the outer parentheses 
>> and still read
>>
>> [:  %:  [:  (+/%#)  *:
>>
>> as "the square root of the mean of the square".
>>
>> Thus you can use
>>
>> rms  =:  [:  %:  [:  (+/%#)  *:
>>
>>
>> Another tip is to read  @:  as "after".  You want to do square root after 
>> doing mean after doing square.  Thus
>>
>> rms  =:  %:  @:  (+/%#)  @:  *:
>>
>> also works.  Pick what you are comfortable with and remember focus on what 
>> is to be DONE.   J's verbs are DOERS.
>>
>>
>> -- Kip Murray
>>
>> Sent from my iPad
>>
>>> On Nov 16, 2013, at 7:20 PM, Don Kelly <[email protected]> wrote:
>>>
>>> This is a novice problem  (which arose from reading  "Easy J" by Linda 
>>> Alvort
>>> I think that I have it worked out but this is something (IMHO) that 
>>> possibly should be in a primer.
>>> I have occasion to use what is called rms or root mean square (usually 
>>> applied to a wave form).
>>> I can define root, mean and square and can do something like:
>>> root mean square 1 2 1 2
>>> or
>>>   %:(+/%#)*: 1 2 1 2                                   NB. in immediate 
>>>execution
>>> 1.58114                                 OK
>>>
>>> A natural tendency equivalent to saying mean=: +/%#
>>> is to try
>>> rms=: %:(+/%#)*:
>>> But rms 1 2 1 2 fails.
>>> However rms=: 3: ' %:(+/%#)*:y'  or rms=: 13: ' %:(+/%#)*:y'
>>>  is OK
>>> and the latter is equivalent to
>>> rms=:[:%:[:(+/%#)*:  which works
>>>
>>> Alternatively  rms=:%:@:(+/%#)@:*:    also workS
>>>
>>> It is obvious that either the capped fork (from use of 13: or use of @: 
>>> work and I see
>>> the reasoning behind this- forcing a sequential " root after mean after 
>>> square".
>>>
>>> Am I right in assuming that immediate execution of
>>> %:(+/%#)*:   y where y is explicitly entered
>>> is treated as 3:   or  13:   ' %:(+/%#)*::y '  (explicitly in that the *:y 
>>> is the argument for mean(+/%#) whose value is the argument for %: i.e. as 
>>> in APL for which 'tacit' doesn't exist)
>>> and rms=: %:(+/%#)*:  as a tacit verb with no argument  is parsed as a  
>>> fork rather than sequentially as desired and runs into never never land?
>>>
>>> The classic example of 'mean=:+/%# as a verb is misleading in that the 
>>> example is a single fork, but trying to extend it
>>> does need a flag as to the need to emphasize the parsing.
>>> (+/%#) *:  1 2 1 2 works but msg =:(+/%#)*: doesn't work but 
>>> msg=:(+/%#)@:*:   or msg=:[:(+/%#)*: do work
>>>
>>>
>>> Don Kelly
>>>
>>> --
>>>
>>> ----------------------------------------------------------------------
>>> For information about J forums see http://www.jsoftware.com/forums.htm
>
>> ----------------------------------------------------------------------
>> For information about J forums see http://www.jsoftware.com/forums.htm
>----------------------------------------------------------------------
>For information about J forums see http://www.jsoftware.com/forums.htm
>
>
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to