Wolfram Research

Function Repository Resource:

RandomMatrix (1.0.0) current version: 2.0.0 »

Source Notebook

Return a pseudo random matrix of a given kind, type and size

Contributed by: Dennis M Schneider

ResourceFunction["RandomMatrix"][type,range,n]

returns an n×n matrix with random entries of type type chosen from the range range.

ResourceFunction["RandomMatrix"][type,range,{m,n}]

returns an m×n matrix with random entries of type type chosen from the range range.

ResourceFunction["RandomMatrix"][kind,type,range,n]

returns an n×n kind matrix with random entries of type type chosen from the range range.

Details and Options

The type can be Integer, Rational, Real, Complex, "GaussianInteger" or "GaussianRational".
When type is equal to Integer, Real, or Complex, ResourceFunction["RandomMatrix"] effectively generates entries using RandomInteger, RandomReal, or RandomComplex, respectively.
When type is Rational, ResourceFunction["RandomMatrix"] uses a custom-defined function RandomRational to generate entries.
When type is "GaussianInteger" or "GaussianRational", ResourceFunction["RandomMatrix"] returns entries of the form RandomInteger[range]+I·RandomInteger[range] or RandomRational[range]+I·RandomRational[range], respectively.
The kinds of square matrices allowed are "Hermitian", "Idempotent", "Involution", "Orthogonal", "SkewHermitian", "SkewSymmetric", "Symmetric","SymmetricIdempotent", "SymmetricInvolution", "Triangular"and "Unitary".
For matrices of kind "Idempotent","Symmetric", "SymmetricIdempotent" or "Hermitian", setting the option "Rank" k will choose a matrix of that kind having rank k.

Examples

Basic Examples (7) 

A random 2×3 real matrix whose entries are approximate real numbers between 0 and 1:

In[1]:=
ResourceFunction["RandomMatrix"][Real, {0, 1}, {2, 3}]
Out[1]=

A random 2×3 real matrix whose entries are approximate real numbers between -5 and 10:

In[2]:=
ResourceFunction["RandomMatrix"][Real, {-5, 10}, {2, 3}]
Out[2]=

A random 2×3 real matrix whose entries are integers between -5 and 10:

In[3]:=
ResourceFunction["RandomMatrix"][Integer, {-5, 10}, {2, 3}]
Out[3]=

A random 2×3 real matrix whose entries are rationals between -5 and 10:

In[4]:=
ResourceFunction["RandomMatrix"][Rational, {-5, 10}, {2, 3}]
Out[4]=

A random 2×3 real matrix whose entries are Gaussian integers between -5 and 10:

In[5]:=
ResourceFunction["RandomMatrix"]["GaussianInteger", {-5, 10}, {2, 3}]
Out[5]=

A random 2×3 real matrix whose entries are Gaussian rationals between -5 and 10:

In[6]:=
ResourceFunction["RandomMatrix"]["GaussianRational", {-5, 10}, {2, 3}]
Out[6]=

A random 2×3 complex matrix whose entries are RandomComplex[{-5+2I,10-3I}]:

In[7]:=
ResourceFunction[
 "RandomMatrix"][Complex, {-5 + 2 I, 10 - 3 I}, {2, 3}]
Out[7]=

Scope (23) 

Examples of special kinds of square matrices:

Triangular (1) 

A random 4 upper triangular matrix with integer entries:

In[8]:=
ResourceFunction["RandomMatrix"]["Triangular", Integer, {-5, 10}, 4] // MatrixForm
Out[36]=

Symmetric (3) 

A random 5 symmetric matrix with integer entries having rank 3:

In[37]:=
(mat = ResourceFunction["RandomMatrix"]["Symmetric", Integer, {-1, 1},
     5, "Rank" -> 3]) // MatrixForm
Out[100]=

Check:

In[101]:=
MatrixRank[mat]
Out[101]=
In[102]:=
Transpose[mat] == mat
Out[102]=

A random 3 symmetric matrix with integer entries:

In[103]:=
(mat = ResourceFunction["RandomMatrix"]["Symmetric", Rational, {0, 1},
     3]) // MatrixForm
Out[129]=

Check:

In[130]:=
Transpose[mat] == mat
Out[130]=

A random 3 symmetric matrix with approximate real entries:

In[131]:=
ResourceFunction["RandomMatrix"]["Symmetric", Real, {-5, 10}, 3]
Out[131]=

Check:

In[132]:=
Transpose[%] == %
Out[132]=

Skew-Symmetric (2) 

A random 3 skew-symmetric matrix with integer entries:

In[133]:=
ResourceFunction["RandomMatrix"]["SkewSymmetric", Integer, {-5, 5}, 3]
Out[133]=

Check:

In[134]:=
Transpose[%] == -%
Out[134]=

A random 5 skew-symmetric matrix with rationals entries having rank 3:

In[135]:=
(mat = ResourceFunction["RandomMatrix"]["SkewSymmetric", Rational, {-5, 5}, 5, "Rank" -> 2]) // MatrixForm
Out[152]=

Check:

In[153]:=
Transpose[mat] == -mat
Out[153]=
In[154]:=
MatrixRank[mat]
Out[154]=

Hermitian (2) 

A random 4 Hermitian matrix with Gaussian integer entries:

In[155]:=
(mat = ResourceFunction["RandomMatrix"]["Hermitian", "GaussianInteger", {-5, 6}, 4]) // MatrixForm
Out[192]=

