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