Class RandomSparseTensor
java.lang.Object
org.flag4j.rng.RandomSparseTensor
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
ConstructorsConstructorDescriptionConstructs a new pseudorandom tensor generator with seed set toRandomState.getGlobalSeed().RandomSparseTensor(long seed) Constructs a pseudorandom tensor generator with a specified seed. -
Method Summary
Modifier and TypeMethodDescriptionrandomCooCMatrix(int rows, int cols, double min, double max, double sparsity) Generates a random sparse matrix with the specified sparsity.randomCooCMatrix(int rows, int cols, double min, double max, int nnz) Generates a random sparse matrix with the specified number of non-zero data.randomCooCMatrix(Shape shape, double min, double max, double sparsity) Generates a random sparse matrix with the specified sparsity.randomCooCMatrix(Shape shape, double min, double max, int nnz) Generates a random sparse matrix with the specified number of non-zero data.randomCooCTensor(Shape shape, double min, double max, double sparsity) Constructs a complex COO tensor with the specified shape and sparsity filled with pseudo-random values uniformly distributed in an annulus centered on the origin of the complex plane.randomCooCTensor(Shape shape, double min, double max, int nnz) Constructs a complex COO tensor with the specified shape and number of non-zero entries filled with pseudo-random values uniformly distributed in an annulus centered on the origin of the complex plane.randomCooCVector(int size, double min, double max, double sparsity) Constructs a COO vector with the specified sparsity entries filled with pseudo-random values uniformly distributed in an annulus centered on the complex plane.randomCooCVector(int size, double min, double max, int nnz) Constructs a COO vector with the specified number of non-zero entries filled with pseudo-random values uniformly distributed in an annulus centered on the complex plane.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 nnz) 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 nnz) Generates a random sparse matrix with the specified number of non-zero data.randomCooTensor(Shape shape, double min, double max, double sparsity) Constructs a COO tensor with the specified shape and sparsity filled with pseudo-random values from a uniform distribution in [min, max).randomCooTensor(Shape shape, double min, double max, int nnz) Constructs a COO tensor with the specified shape and number of non-zero entries filled with pseudo-random values from a uniform distribution in [min, max).randomCooVector(int size, double min, double max, double sparsity) Constructs a COO vector with the specified sparsity filled with pseudo-random values from a uniform distribution in [min, max).randomCooVector(int size, double min, double max, int nnz) Constructs a COO vector with the specified number of non-zero entries filled with pseudo-random values from a uniform distribution in [min, max).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 nnz) 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 nnz) Generates a random sparse matrix with the specified number of non-zero data.randomHermitianCooMatrix(int size, int min, int max, double sparsity) Generates a Hermitiancomplex COO matrixfilled with pseudorandom values uniformly distributed in an annulus centered on the complex plane.randomHermitianCsrMatrix(int size, int min, int max, double sparsity) Generates a Hermitiancomplex CSR matrixfilled with pseudorandom values uniformly distributed in an annulus centered on the complex plane.randomSymmetricCooMatrix(int size, int min, int max, double sparsity) Generates a symmetricCOO matrixfilled with pseudorandom values uniformly distributed in[min, max).randomSymmetricCsrMatrix(int size, int min, int max, double sparsity) Generates a symmetricCSR matrixfilled with pseudorandom values uniformly distributed in[min, max).
-
Constructor Details
-
RandomSparseTensor
public RandomSparseTensor()Constructs a new pseudorandom tensor generator with seed set toRandomState.getGlobalSeed(). -
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
-
randomCooVector
Constructs a COO vector with the specified sparsity filled with pseudo-random values from a uniform distribution in [min, max).- Parameters:
size- Full size of the COO vector.min- Minimum value of the uniform distribution to sample from (inclusive).max- Maximum value of the uniform distribution to sample from (exclusive).sparsity- The sparsity of the COO vector.- Returns:
- A sparse COO vector whose entries are uniformly distributed in [min, max).
-
randomCooVector
Constructs a COO vector with the specified number of non-zero entries filled with pseudo-random values from a uniform distribution in [min, max).- Parameters:
size- Full size of the COO vector.min- Minimum value of the uniform distribution to sample from (inclusive).max- Maximum value of the uniform distribution to sample from (exclusive).nnz- The number of non-zero value to include in the vector.- Returns:
- A sparse COO vector whose entries are uniformly distributed in [min, max).
-
randomCooCVector
Constructs a COO vector with the specified sparsity entries filled with pseudo-random values uniformly distributed in an annulus centered on the complex plane.- Parameters:
size- Full size of the COO vector.min- Inner radius of the annulus (inclusive).max- Outer radius of the annulus (exclusive).sparsity- The sparsity of the COO vector.- Returns:
- A sparse COO vector whose entries are uniformly distributed in [min, max).
-
randomCooCVector
Constructs a COO vector with the specified number of non-zero entries filled with pseudo-random values uniformly distributed in an annulus centered on the complex plane.- Parameters:
size- Full size of the COO vector.min- Inner radius of the annulus (inclusive).max- Outer radius of the annulus (exclusive).nnz- The number of non-zero value to include in the vector.- Returns:
- A sparse COO vector whose entries are uniformly distributed in [min, max).
-
randomCooMatrix
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 valuessparsity- 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
sparsityfilled with random values uniformly distributed in[min, max).
-
randomCooMatrix
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 valuessparsity- 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
sparsityfilled with random values uniformly distributed in[min, max).
-
randomCooMatrix
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 valuesnnz- 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
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 valuesnnz- 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
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 valuessparsity- 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
sparsityfilled with random values uniformly distributed in[min, max).
-
randomCsrMatrix
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 valuessparsity- 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
sparsityfilled with random values uniformly distributed in[min, max).
-
randomCsrMatrix
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 valuesnnz- 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
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 valuesnnz- 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
Generates a symmetricCOO matrixfilled 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- Ifsparsityis not in the range[0.0, 1.0].
-
randomSymmetricCsrMatrix
Generates a symmetricCSR matrixfilled 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 (inclusive).max- Maximum value in uniform 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]. 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- Ifsparsityis not in the range[0.0, 1.0].
-
randomCooCMatrix
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 radiusmin(inclusive) and outer radiusmax(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
sparsityfilled with random values uniformly distributed in an annulus.
-
randomCooCMatrix
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 radiusmin(inclusive) and outer radiusmax(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
sparsityfilled with random values uniformly distributed in an annulus.
-
randomCooCMatrix
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 radiusmin(inclusive) and outer radiusmax(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).nnz- 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.
-
randomCooCMatrix
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 radiusmin(inclusive) and outer radiusmax(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).nnz- 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.
-
randomHermitianCooMatrix
Generates a Hermitiancomplex COO matrixfilled with pseudorandom values uniformly distributed in an annulus centered on the complex plane.- Parameters:
size- Number of rows and columns in the resulting matrix (the result will be a square matrix).min- Inner radius of the annulus (inclusive).max- Outer radius of the annulus (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]. 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- Ifsparsityis not in the range[0.0, 1.0].
-
randomHermitianCsrMatrix
Generates a Hermitiancomplex CSR matrixfilled with pseudorandom values uniformly distributed in an annulus centered on the complex plane.- Parameters:
size- Number of rows and columns in the resulting matrix (the result will be a square matrix).min- Inner radius of the annulus (inclusive).max- Outer radius of the annulus (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]. 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- Ifsparsityis not in the range[0.0, 1.0].
-
randomCooTensor
Constructs a COO tensor with the specified shape and sparsity filled with pseudo-random values from a uniform distribution in [min, max).- Parameters:
shape- Full shape of the COO tensor.min- Minimum value of the uniform distribution to sample from (inclusive).max- Maximum value of the uniform distribution to sample from (exclusive).sparsity- The sparsity of the COO tensor.- Returns:
- A sparse COO tensor whose entries are uniformly distributed in [min, max).
-
randomCooTensor
Constructs a COO tensor with the specified shape and number of non-zero entries filled with pseudo-random values from a uniform distribution in [min, max).- Parameters:
shape- Full shape of the COO tensor.min- Minimum value of the uniform distribution to sample from (inclusive).max- Maximum value of the uniform distribution to sample from (exclusive).nnz- The number of non-zero entries in the COO tensor.- Returns:
- A sparse COO tensor whose entries are uniformly distributed in [min, max).
-
randomCooCTensor
Constructs a complex COO tensor with the specified shape and sparsity filled with pseudo-random values uniformly distributed in an annulus centered on the origin of the complex plane.- Parameters:
shape- Full shape of the COO tensor.min- Minimum radius of the annulus (inclusive).max- Maximum radius of the annulus (exclusive).sparsity- The sparsity of the COO tensor.- Returns:
- A sparse COO tensor whose entries are uniformly distributed in [min, max).
-
randomCooCTensor
Constructs a complex COO tensor with the specified shape and number of non-zero entries filled with pseudo-random values uniformly distributed in an annulus centered on the origin of the complex plane.- Parameters:
shape- Full shape of the COO tensor.min- Minimum radius of the annulus (inclusive).max- Maximum radius of the annulus (exclusive).nnz- The number of non-zero entries in the COO tensor.- Returns:
- A sparse COO tensor whose entries are uniformly distributed in [min, max).
-