# C Program for LU Factorization

9
14411

For this C program for LU factorization, consider a general linear system AX = b, such that the given matrix [A] is factorized into the product of two upper and lower triangular matrices. The solution of linear simultaneous equations sought this way is called LU factorization method. This method is also known as the Triangular method or the LU Decomposition method.

So, the basic principle here is – “A square matrix [A] can be written as the product of a lower triangular matrix [L] and an upper triangular matrix [U], one of them being unit triangular, if all the principal minors of [A] are non-singular. Now, see how the matrix undergoes decomposition in the program for LU Factorization in C language.

[A] = [L][U]

where, L is the lower triangular matrix and U is the upper triangular matrix. The elements of these three matrices row-wise are:

[A] = {a11, a12, a13, a21, a22, a23, a31, a32, a33}
[L] = {1, 0, 0, l21, 1, 0, l31, l32, 1}
[U] = {u11, u12, u13, 0, u22, u23, 0, 0, u33}

The representation obtained upon putting these elements in the 1st expression aforementioned is termed as LU factorization. It is simply a product of a lower and an upper triangular matrix decomposed from the parent matrix A. The diagonal non-zero entry of [L] is the ith pivot during factorization. The elements of matrix A can be obtained by multiplying [L] and [U].

This factorization method is preferred over the Gauss Elimination method in computers and programming languages like C. It facilitates obtaining products in double length. This C program below illustrates the application of LU Factorization method based on the things mentioned above.

## Source Code for LU Factorization in C:

### Input/Output: If you have any questions related to this post – the LU Factorization (LU Decomposition) method or its C source code presented above, do mention them in the comments section.

#### 9 COMMENTS

1. Xiao

Answer should be X = [3;2;4;9]

2. olivier Ndayishimiye

that is good

3. ahsan

is this LU factorization by doolittle method ?

4. Laura Moreno

Hi, I don´t understand why you put 9.3f in the [U] matrix. Thank you

• Here, 9 specifies maximum field width, whereas the 3 known as the “Precision Specifier” allocates how many digits after the decimal point.

Printing 3.141592 using %f displays 3.141592
Printing 3.141592 using %1.1f displays 3.1
Printing 3.141592 using %1.2f displays 3.14
Printing 3.141592 using %3.3f displays 3.142
Printing 3.141592 using %4.4f displays 3.1416
Printing 3.141592 using %4.5f displays 3.14159
Printing 3.141592 using %09.3f displays 00003.142
Printing 3.141592 using %9.3f displays _ _ _ _ 3.142.

• S. Somasegar

printf(“9.3f”,U[i][j]); lacks a percent sign before the 9.
It seems printf(“%9.3f”,U[i][j]); was intended instead.