Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 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. int 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 = 1;
  27. for(; three <= 3; three++){
  28. char filename[20];
  29. sprintf(filename, "lineq%d.dat", three);
  30. printf("\n*********************\nfile %s:\n*********************", filename);
  31. FILE* file = fopen(filename, "r");
  32. int row, col;
  33. fscanf(file, "%d %d", &row, &col);
  34. float **A = (float**)malloc((row+1)*sizeof(float*));
  35. for(int i = 0; i <= row; i++)
  36. A[i] = (float*)malloc((col+1)*sizeof(float));
  37. float *b = (float*)malloc((col+1)*sizeof(float));
  38.  
  39. for(int i = 1; i <= row; i++)
  40. for(int j = 1; j <= col; j++)
  41. fscanf(file, "%f", &A[i][j]);
  42.  
  43. for(int i = 1; i <= col; i++)
  44. fscanf(file, "%f\t", &b[i]);
  45.  
  46. // Gauss-Jordan elimination
  47. // need to do memcpy, since the function changes the value passed by pointer
  48. float **tmp_A = (float**)malloc((row+1)*sizeof(float*));
  49. for(int i = 0; i <= row; i++)
  50. tmp_A[i] = (float*)malloc((col+1)*sizeof(float));
  51. for(int i = 1; i <= row; i++)
  52. for(int j = 1; j <= col; j++)
  53. tmp_A[i][j] = A[i][j];
  54. float **B = (float**)malloc((row+1)*sizeof(float*));
  55. for(int i = 0; i <= row; i++)
  56. B[i] = (float*)malloc((2)*sizeof(float));
  57. for(int i = 1; i <= row; i++)
  58. B[i][1] = b[i];
  59. // do the Gauss-Jordan elimination
  60. if(gaussj(tmp_A, row, B, 1) < 0)
  61. printf("\nGauss-Joran Elimination: Singular Matrix");
  62. else{
  63. printf("\nInverse Matrix:\n");
  64. for(int i = 1; i <= row; i++){
  65. for(int j = 1; j <= col; j++){
  66. printf("%f\t", tmp_A[i][j]);
  67. }
  68. printf("\n");
  69. }
  70. printf("\nGauss-Joran elimination:\n");
  71. for(int i = 1; i <= row; i++)
  72. printf("%f\t", B[i][1]);
  73. }
  74.  
  75. // LU Decomposition
  76. // ludcmp function uses Crout's method
  77. printf("\nLU Decomposition:\n");
  78. for(int i = 1; i <= row; i++)
  79. for(int j = 1; j <= col; j++)
  80. tmp_A[i][j] = A[i][j];
  81. int *idx = (int*)malloc((row+1)*sizeof(int));
  82. float *tmp_b = (float*)malloc((row+1)*sizeof(float));
  83. for(int i = 1; i <= row; i++)
  84. tmp_b[i] = b[i];
  85. // do LU decomposition
  86. // ludcmp returns L and U A, and Crout's method set
  87. // U's diagonal A into 1
  88. float tmp_d;
  89. ludcmp(tmp_A, row, idx, &tmp_d);
  90. float det = 1.0;
  91. for(int i = 1; i <= row; i++)
  92. det *= tmp_A[i][i];
  93. lubksb(tmp_A, row, idx, tmp_b);
  94. for(int i = 1; i <= row; i++)
  95. printf("%f\t", tmp_b[i]);
  96.  
  97. // Singular Value Decomposition
  98. printf("\nSingular Value Decomposition:\n");
  99. for(int i = 1; i <= row; i++)
  100. for(int j = 1; j <= col; j++)
  101. tmp_A[i][j] = A[i][j];
  102. float *w = (float*)malloc((row+1)*sizeof(float));
  103. float **V = (float**)malloc((row+1)*sizeof(float*));
  104. for(int i = 1; i <= row; i++)
  105. tmp_b[i] = b[i];
  106. float *x = (float*)malloc((row+1)*sizeof(float));
  107. for(int i = 0; i <= row; i++)
  108. V[i] = (float*)malloc((col+1)*sizeof(float));
  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("%f\t", x[i]);
  113. printf("\n");
  114.  
  115. printf("determinant: %f\n", det);
  116.  
  117. // mprove
  118. printf("\nmprove with LU Decomposition:\n");
  119. for(int i = 1; i <= row; i++)
  120. for(int j = 1; j <= col; j++)
  121. tmp_A[i][j] = A[i][j];
  122. for(int i = 1; i <= row; i++)
  123. tmp_b[i] = b[i];
  124. ludcmp(tmp_A, row, idx, &tmp_d);
  125. lubksb(tmp_A, row, idx, tmp_b);
  126. mprove(A, tmp_A, row, idx, b, tmp_b);
  127. for(int i = 1; i <= row; i++)
  128. printf("%f\t", tmp_b[i]);
  129. printf("\n");
  130.  
  131. // for(int i = 0; i <= row; i++){
  132. // free(tmp_A[i]);
  133. // free(V[i]);
  134. // free(A[i]);
  135. // }
  136. // free(tmp_A);
  137. // free(tmp_b);
  138. // free(V);
  139. // free(w);
  140. // free(x);
  141. // free(A);
  142. // free(b);
  143. }
  144. return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement