Skip to content

Commit

Permalink
Add quadratic-complex-solutions
Browse files Browse the repository at this point in the history
- Also switch to potentially more numerically stable quadratic formulas
- Closes racket#21
  • Loading branch information
dieggsy committed Feb 24, 2019
1 parent 58f5d62 commit c320001
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
8 changes: 8 additions & 0 deletions math-doc/math/scribblings/math-number-theory.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,14 @@ lowest term is the number @math-style{(p+r)/(q+s)}.
@; ----------------------------------------
@section[#:tag "quadratics"]{The Quadratic Equation}

@defproc[(quadratic-complex-solutions [a Real] [b Real] [c Real]) (Listof Complex)]{
Returns a list of all complex solutions to the equation @math-style{a x^2 + b x +c = 0}.
@interaction[#:eval untyped-eval
(quadratic-solutions 1 0 -1)
(quadratic-solutions 1 2 1)
(quadratic-solutions 1 0 1)]
}

@defproc[(quadratic-solutions [a Real] [b Real] [c Real]) (Listof Real)]{
Returns a list of all real solutions to the equation @math-style{a x^2 + b x +c = 0}.
@interaction[#:eval untyped-eval
Expand Down
21 changes: 11 additions & 10 deletions math-lib/math/private/number-theory/quadratic.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
(require racket/list
"types.rkt")

(provide quadratic-solutions
(provide quadratic-complex-solutions
quadratic-solutions
quadratic-integer-solutions
quadratic-natural-solutions)

(: quadratic-complex-solutions : Real Real Real -> (Listof Complex))
(define (quadratic-complex-solutions a b c)
; return list of solutions to a a x^2 + b x + c = 0
(let* ([d (- (* b b) (* 4 a c))]
[sign (if (>= 0 (real-part (* (conjugate b) (sqrt d)))) 1 -1)]
[q (/ (+ b (* sign d)) -2)])
(list (/ q a) (/ c q))))

(: quadratic-solutions : Real Real Real -> (Listof Real))
(define (quadratic-solutions a b c)
; return list of solutions to a a x^2 + b x + c = 0
(let ([d (- (* b b) (* 4 a c))])
(cond
[(< d 0) '()]
[(= d 0) (list (/ b (* -2 a)))]
[else
(let ([sqrt-d (sqrt d)])
(list (/ (- (- b) sqrt-d) (* 2 a))
(/ (+ (- b) sqrt-d) (* 2 a))))])))
(filter real? (quadratic-solutions a b c)))

(: quadratic-integer-solutions : Real Real Real -> (Listof Integer))
(define (quadratic-integer-solutions a b c)
Expand Down
3 changes: 3 additions & 0 deletions math-test/math/tests/number-theory-tests.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
(require typed/rackunit)

; "quadratic.rkt"
(check-equal? (quadratic-complex-solutions 1 0 -4) '(-2 2))
(check-equal? (quadratic-complex-solutions 1 0 +4) '(0-2i 0+2i))
(check-equal? (quadratic-complex-solutions 1 0 0) '(0))
(check-equal? (quadratic-solutions 1 0 -4) '(-2 2))
(check-equal? (quadratic-solutions 1 0 +4) '())
(check-equal? (quadratic-solutions 1 0 0) '(0))
Expand Down

0 comments on commit c320001

Please sign in to comment.