I don't know if this already passed the forum: how to speed up
exponentiation x^y with y>:0 for integer arguments giving a x: result;
e.g. if you have to change to x: in case:
y > ([:<.308*%@(10&^.))"0, x
([:<.308*%@(10&^.))"0, 2
1023
2^1023
8.988465674e307
2^1024
_
2x^1024
17976931348................
From existing algorithms:
pow=: 4 : 0"0
if. 0=y do. 1 return. end.
if. 2|y do. x * *: x pow -:<:y else. *: x pow -:y end.
)
or:
powi=: 4 : '(*:@]`(x**:@])@.[)/,1x,~2|<[EMAIL PROTECTED]:^:a:,y'"0
or:
powt=: ((*:@[$:-:@])`([**:@[$:-:@<:@])@.(2|]))`1:@.(0=])"0
and:
pow2=: (*/@:^2x^I.@|[EMAIL PROTECTED]:@])"0
From ~help/dictionary/cwhile.htm :
exp =: 4 : 0"0
z=.1
a=.x
n=.y
while. n do.
if. 2|n do. z=.z*a end.
a=.*:a
n=.<.-:n
end.
z
)
ts '3x ^ 100000'
3.138055 362560
ts '3x pow 100000'
1.298968 166464
ts '3 powi 100000'
1.299312 171776
ts '3x powt 100000'
3.138818 209152
ts '3 pow2 100000'
3.716827 576896
ts '3x exp 100000'
3.154187 231680
pow and powi are reasonable candidates. Is there more to gain?
=@@i
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm