Home » Python » python – convert this matrix equation into something numpy can understand-Exceptionshub

python – convert this matrix equation into something numpy can understand-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I know how to solve basic linear matrix equations with numpy.

However, I have a matrix A and the equation A^2 + xA + yI = 0, where x and y are not vectors, but rather a scalar. I is the identity matrix, and 0 is the zero matrix of dimensions matching A.

This is a super easy on paper for small matrices (assuming of course that a solution exists), but I’m practicing for a coding interview and will be expected to solve problems like this with python. And maybe the matrix given will be quite large…

Here is a sample matrix A, which results in solutions x=-2, y=1:

np.array([[1,1,0],
          [0,1,0]
          [0,0,1]]

On paper, this is as easy as solving the system of linear equations x = -2 and x+y=-1. The issue I am facing is parsing the equation in its form above to one that is in the form of a system of equations (or alternatively a linear matrix equation of the form Ax = B).

How to&Answers:

The issue I am facing is parsing the equation in its form above to one that is in the form of a system of equations (or alternatively a linear matrix equation of the form Ax = B

Say that A has n columns. For a square matrix Q with n columns, let E(Q) be the length-n^2 vector formed by iterating over the entries of Q (say in row-major order).

Then solving for x, y in

A^2 + xA + yI = 0

is equivalent to solving for z in the system

B z = -c

where

  • z = [x, y] is a length-2 column vector

  • B is the n^2 X 2 matrix whose columns are E(A) and E(I)

  • c is E(A^2)