| 1 | ;;; -*-  Mode: Lisp -*- 
 | 
|---|
| 2 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 | 
|---|
| 3 | ;;;                                                                              
 | 
|---|
| 4 | ;;;  Copyright (C) 1999, 2002, 2009, 2015 Marek Rychlik <rychlik@u.arizona.edu>          
 | 
|---|
| 5 | ;;;                                                                              
 | 
|---|
| 6 | ;;;  This program is free software; you can redistribute it and/or modify        
 | 
|---|
| 7 | ;;;  it under the terms of the GNU General Public License as published by        
 | 
|---|
| 8 | ;;;  the Free Software Foundation; either version 2 of the License, or           
 | 
|---|
| 9 | ;;;  (at your option) any later version.                                         
 | 
|---|
| 10 | ;;;                                                                              
 | 
|---|
| 11 | ;;;  This program is distributed in the hope that it will be useful,             
 | 
|---|
| 12 | ;;;  but WITHOUT ANY WARRANTY; without even the implied warranty of              
 | 
|---|
| 13 | ;;;  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               
 | 
|---|
| 14 | ;;;  GNU General Public License for more details.                                
 | 
|---|
| 15 | ;;;                                                                              
 | 
|---|
| 16 | ;;;  You should have received a copy of the GNU General Public License           
 | 
|---|
| 17 | ;;;  along with this program; if not, write to the Free Software                 
 | 
|---|
| 18 | ;;;  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
 | 
|---|
| 19 | ;;;                                                                              
 | 
|---|
| 20 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 | 
|---|
| 21 | 
 | 
