Hi all,
At 22:55 01.01.99 -1000, Ralph Freese wrote:
>
>Hi Al,
>
>Johann's version below is my vote for the best. Very nice use of
>Derive's vector functions! You might consider handling the cases when
>n or k is negative. Maybe something like replacing the first line with
>
>
>STIRLING1(n, k) := IF(k = n AND n >= 0, 1, IF(k > n OR k <= 0 OR n <= 0, 0,
> IF(k = 1, (n - 1)!,
>
>
> Ralph
>
I am divided on that. The point is that the definition of Stirling numbers
[n,k] of the first kind can be extended to arbitrary real (or even complex)
values of n in a natural way, if n-k is an integer (cf. Knuth, The Art of
Computer Programming, Volume 1, 3. ed., p 68) but this definition isn't
compatible with that suggested by Ralph. Other CAS are also divided: Maple
seems to agree with him, but Mathematica demands that n and k are
nonnegative integers. I for my part have a preference for the latter
solution which should be noted in the online help.
Nevertheless, there is still another improvement that deserves to be
mentioned:
STERLING1(n, k) := IF(k >= n, MAX(n - k + 1, 0), IF(k, 0, IF(k = 1, (n -
1)!, IF(k = n - 1, COMB(n, 2), IF(k > n/2,
(ITERATE([DELETE_ELEMENT(DELETE_ELEMENT(n_ APPEND(v_, [0]) + APPEND([0],
v_)), n - n_ + 1), n_ + 1], [v_, n_], ITERATE([DELETE_ELEMENT(n_APPEND(v_,
[0]) + APPEND([0], v_)), n_ + 1], [v_, n_], ITERATE([n_ APPEND(v_, [0]) +
APPEND([0], v_), n_ + 1], [v_, n_], [[1], 0], n - k), 2k - n), n -
k))SUB1SUB1, (ITERATE([DELETE_ELEMENT(DELETE_ELEMENT(n_ APPEND([0], v_) +
APPEND(v_, [0])), n - n_ + 1), n_ + 1], [v_, n_],
ITERATE([DELETE_ELEMENT(n_ APPEND([0], v_) + APPEND(v_, [0])), n_ + 1],
[v_, n_], ITERATE([n_ APPEND([0], v_) + APPEND(v_, [0]), n_ + 1], [v_, n_],
[[1], 0], k), n - 2k), k))SUB1SUB1)))))
Now it takes only 10s to compute Stirling(1000,500) on my Pentium 166 PC(
which is almost 4 times faster than with Mathematica 3.0 and about 20 times
faster than with Maple V, Rel.5 !)
At the risk of carrying coals to Newcastle: Huge functions like the one
above should always be copied by means of CTL-C, Alt-TAB (to change to
DfW), CTRL-A and CTRL-V. Before presseing ENTER, some editing may be
necessary to remove special characters like line feeds. Conversely, if you
are the author you should do exactly this before sending the email to make
sure that it works! Sorry again, if I was flogging a dead horse!
Cheers, Johann
_______________________________________________________________________
Johann Wiesenbauer
Institut f. Algebra und Computermathematik
der Technischen Universitaet Wien
Wiedner Hauptstrasse 8-10,
A1040-Vienna, Austria
Tel.: +43 1 58801 11813
Fax: +43 1 58801 11899
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|