1 #include "matrix.h" 2 3 status_t m_print(matrix_t *matrix) 4 { 5 if(matrix == NULL) return ERROR_NULL_POINTER; 6 7 for(size_t i = 0; i < matrix->rows; i++) { 8 putchar('('); 9 putchar(' '); 10 for(size_t j = 0; j < matrix->columns; j++) { 11 printf("%10f ", matrix->array[i][j]); 12 } 13 putchar(')'); 14 putchar('\n'); 15 } 16 return OK; 17 } 18 19 status_t m_initrand(matrix_t *matrix) 20 { 21 if(matrix == NULL) return ERROR_NULL_POINTER; 22 23 srand((unsigned int)time(NULL)); 24 for(size_t i = 0; i < matrix->rows; i++) { 25 for(size_t j = 0; j < matrix->columns; j++) { 26 matrix->array[i][j] = ((double)rand()/(double)(RAND_MAX)) * 20; 27 } 28 } 29 return OK; 30 } 31 32 status_t m_load(matrix_t *matrix) 33 { 34 if(matrix == NULL) return ERROR_NULL_POINTER; 35 36 printf("%s", "Matrix of dimension N x M:\n\ 37 ( 00 01 .. 0N )\n\ 38 ( 10 01 .. 1N )\n\ 39 ( .. .. .. .. )\n\ 40 ( M0 M1 .. MN )\n\n"); 41 char buf[MAX_IN_LEN]; 42 int aux; 43 for(size_t i = 0; i < matrix->rows; i++) { 44 for(size_t j = 0; j < matrix->columns; j++) { 45 empty_string(buf, MAX_IN_LEN); 46 printf("Enter value %ld%ld: ", i, j); 47 for(size_t k = 0; ((aux = getchar()) != '\n') && k < MAX_IN_LEN; k++) 48 if(isdigit(aux) || (aux == '.') || (aux == '-')) 49 buf[k] = aux; 50 51 matrix->array[i][j] = strtod(buf, NULL); 52 } 53 putchar('\n'); 54 } 55 return OK; 56 } 57 58 status_t m_load_dim(matrix_t *matrix) 59 { 60 if(matrix == NULL) return ERROR_NULL_POINTER; 61 62 char *buffer; 63 buffer = (char *)malloc(DIM_BUFFER_MAX_SIZE * sizeof(char)); 64 if(buffer == NULL) return ERROR_ALLOCATING_MEMORY; 65 66 empty_string(buffer, DIM_BUFFER_MAX_SIZE); 67 68 printf("%s", "Enter no. of rows: "); 69 fgets(buffer, DIM_BUFFER_MAX_SIZE, stdin); 70 matrix->rows = strtol(buffer, NULL, 10); 71 72 empty_string(buffer, DIM_BUFFER_MAX_SIZE); 73 74 printf("%s", "Enter no. of columns: "); 75 fgets(buffer, DIM_BUFFER_MAX_SIZE, stdin); 76 matrix->columns = strtol(buffer, NULL, 10); 77 78 free(buffer); 79 return OK; 80 } 81 82 status_t m_transpose(matrix_t *matrix, matrix_t *matrix_transpose) 83 { 84 if((matrix == NULL) || (matrix_transpose == NULL)) 85 return ERROR_NULL_POINTER; 86 87 for(size_t i = 0; i < matrix->rows; i++) { 88 for(size_t j = 0; j < matrix->columns; j++) 89 matrix_transpose->array[j][i] = matrix->array[i][j]; 90 91 } 92 return OK; 93 } 94 95 status_t m_add(matrix_t *matrixA, matrix_t *matrixB, matrix_t *result) 96 { 97 if((matrixA == NULL) || (matrixB == NULL) || (result == NULL)) 98 return ERROR_NULL_POINTER; 99 /* This operation can be only performed if both matrix are squared and has the same dimension*/ 100 if((matrixA->rows != matrixB->rows) || (matrixA->columns != matrixB->columns)) 101 return ERROR_MATRIX_DIMENSION; 102 103 for(size_t i = 0; i < result->rows; i++) { 104 for(size_t j = 0; j < result->columns; j++) { 105 result->array[i][j] = matrixA->array[i][j] + matrixB->array[i][j]; 106 107 } 108 } 109 return OK; 110 } 111 112 status_t m_substract(matrix_t *matrixA, matrix_t *matrixB, matrix_t *result) 113 { 114 if((matrixA == NULL) || (matrixB == NULL) || (result == NULL)) 115 return ERROR_NULL_POINTER; 116 /* This operation can be only performed if both matrix are squared and has the same dimension*/ 117 else if((matrixA->rows != matrixB->rows) || (matrixA->columns != matrixB->columns)) 118 return ERROR_MATRIX_DIMENSION; 119 120 for(size_t i = 0; i < result->rows; i++) { 121 for(size_t j = 0; j < result->columns; j++) { 122 result->array[i][j] = matrixA->array[i][j] - matrixB->array[i][j]; 123 124 } 125 } 126 return OK; 127 } 128 129 status_t m_multiply(matrix_t *matrixA, matrix_t *matrixB, matrix_t *result) 130 { 131 if((matrixA == NULL) || (matrixB == NULL) || (result == NULL)) 132 return ERROR_NULL_POINTER; 133 else if(matrixA->columns != matrixB->rows) 134 return ERROR_MATRIX_DIMENSION; 135 136 double aux; 137 for(size_t i = 0; i < matrixA->rows; i++) { 138 for(size_t j = 0; j < matrixB->columns; j++) { 139 for(size_t k = aux = 0; k < matrixA->columns; k++) 140 aux += ((matrixA->array[i][k])*(matrixB->array[k][j])); 141 142 result->array[i][j] = aux; 143 } 144 } 145 return OK; 146 } 147 148 status_t m_divide(matrix_t *matrixA, matrix_t *matrixB, matrix_t *result) 149 { 150 if((matrixA == NULL) || (matrixB == NULL) || (result == NULL)) 151 return ERROR_NULL_POINTER; 152 else if(matrixA->columns != matrixB->rows) 153 return ERROR_MATRIX_DIMENSION; 154 155 double aux; 156 for(size_t i = 0; i < matrixA->rows; i++) { 157 for(size_t j = 0; j < matrixB->columns; j++) { 158 for(size_t k = aux = 0; k < matrixA->columns; k++) 159 aux += ((matrixA->array[i][k]) / (matrixB->array[k][j])); 160 161 result->array[i][j] = aux; 162 } 163 } 164 return OK; 165 } 166 167 status_t m_create(size_t rows, size_t columns, matrix_t *matrix) 168 { 169 if(matrix == NULL) return ERROR_NULL_POINTER; 170 171 matrix->rows = rows; 172 matrix->columns = columns; 173 174 matrix->array = (double **)malloc((rows) * sizeof(double)); 175 176 for (size_t i = 0; i < rows; i++) 177 matrix->array[i] = (double *)malloc(columns * sizeof(double)); 178 179 return OK; 180 } 181 182 status_t m_destroy(matrix_t *matrix) 183 { 184 if(matrix == NULL) return ERROR_NULL_POINTER; 185 186 for (size_t i = 0; i < matrix->rows; i++) 187 free(matrix->array[i]); 188 189 free(matrix->array); 190 return OK; 191 } 192 193 bool m_isSimetric(matrix_t *matrix) 194 { 195 matrix_t matrix_transpose; 196 m_create(matrix->columns, matrix->rows, &matrix_transpose); 197 m_transpose(matrix, &matrix_transpose); 198 199 size_t aux = 0; 200 for(size_t i = 0; i < matrix->rows; i++) 201 for(size_t j = 0; j < matrix->columns; j++) 202 if(matrix->array[i][j] == matrix_transpose.array[i][j]) 203 aux++; 204 205 //if(aux == (r * c)) 206 // return true; 207 208 //return false; 209 return true * (aux == (matrix->rows * matrix->columns)) + false * (aux != (matrix->rows * matrix->columns)); 210 } 211 212 status_t empty_string(char *string, size_t len) 213 { 214 if(string == NULL) return ERROR_NULL_POINTER; 215 216 for(size_t i = 0; i < len; i++) 217 string[i] = '\0'; 218 219 return OK; 220 }