close Warning: Can't synchronize with repository "(default)" (The repository directory has changed, you should resynchronize the repository with: trac-admin $ENV repository resync '(default)'). Look in the Trac log for more information.

source: branches/f4grobner/rational-field.lisp@ 4331

Last change on this file since 4331 was 4325, checked in by Marek Rychlik, 8 years ago

Now classes INTEGER-RING and RATIONAL-FIELD are a part of the RING package

File size: 3.3 KB
Line 
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(in-package "RING")
23
24(defclass rational-field (ring)
25 ((value :initarg :value :initform 0 :accessor rational-field-value :type rational))
26 (:documentation "An object representing an integer.")
27 )
28
29(defmethod print-object ((self rational-field) stream)
30 (print-unreadable-object (self stream :type t :identity t)
31 (with-accessors ((value rational-field-value))
32 self
33 (format stream "VALUE=~A" value))))
34
35(defmethod multiply-by ((self rational-field) (other rational-field))
36 (with-slots (value)
37 self
38 (with-slots ((other-value value))
39 other
40 (setf value (* value other-value))))
41 self)
42
43(defmethod divide-by ((self rational-field) (other rational-field))
44 (with-slots (value)
45 self
46 (with-slots ((other-value value))
47 other
48 (setf value (/ value other-value))))
49 self)
50
51(defmethod add-to ((self rational-field) (other rational-field))
52 (with-slots (value)
53 self
54 (with-slots ((other-value value))
55 other
56 (setf value (+ value other-value))))
57 self)
58
59(defmethod subtract-from ((self rational-field) (other rational-field))
60 (with-slots (value)
61 self
62 (with-slots ((other-value value))
63 other
64 (setf value (- value other-value))))
65 self)
66
67(defmethod unary-minus ((self rational-field))
68 (with-slots (value)
69 self
70 (setf value (- value)))
71 self)
72
73(defmethod unary-inverse ((self rational-field))
74 (with-slots (value)
75 self
76 (setf value (/ value)))
77 self)
78
79(defmethod universal-zerop ((self rational-field))
80 (with-slots (value)
81 self
82 (zerop value)))
83
84(defmethod universal-equalp ((self rational-field) (other rational-field))
85 (with-slots (value)
86 self
87 (with-slots ((other-value value))
88 other
89 (= value other-value))))
90
91(defmethod ->sexp ((self rational-field) &optional vars)
92 (declare (ignore vars))
93 (rational-field-value self))
94
95(defparameter *rational-field-zero* (make-instance 'rational-field :value 0))
96(defparameter *rational-field-unit* (make-instance 'rational-field :value 1))
97
98(defmethod make-zero-for ((object rational-field))
99 (copy-instance *rational-field-zero*))
100
101(defmethod make-unit-for ((object rational-field))
102 (copy-instance *rational-field-unit*))
Note: See TracBrowser for help on using the repository browser.