source: CGBLisp/src/RCS/rat.lisp,v@ 1

Last change on this file since 1 was 1, checked in by Marek Rychlik, 15 years ago

First import of a version circa 1997.

File size: 3.7 KB
Line 
1head 1.4;
2access;
3symbols;
4locks; strict;
5comment @;;; @;
6
7
81.4
9date 2009.01.22.04.07.12; author marek; state Exp;
10branches;
11next 1.3;
12
131.3
14date 2009.01.19.09.30.19; author marek; state Exp;
15branches;
16next 1.2;
17
181.2
19date 2009.01.19.07.52.26; author marek; state Exp;
20branches;
21next 1.1;
22
231.1
24date 2009.01.19.06.46.19; author marek; state Exp;
25branches;
26next ;
27
28
29desc
30@@
31
32
331.4
34log
35@*** empty log message ***
36@
37text
38@#|
39 $Id$
40 *--------------------------------------------------------------------------*
41 | Copyright (C) 1994, Marek Rychlik (e-mail: rychlik@@math.arizona.edu) |
42 | Department of Mathematics, University of Arizona, Tucson, AZ 85721 |
43 | |
44 | Everyone is permitted to copy, distribute and modify the code in this |
45 | directory, as long as this copyright note is preserved verbatim. |
46 *--------------------------------------------------------------------------*
47|#
48;;
49;; Arithmetic on rational functions in n variables
50;; They are represented as conses of two polynomials, represented
51;; as in the monom package, ordered lexicographically (the default
52;; of the monom package).
53;; This package is trivial in the sense that it does not produce
54;; results in which numerator and denominator are relatively prime.
55;; However, zero testing works correctly. This is all that is
56;; needed for the resultant package
57;;
58
59(defpackage "RAT"
60 (:export num denom rat+ rat- rat* rat/ scalar-times-rat scalar-div-rat
61 rat-zerop rat-uminus rat-expt rat-constant rat-to-poly
62 rat-simplify)
63 (:use "ORDER" "POLY-GCD" "DIVISION" "POLY" "COMMON-LISP"))
64
65(in-package "RAT")
66
67#+debug(proclaim '(optimize (speed 0) (debug 3)))
68#-debug(proclaim '(optimize (speed 3) (safety 0)))
69
70(defun num (p) (car p)) ;numerator
71(defun denom (p) (cdr p)) ;denominator
72
73(defun rat-simplify-2 (num denom)
74 (let ((gcd (poly-gcd num denom)))
75 (cons (poly-exact-divide num gcd)
76 (poly-exact-divide denom gcd))))
77
78(defun rat-simplify (p)
79 (rat-simplify-2 (car p) (cdr p)))
80
81(defun rat+ (p q)
82 ;; p1/p2+q1/q2=(p1*q2+p2*q1)/(p2*q2)
83 (rat-simplify-2
84 (poly+ (poly* (num p) (denom q))
85 (poly* (denom p) (num q)))
86 (poly* (denom p) (denom q))))
87
88(defun rat- (p q)
89 ;; p1/p2-q1/q2=(p1*q2+p2-q1)/(p2*q2)
90 (rat-simplify-2
91 (poly- (poly* (num p) (denom q))
92 (poly* (denom p) (num q)))
93 (poly* (denom p) (denom q))))
94
95;; This could be more efficient if it simplified before multiplication
96(defun rat* (p q)
97 (rat-simplify-2
98 (poly* (num p) (num q))
99 (poly* (denom p) (denom q))))
100
101(defun rat/ (p q)
102 (rat-simplify-2
103 (poly* (num p) (denom q))
104 (poly* (denom p) (num q))))
105
106(defun scalar-times-rat (scalar p)
107 (cons (scalar-times-poly scalar (num p)) (denom p)))
108
109;; scalar/rational-fun
110(defun scalar-div-rat (scalar p)
111 (cons (scalar-times-poly scalar (denom p)) (num p)))
112
113(defun rat-zerop (p)
114 (endp (car p)))
115
116(defun rat-uminus (p)
117 (cons (minus-poly (car p)) (cdr p)))
118
119(defun rat-expt (p n)
120 (cons (poly-expt (car p) n) (poly-expt (cdr p) n)))
121
122(defun rat-constant (c n)
123 "Make a constant rational function equal to c with n variables"
124 (cons (list (cons (make-list n :initial-element 0) c))
125 (list (cons (make-list n :initial-element 0) 1))))
126
127(defun rat-to-poly (p)
128 "Attempt to convert a rational function to a polynomial by
129dividing numerator by denominator. Error if not divisible"
130 (poly-exact-divide (car p) (cdr p)))
131@
132
133
1341.3
135log
136@*** empty log message ***
137@
138text
139@d30 2
140a31 2
141;;(proclaim '(optimize (speed 0) (debug 3)))
142(proclaim '(optimize (speed 3) (safety 0)))
143@
144
145
1461.2
147log
148@*** empty log message ***
149@
150text
151@d30 2
152a31 1
153(proclaim '(optimize (speed 0) (debug 3)))
154@
155
156
1571.1
158log
159@Initial revision
160@
161text
162@d2 1
163a2 1
164 $Id: rat.lisp,v 1.18 1997/12/13 07:10:56 marek Exp $
165d30 2
166@
Note: See TracBrowser for help on using the repository browser.