fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <math.h>
  5.  
  6. #define MAXNOP 50 /*Max number of operations allowed */
  7.  
  8. struct m{
  9. size_t row;
  10. size_t col;
  11. double *data;
  12. };
  13.  
  14. struct m add(struct m *A, struct m *B, double n);
  15. struct m multiply(struct m *A, struct m *B);
  16. void f(double x);
  17. void print_matrix(struct m *A);
  18. void transpose(struct m *A);
  19. double determinant(size_t n, struct m *A);
  20. void scalar_product(double scalar, struct m *B);
  21. void inverse(size_t n, struct m *A);
  22.  
  23. int main(int argc, char *argv[]){
  24.  
  25. struct m *matrix;
  26. int id = 0; /* id of a matrix */
  27. size_t ncol,nrow; /* No of columns of a matrix*/
  28. ncol = nrow = 0;
  29. int nop = 0; /*No of operators*/
  30. int off = 0;
  31. int i;
  32. int n;
  33. double *d;
  34. int maxc=argc>2?atoi(argv[2])*atoi(argv[2]):100; /*define max dimension of a matrix */
  35. char buf[maxc]; /*to store each lines of file */
  36. char *p = buf;
  37. char op[MAXNOP];
  38.  
  39. for (i=0; i < MAXNOP; i++)
  40. op[i]='?';
  41.  
  42. FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;
  43.  
  44. if (!(matrix = malloc(maxc*sizeof *matrix))) {
  45. perror ("malloc-matrix");
  46. return 1;
  47. }
  48.  
  49. /*Read file line by line */
  50. while (fgets (buf, maxc, fp)){
  51.  
  52. if (nrow == 0){
  53. /* allocate/validate max no. of matrix */
  54. d = matrix[id].data = malloc(sizeof(double) * 10 * 10);
  55. }
  56.  
  57. /*check if line contains operator */
  58. if ( (!isdigit(*buf) && buf[1] =='\n') || buf[0] =='^')
  59. {
  60. if (buf[0] =='^' && buf[1] == '-' && buf[2] =='1')
  61. op[nop++] = 'i'; /* matrix inverse operation */
  62. else
  63. op[nop++] = *buf;
  64. matrix[id].col = ncol;
  65. matrix[id].row = nrow;
  66. nrow = ncol = 0;
  67. id++;
  68. continue;
  69. }
  70.  
  71. else /* read integers in a line into d */
  72. {
  73. while (sscanf (p + off, "%lf%n", d, &n) == 1) {
  74. d++;
  75. if(nrow == 0)
  76. ncol++;
  77. off += n;
  78. }
  79. nrow++;
  80. off = 0;
  81. }
  82. } /*end of while fgets cycle */
  83.  
  84. /*Assign last matrix No of columns and rows */
  85. matrix[id].col = ncol;
  86. matrix[id].row = nrow;
  87.  
  88. /*Printing the matrices and operations */
  89. for(i=0; i <= id; i++){
  90. print_matrix(&matrix[i]);
  91. if (op[i] == '*' || op[i] == '-' || op[i] =='+' || op[i] =='t' || op[i] =='T')
  92. printf("%c\n", op[i]);
  93. }
  94.  
  95. printf("=\n");
  96.  
  97. if(nop == 0){ /*Check if there are unary operations to perform*/
  98. if(op[0] == '?'){
  99. printf("The determinant is:\n");
  100. f(determinant(matrix[0].row, &matrix[0]));
  101. putchar('\n');
  102. return 0;
  103. }
  104. }
  105.  
  106. if (nop == 1){
  107. if(op[0] == 't' || op[0] == 'T'){
  108. transpose(&matrix[0]);
  109. printf("The transpose is\n");
  110. print_matrix(&matrix[0]); /*Print the result */
  111. free(matrix);
  112. return 0;
  113. }
  114.  
  115. if(op[0] == 'i'){
  116. if(matrix[0].row != matrix[0].col)
  117. printf("Error: You can only calculate the inverse of square matrices\n");
  118. inverse(matrix[0].row, &matrix[0]);
  119. printf("The inverse matrix is:\n");
  120. print_matrix(&matrix[0]);
  121. free(matrix);
  122. return 0;
  123. }
  124. }
  125.  
  126. /*Transpose the matrices */
  127. for(i=0; i < nop; i++){
  128.  
  129. if(op[i] == 't' || op[i] == 'T')
  130. transpose(&matrix[0]);
  131. }
  132.  
  133. for(i=0; i <= nop; i+=2)
  134. {
  135. if (op[i] == '+' && op[i+1] == '?'){
  136. matrix[i+1] = add(&matrix[i],&matrix[i+1],+1);
  137. break;
  138. }
  139.  
  140. else if(op[i] == '*' && op[i+1] == '?'){
  141. if (matrix[i].row == 1 && matrix[i].col == 1)
  142. scalar_product(matrix[i].data[0], &matrix[i+1]); //Multiplication of Scalar per matrix
  143. else{
  144. matrix[i+1] = multiply(&matrix[i],&matrix[i+1]);
  145. matrix[i+2] = multiply(&matrix[i+1],&matrix[i+2]);
  146. }
  147. break;
  148. }
  149.  
  150. else if (op[i] == '-' && op[i+1] == '?'){
  151. matrix[i+1] = add(&matrix[i],&matrix[i+1],-1);
  152. break;
  153. }
  154.  
  155. if(op[i] == '*' && op[i+1] == '*'){
  156. if (matrix[i].row == 1 && matrix[i].col == 1)
  157. scalar_product(matrix[i].data[0], &matrix[i+1]); //Multiplication of Scalar per matrix
  158. else{
  159. matrix[i+1] = multiply(&matrix[i],&matrix[i+1]);
  160. matrix[i+2] = multiply(&matrix[i+1],&matrix[i+2]);
  161. }
  162. }
  163.  
  164. if(op[i] == '*' && op[i+1] == '+'){
  165. if (matrix[i].row == 1 && matrix[i].col == 1)
  166. scalar_product(matrix[i].data[0],&matrix[i+1]); //Multiplication of Scalar per matrix
  167. else{
  168. matrix[i+1] = multiply(&matrix[i],&matrix[i+1]);
  169. matrix[i+2] = add(&matrix[i+1],&matrix[i+2],+1);
  170. }
  171. }
  172.  
  173. else if (op[i] == '+' && op[i+1] == '*'){
  174. matrix[i+1] = multiply(&matrix[i],&matrix[i+2]);
  175. matrix[i+2] = add(&matrix[i],&matrix[i+1],+1);
  176. }
  177.  
  178. else if (op[i] == '-' && op[i+1] == '*'){
  179. matrix[i+1] = multiply(&matrix[i],&matrix[i+2]);
  180. matrix[i+2] = add(&matrix[i],&matrix[i+1],-1);
  181. }
  182.  
  183. }
  184.  
  185. print_matrix(&matrix[id]); /*Print the result */
  186. free(matrix);
  187.  
  188. return 0;
  189. }
  190.  
  191. struct m multiply(struct m *A, struct m *B)
  192. {
  193. size_t i, j, k;
  194. struct m C;
  195. C.data = malloc(sizeof(double) * A->row * B->col);
  196.  
  197. C.row = A->row;
  198. C.col = B->col;
  199.  
  200. for (i=0; i< C.row; i++)
  201. for (j=0; j < C.col; j++)
  202. C.data[i * C.col + j] = 0;
  203.  
  204. // Multiplying matrix A and B and storing in C.
  205. for(i = 0; i < A->row; ++i)
  206. for(j = 0; j < B->col; ++j)
  207. for(k=0; k < A->col; ++k)
  208. C.data[i * C.col + j] += A->data[i * A->col + k] * B->data[k * B->col + j];
  209.  
  210. return C;
  211. }
  212.  
  213. struct m add(struct m *A, struct m *B, double n)
  214. {
  215. if ( (A->row != B->row) || (A->col != B->col) ){
  216. printf("Error: You can't sum up matrix of different dimension\n");
  217. exit(1);
  218. }
  219.  
  220. size_t i, j;
  221.  
  222. struct m C;
  223. C.data = malloc(sizeof(double) * A->row * B->col);
  224. C.row = A->row;
  225. C.col = A->col;
  226.  
  227. for (i=0; i< C.row; i++)
  228. for (j=0; j < C.col; j++)
  229. C.data[i * C.col + j] = A->data[i * A->col + j] + n *B->data[i * B->col + j];
  230.  
  231. return C;
  232. }
  233.  
  234. void f(double x)
  235. {
  236. double i,f= modf(x,&i);
  237.  
  238. if(fabs(f)<.00001)
  239. printf("%.f ",i);
  240.  
  241. else printf("%f ",x);
  242. }
  243.  
  244. /*printing a Matrix*/
  245.  
  246. void print_matrix(struct m *A){
  247.  
  248. size_t i,j;
  249.  
  250. double *tmp = A->data;
  251.  
  252. for(i=0; i < A->row; i++){
  253. for(j=0; j < A->col; j++){
  254. f(*(tmp++));
  255. }
  256. putchar('\n');
  257. }
  258. }
  259.  
  260. void scalar_product(double scalar, struct m *B)
  261. {
  262. size_t i,j;
  263.  
  264. for(i=0; i < B->row; i++)
  265. for(j=0; j < B->col; j++)
  266. B->data[i * B->col + j] = scalar * B->data[i * B->col + j];
  267. }
  268.  
  269. double determinant(size_t n, struct m *A)
  270. {
  271. size_t i,j,i_count,j_count, count=0;
  272.  
  273. double det = 0;
  274.  
  275. if(n < 1)
  276. {
  277. puts("Error");
  278. exit(1);
  279. }
  280.  
  281. if(n==1) return A->data[0];
  282.  
  283. if(n==2) return (A->data[0]* A->data[1 * A->col + 1] - A->data[0 + 1] * A->data[1*A->col + 0]);
  284.  
  285. else{
  286.  
  287. struct m C;
  288.  
  289. C.row = A->row-1;
  290. C.col = A->col-1;
  291.  
  292. C.data = malloc(sizeof(double) * (A->row-1) * (A->col-1));
  293.  
  294. for(count=0; count < n; count++)
  295. {
  296. //Creating array of Minors
  297. i_count = 0;
  298. for(i=1; i<n; i++)
  299. {
  300. j_count=0;
  301. for(j = 0; j < n; j++)
  302. {
  303. if(j == count)
  304. continue; // don't copy the minor column element
  305. C.data[i_count * C.col + j_count] = A->data[i * A->col + j];
  306. j_count++;
  307. }
  308. i_count++;
  309. }
  310. det += pow(-1, count) * A->data[0 + count] * determinant(n-1,&C);//Recursive call
  311. }
  312. free(C.data);
  313. return det;
  314. }
  315. }
  316.  
  317. void transpose(struct m *A)
  318. {
  319. struct m C;
  320. C.data = malloc(sizeof(double) * A->row * A->col);
  321.  
  322. C.row = A->col;
  323. C.col = A->row;
  324.  
  325. size_t i, j;
  326. for (i = 0; i < C.row; i++)
  327. for (j = 0; j < C.col; j++)
  328. C.data[i * C.col + j] = A->data[j * A->col + i];
  329. *A = C;
  330. }
  331.  
  332. void inverse(size_t n, struct m *A) /*Calculate the inverse of A */
  333. {
  334. double Rdata[(n-1)*(n-1)]; // remaining data values
  335. struct m R = { n-1, n-1, Rdata }; // matrix structure for them
  336. for (count = 0; count < n*n; count++) // Create n*n Matrix of Minors
  337. {
  338. int row = count/n, col = count%n;
  339. for (i_count = i = 0; i < n; i++)
  340. if (i != row) // don't copy the current row
  341. {
  342. for (j_count = j = 0; j < n; j++)
  343. if (j != col) // don't copy the current column
  344. Rdata[i_count*R.col+j_count++] = A->data[i*A->col+j];
  345. i_count++;
  346. }
  347. // transpose by swapping row and column
  348. C.data[col*C.col+row] = pow(-1, row&1 ^ col&1) * determinant(n-1, &R) / det;
  349. }
  350. }
