Function Repository Resource:

CoefficientMatrix

Source Notebook

Returns the coefficient matrix of a system of equations

Contributed by: Dennis M Schneider

ResourceFunction["CoefficientMatrix"][eqns,vars]

returns the coefficient matrix of the system of equations eqns in the variables vars.

Details and Options

The equations eqns may be a list of equations or a matrix equation.

Examples

Basic Examples (3) 

Get the coefficient matrix of a linear system:

In[1]:=
ResourceFunction[
  "CoefficientMatrix"][{2 (x + y) + \[Pi] z == 3 - x, x + 2 y - z - 2 == 0}, {x, y, z}] // MatrixForm
Out[1]=

Matrix equations are allowed:

In[2]:=
ResourceFunction["CoefficientMatrix"][( {
      {3, 2, \[Pi]},
      {1, 2, -1}
     } ) . ( {
      {x},
      {y},
      {z}
     } ) == ( {
     {a},
     {b}
    } ), {x, y, z}] // MatrixForm
Out[2]=
In[3]:=
ResourceFunction["CoefficientMatrix"][( {
      {3, 2, \[Pi]},
      {1, 2, -1}
     } ) . ( {
      {x},
      {y},
      {z}
     } ) == ( {
      {1, 3, 5},
      {2, 4, -6}
     } ) . ( {
      {x},
      {y},
      {z}
     } ), {x, y, z}] // MatrixForm
Out[3]=

Applications (5) 

Find all matrices X that commute with . These matrices must satisfy the system of equations:

In[4]:=
matA = {{1, 2}, {3, 4}};
matX = Table[Subscript[x, i, j], {i, 1, 2}, {j, 1, 2}];
augmat = ResourceFunction["CoefficientMatrix"][
  matA . matX == matX . matA, Flatten[matX]]
Out[3]=

We compute the null space of this matrix:

In[5]:=
nspace = NullSpace[augmat]
Out[5]=

A basis for the matrix X:

In[6]:=
{mat1, mat2} = matX /. Thread[Flatten[matX] -> #] & /@ nspace
Out[6]=

Thus, any such matrix must be of the form:

In[7]:=
generalmat = (\[Alpha] mat1 + \[Beta] mat2)
Out[7]=

Check:

In[8]:=
generalmat . matA == matA . generalmat // Simplify
Out[8]=

Publisher

Dennis M Schneider

Version History

  • 1.0.0 – 31 July 2019

Related Resources

License Information