|---|
| 22 | (defpackage "TERM"
 | 
|---|
| 23 |   (:use :cl :monom :ring)
 | 
|---|
| 24 |   (:export "TERM" 
 | 
|---|
| 25 |            "MAKE-TERM-VARIABLE"
 | 
|---|
| 26 |            )
 | 
|---|
| 27 |   (:documentation "This package implements class TERM. A term is a
 | 
|---|
| 28 | product of a scalar and powers of some variables, such as
 | 
|---|
| 29 | 5*X^2*Y^3. The part of the term without the coefficient is a monomial
 | 
|---|
| 30 | X^2*Y^3, which is represented by class MONOM, provided by the :MONOM
 | 
|---|
| 31 | package. In this implementation, a TERM specializes MONOL. Also, a
 | 
|---|
| 32 | monomial can be considered a TERM whose coefficient is the unit
 | 
|---|
| 33 | element (1) of the underlying ring. The generic method CHANGE-CLASS
 | 
|---|
| 34 | can be used to convert between a MONOM and a TERM, observing this
 | 
|---|
| 35 | convention."))
 | 
|---|
| 36 | 
 | 
|---|
| 37 | (in-package :term)
 | 
|---|
| 38 | 
 | 
|---|
| 39 | (proclaim '(optimize (speed 3) (space 0) (safety 0) (debug 0)))
 | 
|---|
| 40 | 
 | 
|---|
| 41 | (defclass term (monom scalar)
 | 
|---|
| 42 |   ()
 | 
|---|
| 43 |   (:default-initargs :dimension 0 :exponents '() :coeff 1)
 | 
|---|
| 44 |   (:documentation "Implements a term, i.e. a product of a scalar
 | 
|---|
| 45 | and powers of some variables, such as 5*X^2*Y^3."))
 | 
|---|
| 46 | 
 | 
|---|
| 47 | (defmethod print-object ((self term) stream)
 | 
|---|
| 48 |   #+nil
 | 
|---|
| 49 |   (with-accessors ((dimension monom-dimension)
 | 
|---|
| 50 |                    (exponents monom-exponents)
 | 
|---|
| 51 |                    (coeff scalar-coeff))
 | 
|---|
| 52 |       self
 | 
|---|
| 53 |     (format stream "#<TERM DIMENSION=~A EXPONENTS=~A COEFF=~A>"
 | 
|---|
| 54 |             dimension
 | 
|---|
| 55 |             exponents
 | 
|---|
| 56 |             coeff))
 | 
|---|
| 57 |   (print-unreadable-object (self stream :type t :identity t)))
 | 
|---|
| 58 |     
 | 
|---|
| 59 | 
 | 
|---|
| 60 | (defmethod r-equalp ((term1 term) (term2 term))
 | 
|---|
| 61 |   (and (r-equalp (scalar-coeff term1) (scalar-coeff term2))
 | 
|---|
| 62 |        (= (monom-dimension term1) (monom-dimension term2))
 | 
|---|
| 63 |        (equalp (monom-exponents term1) (monom-exponents term2))))
 | 
|---|
| 64 | 
 | 
|---|
| 65 | 
 | 
|---|
| 66 | #|
 | 
|---|
| 67 | (defmethod shared-initialize ((self term) slot-names
 | 
|---|
| 68 |                               &rest 
 | 
|---|
| 69 |                                 initargs 
 | 
|---|
| 70 |                               &key
 | 
|---|
| 71 |                                 coeff
 | 
|---|
| 72 |                                 &allow-other-keys)
 | 
|---|
| 73 |   (declare (ignore initargs))
 | 
|---|
| 74 |   (if (eq slot-names t) (setf slot-names '(coeff)))
 | 
|---|
| 75 |   (dolist (slot-name slot-names)
 | 
|---|
| 76 |     (case slot-name
 | 
|---|
| 77 |       (coeff
 | 
|---|
| 78 |        (setf (slot-value self 'coeff) coeff)))))
 | 
|---|
| 79 | |#
 | 
|---|
| 80 | 
 | 
|---|
| 81 | (defmethod update-instance-for-different-class ((old monom) (new  scalar)
 | 
|---|
| 82 |                                                 &rest
 | 
|---|
| 83 |                                                   initargs
 | 
|---|
| 84 |                                                 &key 
 | 
|---|
| 85 |                                                   &allow-other-keys)
 | 
|---|
| 86 |   (format t "UPDATE-INSTANCE-FOR-DIFFERENT-CLASS MONOM->SCALAR called.%")
 | 
|---|
| 87 |   (format t "Old: ~A~%" old)
 | 
|---|
| 88 |   (format t "Initargs: ~A~%" initargs)
 | 
|---|
| 89 |   (call-next-method))
 | 
|---|
| 90 | 
 | 
|---|
| 91 | #|
 | 
|---|
| 92 | (defun make-term-variable (nvars pos
 | 
|---|
| 93 |                            &optional
 | 
|---|
| 94 |                              (power 1)
 | 
|---|
| 95 |                              (coeff 1))
 | 
|---|
| 96 |   "Construct a term in the polynomial ring RING[X[0],X[1],X[2],...X[NVARS-1]]
 | 
|---|
| 97 | over the ring RING which represents a single variable. It assumes
 | 
|---|
| 98 | number of variables NVARS and the variable is at position
 | 
|---|
| 99 | POS. Optionally, the variable may appear raised to power POWER.
 | 
|---|
| 100 | Optionally, the term may appear with an arbitrary coefficient, which
 | 
|---|
| 101 | defaults to the unit of the RING."
 | 
|---|
| 102 |   (declare (type fixnum nvars pos))
 | 
|---|
| 103 |   (make-term :monom (make-monom-variable nvars pos power)
 | 
|---|
| 104 |              :coeff coeff))
 | 
|---|
| 105 | 
 | 
|---|
| 106 | |#
 | 
|---|
| 107 | 
 | 
|---|
| 108 | (defmethod multiply-by :before ((self term) (other term))
 | 
|---|
| 109 |   "Destructively multiply terms SELF and OTHER and store the result into SELF.
 | 
|---|
| 110 | It returns SELF."
 | 
|---|
| 111 |   (setf (scalar-coeff self) (multiply-by (scalar-coeff self) (scalar-coeff other))))
 | 
|---|
| 112 | 
 | 
|---|
| 113 | (defmethod left-tensor-product-by ((self term) (other term))
 | 
|---|
| 114 |   (setf (scalar-coeff self) (multiply-by (scalar-coeff self) (scalar-coeff other)))
 | 
|---|
| 115 |   (call-next-method))
 | 
|---|
| 116 | 
 | 
|---|
| 117 | (defmethod right-tensor-product-by ((self term) (other term))
 | 
|---|
| 118 |   (setf (scalar-coeff self) (multiply-by (scalar-coeff self) (scalar-coeff other)))
 | 
|---|
| 119 |   (call-next-method))
 | 
|---|
| 120 | 
 | 
|---|
| 121 | (defmethod left-tensor-product-by ((self term) (other monom))
 | 
|---|
| 122 |   (call-next-method))
 | 
|---|
| 123 | 
 | 
|---|
| 124 | (defmethod right-tensor-product-by ((self term) (other monom))
 | 
|---|
| 125 |   (call-next-method))
 | 
|---|
| 126 | 
 | 
|---|
| 127 | (defmethod divide-by ((self term) (other term))
 | 
|---|
| 128 |   "Destructively divide term SELF by OTHER and store the result into SELF.
 | 
|---|
| 129 | It returns SELF."
 | 
|---|
| 130 |   (setf (scalar-coeff self) (divide-by (scalar-coeff self) (scalar-coeff other)))
 | 
|---|
| 131 |   (call-next-method))
 | 
|---|
| 132 | 
 | 
|---|
| 133 | (defmethod unary-minus ((self term))
 | 
|---|
| 134 |   (setf (scalar-coeff self) (unary-minus (scalar-coeff self)))
 | 
|---|
| 135 |   self)
 | 
|---|
| 136 | 
 | 
|---|
| 137 | (defmethod r* ((term1 term) (term2 term))
 | 
|---|
| 138 |   "Non-destructively multiply TERM1 by TERM2."
 | 
|---|
| 139 |   (multiply-by (copy-instance term1) (copy-instance term2)))
 | 
|---|
| 140 | 
 | 
|---|
| 141 | (defmethod r-zerop ((self term))
 | 
|---|
| 142 |   (r-zerop (scalar-coeff self)))
 | 
|---|
| 143 | 
 | 
|---|
| 144 | #|
 | 
|---|
| 145 | 
 | 
|---|
| 146 | (defun term->cons (term) 
 | 
|---|
| 147 |   "A human-readable representation of a term as a cons (MONOM . COEFF)."
 | 
|---|
| 148 |   (declare (type term term))
 | 
|---|
| 149 |   (cons (monom->list (term-monom term)) (scalar-coeff term)))
 | 
|---|
| 150 | 
 | 
|---|
| 151 | |#
 | 
|---|