Comprehensive machine learning library for Apache Flink that enables scalable ML pipelines on distributed stream processing platform.
—
Comprehensive linear algebra library with dense and sparse vectors, matrices, and BLAS operations. Essential for numerical computations in ML algorithms with high-performance native integration.
Base vector abstraction with dense and sparse implementations for efficient numerical operations.
/**
* Base class for vector implementations with common operations
* Supports both dense and sparse representations
*/
public abstract class Vector implements Serializable {
/** Get vector dimension */
public abstract int size();
/** Get element at index */
public abstract double get(int i);
/** Set element at index */
public abstract void set(int i, double val);
/** Add value to element at index */
public abstract void add(int i, double val);
/** Compute L1 norm (sum of absolute values) */
public abstract double normL1();
/** Compute infinity norm (maximum absolute value) */
public abstract double normInf();
/** Compute L2 norm (Euclidean length) */
public abstract double normL2();
/** Compute squared L2 norm */
public abstract double normL2Square();
/** Scale vector by scalar (immutable) */
public abstract Vector scale(double v);
/** Scale vector by scalar (mutable) */
public abstract void scaleEqual(double v);
/** Normalize vector in-place with p-norm */
public abstract void normalizeEqual(double p);
/** Standardize vector in-place with mean and standard deviation */
public abstract void standardizeEqual(double mean, double stdvar);
/** Prepend element to create new vector */
public abstract Vector prefix(double v);
/** Append element to create new vector */
public abstract Vector append(double v);
/** Add vectors element-wise */
public abstract Vector plus(Vector vec);
/** Subtract vectors element-wise */
public abstract Vector minus(Vector vec);
/** Compute dot product with another vector */
public abstract double dot(Vector vec);
/** Get iterator for efficient traversal */
public abstract VectorIterator iterator();
/** Extract subvector with specified indices */
public abstract Vector slice(int[] indexes);
/** Compute outer product to create matrix */
public abstract DenseMatrix outer();
}/**
* Dense vector implementation storing all elements in array
*/
public class DenseVector extends Vector {
/** Create zero vector of size n */
public DenseVector(int n);
/** Create vector from data array */
public DenseVector(double[] data);
/** Get underlying data array */
public double[] getData();
/** Set underlying data array */
public void setData(double[] data);
/** Copy data from another dense vector */
public void setEqual(DenseVector other);
/** Add another vector in-place */
public void plusEqual(Vector other);
/** Subtract another vector in-place */
public void minusEqual(Vector other);
/** Add scaled vector in-place: this += alpha * other */
public void plusScaleEqual(Vector other, double alpha);
/** Compute outer product with another dense vector */
public DenseMatrix outer(DenseVector other);
/** Create deep copy */
public DenseVector clone();
// Static factory methods
/** Create vector of all ones */
public static DenseVector ones(int n);
/** Create vector of all zeros */
public static DenseVector zeros(int n);
/** Create random vector with uniform distribution */
public static DenseVector rand(int n);
}Usage Examples:
import org.apache.flink.ml.common.linalg.DenseVector;
// Create vectors
DenseVector v1 = new DenseVector(new double[]{1.0, 2.0, 3.0});
DenseVector v2 = DenseVector.ones(3);
DenseVector zeros = DenseVector.zeros(5);
// Basic operations
double dot = v1.dot(v2); // 6.0
double norm = v1.normL2(); // 3.741...
DenseVector scaled = v1.scale(2.0); // [2.0, 4.0, 6.0]
// In-place operations
v1.plusEqual(v2); // v1 becomes [2.0, 3.0, 4.0]
v1.scaleEqual(0.5); // v1 becomes [1.0, 1.5, 2.0]
// Vector algebra
DenseVector v3 = v1.plus(v2); // Element-wise addition
DenseVector v4 = v1.minus(v2); // Element-wise subtraction/**
* Sparse vector implementation storing only non-zero elements
* Efficient for vectors with many zero elements
*/
public class SparseVector extends Vector {
/** Create empty sparse vector with undetermined size */
public SparseVector();
/** Create empty sparse vector with determined size */
public SparseVector(int n);
/** Create sparse vector from indices and values arrays */
public SparseVector(int n, int[] indices, double[] values);
/** Create sparse vector from map of index->value pairs */
public SparseVector(int n, Map<Integer, Double> kv);
/** Get indices of non-zero elements */
public int[] getIndices();
/** Get values of non-zero elements */
public double[] getValues();
/** Set vector size */
public void setSize(int n);
/** Get number of non-zero values stored */
public int numberOfValues();
/** Remove elements that are zero */
public void removeZeroValues();
/** Convert to dense vector representation */
public DenseVector toDenseVector();
/** Compute outer product with another sparse vector */
public DenseMatrix outer(SparseVector other);
/** Create deep copy */
public SparseVector clone();
}Usage Examples:
import org.apache.flink.ml.common.linalg.SparseVector;
import java.util.HashMap;
import java.util.Map;
// Create sparse vector from indices/values
int[] indices = {0, 2, 4};
double[] values = {1.0, 3.0, 5.0};
SparseVector sparse1 = new SparseVector(5, indices, values);
// Create from map
Map<Integer, Double> map = new HashMap<>();
map.put(1, 2.0);
map.put(3, 4.0);
SparseVector sparse2 = new SparseVector(5, map);
// Inspect sparse structure
int nonZeros = sparse1.numberOfValues(); // 3
int[] idx = sparse1.getIndices(); // [0, 2, 4]
double[] vals = sparse1.getValues(); // [1.0, 3.0, 5.0]
// Convert to dense if needed
DenseVector dense = sparse1.toDenseVector(); // [1.0, 0.0, 3.0, 0.0, 5.0]Dense matrix implementation with comprehensive linear algebra operations.
/**
* Dense matrix implementation with column-major storage
* Provides comprehensive linear algebra operations
*/
public class DenseMatrix implements Serializable {
/** Create zero matrix of size m x n */
public DenseMatrix(int m, int n);
/** Create matrix from data array in column-major order */
public DenseMatrix(int m, int n, double[] data);
/** Create matrix from data array with specified layout */
public DenseMatrix(int m, int n, double[] data, boolean inRowMajor);
/** Create matrix from 2D array */
public DenseMatrix(double[][] data);
// Element access
/** Get element at (i, j) */
public double get(int i, int j);
/** Set element at (i, j) */
public void set(int i, int j, double s);
/** Add value to element at (i, j) */
public void add(int i, int j, double s);
// Data access
/** Get underlying data array (column-major) */
public double[] getData();
/** Get copy as 2D array */
public double[][] getArrayCopy2D();
/** Get copy as 1D array with specified layout */
public double[] getArrayCopy1D(boolean inRowMajor);
/** Get copy of specific row */
public double[] getRow(int row);
/** Get copy of specific column */
public double[] getColumn(int col);
// Matrix operations
/** Create transpose of matrix */
public DenseMatrix transpose();
/** Scale matrix by scalar (immutable) */
public DenseMatrix scale(double v);
/** Scale matrix by scalar (mutable) */
public void scaleEqual(double v);
/** Add matrices */
public DenseMatrix plus(DenseMatrix mat);
/** Add scalar to all elements */
public DenseMatrix plus(double alpha);
/** Add matrix in-place */
public void plusEquals(DenseMatrix mat);
/** Add scalar to all elements in-place */
public void plusEquals(double alpha);
/** Subtract matrices */
public DenseMatrix minus(DenseMatrix mat);
/** Subtract matrix in-place */
public void minusEquals(DenseMatrix mat);
/** Matrix multiplication */
public DenseMatrix multiplies(DenseMatrix mat);
/** Matrix-vector multiplication */
public DenseVector multiplies(DenseVector x);
/** Matrix-sparse vector multiplication */
public DenseVector multiplies(SparseVector x);
// Matrix properties
/** Check if matrix is square */
public boolean isSquare();
/** Check if matrix is symmetric */
public boolean isSymmetric();
/** Get number of rows */
public int numRows();
/** Get number of columns */
public int numCols();
/** Sum all elements */
public double sum();
// Submatrix operations
/** Select specific rows */
public DenseMatrix selectRows(int[] rows);
/** Get submatrix */
public DenseMatrix getSubMatrix(int m0, int m1, int n0, int n1);
/** Set submatrix */
public void setSubMatrix(DenseMatrix sub, int m0, int m1, int n0, int n1);
/** Create deep copy */
public DenseMatrix clone();
// Static factory methods
/** Create identity matrix */
public static DenseMatrix eye(int n);
/** Create identity-like matrix (m x n) */
public static DenseMatrix eye(int m, int n);
/** Create zero matrix */
public static DenseMatrix zeros(int m, int n);
/** Create matrix of all ones */
public static DenseMatrix ones(int m, int n);
/** Create random matrix */
public static DenseMatrix rand(int m, int n);
/** Create random symmetric matrix */
public static DenseMatrix randSymmetric(int n);
}Usage Examples:
import org.apache.flink.ml.common.linalg.DenseMatrix;
import org.apache.flink.ml.common.linalg.DenseVector;
// Create matrices
DenseMatrix A = new DenseMatrix(new double[][]{
{1.0, 2.0, 3.0},
{4.0, 5.0, 6.0}
}); // 2x3 matrix
DenseMatrix I = DenseMatrix.eye(3); // 3x3 identity
DenseMatrix zeros = DenseMatrix.zeros(2, 4);
DenseMatrix random = DenseMatrix.rand(3, 3);
// Basic operations
double element = A.get(0, 1); // 2.0
A.set(1, 2, 10.0); // Set element to 10.0
DenseMatrix At = A.transpose(); // Transpose
// Matrix arithmetic
DenseMatrix B = DenseMatrix.ones(2, 3);
DenseMatrix sum = A.plus(B); // Matrix addition
DenseMatrix scaled = A.scale(2.0); // Scalar multiplication
// Matrix-vector multiplication
DenseVector x = new DenseVector(new double[]{1.0, 2.0, 3.0});
DenseVector Ax = A.multiplies(x); // Matrix-vector product
// Matrix-matrix multiplication
DenseMatrix C = DenseMatrix.rand(3, 4);
DenseMatrix AC = A.multiplies(C); // 2x4 result
// Properties
boolean isSquare = A.isSquare(); // false (2x3)
int rows = A.numRows(); // 2
int cols = A.numCols(); // 3
double total = A.sum(); // Sum of all elementsHigh-performance Basic Linear Algebra Subprograms (BLAS) operations with native library integration.
/**
* BLAS (Basic Linear Algebra Subprograms) operations
* Provides optimized implementations for common linear algebra operations
*/
public class BLAS {
/** Native BLAS instance for best performance */
public static final com.github.fommil.netlib.BLAS NATIVE_BLAS;
/** Fallback F2J BLAS instance */
public static final com.github.fommil.netlib.BLAS F2J_BLAS;
// Level-1 BLAS operations (vector-vector)
/** Sum of absolute values: ||x||_1 */
public static double asum(int n, double[] x, int offset);
public static double asum(DenseVector x);
public static double asum(SparseVector x);
/** Scaled vector addition: y += a*x */
public static void axpy(double a, double[] x, double[] y);
public static void axpy(double a, DenseVector x, DenseVector y);
public static void axpy(double a, SparseVector x, DenseVector y);
public static void axpy(double a, DenseMatrix x, DenseMatrix y);
/** Dot product: x^T * y */
public static double dot(double[] x, double[] y);
public static double dot(DenseVector x, DenseVector y);
/** Scale vector: x *= a */
public static void scal(double a, double[] x);
public static void scal(double a, DenseVector x);
public static void scal(double a, SparseVector x);
public static void scal(double a, DenseMatrix x);
// Level-2 BLAS operations (matrix-vector)
/** General matrix-vector multiplication: y = alpha*A*x + beta*y */
public static void gemv(double alpha, DenseMatrix matA, boolean transA,
DenseVector x, double beta, DenseVector y);
public static void gemv(double alpha, DenseMatrix matA, boolean transA,
SparseVector x, double beta, DenseVector y);
// Level-3 BLAS operations (matrix-matrix)
/** General matrix-matrix multiplication: C = alpha*A*B + beta*C */
public static void gemm(double alpha, DenseMatrix matA, boolean transA,
DenseMatrix matB, boolean transB,
double beta, DenseMatrix matC);
}Usage Examples:
import org.apache.flink.ml.common.linalg.BLAS;
// Vector operations
DenseVector x = new DenseVector(new double[]{1.0, 2.0, 3.0});
DenseVector y = new DenseVector(new double[]{4.0, 5.0, 6.0});
// Compute dot product
double dotProd = BLAS.dot(x, y); // 32.0
// Scale vector in-place
BLAS.scal(2.0, x); // x becomes [2.0, 4.0, 6.0]
// Scaled vector addition: y += 0.5 * x
BLAS.axpy(0.5, x, y); // y becomes [5.0, 7.0, 9.0]
// Matrix-vector multiplication
DenseMatrix A = DenseMatrix.rand(3, 3);
DenseVector b = DenseVector.zeros(3);
// b = 1.0 * A * x + 0.0 * b (i.e., b = A * x)
BLAS.gemv(1.0, A, false, x, 0.0, b);
// Matrix-matrix multiplication
DenseMatrix B = DenseMatrix.rand(3, 3);
DenseMatrix C = DenseMatrix.zeros(3, 3);
// C = 1.0 * A * B + 0.0 * C (i.e., C = A * B)
BLAS.gemm(1.0, A, false, B, false, 0.0, C);Additional utilities for vector operations and serialization.
/**
* Matrix and vector operations utility class
*/
public class MatVecOp {
/** Vector addition */
public static Vector plus(Vector vec1, Vector vec2);
/** Vector subtraction */
public static Vector minus(Vector vec1, Vector vec2);
/** Dot product */
public static double dot(Vector vec1, Vector vec2);
/** L1 distance between vectors */
public static double sumAbsDiff(Vector vec1, Vector vec2);
/** Squared L2 distance between vectors */
public static double sumSquaredDiff(Vector vec1, Vector vec2);
// Element-wise operations (various apply methods for matrices and vectors)
}/**
* Vector parsing and serialization utilities
*/
public class VectorUtil {
/** Element delimiter character */
public static final char ELEMENT_DELIMITER = ' ';
/** Size header delimiter character */
public static final char HEADER_DELIMITER = '$';
/** Index-value delimiter for sparse vectors */
public static final char INDEX_VALUE_DELIMITER = ':';
/** Parse vector from string representation */
public static Vector parse(String str);
/** Parse dense vector from string */
public static DenseVector parseDense(String str);
/** Parse sparse vector from string */
public static SparseVector parseSparse(String str);
/** Serialize vector to string */
public static String toString(Vector vector);
/** Serialize sparse vector to string */
public static String toString(SparseVector sparseVector);
/** Serialize dense vector to string */
public static String toString(DenseVector denseVector);
}Usage Examples:
import org.apache.flink.ml.common.linalg.VectorUtil;
// Create vectors
DenseVector dense = new DenseVector(new double[]{1.0, 2.0, 3.0});
SparseVector sparse = new SparseVector(5, new int[]{0, 2, 4}, new double[]{1.0, 3.0, 5.0});
// Serialize to strings
String denseStr = VectorUtil.toString(dense); // "1.0 2.0 3.0"
String sparseStr = VectorUtil.toString(sparse); // "$5$0:1.0 2:3.0 4:5.0"
// Parse from strings
DenseVector parsedDense = VectorUtil.parseDense(denseStr);
SparseVector parsedSparse = VectorUtil.parseSparse(sparseStr);
Vector parsed = VectorUtil.parse(denseStr); // Auto-detects type/**
* Iterator for efficiently traversing vector elements
*/
public interface VectorIterator extends Serializable {
/** Check if more elements available */
boolean hasNext();
/** Move to next element */
void next();
/** Get current element index */
int getIndex();
/** Get current element value */
double getValue();
}Usage Example:
SparseVector sparse = new SparseVector(10, new int[]{1, 5, 8}, new double[]{2.0, 4.0, 6.0});
// Iterate over non-zero elements
VectorIterator iter = sparse.iterator();
while (iter.hasNext()) {
iter.next();
int index = iter.getIndex();
double value = iter.getValue();
System.out.println("sparse[" + index + "] = " + value);
}
// Output:
// sparse[1] = 2.0
// sparse[5] = 4.0
// sparse[8] = 6.0Basic statistical operations for multivariate data.
/**
* Multivariate Gaussian (Normal) Distribution
* Provides probability density function calculations
*/
public class MultivariateGaussian {
/** LAPACK instance for linear algebra */
public static final LAPACK LAPACK_INST;
/** BLAS instance for basic operations */
public static final com.github.fommil.netlib.BLAS F2J_BLAS_INST;
/** Machine epsilon for numerical stability */
public static final double EPSILON;
/** Create multivariate Gaussian with mean and covariance */
public MultivariateGaussian(DenseVector mean, DenseMatrix cov);
/** Compute probability density function at point x */
public double pdf(Vector x);
/** Compute log probability density function at point x */
public double logpdf(Vector x);
}Usage Example:
import org.apache.flink.ml.common.statistics.basicstatistic.MultivariateGaussian;
// Define 2D Gaussian distribution
DenseVector mean = new DenseVector(new double[]{0.0, 0.0});
DenseMatrix cov = new DenseMatrix(new double[][]{
{1.0, 0.5},
{0.5, 1.0}
});
MultivariateGaussian gaussian = new MultivariateGaussian(mean, cov);
// Evaluate probability density
DenseVector point = new DenseVector(new double[]{1.0, 1.0});
double density = gaussian.pdf(point);
double logDensity = gaussian.logpdf(point);Install with Tessl CLI
npx tessl i tessl/maven-org-apache-flink--flink-ml-uber-2-11