Function: math-expand-power

math-expand-power is an autoloaded and byte-compiled function defined in calc-poly.el.gz.

Signature

(math-expand-power X N &optional VAR ELSE-NIL)

Source Code

;; Defined in /usr/src/emacs/lisp/calc/calc-poly.el.gz
(defun math-expand-power (x n &optional var else-nil)
  (or (and (natnump n)
	   (memq (car-safe x) '(+ -))
	   (let ((terms nil)
		 (cterms nil))
	     (while (memq (car-safe x) '(+ -))
	       (setq terms (cons (if (eq (car x) '-)
				     (math-neg (nth 2 x))
				   (nth 2 x))
				 terms)
		     x (nth 1 x)))
	     (setq terms (cons x terms))
	     (if var
		 (let ((p terms))
		   (while p
		     (or (math-expr-contains (car p) var)
			 (setq terms (delq (car p) terms)
			       cterms (cons (car p) cterms)))
		     (setq p (cdr p)))
		   (if cterms
		       (setq terms (cons (apply 'calcFunc-add cterms)
					 terms)))))
	     (if (= (length terms) 2)
		 (let ((i 0)
		       (accum 0))
		   (while (<= i n)
		     (setq accum (list '+ accum
				       (list '* (calcFunc-choose n i)
					     (list '*
						   (list '^ (nth 1 terms) i)
						   (list '^ (car terms)
							 (- n i)))))
			   i (1+ i)))
		   accum)
	       (if (= n 2)
		   (let ((accum 0)
			 (p1 terms)
			 p2)
		     (while p1
		       (setq accum (list '+ accum
					 (list '^ (car p1) 2))
			     p2 p1)
		       (while (setq p2 (cdr p2))
			 (setq accum (list '+ accum
					   (list '* 2 (list '*
							    (car p1)
							    (car p2))))))
		       (setq p1 (cdr p1)))
		     accum)
		 (if (= n 3)
		     (let ((accum 0)
			   (p1 terms)
			   p2 p3)
		       (while p1
			 (setq accum (list '+ accum (list '^ (car p1) 3))
			       p2 p1)
			 (while (setq p2 (cdr p2))
			   (setq accum (list '+
					     (list '+
						   accum
						   (list '* 3
							 (list
							  '*
							  (list '^ (car p1) 2)
							  (car p2))))
					     (list '* 3
						   (list
						    '* (car p1)
						    (list '^ (car p2) 2))))
				 p3 p2)
			   (while (setq p3 (cdr p3))
			     (setq accum (list '+ accum
					       (list '* 6
						     (list '*
							   (car p1)
							   (list
							    '* (car p2)
							    (car p3))))))))
			 (setq p1 (cdr p1)))
		       accum))))))
      (and (not else-nil)
	   (list '^ x n))))