[4088] | 1 | ;;; -*- Mode: Lisp -*-
|
---|
| 2 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 3 | ;;;
|
---|
| 4 | ;;; Copyright (C) 1999, 2002, 2009, 2015 Marek Rychlik <rychlik@u.arizona.edu>
|
---|
| 5 | ;;;
|
---|
| 6 | ;;; This program is free software; you can redistribute it and/or modify
|
---|
| 7 | ;;; it under the terms of the GNU General Public License as published by
|
---|
| 8 | ;;; the Free Software Foundation; either version 2 of the License, or
|
---|
| 9 | ;;; (at your option) any later version.
|
---|
| 10 | ;;;
|
---|
| 11 | ;;; This program is distributed in the hope that it will be useful,
|
---|
| 12 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 14 | ;;; GNU General Public License for more details.
|
---|
| 15 | ;;;
|
---|
| 16 | ;;; You should have received a copy of the GNU General Public License
|
---|
| 17 | ;;; along with this program; if not, write to the Free Software
|
---|
| 18 | ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
---|
| 19 | ;;;
|
---|
| 20 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 21 |
|
---|
| 22 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 23 | ;;
|
---|
| 24 | ;; Run tests using 5am unit testing framework
|
---|
| 25 | ;;
|
---|
| 26 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
---|
| 27 |
|
---|
| 28 | ;; We assume that QuickLisp package manager is installed.
|
---|
| 29 | ;; See :
|
---|
| 30 | ;; https://www.quicklisp.org/beta/
|
---|
| 31 | ;;
|
---|
| 32 |
|
---|
| 33 | ;; The following is unnecessary after running:
|
---|
| 34 | ;; * (ql:add-to-init-file)
|
---|
| 35 | ;; at lisp prompt:
|
---|
| 36 | ;;(load "~/quicklisp/setup")
|
---|
| 37 |
|
---|
| 38 | (ql:quickload :fiveam)
|
---|
| 39 |
|
---|
| 40 | ;; Unless NGROBNER system loaded by ASDF,
|
---|
| 41 | ;; load the dependencies directly
|
---|
| 42 | #-ngrobner
|
---|
| 43 | (progn
|
---|
| 44 | (require :utils "utils")
|
---|
| 45 | (require :copy "copy")
|
---|
| 46 | (require :monom "monom")
|
---|
| 47 | (require :polynomial "polynomial")
|
---|
| 48 | (require :infix "infix")
|
---|
| 49 | (require :symbolic-polynomial "symbolic-polynomial")
|
---|
| 50 | (require :division "division"))
|
---|
| 51 |
|
---|
| 52 | (defpackage #:5am-division
|
---|
| 53 | (:use :cl :it.bese.fiveam :monom :polynomial :infix :symbolic-polynomial :division))
|
---|
| 54 |
|
---|
| 55 | (in-package :5am-division)
|
---|
| 56 |
|
---|
| 57 | (def-suite division-suite
|
---|
| 58 | :description "Division algorithm suite")
|
---|
| 59 |
|
---|
| 60 | (in-suite division-suite)
|
---|
| 61 |
|
---|
| 62 | ;; Manual calculation supporting the test below.
|
---|
| 63 | ;; We divide X^2 by [X+Y,X-2*Y] with LEX> as order.
|
---|
| 64 | ;; LM(X^2)=X^2 is divisible by LM(X+Y)=X so the first partial quotient is X.
|
---|
| 65 | ;; Next, X^2 - X*(X+Y) = -X*Y.
|
---|
| 66 | ;; LM(-X*Y)=X*Y is divibile by LM(X+Y)=X so the second partial quotient is -Y.
|
---|
| 67 | ;; Next, -X*Y-(-Y)*(X+Y) = Y^2.
|
---|
| 68 | ;; LM(Y^2)=Y^2 is not divisible by LM(X+Y)=X or LM(X-2*Y)=X. Hence, division
|
---|
| 69 | ;; ends. The list of quotients is [X-Y,0]. The remainder is Y^2
|
---|
| 70 |
|
---|
| 71 | (def-fixture division-context ()
|
---|
| 72 | (let* ((f (string->poly "x^2" '(x y)))
|
---|
| 73 | (y-sq (string->poly "y^2" '(x y)))
|
---|
| 74 | (fl (cdr (string->poly "[x+y,x-2*y]" '(x y))))
|
---|
| 75 | (quotients (cdr (string->poly "[x-y,0]" '(x y)))))
|
---|
| 76 | (&body)))
|
---|
| 77 |
|
---|
| 78 | (test normal-form
|
---|
| 79 | "Normal form"
|
---|
| 80 | (with-fixture division-context ()
|
---|
| 81 | (is (universal-equalp (multiple-value-list (normal-form f fl)) (list y-sq 1 2)))
|
---|
| 82 | (is (universal-equalp (multiple-value-list (poly-pseudo-divide f fl))
|
---|
| 83 | (list quotients y-sq 1 2)))
|
---|
| 84 | (is-false (buchberger-criterion fl))))
|
---|
| 85 |
|
---|
[4212] | 86 | (test normal-form-easy
|
---|
[4213] | 87 | "Easy normal form tests"
|
---|
| 88 | (is (universal-zerop (normal-form (string->poly "0" '(x y)) (cdr (string->poly "[x,y]" '(x y))))))
|
---|
| 89 | ;; Maxima equivalent: poly_normal_form(3*x^2*y-x*y-1,[x-y,x+y],[x,y]);
|
---|
| 90 | (is (universal-equalp (normal-form (string->poly "3*x^2*y-x*y-1" '(x y)) (cdr (string->poly "[x-y,x+y]" '(x y))))
|
---|
| 91 | (string->poly "3*y^3-y^2-1" '(x y))))
|
---|
| 92 | ;; Maxima equivalent: poly_normal_form(3*x^2*y*z-x*y^3-1,[x^2-2*y,x+y*z],[x,y,z]);
|
---|
| 93 | (is (universal-equalp (normal-form (string->poly "3*x^2*y*z-x*y^3-1" '(x y z))
|
---|
| 94 | (cdr (string->poly "[x^2-2*y,x+y*z]" '(x y z))))
|
---|
| 95 | (string->poly "y^4*z+6*y^2*z-1" '(x y z)))))
|
---|
[4212] | 96 |
|
---|
[4088] | 97 | (def-fixture exact-division-context ()
|
---|
| 98 | (let* ((f (string->poly "x^2-4*y^2" '(x y)))
|
---|
| 99 | (g (string->poly "x+2*y" '(x y)))
|
---|
| 100 | (h (string->poly "x-2*y" '(x y))))
|
---|
| 101 | (&body)))
|
---|
| 102 |
|
---|
| 103 | (test exact-division
|
---|
| 104 | "Exact division in polynomial ring"
|
---|
| 105 | (with-fixture exact-division-context ()
|
---|
[4172] | 106 | (is (universal-equalp (poly-exact-divide f g) h))
|
---|
| 107 | (is (universal-zerop (subtract-from (poly-exact-divide f g) h)))))
|
---|
[4088] | 108 |
|
---|
| 109 |
|
---|
| 110 | (run! 'division-suite)
|
---|
| 111 | (format t "All tests done!~%")
|
---|
| 112 |
|
---|
| 113 |
|
---|