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 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 TypeMethodDescriptionrandomCooMatrix
(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 symmetricCOO matrix
filled with pseudorandom values uniformly distributed in[min, max)
.randomSymmetricCsrMatrix
(int size, int min, int max, double sparsity) Generates a symmetricCSR matrix
filled with pseudorandom values uniformly distributed in[min, max)
.
-
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
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
sparsity
filled 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
sparsity
filled 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 valuesnumNonZeroEntries
- 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 valuesnumNonZeroEntries
- 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
sparsity
filled 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
sparsity
filled 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 valuesnumNonZeroEntries
- 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 valuesnumNonZeroEntries
- 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 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
- Ifsparsity
is not in the range[0.0, 1.0]
.
-
randomSymmetricCsrMatrix
Generates a symmetricCSR 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
- Ifsparsity
is not in the range[0.0, 1.0]
.
-
randomSparseCMatrix
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
sparsity
filled with random values uniformly distributed in an annulus.
-
randomSparseCMatrix
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
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 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).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
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).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.
-