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)
 
void display (double **A, int N)
 
int main (int argc, char **argv)
 

Detailed Description

LU decomposition of a square matrix

Author
Krishna Vedala

Function Documentation

◆ display()

void display ( double **  A,
int  N 
)

Function to display square matrix

69 {
70  for (int i = 0; i < N; i++)
71  {
72  for (int j = 0; j < N; j++)
73  {
74  printf("% 3.3g \t", A[i][j]);
75  }
76  putchar('\n');
77  }
78 }

◆ 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++)
36  lu_sum += L[row][j] * U[j][col];
37 
38  // Evaluate U[i,k]
39  U[row][col] = A[row][col] - lu_sum;
40  }
41 
42  // Lower triangular matrix
43 #ifdef _OPENMP
44 #pragma omp for
45 #endif
46  for (col = row; col < mat_size; col++)
47  {
48  if (row == col)
49  {
50  L[row][col] = 1.;
51  continue;
52  }
53 
54  // Summation of L[i,j] * U[j,k]
55  double lu_sum = 0.;
56  for (j = 0; j < row; j++)
57  lu_sum += L[col][j] * U[j][row];
58 
59  // Evaluate U[i,k]
60  L[col][row] = (A[col][row] - lu_sum) / U[row][row];
61  }
62  }
63 
64  return 0;
65 }

◆ main()

int main ( int  argc,
char **  argv 
)

Main function

82 {
83  int mat_size = 3; // default matrix size
84  const int range = 10;
85  const int range2 = range >> 1;
86 
87  if (argc == 2)
88  mat_size = atoi(argv[1]);
89 
90  srand(time(NULL)); // random number initializer
91 
92  /* Create a square matrix with random values */
93  double **A = (double **)malloc(mat_size * sizeof(double *));
94  double **L = (double **)malloc(mat_size * sizeof(double *)); // output
95  double **U = (double **)malloc(mat_size * sizeof(double *)); // output
96  for (int i = 0; i < mat_size; i++)
97  {
98  // calloc so that all valeus are '0' by default
99  A[i] = (double *)calloc(mat_size, sizeof(double));
100  L[i] = (double *)calloc(mat_size, sizeof(double));
101  U[i] = (double *)calloc(mat_size, sizeof(double));
102  for (int j = 0; j < mat_size; j++)
103  /* create random values in the limits [-range2, range-1] */
104  A[i][j] = (double)(rand() % range - range2);
105  }
106 
107  lu_decomposition(A, L, U, mat_size);
108 
109  printf("A = \n");
110  display(A, mat_size);
111  printf("\nL = \n");
112  display(L, mat_size);
113  printf("\nU = \n");
114  display(U, mat_size);
115 
116  /* Free dynamically allocated memory */
117  for (int i = 0; i < mat_size; i++)
118  {
119  free(A[i]);
120  free(L[i]);
121  free(U[i]);
122  }
123  free(A);
124  free(L);
125  free(U);
126 
127  return 0;
128 }
Here is the call graph for this function:
L
Definition: list.h:8
data
Definition: prime_factoriziation.c:25
N
#define N
Definition: sol1.c:111
lu_decomposition
int lu_decomposition(double **A, double **L, double **U, int mat_size)
Definition: lu_decompose.c:20
display
void display(double **A, int N)
Definition: lu_decompose.c:68