1 ;;; -*- Mode:Lisp; Package:CL-MAXIMA; Base:10 -*-
4 (defun $complexity
(v &optional sum
)
5 (cond ((eql v
0)(if sum -
1 '$z
))
9 sum
($complexity u sum
)))
10 (t (cons (car v
) (mapcar '$complexity
(cdr v
))))))
11 (t (gen-pcomplexity (st-rat v
)))))
13 (defun complexity-difference1 (x y
)
14 (cond ((eq x
'$z
) (setq x -
1)))
15 (cond ((eq y
'$z
) (setq y -
1)))
19 (loop for v in
(cdr x
) for w in
(cdr y
)
20 sum
(complexity-difference1 v w
)))))
23 (defun $complexity_difference
(a-list v
)
25 ((let ((x ($complexity
($psublis a-list v
)))
27 (complexity-difference1 y x
)))))
33 (loop for w in
(cdr v
) collect
($pdegree w va
))))
34 (t (pdegree (st-rat v
) (car (st-rat va
))))))
37 (defun $delete_matrix_row
(mat i
)
39 (loop for v in
(cdr mat
) for j from
1
40 unless
(eql j i
) collect v
)))
42 (defun $complexity_less_p
(x y
) (< ($complexity x t
) ($complexity y t
)))
46 (cons (car x
) (mapcar '$my_rat
(cdr x
))))
47 (t (header-poly (st-rat x
)))))
49 (defun $mat_entry
(mat i j
)
52 (defvar $pivot_list
'((mlist)))
54 (defun $eliminate_pivot
(mat i j
)
55 (setq mat
($my_rat mat
))
56 (let ((pivot ($mat_entry mat i j
))
57 (pivot-row (nth i mat
)))
58 (format t
"~%Using pivot:")
60 (setq $pivot_list
($append $pivot_list
`((mlist) ,(header-poly pivot
) ,i
,j
)))
62 (loop for v in
(cdr mat
) for ii from
1
65 (let* ((this (st-rat (nth j v
))))
66 (cond ((eql 0 this
) v
)
67 (t (let ((quot (nred this pivot
)))
69 (loop for u in
(cdr v
)
70 for a in
(cdr pivot-row
)
73 (n- (n* (function-numerator quot
) a
)
74 (n* (function-denominator quot
) u
)))))))))))))
77 (defun $psublis
(a-list poly
&optional
(denom 1) palist
)
78 "use psublis([y=x^2,v=u^3],denom,poly)"
81 (loop for
(u v repl
) in
(cdr a-list
) by
'cdr
82 do
(check-arg u
(eq (car u
) 'mequal
) "Type a=repl")
84 (cons (p-var (st-rat v
))
86 (cond ((mbagp poly
) (cons (car poly
)
87 (loop for v in
(cdr poly
) collect
88 ($psublis a-list v denom palist
))))
89 (t (header-poly(psublis palist
(st-rat denom
) (st-rat poly
))))))
92 (defun $eliminate_pivot_list_hack
(matrix pivot-list
)
93 (loop for
(piv i j
) on
(cdr pivot-list
) by
'cdddr
95 (format t
"~%Pivot in list was :") (displa ($matrix_entry matrix i j
))
96 (setq matrix
($eliminate_pivot matrix i j
))
97 finally
(return matrix
)))
100 (defun $get_relation_matrix
(relations mons
)
101 (setq mons
(st-rat mons
))
103 (loop for v in
(cdr relations
)
104 do
(setq v
(st-rat v
))
108 collect
(header-poly (pcoeff v w
)))))))
110 (defun $switch_rows
(mat i j
)
111 (let ((nmat (copy-list mat
)))
112 (setf (nth i nmat
) (nth j mat
))
113 (setf (nth j nmat
) (nth i mat
))
116 (defun number_zeros(a)
117 (loop for v in a when
(eql v
'$z
) count t
))
119 (defun $row_less
(a b
) (> (number_zeros a
) (number_zeros b
)))
121 (defun $row_sort
(mat pred
)
122 (cons '($matrix
) (sort (copy-list (cdr mat
)) pred
)))
124 (defun $reorder_matrix
(mat &aux rows
)
126 (loop for u in
(cdr mat
)
127 collect
(cons (loop for v in
(cdr u
) sum
(gen-pcomplexity (st-rat v
))) u
)))
128 (setq rows
(sort rows
#'< :key
#'car
))
129 (cons '($matrix
) (mapcar 'cdr rows
)))
131 (defun $best_row
(mat &aux tem at
)
132 (loop for u in
(cdr mat
) for i from
1
133 minimize
(setq tem
(loop for v in
(cdr u
) sum
(gen-pcomplexity (st-rat v
)))) into the-min
134 when
(eql tem the-min
)
136 finally
(return at
)))
139 (defun $best_piv
(row &aux tem at
)
140 (loop for v in
(cdr row
) for i from
1
141 unless
(eql (setq v
(st-rat v
)) 0)
142 minimize
(setq tem
(gen-pcomplexity (st-rat v
))) into the-min
144 when
(eql tem the-min
) do
(setq at i
)
145 finally
(return at
)))
147 (defvar $current nil
)
150 (defun $invertible_pivots
(mat g
&aux pivs
)
152 (loop for ro in
(cdr ($transpose mat
)) for i from
1
154 (loop for u in
(cdr ro
) for j from
1
155 do
(setq u
(st-rat u
))
156 when
(may-invertp u g
)
157 collect
(list '(mlist) (header-poly u
) j i
($complexity ro t
) (pcomplexity u
)))
159 finally
(setq pivs
(sort all
'< :key
#'(lambda (x) (nth 4 x
))))
160 (return (cons '(mlist) pivs
))))
162 ;(defun $invertible_pivots(mat g &aux pivs)
163 ; (setq g (st-rat g))
164 ; (loop for ro in (cdr mat) for i from 1
166 ; (loop for u in (cdr ro) for j from 1
167 ; do (setq u (st-rat u))
168 ; when (may-invertp u g)
169 ; collect (list '(mlist) (header-poly u) i j ($complexity ro t) (pcomplexity u)))
171 ; finally (setq pivs (sort all '< :key '(lambda (x) (nth 4 x))))
172 ; (return (cons '(mlist) pivs))))
174 (defun $reduce_by_complexity
(&optional mat
)
175 (setq $current
(or mat $current
))
176 (let* ((i ($best_row mat
))
177 (j ($best_piv
(nth i mat
))))
179 (setq $current
($eliminate_pivot mat i j
))))
182 (defun $sort_complexity
(list)
183 (loop for v in
(cdr list
)
184 collect
(cons (pcomplexity (st-rat v
)) v
) into all
185 finally
(setq all
(sort all
'< :key
'car
))
186 (return (cons (car list
) (mapcar 'cdr all
)))))
188 (defvar $gcds_used
'((mlist)))
190 (defun $p_projective
(lis &optional agcd
&aux
(zero-row t
))
191 "Cancel a gcd of AGCD and The rest of the elements of LIS. If LIS is a
192 list of lists call this function on each of the lists independently."
194 (and agcd
(setq agcd
(st-rat agcd
)))
195 (cond ((mbagp (second lis
))
197 (loop for w in
(cdr lis
)
198 collect
($p_projective w agcd
))))
200 (loop for v in
(cdr lis
)
201 do
(setq v
(st-rat v
))
202 (assert (affine-polynomialp v
))
203 (cond ((and zero-row
(not (pzerop v
)) (setq zero-row nil
))))
204 (setq agcd
(cond (agcd (pgcd agcd v
))
207 (cond ((numberp agcd
) lis
)
211 (setq $gcds_used
($append $gcds_used
`((mlist) ,(header-poly agcd
))))
212 (format t
"~%Found a non trivial factor:") (sh agcd
)
214 (loop for vv in
(cdr lis
)
215 collect
(header-poly (pquotient (st-rat vv
) agcd
)))))))))))
219 (defun $cancel_pivot
(lis piv
)
220 (setq piv
(st-rat piv
))
221 (assert (affine-polynomialp piv
))
223 (cons (car lis
) (loop for w in
(cdr lis
) collect
($cancel_pivot w piv
))))
225 (setq lis
(st-rat lis
))
226 (assert (affine-polynomialp lis
))
227 (let ((tem (pgcd lis piv
)))
228 (cond ((numberp tem
) (header-poly lis
))
229 (t (header-poly (pquotient lis tem
))))))))
231 (defun $linearize_nc
(x)
233 (cond ((and (consp (car x
)) (eq (caar x
) 'mnctimes
))
234 (setf (car x
) '(mtimes))
235 (loop for v on
(cdr x
)
238 do
(setf (car v
) (intern (format nil
"~a~d" term i
)))))
239 (t (cons ($linearize_nc
(car x
)) ($linearize_nc
(cdr x
))))))
242 (defun $linearize_nc_to_nc
(x)
244 (cond ((and (consp (car x
)) (eq (caar x
) 'mnctimes
))
245 (loop for v on
(cdr x
)
248 do
(setf (car v
) (intern (format nil
"~a~d" term i
)))))
249 (t (cons ($linearize_nc_to_nc
(car x
)) ($linearize_nc_to_nc
(cdr x
)))))
259 (round ($binomial
(+ n
3) 3)))
261 (push 'hilbert_4
*all-rank-functions
*)
263 ;(loop for i below 10 collect (list i (hilbert_tem i)))
265 (defun $minors2_2
(mat cols
)
266 (let ((nmat (apply '$submatrix mat
(cdr cols
))))
268 (loop for ro in
(list-tableaux 2 (1- (length nmat
)))
270 collect
(cons '($matrix
) (loop for i in ro
271 collect
(nth i nmat
)))))))
273 (defun $minors
(n mat cols
)
274 (let ((nmat (apply '$submatrix mat
(cdr cols
))))
276 (loop for ro in
(list-tableaux n
(1- (length nmat
)))
278 collect
(cons '($matrix
) (loop for i in ro
279 collect
(nth i nmat
)))))))
281 (loop for i below
5 collect i
)
286 (defun sublist (l pred
)
287 (loop for v in l when
(apply pred v
) collect v
))
290 (cons '(mlist) (loop for w in l collect
(cons '(mlist) w
))))
292 ;(setq $lis1 (lis (sublist (cartesian-product he he he he) #'(lambda ( i j l m) (and (< i j) (<= m l))))))
294 ;(setq $lis2 (lis (sublist (cartesian-product he he he he) #'(lambda ( i j l m) (and (<= i j) (< m l))))))
296 ;; POLYS is a list of polynomials and a parallel list DEGS of degrees.
297 ;; We compute all monomials in POLYS of deg l
298 (defun monoms (polys degs
&optional
(cross '(nil)))
299 (cond ((null polys
) '(1))
301 (loop for v in
(monoms (cdr polys
) (cdr degs
))
303 (loop for j to
(car degs
)
304 collect
(n* (pexpt (car polys
) j
) v
))))))
306 (defun $extract_c_equations
(poly vars-to-exclude
)
307 (let ((monoms (st-rat vars-to-exclude
))
308 (vars1 (mapcar 'car
(st-rat vars-to-exclude
))))
309 (assert ($listp poly
))
310 (let (ans all-monoms some-monoms
)
312 (loop for v in
(cdr poly
)
313 do
(setq v
(function-numerator (st-rat v
)))
314 (setq degs
(loop for w in vars1
315 collect
(pdegree v w
)))
316 (setq some-monoms
(monoms monoms degs
))
317 (setq all-monoms
(append all-monoms some-monoms
))
319 (loop for w in some-monoms
320 collect
(new-disrep (pcoeff v w vars1
)))))
323 (cons '(mlist) (mapcar 'new-disrep all-monoms
))))))
326 (save-linenumbers :file
"/tmp/lines"))