head 1.11; access; symbols; locks; strict; comment @;;; @; 1.11 date 2009.01.22.04.05.13; author marek; state Exp; branches; next 1.10; 1.10 date 2009.01.21.23.37.07; author marek; state Exp; branches; next 1.9; 1.9 date 2009.01.21.23.36.04; author marek; state Exp; branches; next 1.8; 1.8 date 2009.01.21.23.24.21; author marek; state Exp; branches; next 1.7; 1.7 date 2009.01.21.19.40.18; author marek; state Exp; branches; next 1.6; 1.6 date 2009.01.21.19.38.54; author marek; state Exp; branches; next 1.5; 1.5 date 2009.01.21.19.36.16; author marek; state Exp; branches; next 1.4; 1.4 date 2009.01.21.07.20.43; author marek; state Exp; branches; next 1.3; 1.3 date 2009.01.19.09.28.06; author marek; state Exp; branches; next 1.2; 1.2 date 2009.01.19.07.42.23; author marek; state Exp; branches; next 1.1; 1.1 date 2009.01.19.07.36.08; author marek; state Exp; branches; next ; desc @@ 1.11 log @*** empty log message *** @ text @;;; -*- Mode: Lisp; Syntax: Common-Lisp; Package: Grobner; Base: 10 -*- #| $Id$ *--------------------------------------------------------------------------* | Copyright (C) 1994, Marek Rychlik (e-mail: rychlik@@math.arizona.edu) | | Department of Mathematics, University of Arizona, Tucson, AZ 85721 | | | | Everyone is permitted to copy, distribute and modify the code in this | | directory, as long as this copyright note is preserved verbatim. | *--------------------------------------------------------------------------* |# (defpackage "PARSE" (:export parse parse-to-alist parse-string-to-alist parse-to-sorted-alist parse-string-to-sorted-alist ^ [ poly-eval) (:use "ORDER" "POLY" "COEFFICIENT-RING" "COMMON-LISP") (:shadow sort-poly)) (in-package "PARSE") #+debug(proclaim '(optimize (speed 0) (debug 3))) #-debug(proclaim '(optimize (speed 3) (debug 0))) ;; The function PARSE yields the representations as above. The two functions ;; PARSE-TO-ALIST and PARSE-STRING-TO-ALIST parse polynomials to the alist ;; representations. For example ;; ;; >(parse)x^2-y^2+(-4/3)*u^2*w^3-5 ---> ;; (+ (* 1 (^ X 2)) (* -1 (^ Y 2)) (* -4/3 (^ U 2) (^ W 3)) (* -5)) ;; ;; >(parse-to-alist '(x y u w))x^2-y^2+(-4/3)*u^2*w^3-5 ---> ;; (((0 0 2 3) . -4/3) ((0 2 0 0) . -1) ((2 0 0 0) . 1) ((0 0 0 0) . -5)) ;; ;; >(parse-string-to-alist "x^2-y^2+(-4/3)*u^2*w^3-5" '(x y u w)) ---> ;; (((0 0 2 3) . -4/3) ((0 2 0 0) . -1) ((2 0 0 0) . 1) ((0 0 0 0) . -5)) ;; ;; >(parse-string-to-alist "[x^2-y^2+(-4/3)*u^2*w^3-5,y]" '(x y u w)) ;; ([ (((0 0 2 3) . -4/3) ((0 2 0 0) . -1) ((2 0 0 0) . 1) ;; ((0 0 0 0) . -5)) ;; (((0 1 0 0) . 1))) ;; The functions PARSE-TO-SORTED-ALIST and PARSE-STRING-TO-SORTED-ALIST ;; in addition sort terms by the predicate defined in the ORDER package ;; For instance: ;; >(parse-to-sorted-alist '(x y u w))x^2-y^2+(-4/3)*u^2*w^3-5 ;; (((2 0 0 0) . 1) ((0 2 0 0) . -1) ((0 0 2 3) . -4/3) ((0 0 0 0) . -5)) ;; >(parse-to-sorted-alist '(x y u w) t #'grlex>)x^2-y^2+(-4/3)*u^2*w^3-5 ;; (((0 0 2 3) . -4/3) ((2 0 0 0) . 1) ((0 2 0 0) . -1) ((0 0 0 0) . -5)) ;;(eval-when (compile) ;; (proclaim '(optimize safety))) (defun convert-number (number-or-poly n) "Returns NUMBER-OR-POLY, if it is a polynomial. If it is a number, it converts it to the constant monomial in N variables. If the result is a number then convert it to a polynomial in N variables." (if (numberp number-or-poly) (list (cons (make-list n :initial-element 0) number-or-poly)) number-or-poly)) (defun $poly+ (p q n order ring) "Add two polynomials P and Q, where each polynomial is either a numeric constant or a polynomial in internal representation. If the result is a number then convert it to a polynomial in N variables." (poly+ (convert-number p n) (convert-number q n) order ring)) (defun $poly- (p q n order ring) "Subtract two polynomials P and Q, where each polynomial is either a numeric constant or a polynomial in internal representation. If the result is a number then convert it to a polynomial in N variables." (poly- (convert-number p n) (convert-number q n) order ring)) (defun $minus-poly (p n ring) "Negation of P is a polynomial is either a numeric constant or a polynomial in internal representation. If the result is a number then convert it to a polynomial in N variables." (minus-poly (convert-number p n) ring)) (defun $poly* (p q n order ring) "Multiply two polynomials P and Q, where each polynomial is either a numeric constant or a polynomial in internal representation. If the result is a number then convert it to a polynomial in N variables." (poly* (convert-number p n) (convert-number q n) order ring)) (defun $poly/ (p q ring) "Divide a polynomials P which is either a numeric constant or a polynomial in internal representation, by a number Q." (if (numberp p) (common-lisp:/ p q) (scalar-times-poly (common-lisp:/ q) p ring))) (defun $poly-expt (p l n order ring) "Raise polynomial P, which is a polynomial in internal representation or a numeric constant, to power L. If P is a number, convert the result to a polynomial in N variables." (poly-expt (convert-number p n) l order ring)) (defun parse (&optional stream) "Parser of infis expressions with integer/rational coefficients The parser will recognize two kinds of polynomial expressions: - polynomials in fully expanded forms with coefficients written in front of symbolic expressions; constants can be optionally enclosed in (); for example, the infix form X^2-Y^2+(-4/3)*U^2*W^3-5 parses to (+ (- (EXPT X 2) (EXPT Y 2)) (* (- (/ 4 3)) (EXPT U 2) (EXPT W 3)) (- 5)) - lists of polynomials; for example [X-Y, X^2+3*Z] parses to (:[ (- X Y) (+ (EXPT X 2) (* 3 Z))) where the first symbol [ marks a list of polynomials. -other infix expressions, for example [(X-Y)*(X+Y)/Z,(X+1)^2] parses to: (:[ (/ (* (- X Y) (+ X Y)) Z) (EXPT (+ X 1) 2)) Currently this function is implemented using M. Kantrowitz's INFIX package." (read-from-string (concatenate 'string "#I(" (with-output-to-string (s) (loop (multiple-value-bind (line eof) (read-line stream t) (format s "~A" line) (when eof (return))))) ")"))) ;; Translate output from parse to a pure list form ;; assuming variables are VARS (defun alist-form (plist vars) "Translates an expression PLIST, which should be a list of polynomials in variables VARS, to an alist representation of a polynomial. It returns the alist. See also PARSE-TO-ALIST." (cond ((endp plist) nil) ((eql (first plist) '[) (cons '[ (mapcar #'(lambda (x) (alist-form x vars)) (rest plist)))) (t (assert (eql (car plist) '+)) (alist-form-1 (rest plist) vars)))) (defun alist-form-1 (p vars &aux (ht (make-hash-table :test #'equal :size 16)) stack) (dolist (term p) (assert (eql (car term) '*)) (incf (gethash (powers (cddr term) vars) ht 0) (second term))) (maphash #'(lambda (key value) (unless (zerop value) (push (cons key value) stack))) ht) stack) (defun powers (monom vars &aux (tab (pairlis vars (make-list (length vars) :initial-element 0)))) (dolist (e monom (mapcar #'(lambda (v) (cdr (assoc v tab))) vars)) (assert (equal (first e) '^)) (assert (integerp (third e))) (assert (= (length e) 3)) (let ((x (assoc (second e) tab))) (if (null x) (error "Variable ~a not in the list of variables." (second e)) (incf (cdr x) (third e)))))) ;; New implementation based on the INFIX package of Mark Kantorowitz (defun parse-to-alist (vars &optional stream) "Parse an expression already in prefix form to an association list form according to the internal CGBlisp polynomial syntax: a polynomial is an alist of pairs (MONOM . COEFFICIENT). For example: (WITH-INPUT-FROM-STRING (S \"X^2-Y^2+(-4/3)*U^2*W^3-5\") (PARSE-TO-ALIST '(X Y U W) S)) evaluates to (((0 0 2 3) . -4/3) ((0 2 0 0) . -1) ((2 0 0 0) . 1) ((0 0 0 0) . -5))" (poly-eval (parse stream) vars)) (defun parse-string-to-alist (str vars) "Parse string STR and return a polynomial as a sorted association list of pairs (MONOM . COEFFICIENT). For example: (parse-string-to-alist \"[x^2-y^2+(-4/3)*u^2*w^3-5,y]\" '(x y u w)) ([ (((0 0 2 3) . -4/3) ((0 2 0 0) . -1) ((2 0 0 0) . 1) ((0 0 0 0) . -5)) (((0 1 0 0) . 1))) The functions PARSE-TO-SORTED-ALIST and PARSE-STRING-TO-SORTED-ALIST sort terms by the predicate defined in the ORDER package." (with-input-from-string (stream str) (parse-to-alist vars stream))) (defun parse-to-sorted-alist (vars &optional (order #'lex>) (stream t)) "Parses streasm STREAM and returns a polynomial represented as a sorted alist. For example: (WITH-INPUT-FROM-STRING (S \"X^2-Y^2+(-4/3)*U^2*W^3-5\") (PARSE-TO-SORTED-ALIST '(X Y U W) S)) returns (((2 0 0 0) . 1) ((0 2 0 0) . -1) ((0 0 2 3) . -4/3) ((0 0 0 0) . -5)) and (WITH-INPUT-FROM-STRING (S \"X^2-Y^2+(-4/3)*U^2*W^3-5\") (PARSE-TO-SORTED-ALIST '(X Y U W) T #'GRLEX>) S) returns (((0 0 2 3) . -4/3) ((2 0 0 0) . 1) ((0 2 0 0) . -1) ((0 0 0 0) . -5))" (sort-poly (parse-to-alist vars stream) order)) (defun parse-string-to-sorted-alist (str vars &optional (order #'lex>)) "Parse a string to a sorted alist form, the internal representation of polynomials used by our system." (with-input-from-string (stream str) (parse-to-sorted-alist vars order stream))) (defun sort-poly-1 (p order) "Sort the terms of a single polynomial P using an admissible monomial order ORDER. Returns the sorted polynomial. Destructively modifies P." (sort p order :key #'first)) ;; Sort a polynomial or polynomial list (defun sort-poly (poly-or-poly-list &optional (order #'lex>)) "Sort POLY-OR-POLY-LIST, which could be either a single polynomial or a list of polynomials in internal alist representation, using admissible monomial order ORDER. Each polynomial is sorted using SORT-POLY-1." (cond ((eql poly-or-poly-list :syntax-error) nil) ((null poly-or-poly-list) nil) ((eql (car poly-or-poly-list) '[) (cons '[ (mapcar #'(lambda (p) (sort-poly-1 p order)) (rest poly-or-poly-list)))) (t (sort-poly-1 poly-or-poly-list order)))) (defun poly-eval-1 (expr vars order ring &aux (n (length vars))) "Evaluate an expression EXPR as polynomial by substituting operators + - * expt with corresponding polynomial operators and variables VARS with monomials (1 0 ... 0), (0 1 ... 0) etc. We use special versions of binary operators $poly+, $poly-, $minus-poly, $poly* and $poly-expt which work like the corresponding functions in the POLY package, but accept scalars as arguments as well." (eval (sublis (pairlis '(+ - * / expt) `((lambda (&rest r) (reduce #'(lambda (p q) ($poly+ p q ,n ,order ,ring)) r)) (lambda (p &rest r) (if (endp r) ($minus-poly p ,n ,ring) ($poly- p (reduce #'(lambda (p q) ($poly+ p q ,n ,order ,ring)) r) ,n ,order ,ring))) (lambda (&rest r) (reduce #'(lambda (p q) ($poly* p q ,n ,order ,ring)) r)) (lambda (p q) ($poly/ p q ,ring)) (lambda (p l) ($poly-expt p l ,n ,order ,ring)))) (sublis (pairlis vars (monom-basis (length vars))) expr)))) (defun poly-eval (expr vars &optional (order #'lex>) (ring *coefficient-ring*)) "Evaluate an expression EXPR, which should be a polynomial expression or a list of polynomial expressions (a list of expressions marked by prepending keyword :[ to it) given in lisp prefix notation, in variables VARS, which should be a list of symbols. The result of the evaluation is a polynomial or a list of polynomials (marked by prepending symbol '[) in the internal alist form. This evaluator is used by the PARSE package to convert input from strings directly to internal form." (cond ((numberp expr) (list (cons (make-list (length vars) :initial-element 0) expr))) ((or (symbolp expr) (not (eq (car expr) :[))) (poly-eval-1 expr vars order ring)) (t (cons '[ (mapcar #'(lambda (p) (poly-eval-1 p vars order ring)) (rest expr)))))) ;; Return the standard basis of the monomials in n variables (defun monom-basis (n &aux (basis (copy-tree (make-list n :initial-element (list 'quote (list (cons (make-list n :initial-element 0) 1))))))) "Generate a list of monomials ((1 0 ... 0) (0 1 0 ... 0) ... (0 0 ... 1) which correspond to linear monomials X1, X2, ... XN." (dotimes (i n basis) (setf (elt (caaadr (elt basis i)) i) 1))) @ 1.10 log @*** empty log message *** @ text @d21 2 a22 2 ;;(proclaim '(optimize (speed 0) (debug 3))) (proclaim '(optimize (speed 3) (debug 0))) @ 1.9 log @*** empty log message *** @ text @d3 1 a3 1 $Id: parse.lisp,v 1.8 2009/01/21 23:24:21 marek Exp marek $ d248 1 a248 1 ($poly- p (reduce #'(lambda (p q) ($poly+ p q n ,order ,ring)) r) ,n d252 1 a252 1 (lambda (p l) ($poly-expt p l n ,order ,ring)))) @ 1.8 log @*** empty log message *** @ text @d3 1 a3 1 $Id$ d245 8 a252 8 (list #'(lambda (&rest r) (reduce #'(lambda (p q) ($poly+ p q n order ring)) r)) #'(lambda (p &rest r) (if (endp r) ($minus-poly p n ring) ($poly- p (reduce #'(lambda (p q) ($poly+ p q n order ring)) r) n order ring))) #'(lambda (&rest r) (reduce #'(lambda (p q) ($poly* p q n order ring)) r)) #'(lambda (p q) ($poly/ p q ring)) #'(lambda (p l) ($poly-expt p l n order ring)))) @ 1.7 log @*** empty log message *** @ text @d17 1 a17 1 (:shadow sort-poly + - * / expt)) @ 1.6 log @*** empty log message *** @ text @d242 14 a255 13 (sublis (pairlis vars (monom-basis (length vars))) (labels ((+ (&rest r) (reduce #'(lambda (p q) ($poly+ p q n order ring)) r)) (- (p &rest r) (if (endp r) ($minus-poly p n ring) ($poly- p (reduce #'(lambda (p q) ($poly+ p q n order ring)) r) n order ring))) (* (&rest r) (reduce #'(lambda (p q) ($poly* p q n order ring)) r)) (/ (p q) ($poly/ p q ring)) (expt (p l) ($poly-expt p l n order ring))) expr))) @ 1.5 log @*** empty log message *** @ text @d233 1 a233 4 (defmacro poly-eval-1 (expr vars order ring &aux (n (gensym)) (form (gensym))) d242 13 a254 17 `(let* ((,n (length ,vars)) (,form (sublis (pairlis ,vars (monom-basis ,n)) ,expr))) (labels ((+ (&rest r) (reduce #'(lambda (p q) ($poly+ p q ,n ,order ,ring)) r)) (- (p &rest r) (if (endp r) ($minus-poly p ,n ,ring) ($poly- p (reduce #'(lambda (p q) ($poly+ p q ,n ,order ,ring)) r) ,n ,order ,ring))) (* (&rest r) (reduce #'(lambda (p q) ($poly* p q ,n ,order ,ring)) r)) (/ (p q) ($poly/ p q ,ring)) (expt (p l) ($poly-expt p l ,n ,order ,ring))) ,form))) @ 1.4 log @*** empty log message *** @ text @d233 4 a236 1 (defun poly-eval-1 (expr vars order ring &aux (n (length vars))) d245 17 a261 13 (sublis (pairlis vars (monom-basis (length vars))) (labels ((+ (&rest r) (reduce #'(lambda (p q) ($poly+ p q n order ring)) r)) (- (p &rest r) (if (endp r) ($minus-poly p n ring) ($poly- p (reduce #'(lambda (p q) ($poly+ p q n order ring)) r) n order ring))) (* (&rest r) (reduce #'(lambda (p q) ($poly* p q n order ring)) r)) (/ (p q) ($poly/ p q ring)) (expt (p l) ($poly-expt p l n order ring))) expr))) @ 1.3 log @*** empty log message *** @ text @d17 1 a17 1 (:shadow sort-poly)) d88 2 a89 2 (/ p q) (scalar-times-poly (/ q) p ring))) d242 13 a254 14 (eval (sublis (pairlis '(+ - * / expt) `((lambda (&rest r) (reduce #'(lambda (p q) ($poly+ p q ,n ,order ,ring)) r)) (lambda (p &rest r) (if (endp r) ($minus-poly p ,n ,ring) ($poly- p (reduce #'(lambda (p q) ($poly+ p q ,n ,order ,ring)) r) ,n ,order ,ring))) (lambda (&rest r) (reduce #'(lambda (p q) ($poly* p q ,n ,order ,ring)) r)) (lambda (p q) ($poly/ p q ,ring)) (lambda (p l) ($poly-expt p l ,n ,order ,ring)))) (sublis (pairlis vars (monom-basis (length vars))) expr)))) @ 1.2 log @*** empty log message *** @ text @d21 2 a22 1 (proclaim '(optimize (speed 0) (debug 3))) d49 2 a50 2 (eval-when (compile) (proclaim '(optimize safety))) d245 8 a252 8 (list #'(lambda (&rest r) (reduce #'(lambda (p q) ($poly+ p q n order ring)) r)) #'(lambda (p &rest r) (if (endp r) ($minus-poly p n ring) ($poly- p (reduce #'(lambda (p q) ($poly+ p q n order ring)) r) n order ring))) #'(lambda (&rest r) (reduce #'(lambda (p q) ($poly* p q n order ring)) r)) #'(lambda (p q) ($poly/ p q ring)) #'(lambda (p l) ($poly-expt p l n order ring)))) @ 1.1 log @Initial revision @ text @d3 1 a3 1 $Id: parse.lisp,v 1.48 1997/12/25 02:18:21 marek Exp $ d21 1 @