SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.analysis.function
Class FunctionOps

java.lang.Object
  extended by com.numericalmethod.suanshu.analysis.function.FunctionOps

public class FunctionOps
extends java.lang.Object

This class collects some commonly used mathematical functions.


Constructor Summary
FunctionOps()
           
 
Method Summary
static double combination(int n, int k)
          Compute the combination function/binomial coefficient.
static double dotProduct(double[] x1, double[] x2)
          Compute the element-wise multiplication between two double[]s.
static long dotProduct(long[] x1, long[] x2)
          Compute the element-wise multiplication between two long[]s.
static double factorial(int n)
          Compute the n factorial.
static long mod(long x, long m)
          Compute the positive modulus of a number.
static long modpow(long base, long exponent, long modulus)
           be mod m This implements a variant of the algorithm on page 244 of Bruce Schneier's Applied Cryptography, 2e, ISBN 0-471-11709-9.
static double permutation(int n, int k)
          Compute the permutation function.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FunctionOps

public FunctionOps()
Method Detail

modpow

public static long modpow(long base,
                          long exponent,
                          long modulus)
be mod m
This implements a variant of the algorithm on page 244 of Bruce Schneier's Applied Cryptography, 2e, ISBN 0-471-11709-9.

We use the fact that

(a * b) mod m = ((a mod m) * (b mod m)) mod m

This code may fail without being noticed for very large numbers because overflow in integer operations does not throw any exception in the JVM.

Parameters:
base - positive integer b
exponent - positive integer e
modulus - positive integer m
Returns:
be mod m
Throws:
java.lang.IllegalArgumentException - if not all inputs are positive integers
See Also:

mod

public static long mod(long x,
                       long m)
Compute the positive modulus of a number.

If x is positive, we return x % m; otherwise, we return the smallest positive integer less than m, having the same modulo.

For example,

-1 mod 5 = 4

Parameters:
x - an integer
m - the modulus
Returns:
x mod m
See Also:

dotProduct

public static long dotProduct(long[] x1,
                              long[] x2)
Compute the element-wise multiplication between two long[]s. This operation is also called inner product when used in the context of vector space.

Parameters:
x1 - a long array
x2 - a long array
Returns:
x1 * x2
See Also:
Wikipedia: Dot product

dotProduct

public static double dotProduct(double[] x1,
                                double[] x2)
Compute the element-wise multiplication between two double[]s.

Parameters:
x1 - a double array
x2 - a double array
Returns:
x1 * x2
See Also:
Wikipedia: Dot product

factorial

public static double factorial(int n)
Compute the n factorial.

Parameters:
n - an integer
Returns:
n!
See Also:
Wikipedia: Factorial

combination

public static double combination(int n,
                                 int k)
Compute the combination function/binomial coefficient. It is the number of k-combinations (each of size k) from a set S with n elements (size n).

Parameters:
n - the size of the full set
k - the size of a combination
Returns:
n! / (n-k)! / k!
See Also:
Wikipedia: Combination

permutation

public static double permutation(int n,
                                 int k)
Compute the permutation function. It is the number of k-permutations (each of size k) from a set S with n elements (size n).

Parameters:
n - the size of the full set
k - the size of a permutation
Returns:
n! / (n-k)!
See Also:
Wikipedia: Permutation

SuanShu, a Java numerical and statistical library

Copyright © 2011 Numerical Method Inc. Ltd. All Rights Reserved.