Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
lu_decompose.c File Reference

LU decomposition of a square matrix More...

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
Include dependency graph for lu_decompose.c:

Functions

int lu_decomposition (double **A, double **L, double **U, int mat_size)
 Perform LU decomposition on matrix. More...
 
void display (double **A, int N)
 Function to display square matrix.
 
int main (int argc, char **argv)
 Main function.
 

Detailed Description

LU decomposition of a square matrix

Author
Krishna Vedala

Function Documentation

◆ lu_decomposition()

int lu_decomposition ( double **  A,
double **  L,
double **  U,
int  mat_size 
)

Perform LU decomposition on matrix.

Parameters
[in]Amatrix to decompose
[out]Loutput L matrix
[out]Uoutput U matrix
[in]mat_sizeinput square matrix size
21 {
22  int row, col, j;
23 
24  // regularize each row
25  for (row = 0; row < mat_size; row++)
26  {
27  // Upper triangular matrix
28 #ifdef _OPENMP
29 #pragma omp for
30 #endif
31  for (col = row; col < mat_size; col++)
32  {
33  // Summation of L[i,j] * U[j,k]
34  double lu_sum = 0.;
35  for (j = 0; j < row; j++) lu_sum += L[row][j] * U[j][col];
36 
37  // Evaluate U[i,k]
38  U[row][col] = A[row][col] - lu_sum;
39  }
40 
41  // Lower triangular matrix
42 #ifdef _OPENMP
43 #pragma omp for
44 #endif
45  for (col = row; col < mat_size; col++)
46  {
47  if (row == col)
48  {
49  L[row][col] = 1.;
50  continue;
51  }
52 
53  // Summation of L[i,j] * U[j,k]
54  double lu_sum = 0.;
55  for (j = 0; j < row; j++) lu_sum += L[col][j] * U[j][row];
56 
57  // Evaluate U[i,k]
58  L[col][row] = (A[col][row] - lu_sum) / U[row][row];
59  }
60  }
61 
62  return 0;
63 }
L
Definition: list.h:8