Re: what's it worth to write a short program for polynomial multiplication?
- From: rjf <fateman@xxxxxxxxx>
- Date: Sun, 15 Jun 2008 14:24:59 -0700 (PDT)
On Jun 15, 12:58 pm, Christopher Creutzig <christop...@xxxxxxxxxxx>
wrote:
rjf wrote:
. In any
case, I thought I'd throw a ruby program to multiply polynomials stored
as hash tables with exponents as keys into the pot:
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.
In lisp, += is incf.
a.each{|ea,ca| X
is (maphash #'(lambda(ea ca) X) a)
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.
And the parentheses counting as well as indentation are done by almost
any modern programming text editor.
After compilation, this program is reasonable in speed.
.
- Follow-Ups:
- Re: what's it worth to write a short program for polynomial multiplication?
- From: Dan Christensen
- Re: what's it worth to write a short program for polynomial multiplication?
- 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
- what's it worth to write a short program for polynomial multiplication?
- Prev by Date: Re: which calculator or PDA to buy for calculus study? (HP-50G,
- Next by Date: Re: which calculator or PDA to buy for calculus study? (HP-50G, TI-89, Classpad 330, PDA....)
- 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
|