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

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

Moving sources to trunk

File size: 4.7 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(proclaim '(optimize (speed 0) (debug 3)))
36
37;;----------------------------------------------------------------
38;; BASIC OPERATIONS ON POLYNOMIALS of many variables WITH SUGAR
39;; Hybrid operations involving polynomials and monomials or terms
40;;----------------------------------------------------------------
41;; The representation is as follows:
42;;
43;; polynomial-with-sugar: (poly . sugar)
44;;
45;;----------------------------------------------------------------
46
47(defun poly-with-sugar-poly (p) (car p))
48(defun poly-with-sugar-sugar (p) (cdr p))
49(defun poly-with-sugar-tail (p) (cons (cdar p) (cdr p)))
50
51(defsetf poly-with-sugar-poly (p) (poly)
52 `(setf (car ,p) ,poly))
53
54(defsetf poly-with-sugar-sugar (p) (sugar)
55 `(setf (cdr ,p) ,sugar))
56
57(defsetf poly-with-sugar-tail (p) (tail)
58 `(setf (cdar ,p) ,tail))
59
60;;----------------------------------------------------------------
61;; Sugar of monomials and terms
62;; The total degree is used
63;;----------------------------------------------------------------
64(defun monom-sugar (m)
65 (total-degree m))
66
67(defun coefficient-sugar (c ring)
68 (funcall (ring-length ring) c))
69
70(defun term-sugar (term ring)
71 (declare (ignore ring))
72 (monom-sugar (term-monom term)))
73
74;;----------------------------------------------------------------
75;; Initialize sugar of an ordinary polynomial
76;;----------------------------------------------------------------
77(defun poly-add-sugar (poly ring)
78 (cons poly
79 (apply #'max (mapcar #'(lambda (term) (term-sugar term ring)) poly))))
80
81;; Multiplies scalar c by a polynomial p
82(defun scalar-times-poly-with-sugar (c p ring)
83 (cons (scalar-times-poly c (poly-with-sugar-poly p) ring)
84 (poly-with-sugar-sugar p)))
85
86;; Multiplies term m by a poly-with-sugar f
87(defun term-times-poly-with-sugar (term f ring)
88 (cons (term-times-poly term (poly-with-sugar-poly f) ring)
89 (+ (poly-with-sugar-sugar f) (term-sugar term ring))))
90
91;; Multiply a monom by a poly-with-sugarnomial
92(defun monom-times-poly-with-sugar (m f)
93 (cons
94 (monom-times-poly m (poly-with-sugar-poly f))
95 (+ (poly-with-sugar-sugar f) (monom-sugar m))))
96
97;; Changes the sign of the polynomial f with sugar
98(defun minus-poly-with-sugar (f ring)
99 (cons
100 (minus-poly (poly-with-sugar-poly f) ring)
101 (poly-with-sugar-poly f)))
102
103;; Addition and subtraction of polynomials with sugar
104(defun poly-with-sugar+ (p q pred ring)
105 (cons
106 (poly+ (poly-with-sugar-poly p) (poly-with-sugar-poly q) pred ring)
107 (max (poly-with-sugar-sugar p) (poly-with-sugar-sugar q))))
108
109;; Addition and subtraction of polynomials with sugar
110(defun poly-with-sugar- (p q pred ring)
111 (cons
112 (poly- (poly-with-sugar-poly p) (poly-with-sugar-poly q) pred ring)
113 (max (poly-with-sugar-sugar p) (poly-with-sugar-sugar q))))
114
115;; Implement f-term*g
116(defun poly-with-sugar-op (f term g pred ring)
117 (poly-with-sugar- f (term-times-poly-with-sugar term g ring) pred ring))
118
119
120;; Destructively reverse the order of terms in a polynomial with sugar
121(defun poly-with-sugar-nreverse (p)
122 (setf (poly-with-sugar-poly p) (nreverse (poly-with-sugar-poly p)))
123 p)
124
125;; Append two polynomials with sugar, which are assumed to be
126;; sorted and all terms of q are smaller than terms of p
127(defun poly-with-sugar-append (p q)
128 (cons (append (poly-with-sugar-poly p) (poly-with-sugar-poly q))
129 (max (poly-with-sugar-sugar p) (poly-with-sugar-sugar q))))
130
131(defun poly-with-sugar-zerop (p)
132 (poly-zerop (poly-with-sugar-poly p)))
133
134(defun poly-with-sugar-lm (p)
135 (lm (poly-with-sugar-poly p)))
136
137(defun poly-with-sugar-lc (p)
138 (lc (poly-with-sugar-poly p)))
139
140(defun poly-with-sugar-lt (p)
141 (lt (poly-with-sugar-poly p)))
Note: See TracBrowser for help on using the repository browser.