F
F
Floydreme2020-01-03 18:09:54
Python
Floydreme, 2020-01-03 18:09:54

Why did Cramer's method fail?

Hello! I am writing my Cramer method in python. There was a problem with the formation of the following matrices. According to my logic, the code should work fine, because the argument of the defaultMatrix function does not change in any way, and only the variables that store the original array change, but the python thinks otherwise ... What could be wrong? Thanks

Here is the code:

def CramersRule(defaultMatrix, values):
        matrix1 = defaultMatrix
        for i in range(3):
            matrix1[i][0] = -values[I]

        matrix2 = defaultMatrix
        for i in range(3):
            matrix2[i][1] = -values[I]

        matrix3 = defaultMatrix
        for i in range(3):
            matrix3[i][2] = -values[i]
        # ...


Here is the output to the console:
quRBCBHfDHA.jpg?size=1280x201&quality=96&proxy=1&sign=1d844c79d6445fddfde1d19752a558d3&type=album

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2020-01-03
@FloydReme

defaultMatrix does not change in any way, but only the variables that store the original array change

Is changing. The list is a Mutable object, changing the variable to which the list is assigned changes the original list as well. the variable does not copy the original list, but is only a reference to the same object.
https://medium.com/@meghamohan/mutable-and-immutab...
There is a dip copy for copying, for example https://docs.python.org/3/library/copy.html
import copy

matrix1 = copy.deepcopy(defaultMatrix)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question