Comprehensive linear algebra operations including dense and sparse vectors, dense matrices, and BLAS routines for high-performance mathematical computations. The linear algebra package provides the foundation for machine learning algorithms with optimized implementations.
Abstract base class for all vector types, providing common interface for mathematical operations on both dense and sparse vectors.
/**
* Base class for dense and sparse vector operations
*/
public abstract class Vector implements Serializable {
/** Get vector size */
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 to element at index */
public abstract void add(int i, double val);
/** L1 norm (sum of absolute values) */
public abstract double normL1();
/** Infinity norm (maximum absolute value) */
public abstract double normInf();
/** L2 norm (Euclidean norm) */
public abstract double normL2();
/** L2 norm squared */
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 by p-norm (mutable) */
public abstract void normalizeEqual(double p);
/** Standardize vector with mean and standard deviation (mutable) */
public abstract void standardizeEqual(double mean, double stdvar);
/** Add element to head of vector */
public abstract Vector prefix(double v);
/** Add element to tail of vector */
public abstract Vector append(double v);
/** Add vectors (immutable) */
public abstract Vector plus(Vector vec);
/** Subtract vectors (immutable) */
public abstract Vector minus(Vector vec);
/** Dot product */
public abstract double dot(Vector vec);
/** Get vector iterator */
public abstract VectorIterator iterator();
/** Extract subvector by indices */
public abstract Vector slice(int[] indexes);
/** Outer product with itself */
public abstract DenseMatrix outer();
}Dense vector implementation using double array storage for high-performance operations on fully populated vectors.
/**
* Dense vector implementation with double array storage
*/
public class DenseVector extends Vector {
/** Double array data storage */
public double[] data;
/** Empty constructor */
public DenseVector();
/** Size constructor with zero initialization */
public DenseVector(int n);
/** Data constructor from array */
public DenseVector(double[] data);
/** Get data array */
public double[] getData();
/** Set data array */
public void setData(double[] data);
/** Create ones vector */
public static DenseVector ones(int n);
/** Create zeros vector */
public static DenseVector zeros(int n);
/** Create random vector with uniform distribution [0,1) */
public static DenseVector rand(int n);
/** Clone vector */
public DenseVector clone();
/** Copy from another vector (mutable) */
public void setEqual(DenseVector other);
/** Add vector (mutable) */
public void plusEqual(Vector other);
/** Subtract vector (mutable) */
public void minusEqual(Vector other);
/** Add scaled vector: this += alpha * other (mutable) */
public void plusScaleEqual(Vector other, double alpha);
/** Outer product with another dense vector */
public DenseMatrix outer(DenseVector other);
}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);
DenseVector random = DenseVector.rand(4);
// Basic operations
double norm = v1.normL2(); // L2 norm
DenseVector scaled = v1.scale(2.0); // Scale by 2
DenseVector sum = v1.plus(v2); // Vector addition
double dotProduct = v1.dot(v2); // Dot product
// Mutable operations
v1.scaleEqual(0.5); // Scale in-place
v1.plusEqual(v2); // Add in-place
v1.normalizeEqual(2.0); // L2 normalize
// Matrix operations
DenseMatrix outer = v1.outer(v2); // Outer productSparse vector implementation using indices and values arrays for memory-efficient storage of vectors with many zero elements.
/**
* Sparse vector implementation with indices and values arrays
*/
public class SparseVector extends Vector {
/** Vector size */
public int n;
/** Non-zero indices */
public int[] indices;
/** Non-zero values */
public double[] values;
/** Empty constructor (undetermined size) */
public SparseVector();
/** Size constructor */
public SparseVector(int n);
/** Full constructor */
public SparseVector(int n, int[] indices, double[] values);
/** Map constructor */
public SparseVector(int n, Map<Integer, Double> kv);
/** Get indices array */
public int[] getIndices();
/** Get values array */
public double[] getValues();
/** Set vector size */
public void setSize(int n);
/** Get number of non-zero values */
public int numberOfValues();
/** Remove zero entries (cleanup) */
public void removeZeroValues();
/** Convert to dense vector */
public DenseVector toDenseVector();
/** Outer product with another sparse vector */
public DenseMatrix outer(SparseVector other);
/** Clone vector */
public SparseVector clone();
}Usage Examples:
import org.apache.flink.ml.common.linalg.SparseVector;
import java.util.HashMap;
import java.util.Map;
// Create sparse vectors
int[] indices = {0, 2, 4};
double[] values = {1.0, 3.0, 5.0};
SparseVector sparse1 = new SparseVector(10, indices, values);
// From map
Map<Integer, Double> map = new HashMap<>();
map.put(1, 2.0);
map.put(5, 7.0);
SparseVector sparse2 = new SparseVector(10, map);
// Operations
double norm = sparse1.normL2();
int nonZeros = sparse1.numberOfValues();
DenseVector dense = sparse1.toDenseVector();
// Cleanup
sparse1.removeZeroValues(); // Remove explicit zerosDense matrix implementation with column-major storage for efficient matrix operations and linear algebra computations.
/**
* Dense matrix implementation with column-major storage
*/
public class DenseMatrix implements Serializable {
/** Row dimension */
public int m;
/** Column dimension */
public int n;
/** Data array (column-major) */
public double[] data;
/** Dimensions constructor */
public DenseMatrix(int m, int n);
/** Data constructor (column-major) */
public DenseMatrix(int m, int n, double[] data);
/** Data constructor with layout specification */
public DenseMatrix(int m, int n, double[] data, boolean inRowMajor);
/** 2D array constructor */
public DenseMatrix(double[][] data);
/** Create identity matrix */
public static DenseMatrix eye(int n);
/** Create m×n identity matrix */
public static DenseMatrix eye(int m, int n);
/** Create zero matrix */
public static DenseMatrix zeros(int m, int n);
/** Create ones matrix */
public static DenseMatrix ones(int m, int n);
/** Create random matrix with uniform distribution [0,1) */
public static DenseMatrix rand(int m, int n);
/** Create random symmetric matrix */
public static DenseMatrix randSymmetric(int n);
/** 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 to element at (i,j) */
public void add(int i, int j, double s);
/** Get data array */
public double[] getData();
/** Get 2D array copy */
public double[][] getArrayCopy2D();
/** Get 1D array copy with layout specification */
public double[] getArrayCopy1D(boolean inRowMajor);
/** Get row as array */
public double[] getRow(int row);
/** Get column as array */
public double[] getColumn(int col);
/** Select rows by indices */
public DenseMatrix selectRows(int[] rows);
/** Get sub-matrix */
public DenseMatrix getSubMatrix(int m0, int m1, int n0, int n1);
/** Set sub-matrix */
public void setSubMatrix(DenseMatrix sub, int m0, int m1, int n0, int n1);
/** Check if square matrix */
public boolean isSquare();
/** Check if symmetric matrix */
public boolean isSymmetric();
/** Get row count */
public int numRows();
/** Get column count */
public int numCols();
/** Sum all elements */
public double sum();
/** Scale matrix by scalar (immutable) */
public DenseMatrix scale(double v);
/** Scale matrix by scalar (mutable) */
public void scaleEqual(double v);
/** Add matrices (immutable) */
public DenseMatrix plus(DenseMatrix mat);
/** Add scalar to all elements (immutable) */
public DenseMatrix plus(double alpha);
/** Add matrix (mutable) */
public void plusEquals(DenseMatrix mat);
/** Add scalar to all elements (mutable) */
public void plusEquals(double alpha);
/** Subtract matrices (immutable) */
public DenseMatrix minus(DenseMatrix mat);
/** Subtract matrix (mutable) */
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 transpose */
public DenseMatrix transpose();
/** Clone matrix */
public DenseMatrix clone();
}Usage Examples:
import org.apache.flink.ml.common.linalg.DenseMatrix;
import org.apache.flink.ml.common.linalg.DenseVector;
// Create matrices
DenseMatrix identity = DenseMatrix.eye(3);
DenseMatrix zeros = DenseMatrix.zeros(3, 4);
DenseMatrix random = DenseMatrix.rand(2, 2);
// From 2D array
double[][] array2d = {{1, 2}, {3, 4}};
DenseMatrix mat = new DenseMatrix(array2d);
// Basic operations
double element = mat.get(0, 1); // Get element
mat.set(1, 0, 5.0); // Set element
int rows = mat.numRows(); // Get dimensions
boolean square = mat.isSquare(); // Check properties
// Matrix operations
DenseMatrix scaled = mat.scale(2.0); // Scale
DenseMatrix sum = mat.plus(identity); // Addition
DenseMatrix product = mat.multiplies(identity); // Multiplication
DenseMatrix transposed = mat.transpose(); // Transpose
// Matrix-vector operations
DenseVector vector = new DenseVector(new double[]{1, 2});
DenseVector result = mat.multiplies(vector);Basic Linear Algebra Subprograms providing high-performance implementations of common linear algebra operations.
/**
* BLAS routines for vector and matrix operations
*/
public class BLAS {
/** Sum of absolute values (L1 norm) */
public static double asum(DenseVector x);
public static double asum(SparseVector x);
/** y += a*x (AXPY operation) */
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 */
public static double dot(DenseVector x, DenseVector y);
/** Scale: x = a*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);
/** General matrix multiplication: C = alpha*op(A)*op(B) + beta*C */
public static void gemm(double alpha, DenseMatrix matA, boolean transA,
DenseMatrix matB, boolean transB, double beta, DenseMatrix matC);
/** General matrix-vector multiplication: y = alpha*op(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);
}Usage Examples:
import org.apache.flink.ml.common.linalg.BLAS;
// Vector operations
double norm1 = BLAS.asum(vector1); // L1 norm
double dotProd = BLAS.dot(vector1, vector2); // Dot product
BLAS.scal(2.0, vector1); // Scale vector
BLAS.axpy(1.5, vector2, vector1); // vector1 += 1.5 * vector2
// Matrix operations
BLAS.scal(0.5, matrix); // Scale matrix
BLAS.axpy(2.0, matrixA, matrixB); // matrixB += 2.0 * matrixA
// Matrix multiplication: C = 2.0*A*B + 1.0*C
BLAS.gemm(2.0, matrixA, false, matrixB, false, 1.0, matrixC);
// Matrix-vector multiplication: y = 1.0*A*x + 0.0*y
BLAS.gemv(1.0, matrix, false, inputVector, 0.0, outputVector);Utility functions for vector parsing, formatting, and type conversion operations.
/**
* Utility methods for vector parsing and formatting
*/
public class VectorUtil {
/** Parse vector from string (auto-detects dense/sparse) */
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);
/** Convert vector to string */
public static String toString(Vector vector);
public static String toString(DenseVector denseVector);
public static String toString(SparseVector sparseVector);
}
/**
* Iterator interface for 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 Examples:
import org.apache.flink.ml.common.linalg.VectorUtil;
import org.apache.flink.ml.common.linalg.VectorIterator;
// Parse vectors from strings
Vector vec = VectorUtil.parse("1.0 2.0 3.0"); // Dense format
Vector sparse = VectorUtil.parse("(10,[1,3],[2.0,4.0])"); // Sparse format
// Convert to strings
String vecStr = VectorUtil.toString(vec);
// Iterate over vector elements
VectorIterator iter = vec.iterator();
while (iter.hasNext()) {
iter.next();
int index = iter.getIndex();
double value = iter.getValue();
System.out.println("Index: " + index + ", Value: " + value);
}