Function Repository Resource:

RandomSparseSPDMatrix

Source Notebook

Create a random sparse symmetric positive definite matrix

Contributed by: Jan Mangaldan

ResourceFunction["RandomSparseSPDMatrix"][n]

creates an n×n random sparse symmetric positive definite matrix.

Details and Options

ResourceFunction["RandomSparseSPDMatrix"] constructs a random matrix from its Cholesky factor in which the diagonal is set to one and all other nonzero entries of the lower triangle are sampled from a uniform distribution with bounds {min,max}.
ResourceFunction["RandomSparseSPDMatrix"] returns a SparseArray object.
The following options can be given:
"CholeskyFactorDensity"density of the off-diagonal entries of the Cholesky factor
“CholeskyFactorRange"{,1}the min and max values of the off-diagonal entries of the Cholesky factor
WorkingPrecisionMachinePrecisionthe precision to use in internal computations
The value of "CholeskyFactorDensity" must be between 0 and 1 where 1 represents a dense matrix.

Examples

Basic Examples (3) 

Construct a random symmetric positive definite matrix:

In[1]:=
ResourceFunction["RandomSparseSPDMatrix"][7]
Out[1]=

Show the matrix:

In[2]:=
Normal[%] // MatrixForm
Out[2]=

Verify that the matrix is symmetric and positive definite:

In[3]:=
SymmetricMatrixQ[%] && PositiveDefiniteMatrixQ[%]
Out[3]=

Options (4) 

CholeskyFactorDensity (1) 

Use the option "CholeskyFactorDensity"to construct a dense matrix:

In[4]:=
ResourceFunction["RandomSparseSPDMatrix"][5, "CholeskyFactorDensity" -> 1]
Out[4]=
In[5]:=
Normal[%] // MatrixForm
Out[5]=

CholeskyFactorRange (1) 

Create a matrix with larger numerical values using the "CholeskyFactorRange" option:

In[6]:=
ResourceFunction["RandomSparseSPDMatrix"][5, "CholeskyFactorRange" -> {0.1, 10}]
Out[6]=
In[7]:=
Normal[%] // MatrixForm
Out[7]=

WorkingPrecision (2) 

A random symmetric positive definite matrix with machine-number entries:

In[8]:=
ResourceFunction["RandomSparseSPDMatrix"][5, WorkingPrecision -> MachinePrecision]
Out[8]=
In[9]:=
Normal[%] // MatrixForm
Out[9]=

A random symmetric positive definite matrix with 24-digit precision entries:

In[10]:=
ResourceFunction["RandomSparseSPDMatrix"][5, WorkingPrecision -> 24]
Out[10]=
In[11]:=
Normal[%] // MatrixForm
Out[11]=

Version History

  • 1.0.0 – 28 March 2022

Related Resources

License Information