fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #define N 10
  6. #define TMAX 100
  7. #define EPS (10e-6)
  8.  
  9. void init_vector();
  10. double cg_method(double **matrix_a, double *vector_x, double *vector_b);
  11. void init_vector(double *vector_x);
  12. void vector_x_matrix(double *vector_y, double **matrix_a,double *vector_x);
  13. double dot_product(double *vector_x, double *vector_y);
  14. double vector_norm(double *vector_x);
  15.  
  16. int main (void){
  17. FILE *fin, *fout;
  18. static double **matrix_a;
  19. static double *vector_b;
  20. static double *vector_x;
  21. int i,j;
  22.  
  23. matrix_a = (double**)malloc(sizeof(double*)*N);
  24. for(i=0;i<N;i++){
  25. matrix_a[i] = (double*)malloc(sizeof(double)*N);
  26. }
  27.  
  28. vector_b = (double*)malloc(sizeof(double)*N);
  29. vector_x = (double*)malloc(sizeof(double)*N);
  30.  
  31. if((fin = fopen("input_sp.dat","r"))==NULL)
  32. {
  33. printf("no file :input_sp.dat\n");
  34. }
  35. if((fout = fopen("output_sp.dat","w"))==NULL)
  36. {
  37. printf("do not maike file : output_sp.dat\n");
  38. exit(1);
  39. }
  40.  
  41. fprintf( fout, "A\n");
  42. printf("matrix_a\n");
  43. for( i = 0 ; i < N ; i++)
  44. {
  45. for( j = 0 ; j < N ; j++)
  46. {
  47. fscanf(fin, "%lf", &matrix_a[i][j]);
  48. fprintf(fout, "%5.2f\t", matrix_a[i][j]);
  49. printf("%lf\n",matrix_a[i][j]);
  50. }
  51. printf("\n");
  52. fprintf( fout, "\n");
  53. }
  54.  
  55. fprintf( fout, "b\n");
  56. printf("vector_b\n");
  57. for( i = 0 ; i < N ; i++)
  58. {
  59. fscanf(fin, "%lf", &vector_b[i]);
  60. fprintf(fout, "%5.2f\t", vector_b[i]);
  61. fprintf( fout, "\n");
  62. printf("%lf\n",vector_b[i]);
  63. }
  64.  
  65. printf("\n");
  66.  
  67. fprintf( fout, "x\n");
  68. printf("vector_x\n");
  69. for( i = 0 ; i < N ; i++)
  70. {
  71. fscanf(fin, "%lf", &vector_x[i]);
  72. fprintf(fout, "%5.2f\t", vector_x[i]);
  73. fprintf( fout, "\n");
  74. printf("%lf\n",vector_x[i]);
  75. }
  76.  
  77. printf("\n");
  78.  
  79. vector_x = cg_method(double **matrix_a,double *vector_x,double *vector_b);
  80.  
  81. fprintf( fout, "Ax=b is solved\n");
  82. for( i = 1 ; i <= N ; i++ )
  83. {
  84. fprintf(fout, "%f\n", vector_x[i]);
  85. }
  86. fclose(fin);
  87. fclose(fout);
  88.  
  89.  
  90. return 0;
  91. }
  92.  
  93. void init_vector(double *vector_x)
  94. {
  95. int i;
  96. for(i = 0; i < N; i++){
  97. vector_x[i] = 0;
  98. }
  99. }
  100.  
  101. void vector_x_matrix(double *vector_y,double *matrix_a,double *vector_x)
  102. {
  103. int i, j;
  104. double vxm;
  105. for(i = 0; i < N; i++){
  106. vxm = 0;
  107. for(j = 0; j < N; j++){
  108. vxm += matrix_a[i][j] * vector_x[i];
  109. }
  110. vector_y[i] = vxm;
  111. }
  112. }
  113.  
  114. double dot_product(double *vector_x,double *vector_y)
  115. {
  116. int i;
  117. double dot_p = 0;
  118. for(i = 0; i < N; i++){
  119. dot_p += vector_x[i] * vector_y[i];
  120. }
  121. return dot_p;
  122. }
  123.  
  124. double vector_norm(vector_x)
  125. {
  126. int i;
  127. double norm = 0;
  128. for(i = 0; i < N; i++){
  129. norm += fabs(vector_x[i]);
  130. }
  131. return norm;
  132. }
  133.  
  134. double cg_method(double **matrix_a, double *vector_x, double *vector_b)
  135. {
  136. double *vector_p;
  137. double *vector_r;
  138. double *vector_ax;
  139. double *vector_ap;
  140. int i, iter;
  141.  
  142. vector_p = (double*)malloc(sizeof(double)*N);
  143. vector_r = (double*)malloc(sizeof(double)*N);
  144. vector_ax = (double*)malloc(sizeof(double)*N);
  145. vector_ap = (double*)malloc(sizeof(double)*N);
  146.  
  147. vector_x_matrix(double *vector_ax, double *matrix_a, double *vector_x);
  148.  
  149. for(i = 0; i < N; i++){
  150. vector_p[i] = vector_b[i] - vector_ax[i];
  151. vector_r[i] = vector_p[i];
  152. }
  153.  
  154. for(iter = 1; iter < TMAX; iter++){
  155. double alpha, beta, err = 0;
  156.  
  157. vector_x_matrix(double *vector_ap, double *matrix_a, double *vector_p);
  158. alpha = +dot_product(double *vector_p, *vector_r)/dot_product(double *vector_p, double *vector_ap);
  159.  
  160. for(i = 0; i < N; i++){
  161. vector_x[i] += +alpha * vector_p[i];
  162. vector_r[i] += -alpha * vector_ap[i];
  163. }
  164.  
  165. err = vector_norm(r);
  166. printf("LOOP : %d\t Error : %g\n", iter, err);
  167. if(EPS > err) break;
  168.  
  169. beta = -dot_product(double *vector_r, double *vector_ap)/dot_product(double *vector_p, double *vector_ap);
  170. for(i = 0; i < N; i++){
  171. vector_p[i] = vector_r[i] + beta * vector_p[i];
  172. }
  173. }
  174. }
  175.  
  176.  
  177. -----------------------------------
  178. input_sp.dat
  179.  
  180. 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
  181. 1.000000 3.000000 3.000000 3.000000 3.000000 3.000000 3.000000 3.000000 3.000000 3.000000
  182. 1.000000 3.000000 5.000000 5.000000 5.000000 5.000000 5.000000 5.000000 5.000000 5.000000
  183. 1.000000 3.000000 5.000000 7.000000 7.000000 7.000000 7.000000 7.000000 7.000000 7.000000
  184. 1.000000 3.000000 5.000000 7.000000 9.000000 9.000000 9.000000 9.000000 9.000000 9.000000
  185. 1.000000 3.000000 5.000000 7.000000 9.000000 11.000000 11.000000 11.000000 11.000000 11.000000
  186. 1.000000 3.000000 5.000000 7.000000 9.000000 11.000000 13.000000 13.000000 13.000000 13.000000
  187. 1.000000 3.000000 5.000000 7.000000 9.000000 11.000000 13.000000 15.000000 15.000000 15.000000
  188. 1.000000 3.000000 5.000000 7.000000 9.000000 11.000000 13.000000 15.000000 17.000000 17.000000
  189. 1.000000 3.000000 5.000000 7.000000 9.000000 11.000000 13.000000 15.000000 17.000000 19.000000
  190.  
  191. 10.000000 28.000000 44.000000 58.000000 70.000000 80.000000 88.000000 94.000000 98.000000 100.000000
  192.  
  193. 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
  194.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘main’:
