1 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 25 public class RationalFunction<F extends Field<F>> extends Function<F, F> 26 implements Field<RationalFunction<F>> { 27 28 31 34 private Polynomial<F> _dividend; 35 36 39 private Polynomial<F> _divisor; 40 41 44 private RationalFunction() { 45 } 46 47 52 public Polynomial<F> getDividend() { 53 return _dividend; 54 } 55 56 61 public Polynomial<F> getDivisor() { 62 return _divisor; 63 } 64 65 72 @SuppressWarnings ("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 ("unchecked") 88 protected void cleanup(RationalFunction rf) { 89 rf._dividend = null; 90 rf._divisor = null; 91 } 92 }; 93 94 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 111 public RationalFunction<F> opposite() { 112 return valueOf(_dividend.opposite(), _divisor); 113 } 114 115 121 public RationalFunction<F> minus(RationalFunction<F> that) { 122 return this.plus(that.opposite()); 123 } 124 125 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 141 public RationalFunction<F> inverse() { 142 return valueOf(_divisor, _dividend); 143 } 144 145 151 public RationalFunction<F> divide(RationalFunction<F> that) { 152 return this.times(that.inverse()); 153 } 154 155 @SuppressWarnings ("unchecked") 156 @Override 157 public List <Variable<F>> 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 209 @Override 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 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 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 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 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 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 |