1 package JSci.maths; 2 3 import java.lang.Comparable ; 4 import java.lang.Double ; 5 6 import JSci.GlobalSettings; 7 import JSci.maths.groups.AbelianGroup; 8 import JSci.maths.fields.*; 9 10 17 public final class MathDouble extends Number implements Comparable , Field.Member { 18 private static final long serialVersionUID = 8616680319093653108L; 19 20 private final double x; 21 24 public MathDouble(final double num) { 25 x=num; 26 } 27 32 public MathDouble(final String s) throws NumberFormatException { 33 x=Double.parseDouble(s); 34 } 35 39 public boolean equals(Object obj) { 40 return equals(obj, GlobalSettings.ZERO_TOL); 41 } 42 public boolean equals(Object obj, double tol) { 43 if(obj instanceof Number ) { 44 return Math.abs(x-((Number )obj).doubleValue()) <= tol; 45 } else 46 return false; 47 } 48 public int hashCode() { 49 return (int) x; 50 } 51 58 public int compareTo(Object obj) throws IllegalArgumentException { 59 if(obj!=null && (obj instanceof Number )) { 60 double objValue = ((Number )obj).doubleValue(); 61 if(Math.abs(x-objValue) <= GlobalSettings.ZERO_TOL) 62 return 0; 63 else 64 return (int)(x-objValue); 65 } else 66 throw new IllegalArgumentException ("Invalid object: "+obj.getClass()); 67 } 68 71 public String toString() { 72 return Double.toString(x); 73 } 74 77 public double value() { 78 return x; 79 } 80 public int intValue() { 81 return (int) x; 82 } 83 public long longValue() { 84 return (long) x; 85 } 86 public float floatValue() { 87 return (float) x; 88 } 89 public double doubleValue() { 90 return x; 91 } 92 95 public static boolean isZero(double x) { 96 return (Math.abs(x) <= GlobalSettings.ZERO_TOL); 97 } 98 101 public boolean isNaN() { 102 return (x==Double.NaN); 103 } 104 107 public boolean isInfinite() { 108 return (x==Double.POSITIVE_INFINITY) || (x==Double.NEGATIVE_INFINITY); 109 } 110 public Object getSet() { 111 return RealField.getInstance(); 112 } 113 116 public AbelianGroup.Member negate() { 117 return new MathDouble(-x); 118 } 119 122 public Field.Member inverse() { 123 return new MathDouble(1.0/x); 124 } 125 128 public AbelianGroup.Member add(final AbelianGroup.Member n) { 129 if(n instanceof Number ) 130 return add(((Number )n).doubleValue()); 131 else 132 throw new IllegalArgumentException ("Member class not recognised by this method: "+n.getClass()); 133 } 134 137 public MathDouble add(final MathDouble n) { 138 return add(n.x); 139 } 140 public MathDouble add(double y) { 141 return new MathDouble(x+y); 142 } 143 146 public AbelianGroup.Member subtract(final AbelianGroup.Member n) { 147 if(n instanceof Number ) 148 return subtract(((Number )n).doubleValue()); 149 else 150 throw new IllegalArgumentException ("Member class not recognised by this method: "+n.getClass()); 151 } 152 155 public MathDouble subtract(final MathDouble n) { 156 return subtract(n.x); 157 } 158 public MathDouble subtract(double y) { 159 return new MathDouble(x-y); 160 } 161 164 public Ring.Member multiply(final Ring.Member n) { 165 if(n instanceof Number ) 166 return multiply(((Number )n).doubleValue()); 167 else 168 throw new IllegalArgumentException ("Member class not recognised by this method: "+n.getClass()); 169 } 170 173 public MathDouble multiply(final MathDouble n) { 174 return multiply(n.x); 175 } 176 public MathDouble multiply(double y) { 177 return new MathDouble(x*y); 178 } 179 182 public Field.Member divide(final Field.Member n) { 183 if(n instanceof Number ) 184 return divide(((Number )n).doubleValue()); 185 else 186 throw new IllegalArgumentException ("Member class not recognised by this method: "+n.getClass()); 187 } 188 191 public MathDouble divide(final MathDouble n) { 192 return divide(n.x); 193 } 194 public MathDouble divide(double y) { 195 return new MathDouble(x/y); 196 } 197 198 202 204 207 public static MathDouble exp(final MathDouble x) { 208 return exp(x.x); 209 } 210 public static MathDouble exp(Number x) { 211 return exp(x.doubleValue()); 212 } 213 public static MathDouble exp(double x) { 214 return new MathDouble(Math.exp(x)); 215 } 216 217 219 222 public static MathDouble log(final MathDouble x) { 223 return log(x.x); 224 } 225 public static MathDouble log(Number x) { 226 return log(x.doubleValue()); 227 } 228 public static MathDouble log(double x) { 229 return new MathDouble(Math.log(x)); 230 } 231 232 234 238 public static MathDouble sin(final MathDouble x) { 239 return sin(x.x); 240 } 241 public static MathDouble sin(Number x) { 242 return sin(x.doubleValue()); 243 } 244 public static MathDouble sin(double x) { 245 return new MathDouble(Math.sin(x)); 246 } 247 248 250 254 public static MathDouble cos(final MathDouble x) { 255 return cos(x.x); 256 } 257 public static MathDouble cos(Number x) { 258 return cos(x.doubleValue()); 259 } 260 public static MathDouble cos(double x) { 261 return new MathDouble(Math.cos(x)); 262 } 263 264 266 270 public static MathDouble tan(final MathDouble x) { 271 return tan(x.x); 272 } 273 public static MathDouble tan(Number x) { 274 return tan(x.doubleValue()); 275 } 276 public static MathDouble tan(double x) { 277 return new MathDouble(Math.tan(x)); 278 } 279 280 282 285 public static MathDouble sinh(final MathDouble x) { 286 return sinh(x.x); 287 } 288 public static MathDouble sinh(Number x) { 289 return sinh(x.doubleValue()); 290 } 291 public static MathDouble sinh(double x) { 292 return new MathDouble(ExtraMath.sinh(x)); 293 } 294 295 297 300 public static MathDouble cosh(final MathDouble x) { 301 return cosh(x.x); 302 } 303 public static MathDouble cosh(Number x) { 304 return cosh(x.doubleValue()); 305 } 306 public static MathDouble cosh(double x) { 307 return new MathDouble(ExtraMath.cosh(x)); 308 } 309 310 312 315 public static MathDouble tanh(final MathDouble x) { 316 return tanh(x.x); 317 } 318 public static MathDouble tanh(Number x) { 319 return tanh(x.doubleValue()); 320 } 321 public static MathDouble tanh(double x) { 322 return new MathDouble(ExtraMath.tanh(x)); 323 } 324 325 327 330 public static MathDouble asin(final MathDouble x) { 331 return asin(x.x); 332 } 333 public static MathDouble asin(Number x) { 334 return asin(x.doubleValue()); 335 } 336 public static MathDouble asin(double x) { 337 return new MathDouble(Math.asin(x)); 338 } 339 340 342 345 public static MathDouble acos(final MathDouble x) { 346 return acos(x.x); 347 } 348 public static MathDouble acos(Number x) { 349 return acos(x.doubleValue()); 350 } 351 public static MathDouble acos(double x) { 352 return new MathDouble(Math.acos(x)); 353 } 354 355 357 360 public static MathDouble atan(final MathDouble x) { 361 return atan(x.x); 362 } 363 public static MathDouble atan(Number x) { 364 return atan(x.doubleValue()); 365 } 366 public static MathDouble atan(double x) { 367 return new MathDouble(Math.atan(x)); 368 } 369 370 372 375 public static MathDouble asinh(final MathDouble x) { 376 return asinh(x.x); 377 } 378 public static MathDouble asinh(Number x) { 379 return asinh(x.doubleValue()); 380 } 381 public static MathDouble asinh(double x) { 382 return new MathDouble(ExtraMath.asinh(x)); 383 } 384 385 387 390 public static MathDouble acosh(final MathDouble x) { 391 return acosh(x.x); 392 } 393 public static MathDouble acosh(Number x) { 394 return acosh(x.doubleValue()); 395 } 396 public static MathDouble acosh(double x) { 397 return new MathDouble(ExtraMath.acosh(x)); 398 } 399 400 402 405 public static MathDouble atanh(final MathDouble x) { 406 return atanh(x.x); 407 } 408 public static MathDouble atanh(Number x) { 409 return atanh(x.doubleValue()); 410 } 411 public static MathDouble atanh(double x) { 412 return new MathDouble(ExtraMath.atanh(x)); 413 } 414 } 415 | Popular Tags |