Class RandomDenseTensor

java.lang.Object
org.flag4j.rng.RandomDenseTensor

public class RandomDenseTensor extends Object
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

    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.
    RandomDenseTensor(long seed)
    Constructs a pseudorandom tensor generator with a specified seed.
  • Method Summary

    Modifier and Type
    Method
    Description
    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.
    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.
    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.
    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.
    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.
    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).
    Generates a pseudorandom symmetric positive definite matrix.
    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).
    Gets a pseudorandom orthogonal matrix.
    Generates a symmetric matrix filled with pseudorandom values uniformly distributed in [0, 1).
    Generates a pseudorandom symmetric positive definite matrix.
    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 specified size.
    randomTriuMatrix(int size, int min, int max)
    Gets a pseudorandom upper triangular matrix of the specified 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).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      public Tensor randomTensor(Shape shape)
      Generates a tensor filled with pseudorandom values uniformly distributed in [0, 1).
      Parameters:
      shape - Shape of the tensor.
      Returns:
      A tensor filled with pseudorandom values uniformly distributed in [0, 1).
    • randomTensor

      public Tensor randomTensor(Shape shape, double min, double max)
      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 - If max is less than min.
    • randnTensor

      public Tensor 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.
      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

      public Tensor 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.
      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

      public CTensor 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.
      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

      public CTensor 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.
      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 - If min >= max or min < 0.
    • randnCTensor

      public CTensor 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. 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

      public CTensor 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. 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

      public Vector randomVector(int size)
      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

      public Vector randomVector(int size, double min, double max)
      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 - If max is less than min.
    • randnVector

      public Vector 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.
      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

      public Vector 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.
      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

      public CVector 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.
      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

      public CVector 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.
      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 - If min >= max or min < 0.
    • randnCVector

      public CVector 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. 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

      public CVector 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. 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

      public Matrix randomMatrix(Shape shape)
      Generates a matrix filled with pseudorandom values uniformly distributed in [0, 1).
      Parameters:
      shape - Shape of the matrix.
      Returns:
      A matrix filled with pseudorandom values uniformly distributed in [0, 1).
    • randomMatrix

      public Matrix randomMatrix(Shape shape, double min, double max)
      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 - If max is less than min.
    • randnMatrix

      public Matrix 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.
      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

      public Matrix 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.
      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

      public CMatrix 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.
      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

      public CMatrix 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.
      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 - If min >= max or min < 0.
    • randnCMatrix

      public CMatrix randnCMatrix(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. 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

      public CMatrix 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. 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

      public Matrix randomSymmetricMatrix(int size)
      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

      public Matrix randomOrthogonalMatrix(int size)

      Gets a pseudorandom orthogonal matrix.

      The matrix is generated as if by randomMatrix(new Shape(size, size)). Then, a QR decomposition is computed on this pseudorandom matrix and the Q matrix from this decomposition is returned.

      Parameters:
      size - Size of the orthogonal matrix.
      Returns:
      A pseudorandom orthogonal matrix.
    • randomUnitaryMatrix

      public CMatrix randomUnitaryMatrix(int size)

      Gets a pseudorandom unitary matrix.

      The matrix is generated as if by randomCMatrix(new Shape(size, size)). Then, a QR decomposition is computed on this pseudorandom matrix and the Q matrix from this decomposition is returned.

      Parameters:
      size - Size of the unitary matrix.
      Returns:
      A pseudorandom unitary matrix.
    • randomTriuMatrix

      public Matrix randomTriuMatrix(int size, int min, int max)
      Gets a pseudorandom upper triangular matrix of the specified size. 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

      public Matrix randomTrilMatrix(int size, int min, int max)
      Gets a pseudorandom lower triangular matrix of the specified size. 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

      public Matrix randomDiagMatrix(int size, int min, int max)
      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

      public Matrix randomSymmPosDefMatrix(int size)
      Generates a pseudorandom symmetric positive definite matrix. This is done as if by
           Matrix 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

      public CMatrix randomHermPosDefMatrix(int size)
      Generates a pseudorandom symmetric positive definite matrix. This is done as if by
           Matrix 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: