Re: what's it worth to write a short program for polynomial multiplication?



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.
.



Relevant Pages

  • Re: NEWBIE: Read/write IO (Random Access)/:overwrite- replace?
    ... enumerate the keys present in the db, like you do with a hash table. ... Keep in one db file a mapping, ... Here is a prototype in Lisp using hash ... (setf (gethash 1 data) ...
    (comp.lang.lisp)
  • Re: question about Clojure immutable structures
    ... the default functional semantics for data structures that are ... I want to have some central hash table that all the threads could ... So how I'm gonna do that with immutable data structures? ... Lisp, ...) ...
    (comp.lang.lisp)
  • Re: Writing a Lisp interpreter in Perl?
    ... Actually Mike, that was pretty close to what I was planning on doing. ... Jeff already posted a link to a Lisp interpretor someone else ... > This function takes a hash as part of the input, ...
    (comp.lang.perl.misc)
  • Re: question about Clojure immutable structures
    ... the default functional semantics for data structures that are ... I want to have some central hash table that all the threads could ... So how I'm gonna do that with immutable data structures? ... Lisp is not dead. ...
    (comp.lang.lisp)
  • Re: question about Clojure immutable structures
    ... I want to have some central hash table that all the threads could ... The persistant datastructures in Clojure are immutable. ... To add a result of a fib call to the db each thread will do: ... Lisp is not dead. ...
    (comp.lang.lisp)