LU decomposition of a square matrix
More...
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
|
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. More...
|
|
int | main (int argc, char **argv) |
| Main function. More...
|
|
◆ 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] | A | matrix to decompose |
[out] | L | output L matrix |
[out] | U | output U matrix |
[in] | mat_size | input square matrix size |
21{
22 int row, col, j;
23
24
25 for (row = 0; row < mat_size; row++)
26 {
27
28#ifdef _OPENMP
29#pragma omp for
30#endif
31 for (col = row; col < mat_size; col++)
32 {
33
34 double lu_sum = 0.;
35 for (j = 0; j < row; j++) lu_sum +=
L[row][j] * U[j][col];
36
37
38 U[row][col] = A[row][col] - lu_sum;
39 }
40
41
42#ifdef _OPENMP
43#pragma omp for
44#endif
45 for (col = row; col < mat_size; col++)
46 {
47 if (row == col)
48 {
50 continue;
51 }
52
53
54 double lu_sum = 0.;
55 for (j = 0; j < row; j++) lu_sum +=
L[col][j] * U[j][row];
56
57
58 L[col][row] = (A[col][row] - lu_sum) / U[row][row];
59 }
60 }
61
62 return 0;
63}
◆ main()
int main |
( |
int |
argc, |
|
|
char ** |
argv |
|
) |
| |
Main function.
80{
81 int mat_size = 3;
83 const int range2 =
range >> 1;
84
85 if (argc == 2)
86 mat_size = atoi(argv[1]);
87
88 srand(time(NULL));
89
90
91 double **A = (
double **)
malloc(mat_size *
sizeof(
double *));
92 double **
L = (
double **)
malloc(mat_size *
sizeof(
double *));
93 double **U = (
double **)
malloc(mat_size *
sizeof(
double *));
94 for (int i = 0; i < mat_size; i++)
95 {
96
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
102 A[i][j] = (
double)(rand() %
range - range2);
103 }
104
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
115 for (int i = 0; i < mat_size; i++)
116 {
120 }
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