Subject: Maxima rationals compared to CL rationals
From: Barton Willis
Date: Thu, 2 Aug 2007 14:05:06 -0500
Camm,
Wow nice job! -- the GCL 2.7.0 time is *super* fast. Thanks for the
speedy reply.
If anybody would like to experiment with using CL rationals
for *some* internal calculations, try the following code. If Maxima
used CL rationals in expressions, the overhead of using the
cl-rat-to-maxima and the maxima-to-cl functions could be
eliminated.
(defun exptb (x n)
(if (integerp x) (cl-rat-to-maxima (expt x n)) (expt x n)))
(defun *red (n d)
(setq n (/ n d))
(if $float (float n) (cl-rat-to-maxima n)))
(defun maxima-rat-to-cl (x)
(/ (num1 x) (denom1 x)))
;; Multiply rational numbers x and y. When the product isn't an integer and
;; $float is true, convert the product into a double float.
(defun timeskl (x y)
(setq x (* (maxima-rat-to-cl x) (maxima-rat-to-cl y)))
(cond ((integerp x) x)
($float (float x))
(t (cl-rat-to-maxima x))))
;; Add two numbers -- each number can be an integer, a Maxima rational,
;; a double float, or a big float.
(defun addk (x y) ; x and y are assumed to be reduced
(cond ((eq x 0) y)
((eq y 0) x)
(t
(cond ((or ($bfloatp x) ($bfloatp y)) (setq x ($bfloat x) y ($bfloat y)))
((floatp x) (setq y ($float y)))
((floatp y) (setq x ($float x))))
(setq x (cond (($bfloatp x) (addbigfloat (list x y)))
((floatp x) (+ x y))
(t (+ (maxima-rat-to-cl x) (maxima-rat-to-cl y)))))
(cond ((or (floatp x) ($bfloatp x) (integerp x)) x)
($float (float x))
(t (cl-rat-to-maxima x))))))
Barton
-----camm at intech19.enhanced.com wrote: -----
>To: Barton Willis
>From: Camm Maguire
>Sent by: camm at intech19.enhanced.com
>Date: 08/02/2007 01:48PM
>cc: maxima , gcl-devel at gnu.org
>Subject: Re: [Maxima] Maxima rationals compared to CL rationals
>
>Greetings!
>
>Just FYI ---
>
>gcl
>GCL (GNU Common Lisp) 2.6.8 CLtL1 Jul 4 2007 16:45:08
>Source License: LGPL(gcl,gmp), GPL(unexec,bfd,xgcl)
>Binary License: GPL due to GPL'ed components: (XGCL READLINE BFD UNEXEC)
>Modifications of this banner must retain notice of a compatible license
>Dedicated to the memory of W. Schelter
>
>Use (help) to get some basic information on how to use GCL.
>Temporary directory for compiler files set to /tmp/
>
>>(defun h (n)
> (let ((acc 0))
> (dotimes (i n acc)
> (setq acc (+ acc (/ 1 (+ 1 i)))))))
>
>H
>
>>(compile 'h)
>
>Compiling /tmp/gazonk_16089_0.lsp.
>End of Pass 1.
>End of Pass 2.
>OPTIMIZE levels: Safety=0 (No runtime error checking), Space=0, Speed=3
>Finished compiling /tmp/gazonk_16089_0.lsp.
>Loading /tmp/gazonk_16089_0.o
>start address -T 0x8644000 Finished loading /tmp/gazonk_16089_0.o
>#
>NIL
>NIL
>
>>(time (progn (h 5000) nil))
>
>real time : 12.820 secs
>run-gbc time : 6.510 secs
>child run time : 0.000 secs
>gbc time : 0.390 secs
>NIL
>
>>
>GCL_ANSI=t gclcvs
>GCL (GNU Common Lisp) 2.7.0 ANSI Jul 18 2007 10:48:57
>Source License: LGPL(gcl,gmp,pargcl), GPL(unexec,bfd,xgcl)
>Binary License: GPL due to GPL'ed components: (XGCL READLINE BFD UNEXEC)
>Modifications of this banner must retain notice of a compatible license
>Dedicated to the memory of W. Schelter
>
>Use (help) to get some basic information on how to use GCL.
>
>Temporary directory for compiler files set to /tmp/
>
>>(defun h (n)
> (let ((acc 0))
> (dotimes (i n acc)
> (setq acc (+ acc (/ 1 (+ 1 i)))))))
>
>H
>
>>(compile 'h)
>
>;; Compiling /tmp/gazonk_16375_0.lsp.
>;; End of Pass 1.
>;; End of Pass 2.
>;; OPTIMIZE levels: Safety=0 (No runtime error checking), Space=0,
>Speed=3, (Debug quality ignored)
>;; Finished compiling /tmp/gazonk_16375_0.o.
>;; Loading /tmp/gazonk_16375_0.o
> ;; start address -T 0xaa5d88 ;; Finished loading /tmp/gazonk_16375_0.o
>#
>NIL
>NIL
>
>>(time (progn (h 5000) nil))
>
>real time : 0.080 secs
>run-gbc time : 0.010 secs
>child run time : 0.000 secs
>gbc time : 0.070 secs
>NIL
>
>Take care,
>
>
>Barton Willis writes:
>
>> Which function will compute 1 + 1/2 + ... + 1/5000 the fastest?
>>
>> ;; Use Maxima rational numbers with add & div.
>>
>> (defun $harmonic (n)
>> (let ((acc 0))
>> (dotimes (i n acc)
>> (setq acc (add acc (div 1 (+ 1 i)))))))
>>
>> ;; Use CL rationals with + and /.
>>
>> (defun $harmonic2 (n)
>> (let ((acc 0))
>> (dotimes (i n (cl-rat-to-maxima acc))
>> (setq acc (+ acc (/ 1 (+ 1 i)))))))
>>
>> Using GCL, I get
>>
>> (%i1) load("harmonic.o")$
>>
>> (%i2) harmonic(5000)$
>> Evaluation took 0.19 seconds (0.19 elapsed)
>>
>> (%i3) harmonic2(5000)$
>> Evaluation took 19.89 seconds (19.89 elapsed)
>>
>> Is this due to fast code in addk? Or is GCL just slow
>> with rational addition and division? Or something else?
>> What's the story?
>>
>> Barton
>> _______________________________________________
>> Maxima mailing list
>> Maxima at math.utexas.edu
>> http://www.math.utexas.edu/mailman/listinfo/maxima
>>
>>
>>
>
>--
>Camm Maguire camm at enhanced.com
>==========================================================================
>"The earth is but one country, and mankind its citizens." -- Baha'u'llah