Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. /**
  2. * This is homework5, which deals with A equation using
  3. * 1. Gauss-Jordan Elimination
  4. * 2. LU Decomposition
  5. * 3. Singular Value Decomposition
  6. **/
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. // compile with gcc -g -o main *.c -lm
  12.  
  13. // 1. Ax=b 풀기
  14. // 여기에서 Ai와 bi는 course homepage에서 확인 가능
  15. // 2. iterative improvement method
  16. // 3. A의 inverse와 determinant 구하기
  17.  
  18. void gaussj(float **a, int n, float **b, int m);
  19. void ludcmp(float **a, int n, int *indx, float *d);
  20. void lubksb(float **a, int n, int *indx, float *d);
  21. void svdcmp(float **a, int m, int n, float w[], float **v);
  22. void svbksb(float **u, float w[], float **v, int m, int n, float b[], float x[]);
  23. void mprove(float **a, float **alud, int n, int indx[], float b[], float x[]);
  24.  
  25. int main(void){
  26. int three = 2;
  27. for(; three <= 3; three++){
  28. char filename[20];
  29. sprintf(filename, "lineq%d.dat", three);
  30. printf("\nfile %s:\n", filename);
  31. FILE* file = fopen(filename, "r");
  32. int row, col;
  33. fscanf(file, "%d %d", &row, &col);
  34. row++, col++;
  35. float **A = (float**)malloc(row*sizeof(float*));
  36. for(int i = 0; i < row; i++)
  37. A[i] = (float*)malloc(col*sizeof(float));
  38. float *b = (float*)malloc(col*sizeof(float));
  39. row--, col--;
  40. printf("\nGiven matrix A: \n");
  41. for(int i = 1; i <= row; i++){
  42. for(int j = 1; j <= col; j++){
  43. fscanf(file, "%f", &A[i][j]);
  44. printf("%.2f\t", A[i][j]);
  45. }
  46. printf("\n");
  47. }
  48. printf("Given matrix b:\n");
  49. for(int i = 1; i <= col; i++){
  50. fscanf(file, "%f\t", &b[i]);
  51. printf("%.2f ", b[i]);
  52. }
  53. printf("\n");
  54. // Gauss-Jordan elimination
  55. printf("\nGauss-Joran elimination:\n");
  56. // need to do memcpy, since the function changes the value passed by pointer
  57. row++, col++;
  58. float **tmp_A = (float**)malloc(row*sizeof(float*));
  59. for(int i = 0; i < row; i++){
  60. tmp_A[i] = (float*)malloc(col*sizeof(float));
  61. memcpy(tmp_A[i], A[i], sizeof(float)*col);
  62. }
  63. float **B = (float**)malloc(row*sizeof(float*));
  64. for(int i = 1; i <= row; i++)
  65. B[i] = (float*)malloc((col+1)*sizeof(float));
  66. memcpy(B, A, sizeof(float)*row*col);
  67. for(int i = 1; i <= col; i++)
  68. B[row - 1][i] = b[i];
  69. row--, col--;
  70. // do the Gauss-Jordan elimination
  71. gaussj(tmp_A, row, B, col);
  72. for(int i = 1; i <= row; i++)
  73. printf("%.2f\t", B[row][i]);
  74. printf("\n");
  75.  
  76.  
  77. // LU Decomposition
  78. // ludcmp function uses Crout's method
  79. printf("\nLU Decomposition:\n");
  80. row++, col++;
  81. for(int i = 0; i < row; i++)
  82. memcpy(tmp_A[i], A[i], sizeof(float)*col);
  83. int *idx = (int*)malloc(row*sizeof(int));
  84. float *tmp_b = (float*)malloc(col*sizeof(float));
  85. memcpy(tmp_b, b, sizeof(b));
  86. row--, col--;
  87. // do LU decomposition
  88. // ludcmp returns L and U A, and Crout's method set
  89. // U's diagonal A into 1
  90. float tmp_d;
  91. ludcmp(tmp_A, row, idx, &tmp_d);
  92. lubksb(tmp_A, row, idx, tmp_b);
  93. for(int i = 1; i <= row; i++)
  94. printf("%.2f\t", tmp_b[i]);
  95. printf("\n");
  96.  
  97. // Singular Value Decomposition
  98. printf("\nSingular Value Decomposition:\n");
  99. row++, col++;
  100. for(int i = 0; i < row; i++)
  101. memcpy(tmp_A[i], A[i], sizeof(float)*col);
  102. float *w = (float*)malloc(row * sizeof(float));
  103. float **V = (float**)malloc(row*sizeof(float*));
  104. memcpy(tmp_b, b, sizeof(b));
  105. float *x = (float*)malloc(row * sizeof(float));
  106. for(int i = 0; i < row; i++)
  107. V[i] = (float*)malloc(col*sizeof(float));
  108. row--, col--;
  109. svdcmp(tmp_A, row, col, w, V);
  110. svbksb(tmp_A, w, V, row, col, tmp_b, x);
  111. for(int i = 1; i <= row; i++)
  112. printf("%.2f\t", x[i]);
  113. printf("\n");
  114.  
  115. row++, col++;
  116. for(int i = 0; i < row; i++){
  117. free(tmp_A[i]);
  118. free(A[i]);
  119. }
  120. free(tmp_A);
  121. free(A);
  122. }
  123. return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement