[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"
|
---|
[4301] | 23 | (:use :cl :copy :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"
|
---|
[4309] | 34 | "->SEXP"
|
---|
| 35 | "MAKE-ZERO-FOR"
|
---|
| 36 | "MAKE-UNIT-FOR")
|
---|
[4270] | 37 | (:documentation "Wraps rationals into an object."))
|
---|
[4269] | 38 |
|
---|
| 39 | (in-package "RATIONAL-FIELD")
|
---|
| 40 |
|
---|
| 41 | (defclass rational-field (ring)
|
---|
[4315] | 42 | ((value :initarg :value :initform 0 :accessor rational-field-value :type rational))
|
---|
[4269] | 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-equalp ((self rational-field) (other rational-field))
|
---|
| 102 | (with-slots (value)
|
---|
| 103 | self
|
---|
| 104 | (with-slots ((other-value value))
|
---|
| 105 | other
|
---|
| 106 | (= value other-value))))
|
---|
| 107 |
|
---|
| 108 | (defmethod ->sexp ((self rational-field) &optional vars)
|
---|
| 109 | (declare (ignore vars))
|
---|
| 110 | (rational-field-value self))
|
---|
[4308] | 111 |
|
---|
| 112 | (defmethod make-zero-for ((object rational-field))
|
---|
[4313] | 113 | ;; NOTE: Always the same object is returned
|
---|
| 114 | #.(make-instance 'rational-field :value 0))
|
---|
[4308] | 115 |
|
---|
| 116 | (defmethod make-unit-for ((object rational-field))
|
---|
[4313] | 117 | ;; NOTE: Always the same object is returned
|
---|
| 118 | #.(make-instance 'rational-field :value 1))
|
---|