Function: math-simplify-divide

math-simplify-divide is a byte-compiled function defined in calc-alg.el.gz.

Signature

(math-simplify-divide EXPR)

Source Code

;; Defined in /usr/src/emacs/lisp/calc/calc-alg.el.gz
(defun math-simplify-divide (expr)
  (let ((np (cdr expr))
	(nover nil)
	(nn (and (or (eq (car expr) '/)
                     (not (Math-realp (nth 2 expr))))
		 (math-common-constant-factor (nth 2 expr))))
	n op)
    (if nn
	(progn
	  (setq n (and (or (eq (car expr) '/)
                           (not (Math-realp (nth 1 expr))))
		       (math-common-constant-factor (nth 1 expr))))
	  (if (and (eq (car-safe nn) 'frac) (eq (nth 1 nn) 1) (not n))
	      (unless (and (eq (car-safe expr) 'calcFunc-eq)
                           (eq (car-safe (nth 1 expr)) 'var)
                           (not (math-expr-contains (nth 2 expr)
                                                    (nth 1 expr))))
		(setcar (cdr expr)
                        (math-mul (nth 2 nn) (nth 1 expr)))
		(setcar (cdr (cdr expr))
			(math-cancel-common-factor (nth 2 expr) nn))
		(if (and (math-negp nn)
			 (setq op (assq (car expr) calc-tweak-eqn-table)))
		    (setcar expr (nth 1 op))))
	    (if (and n (not (eq (setq n (math-frac-gcd n nn)) 1)))
		(progn
		  (setcar (cdr expr)
			  (math-cancel-common-factor (nth 1 expr) n))
		  (setcar (cdr (cdr expr))
			  (math-cancel-common-factor (nth 2 expr) n))
		  (if (and (math-negp n)
			   (setq op (assq (car expr)
                                          calc-tweak-eqn-table)))
		      (setcar expr (nth 1 op))))))))
    (let ((math--simplify-divide-expr expr)) ;For use in math-simplify-divisor
      (if (and (eq (car-safe (car np)) '/)
	       (math-known-scalarp (nth 2 expr) t))
	  (progn
	    (setq np (cdr (nth 1 expr)))
	    (while (eq (car-safe (setq n (car np))) '*)
	      (and (math-known-scalarp (nth 2 n) t)
		   (math-simplify-divisor (cdr n) (cdr (cdr expr)) nil t))
	      (setq np (cdr (cdr n))))
	    (math-simplify-divisor np (cdr (cdr expr)) nil t)
	    (setq nover t
		  np (cdr (cdr (nth 1 expr))))))
      (while (eq (car-safe (setq n (car np))) '*)
        (and (math-known-scalarp (nth 2 n) t)
	     (math-simplify-divisor (cdr n) (cdr (cdr expr)) nover t))
        (setq np (cdr (cdr n))))
      (math-simplify-divisor np (cdr (cdr expr)) nover t)
      expr)))