Class RandomSparseTensor

java.lang.Object
org.flag4j.rng.RandomSparseTensor

public class RandomSparseTensor extends Object
A utility class for generating random sparse tensors and matrices with customizable sparsity and distributions.

Features:

  • Generate sparse matrices and tensors with specified sparsity levels.
  • Support for uniform and annular (complex valued) distributions for non-zero values.
  • Create symmetric sparse matrices.

Example Usage:


     RandomSparseTensor generator = new RandomSparseTensor(12345L);
     CooMatrix sparseMatrix = generator.randomCooMatrix(100, 100, 0, 10, 0.95);
     CsrMatrix csrMatrix = generator.randomCsrMatrix(new Shape(50, 50), 0, 1, 0.9);
 
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new pseudorandom tensor generator with a seed which is unlikely to be the same as other from any other invocation of this constructor.
    RandomSparseTensor(long seed)
    Constructs a pseudorandom tensor generator with a specified seed.
  • Method Summary

    Modifier and Type
    Method
    Description
    randomCooMatrix(int rows, int cols, double min, double max, double sparsity)
    Generates a random sparse matrix with the specified sparsity.
    randomCooMatrix(int rows, int cols, double min, double max, int numNonZeroEntries)
    Generates a random sparse matrix with the specified number of non-zero data.
    randomCooMatrix(Shape shape, double min, double max, double sparsity)
    Generates a random sparse matrix with the specified sparsity.
    randomCooMatrix(Shape shape, double min, double max, int numNonZeroEntries)
    Generates a random sparse matrix with the specified number of non-zero data.
    randomCsrMatrix(int rows, int cols, double min, double max, double sparsity)
    Generates a random sparse matrix with the specified sparsity.
    randomCsrMatrix(int rows, int cols, double min, double max, int numNonZeroEntries)
    Generates a random sparse matrix with the specified number of non-zero data.
    randomCsrMatrix(Shape shape, double min, double max, double sparsity)
    Generates a random sparse matrix with the specified sparsity.
    randomCsrMatrix(Shape shape, double min, double max, int numNonZeroEntries)
    Generates a random sparse matrix with the specified number of non-zero data.
    randomSparseCMatrix(int rows, int cols, double min, double max, double sparsity)
    Generates a random sparse matrix with the specified sparsity.
    randomSparseCMatrix(int rows, int cols, double min, double max, int numNonZeroEntries)
    Generates a random sparse matrix with the specified number of non-zero data.
    randomSparseCMatrix(Shape shape, double min, double max, double sparsity)
    Generates a random sparse matrix with the specified sparsity.
    randomSparseCMatrix(Shape shape, double min, double max, int numNonZeroEntries)
    Generates a random sparse matrix with the specified number of non-zero data.
    randomSymmetricCooMatrix(int size, int min, int max, double sparsity)
    Generates a symmetric COO matrix filled with pseudorandom values uniformly distributed in [min, max).
    randomSymmetricCsrMatrix(int size, int min, int max, double sparsity)
    Generates a symmetric CSR matrix filled with pseudorandom values uniformly distributed in [min, max).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • RandomSparseTensor

      public RandomSparseTensor()
      Constructs a new pseudorandom tensor generator with a seed which is unlikely to be the same as other from any other invocation of this constructor.
    • RandomSparseTensor

      public RandomSparseTensor(long seed)
      Constructs a pseudorandom tensor generator with a specified seed. Use this constructor for reproducible results.
      Parameters:
      seed - Seed of the pseudorandom tensor generator.
  • Method Details

    • randomCooMatrix

      public CooMatrix randomCooMatrix(int rows, int cols, double min, double max, double sparsity)
      Generates a random sparse matrix with the specified sparsity. The non-zero values will have a uniform distribution in [min, max). Values will be uniformly distributed throughout the matrix.
      Parameters:
      rows - Number of rows in the sparse matrix.
      cols - Number of columns in the sparse matrix.
      min - Minimum value for random non-zero values in the sparse matrix.
      max - Maximum value for random non-zero values
      sparsity - Desired sparsity of the resulting matrix. i.e. the percent of values which are zero. Must be a value in [0.0, 1.0].
      Returns:
      A sparse matrix with sparsity approximately equal to sparsity filled with random values uniformly distributed in [min, max).
    • randomCooMatrix

      public CooMatrix randomCooMatrix(Shape shape, double min, double max, double sparsity)
      Generates a random sparse matrix with the specified sparsity. The non-zero values will have a uniform distribution in [min, max). Values will be uniformly distributed throughout the matrix.
      Parameters:
      shape - Shape of the sparse matrix to generate.
      min - Minimum value for random non-zero values in the sparse matrix.
      max - Maximum value for random non-zero values
      sparsity - Desired sparsity of the resulting matrix. i.e. the percent of values which are zero. Must be a value in [0.0, 1.0].
      Returns:
      A sparse matrix with sparsity approximately equal to sparsity filled with random values uniformly distributed in [min, max).
    • randomCooMatrix

      public CooMatrix randomCooMatrix(int rows, int cols, double min, double max, int numNonZeroEntries)
      Generates a random sparse matrix with the specified number of non-zero data. The non-zero values will have a uniform distribution in [min, max). Values will be uniformly distributed throughout the matrix.
      Parameters:
      rows - Number of rows in the random sparse matrix.
      cols - Number of columns in the random sparse matrix.
      min - Minimum value for random non-zero values in the sparse matrix.
      max - Maximum value for random non-zero values
      numNonZeroEntries - Desired number of non-zero data int the random sparse matrix.
      Returns:
      A sparse matrix filled with the specified number of non-zero data uniformly distributed in [min, max).
    • randomCooMatrix

      public CooMatrix randomCooMatrix(Shape shape, double min, double max, int numNonZeroEntries)
      Generates a random sparse matrix with the specified number of non-zero data. The non-zero values will have a uniform distribution in [min, max). Values will be uniformly distributed throughout the matrix.
      Parameters:
      shape - Shape of the sparse matrix to generate.
      min - Minimum value for random non-zero values in the sparse matrix.
      max - Maximum value for random non-zero values
      numNonZeroEntries - Desired number of non-zero data int the random sparse matrix.
      Returns:
      A sparse matrix filled with the specified number of non-zero data uniformly distributed in [min, max).
    • randomCsrMatrix

      public CsrMatrix randomCsrMatrix(int rows, int cols, double min, double max, double sparsity)
      Generates a random sparse matrix with the specified sparsity. The non-zero values will have a uniform distribution in [min, max). Values will be uniformly distributed throughout the matrix.
      Parameters:
      rows - Number of rows in the sparse matrix.
      cols - Number of columns in the sparse matrix.
      min - Minimum value for random non-zero values in the sparse matrix.
      max - Maximum value for random non-zero values
      sparsity - Desired sparsity of the resulting matrix. i.e. the percent of values which are zero. Must be a value in [0.0, 1.0].
      Returns:
      A sparse matrix with sparsity approximately equal to sparsity filled with random values uniformly distributed in [min, max).
    • randomCsrMatrix

      public CsrMatrix randomCsrMatrix(Shape shape, double min, double max, double sparsity)
      Generates a random sparse matrix with the specified sparsity. The non-zero values will have a uniform distribution in [min, max). Values will be uniformly distributed throughout the matrix.
      Parameters:
      shape - Shape of the sparse matrix to generate.
      min - Minimum value for random non-zero values in the sparse matrix.
      max - Maximum value for random non-zero values
      sparsity - Desired sparsity of the resulting matrix. i.e. the percent of values which are zero. Must be a value in [0.0, 1.0].
      Returns:
      A sparse matrix with sparsity approximately equal to sparsity filled with random values uniformly distributed in [min, max).
    • randomCsrMatrix

      public CsrMatrix randomCsrMatrix(int rows, int cols, double min, double max, int numNonZeroEntries)
      Generates a random sparse matrix with the specified number of non-zero data. The non-zero values will have a uniform distribution in [min, max). Values will be uniformly distributed throughout the matrix.
      Parameters:
      rows - Number of rows in the random sparse matrix.
      cols - Number of columns in the random sparse matrix.
      min - Minimum value for random non-zero values in the sparse matrix.
      max - Maximum value for random non-zero values
      numNonZeroEntries - Desired number of non-zero data int the random sparse matrix.
      Returns:
      A sparse matrix filled with the specified number of non-zero data uniformly distributed in [min, max).
    • randomCsrMatrix

      public CsrMatrix randomCsrMatrix(Shape shape, double min, double max, int numNonZeroEntries)
      Generates a random sparse matrix with the specified number of non-zero data. The non-zero values will have a uniform distribution in [min, max). Values will be uniformly distributed throughout the matrix.
      Parameters:
      shape - Shape of the sparse matrix to generate.
      min - Minimum value for random non-zero values in the sparse matrix.
      max - Maximum value for random non-zero values
      numNonZeroEntries - Desired number of non-zero data int the random sparse matrix.
      Returns:
      A sparse matrix filled with the specified number of non-zero data uniformly distributed in [min, max).
    • randomSymmetricCooMatrix

      public CooMatrix randomSymmetricCooMatrix(int size, int min, int max, double sparsity)
      Generates a symmetric COO matrix filled with pseudorandom values uniformly distributed in [min, max).
      Parameters:
      size - Number of rows and columns in the resulting matrix (the result will be a square matrix).
      min - Minimum value in uniform distribution.
      max - Maximum value in uniform distribution.
      sparsity - Desired sparsity of the resulting matrix. i.e. the percent of values which are zero. Must be a value in [0.0, 1.0]. The true sparsity may slightly differ to ensure the matrix is symmetric.
      Returns:
      A symmetric matrix filled with pseudorandom values uniformly distributed in [min, max).
      Throws:
      IllegalArgumentException - If sparsity is not in the range [0.0, 1.0].
    • randomSymmetricCsrMatrix

      public CsrMatrix randomSymmetricCsrMatrix(int size, int min, int max, double sparsity)
      Generates a symmetric CSR matrix filled with pseudorandom values uniformly distributed in [min, max).
      Parameters:
      size - Number of rows and columns in the resulting matrix (the result will be a square matrix).
      min - Minimum value in uniform distribution.
      max - Maximum value in uniform distribution.
      sparsity - Desired sparsity of the resulting matrix. i.e. the percent of values which are zero. Must be a value in [0.0, 1.0]. The true sparsity may slightly differ to ensure the matrix is symmetric.
      Returns:
      A symmetric matrix filled with pseudorandom values uniformly distributed in [min, max).
      Throws:
      IllegalArgumentException - If sparsity is not in the range [0.0, 1.0].
    • randomSparseCMatrix

      public CooCMatrix randomSparseCMatrix(int rows, int cols, double min, double max, double sparsity)
      Generates a random sparse matrix with the specified sparsity. The non-zero values will have a uniform distribution in the annulus (i.e. washer) with inner radius min (inclusive) and outer radius max (exclusive). Values will be uniformly distributed throughout the matrix.
      Parameters:
      rows - Number of rows in the sparse matrix.
      cols - Number of columns in the sparse matrix.
      min - Inner radius of the annular distribution (inclusive).
      max - Outer radius of the annular distribution (exclusive).
      sparsity - Desired sparsity of the resulting matrix. i.e. the percent of values which are zero. Must be a value in [0.0, 1.0].
      Returns:
      A sparse matrix with sparsity approximately equal to sparsity filled with random values uniformly distributed in an annulus.
    • randomSparseCMatrix

      public CooCMatrix randomSparseCMatrix(Shape shape, double min, double max, double sparsity)
      Generates a random sparse matrix with the specified sparsity. The non-zero values will have a uniform distribution in the annulus (i.e. washer) with inner radius min (inclusive) and outer radius max (exclusive). Values will be uniformly distributed throughout the matrix.
      Parameters:
      shape - Shape of the sparse matrix to generate.
      min - Inner radius of the annular distribution (inclusive).
      max - Outer radius of the annular distribution (exclusive).
      sparsity - Desired sparsity of the resulting matrix. i.e. the percent of values which are zero. Must be a value in [0.0, 1.0].
      Returns:
      A sparse matrix with sparsity approximately equal to sparsity filled with random values uniformly distributed in an annulus.
    • randomSparseCMatrix

      public CooCMatrix randomSparseCMatrix(int rows, int cols, double min, double max, int numNonZeroEntries)
      Generates a random sparse matrix with the specified number of non-zero data. The non-zero values will have a uniform distribution in the annulus (i.e. washer) with inner radius min (inclusive) and outer radius max (exclusive). Values will be uniformly distributed throughout the matrix.
      Parameters:
      rows - Number of rows in the random sparse matrix.
      cols - Number of columns in the random sparse matrix.
      min - Inner radius of the annular distribution (inclusive).
      max - Outer radius of the annular distribution (exclusive).
      numNonZeroEntries - Desired number of non-zero data int the random sparse matrix.
      Returns:
      A sparse matrix filled with the specified number of non-zero data uniformly distributed in an annulus.
    • randomSparseCMatrix

      public CooCMatrix randomSparseCMatrix(Shape shape, double min, double max, int numNonZeroEntries)
      Generates a random sparse matrix with the specified number of non-zero data. The non-zero values will have a uniform distribution in the annulus (i.e. washer) with inner radius min (inclusive) and outer radius max (exclusive). Non-zero values will be uniformly distributed throughout the matrix.
      Parameters:
      shape - Shape of the sparse matrix to generate.
      min - Inner radius of the annular distribution (inclusive).
      max - Outer radius of the annular distribution (exclusive).
      numNonZeroEntries - Desired number of non-zero data int the random sparse matrix.
      Returns:
      A sparse matrix filled with the specified number of non-zero data uniformly distributed in an annulus.