Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Evaluate the matrix sign function
| ResourceFunction["MatrixSign"][m] gives the matrix sign of m. | |
| ResourceFunction["MatrixSign"][m,v] gives the matrix sign of m applied to the vector v. | 
Sign of a 2×2 matrix:
| In[1]:= | ![ResourceFunction["MatrixSign"][{{1.2, 5.6}, {3, 4}}]](https://www.wolframcloud.com/obj/resourcesystem/images/c73/c734d31c-043a-4274-a3d5-e60025a7f129/7139c46d8f1c8251.png) | 
| Out[1]= |  | 
Sign applied to a vector:
| In[2]:= | ![ResourceFunction["MatrixSign"][{{1.2, 5.6}, {3, 4}}, {1, 2}]](https://www.wolframcloud.com/obj/resourcesystem/images/c73/c734d31c-043a-4274-a3d5-e60025a7f129/62e56a4bae98ec8f.png) | 
| Out[2]= |  | 
Find the matrix sign of a MachinePrecision matrix:
| In[3]:= | ![ResourceFunction[
 "MatrixSign"][{{1.25, 3, 2}, {7.9, -1.4, 2}, {3.2, 2.7, 6.5}}]](https://www.wolframcloud.com/obj/resourcesystem/images/c73/c734d31c-043a-4274-a3d5-e60025a7f129/695606528076e46f.png) | 
| Out[3]= |  | 
Matrix sign of a complex matrix:
| In[4]:= | ![ResourceFunction[
  "MatrixSign"][{{1. + I, 2, 3 - 2 I}, {0, 4 \[Pi], 5 I}, {E, 0, 6}}] // MatrixForm](https://www.wolframcloud.com/obj/resourcesystem/images/c73/c734d31c-043a-4274-a3d5-e60025a7f129/2b6292f549aa3350.png) | 
| Out[4]= |  | 
Matrix sign of an exact matrix:
| In[5]:= | ![ResourceFunction["MatrixSign"][{{3, 1, 1}, {1, 0, 1}, {1, 2, 1}}]](https://www.wolframcloud.com/obj/resourcesystem/images/c73/c734d31c-043a-4274-a3d5-e60025a7f129/7869d3c8737bfd62.png) | 
| Out[5]= |  | 
Matrix sign of an arbitrary-precision matrix:
| In[6]:= | ![ResourceFunction["MatrixSign"][
 RandomReal[{-1, 1}, {2, 2}, WorkingPrecision -> 20]]](https://www.wolframcloud.com/obj/resourcesystem/images/c73/c734d31c-043a-4274-a3d5-e60025a7f129/52be1cf92707d659.png) | 
| Out[6]= |  | 
Matrix sign of a symbolic matrix:
| In[7]:= | ![ResourceFunction["MatrixSign"][{{a, b}, {0, c}}]](https://www.wolframcloud.com/obj/resourcesystem/images/c73/c734d31c-043a-4274-a3d5-e60025a7f129/16ae514be64befb9.png) | 
| Out[7]= |  | 
Computing the sign of large machine-precision matrices is efficient:
| In[8]:= | ![m = RandomReal[{0, 1}, {800, 800}];
ResourceFunction["MatrixSign"][m]; // AbsoluteTiming](https://www.wolframcloud.com/obj/resourcesystem/images/c73/c734d31c-043a-4274-a3d5-e60025a7f129/7dcc17961fbc686a.png) | 
| Out[8]= |  | 
Directly applying the sign to a single vector is more efficient:
| In[9]:= | ![ResourceFunction["MatrixSign"][m, Range[800]]; // AbsoluteTiming](https://www.wolframcloud.com/obj/resourcesystem/images/c73/c734d31c-043a-4274-a3d5-e60025a7f129/20ffd8081d987736.png) | 
| Out[9]= |  | 
Directly apply the matrix sign of a sparse matrix to a sparse vector:
| In[10]:= | ![m = SparseArray[{Band[{2, 1}] -> 2, Band[{1, 2}] -> 2}, {1000, 1000}];
v = SparseArray[{{1} -> 1, {-1} -> -1.5}, {1000}];
ResourceFunction["MatrixSign"][m, v] // Short](https://www.wolframcloud.com/obj/resourcesystem/images/c73/c734d31c-043a-4274-a3d5-e60025a7f129/4dccf9f483115ad9.png) | 
| Out[10]= |  | 
The matrix sign is involutory:
| In[11]:= | ![m = RandomReal[1, {5, 5}];
MatrixPower[ResourceFunction["MatrixSign"][m], 2] // Chop](https://www.wolframcloud.com/obj/resourcesystem/images/c73/c734d31c-043a-4274-a3d5-e60025a7f129/3f2bc2708faa44d6.png) | 
| Out[11]= |  | 
If m is invertible, its matrix sign has eigenvalues of ±1:
| In[12]:= | ![sgn = ResourceFunction["MatrixSign"][{{1.2, 5.6}, {3, 4}}];
Eigenvalues[%]](https://www.wolframcloud.com/obj/resourcesystem/images/c73/c734d31c-043a-4274-a3d5-e60025a7f129/68f34833652929d5.png) | 
| Out[13]= |  | 
The matrix sign of a diagonal matrix is diagonal:
| In[14]:= | ![d = {1, 2, 3, 4};
ResourceFunction["MatrixSign"][DiagonalMatrix[d t]]](https://www.wolframcloud.com/obj/resourcesystem/images/c73/c734d31c-043a-4274-a3d5-e60025a7f129/72d0b65276bcc5b5.png) | 
| Out[14]= |  | 
If m is invertible, then sgn(m) is unimodular (has Det(sgn(m))=±1):
| In[15]:= | ![m = RandomComplex[{-1 - I, 1 + I}, {3, 3}]](https://www.wolframcloud.com/obj/resourcesystem/images/c73/c734d31c-043a-4274-a3d5-e60025a7f129/7c8bfdbd931cf34b.png) | 
| Out[15]= |  | 
| In[16]:= | ![Det[ResourceFunction["MatrixSign"][m]] // Chop](https://www.wolframcloud.com/obj/resourcesystem/images/c73/c734d31c-043a-4274-a3d5-e60025a7f129/2dae5dc10bc12b5d.png) | 
| Out[16]= |  | 
If m is diagonalizable with m=v-1.d.v, then sgn(m)=v-1.sgn(Re(d)).v:
| In[17]:= | ![m = RandomReal[1, {3, 3}];
{d, vt} = Eigensystem[m]](https://www.wolframcloud.com/obj/resourcesystem/images/c73/c734d31c-043a-4274-a3d5-e60025a7f129/67e0088d8894adf3.png) | 
| Out[17]= |  | 
| In[18]:= | ![v = Transpose[vt];
v . DiagonalMatrix[Sign[Re[d]]] . Inverse[v] - ResourceFunction["MatrixSign"][m] // Chop](https://www.wolframcloud.com/obj/resourcesystem/images/c73/c734d31c-043a-4274-a3d5-e60025a7f129/3d37931bfe1f1c8d.png) | 
| Out[18]= |  | 
Verify an identity involving the matrix sign and the matrix square root:
| In[19]:= | ![With[{a = RandomReal[{-1, 1}, {3, 3}, WorkingPrecision -> 25], b = RandomReal[{-1, 1}, {3, 3}, WorkingPrecision -> 25]},
 ResourceFunction["MatrixSign"][ArrayFlatten[( {
      {0, a},
      {b, 0}
     } )]] == With[{c = a . MatrixPower[b . a, -1/2]}, ArrayFlatten[( {
      {0, c},
      {Inverse[c], 0}
     } )]]]](https://www.wolframcloud.com/obj/resourcesystem/images/c73/c734d31c-043a-4274-a3d5-e60025a7f129/5d6e6c61ef884995.png) | 
| Out[19]= |  | 
This work is licensed under a Creative Commons Attribution 4.0 International License