Joe
Joe

Reputation: 69

python inverse matrix without numpy

I can't figure it out what's wrong with my code, it's rly frustrating. I have to make inverse matrix function, what I thought I've done. I don't know why it doesn't work. The problem is probably in line with stars, after this step my matrix named mat is changed to identity matrix, but why? Before stars it prints my mat matrix normaly which I gave to function, but after stars it's a identity matrix, I don't understand why it happend. Here's what I have:

def identity_matrix_convertion(m):
    x = m[:]
    for i in range(len(x)):
        for j in range(len(x[0])):
            if i == j:
                x[i][j] = 1
            else:
                x[i][j] = 0
    return x

def inverse_matrix(mat):
    n = len(mat)
    am = mat[:]
    show_matrix(mat)
    **i = identity_matrix_convertion(am)**
    show_matrix(mat)
    im = i[:]
    ind = list(range(n))
    print(len(mat))
    if determinant(mat) == 0:
        print("This matrix doesn't have an inverse.")
    if len(mat) == len(mat[0]):
        for i in range(n):
            scal = 1.0 / am[i][i]
            for j in range(n):
                am[i][j] *= scal
                im[i][j] *= scal
            for k in ind[0:i] + ind[i + 1:]:
                current_scal = am[k][i]
                for l in range(n):
                    am[k][l] = am[k][l] - current_scal * am[i][j]
                    im[k][l] = im[k][l] - current_scal * im[i][j]
    return im

so after line **i = identity_matrix_convertion(am)** my mat matrix is changed into identity matrix, but why?

    The result is:
    
    1.0 2.0 3.0 
    2.0 1.0 3.0 
    4.0 3.0 2.0 
    
    The result is:
    1 0 0 
    0 1 0 
    0 0 1 

Upvotes: 1

Views: 2537

Answers (2)

Csaba Toth
Csaba Toth

Reputation: 10729

Following up on @NumberC's answer, the x = m[:] does make a copy, but only a shallow copy. The copied list will contain references to internal lists of the other list, and so manipulating those lists within x cause change in m too. This is because we represent the 2D matrix as list of lists. If someone would reorder the lists in m (so not the items within the lists but just the pure order of the lists within m) the order would stay the same in the x copy. However any change within the list of the lists is mutating the other too. I hope this is not confusing, List changes unexpectedly after assignment. Why is this and how can I prevent it? has some figures.

Since we don't have to scare away from [:] we can still say:

x = new_list = [y[:] for y in m]

Or even better: we can use the built-in copy module (available everywhere):

import copy
x = copy.deepcopy(m)

See also: Copying nested lists in Python

Upvotes: 0

NumberC
NumberC

Reputation: 596

Instead of saying x = m[:] in the identity_matrix_convertion() function, you should add the following snippet:

x = []
for i in m:
    arr = [a for a in i]
    x.append(arr)

x = m[:] is still referencing m instead of just making a copy.

Upvotes: 1

Related Questions