Class RandomDenseTensor
java.lang.Object
org.flag4j.rng.RandomDenseTensor
A utility class for generating random dense tensors, matrices, and vectors with customizable distributions.
Features:
- Generate random tensors, matrices, and vectors with real or complex entries.
- Support for configurable uniform and Gaussian distributions.
- Create specialized matrices, such as symmetric, orthogonal, unitary, triangular, and positive definite.
This class relies on the RandomComplex
and RandomArray
classes for pseudorandom number
generation and array population.
Example Usage:
RandomDenseTensor rtg = new RandomDenseTensor(12345L);
// Uniformly distributed tensor.
Tensor tensor = rtg.randomTensor(new Shape(3, 3, 5), 0.0, 10.0);
// Random unitary matrix.
CMatrix unitaryMatrix = rtg.randomUnitaryMatrix(5);
// Random symmetric positive definite matrix.
Matrix symmPosDef = rtg.randomSymPosDef(12);
- 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.RandomDenseTensor
(long seed) Constructs a pseudorandom tensor generator with a specified seed. -
Method Summary
Modifier and TypeMethodDescriptionrandnCMatrix
(Shape shape) Generates a matrix filled with pseudorandom values sampled from a bivariate standard Gaussian (normal) distribution with mean zero and standard deviation one along both the real and imaginary axes.randnCMatrix
(Shape shape, double mean, double std) Generates a matrix filled with pseudorandom values sampled from a bivariate Gaussian (normal) distribution with specified mean and standard deviation along both the real and imaginary axes.randnCMatrix
(Shape shape, double meanRe, double stdRe, double meanIm, double stdIm, double corrCoeff) Generates a matrix filled with pseudorandom values sampled from a bivariate Gaussian (normal) distribution.randnCTensor
(Shape shape) Generates a tensor filled with pseudorandom values sampled from a bivariate standard Gaussian (normal) distribution with mean zero and standard deviation one along both the real and imaginary axes.randnCTensor
(Shape shape, double mean, double std) Generates a tensor filled with pseudorandom values sampled from a bivariate Gaussian (normal) distribution with specified mean and standard deviation along both the real and imaginary axes.randnCTensor
(Shape shape, double meanRe, double stdRe, double meanIm, double stdIm, double corrCoeff) Generates a tensor filled with pseudorandom values sampled from a bivariate Gaussian (normal) distribution.randnCVector
(int size) Generates a vector filled with pseudorandom values sampled from a bivariate standard Gaussian (normal) distribution with mean zero and standard deviation one along both the real and imaginary axes.randnCVector
(int size, double mean, double std) Generates a vector filled with pseudorandom values sampled from a bivariate Gaussian (normal) distribution with specified mean and standard deviation along both the real and imaginary axes.randnCVector
(int size, double meanRe, double stdRe, double meanIm, double stdIm, double corrCoeff) Generates a vector filled with pseudorandom values sampled from a bivariate Gaussian (normal) distribution.randnMatrix
(Shape shape) Generates a matrix filled with pseudorandom values sampled from a normal distribution with mean of 0.0 and standard deviation of 1.0.randnMatrix
(Shape shape, double mean, double std) Generates a matrix filled with pseudorandom values sampled from a normal distribution with specified mean and standard deviation.randnTensor
(Shape shape) Generates a tensor filled with pseudorandom values sampled from a normal distribution with mean of 0.0 and standard deviation of 1.0.randnTensor
(Shape shape, double mean, double std) Generates a tensor filled with pseudorandom values sampled from a normal distribution with specified mean and standard deviation.randnVector
(int size) Generates a vector filled with pseudorandom values sampled from a normal distribution with mean of 0.0 and standard deviation of 1.0.randnVector
(int size, double mean, double std) Generates a vector filled with pseudorandom values sampled from a normal distribution with specified mean and standard deviation.randomCMatrix
(Shape shape) Generates a matrix filled with pseudorandom complex values uniformly distributed in the unit disk centered at the origin of the complex plane.randomCMatrix
(Shape shape, double min, double max) Generates a matrix filled with pseudorandom complex values uniformly distributed in an annulus (i.e. washer) centered at the origin of the complex plane.randomCTensor
(Shape shape) Generates a tensor filled with pseudorandom complex values uniformly distributed in the unit disk centered at the origin of the complex plane.randomCTensor
(Shape shape, double min, double max) Generates a tensor filled with pseudorandom complex values uniformly distributed in an annulus (i.e. washer) centered at the origin of the complex plane.randomCVector
(int size) Generates a vector filled with pseudorandom complex values uniformly distributed in the unit disk centered at the origin of the complex plane.randomCVector
(int size, double min, double max) Generates a vector filled with pseudorandom complex values uniformly distributed in an annulus (i.e. washer) centered at the origin of the complex plane.randomDiagMatrix
(int size, int min, int max) Generates a random diagonal matrix whose diagonal entries are uniformly distributed in [min
,max
).randomHermPosDefMatrix
(int size) Generates a pseudorandom symmetric positive definite matrix.randomMatrix
(Shape shape) Generates a matrix filled with pseudorandom values uniformly distributed in[0, 1)
.randomMatrix
(Shape shape, double min, double max) Generates a matrix filled with pseudorandom values uniformly distributed in[min, max)
.randomOrthogonalMatrix
(int size) Gets a pseudorandom orthogonal matrix.randomSymmetricMatrix
(int size) Generates a symmetric matrix filled with pseudorandom values uniformly distributed in[0, 1)
.randomSymmPosDefMatrix
(int size) Generates a pseudorandom symmetric positive definite matrix.randomTensor
(Shape shape) Generates a tensor filled with pseudorandom values uniformly distributed in[0, 1)
.randomTensor
(Shape shape, double min, double max) Generates a tensor filled with pseudorandom values uniformly distributed in[min, max)
.randomTrilMatrix
(int size, int min, int max) Gets a pseudorandom lower triangular matrix of the specifiedsize
.randomTriuMatrix
(int size, int min, int max) Gets a pseudorandom upper triangular matrix of the specifiedsize
.randomUnitaryMatrix
(int size) Gets a pseudorandom unitary matrix.randomVector
(int size) Generates a vector filled with pseudorandom values uniformly distributed in[0, 1)
.randomVector
(int size, double min, double max) Generates a vector filled with pseudorandom values uniformly distributed in[min, max)
.
-
Constructor Details
-
RandomDenseTensor
public RandomDenseTensor()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. -
RandomDenseTensor
public RandomDenseTensor(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
-
randomTensor
-
randomTensor
Generates a tensor filled with pseudorandom values uniformly distributed in[min, max)
.- Parameters:
shape
- Shape of the tensor.min
- Minimum value for the uniform distribution.max
- Maximum value for the uniform distribution.- Returns:
- A tensor filled with pseudorandom values uniformly distributed in
[min, max)
. - Throws:
IllegalArgumentException
- Ifmax
is less thanmin
.
-
randnTensor
Generates a tensor filled with pseudorandom values sampled from a normal distribution with mean of 0.0 and standard deviation of 1.0.- Parameters:
shape
- Shape of the tensor.- Returns:
- A tensor filled with pseudorandom values sampled from a normal distribution with mean of 0.0 and standard deviation of 1.0.
-
randnTensor
Generates a tensor filled with pseudorandom values sampled from a normal distribution with specified mean and standard deviation.- Parameters:
shape
- Shape of the tensor.mean
- Mean of the normal distribution to sample from.std
- Standard deviation of normal distribution to sample from.- Returns:
- A tensor filled with pseudorandom values sampled from a normal distribution with specified mean and standard deviation.
- Throws:
IllegalArgumentException
- If standard deviation is negative.
-
randomCTensor
Generates a tensor filled with pseudorandom complex values uniformly distributed in the unit disk centered at the origin of the complex plane.- Parameters:
shape
- Shape of the tensor.- Returns:
- A tensor filled with pseudorandom complex values uniformly distributed in the unit disk centered at the origin of the complex plane.
-
randomCTensor
Generates a tensor filled with pseudorandom complex values uniformly distributed in an annulus (i.e. washer) centered at the origin of the complex plane.- Parameters:
shape
- Shape of the tensor.min
- Inner radius of annulus.max
- Outer radius of annulus.- Returns:
- A tensor filled with pseudorandom values with magnitudes uniformly distributed in an annulus (i.e. washer) centered at the origin of the complex plane.
- Throws:
IllegalArgumentException
- Ifmin >= max
ormin < 0
.
-
randnCTensor
Generates a tensor filled with pseudorandom values sampled from a bivariate standard Gaussian (normal) distribution with mean zero and standard deviation one along both the real and imaginary axes. The correlation coefficient of the distribution will be zero.- Parameters:
shape
- Shape of the tensor.- Returns:
- A pseudorandom values sampled from a bivariate standard Gaussian (normal) distribution with a correlation coefficient of zero.
- See Also:
-
randnCTensor
Generates a tensor filled with pseudorandom values sampled from a bivariate Gaussian (normal) distribution with specified mean and standard deviation along both the real and imaginary axes. The correlation coefficient of the distribution will be zero.- Parameters:
shape
- Shape of the tensor.mean
- Mean of the Gaussian distribution.std
- Standard deviation of the Gaussian distribution.- Returns:
- A tensor filled with pseudorandom values sampled from a bivariate Gaussian distribution with specified mean and standard deviation.
- See Also:
-
randnCTensor
public CTensor randnCTensor(Shape shape, double meanRe, double stdRe, double meanIm, double stdIm, double corrCoeff) Generates a tensor filled with pseudorandom values sampled from a bivariate Gaussian (normal) distribution.- Parameters:
shape
- Shape of the tensor.meanRe
- Mean along real axis of complex plane for the distribution.stdRe
- Standard deviation along real axis of complex plane for the distribution.meanIm
- Mean along imaginary axis of complex plane for the distribution.stdIm
- Standard deviation along imaginary axis of complex plane for the distribution.corrCoeff
- Correlation coefficient of the bivariate distribution.rng
- Pseudorandom number generator to use when randomly sampling from this distribution.- Returns:
- A tensor filled with pseudorandom values sampled from a bivariate Gaussian distribution with specified means, standard deviations and correlation coefficient.
- See Also:
-
randomVector
Generates a vector filled with pseudorandom values uniformly distributed in[0, 1)
.- Parameters:
int
- size of the vector.- Returns:
- A vector filled with pseudorandom values uniformly distributed in
[0, 1)
.
-
randomVector
Generates a vector filled with pseudorandom values uniformly distributed in[min, max)
.- Parameters:
min
- Minimum value for the uniform distribution.max
- Maximum value for the uniform distribution.int
- size of the vector.- Returns:
- A vector filled with pseudorandom values uniformly distributed in
[min, max)
. - Throws:
IllegalArgumentException
- Ifmax
is less thanmin
.
-
randnVector
Generates a vector filled with pseudorandom values sampled from a normal distribution with mean of 0.0 and standard deviation of 1.0.- Parameters:
int
- size of the vector.- Returns:
- A vector filled with pseudorandom values sampled from a normal distribution with mean of 0.0 and standard deviation of 1.0.
-
randnVector
Generates a vector filled with pseudorandom values sampled from a normal distribution with specified mean and standard deviation.- Parameters:
mean
- Mean of the normal distribution to sample from.std
- Standard deviation of normal distribution to sample from.int
- size of the vector.- Returns:
- A vector filled with pseudorandom values sampled from a normal distribution with specified mean and standard deviation.
- Throws:
IllegalArgumentException
- If standard deviation is negative.
-
randomCVector
Generates a vector filled with pseudorandom complex values uniformly distributed in the unit disk centered at the origin of the complex plane.- Parameters:
int
- size of the vector.- Returns:
- A vector filled with pseudorandom complex values uniformly distributed in the unit disk centered at the origin of the complex plane.
-
randomCVector
Generates a vector filled with pseudorandom complex values uniformly distributed in an annulus (i.e. washer) centered at the origin of the complex plane.- Parameters:
min
- Inner radius of annulus.max
- Outer radius of annulus.int
- size of the vector.- Returns:
- A vector filled with pseudorandom values with magnitudes uniformly distributed in an annulus (i.e. washer) centered at the origin of the complex plane.
- Throws:
IllegalArgumentException
- Ifmin >= max
ormin < 0
.
-
randnCVector
Generates a vector filled with pseudorandom values sampled from a bivariate standard Gaussian (normal) distribution with mean zero and standard deviation one along both the real and imaginary axes. The correlation coefficient of the distribution will be zero.- Parameters:
int
- size of the vector.- Returns:
- A pseudorandom values sampled from a bivariate standard Gaussian (normal) distribution with a correlation coefficient of zero.
- See Also:
-
randnCVector
Generates a vector filled with pseudorandom values sampled from a bivariate Gaussian (normal) distribution with specified mean and standard deviation along both the real and imaginary axes. The correlation coefficient of the distribution will be zero.- Parameters:
mean
- Mean of the Gaussian distribution.std
- Standard deviation of the Gaussian distribution.int
- size of the vector.- Returns:
- A vector filled with pseudorandom values sampled from a bivariate Gaussian distribution with specified mean and standard deviation.
- See Also:
-
randnCVector
public CVector randnCVector(int size, double meanRe, double stdRe, double meanIm, double stdIm, double corrCoeff) Generates a vector filled with pseudorandom values sampled from a bivariate Gaussian (normal) distribution.- Parameters:
meanRe
- Mean along real axis of complex plane for the distribution.stdRe
- Standard deviation along real axis of complex plane for the distribution.meanIm
- Mean along imaginary axis of complex plane for the distribution.stdIm
- Standard deviation along imaginary axis of complex plane for the distribution.corrCoeff
- Correlation coefficient of the bivariate distribution.int
- size of the vector.rng
- Pseudorandom number generator to use when randomly sampling from this distribution.- Returns:
- A vector filled with pseudorandom values sampled from a bivariate Gaussian distribution with specified means, standard deviations and correlation coefficient.
- See Also:
-
randomMatrix
-
randomMatrix
Generates a matrix filled with pseudorandom values uniformly distributed in[min, max)
.- Parameters:
shape
- Shape of the matrix.min
- Minimum value for the uniform distribution.max
- Maximum value for the uniform distribution.- Returns:
- A matrix filled with pseudorandom values uniformly distributed in
[min, max)
. - Throws:
IllegalArgumentException
- Ifmax
is less thanmin
.
-
randnMatrix
Generates a matrix filled with pseudorandom values sampled from a normal distribution with mean of 0.0 and standard deviation of 1.0.- Parameters:
shape
- Shape of the matrix.- Returns:
- A matrix filled with pseudorandom values sampled from a normal distribution with mean of 0.0 and standard deviation of 1.0.
-
randnMatrix
Generates a matrix filled with pseudorandom values sampled from a normal distribution with specified mean and standard deviation.- Parameters:
shape
- Shape of the matrix.mean
- Mean of the normal distribution to sample from.std
- Standard deviation of normal distribution to sample from.- Returns:
- A matrix filled with pseudorandom values sampled from a normal distribution with specified mean and standard deviation.
- Throws:
IllegalArgumentException
- If standard deviation is negative.
-
randomCMatrix
Generates a matrix filled with pseudorandom complex values uniformly distributed in the unit disk centered at the origin of the complex plane.- Parameters:
shape
- Shape of the matrix.- Returns:
- A matrix filled with pseudorandom complex values uniformly distributed in the unit disk centered at the origin of the complex plane.
-
randomCMatrix
Generates a matrix filled with pseudorandom complex values uniformly distributed in an annulus (i.e. washer) centered at the origin of the complex plane.- Parameters:
shape
- Shape of the matrix.min
- Inner radius of annulus.max
- Outer radius of annulus.- Returns:
- A matrix filled with pseudorandom values with magnitudes uniformly distributed in an annulus (i.e. washer) centered at the origin of the complex plane.
- Throws:
IllegalArgumentException
- Ifmin >= max
ormin < 0
.
-
randnCMatrix
Generates a matrix filled with pseudorandom values sampled from a bivariate standard Gaussian (normal) distribution with mean zero and standard deviation one along both the real and imaginary axes. The correlation coefficient of the distribution will be zero.- Parameters:
shape
- Shape of the matrix.- Returns:
- A pseudorandom values sampled from a bivariate standard Gaussian (normal) distribution with a correlation coefficient of zero.
- See Also:
-
randnCMatrix
Generates a matrix filled with pseudorandom values sampled from a bivariate Gaussian (normal) distribution with specified mean and standard deviation along both the real and imaginary axes. The correlation coefficient of the distribution will be zero.- Parameters:
shape
- Shape of the matrix.mean
- Mean of the Gaussian distribution.std
- Standard deviation of the Gaussian distribution.- Returns:
- A matrix filled with pseudorandom values sampled from a bivariate Gaussian distribution with specified mean and standard deviation.
- See Also:
-
randnCMatrix
public CMatrix randnCMatrix(Shape shape, double meanRe, double stdRe, double meanIm, double stdIm, double corrCoeff) Generates a matrix filled with pseudorandom values sampled from a bivariate Gaussian (normal) distribution.- Parameters:
shape
- Shape of the matrix.meanRe
- Mean along real axis of complex plane for the distribution.stdRe
- Standard deviation along real axis of complex plane for the distribution.meanIm
- Mean along imaginary axis of complex plane for the distribution.stdIm
- Standard deviation along imaginary axis of complex plane for the distribution.corrCoeff
- Correlation coefficient of the bivariate distribution.rng
- Pseudorandom number generator to use when randomly sampling from this distribution.- Returns:
- A matrix filled with pseudorandom values sampled from a bivariate Gaussian distribution with specified means, standard deviations and correlation coefficient.
- See Also:
-
randomSymmetricMatrix
Generates a symmetric matrix filled with pseudorandom values uniformly distributed in[0, 1)
.- Parameters:
size
- Number of rows and columns in the resulting matrix (the result will be a square matrix).- Returns:
- A symmetric matrix filled with pseudorandom values uniformly distributed in
[0, 1)
.
-
randomOrthogonalMatrix
Gets a pseudorandom orthogonal matrix.
The matrix is generated as if by
randomMatrix(new Shape(size, size))
. Then, aQR
decomposition is computed on this pseudorandom matrix and theQ
matrix from this decomposition is returned.- Parameters:
size
- Size of the orthogonal matrix.- Returns:
- A pseudorandom orthogonal matrix.
-
randomUnitaryMatrix
Gets a pseudorandom unitary matrix.
The matrix is generated as if by
randomCMatrix(new Shape(size, size))
. Then, aQR
decomposition is computed on this pseudorandom matrix and theQ
matrix from this decomposition is returned.- Parameters:
size
- Size of the unitary matrix.- Returns:
- A pseudorandom unitary matrix.
-
randomTriuMatrix
Gets a pseudorandom upper triangular matrix of the specifiedsize
. The non-zero entries of the matrix are distributed according to a uniform distribution un [min
,max
).- Parameters:
size
- Size if the upper triangular matrix.min
- Lower bound of the uniform distribution (inclusive).max
- Upper bound of the uniform distribution (exclusive).- Returns:
- A pseudorandom upper triangular matrix of the specified size.
-
randomTrilMatrix
Gets a pseudorandom lower triangular matrix of the specifiedsize
. The non-zero entries of the matrix are distributed according to a uniform distribution un [min
,max
).- Parameters:
size
- Size if the lower triangular matrix.min
- Lower bound of the uniform distribution (inclusive).max
- Upper bound of the uniform distribution (exclusive).- Returns:
- A pseudorandom lower triangular matrix of the specified size.
-
randomDiagMatrix
Generates a random diagonal matrix whose diagonal entries are uniformly distributed in [min
,max
).- Parameters:
size
- Size of the diagonal matrix to construct.min
- Lower bound of the uniform distribution (inclusive).max
- Upper bound of the uniform distribution (exclusive).- Returns:
- A diagonal matrix with diagonal entries are uniformly distributed in [
min
,max
).
-
randomSymmPosDefMatrix
Generates a pseudorandom symmetric positive definite matrix. This is done as if byMatrix D = randomDiagMatrix(size, 0, 1); Matrix Q = randomOrthogonalMatrix(size); return Q.T().mult(D).mult(Q);
- Parameters:
size
- Size of the symmetric positive definite matrix to generate.- Returns:
- A pseudorandom symmetric positive definite matrix.
- See Also:
-
randomHermPosDefMatrix
Generates a pseudorandom symmetric positive definite matrix. This is done as if byMatrix D = randomDiagMatrix(size, 0, 1); CMatrix U = randomUnitaryMatrix(size); return U.H().mult(D).mult(U);
- Parameters:
size
- Size of the symmetric positive definite matrix to generate.- Returns:
- A pseudorandom symmetric positive definite matrix.
- See Also:
-