prog.c:79: error: expected expression before ‘double’
prog.c:79: error: too few arguments to function ‘cg_method’
prog.c:47: warning: ignoring return value of ‘fscanf’, declared with attribute warn_unused_result
prog.c:59: warning: ignoring return value of ‘fscanf’, declared with attribute warn_unused_result
prog.c:71: warning: ignoring return value of ‘fscanf’, declared with attribute warn_unused_result
prog.c: At top level:
prog.c:101: error: conflicting types for ‘vector_x_matrix’
prog.c:12: error: previous declaration of ‘vector_x_matrix’ was here
prog.c: In function ‘vector_x_matrix’:
prog.c:108: error: subscripted value is neither array nor pointer
prog.c: In function ‘vector_norm’:
prog.c:125: error: argument ‘vector_x’ doesn’t match prototype
prog.c:14: error: prototype declaration
prog.c:129: error: subscripted value is neither array nor pointer
prog.c: In function ‘cg_method’:
prog.c:147: error: expected expression before ‘double’
prog.c:147: error: too few arguments to function ‘vector_x_matrix’
prog.c:157: error: expected expression before ‘double’
prog.c:157: error: too few arguments to function ‘vector_x_matrix’
prog.c:158: error: expected expression before ‘double’
prog.c:158: error: too few arguments to function ‘dot_product’
prog.c:158: error: expected expression before ‘double’
prog.c:158: error: too few arguments to function ‘dot_product’
prog.c:165: error: ‘r’ undeclared (first use in this function)
prog.c:165: error: (Each undeclared identifier is reported only once
prog.c:165: error: for each function it appears in.)
prog.c:169: error: expected expression before ‘double’
prog.c:169: error: too few arguments to function ‘dot_product’
prog.c:169: error: expected expression before ‘double’
prog.c:169: error: too few arguments to function ‘dot_product’
prog.c: At top level:
prog.c:177: error: expected identifier or ‘(’ before ‘--’ token
stdout
Standard output is empty