source: CGBLisp/src/dynamics.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: 9.8 KB
Line 
1;;; -*- Mode: Lisp; Syntax: Common-Lisp; Package: Grobner; Base: 10 -*-
2#|
3 $Id: dynamics.lisp,v 1.7 2009/01/23 10:49:32 marek Exp $
4 *--------------------------------------------------------------------------*
5 | Copyright (C) 1994, Marek Rychlik (e-mail: rychlik@math.arizona.edu) |
6 | Department of Mathematics, University of Arizona, Tucson, AZ 85721 |
7 | |
8 | Everyone is permitted to copy, distribute and modify the code in this |
9 | directory, as long as this copyright note is preserved verbatim. |
10 *--------------------------------------------------------------------------*
11|#
12
13(defpackage "DYNAMICS"
14 (:use "ORDER" "MONOM" "COEFFICIENT-RING" "GROBNER" "MAKELIST" "PRINTER" "TERM" "POLY" "COMMON-LISP")
15 (:export poly-composition poly-scalar-composition poly-dynamic-power
16 poly-evaluate poly-scalar-evaluate factorial
17 poly-scalar-diff poly-diff standard-vector
18 scalar-partial partial drop-elt drop-row drop-column
19 determinant minor matrix- poly-list-
20 characteristic-combination
21 characteristic-matrix
22 characteristic-polynomial
23 identity-matrix
24 print-matrix
25 jacobi-matrix
26 jacobian))
27
28(in-package "DYNAMICS")
29
30#+debug(proclaim '(optimize (speed 0) (debug 3)))
31#-debug(proclaim '(optimize (speed 3) (debug 0)))
32
33(defun poly-scalar-composition (f G &optional (order #'lex>))
34 "Returns a polynomial obtained by substituting a list of polynomials G=(G1,G2,...,GN)
35 into a polynomial F(X1,X2,...,XN). All polynomials are assumed to be in the internal form,
36so variables do not explicitly apprear in the calculation."
37 (reduce #'(lambda (x y) (poly+ x y order))
38 (mapcar #'(lambda (x)
39 (scalar-times-poly
40 (cdr x)
41 (poly-mexpt G (car x) order)))
42 f)))
43
44(defun poly-composition (F G &optional (order #'lex>))
45 "Return the composition of a polynomial map F with a polynomial map
46G. Both maps are represented as lists of polynomials, and each polynomial
47is in the internal alist representation. The restriction is that the length
48of the list G must be the number of variables in the list F."
49 (mapcar #'(lambda (x) (poly-scalar-composition x G order)) F))
50
51
52(defun poly-dynamic-power (F n &optional (order #'lex>))
53 "Calculate the composition FoFo...oF (n times), where
54F is a polynomial map represented as a list of polynomials."
55 (reduce #'(lambda (x y) (poly-composition x y order))
56 (make-list n :initial-element F)))
57
58(defun poly-scalar-evaluate (f x &optional (order #'lex>))
59 "Evaluate a polynomial F at a point X. This operation is implemented
60through POLY-SCALAR-COMPOSITION."
61 (if (endp f)
62 0
63 (let ((p (poly-scalar-composition
64 f
65 (mapcar #'(lambda (u)
66 (if (zerop u)
67 nil
68 (list (cons (make-list (length (caar f)) :initial-element 0)
69 u))))
70 x)
71 order)))
72 (if (endp p) 0
73 (cdar p)))))
74
75(defun poly-evaluate (F x &optional (order #'lex>))
76 "Evaluate a polynomial map F, represented as list of polynomials, at a point X."
77 (mapcar #'(lambda (h) (poly-scalar-evaluate h x order)) F))
78
79(defun factorial (n &optional (k n) &aux (result 1))
80 "Return N!/(N-K)!=N(N-1)(N-K+1)."
81 (dotimes (j k result)
82 (setf result (* result (- n j)))))
83
84(defun poly-scalar-diff (f m)
85 "Return the partial derivative of a polynomial F over multiple
86 variables according to multiindex M."
87 (setf f (remove-if #'(lambda (x) (some #'< (car x) m)) f))
88 (mapcar #'(lambda (x) (cons (monom/ (car x) m)
89 (* (cdr x) (apply #'* (mapcar #'factorial (car x) m)))))
90 f))
91
92(defun poly-diff (F m)
93 "Return the partial derivative of a polynomial map F, represented as a list of polynomials,
94with respect to several variables, according to multi-index M."
95 (mapcar #'(lambda (h) (poly-scalar-diff h m)) F))
96
97
98(defun scalar-partial (f k &optional (l 1))
99 "Returns the L-th partial derivative of a polynomial F over the K-th variable."
100 (when f
101 (poly-scalar-diff f (standard-vector (length (caar f)) k l))))
102
103(defun partial (F k &optional (l 1))
104 "Returns the L-th partial derivative over the K-th variable, of a polynomial
105map F, represented as a list of polynomials."
106 (when (and F (car F) (caar F))
107 (poly-diff F (standard-vector (length (caaar F)) k l))))
108
109
110(defun determinant (F &optional (order #'lex>) &aux (result nil))
111 "Returns the determinant of a polynomial matrix F, which is a list of
112rows of the matrix, each row is a list of polynomials. The algorithm
113is recursive expansion along columns."
114 (cond
115 ((= (length F) 1) (setf result (caar F)))
116 (t
117 (dotimes (i (length F) result)
118 (setf result (poly+ result
119 (scalar-times-poly
120 (expt -1 i)
121 (poly* (car (elt F i)) (minor F i 0 order) order))
122 order))))))
123
124
125(defun minor (F i j &optional (order #'lex>))
126 "Calculate the minor of a polynomial matrix F with respect to entry (I,J)."
127 (determinant (drop-row (drop-column F j) i) order))
128
129(defun drop-row (F i)
130 "Discards the I-th row from a polynomial matrix F."
131 (drop-elt F i))
132
133(defun drop-column (F j)
134 "Discards the J-th column from a polynomial matrix F."
135 (mapcar #'(lambda (x) (drop-elt x j)) F))
136
137(defun drop-elt (lst j)
138 "Discards the J-th element from a list LST."
139 (append (subseq lst 0 j) (subseq lst (1+ j))))
140
141;; Matrix operations
142
143(defun matrix- (F G &optional (order #'lex>))
144 "Returns difference of two polynomial matrices F and G."
145 (mapcar #'(lambda (x y) (poly-list- x y order)) F G))
146
147(defun scalar-times-matrix (s F)
148 "Returns a product of a polynomial S by a polynomial matrix F."
149 (mapcar #'(lambda (x) (scalar-times-poly-list s x)) F))
150
151(defun monom-times-matrix (m F)
152 "Returns a product of a monomial M by a polynomial matrix F."
153 (mapcar #'(lambda (x) (monom-times-poly-list m x)) F))
154
155(defun term-times-matrix (term F)
156 "Returns a product of a term TERM by a polynomial matrix F."
157 (mapcar #'(lambda (x) (term-times-poly-list term x)) F))
158
159
160;; Polynomial list operations
161(defun poly-list- (F G &optional (order #'lex>))
162 "Returns the list of differences of two lists of polynomials
163F and G (polynomial maps)."
164 (mapcar #'(lambda (x y) (poly- x y order)) F G))
165
166(defun scalar-times-poly-list (s F)
167 "Returns the list of products of a polynomial S by the
168list of polynomials F."
169 (mapcar #'(lambda (x) (scalar-times-poly s x)) F))
170
171(defun monom-times-poly-list (m f)
172 "Returns the list of products of a monomial M by the
173list of polynomials F."
174 (mapcar #'(lambda (x) (monom-times-poly m x)) F))
175
176(defun term-times-poly-list (term f)
177 "Returns the list of products of a term TERM by the
178list of polynomials F."
179 (mapcar #'(lambda (x) (term-times-poly term x)) F))
180
181;; Generalized Characteristic polynomial operations
182;; det(A - u1*B1-u2*B2-...-um*Bm)
183
184(defun characteristic-combination (A B &optional (order #'lex>)
185 &aux (n (length B)))
186 "Returns A - U1 * B1 - U2 * B2 - ... - UM * BM where A is a polynomial
187and B=(B1,B2,...,BM) is a polynomial list, where U1, U2, ... , UM are
188new variables. These variables will be added to every polynomial
189A and BI as the last M variables."
190 (setf A (poly-extend-end A (make-list n :initial-element 0)))
191 (dotimes (i (length B) A)
192 (setf A (poly- A (poly-extend-end (elt B i) (standard-vector n i)) order))))
193
194;; A is a list of polynomials; B is a list of lists of polynomials
195(defun characteristic-combination-poly-list (A B &optional (order #'lex>))
196 "Returns A - U1 * B1 - U2 * B2 - ... - UM * BM where A is a polynomial list
197and B=(B1, B2, ... , BM) is a list of polynomial lists, where U1, U2, ... ,UM are
198new variables. These variables will be added to every polynomial
199A and BI as the last M variables. Se also CHARACTERISTIC-COMBINATION."
200 (apply #'mapcar
201 (cons #'(lambda (&rest x)
202 (funcall #'characteristic-combination (car x) (rest x) order))
203 (cons A B))))
204
205;; Finally, the case of matrices
206(defun characteristic-matrix (A &optional
207 (order #'lex>)
208 (B (list (identity-matrix (length A) (length (caaaar A))))))
209 "Returns A - U1*B1 - U2*B2 - ... - UM * BM where A is a polynomial matrix
210and B=(B1,B2,...,BM) is a list of polynomial matrices, where U1, U2, .., UM are
211new variables. These variables will be added to every polynomial
212A and BI as the last M variables. Se also CHARACTERISTIC-COMBINATION."
213 (apply #'mapcar
214 (cons #'(lambda (&rest x)
215 (funcall #'characteristic-combination-poly-list (car x) (rest x) order))
216 (cons A B))))
217
218;; Characteristic polynomial
219(defun characteristic-polynomial (A &optional
220 (order #'lex>)
221 (B (list (identity-matrix (length A) (length (caaaar A))))))
222 "Returns the generalized characteristic polynomial, i.e. the determinant
223DET(A - U1 * B1 - U2 * B2 - ... - UM * BM), where A and BI are square polynomial matrices in
224N variables. The resulting polynomial will have N+M variables, with U1, U2, ..., UM added
225as the last M variables."
226 (determinant (characteristic-matrix A order B) order))
227
228(defun identity-matrix (dim nvars)
229 "Return the polynomial matrix which is the identity matrix. DIM is the requested
230dimension and NVARS is the number of variables of each entry."
231 (labels ((zero-monom () (make-list nvars :initial-element 0))
232 (entry (i j) (if (= i j)
233 (list (cons (zero-monom) 1))
234 nil)))
235 (makelist (makelist (entry i j) (i 1 dim)) (j 1 dim))))
236
237(defun print-matrix (F vars)
238 "Prints a polynomial matrix F, using a list of symbols VARS as variable names."
239 (mapcar #'(lambda (x) (poly-print (cons '[ x) vars) (terpri)) F)
240 F)
241
242(defun jacobi-matrix (F &optional (m (length F)) (n (length (caaaar F))))
243 "Returns the Jacobi matrix of a polynomial list F over the first N variables."
244 (makelist (makelist (scalar-partial (elt F i) j)
245 (j 0 (1- n)))
246 (i 0 (1- m))))
247
248(defun jacobian (F &optional (order #'lex>) (m (length F)) (n (length (caaaar F))))
249 "Returns the Jacobian (determinant) of a polynomial list F over the first N variables."
250 (determinant (jacobi-matrix F m n) order))
Note: See TracBrowser for help on using the repository browser.