Measure seven times - cut once, or use the saying in practice

I sat on the Coursera course. The course was aimed at beginners in Java, and in one of the video tutorials the topic 7 Steps for Solving Problems was raised .

I, with a clear conscience, listened to the video tutorial, thought to myself, this business has gone far and for a long time.

The rest of the course was easy, relaxed. But, on one of the subjects at the university, namely in the so-called “Scientific Programming”, the following task appeared:
Solve a system of linear equations Ax=busing the following matrix expansion A:

A=LU,

Where LIs the lower triangular matrix, and UIs the upper triangular matrix.

The topic of linear algebra, which we took on the 1st course. Frankly, I remembered the general principles of operations with matrices, vectors, and the holy of holies - the Gauss method. If you think it over with brains, then we did not study any LU decomposition, but the Google method always helps. Rescued, up to this point.

I found a general algorithm for finding matrices Land U, but at half past three in the morning no coffee gave me the opportunity to properly implement it (given that we coded there on Octave). In the end, I freaked out, and decided to nevertheless listen to the cleverly named proverb.

1. Describe the problem by hand


All that you lack in life is to understand what the problem really is. Let's take an example of the above problem and write it on a piece of paper. In a good way, the following conditions for the existence of matrices must be satisfiedL and U:

- the inverse matrix must existA, I mean A1;
- all major minors should not be degenerate, that isΔ(1,...,n)0

Well, that’s why we at least need a “fool test”, because there are surely smart people who want to use the matrix that is not suitable for us. But for now, let’s omit it.

The problem is clear to us: find matricesL and Uthat will fall under the above conditions. Having tried to google the methods of solution, it is unlikely that you will be able to correctly implement the algorithm the first time (if you are not good at linear algebra). After going through tons of attempts, we most likely will develop some sequence of actions to solve our problem, and then we proceed to step 2.

2. Describe what you did


Take the matrix as an example

A=(123024110).



Now we apply our Gauss method to it and get rid of the extra elements in line 3, by subtracting the first line from it, multiplied by the ratio of the first element of 3 lines to the first element of 1 line:

a3=a3a1a31a11.



Now, get rid of the second element of the 3 line with a similar action:

a3=a3a2a32a22.



As a result, we obtain the matrix

A=(123024000)=U.



And now, the coefficients we found (the result of the ratio of elements aijajj) take the values ​​of the matrix L, and get

L=(1000101121).



Looking through the template, is not it?

3. Finding patterns


At this stage, most likely, you will return to step 1 and 2, because the template you did not always find will be true for all cases. But nothing, since it didn’t work out, it will happen next.

So, based on the example, we can form the following algorithm / solution template:

1. Define the square matrixA,nth order;
2. Set the matrixL=Iwhere I- identity matrix nth order;
3. Set the matrixU=A;
4. We apply the following successive transformations to the matrices, provided that{i=2,,nj=1,,i

lij=uijujjui=uiujlij,



At n=3we get:

L=(100l2110l31l321),U=(u11u12u130u22u2300u33)



4. Checking the template / algorithm


There is an algorithm - there are millions of matrices to test it! You can not worry about this template, it is obviously true (personally tested on the most non-degenerate of the possible matrices). But remember: hurry up - you make people laugh . So always check the templates you created so that you don’t continue to jump like a damn to the very beginning.

5. Translation of the algorithm into code


The obvious step, everything you need is often already in the syntax of the language, and if not, no one bothers you to create classes and functions to solve this problem, then you have an algorithm. Since I implemented this algorithm as part of an educational university program, I could only use Octave for implementation, so all the code I present to you is in its syntax (it is very similar to Python, so I will use its highlight for formatting):

A = input("Enter the matrix A: ")
numOfRows = size(A, 1)
numOfCols = size(A, 2)

if numOfRows ~= numOfCols
    disp("Wrong matrix.")
else
    n = numOfCols
    disp("----------------")
    L = eye(3)
    disp("Making U = A")
    U = A
    disp("----------------")

    for i=2:n
        disp("Step "), disp(i-1)
        for j=1:i-1
            L(i,j) = U(i,j)/U(j,j)
            U(i,:) = U(i,:) - U(j,:)*L(i,j)
        end
        disp("----------------")
    end

    disp("Check the answer of L*U: "), disp(L*U)
end

6. Search for failed tests


You should always try to “break” your code. Here (obviously) there is no complete “protection from the fool”, but I took as the basis the assumption that the initially correct matrix will be introduced.

At this step, you should find all possible errors, inaccuracies in your code, as a result of which the bias of the constructed algorithm may also be revealed. Therefore, here you should try out as many omissions that could have been made.

7. Debug failed tests


The most logical, and probably expected of the steps. The code must be biased and the algorithm corrected. There is nothing more to be added here.

Conclusion


I can say that it is unlikely that I would ever seriously take this approach to solving problems, if not for my sleepy state. But it is worth paying attention to this method - it is short, and all its laboriousness consists only in the fact that it must be used.

Note


Thanks to the linear algebra textbooks for the material for solving the problem.

All Articles