Orbital library

orbital.math
Class MathUtilities

java.lang.Object
  extended by orbital.math.MathUtilities

public final class MathUtilities
extends java.lang.Object

This class contains basic mathematical utilities.

Properties: math precision settings
Property Name Property Value
orbital.math.MathUtilities.defaultPrecisionDigits the default number of precision digits (for display etc.).
orbital.math.MathUtilities.defaultTolerance the default tolerance for two numbers to be treated equal. (experimental)

Author:
André Platzer
See Also:
Math, Utility, AlgebraicAlgorithms, NumericalAlgorithms, Stat, Evaluations
Stereotype:
Utilities, Module

Method Summary
static double ceily(double a)
           
static double ceily(double a, double precision)
          Returns the smallest (closest to negative infinity) double value that is not less than the argument and is equal for at least a precision value.
static boolean equals(Arithmetic a, Arithmetic b, double tolerance)
           
static boolean equals(double a, double b, double tolerance)
          Checks whether two numbers are roughly equal.
static boolean even(int value)
          This function is true only when the value is an even number.
static long factorial(int n)
          n! factorial.
static double floory(double a)
           
static double floory(double a, double precision)
          Returns the largest (closest to positive infinity) double value that is not greater than the argument and is equal for at least a precision value.
static java.lang.String format(byte[] v)
           
static java.lang.String format(double v)
          formats a numeric double-value.
static java.lang.String format(double v, int precisionDigits)
          Formats a numeric double-value as a String representation with a specified number of precision digits.
static java.lang.String format(java.lang.Object o)
          Formats an object as a String representation.
