edu.neu.ccs.util
Class MathUtilities

java.lang.Object
  extended byedu.neu.ccs.util.MathUtilities

public class MathUtilities
extends Object

The class MathUtilities collects several useful static mathematical functions. The class may be viewed as an extension of the tools found in java.lang.Math.

In 2.6.0, added the hyperbolic functions.

In 2.6.0, added getTileIndexLimits which is a tool for computing tilings.

Since:
1.2
Version:
2.6.0

Constructor Summary
MathUtilities()
           
 
Method Summary
static double acosdeg(double value)
          Return the arc cosine in degrees of the specified input value in the range -1 to +1.
static double acosh(double c)
          Inverse hyperbolic cosine.
static double asindeg(double value)
          Return the arc sine in degrees of the specified input value in the range -1 to +1.
static double asinh(double s)
          Inverse hyperbolic sine.
static double atan2deg(double y, double x)
          Return the polar angle in degrees of the point (x, y) where the y-coordinate is given first and the x-coordinate second.
static double atandeg(double value)
          Return the arc tangent in degrees of the specified input value.
static double atanh(double t)
          Inverse hyperbolic tangent.
static double cosdeg(double degrees)
          Return the cosine of the given angle specified in degrees.
static double cosh(double x)
          Hyperbolic cosine.
static int GCD(int a, int b)
          Returns the greatest common divisor of the int inputs a and b.
static long GCD(long a, long b)
          Returns the greatest common divisor of the long inputs a and b.
static int[] getTileIndexLimits(double x, double w, double x0, double w0)
          Returns an integer array with indices i and j that determine how a small interval from x0 to x0+w0 may be tiled to cover a large interval from x to x+w.
static int LCM(int a, int b)
          Returns the least common multiple of the int inputs a and b.
static long LCM(long a, long b)
          Returns the least common multiple of the long inputs a and b.
static int modulus(int number, int base)
          Returns the modulus of int number relative to int base.
static long modulus(long number, long base)
          Returns the modulus of long number relative to long base.
static double power(double x, int n)
          Returns the n-th power of x, xn, using the given integer exponent n.
static double randomDouble(double x)
          Return a random double r in the range between 0 and x.
static double randomDouble(double x, double y)
          Return a random double r in the range between x and y.
static int randomInt(int a)
          Return a random int r in the range between 0 and a inclusive.
static int randomInt(int a, int b)
          Return a random int r in the range between a and b inclusive.
static double root(double x, double n)
          Returns the n-th root of x.
static double sindeg(double degrees)
          Return the sine of the given angle specified in degrees.
static double sinh(double x)
          Hyperbolic sine.
static double tandeg(double degrees)
          Return the tangent of the given angle specified in degrees.
static double tanh(double x)
          Hyperbolic tangent.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MathUtilities

public MathUtilities()
Method Detail

randomInt

public static int randomInt(int a)
Return a random int r in the range between 0 and a inclusive.

Parameters:
a - an endpoint of a range

randomInt

public static int randomInt(int a,
                            int b)
Return a random int r in the range between a and b inclusive.

Parameters:
a - endpoint 1 of a range
b - endpoint 2 of a range

randomDouble

public static double randomDouble(double x)
Return a random double r in the range between 0 and x.

Parameters:
x - an endpoint of a range

randomDouble

public static double randomDouble(double x,
                                  double y)
Return a random double r in the range between x and y.

Parameters:
x - endpoint 1 of a range
y - endpoint 2 of a range

sindeg

public static double sindeg(double degrees)
Return the sine of the given angle specified in degrees.


cosdeg

public static double cosdeg(double degrees)
Return the cosine of the given angle specified in degrees.


tandeg

public static double tandeg(double degrees)
Return the tangent of the given angle specified in degrees.


asindeg

public static double asindeg(double value)
Return the arc sine in degrees of the specified input value in the range -1 to +1.


acosdeg

public static double acosdeg(double value)
Return the arc cosine in degrees of the specified input value in the range -1 to +1.


atandeg

public static double atandeg(double value)
Return the arc tangent in degrees of the specified input value.


atan2deg

public static double atan2deg(double y,
                              double x)
Return the polar angle in degrees of the point (x, y) where the y-coordinate is given first and the x-coordinate second.

Parameters:
y - the y-coordinate of the point
x - the x-coordinate of the point

