Skip to content

Commit

Permalink
3 minutes of work on new-roots.lisp
Browse files Browse the repository at this point in the history
 Split SOLVE-QUADRIC into two parts -- inline the optional
 argument parsing and keep the body out-of-line.
  • Loading branch information
nikodemus committed Oct 9, 2010
1 parent 0ed94f9 commit acccf0e
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions new-roots.lisp
Expand Up @@ -37,10 +37,7 @@
(declare (fixnum n))
(make-array n :element-type 'double-float))

(defun solve-quadric (quadric &optional (roots (make-roots 2))
&key (offset 0))
"Solve QUADRIC, returning number of real roots and storing their values in ROOTS,
starting at OFFSET."
(defun %solve-quadric (quadric roots offset)
(declare (type (simple-array double-float (*)) quadric roots))
;; normal form: x^2 + px + q = 0
(let* ((K (aref quadric 2))
Expand All @@ -58,6 +55,12 @@ starting at OFFSET."
(aref roots (+ offset 1)) (- (- sqrt-D) p))
(values 2 roots))))))

(declaim (inline solve-quadric))
(defun solve-quadric (quadric &optional (roots (make-roots 2)))
"Solve QUADRIC, returning number of real roots and storing their values in ROOTS,
starting at OFFSET."
(%solve-quadric quadric roots 0))

(defun solve-cubic (cubic &optional (roots (make-roots 3)))
"Solve CUBIC, returning number of real roots and storing their values in ROOTS."
(declare (type (simple-array double-float (*)) cubic roots))
Expand Down Expand Up @@ -164,11 +167,11 @@ starting at OFFSET."
(setf (aref coeffs 0) (- z u)
(aref coeffs 1) (if (< q 0) (- v) v)
(aref coeffs 2) 1.0d0)
(setf n-roots (solve-quadric coeffs roots))
(setf n-roots (%solve-quadric coeffs roots 0))
(setf (aref coeffs 0) (+ z u)
(aref coeffs 1) (if (< q 0) v (- v))
(aref coeffs 2) 1.0d0)
(incf n-roots (solve-quadric coeffs roots :offset n-roots))))))
(incf n-roots (%solve-quadric coeffs roots n-roots))))))
;; resubstitute
(let ((sub (* 1/4 A)))
(dotimes (i n-roots)
Expand Down

0 comments on commit acccf0e

Please sign in to comment.