Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Create a random sparse symmetric positive definite matrix using the Python package ProbNum
| ResourceFunction["RandomSparseSPDMatrixProbNum"][n] creates an n⨯n random sparse symmetric positive definite matrix. | |
| ResourceFunction["RandomSparseSPDMatrixProbNum"][session,…] uses the specified running ExternalSessionObject. | 
| "CholeskyFactorDensity" | 0.1 | density of the off-diagonal entries of the Cholesky factor | 
| “CholeskyFactorRange" | {0.1,1.} | the min and max values of the off-diagonal elements of the Cholesky factor | 
| RandomSeeding | Automatic | seeding of the pseudorandom generator | 
Construct a random symmetric positive definite matrix:
| In[1]:= | ![ResourceFunction["RandomSparseSPDMatrixProbNum"][7]](https://www.wolframcloud.com/obj/resourcesystem/images/abc/abc018ca-156d-4581-a799-714740965020/71284768f78cf639.png) | 
| Out[1]= |  | 
Show the matrix:
| In[2]:= | ![Normal[%] // MatrixForm](https://www.wolframcloud.com/obj/resourcesystem/images/abc/abc018ca-156d-4581-a799-714740965020/44164eebc513e0e0.png) | 
| Out[2]= |  | 
Verify that the matrix is symmetric and positive definite:
| In[3]:= | ![SymmetricMatrixQ[%] && PositiveDefiniteMatrixQ[%]](https://www.wolframcloud.com/obj/resourcesystem/images/abc/abc018ca-156d-4581-a799-714740965020/154dd09767868ed6.png) | 
| Out[3]= |  | 
Construct a matrix:
| In[4]:= | ![ResourceFunction["RandomSparseSPDMatrixProbNum"][5]](https://www.wolframcloud.com/obj/resourcesystem/images/abc/abc018ca-156d-4581-a799-714740965020/42d5e89be36b4723.png) | 
| Out[4]= |  | 
Use a the specified Python session:
| In[5]:= | ![session = StartExternalSession["Python"]](https://www.wolframcloud.com/obj/resourcesystem/images/abc/abc018ca-156d-4581-a799-714740965020/7d944fe6c0a41f9c.png) | 
| Out[5]= |  | 
| In[6]:= | ![ResourceFunction["RandomSparseSPDMatrixProbNum"][session, 5]](https://www.wolframcloud.com/obj/resourcesystem/images/abc/abc018ca-156d-4581-a799-714740965020/194b8c181a49d02e.png) | 
| Out[6]= |  | 
Close the session:
| In[7]:= | ![DeleteObject[session]](https://www.wolframcloud.com/obj/resourcesystem/images/abc/abc018ca-156d-4581-a799-714740965020/7bebb409203d37d9.png) | 
Use the option "CholeskyFactorDensity" to construct a dense matrix:
| In[8]:= | ![ResourceFunction["RandomSparseSPDMatrixProbNum"][5, "CholeskyFactorDensity" -> 1]](https://www.wolframcloud.com/obj/resourcesystem/images/abc/abc018ca-156d-4581-a799-714740965020/6eb50f686443ae16.png) | 
| Out[8]= |  | 
| In[9]:= | ![Normal[%] // MatrixForm](https://www.wolframcloud.com/obj/resourcesystem/images/abc/abc018ca-156d-4581-a799-714740965020/011f80e7a68737fe.png) | 
| Out[9]= |  | 
Create a matrix with larger numerical values using the "CholeskyFactorRange" option:
| In[10]:= | ![ResourceFunction["RandomSparseSPDMatrixProbNum"][5, "CholeskyFactorRange" -> {0.1, 10}]](https://www.wolframcloud.com/obj/resourcesystem/images/abc/abc018ca-156d-4581-a799-714740965020/07eceb35a2420abc.png) | 
| Out[10]= |  | 
| In[11]:= | ![Normal[%] // MatrixForm](https://www.wolframcloud.com/obj/resourcesystem/images/abc/abc018ca-156d-4581-a799-714740965020/7895b267dabe859b.png) | 
| Out[11]= |  | 
Ensure reproducibility of the results:
| In[12]:= | ![ResourceFunction["RandomSparseSPDMatrixProbNum"][5, RandomSeeding -> 42] // Normal](https://www.wolframcloud.com/obj/resourcesystem/images/abc/abc018ca-156d-4581-a799-714740965020/779973ecad41c005.png) | 
| Out[12]= |  | 
| In[13]:= | ![ResourceFunction["RandomSparseSPDMatrixProbNum"][5, RandomSeeding -> 42] // Normal](https://www.wolframcloud.com/obj/resourcesystem/images/abc/abc018ca-156d-4581-a799-714740965020/1d12215044ae41c8.png) | 
| Out[13]= |  | 
| In[14]:= | ![SameQ[%, %%]](https://www.wolframcloud.com/obj/resourcesystem/images/abc/abc018ca-156d-4581-a799-714740965020/415a59b8201638db.png) | 
| Out[14]= |  | 
Use the specified random number generator from the Python package numpy for seeding:
| In[15]:= | ![session = StartExternalSession["Python"]](https://www.wolframcloud.com/obj/resourcesystem/images/abc/abc018ca-156d-4581-a799-714740965020/0e3a30a842074291.png) | 
| Out[15]= |  | 
| In[16]:= | ![ResourceFunction[
  "PythonObject"][session, "numpy.random.default_rng(seed=42)", Initialization -> "import numpy"]](https://www.wolframcloud.com/obj/resourcesystem/images/abc/abc018ca-156d-4581-a799-714740965020/1437946cc549e32a.png) | 
| Out[16]= |  | 
| In[17]:= | ![ResourceFunction["RandomSparseSPDMatrixProbNum"][session, 5, RandomSeeding -> %] // Normal](https://www.wolframcloud.com/obj/resourcesystem/images/abc/abc018ca-156d-4581-a799-714740965020/7d034b20f2101afc.png) | 
| Out[17]= |  | 
| In[18]:= | ![DeleteObject[session]](https://www.wolframcloud.com/obj/resourcesystem/images/abc/abc018ca-156d-4581-a799-714740965020/4344c0e4036ef024.png) | 
A random sparse symmetric positive definite matrix can be constructed with the resource function ProbNumObject:
| In[19]:= | ![p = ResourceFunction["ProbNumObject"][]](https://www.wolframcloud.com/obj/resourcesystem/images/abc/abc018ca-156d-4581-a799-714740965020/5731deb4b622bbbf.png) | 
| Out[19]= |  | 
| In[20]:= | ![rng = ResourceFunction["ProbNumObject"][p, "RNG"[42]]](https://www.wolframcloud.com/obj/resourcesystem/images/abc/abc018ca-156d-4581-a799-714740965020/029c934690a5cb5e.png) | 
| Out[20]= |  | 
| In[21]:= | ![p["RandomSparseSPDMatrix"[rng, 5, .1]]](https://www.wolframcloud.com/obj/resourcesystem/images/abc/abc018ca-156d-4581-a799-714740965020/36efb8c8e7de675c.png) | 
| Out[21]= |  | 
| In[22]:= |  | 
| Out[22]= |  | 
Construct the same matrix with RandomSparseSPDMatrixProbNum:
| In[23]:= | ![ResourceFunction["RandomSparseSPDMatrixProbNum"][5, RandomSeeding -> 42]](https://www.wolframcloud.com/obj/resourcesystem/images/abc/abc018ca-156d-4581-a799-714740965020/6ef2dc8eba3aa85c.png) | 
| Out[23]= |  | 
| In[24]:= | ![Normal[%] === m](https://www.wolframcloud.com/obj/resourcesystem/images/abc/abc018ca-156d-4581-a799-714740965020/5296df5cd062c827.png) | 
| Out[24]= |  | 
This work is licensed under a Creative Commons Attribution 4.0 International License