close Warning: Can't synchronize with repository "(default)" (The repository directory has changed, you should resynchronize the repository with: trac-admin $ENV repository resync '(default)'). Look in the Trac log for more information.

source: branches/f4grobner/5am-division.lisp@ 4356

Last change on this file since 4356 was 4325, checked in by Marek Rychlik, 9 years ago

Now classes INTEGER-RING and RATIONAL-FIELD are a part of the RING package

File size: 4.0 KB
RevLine 
[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(defpackage #:5am-division
[4325]41 (:use :cl :it.bese.fiveam :monom :polynomial :infix :symbolic-polynomial :division :ring))
[4088]42
43(in-package :5am-division)
44
45(def-suite division-suite
46 :description "Division algorithm suite")
47
48(in-suite division-suite)
49
50;; Manual calculation supporting the test below.
51;; We divide X^2 by [X+Y,X-2*Y] with LEX> as order.
52;; LM(X^2)=X^2 is divisible by LM(X+Y)=X so the first partial quotient is X.
53;; Next, X^2 - X*(X+Y) = -X*Y.
54;; LM(-X*Y)=X*Y is divibile by LM(X+Y)=X so the second partial quotient is -Y.
55;; Next, -X*Y-(-Y)*(X+Y) = Y^2.
56;; LM(Y^2)=Y^2 is not divisible by LM(X+Y)=X or LM(X-2*Y)=X. Hence, division
57;; ends. The list of quotients is [X-Y,0]. The remainder is Y^2
58
59(def-fixture division-context ()
60 (let* ((f (string->poly "x^2" '(x y)))
61 (y-sq (string->poly "y^2" '(x y)))
62 (fl (cdr (string->poly "[x+y,x-2*y]" '(x y))))
[4312]63 (quotients (cdr (string->poly "[x-y,0]" '(x y))))
64 (one (make-instance 'integer-ring :value 1)))
[4088]65 (&body)))
66
67(test normal-form
68 "Normal form"
69 (with-fixture division-context ()
[4312]70 (is (universal-equalp (multiple-value-list (normal-form f fl)) (list y-sq one 2)))
71 (is (universal-equalp (multiple-value-list (poly-pseudo-divide f fl)) (list quotients y-sq one 2)))
72 (is-false (buchberger-criterion fl))
73 )
74 )
[4088]75
[4212]76(test normal-form-easy
[4213]77 "Easy normal form tests"
78 (is (universal-zerop (normal-form (string->poly "0" '(x y)) (cdr (string->poly "[x,y]" '(x y))))))
79 ;; Maxima equivalent: poly_normal_form(3*x^2*y-x*y-1,[x-y,x+y],[x,y]);
80 (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))))
81 (string->poly "3*y^3-y^2-1" '(x y))))
82 ;; Maxima equivalent: poly_normal_form(3*x^2*y*z-x*y^3-1,[x^2-2*y,x+y*z],[x,y,z]);
83 (is (universal-equalp (normal-form (string->poly "3*x^2*y*z-x*y^3-1" '(x y z))
84 (cdr (string->poly "[x^2-2*y,x+y*z]" '(x y z))))
85 (string->poly "y^4*z+6*y^2*z-1" '(x y z)))))
[4212]86
[4088]87(def-fixture exact-division-context ()
88 (let* ((f (string->poly "x^2-4*y^2" '(x y)))
89 (g (string->poly "x+2*y" '(x y)))
90 (h (string->poly "x-2*y" '(x y))))
91 (&body)))
92
93(test exact-division
94 "Exact division in polynomial ring"
95 (with-fixture exact-division-context ()
[4172]96 (is (universal-equalp (poly-exact-divide f g) h))
97 (is (universal-zerop (subtract-from (poly-exact-divide f g) h)))))
[4088]98
99
100(run! 'division-suite)
101(format t "All tests done!~%")
102
103
Note: See TracBrowser for help on using the repository browser.