sinh

public static double sinh(double x)

Hyperbolic sine.

Returns (exp(x) - 1/exp(x))/2 but computes the exponential only once.

Parameters:
x - the argument

cosh

public static double cosh(double x)

Hyperbolic cosine.

Returns (exp(x) + 1/exp(x))/2 but computes the exponential only once.

Parameters:
x - the argument

tanh

public static double tanh(double x)

Hyperbolic tangent.

Effectively returns sinh(x)/cosh(x) but computes only one exponential.

Parameters:
x - the argument

asinh

public static double asinh(double s)

Inverse hyperbolic sine.

Returns log(s + sqrt(s*s + 1)).

Parameters:
s - the sinh value to invert

acosh

public static double acosh(double c)

Inverse hyperbolic cosine.

Returns log(c + sqrt(c*c - 1)). This chooses the positive branch of acosh, that is, the return value is always >=0.

If c < 1 returns Nan.

Parameters:
c - the cosh value to invert

atanh

public static double atanh(double t)

Inverse hyperbolic tangent.

Returns log(sqrt((1 + t)/(1 - t))).

If abs(t) >= 1 returns Nan.

Parameters:
t - the tanh value to invert

GCD

public static int GCD(int a,
                      int b)

Returns the greatest common divisor of the int inputs a and b.

The return value will be positive or zero and will be zero only if both a and b are zero.


GCD

public static long GCD(long a,
                       long b)

Returns the greatest common divisor of the long inputs a and b.

The return value will be positive or zero and will be zero only if both a and b are zero.


LCM

public static int LCM(int a,
                      int b)

Returns the least common multiple of the int inputs a and b.

The return value will be positive or zero and will be zero only if both a and b are zero.


LCM

public static long LCM(long a,
                       long b)

Returns the least common multiple of the long inputs a and b.

The return value will be positive or zero and will be zero only if both a and b are zero.


modulus

public static int modulus(int number,
                          int base)

Returns the modulus of int number relative to int base.

Returns n such that n is congruent to number modulo base and 0 <= n < Math.abs(base).

Throws ArithmeticException if base is zero.


modulus

public static long modulus(long number,
                           long base)

Returns the modulus of long number relative to long base.

Returns n such that n is congruent to number modulo base and 0 <= n < Math.abs(base).

Throws ArithmeticException if base is zero.


power

public static double power(double x,
                           int n)

Returns the n-th power of x, xn, using the given integer exponent n.

Computes using ordinary arithmetic operations.

Returns 1.0 regardless of x if n equals 0.

Returns Double.NaN if n is negative and x is zero.

Note: Departs from the convention of the Java Math class by calling the method power rather than simply pow.

Note added in 2.5.0: This method uses only multiplication and division and does not use exp or log. Recents experiments showed that this method may be less accurate than calling Math.pow. Hence the use of this method is no longer recommended. It is retained in JPT to avoid breaking old code.

Parameters:
x - the base
n - the exponent

root

public static double root(double x,
                          double n)

Returns the n-th root of x.

If n is 0, returns NaN.

If x is 0 and n > 0, returns 0; otherwise returns NaN.

If x > 0, returns Math.pow(x,1.0/n).

If x < 0 and n is an odd integer, then, with z = -x, returns -Math.pow(z,1.0/n); otherwise, returns NaN.

Parameters:
x - the base
n - the inverse of the exponent

getTileIndexLimits

public static int[] getTileIndexLimits(double x,
                                       double w,
                                       double x0,
                                       double w0)

Returns an integer array with indices i and j that determine how a small interval from x0 to x0+w0 may be tiled to cover a large interval from x to x+w.

Specifically:

With these specifications, a loop on k, starting at k=i and running as long as k, will produce intervals (x0+k*w0,x0+k*w0+w0) that cover the interval (x,x+w).

This utility is used in AbstractPaintable to manage tiling but it is included here since it might have wider value.

This function returns null if:

  • w<=0
  • w0<=0
  • The computations do not produce values that fall into the integer range.

Although the discussion and parameter names focus on the horizontal direction, the method can clearly be used to find tiling indices for vertical tiling as well.

Parameters:
x - the left endpoint of the interval to cover
w - the width < 0 of the interval to cover
x0 - the left endpoint of the covering interval
w0 - the width < 0 of the covering interval