source: CGBLisp/src/poly-with-sugar.lisp@ 1

Last change on this file since 1 was 1, checked in by Marek Rychlik, 15 years ago

First import of a version circa 1997.

File size: 4.8 KB
Line 
1#|
2 $Id: poly-with-sugar.lisp,v 1.3 2009/01/22 04:06:12 marek Exp $
3 *--------------------------------------------------------------------------*
4 | Copyright (C) 1994, Marek Rychlik (e-mail: rychlik@math.arizona.edu) |
5 | Department of Mathematics, University of Arizona, Tucson, AZ 85721 |
6 | |
7 | Everyone is permitted to copy, distribute and modify the code in this |
8 | directory, as long as this copyright note is preserved verbatim. |
9 *--------------------------------------------------------------------------*
10|#
11(defpackage "POLY-WITH-SUGAR"
12 (:export poly-with-sugar-poly
13 poly-with-sugar-tail
14 poly-with-sugar-sugar
15 monom-sugar
16 term-sugar
17 monom-times-poly-with-sugar
18 scalar-times-poly-with-sugar
19 term-times-poly-with-sugar
20 minus-poly-with-sugar
21 poly-with-sugar+
22 poly-with-sugar-
23 poly-with-sugar-op
24 poly-with-sugar-zerop
25 poly-with-sugar-nreverse
26 poly-with-sugar-append
27 poly-with-sugar-lm
28 poly-with-sugar-lc
29 poly-with-sugar-lt
30 poly-add-sugar)
31 (:use "ORDER" "MONOM" "TERM" "TERM" "POLY" "COEFFICIENT-RING" "COMMON-LISP"))
32
33(in-package "POLY-WITH-SUGAR")
34
35#+debug(proclaim '(optimize (speed 0) (debug 3)))
36#-debug(proclaim '(optimize (speed 3) (debug 0)))
37
38;;----------------------------------------------------------------
39;; BASIC OPERATIONS ON POLYNOMIALS of many variables WITH SUGAR
40;; Hybrid operations involving polynomials and monomials or terms
41;;----------------------------------------------------------------
42;; The representation is as follows:
43;;
44;; polynomial-with-sugar: (poly . sugar)
45;;
46;;----------------------------------------------------------------
47
48(defun poly-with-sugar-poly (p) (car p))
49(defun poly-with-sugar-sugar (p) (cdr p))
50(defun poly-with-sugar-tail (p) (cons (cdar p) (cdr p)))
51
52(defsetf poly-with-sugar-poly (p) (poly)
53 `(setf (car ,p) ,poly))
54
55(defsetf poly-with-sugar-sugar (p) (sugar)
56 `(setf (cdr ,p) ,sugar))
57
58(defsetf poly-with-sugar-tail (p) (tail)
59 `(setf (cdar ,p) ,tail))
60
61;;----------------------------------------------------------------
62;; Sugar of monomials and terms
63;; The total degree is used
64;;----------------------------------------------------------------
65(defun monom-sugar (m)
66 (total-degree m))
67
68(defun coefficient-sugar (c ring)
69 (funcall (ring-length ring) c))
70
71(defun term-sugar (term ring)
72 (declare (ignore ring))
73 (monom-sugar (term-monom term)))
74
75;;----------------------------------------------------------------
76;; Initialize sugar of an ordinary polynomial
77;;----------------------------------------------------------------
78(defun poly-add-sugar (poly ring)
79 (cons poly
80 (apply #'max (mapcar #'(lambda (term) (term-sugar term ring)) poly))))
81
82;; Multiplies scalar c by a polynomial p
83(defun scalar-times-poly-with-sugar (c p ring)
84 (cons (scalar-times-poly c (poly-with-sugar-poly p) ring)
85 (poly-with-sugar-sugar p)))
86
87;; Multiplies term m by a poly-with-sugar f
88(defun term-times-poly-with-sugar (term f ring)
89 (cons (term-times-poly term (poly-with-sugar-poly f) ring)
90 (+ (poly-with-sugar-sugar f) (term-sugar term ring))))
91
92;; Multiply a monom by a poly-with-sugarnomial
93(defun monom-times-poly-with-sugar (m f)
94 (cons
95 (monom-times-poly m (poly-with-sugar-poly f))
96 (+ (poly-with-sugar-sugar f) (monom-sugar m))))
97
98;; Changes the sign of the polynomial f with sugar
99(defun minus-poly-with-sugar (f ring)
100 (cons
101 (minus-poly (poly-with-sugar-poly f) ring)
102 (poly-with-sugar-poly f)))
103
104;; Addition and subtraction of polynomials with sugar
105(defun poly-with-sugar+ (p q pred ring)
106 (cons
107 (poly+ (poly-with-sugar-poly p) (poly-with-sugar-poly q) pred ring)
108 (max (poly-with-sugar-sugar p) (poly-with-sugar-sugar q))))
109
110;; Addition and subtraction of polynomials with sugar
111(defun poly-with-sugar- (p q pred ring)
112 (cons
113 (poly- (poly-with-sugar-poly p) (poly-with-sugar-poly q) pred ring)
114 (max (poly-with-sugar-sugar p) (poly-with-sugar-sugar q))))
115
116;; Implement f-term*g
117(defun poly-with-sugar-op (f term g pred ring)
118 (poly-with-sugar- f (term-times-poly-with-sugar term g ring) pred ring))
119
120
121;; Destructively reverse the order of terms in a polynomial with sugar
122(defun poly-with-sugar-nreverse (p)
123 (setf (poly-with-sugar-poly p) (nreverse (poly-with-sugar-poly p)))
124 p)
125
126;; Append two polynomials with sugar, which are assumed to be
127;; sorted and all terms of q are smaller than terms of p
128(defun poly-with-sugar-append (p q)
129 (cons (append (poly-with-sugar-poly p) (poly-with-sugar-poly q))
130 (max (poly-with-sugar-sugar p) (poly-with-sugar-sugar q))))
131
132(defun poly-with-sugar-zerop (p)
133 (poly-zerop (poly-with-sugar-poly p)))
134
135(defun poly-with-sugar-lm (p)
136 (lm (poly-with-sugar-poly p)))
137
138(defun poly-with-sugar-lc (p)
139 (lc (poly-with-sugar-poly p)))
140
141(defun poly-with-sugar-lt (p)
142 (lt (poly-with-sugar-poly p)))
Note: See TracBrowser for help on using the repository browser.