Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. *(result.matrix+ i*r + j)
  2.  
  3. result[i][j]
  4.  
  5. /**
  6. Matrix Multiplication
  7. matrices.c
  8. Matrix data structure in C.
  9.  
  10. @author Michael Asper
  11. @version 1.0 3/29/17
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <time.h>
  17.  
  18. typedef struct Matrix {
  19. int rowSize;
  20. int columnSize;
  21. long int* matrix;
  22. } Matrix;
  23.  
  24.  
  25. /**
  26. Randomizes the elements of a matrix
  27.  
  28. @param *m pointer to Matrix to randomize;
  29. */
  30. void randomize(Matrix *m){
  31. int i,j;
  32. for(i = 0; i < m->rowSize ; i++){
  33. for(j = 0; j < m->columnSize; j++){
  34. *(m->matrix + i*m->rowSize + j)= rand() % 5000;
  35. }
  36. }
  37. }
  38.  
  39. /**
  40. Returns a r x c Matrix with all 0s.
  41.  
  42. @param r The row size of the matrix
  43. @param c The column size of the matrix
  44. @return r x c Matrix
  45. */
  46. Matrix createMatrix(int r, int c){
  47. Matrix temp = {r, c, calloc(r * c, sizeof(long int *))};
  48. return temp;
  49. }
  50.  
  51. /**
  52. Returns a r x c Matrix with random numbers.
  53.  
  54. @param r The row size of the matrix
  55. @param c The column size of the matrix
  56. @return r x c Matrix
  57. */
  58. Matrix createRandMatrix(int r, int c){
  59. Matrix temp = createMatrix(r,c);
  60. randomize(&temp);
  61. return temp;
  62. }
  63.  
  64. /**
  65. Prints matrix.
  66.  
  67. @param *m Pointer to Matrix you want to print
  68. */
  69. void printMatrix(Matrix *m){
  70.  
  71. int i,j;
  72. for(i = 0; i < m->rowSize ; i++){
  73. for(j = 0; j < m->columnSize; j++){
  74. printf("%li ", *(m->matrix + i*m->rowSize + j));
  75. }
  76. printf("n");
  77. }
  78. }
  79.  
  80. /**
  81. Adds two matrices together
  82.  
  83. @param *a pointer to first matrix (A);
  84. @param *b pointer to second matrix (B);
  85. @return A+B
  86. */
  87. Matrix add(Matrix *a, Matrix *b){
  88. //check if matrices are compatible
  89. if(a->rowSize != b->rowSize || a->columnSize != b->columnSize){
  90. fprintf(stderr, "Error: Incompatible sizes");
  91. exit(0);
  92. }
  93. //create result matrix
  94. int r = a->rowSize;
  95. int c = a->columnSize;
  96. Matrix result = createMatrix(r,c);
  97. //add matrices
  98. int i,j;
  99. for(i = 0; i < r ; i++){
  100. for(j = 0; j < c; j++){
  101. //result[i][j] = a[i][j]+b[i][j]
  102. *(result.matrix+ i*r + j) = *(a->matrix + i*r + j) + *(b->matrix + i*r + j);
  103. }
  104. }
  105. return result;
  106. }
  107.  
  108. /**
  109. Subtracts two matrices together
  110.  
  111. @param *a pointer to first matrix (A);
  112. @param *b pointer to second matrix (B);
  113. @return A-B
  114. */
  115. Matrix sub(Matrix *a, Matrix *b){
  116. //check if matrices are compatible
  117. if(a->rowSize != b->rowSize || a->columnSize != b->columnSize){
  118. fprintf(stderr, "Error: Incompatible sizes");
  119. exit(0);
  120. }
  121. //create result matrix
  122. int r = a->rowSize;
  123. int c = a->columnSize;
  124. Matrix result = createMatrix(r,c);
  125. //subtracts matrix
  126. int i,j;
  127. for(i = 0; i < r ; i++){
  128. for(j = 0; j < c; j++){
  129. //result[i][j] = a[i][j]-b[i][j]
  130. *(result.matrix+ i*r + j) = *(a->matrix + i*r + j) - *(b->matrix + i*r + j);
  131. }
  132. }
  133. return result;
  134. }
  135.  
  136. /**
  137. Multiplies two matrices together
  138.  
  139. @param *a pointer to first matrix (A);
  140. @param *b pointer to second matrix (B);
  141. @return A*B
  142. */
  143. Matrix multiply(Matrix *a, Matrix *b){
  144. //check if matrices are compatible
  145. if(a->columnSize != b->rowSize ){
  146. fprintf(stderr, "Error: Incompatible sizes");
  147. exit(0);
  148. }
  149.  
  150. //initialize return matrix
  151. int r = a->rowSize;
  152. int c = b->columnSize;
  153. Matrix result = createMatrix(r,c);
  154.  
  155. //multiply matrices
  156. int i,j;
  157. for(i = 0; i < r ; i++){
  158. for(j = 0; j < c; j++){
  159. long int sum = 0;
  160. int k;
  161. for(k = 0; k < a->columnSize; k++){
  162. //sum += a[i][k] * b[k][j]
  163. sum = sum + (*(a->matrix + i*a->rowSize + k)**(b->matrix + k*b->rowSize + j));
  164. }
  165. *(result.matrix+ i*r + j) = sum;
  166. }
  167. }
  168. return result;
  169. }
  170.  
  171.  
  172. int main(){
  173. // seed random with time
  174. time_t t;
  175. srand((unsigned) time(&t));
  176.  
  177. //setup random matrices and multiply
  178. Matrix a = createRandMatrix(3,100);
  179. Matrix b = createRandMatrix(100,3);
  180. Matrix result = multiply(&a,&b);
  181. printMatrix(&result);
  182.  
  183. return 0;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement