doubleJ
doubleJ

Reputation: 23

Getting an error message 'LinAlgError: Singular matrix' with full rank matrix while using Numpy

I am using Python 3.7 and Numpy 1.16.5.

I tried to use the following code:

import numpy as np

M = [[np.eye(3), np.zeros((3,3))],[temp4, np.eye(3)]]
FTp = [[-0.0003],[0.0008],[0.0008],[0.0055],[0.0020],[0.0044]]
FT = np.linalg.solve(M,FTp)

The purpose of this code is to get a left division between M and FTp (FT = M\FTp).

temp4 is a custom-valued 3x3 matrix. Whatever value temp4 has, the matrix M should be full-ranked.

However, when I tried to run this code, I got the following message:

LinAlgError: Singular matrix

What caused this error and how can I fix it?

Upvotes: 1

Views: 273

Answers (1)

FBruzzesi
FBruzzesi

Reputation: 6475

Using np.block to create a 2d-array starting from 2d-blocks:

import numpy as np

tmp = np.random.rand(3,3)
M = np.block([[np.eye(3), np.zeros((3,3))],[tmp, np.eye(3)]])
M.shape # (6, 6)

FTp = [[-0.0003],[0.0008],[0.0008],[0.0055],[0.0020],[0.0044]]
FT = np.linalg.solve(M,FTp)

FT
array([[-0.0003    ],
       [ 0.0008    ],
       [ 0.0008    ],
       [ 0.00477519],
       [ 0.0014083 ],
       [ 0.00380704]])

Upvotes: 2

Related Questions