Adding a submatrix at a certain location rounds down all entries in the submatrix (python)

80 views Asked by At

I wanted to add a submatrix at a certain location and found the following article, which solved that problem: Add submatrices at certain locations

My problem now is, that when ever I do this, the submatrix which originally consisted of floating points will be rounded down to integers. I need the precision.

Concretely, it looks like this:

[[  1.   0.   0.]
 [  0. -22. -10.]
 [  0. -10.   1.]]

[[-22.           0.        ]
 [  0.           5.54545455]]

[[  1   0   0]
 [  0 -22   0]
 [  0   0   5]]

My code takes a matrix, A, and a list, matrix_list consting of multiple submatrices. Then I add them together in the following code

for i, C in enumerate(matrix_list):
    A[i:C.shape[0] + 1, i:C.shape[0] + 1] = C

I've tried adding np.set_printoptions(precision=20), to see if it was a printing problem, but it only makes a difference, where there was already a floating point.

1

There are 1 answers

0
niklasaa On

I figured it out. If the original values of A are all int, the matrix is an int matrix. I therefor added the line A = A.astype(np.float32), which fixed the problem.

This can be closed (I sadly cannot do that myself yet)