Arithmetic mean forward or backward?

I am familiar with this way of doing the arithmetic mean in J:

+/ % #

      

But he also showed here how

# %~ +/

      

Are the two versions interchangeable, and if not, when should I use one or the other?

+3


source to share


3 answers


Dyadic ~

overrides verb arguments. x f~ y

is equivalent y f x

. You use ~

when you, um, want to change the arguments of a verb.



One of its most common uses is forks and hooks. For example, since y f (g y)

- (f g) y

, you can use ((f~) g) y

whenever you need (g y) f y

.

+3


source


In the reverse average example, I really don't see a reason that one way would be more efficient than the other (the VVV shape for a fork), but since the forks in J can be asymmetrical (in the NVV shape) I can see some reasons why reversing a mid-fork would be an advantage. Take for example:



   (5 # $) 1 2 3  NB. (N V V) form
3 3 3 3 3
   (5 #~ $) 1 2 3 NB. (N V~ V) becomes effectively (V V N)
5 5 5
   ($ # 5) 1 2 3  NB. (V V N) is a syntax error  
|syntax error
|       ($#5)1 2 3

      

+2


source


Dyadic~

is a "passive" adverb that swaps left and right arguments. Thus, it is the x f~ y

same as y f x

. +/ % #

and are # %~ +/

equivalent. 2 % 5

gives you 0.4

, but 2 %~ 5

gives 2.5

.

Among the places where it can be convenient, you need to check the results of the line with which you are working. While you will probably be experiencing something a little more complicated, you can test yourself by repeating your last line and just adding to the left, without rearranging anything or adding parentheses.

   string =. 'J is beyond awesome.'
   'e' = string
0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0
   string #~ 'e' = string
eee

      

monadic~

is an adverb "Reflex" that makes the modified verb act like a dyad, duplicating a single argument for both left and right. While this is another shortcut for organizing your arguments, it is very different from the dyadic one ~

. *~ 4

is 16

because you yourself are multiplying y

( y * y

).

+1


source







All Articles