Testing of simplify().



Dear List,

I am using the Maxima program pasted below to test the simplify() lisp 
function (attached) kindly contributed some time ago by Ziga Lenarcic.

In this case there is an error message:

Division by 0
#0: test_simplifyv3()(test_simplifyv3.mac line 41)
 -- an error.  To debug this try debugmode(true);
(%i30) 

I dont know lisp, but it seems to be caused by the lisp function:

(defun $rationalize_numerator (x)
  (let (($algebraic t))
    (if (zerop1 x) x (div 1 (sratsimp (div 1 x))))))

Also, its not clear whether Maxima takes into account the assume statement, as 
this leads to further simplifications.

Any assistance would be appreciated. 

Thanks very much.

Regards,

C. Frangos.





test_simplifyv3() := block(

display2d : false,


Aconz : matrix([1,0,Lo1*sin(phi),-a*sin(phi),0,0,0,0,0],
	       [0,1,-Lo1*cos(phi),a*cos(phi),0,0,0,0,0],
	       [1,0,-Lo1*sin(phi),0,-a*sin(phi),0,0,0,0],
	       [0,1,Lo1*cos(phi),0,a*cos(phi),0,0,0,0],
	       [1,0,Lo1*sin(phi)-Lc*cos(phi),0,0,0,
		-a*(cos(delta3)*sin(phi)+sin(delta3)*cos(phi)),0,0],
	       [0,1,-(Lc*sin(phi)+Lo1*cos(phi)),0,0,0,
		a*(cos(delta3)*cos(phi)-sin(delta3)*sin(phi)),0,0],
	       [1,0,-(Lo1*sin(phi)+Lc*cos(phi)),0,0,
		-a*(sin(phi)/sqrt(sin(delta3)^2*Lc^2
				   /(2*sin(delta3)*Lo1+cos(delta3)*Lc)^2
				   +1)
		   +sin(delta3)*Lc*cos(phi)
		    /((2*sin(delta3)*Lo1+cos(delta3)*Lc)
		     *sqrt(sin(delta3)^2*Lc^2
			    /(2*sin(delta3)*Lo1+cos(delta3)*Lc)^2
			    +1))),0,0,0],
	       [0,1,-(Lc*sin(phi)-Lo1*cos(phi)),0,0,
		a*(cos(phi)/sqrt(sin(delta3)^2*Lc^2
				  /(2*sin(delta3)*Lo1+cos(delta3)*Lc)^2
				  +1)
		  -sin(delta3)*Lc*sin(phi)
		   /((2*sin(delta3)*Lo1+cos(delta3)*Lc)
		    *sqrt(sin(delta3)^2*Lc^2
			   /(2*sin(delta3)*Lo1+cos(delta3)*Lc)^2
			   +1))),0,0,0],[0,0,0,1,1,0,0,0,-kon]),


display(Aconz),

assume(2*sin(delta3)*Lo1+cos(delta3)*Lc > 0),

Aconzs : simplify(Aconz),

display(Aconzs),

return(" ")

);






-------------- next part --------------
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Simplify
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; This library is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by the 
;;; Free Software Foundation; either version 2 of the License, or (at
;;; your option) any later version.
;;;
;;; This library is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; Library General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License along 
;;; with this library; if not, write to the Free Software
;;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
;;;
;;; Copyright (C) 2009 Ziga Lenarcic
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun $leaf_count (expr)
  (cond ((atom expr) 1)
        ((eq (caar expr) 'mrat) ($leaf_count (ratdisrep expr)))
        (t (1+ (reduce #'+ (mapcar #'$leaf_count (rest expr)))))))

(defun $rationalize_numerator (x)
  (let (($algebraic t))
    (if (zerop1 x) x (div 1 (sratsimp (div 1 x))))))

(defun $algebraic_simp (x)
  (let (($algebraic t))
    (sratsimp x)))

(defun hec (x)
  ($radcan ($trigreduce x)))
(defun hec2 (x)
  ($radcan ($demoivre x)))
(defun hec3 (x)
  (mfuncall '$trigsimp x))
(defparameter *simplify-functions*
  '(
  $factor
  $factorsum
  $expand
  $rationalize_numerator
  $algebraic_simp
  $ratsimp
  $radcan
  $rectform
  $trigreduce
  hec3
  hec
  $rectform
  hec2
  $exponentialize))

(defun $simplify (expr &optional (cost-function #'$leaf_count))
  (if (listp expr)
  (let ((best-expr expr)
        (best-cost (mfuncall cost-function expr)))
    (dolist (fun *simplify-functions*)
      (let* ((new-expr (funcall fun expr))
             (new-cost (mfuncall cost-function new-expr)))
        (if (< new-cost best-cost) (setf best-cost new-cost
                                         best-expr new-expr)
          (let* ((new-expr2 (funcall fun best-expr))
                 (new-cost2 (mfuncall cost-function new-expr2)))
            (when (< new-cost2 best-cost)
              (setf best-cost new-cost2
                    best-expr new-expr2))))
      ))

    (if (listp best-expr)
      (cons (first best-expr) (mapcar #'$simplify (rest best-expr)))
      best-expr)
    )
  expr))