It's a matter of taste whether expt is an improvement over exp. Me, I
think what you'd really like is to encapsulate the expansion while
allowing for a different multiply; in other words, to write an adverb.
I would go for
NB. Adverb to take exponential using the power-series definition
NB. u is (verb for initial y^0 value)`(verb for mul)`(verb for add)
expu =: 1 : 0
'`init mul add' =. u
s =. t =. init y
for_k. % >: i. 30 do. NB. Max # iterations
ns =. s add t =. k * t mul y
if. ns -: s do. break. end.
s =. ns
end.
)
Since the definition depends on linearity, you should consider whether
you need a verb for addition (or should just use +), and whether you
need a verb for scalar multiplication (instead of using * as I have here).
1"0`*`+ expuv 1 2 3 0j1p1
2.71828 7.38906 20.0855 _1j2.73774e_15
Long ago I went to a lot of trouble to create a tacit version of Givens
rotation (something to do with tridiagonal matrices). It ended up hard
to understand and not much faster than explicit code - even though it
had to run at rank 0. If you are going to feed large arrays into exp, I
bet the explicit version will be faster as well as more maintainable.
Henry Rich
On 12/22/2013 7:24 PM, km wrote:
Henry, verb expt below is what I came up with. A slight modification will
give me what I really want, a tacit exponential for quaternions represented as
complex pairs. --Kip
clean =: (* *!.1e_14@|)"0&.+.
exp =: 3 : 0
os =. 0
s =. 1
k =. 0
t =. 1
while. s ~: os do.
os =. s
k =. >: k
t =. t * y % k
s =. s + t
end.
clean s
)
NB. u ^: p ^: _ is equivalent to
NB. 3 : 't =. y while. p t do. u t end.'
yy =: 1 0 2 , ] , ]
NB. yy provides initial (sum , oldsum, futureindex, nextterm, y)
p =: {. ~: {.@}. NB. stop when sum is oldsum (tolerantly)
u =: ({. + 3&{), {. , >:@(2&{) , (3&{ * {: % 2&{) , {:
NB. u finds the next (sum, oldsum, futureindex, nextterm, y)
expt =: [: clean@{. [: u ^: p ^: _ yy NB. tacit exponential
expt 0
1
expt 1
2.71828
expt 0j1p1
_1
Sent from my iPad
On Dec 22, 2013, at 10:18 AM, Henry Rich <[email protected]> wrote:
I can't figure out a pleasing way to do this. You need 4 things: k,s,y,t. 3
of these change. So, you need to join them as a boxed list and then iterate,
but you can't iterate until they stop changing, because the iteration number
always changes. t also changes. Yuck.
Henry Rich
On 12/22/2013 3:27 AM, km wrote:
Thanks, Raul, for a cool solution. I'm still interested in tacitly terminating
a series when its partial sums stop changing. --Kip
Sent from my iPad
On Dec 22, 2013, at 12:34 AM, Raul Miller <[email protected]> wrote:
^ 2
7.38906
(%!i.40) p. 2
7.38906
(^ = (%!i.40)&p.) 0j1p1
1
I'd just use ^
--
Raul
On Sat, Dec 21, 2013 at 5:43 PM, km <[email protected]> wrote:
Verb exp below uses the series for monadic ^ to calculate ^ y . Can you remind me
how to do exp tacitly? You may omit the "clean" part. Verb clean
replaces tiny real numbers by 0 .
clean =: (* *!.1e_14@|)"0&.+.
exp =: 3 : 0
os =. 0
s =. 1
k =. 0
t =. 1
while. s ~: os do.
os =. s
k =. >: k
t =. t * y % k
s =. s + t
end.
clean s
)
exp 1
2.71828
^1
2.71828
exp 0j1p1
_1
^ 0j1p1
_1j1.22465e_16
(^0j1p1) = exp 0j1p1
1
VERSION NB. iPad
1.3 5
--Kip Murray
Sent from my iPad
----------------------------------------------------------------------
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
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm