Re: what's it worth to write a short program for polynomial multiplication?
- From: Dan Christensen <jdc@xxxxxx>
- Date: Mon, 16 Jun 2008 14:00:52 -0400
rjf <fateman@xxxxxxxxx> writes:
On Jun 15, 12:58 pm, Christopher Creutzig <christop...@xxxxxxxxxxx>
wrote:
c = Hash.new(0)
a.each {|ea,ca| b.each {|eb,cb| c[ea+eb] += ca*cb }}
Certainly not optimized for anything, maybe(!) except for code size. The
code would look virtually the same in almost any language that provides
hash tables, which certainly includes C++, Python, MuPAD, Maple, and I
would be very surprised if it did not include LISP, Axiom etc.
The equivalent program in lisp is
(defun pt (a b)
(let ((z (make-hash-table)))
(maphash (lambda(e c)
(maphash (lambda (e2 c2)
(incf (gethash (+ e e2) z 0) (* c c2))) a)) b) z))
While persons unused to Lisp may find there are "lots of parentheses",
persons used to Lisp may find it
distracting in Ruby or C that one must remember symbols such as {[]}
+=*. all with different precedence rules, syntactic parts, etc.
As Christopher pointed out, the code will be similar in all
languages with hash tables, but the readability does vary.
Python tends to avoid punctuation. Here's the equivalent code:
c = collections.defaultdict(int)
for ea in a:
for eb in b:
c[ea+eb] += a[ea]*b[eb]
The only cryptic line is the first, which defines a hash table
whose default value is 0.
Dan
.
- Follow-Ups:
- References:
- what's it worth to write a short program for polynomial multiplication?
- From: rjf
- Re: what's it worth to write a short program for polynomial multiplication?
- From: Herman Rubin
- Re: what's it worth to write a short program for polynomial multiplication?
- From: rjf
- Re: what's it worth to write a short program for polynomial multiplication?
- From: Herman Rubin
- Re: what's it worth to write a short program for polynomial multiplication?
- From: rjf
- Re: what's it worth to write a short program for polynomial multiplication?
- From: Christopher Creutzig
- Re: what's it worth to write a short program for polynomial multiplication?
- From: rjf
- Re: what's it worth to write a short program for polynomial multiplication?
- From: Christopher Creutzig
- Re: what's it worth to write a short program for polynomial multiplication?
- From: rjf
- what's it worth to write a short program for polynomial multiplication?
- Prev by Date: Re: Maxima question (bigfloat computation of zeta function)
- Next by Date: Re: Maxima question (bigfloat computation of zeta function)
- Previous by thread: Re: what's it worth to write a short program for polynomial multiplication?
- Next by thread: Re: what's it worth to write a short program for polynomial multiplication?
- Index(es):
Relevant Pages
|