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

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

* empty log message *

File size: 3.7 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 "MAKE-ZERO-FOR"
36 "MAKE-UNIT-FOR")
37 (:documentation "Wraps rationals 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 (setf value (/ value)))
94 self)
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))
111
112(defparameter *rational-field-zero* (make-instance 'rational-field :value 0))
113(defparameter *rational-field-unit* (make-instance 'rational-field :value 1))
114
115(defmethod make-zero-for ((object rational-field))
116 (copy-instance *rational-field-zero*))
117
118(defmethod make-unit-for ((object rational-field))
119 (copy-instance *rational-field-unit*))
Note: See TracBrowser for help on using the repository browser.