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-symbolic-poly.lisp@ 4398

Last change on this file since 4398 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: 8.2 KB
RevLine 
[3222]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
[3224]40(defpackage #:5am-symbolic-poly
[3725]41 (:use :cl :it.bese.fiveam :monom :polynomial :infix :symbolic-polynomial))
[3222]42
[3224]43(in-package :5am-symbolic-poly)
[3222]44
[3225]45(def-suite symbolic-poly-suite
46 :description "Symbolic polynomial package suite")
[3222]47
[3227]48(in-suite symbolic-poly-suite)
[3222]49
[3391]50(test read-infix-form
51 "Infix form reader"
52 (is (equalp (with-input-from-string (s "X^2-Y^2+(-4/3)*U^2*W^3-5") (read-infix-form :stream s))
53 '(+ (- (EXPT X 2) (EXPT Y 2)) (* (- (/ 4 3)) (EXPT U 2) (EXPT W 3)) (- 5))))
[4042]54 (is (equalp (poly->string (string->poly "1" '(x y u w))) "1"))
55 (is (equalp (->sexp (string->poly "X^2-Y^2+(-4/3)*U^2*W^3-5" '(x y u w)))
[4044]56 '(+ (EXPT X 2) (* -1 (EXPT Y 2)) (* -4/3 (EXPT U 2) (EXPT W 3)) -5))))
[3391]57
[3888]58
[3271]59(def-fixture sym-poly-context ()
[3267]60 (let ((p (make-instance 'poly))
[3271]61 (p-symbolic (make-instance 'symbolic-poly :vars '(x))))
[3267]62 (dolist (x '( ((2) . 22) ((4) . 44) ((5) . 55) ((8) . 88) ((9) . 99) ))
[3730]63 (poly-insert-term p (make-instance 'term :exponents (car x) :coeff (cdr x)))
64 (poly-insert-term p-symbolic (make-instance 'term :exponents (car x) :coeff (cdr x))))
[3271]65 (&body)))
[3222]66
[3271]67(test sym-poly
68 "Symbolic polynomial"
69 (with-fixture sym-poly-context ()
[3726]70 (is (universal-equalp (change-class p 'symbolic-poly :vars '(x)) p-symbolic )))
[3281]71 (with-fixture sym-poly-context ()
72 (signals
73 (error "Number of variables does not equal dimension.")
[3726]74 (universal-equalp (change-class p 'symbolic-poly :vars '(x y)) p-symbolic ))))
[3267]75
[4203]76;; The following example uses the Grobner basis obtained with Maxima
77;; Grobner package
78;;
79;; poly_grobner([x-3*u-3*u*v^2+u^3,y-3*v-3*u^2*v+v^3,z-3*u^2+3*v^2],[u,v,x,y,z]);
80;;
81;; The Maxima output is placed in the string STR-1. The string STR-1
82;; is parsed STRING->POLY to a polynomial list GB-1. Then we take GB-1 and apply
83;; POLY->STRING to it. Then we feed the string STR-2 into Maxima as
84;; input and format according to Maxima rules. The resulting string is
85;; STR-3. We parse it with STRING->POLY to a polynomial list GB-2.
86;; Obviously, we should have equality GB-1 and GB-2. This is the test.
87;;
88(def-fixture big-poly-list-context ()
89 (let* ((vars '(u v x y z))
90 (str-1 "[x-3*u*v^2+u^3-3*u,y+v^3-3*u^2*v-3*v,z+3*v^2-3*u^2,
91 (-u*z)-3*x+6*u*v^2+9*u,(-v*z)+y-2*v^3-3*v,z^2+6*v^2*z-9*z-9*v*y+9*u*x,
92 4*u*v*z-3*u*y+3*v*x,2*u*z^2+6*x*z-18*u*z-9*u*v*y+9*v^2*x,
93 (-8*u*z^3)-24*x*z^2+72*u*z^2-36*v^2*x*z+27*u*y^2-27*v*x*y,
94 z^3+18*v^2*z^2-18*z^2-54*v*y*z+54*v^2*z+81*z+27*y^2-27*x^2,
95 (-4*z^4)+48*z^3-108*v*y*z^2+108*z^2+135*y^2*z+324*v*y*z+108*x^2*z
96 -1296*v^2*z-1944*z-243*v^2*y^2-648*y^2+243*v^2*x^2+648*x^2,
97 8*v*z^3-9*y*z^2+72*v*z^2+54*v^2*y*z-27*y*z-27*v*y^2+27*v*x^2,
98 (-8*v*z^4)+12*y*z^3-96*v*z^3-216*v*z^2-135*v*y^2*z+324*y*z-27*v*x^2*z
99 +81*y^3+81*v*y^2-81*x^2*y-81*v*x^2,
100 (-64*v*z^6)+120*y*z^5-1152*v*z^5+288*y*z^4-5184*v*z^4-648*v*y^2*z^3
101 -216*y*z^3+6912*v*z^3+81*y^3*z^2-9720*v*y^2*z^2
102 -1539*x^2*y*z^2+31104*y*z^2+62208*v*z^2+8505*y^3*z
103 +46656*v*y^2*z-8505*x^2*y*z-93312*y*z+729*v*y^4-23328*y^3
104 -1458*v*x^2*y^2-23328*v*y^2+23328*x^2*y+729*v*x^4
105 +23328*v*x^2,
106 8*z^6-72*z^5+648*v*y*z^4-648*z^4-945*y^2*z^3+5184*v*y*z^3-189*x^2*z^3
107 +5832*z^3+972*y^2*z^2+17496*v*y*z^2-2430*x^2*z^2+8748*v*y^3*z
108 -19683*y^2*z+2187*x^2*z-5103*y^4-4374*v*y^3+5832*x^2*y^2
109 +4374*v*x^2*y-729*x^4,
110 8*z^7-48*z^6+648*v*y*z^5-864*z^5-945*y^2*z^4+5832*v*y*z^4-189*x^2*z^4
111 +3888*z^4+81*y^2*z^3+17496*v*y*z^3-2997*x^2*z^3+17496*z^3
112 +8748*v*y^3*z^2-16767*y^2*z^2+17496*v*y*z^2-5103*x^2*z^2
113 -5103*y^4*z+5832*x^2*y^2*z-6561*y^2*z-729*x^4*z+6561*x^2*z
114 -2187*y^4+4374*x^2*y^2-2187*x^4,
115 64*z^9-10368*z^7+1296*y^2*z^6-1296*x^2*z^6-34992*y^2*z^5-34992*x^2*z^5
116 +419904*z^5+174960*y^2*z^4-174960*x^2*z^4-10935*y^4*z^3
117 -56862*x^2*y^2*z^3+314928*y^2*z^3-10935*x^4*z^3+314928*x^2*z^3
118 +118098*y^4*z^2-118098*x^4*z^2+59049*y^4*z-118098*x^2*y^2*z
119 +59049*x^4*z+19683*y^6-59049*x^2*y^4+59049*x^4*y^2-19683*x^6]")
[4204]120 ;; Parse STR-1 to polynomial list GB-1
[4203]121 (gb-1 (string->poly str-1 vars))
[4204]122 ;; Format GB-1 as a string STR-2
[4203]123 (str-2 (poly->string gb-1))
[4204]124 ;; Here STR-3 is a Maxima-postprocessed STR-2"
[4203]125 (str-3 "[X-3*U*V^2+U^3-3*U,Y+V^3-3*U^2*V-3*V,Z+3*V^2-3*U^2,
126 (-U*Z)-3*X+6*U*V^2+9*U,(-V*Z)+Y-2*V^3-3*V,Z^2+6*V^2*Z-9*Z-9*V*Y+9*U*X,
127 4*U*V*Z-3*U*Y+3*V*X,2*U*Z^2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z^4+81*Y^2*Z^3+17496*V*Y*Z^3-2997*X^2*Z^3+17496*Z^3
147 +8748*V*Y^3*Z^2-16767*Y^2*Z^2+17496*V*Y*Z^2-5103*X^2*Z^2
148 -5103*Y^4*Z+5832*X^2*Y^2*Z-6561*Y^2*Z-729*X^4*Z+6561*X^2*Z
149 -2187*Y^4+4374*X^2*Y^2-2187*X^4,
150 64*Z^9-10368*Z^7+1296*Y^2*Z^6-1296*X^2*Z^6-34992*Y^2*Z^5-34992*X^2*Z^5
151 +419904*Z^5+174960*Y^2*Z^4-174960*X^2*Z^4-10935*Y^4*Z^3
152 -56862*X^2*Y^2*Z^3+314928*Y^2*Z^3-10935*X^4*Z^3+314928*X^2*Z^3
153 +118098*Y^4*Z^2-118098*X^4*Z^2+59049*Y^4*Z-118098*X^2*Y^2*Z
154 +59049*X^4*Z+19683*Y^6-59049*X^2*Y^4+59049*X^4*Y^2-19683*X^6]")
[4204]155 ;; Finally, obtain a polynomial list GB-2 by parsing STR-3
[4203]156 (gb-2 (string->poly str-3 vars)))
[4204]157 ;; Discart STR-2
[4203]158 (declare (ignore str-2))
159 (&body)))
[3271]160
[4203]161(test big-poly-list
162 "Big polynomial list"
163 (with-fixture big-poly-list-context ()
164 (is (universal-equalp gb-1 gb-2))))
165
166
[3225]167(run! 'symbolic-poly-suite)
[3222]168(format t "All tests done!~%")
169
170
Note: See TracBrowser for help on using the repository browser.