Implementation:Haifengl Smile MathEx
| Knowledge Sources | |
|---|---|
| Domains | Mathematics, Statistics, Numerical Computing |
| Last Updated | 2026-02-08 22:00 GMT |
Overview
MathEx is a large utility class providing an extensive collection of extra numeric functions for scalar, vector, and matrix operations, including random number generation, statistical computations, and root finding.
Description
The MathEx class in the smile.math package serves as the central math utility hub for the Smile library. It is a non-instantiable class (private constructor) containing only public static methods organized into several categories:
- Machine precision constants --
EPSILON,FLOAT_EPSILON,RADIX,DIGITS, etc., dynamically determined at startup - Scalar functions --
log2,log,log1pe,sigmoid,pow2,factorial,lfactorial,choose,lchoose,round,isPower2,isProbablePrime - Vector functions --
min,max,mean,sum,var,stdev(sd),cov, norms (norm1,norm2,normInf),normalize,unitize,cor, distance, dot product, histogram, element-wise operations (plus,minus,times,divide) - Matrix functions --
min,max,rowSums,colSums,rowMeans,colMeans,transpose,cov,cor - Random functions -- thread-safe random number generation via
random(),randomInt(),randomLong(),permutate(),randn() - Utility --
softmax, array concatenation (c(),cbind()),slice,omit,reverse,mode,unique,swap,isZero,equals
The class uses ThreadLocal<Random> for thread-safe random number generation with pre-defined seeds for reproducibility.
Usage
Use MathEx whenever you need basic mathematical and statistical operations in Smile. It is the foundational utility class imported throughout the library. Many methods are designed to work with primitive arrays for performance.
Code Reference
Source Location
- Repository: Haifengl_Smile
- File: base/src/main/java/smile/math/MathEx.java
- Lines: 1-4848
Signature
public class MathEx {
// Machine precision constants
public static final double EPSILON;
public static final float FLOAT_EPSILON;
public static final int RADIX;
public static final int DIGITS;
// Scalar functions
public static double log2(double x);
public static double log(double x);
public static double log1pe(double x);
public static double sigmoid(double x);
public static double pow2(double x);
public static boolean isPower2(int x);
public static boolean isProbablePrime(long n, int k);
public static double round(double x, int decimal);
public static double factorial(int n);
public static double lfactorial(int n);
public static double choose(int n, int k);
public static double lchoose(int n, int k);
// Random functions
public static double random();
public static double[] random(int n);
public static double random(double lo, double hi);
public static int randomInt(int n);
public static int randomInt(int lo, int hi);
public static long randomLong();
public static int[] permutate(int n);
public static void permutate(int[] x);
public static void setSeed(long seed);
// Vector functions
public static int min(int[] array);
public static double min(double[] array);
public static int max(int[] array);
public static double max(double[] array);
public static int whichMin(double[] array);
public static int whichMax(double[] array);
public static double mean(double[] array);
public static double sum(double[] array);
public static double var(double[] array);
public static double stdev(double[] array);
// Utility
public static int softmax(double[] posteriori);
public static boolean equals(double a, double b);
public static boolean isZero(double x);
public static void swap(int[] x, int i, int j);
public static int[] unique(int[] x);
// ... and many more
}
Import
import smile.math.MathEx;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| x, a, b | double, int, long |
Varies | Scalar values for math operations |
| array | int[], float[], double[] |
Varies | Arrays for vector operations (min, max, mean, sum, var, stdev, etc.) |
| matrix | double[][], int[][] |
Varies | Matrices for matrix operations (rowSums, colSums, transpose, etc.) |
| n, k | int |
Varies | Integer parameters for combinatorics, random generation, and primality testing |
| prob | double[] |
Varies | Probability weights for weighted random sampling |
Outputs
| Name | Type | Description |
|---|---|---|
| (scalar) | double, int, long, boolean |
Results of scalar operations (log2, factorial, min, max, etc.) |
| (array) | double[], int[] |
Results of vector operations (random, permutate, slice, etc.) |
| (matrix) | double[][] |
Results of matrix operations (transpose, randn, etc.) |
Usage Examples
Basic Scalar Operations
double logBase2 = MathEx.log2(1024); // 10.0
double sig = MathEx.sigmoid(0.0); // 0.5
double sq = MathEx.pow2(5.0); // 25.0
double comb = MathEx.choose(10, 3); // 120.0
double rounded = MathEx.round(3.14159, 2); // 3.14
Vector Statistics
double[] data = {1.0, 2.0, 3.0, 4.0, 5.0};
double min = MathEx.min(data); // 1.0
double max = MathEx.max(data); // 5.0
double avg = MathEx.mean(data); // 3.0
double sd = MathEx.stdev(data); // ~1.58
double total = MathEx.sum(data); // 15.0
Random Number Generation
MathEx.setSeed(42L);
double r = MathEx.random(); // uniform in [0, 1)
int ri = MathEx.randomInt(100); // uniform in [0, 100)
int[] perm = MathEx.permutate(10); // random permutation of 0..9
double[][] rn = MathEx.randn(5, 3); // 5x3 standard normal matrix
Floating-Point Comparison
boolean eq = MathEx.equals(1.0 / 3.0 * 3.0, 1.0); // true (within EPSILON)