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 is7  * freely granted, provided that this notice is preserved.8  */9 package org.jscience.mathematics.functions;10 11 import java.util.List ;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 Jean-Marie Dautelle23  * @version 3.1, April 1, 200624  */25 public class RationalFunction> extends Function26         implements Field> {27 28     /**29      * Holds the factory for rational functions.30      */31     /**32      * Holds the dividend.33      */34     private Polynomial _dividend;35 36     /**37      * Holds the divisor.38      */39     private Polynomial _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 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 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 dividend / divisor71      */72     @SuppressWarnings ("unchecked")73     public static > RationalFunction valueOf(74             Polynomial dividend, Polynomial divisor) {75         RationalFunction rf = FACTORY.object();76         rf._dividend = dividend;77         rf._divisor = divisor;78         return rf;79     }80 81     private static final Factory FACTORY = new Factory() {82 83         protected RationalFunction create() {84             return new RationalFunction();85         }86 87         @SuppressWarnings ("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 this + that99      */100     public RationalFunction plus(RationalFunction that) {101         return valueOf(this._dividend.times(that._divisor).plus(102                 this._divisor.times(that._dividend)), this._divisor103                 .times(that._divisor));104     }105 106     /**107      * Returns the opposite of this rational function.108      * 109      * @return - this110      */111     public RationalFunction 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 this - that120      */121     public RationalFunction minus(RationalFunction 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 this ยท that130      */131     public RationalFunction times(RationalFunction that) {132         return valueOf(this._dividend.times(that._dividend), this._divisor133                 .times(that._divisor));134     }135 136     /**137      * Returns the inverse of this rational function.138      * 139      * @return 1 / this140      */141     public RationalFunction 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 this / that150      */151     public RationalFunction divide(RationalFunction that) {152         return this.times(that.inverse());153     }154 155     @SuppressWarnings ("unchecked")156     @Override 157     public List > getVariables() {158         return merge(_dividend.getVariables(), _divisor.getVariables());159     }160 161     @SuppressWarnings ("unchecked")162     @Override 163     public F evaluate() {164         return _dividend.evaluate().times(_divisor.evaluate().inverse());165     }166 167     @Override 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 179     public boolean equals(Object 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 190     public int hashCode() {191         return _dividend.hashCode() - _divisor.hashCode();192     }193 194     @Override 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 210     public RationalFunction differentiate(Variable 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 217     public Function plus(Function that) {218         return (that instanceof RationalFunction) ?219                 this.plus((RationalFunction)that) : super.plus(that); 220     }221 222     @Override 223     public Function minus(Function that) {224         return (that instanceof RationalFunction) ?225                 this.minus((RationalFunction)that) : super.minus(that); 226     }227 228     @Override 229     public Function times(Function that) {230         return (that instanceof RationalFunction) ?231                 this.times((RationalFunction)that) : super.times(that); 232     }233     234     @Override 235     public Function divide(Function that) {236         return (that instanceof RationalFunction) ?237                 this.divide((RationalFunction)that) : super.divide(that); 238     }239     240     @Override 241     public RationalFunction pow(int n) {242         return (RationalFunction) super.pow(n);243     }244 245     private static final long serialVersionUID = 1L;246 }` Popular Tags