KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jscience > mathematics > functions > RationalFunction


1 /*
2  * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
3  * Copyright (C) 2006 - JScience (http://jscience.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package org.jscience.mathematics.functions;
10
11 import java.util.List JavaDoc;
12
13 import org.jscience.mathematics.structures.Field;
14
15 import javolution.text.Text;
16 import javolution.text.TextBuilder;
17
18 /**
19  * This class represents the quotient of two {@link Polynomial},
20  * it is also a {@link Field field} (invertible).
21  *
22  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
23  * @version 3.1, April 1, 2006
24  */

25 public class RationalFunction<F extends Field<F>> extends Function<F, F>
26         implements Field<RationalFunction<F>> {
27
28     /**
29      * Holds the factory for rational functions.
30      */

31     /**
32      * Holds the dividend.
33      */

34     private Polynomial<F> _dividend;
35
36     /**
37      * Holds the divisor.
38      */

39     private Polynomial<F> _divisor;
40
41     /**
42      * Default constructor.
43      */

44     private RationalFunction() {
45     }
46
47     /**
48      * Returns the dividend of this rational function.
49      *
50      * @return this rational function dividend.
51      */

52     public Polynomial<F> getDividend() {
53         return _dividend;
54     }
55
56     /**
57      * Returns the divisor of this rational function.
58      *
59      * @return this rational function divisor.
60      */

61     public Polynomial<F> getDivisor() {
62         return _divisor;
63     }
64
65     /**
66      * Returns the rational function from the specified dividend and divisor.
67      *
68      * @param dividend the dividend value.
69      * @param divisor the divisor value.
70      * @return <code>dividend / divisor</code>
71      */

72     @SuppressWarnings JavaDoc("unchecked")
73     public static <F extends Field<F>> RationalFunction<F> valueOf(
74             Polynomial<F> dividend, Polynomial<F> divisor) {
75         RationalFunction<F> rf = FACTORY.object();
76         rf._dividend = dividend;
77         rf._divisor = divisor;
78         return rf;
79     }
80
81     private static final Factory<RationalFunction> FACTORY = new Factory<RationalFunction>() {
82
83         protected RationalFunction create() {
84             return new RationalFunction();
85         }
86
87         @SuppressWarnings JavaDoc("unchecked")
88         protected void cleanup(RationalFunction rf) {
89             rf._dividend = null;
90             rf._divisor = null;
91         }
92     };
93
94     /**
95      * Returns the sum of two rational functions.
96      *
97      * @param that the rational function being added.
98      * @return <code>this + that</code>
99      */

100     public RationalFunction<F> plus(RationalFunction<F> that) {
101         return valueOf(this._dividend.times(that._divisor).plus(
102                 this._divisor.times(that._dividend)), this._divisor
103                 .times(that._divisor));
104     }
105
106     /**
107      * Returns the opposite of this rational function.
108      *
109      * @return <code>- this</code>
110      */

111     public RationalFunction<F> opposite() {
112         return valueOf(_dividend.opposite(), _divisor);
113     }
114
115     /**
116      * Returns the difference of two rational functions.
117      *
118      * @param that the rational function being subtracted.
119      * @return <code>this - that</code>
120      */

121     public RationalFunction<F> minus(RationalFunction<F> that) {
122         return this.plus(that.opposite());
123     }
124
125     /**
126      * Returns the product of two rational functions.
127      *
128      * @param that the rational function multiplier.
129      * @return <code>this ยท that</code>
130      */

131     public RationalFunction<F> times(RationalFunction<F> that) {
132         return valueOf(this._dividend.times(that._dividend), this._divisor
133                 .times(that._divisor));
134     }
135
136     /**
137      * Returns the inverse of this rational function.
138      *
139      * @return <code>1 / this</code>
140      */

141     public RationalFunction<F> inverse() {
142         return valueOf(_divisor, _dividend);
143     }
144
145     /**
146      * Returns the quotient of two rational functions.
147      *
148      * @param that the rational function divisor.
149      * @return <code>this / that</code>
150      */

151     public RationalFunction<F> divide(RationalFunction<F> that) {
152         return this.times(that.inverse());
153     }
154
155     @SuppressWarnings JavaDoc("unchecked")
156     @Override JavaDoc
157     public List JavaDoc<Variable<F>> getVariables() {
158         return merge(_dividend.getVariables(), _divisor.getVariables());
159     }
160
161     @SuppressWarnings JavaDoc("unchecked")
162     @Override JavaDoc
163     public F evaluate() {
164         return _dividend.evaluate().times(_divisor.evaluate().inverse());
165     }
166
167     @Override JavaDoc
168     public Text toText() {
169         TextBuilder tb = TextBuilder.newInstance();
170         tb.append('(');
171         tb.append(_dividend);
172         tb.append(")/(");
173         tb.append(_divisor);
174         tb.append(')');
175         return tb.toText();
176     }
177
178     @Override JavaDoc
179     public boolean equals(Object JavaDoc obj) {
180         if (obj instanceof RationalFunction) {
181             RationalFunction that = (RationalFunction) obj;
182             return this._dividend.equals(this._dividend)
183                     && this._divisor.equals(that._divisor);
184         } else {
185             return false;
186         }
187     }
188
189     @Override JavaDoc
190     public int hashCode() {
191         return _dividend.hashCode() - _divisor.hashCode();
192     }
193
194     @Override JavaDoc
195     public boolean move(ObjectSpace os) {
196         if (super.move(os)) {
197             _dividend.move(os);
198             _divisor.move(os);
199             return true;
200         }
201         return false;
202     }
203
204
205     //////////////////////////////////////////////////////////////////////
206
// Overrides parent method potentially returning rational functions //
207
//////////////////////////////////////////////////////////////////////
208

209     @Override JavaDoc
210     public RationalFunction<F> differentiate(Variable<F> v) {
211         return valueOf(_divisor.times(_dividend.differentiate(v)).plus(
212                 _dividend.times(_divisor.differentiate(v)).opposite()),
213                 _dividend.pow(2));
214     }
215
216     @Override JavaDoc
217     public Function<F, F> plus(Function<F, F> that) {
218         return (that instanceof RationalFunction) ?
219                 this.plus((RationalFunction<F>)that) : super.plus(that);
220     }
221
222     @Override JavaDoc
223     public Function<F, F> minus(Function<F, F> that) {
224         return (that instanceof RationalFunction) ?
225                 this.minus((RationalFunction<F>)that) : super.minus(that);
226     }
227
228     @Override JavaDoc
229     public Function<F, F> times(Function<F, F> that) {
230         return (that instanceof RationalFunction) ?
231                 this.times((RationalFunction<F>)that) : super.times(that);
232     }
233     
234     @Override JavaDoc
235     public Function<F, F> divide(Function<F, F> that) {
236         return (that instanceof RationalFunction) ?
237                 this.divide((RationalFunction<F>)that) : super.divide(that);
238     }
239     
240     @Override JavaDoc
241     public RationalFunction<F> pow(int n) {
242         return (RationalFunction<F>) super.pow(n);
243     }
244
245     private static final long serialVersionUID = 1L;
246 }
Popular Tags