LU decomposition of a band matrix
Time limit3sMemory limit128 MB
Factor a tridiagonal matrix into unit-diagonal L and upper U, printing -1 when the factors do not exist or a pivot is zero.
- Level
Medium5 of 10
- Topics
- Matrix, Math, Implementation
- Solved
- No attempts yet
Problem
You are given an matrix . An LU decomposition splits into a product , where is a lower triangular matrix and is an upper triangular matrix.
- A lower triangular matrix has whenever .
- An upper triangular matrix has whenever .
This problem only deals with band matrices whose band has width 1. In other words, carries values on the main diagonal and on the two diagonals next to it, and every other entry is 0.
Find and such that every diagonal entry of is 1. When such a pair exists, it is unique.
Input
The first line contains the size of the square matrix (). Each of the next lines contains the entries of one row of , in order.
Every entry is an integer with absolute value at most 1000, and every position with holds 0. For inputs whose decomposition exists, every entry of and has absolute value at most .
Output
If the decomposition exists and no diagonal entry of is 0, print on lines and then on the next lines. Each line holds the entries of that row, separated by one space.
Round the exact value of each entry at the fourth decimal place and print three decimals. Round halves away from zero, so becomes 0.063 and becomes -0.063. When the rounded value is zero, print 0.000 without a sign.
If the decomposition is impossible, or some diagonal entry of is 0, print -1 on the first line and nothing else.