001: /*
002: * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
003: * Copyright (C) 2006 - JScience (http://jscience.org/)
004: * All rights reserved.
005: *
006: * Permission to use, copy, modify, and distribute this software is
007: * freely granted, provided that this notice is preserved.
008: */
009: package org.jscience.mathematics.function;
010:
011: import java.util.List;
012: import org.jscience.mathematics.structure.Field;
013:
014: import javolution.context.ObjectFactory;
015: import javolution.text.Text;
016: import javolution.text.TextBuilder;
017:
018: /**
019: * This class represents the quotient of two {@link Polynomial},
020: * it is also a {@link Field field} (invertible).
021: *
022: * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
023: * @version 3.1, April 1, 2006
024: */
025: public class RationalFunction<F extends Field<F>> extends
026: Function<F, F> implements Field<RationalFunction<F>> {
027:
028: /**
029: * Holds the factory for rational functions.
030: */
031: /**
032: * Holds the dividend.
033: */
034: private Polynomial<F> _dividend;
035:
036: /**
037: * Holds the divisor.
038: */
039: private Polynomial<F> _divisor;
040:
041: /**
042: * Default constructor.
043: */
044: private RationalFunction() {
045: }
046:
047: /**
048: * Returns the dividend of this rational function.
049: *
050: * @return this rational function dividend.
051: */
052: public Polynomial<F> getDividend() {
053: return _dividend;
054: }
055:
056: /**
057: * Returns the divisor of this rational function.
058: *
059: * @return this rational function divisor.
060: */
061: public Polynomial<F> getDivisor() {
062: return _divisor;
063: }
064:
065: /**
066: * Returns the rational function from the specified dividend and divisor.
067: *
068: * @param dividend the dividend value.
069: * @param divisor the divisor value.
070: * @return <code>dividend / divisor</code>
071: */
072: @SuppressWarnings("unchecked")
073: public static <F extends Field<F>> RationalFunction<F> valueOf(
074: Polynomial<F> dividend, Polynomial<F> divisor) {
075: RationalFunction<F> rf = FACTORY.object();
076: rf._dividend = dividend;
077: rf._divisor = divisor;
078: return rf;
079: }
080:
081: @SuppressWarnings("unchecked")
082: private static final ObjectFactory<RationalFunction> FACTORY = new ObjectFactory<RationalFunction>() {
083:
084: protected RationalFunction create() {
085: return new RationalFunction();
086: }
087:
088: @SuppressWarnings("unchecked")
089: protected void cleanup(RationalFunction rf) {
090: rf._dividend = null;
091: rf._divisor = null;
092: }
093: };
094:
095: /**
096: * Returns the sum of two rational functions.
097: *
098: * @param that the rational function being added.
099: * @return <code>this + that</code>
100: */
101: public RationalFunction<F> plus(RationalFunction<F> that) {
102: return valueOf(this ._dividend.times(that._divisor).plus(
103: this ._divisor.times(that._dividend)), this ._divisor
104: .times(that._divisor));
105: }
106:
107: /**
108: * Returns the opposite of this rational function.
109: *
110: * @return <code>- this</code>
111: */
112: public RationalFunction<F> opposite() {
113: return valueOf(_dividend.opposite(), _divisor);
114: }
115:
116: /**
117: * Returns the difference of two rational functions.
118: *
119: * @param that the rational function being subtracted.
120: * @return <code>this - that</code>
121: */
122: public RationalFunction<F> minus(RationalFunction<F> that) {
123: return this .plus(that.opposite());
124: }
125:
126: /**
127: * Returns the product of two rational functions.
128: *
129: * @param that the rational function multiplier.
130: * @return <code>this ยท that</code>
131: */
132: public RationalFunction<F> times(RationalFunction<F> that) {
133: return valueOf(this ._dividend.times(that._dividend),
134: this ._divisor.times(that._divisor));
135: }
136:
137: /**
138: * Returns the inverse of this rational function.
139: *
140: * @return <code>1 / this</code>
141: */
142: public RationalFunction<F> inverse() {
143: return valueOf(_divisor, _dividend);
144: }
145:
146: /**
147: * Returns the quotient of two rational functions.
148: *
149: * @param that the rational function divisor.
150: * @return <code>this / that</code>
151: */
152: public RationalFunction<F> divide(RationalFunction<F> that) {
153: return this .times(that.inverse());
154: }
155:
156: @SuppressWarnings("unchecked")
157: @Override
158: public List<Variable<F>> getVariables() {
159: return merge(_dividend.getVariables(), _divisor.getVariables());
160: }
161:
162: @SuppressWarnings("unchecked")
163: @Override
164: public F evaluate() {
165: return _dividend.evaluate()
166: .times(_divisor.evaluate().inverse());
167: }
168:
169: @Override
170: public Text toText() {
171: TextBuilder tb = TextBuilder.newInstance();
172: tb.append('(');
173: tb.append(_dividend);
174: tb.append(")/(");
175: tb.append(_divisor);
176: tb.append(')');
177: return tb.toText();
178: }
179:
180: @Override
181: public boolean equals(Object obj) {
182: if (obj instanceof RationalFunction) {
183: RationalFunction<?> that = (RationalFunction<?>) obj;
184: return this ._dividend.equals(this ._dividend)
185: && this ._divisor.equals(that._divisor);
186: } else {
187: return false;
188: }
189: }
190:
191: @Override
192: public int hashCode() {
193: return _dividend.hashCode() - _divisor.hashCode();
194: }
195:
196: //////////////////////////////////////////////////////////////////////
197: // Overrides parent method potentially returning rational functions //
198: //////////////////////////////////////////////////////////////////////
199:
200: @Override
201: public RationalFunction<F> differentiate(Variable<F> v) {
202: return valueOf(_divisor.times(_dividend.differentiate(v)).plus(
203: _dividend.times(_divisor.differentiate(v)).opposite()),
204: _dividend.pow(2));
205: }
206:
207: @SuppressWarnings("unchecked")
208: @Override
209: public Function<F, F> plus(Function<F, F> that) {
210: return (that instanceof RationalFunction) ? this
211: .plus((RationalFunction<F>) that) : super .plus(that);
212: }
213:
214: @SuppressWarnings("unchecked")
215: @Override
216: public Function<F, F> minus(Function<F, F> that) {
217: return (that instanceof RationalFunction) ? this
218: .minus((RationalFunction<F>) that) : super .minus(that);
219: }
220:
221: @SuppressWarnings("unchecked")
222: @Override
223: public Function<F, F> times(Function<F, F> that) {
224: return (that instanceof RationalFunction) ? this
225: .times((RationalFunction<F>) that) : super .times(that);
226: }
227:
228: @SuppressWarnings("unchecked")
229: @Override
230: public Function<F, F> divide(Function<F, F> that) {
231: return (that instanceof RationalFunction) ? this
232: .divide((RationalFunction<F>) that) : super
233: .divide(that);
234: }
235:
236: @SuppressWarnings("unchecked")
237: @Override
238: public RationalFunction<F> pow(int n) {
239: return (RationalFunction<F>) super .pow(n);
240: }
241:
242: /**
243: * Returns a copy of this rational function.
244: * {@link javolution.context.AllocatorContext allocated}
245: * by the calling thread (possibly on the stack).
246: *
247: * @return an identical and independant copy of this rational function.
248: */
249: public RationalFunction<F> copy() {
250: return RationalFunction.valueOf(_dividend.copy(), _divisor
251: .copy());
252: }
253:
254: private static final long serialVersionUID = 1L;
255:
256: }
|