static double fract(double a)
          Returns the fractional part of a double value that is in range of [0,1[ and is equal for all fractional digits.
static int gaussian(double a)
          Returns the Gaussian integer part of a double value.
static Euclidean[] gcd(Euclidean[] elements)
          Deprecated. Use AlgebraicAlgorithms.gcd(Euclidean[]) instead.
static Euclidean gcd(Euclidean a, Euclidean b)
          Deprecated. Use AlgebraicAlgorithms.gcd(Euclidean,Euclidean) instead.
static int gcd(int a, int b)
          Returns greatest common divisor (GCD) of two integers.
static java.math.BigInteger generatePrime(int strength, int certainty, java.util.Random randSource, boolean strongPrime)
          Generate a probable prime number.
static java.math.BigInteger generatePrime(int strength, java.util.Random randSource)
          Generate a true prime number.
static int getDefaultPrecisionDigits()
          Get the default number of precision digits (for display etc.).
static double getDefaultTolerance()
          Get the default tolerance for two numbers to be treated equal.
static Function getEqualizer()
          Deprecated. Since Orbital.1.1 use Values.getDefault().ValueFactory.getCoercer() instead.
static Arithmetic integrate(Function f, Arithmetic a, Arithmetic b)
          Returns ∫ab f dx.
static boolean isin(double value, double lower, double higher)
          Whether a value is in a specified range.
static boolean isin(int value, int lower, int higher)
          Whether a value is in a specified range.
static boolean isin(Real value, Real lower, Real higher)
          Whether a value is in a specified range.
static boolean isInteger(double value)
          Check whether the given value is an integer, only.
static boolean isPrime(java.math.BigInteger val)
          Really check whether a BigInteger is prime.
static boolean isProbability(double value)
          Check whether the given value is a probability value.
static Euclidean lcm(Euclidean a, Euclidean b)
          Deprecated. Use AlgebraicAlgorithms.lcm(Euclidean,Euclidean) instead.
static int lcm(int a, int b)
          Returns least common multiple (LCM) of two integers.
static int multinomial(int[] n)
          Multinomial coefficient which is a generalized binomial coefficient.
static int nCr(int n, int r)
          binomial coefficient (nr) = nCr number of combinations without repetition.
static int nPr(int n, int r)
          nPr number of permutations without repetition.
static boolean odd(int value)
          This function is true only when the value is an odd number.
static double precisionFor(double a)
          Get the precision for a default tolerance relative to the magnitude of a.
static double precisionFor(double a, double tolerance)
          Get the precision for a given a specified tolerance relative to the magnitude of a.
static int round(double a, int rounding_style)
          Returns the mathematically rounded part of a double value.
static double roundy(double a)
           
static double roundy(double a, double precision)
          Returns the closest double to the argument that is equal for at least a precision value.
static void setDefaultPrecisionDigits(int defaultPrecisionDigits)
          Set the default number of precision digits (for display etc.).
static void setDefaultTolerance(double defaultTolerance)
          Set the default tolerance for two numbers to be treated equal.
static void setEqualizer(Function equalizer)
          Deprecated. Since Orbital.1.1 use Values.getDefault().ValueFactory.setCoercer(orbital.logic.functor.Function) instead.
static int sign(double value)
          The signum of a number.
static int sign(int value)
           
static java.lang.String toRoman(short arabic)
          Converts arabic numbers to roman numbers.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

setDefaultPrecisionDigits

public static void setDefaultPrecisionDigits(int defaultPrecisionDigits)
Set the default number of precision digits (for display etc.).


getDefaultPrecisionDigits

public static int getDefaultPrecisionDigits()
Get the default number of precision digits (for display etc.).


setDefaultTolerance

public static void setDefaultTolerance(double defaultTolerance)
Set the default tolerance for two numbers to be treated equal. (experimental)

Preconditions:
defaultTolerance ≥ 0

getDefaultTolerance

public static double getDefaultTolerance()
Get the default tolerance for two numbers to be treated equal. (experimental)

Postconditions:
RES ≥ 0

isin

public static final boolean isin(int value,
                                 int lower,
                                 int higher)
Whether a value is in a specified range.

Returns:
value ∈ [lower, higher].

isin

public static final boolean isin(double value,
                                 double lower,
                                 double higher)
Whether a value is in a specified range.

Returns:
value ∈ [lower, higher].

isin

public static final boolean isin(Real value,
                                 Real lower,
                                 Real higher)
Whether a value is in a specified range.

Returns:
value ∈ [lower, higher].

even

public static final boolean even(int value)
This function is true only when the value is an even number.

Returns:
(value&1)==0.

odd

public static final boolean odd(int value)
This function is true only when the value is an odd number.

Returns:
(value&1)!=0.

sign

public static final int sign(double value)
The signum of a number.

Returns:
1 if value > 0, -1 if value < 0 and 0 if value = 0.

sign

public static final int sign(int value)

isProbability

public static boolean isProbability(double value)
Check whether the given value is a probability value. Probabilities range from 0 to 1.

Returns:
whether value is in range [0,1].

isInteger

public static boolean isInteger(double value)
Check whether the given value is an integer, only. Integers have no fractional part.


gcd

public static Euclidean gcd(Euclidean a,
                            Euclidean b)
Deprecated. Use AlgebraicAlgorithms.gcd(Euclidean,Euclidean) instead.


lcm

public static Euclidean lcm(Euclidean a,
                            Euclidean b)
Deprecated. Use AlgebraicAlgorithms.lcm(Euclidean,Euclidean) instead.


gcd

public static Euclidean[] gcd(Euclidean[] elements)
Deprecated. Use AlgebraicAlgorithms.gcd(Euclidean[]) instead.


gcd

public static int gcd(int a,
                      int b)
Returns greatest common divisor (GCD) of two integers.

See Also:
AlgebraicAlgorithms.gcd(int,int)

lcm

public static int lcm(int a,
                      int b)
Returns least common multiple (LCM) of two integers.

See Also:
AlgebraicAlgorithms.lcm(int,int)

generatePrime

public static java.math.BigInteger generatePrime(int strength,
                                                 int certainty,
                                                 java.util.Random randSource,
                                                 boolean strongPrime)
Generate a probable prime number. The BigInteger returned is prime with a certain probability depending on the value certainty.

Parameters:
strength - - bitlength of the result.
certainty - - the probability for being prime exceeds 1 - 1/2certainty.
randSource - - the Random-Source necessary to generate primes. For privacy should be an instance of SecureRandom.
strongPrime - - true to produce cryptographically strong primes where (p-1)/2 is prime again.
See Also:
BigInteger.BigInteger(int,int,Random)
Attributes:
Monte Carlo

generatePrime

public static java.math.BigInteger generatePrime(int strength,
                                                 java.util.Random randSource)
Generate a true prime number.

Parameters:
strength - - bitlength of the result.
randSource - - the Random-Source necessary to generate primes. For privacy should be an instance of SecureRandom.
Attributes:
Las Vegas

isPrime

public static boolean isPrime(java.math.BigInteger val)
Really check whether a BigInteger is prime. A number is prime, if its absolute is not evenly divisible by any number other than 1 (and itself of course). Runtime of this brute force implementation is long.

If you have all factors d that divide p evenly and are d≤√(p), then you can get the complements p/d. To test whether p is prime, you check all d that divide p evenly with d≤√(p)≤t≤p. If you cannot calculate squarer roots (as with BigNumbers) you must find a clever t. Since with p having bitlength n, then p≤2n+1, therefore √(p)≤2(n+1)/2=:t. So all d's that you need to check for divisibility are those with bitlength≤(n+1)/2.

The d's starting at 3 will be increased by 4, 2, 2, 2, and again 4, 2, 2, 2 because those numbers divisible by 2 and 5 are already checked for earlier.

See Also:
BigInteger.isProbablePrime(int)

factorial

public static long factorial(int n)
n! factorial.

Returns:
the factorial n! = {|1, (1+x)*y|} n = Γ(n+1).

nCr

public static int nCr(int n,
                      int r)
binomial coefficient (nr) = nCr number of combinations without repetition.

Returns:
(nr) := n! / (r! * (n-r)!).
See Also:
getCombinations(int, int, false)

nPr

public static int nPr(int n,
                      int r)
nPr number of permutations without repetition.

Returns:
nr := n! / (n-r)!
See Also:
getPermutations(int, int, false)

multinomial

public static int multinomial(int[] n)
Multinomial coefficient which is a generalized binomial coefficient.

See Also:
nCr(int, int)

round

public static int round(double a,
                        int rounding_style)
Returns the mathematically rounded part of a double value.


fract

public static double fract(double a)
Returns the fractional part of a double value that is in range of [0,1[ and is equal for all fractional digits.

Returns:
a - ⌊a⌋.
See Also:
Math.floor(double)

gaussian

public static int gaussian(double a)
Returns the Gaussian integer part of a double value. Its the largest integer that is smaller than or equal.

Returns:
⌈a⌉ := sup {z in Z | z<=a} == (int) Math.floor(a).
See Also:
Math.floor(double)

ceily

public static double ceily(double a,
                           double precision)
Returns the smallest (closest to negative infinity) double value that is not less than the argument and is equal for at least a precision value.

Returns:
⌈a/precision⌉*precision = Math.ceil(a/precision)*precision.
See Also:
floory(double, double)

ceily

public static double ceily(double a)

floory

public static double floory(double a,
                            double precision)
Returns the largest (closest to positive infinity) double value that is not greater than the argument and is equal for at least a precision value. MathUtilities.floory(a, .01) will result in a rounded down to a number formated as .##, while MathUtilities.floory(a, 100) will result in a rounded down to a number formated as several #s preceding######00..

Returns:
⌊a/precision⌋*precision = Math.floor(a/precision)*precision.
See Also:
ceily(double, double)

floory

public static double floory(double a)

roundy

public static double roundy(double a,
                            double precision)
Returns the closest double to the argument that is equal for at least a precision value.

Parameters:
a - a double value.
precision - a double value setting the precision. The smaller the precision, the more precise, the number returned, the larger the less precise and rounded earlier. Precisions above 1 will even round the non-fractional part.
Returns:
(double) Math.round(a / precision) * precision.

roundy

public static double roundy(double a)

precisionFor

public static double precisionFor(double a,
                                  double tolerance)
Get the precision for a given a specified tolerance relative to the magnitude of a.

Roughly gives the precision for tolerance percent of a, but adjusted to decimal digits.

Returns:
tolerance * 10⌈㏒10 a⌉.

precisionFor

public static double precisionFor(double a)
Get the precision for a default tolerance relative to the magnitude of a.


equals

public static boolean equals(double a,
                             double b,
                             double tolerance)
Checks whether two numbers are roughly equal.

Parameters:
tolerance - specifies how much a and b may differ to be treated as equal.
Returns:
a ≈ b. More precisely |a-b| < tolerance.
See Also:
Arithmetic.equals(Object,Real)

equals

public static boolean equals(Arithmetic a,
                             Arithmetic b,
                             double tolerance)

getEqualizer

public static final Function getEqualizer()
Deprecated. Since Orbital.1.1 use Values.getDefault().ValueFactory.getCoercer() instead.


setEqualizer

public static final void setEqualizer(Function equalizer)
                               throws java.lang.SecurityException
Deprecated. Since Orbital.1.1 use Values.getDefault().ValueFactory.setCoercer(orbital.logic.functor.Function) instead.

Throws:
java.lang.SecurityException

integrate

public static Arithmetic integrate(Function f,
                                   Arithmetic a,
                                   Arithmetic b)
Returns ∫ab f dx.

See Also:
NumericalAlgorithms.integrate(orbital.math.functional.Function, Arithmetic, Arithmetic)

format

public static java.lang.String format(double v,
                                      int precisionDigits)
Formats a numeric double-value as a String representation with a specified number of precision digits.


format

public static java.lang.String format(double v)
formats a numeric double-value.

See Also:
DefaultTolerance

format

public static java.lang.String format(byte[] v)

format

public static java.lang.String format(java.lang.Object o)
Formats an object as a String representation.

Does special handling for numbers, arithmetic objects, functors, for arrays, for two-dimensional arrays, etc.

See Also:
ArithmeticFormat

toRoman

public static java.lang.String toRoman(short arabic)
Converts arabic numbers to roman numbers.

Parameters:
arabic - the short value (in arabic chiffres) to be converted into roman chiffres. It must be an integer value between 1 (I) and 3999 (MMMCMXCIX).

Orbital library
1.3.0: 11 Apr 2009

Copyright © 1996-2009 André Platzer
All Rights Reserved.