Alex Giannakopoulos wrote:
>  Which of these two is the more efficient (if there's any difference) 

As Kip and Linda pointed out, the dyadic valence of ^. provides for
arbitrary bases. So one would assume 10&^. would be most efficient.

>  is there any way way mere mortals can tell?

But how can we test that assumption? There are a couple of foreign verbs
that measure the time & space utilization of a sentence.  

   time      =: 6!:2
   space     =: 7!:2@:]      NB. @:] because 7!:2 is a monad, and 6!:2 is a
dyad
   timespacex=: time , space
   
   expInv    =:  ^inv 
   divLog    =:  %&^.~       NB. Nice use of & BTW.
   log       =:  ^.
   
   D  =:  1e3 %~ 4e6 ? 1e7   NB. Important to build argument outside of
timed code   
   E  =:  '10 expInv D';'10 divLog D';'10 log D'  NB. expressions as strings
   
   TS =:  10 timespacex&> E  NB. Average over 10 runs (for time, not space)
   
   '0.2f' 8!:2 (%"1 <./) TS NB. Most efficient expression scores a 1; worse
scores are multiples of that.
 1.00 2.00
14.23 1.00
 1.11 2.00
   
Here, we see ^. and ^ ^:_1 are essentially the same, which is unsurprising,
because

   ^ b._1  NB. How is ^'s inverse calculated?
^.

Log is literally the inverse of exp.  The more interesting result is that
while %&^. is an order of magnitude slower than ^. , it consistently takes
only half the space. I have no idea why, but these kinds of tests have,
historically, revealed opportunities for improvement to the interpreter.

>  Further, if I only wanted to find the order of a given +ve integer
>  would it be better to do <. @ log10  (or <. @ logb10)
>  or something like:  <:@#@(10&#. inv)

Just as above, we can use 6!:2 and 7!:2 to measure & compare these
approaches, but I'll leave that as an exercise.  I will note that, as you
noticed, #.^:_1 already calculates the order of y relative to x . How? 
Why, let's just ask!

   10&#. b._1
($&10@>:@(10&(<.@^.))@(1&>.)@(>./)@:|@, #: ]) :.(10&#.)

Which (as you can see) is largely concerned with the calculation of said
magnitude.  To externalize that calculation and make it reusable, we might
distill it thus:

   NB. Special code indicates <.@^. is recognized but <.@:^. isn't, which
would otherwise be preferred
   fbase =:  <.@^.

   NB. Number of Digits Required in base x to represent maximal magnitude
in y   
   ndr   =:  10&$: : (>:@:fbase 1 >. |@:,)

Here, x can be any base (haxadecimal, binary, etc, though decimal is the
default for the monad), and y can be any array of integers. In particular,
y can contain positives, negatives (hence |), and even zero (hence 1 >. ),
and ndr will produce the number of digits required to represent the
number(s) with the greatest magnitude, and thus the minimum number of
digits required to represent any value in y in the base x. 

-Dan
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to