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@ 4308

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

* empty log message *

File size: 3.5 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(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-EQUALP"
33 "UNIVERSAL-ZEROP"
34 "->SEXP")
35 (:documentation "Wraps rationals into an object."))
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
58(defmethod divide-by ((self rational-field) (other rational-field))
59 (with-slots (value)
60 self
61 (with-slots ((other-value value))
62 other
63 (setf value (/ value other-value))))
64 self)
65
66(defmethod add-to ((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 subtract-from ((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 unary-minus ((self rational-field))
83 (with-slots (value)
84 self
85 (setf value (- value)))
86 self)
87
88(defmethod unary-inverse ((self rational-field))
89 (with-slots (value)
90 self
91 (setf value (/ value)))
92 self)
93
94(defmethod universal-zerop ((self rational-field))
95 (with-slots (value)
96 self
97 (zerop value)))
98
99(defmethod universal-equalp ((self rational-field) (other rational-field))
100 (with-slots (value)
101 self
102 (with-slots ((other-value value))
103 other
104 (= value other-value))))
105
106(defmethod ->sexp ((self rational-field) &optional vars)
107 (declare (ignore vars))
108 (rational-field-value self))
109
110(defmethod make-zero-for ((object rational-field))
111 (make-instance 'rational-field :value 0))
112
113(defmethod make-unit-for ((object rational-field))
114 (make-instance 'rational-field :value 1))
Note: See TracBrowser for help on using the repository browser.