[1] | 1 | #|
|
---|
| 2 | $Id: term.lisp,v 1.4 2009/01/22 04:08:39 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 |
|
---|
| 12 | (defpackage "TERM"
|
---|
| 13 | (:use "MONOM" "COEFFICIENT-RING" "COMMON-LISP")
|
---|
| 14 | (:export term-divides-p term* term/ term-coefficient term-monom monom-times-term))
|
---|
| 15 |
|
---|
| 16 | (in-package "TERM")
|
---|
| 17 |
|
---|
[8] | 18 | (proclaim '(optimize (speed 0) (debug 3)))
|
---|
[1] | 19 |
|
---|
| 20 | ;;----------------------------------------------------------------
|
---|
| 21 | ;; Basic operations on terms
|
---|
| 22 | ;;----------------------------------------------------------------
|
---|
| 23 | ;; THE REPRESENTATION: Terms are cons cells consisting
|
---|
| 24 | ;; of a monom and coefficient
|
---|
| 25 | ;;
|
---|
| 26 | ;; term: (monom . coefficient)
|
---|
| 27 | ;;
|
---|
| 28 | ;;----------------------------------------------------------------
|
---|
| 29 | ;; EXAMPLES: Suppose that variables are x and y. Then
|
---|
| 30 | ;; Term 3*x*y^2 ---> ((1 2) . 3)
|
---|
| 31 | ;;----------------------------------------------------------------
|
---|
| 32 |
|
---|
| 33 | ;; Multiplication of terms
|
---|
| 34 | (defun term* (term1 term2 &optional (ring *coefficient-ring*))
|
---|
| 35 | (cons
|
---|
| 36 | (monom* (car term1) (car term2))
|
---|
| 37 | (funcall (ring-* ring) (cdr term1) (cdr term2))))
|
---|
| 38 |
|
---|
| 39 | ;;;; Division of terms
|
---|
| 40 | (defun term/ (term1 term2 &optional (ring *coefficient-ring*))
|
---|
| 41 | (cons
|
---|
| 42 | (monom/ (car term1) (car term2))
|
---|
| 43 | (funcall (ring-/ ring) (cdr term1) (cdr term2))))
|
---|
| 44 |
|
---|
| 45 | ;; Multiply monomial by term
|
---|
| 46 | (defun monom-times-term (m term)
|
---|
| 47 | (cons (monom* m (car term)) (cdr term)))
|
---|
| 48 |
|
---|
| 49 | ;; Whether a term divides another term
|
---|
| 50 | (defun term-divides-p (term1 term2)
|
---|
| 51 | (monom-divides-p (car term1) (car term2)))
|
---|
| 52 |
|
---|
| 53 | ;; Accessor functions
|
---|
| 54 | (defmacro term-monom (term) `(car ,term))
|
---|
| 55 | (defmacro term-coefficient (term) `(cdr ,term))
|
---|
| 56 |
|
---|
| 57 | ;; Setf methods
|
---|
| 58 | (defsetf term-monom (term) (monom)
|
---|
| 59 | `(setf (car ,term) ,monom))
|
---|
| 60 |
|
---|
| 61 | (defsetf term-coefficient (term) (coefficient)
|
---|
| 62 | `(setf (cdr ,term) ,coefficient))
|
---|
| 63 |
|
---|