Last active
August 29, 2015 14:11
-
-
Save samrat/53ea2374f62a0124a74a to your computer and use it in GitHub Desktop.
LU factorization of square matrices. This is an implementation of the algorithm given in Strang's Intro to Linear Algebra textbook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <math.h> | |
void square_lu(float A[][3], float L[][3], float U[][3]) | |
{ | |
int n = 3; | |
for (int k = 0; k < n; k++) | |
{ | |
L[k][k] = 1.0f; | |
for (int i = k+1; i < n; i++) | |
{ | |
L[i][k] = A[i][k] / A[k][k]; /* load multipliers for column k into L */ | |
for (int j = k+1; j < n; j++) | |
{ | |
A[i][j] = A[i][j] - (L[i][k] * A[k][j]); | |
} | |
} | |
for (int j = k; j < n; j++) | |
{ | |
U[k][j] = A[k][j]; | |
} | |
} | |
} | |
int main() | |
{ | |
float L[3][3]; | |
float U[3][3]; | |
float A[3][3] = {1,2,3, 4,5,6, 7,8,10}; | |
square_lu(A, L, U); | |
printf("L:\n"); | |
for (int i = 0; i < 3; i++) | |
{ | |
for (int j = 0; j < 3; j++) | |
{ | |
printf("%.2f\t", L[i][j]); | |
} | |
printf("\n"); | |
} | |
printf("\nU:\n"); | |
for (int i = 0; i < 3; i++) | |
{ | |
for (int j = 0; j < 3; j++) | |
{ | |
printf("%.2f\t", U[i][j]); | |
} | |
printf("\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment