[4269] | 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 "RATIONAL-FIELD"
|
---|
| 23 | (:use :cl :copy :ring)
|
---|
| 24 | (:export "RATIONAL-FIELD"
|
---|
| 25 | "RATIONAL-FIELD-VALUE"
|
---|
| 26 | "ADD-TO"
|
---|
| 27 | "SUBTRACT-FROM"
|
---|
| 28 | "MULTIPLY-BY"
|
---|
| 29 | "DIVIDE-BY"
|
---|
| 30 | "UNARY-MINUS"
|
---|
| 31 | "UNARY-INVERSE"
|
---|
| 32 | "UNIVERSAL-GCD"
|
---|
| 33 | "UNIVERSAL-EZGCD"
|
---|
| 34 | "UNIVERSAL-EQUALP"
|
---|
| 35 | "UNIVERSAL-ZEROP"
|
---|
| 36 | "->SEXP")
|
---|
[4270] | 37 | (:documentation "Wraps rationals into an object."))
|
---|
[4269] | 38 |
|
---|
| 39 | (in-package "RATIONAL-FIELD")
|
---|
| 40 |
|
---|
| 41 | (defclass rational-field (ring)
|
---|
| 42 | ((value :initarg :value :initform 0 :accessor rational-field-value :type rational))
|
---|
| 43 | (:documentation "An object representing an integer.")
|
---|
| 44 | )
|
---|
| 45 |
|
---|
| 46 | (defmethod print-object ((self rational-field) stream)
|
---|
| 47 | (print-unreadable-object (self stream :type t :identity t)
|
---|
| 48 | (with-accessors ((value rational-field-value))
|
---|
| 49 | self
|
---|
| 50 | (format stream "VALUE=~A" value))))
|
---|
| 51 |
|
---|
| 52 | (defmethod multiply-by ((self rational-field) (other rational-field))
|
---|
| 53 | (with-slots (value)
|
---|
| 54 | self
|
---|
| 55 | (with-slots ((other-value value))
|
---|
| 56 | other
|
---|
| 57 | (setf value (* value other-value))))
|
---|
| 58 | self)
|
---|
| 59 |
|
---|
| 60 | (defmethod divide-by ((self rational-field) (other rational-field))
|
---|
| 61 | (with-slots (value)
|
---|
| 62 | self
|
---|
| 63 | (with-slots ((other-value value))
|
---|
| 64 | other
|
---|
| 65 | (setf value (/ value other-value))))
|
---|
| 66 | self)
|
---|
| 67 |
|
---|
| 68 | (defmethod add-to ((self rational-field) (other rational-field))
|
---|
| 69 | (with-slots (value)
|
---|
| 70 | self
|
---|
| 71 | (with-slots ((other-value value))
|
---|
| 72 | other
|
---|
| 73 | (setf value (+ value other-value))))
|
---|
| 74 | self)
|
---|
| 75 |
|
---|
| 76 | (defmethod subtract-from ((self rational-field) (other rational-field))
|
---|
| 77 | (with-slots (value)
|
---|
| 78 | self
|
---|
| 79 | (with-slots ((other-value value))
|
---|
| 80 | other
|
---|
| 81 | (setf value (- value other-value))))
|
---|
| 82 | self)
|
---|
| 83 |
|
---|
| 84 | (defmethod unary-minus ((self rational-field))
|
---|
| 85 | (with-slots (value)
|
---|
| 86 | self
|
---|
| 87 | (setf value (- value)))
|
---|
| 88 | self)
|
---|
| 89 |
|
---|
| 90 | (defmethod unary-inverse ((self rational-field))
|
---|
| 91 | (with-slots (value)
|
---|
| 92 | self
|
---|
[4272] | 93 | (setf value (/ value)))
|
---|
[4271] | 94 | self)
|
---|
[4269] | 95 |
|
---|
| 96 | (defmethod universal-zerop ((self rational-field))
|
---|
| 97 | (with-slots (value)
|
---|
| 98 | self
|
---|
| 99 | (zerop value)))
|
---|
| 100 |
|
---|
| 101 | (defmethod universal-gcd ((self rational-field) (other rational-field))
|
---|
[4270] | 102 | (make-instance 'rational-field :value 1))
|
---|
[4269] | 103 |
|
---|
| 104 |
|
---|
| 105 | (defmethod universal-ezgcd ((object rational-field) (other rational-field))
|
---|
| 106 | (values (make-instance 'rational-field :value 1)
|
---|
| 107 | (copy-instance object)
|
---|
| 108 | (copy-instance other)))
|
---|
| 109 |
|
---|
| 110 | (defmethod universal-equalp ((self rational-field) (other rational-field))
|
---|
| 111 | (with-slots (value)
|
---|
| 112 | self
|
---|
| 113 | (with-slots ((other-value value))
|
---|
| 114 | other
|
---|
| 115 | (= value other-value))))
|
---|
| 116 |
|
---|
| 117 | (defmethod ->sexp ((self rational-field) &optional vars)
|
---|
| 118 | (declare (ignore vars))
|
---|
| 119 | (rational-field-value self))
|
---|