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")
|
---|
37 | (:documentation "Wraps integers into an object."))
|
---|
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
|
---|
93 | (case value
|
---|
94 | (1 self)
|
---|
95 | (-1 self)
|
---|
96 | (otherwise (error "Non-invertible ring element: ~S" self)))))
|
---|
97 |
|
---|
98 | (defmethod universal-zerop ((self rational-field))
|
---|
99 | (with-slots (value)
|
---|
100 | self
|
---|
101 | (zerop value)))
|
---|
102 |
|
---|
103 | (defmethod universal-gcd ((self rational-field) (other rational-field))
|
---|
104 | (make-instance 'rational-field :value 1))))
|
---|
105 |
|
---|
106 |
|
---|
107 | (defmethod universal-ezgcd ((object rational-field) (other rational-field))
|
---|
108 | (values (make-instance 'rational-field :value 1)
|
---|
109 | (copy-instance object)
|
---|
110 | (copy-instance other)))
|
---|
111 |
|
---|
112 | (defmethod universal-equalp ((self rational-field) (other rational-field))
|
---|
113 | (with-slots (value)
|
---|
114 | self
|
---|
115 | (with-slots ((other-value value))
|
---|
116 | other
|
---|
117 | (= value other-value))))
|
---|
118 |
|
---|
119 | (defmethod ->sexp ((self rational-field) &optional vars)
|
---|
120 | (declare (ignore vars))
|
---|
121 | (rational-field-value self))
|
---|