Check:

In[193]:=
ConjugateTranspose[mat] == mat
Out[193]=

Skew-Hermitian (2) 

A random 4 skew-Hermitian matrix with integer entries:

In[194]:=
(mat = ResourceFunction["RandomMatrix"]["SkewHermitian", "GaussianInteger", {-5, 9}, 4]) // MatrixForm
Out[195]=

Check:

In[196]:=
ConjugateTranspose[mat] == -mat
Out[196]=

Involution (skew reflection) (2) 

A random 4 involution matrix with integer entries; geometrically these matrices represent (not necessarily orthogonal) reflections:

In[197]:=
ResourceFunction["RandomMatrix"]["Involution", Integer, {-5, 6}, 5] // MatrixForm
Out[220]=

Check:

In[221]:=
% . % == IdentityMatrix[5]
Out[221]=

Symmetric Involution (reflections) (2) 

A random 4 symmetric involution with integer entries (geometrically these matrices represent orthogonal reflections in the column space of the matrix):

In[222]:=
(matR = ResourceFunction["RandomMatrix"]["SymmetricInvolution", Integer, {-3, 3}, 4]) // MatrixForm
Out[223]=

Check:

In[224]:=
matR . matR == IdentityMatrix[4]
Out[224]=

Symmetric Idempotent (orthogonal projections) (4) 

A random 4 symmetric idempotent with integer entries (geometrically these matrices represent orthogonal projections onto the column space of the matrix):

In[225]:=
(matP = ResourceFunction["RandomMatrix"]["SymmetricIdempotent", Integer, {-3, 3}, 4]) // MatrixForm
Out[226]=

Check:

In[227]:=
matP . matP == matP
Out[227]=

A projection onto a two dimensional subspace (its column space):

In[228]:=
(matP = ResourceFunction["RandomMatrix"]["SymmetricIdempotent", Integer, {-3, 3}, 4, "Rank" -> 2]) // MatrixForm
Out[301]=

Check:

In[302]:=
matP . matP == matP
Out[302]=
In[303]:=
ResourceFunction[
ResourceObject[<|"Name" -> "ColumnSpaceBasis", "ShortName" -> "ColumnSpaceBasis", "UUID" -> "1d34cddd-7f8a-4fe3-9bd4-0585f9dae910", "ResourceType" -> "Function", "Version" -> "1.0.0", "Description" -> "Return a basis for the subspace spanned by the columns of a matrix", "RepositoryLocation" -> URL[
     "https://www.wolframcloud.com/objects/resourcesystem/api/1.0"], "SymbolName" -> "FunctionRepository`$89134f5f32cb4a0baa2fbcb50e2a5271`ColumnSpaceBasis", "FunctionLocation" -> CloudObject[
     "https://www.wolframcloud.com/obj/ff0d1b87-82ed-4b71-b682-c44f319dc981"]|>, ResourceSystemBase -> Automatic]][matP]
Out[303]=

Idempotent (skew projections) (2) 

A random 5×5 idempotent with integer entries (geometrically these matrices represent skew or oblique projections onto the column space of the matrix):

In[304]:=
(matP = ResourceFunction["RandomMatrix"]["Idempotent", Integer, {-3, 3}, 5]) // MatrixForm
Out[385]=

Check:

In[386]:=
MatrixRank[matP]
Out[386]=
In[387]:=
matX = ResourceFunction[
ResourceObject[<|"Name" -> "ColumnSpaceBasis", "ShortName" -> "ColumnSpaceBasis", "UUID" -> "1d34cddd-7f8a-4fe3-9bd4-0585f9dae910", "ResourceType" -> "Function", "Version" -> "1.0.0", "Description" -> "Return a basis for the subspace spanned by the columns of a matrix", "RepositoryLocation" -> URL[
      "https://www.wolframcloud.com/objects/resourcesystem/api/1.0"], "SymbolName" -> "FunctionRepository`$89134f5f32cb4a0baa2fbcb50e2a5271`ColumnSpaceBasis", "FunctionLocation" -> CloudObject[
      "https://www.wolframcloud.com/obj/ff0d1b87-82ed-4b71-b682-c44f319dc981"]|>, ResourceSystemBase -> Automatic]][matP]
Out[387]=

Orthogonal (1) 

In[388]:=
(mat = ResourceFunction["RandomMatrix"]["Orthogonal", Integer, {-1, 1}, 4]) // MatrixForm
Out[389]=

Check:

In[390]:=
Transpose[mat] . mat == IdentityMatrix[4]
Out[390]=

Unitary (2) 

A random 4 unitary matrix with Gaussian integer entries:

In[391]:=
(matP = ResourceFunction["RandomMatrix"]["Unitary", "GaussianInteger", {-1, 1}, 4]) // MatrixForm
Out[392]=

Check:

In[393]:=
matP . ConjugateTranspose[matP] - IdentityMatrix[4] // Simplify
Out[393]=

A random 4 unitary matrix with complex entries:

In[394]:=
(matP = ResourceFunction["RandomMatrix"]["Unitary", Complex, {-1 + 2 I, 1 - 3 I}, 4]) // MatrixForm
Out[395]=

Check:

In[396]:=
matP . ConjugateTranspose[matP] - IdentityMatrix[4] // Chop
Out[396]=

Publisher

Dennis M Schneider

Version History

  • 2.0.0 – 21 August 2023
  • 1.0.0 – 08 January 2021

Related Resources

License Information