Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
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.
 
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

◆ display()

void display ( double **  A,
int  N 
)

Function to display square matrix.

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

◆ 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}
Definition list.h:8

◆ main()

int main ( int  argc,
char **  argv 
)

Main function.

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