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@ 4312

Last change on this file since 4312 was 4312, checked in by Marek Rychlik, 8 years ago

Added ring classes

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