mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-26 15:29:43 +03:00
Merge pull request #13 from kvedala/documentation/fixes
[feat] LU decomposition
This commit is contained in:
commit
2d0ddc3a77
@ -60,11 +60,11 @@ if(USE_OPENMP)
|
|||||||
endif()
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
add_subdirectory(conversions)
|
|
||||||
add_subdirectory(misc)
|
add_subdirectory(misc)
|
||||||
add_subdirectory(project_euler)
|
|
||||||
add_subdirectory(sorting)
|
add_subdirectory(sorting)
|
||||||
add_subdirectory(searching)
|
add_subdirectory(searching)
|
||||||
|
add_subdirectory(conversions)
|
||||||
|
add_subdirectory(project_euler)
|
||||||
add_subdirectory(machine_learning)
|
add_subdirectory(machine_learning)
|
||||||
add_subdirectory(numerical_methods)
|
add_subdirectory(numerical_methods)
|
||||||
|
|
||||||
|
@ -242,6 +242,7 @@
|
|||||||
* [Gauss Elimination](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/gauss_elimination.c)
|
* [Gauss Elimination](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/gauss_elimination.c)
|
||||||
* [Gauss Seidel Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/gauss_seidel_method.c)
|
* [Gauss Seidel Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/gauss_seidel_method.c)
|
||||||
* [Lagrange Theorem](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/lagrange_theorem.c)
|
* [Lagrange Theorem](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/lagrange_theorem.c)
|
||||||
|
* [Lu Decompose](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/lu_decompose.c)
|
||||||
* [Mean](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/mean.c)
|
* [Mean](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/mean.c)
|
||||||
* [Median](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/median.c)
|
* [Median](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/median.c)
|
||||||
* [Newton Raphson Root](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/newton_raphson_root.c)
|
* [Newton Raphson Root](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/numerical_methods/newton_raphson_root.c)
|
||||||
|
@ -38,11 +38,12 @@ char *abbreviate(const char *phrase)
|
|||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
counter++;
|
counter++;
|
||||||
char words[counter][80];
|
char **words = (char **)malloc(counter * sizeof(char *));
|
||||||
|
|
||||||
/* initalizes words-array with empty strings */
|
/* initalizes words-array with empty strings */
|
||||||
for (i = 0; i < counter; i++)
|
for (i = 0; i < counter; i++)
|
||||||
{
|
{
|
||||||
|
words[i] = (char *)malloc(80 * sizeof(char));
|
||||||
strcpy(words[i], "");
|
strcpy(words[i], "");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,5 +84,9 @@ char *abbreviate(const char *phrase)
|
|||||||
strcat(acr, words[i]);
|
strcat(acr, words[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < counter; i++)
|
||||||
|
free(words[i]);
|
||||||
|
free(words);
|
||||||
|
|
||||||
return acr;
|
return acr;
|
||||||
}
|
}
|
117
numerical_methods/lu_decompose.c
Normal file
117
numerical_methods/lu_decompose.c
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
/**
|
||||||
|
* \file
|
||||||
|
* \brief [LU decomposition](https://en.wikipedia.org/wiki/LU_decompositon) of a
|
||||||
|
* square matrix
|
||||||
|
* \author [Krishna Vedala](https://github.com/kvedala)
|
||||||
|
*/
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
#ifdef _OPENMP
|
||||||
|
#include <omp.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/** Perform LU decomposition on matrix
|
||||||
|
* \param[in] A matrix to decompose
|
||||||
|
* \param[out] L output L matrix
|
||||||
|
* \param[out] U output U matrix
|
||||||
|
* \param[in] mat_size input square matrix size
|
||||||
|
*/
|
||||||
|
int lu_decomposition(double **A, double **L, double **U, int mat_size)
|
||||||
|
{
|
||||||
|
int row, col, j;
|
||||||
|
|
||||||
|
// regularize each row
|
||||||
|
for (row = 0; row < mat_size; row++)
|
||||||
|
{
|
||||||
|
// Upper triangular matrix
|
||||||
|
#ifdef _OPENMP
|
||||||
|
#pragma omp for
|
||||||
|
#endif
|
||||||
|
for (col = row; col < mat_size; col++)
|
||||||
|
{
|
||||||
|
// Summation of L[i,j] * U[j,k]
|
||||||
|
double lu_sum = 0.;
|
||||||
|
for (j = 0; j < row; j++)
|
||||||
|
lu_sum += L[row][j] * U[j][col];
|
||||||
|
|
||||||
|
// Evaluate U[i,k]
|
||||||
|
U[row][col] = A[row][col] - lu_sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lower triangular matrix
|
||||||
|
#ifdef _OPENMP
|
||||||
|
#pragma omp for
|
||||||
|
#endif
|
||||||
|
for (col = row; col < mat_size; col++)
|
||||||
|
{
|
||||||
|
if (row == col)
|
||||||
|
{
|
||||||
|
L[row][col] = 1.;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Summation of L[i,j] * U[j,k]
|
||||||
|
double lu_sum = 0.;
|
||||||
|
for (j = 0; j < row; j++)
|
||||||
|
lu_sum += L[col][j] * U[j][row];
|
||||||
|
|
||||||
|
// Evaluate U[i,k]
|
||||||
|
L[col][row] = (A[col][row] - lu_sum) / U[row][row];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Function to display square matrix */
|
||||||
|
void display(double **A, int N)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < N; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < N; j++)
|
||||||
|
{
|
||||||
|
printf("% 3.3g \t", A[i][j]);
|
||||||
|
}
|
||||||
|
putchar('\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Main function */
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int mat_size = 3; // default matrix size
|
||||||
|
const int range = 10;
|
||||||
|
const int range2 = range >> 1;
|
||||||
|
|
||||||
|
if (argc == 2)
|
||||||
|
mat_size = atoi(argv[1]);
|
||||||
|
|
||||||
|
srand(time(NULL)); // random number initializer
|
||||||
|
|
||||||
|
/* Create a square matrix with random values */
|
||||||
|
double **A = (double **)malloc(mat_size * sizeof(double *));
|
||||||
|
double **L = (double **)malloc(mat_size * sizeof(double *)); // output
|
||||||
|
double **U = (double **)malloc(mat_size * sizeof(double *)); // output
|
||||||
|
for (int i = 0; i < mat_size; i++)
|
||||||
|
{
|
||||||
|
// calloc so that all valeus are '0' by default
|
||||||
|
A[i] = (double *)calloc(mat_size, sizeof(double));
|
||||||
|
L[i] = (double *)calloc(mat_size, sizeof(double));
|
||||||
|
U[i] = (double *)calloc(mat_size, sizeof(double));
|
||||||
|
for (int j = 0; j < mat_size; j++)
|
||||||
|
/* create random values in the limits [-range2, range-1] */
|
||||||
|
A[i][j] = (double)(rand() % range - range2);
|
||||||
|
}
|
||||||
|
|
||||||
|
lu_decomposition(A, L, U, mat_size);
|
||||||
|
|
||||||
|
printf("A = \n");
|
||||||
|
display(A, mat_size);
|
||||||
|
printf("\nL = \n");
|
||||||
|
display(L, mat_size);
|
||||||
|
printf("\nU = \n");
|
||||||
|
display(U, mat_size);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
@ -19,6 +19,6 @@ foreach( testsourcefile ${APP_SOURCES} )
|
|||||||
if(MATH_LIBRARY)
|
if(MATH_LIBRARY)
|
||||||
target_link_libraries(${testname} ${MATH_LIBRARY})
|
target_link_libraries(${testname} ${MATH_LIBRARY})
|
||||||
endif()
|
endif()
|
||||||
install(TARGETS ${testname} DESTINATION "bin/misc")
|
install(TARGETS ${testname} DESTINATION "bin/project_euler")
|
||||||
|
|
||||||
endforeach( testsourcefile ${APP_SOURCES} )
|
endforeach( testsourcefile ${APP_SOURCES} )
|
||||||
|
@ -26,10 +26,9 @@ long MAX_N = 28123; /**< Limit of numbers to check */
|
|||||||
char *abundant_flags = NULL;
|
char *abundant_flags = NULL;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns:
|
* \returns -1 if N is deficient
|
||||||
* -1 if N is deficient
|
* \returns 1 if N is abundant
|
||||||
* 1 if N is abundant
|
* \returns 0 if N is perfect
|
||||||
* 0 if N is perfect
|
|
||||||
**/
|
**/
|
||||||
char get_perfect_number(unsigned long N)
|
char get_perfect_number(unsigned long N)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user