Compilation error #stdin compilation error #stdout 0s 9432KB
stdin
1 2 3
0 -1 3
0 0 2
^-1
compilation info
prog.c: In function ‘inverse’:
prog.c:336:10: error: ‘count’ undeclared (first use in this function)
     for (count = 0; count < n*n; count++)   // Create n*n Matrix of Minors
          ^~~~~
prog.c:336:10: note: each undeclared identifier is reported only once for each function it appears in
prog.c:339:14: error: ‘i_count’ undeclared (first use in this function)
         for (i_count = i = 0; i < n; i++)
              ^~~~~~~
prog.c:339:24: error: ‘i’ undeclared (first use in this function)
         for (i_count = i = 0; i < n; i++)
                        ^
prog.c:342:22: error: ‘j_count’ undeclared (first use in this function)
                 for (j_count = j = 0; j < n; j++)
                      ^~~~~~~
prog.c:342:32: error: ‘j’ undeclared (first use in this function)
                 for (j_count = j = 0; j < n; j++)
                                ^
prog.c:348:9: error: ‘C’ undeclared (first use in this function)
         C.data[col*C.col+row] = pow(-1, row&1 ^ col&1) * determinant(n-1, &R) / det;
         ^
prog.c:348:44: warning: suggest parentheses around arithmetic in operand of ‘^’ [-Wparentheses]
         C.data[col*C.col+row] = pow(-1, row&1 ^ col&1) * determinant(n-1, &R) / det;
                                         ~~~^~
prog.c:348:81: error: ‘det’ undeclared (first use in this function)
         C.data[col*C.col+row] = pow(-1, row&1 ^ col&1) * determinant(n-1, &R) / det;
                                                                                 ^~~
stdout
Standard output is empty