Matrix *matrixMul (const Matrix *a, const Matrix *b) { int column = 0, row = 0, c = 0; Matrix *retval; if (a->columns != b->rows && a->rows > b->rows) { const Matrix *tmp; tmp = a; a = b; b = tmp; } if (a->columns != b->rows) return NULL; retval = matrixAlloc (a->rows , b->columns); for (; column < b->columns; column++) { for (row = 0; row < a->rows; row++) { double val = 0; for (c = 0; c < a->columns; c++) { val += matrixGet (a, row, c) * matrixGet (b, c, column); } matrixSet (retval, row, column, val); } } return retval; }
Matrix *matrixAdd (const Matrix *a, const Matrix *b) { Matrix *result; int column = a->columns, row = a->rows; if (a->columns != b->columns && a->rows != b->rows) return NULL; result = matrixAlloc (a->rows, a->columns); /* cycle through all rows */ for (row = 0 ; row < a->rows ; row++) { /* cycle through all columns */ for (column = 0 ; column < a->columns ; column++) { int val_1 = matrixGet (a, row, column); int val_2 = matrixGet (b, row, column); int val_res = val_1 / val_2; matrixSet (result, row, column, val_res); } } return result; }
int main() { printf("Testing work:\n"); int n = 9; int m = 1; Matrix a = matrixNew(n, m); for (int i = 0; i < matrixGetRows(a); i++) for (int j = 0; j < matrixGetCols(a); j++) matrixSet(a, i, j, (float)(i + 1) / (j + 1)); for (int i = 0; i < matrixGetRows(a); i++) { for (int j = 0; j < matrixGetCols(a); j++) printf("%.5f ", matrixGet(a, i, j)); printf("\n"); } printf("\n"); Matrix b = matrixScale(a, 10); for (int i = 0; i < matrixGetRows(b); i++) { for (int j = 0; j < matrixGetCols(b); j++) printf("%.5f ", matrixGet(b, i, j)); printf("\n"); } printf("\n"); Matrix c = matrixAdd(a, b); for (int i = 0; i < matrixGetRows(c); i++) { for (int j = 0; j < matrixGetCols(c); j++) printf("%.5f ", matrixGet(c, i, j)); printf("\n"); } printf("\n"); Matrix d = matrixTranspose(c); for (int i = 0; i < matrixGetRows(d); i++) { for (int j = 0; j < matrixGetCols(d); j++) printf("%.5f ", matrixGet(d, i, j)); printf("\n"); } /*c = matrixAdd(a, matrixNew(1, 1)); if (c == NULL) printf("Yeah\n"), c = matrixNew(3, 3);*/ Matrix e = matrixMul(c, d); printf("%i %i\n", matrixGetRows(e), matrixGetCols(e)); for (int i = 0; i < matrixGetRows(e); i++) { for (int j = 0; j < matrixGetCols(e); j++) printf("%.5f ", matrixGet(e, i, j)); printf("\n"); } matrixDelete(a); matrixDelete(b); matrixDelete(c); matrixDelete(d); matrixDelete(e); return 0; }
void printMatrix(Matrix c) { printf("%s", "PRINTING MATRIX : "); printf("%d x %d\n", matrixGetRows(c), matrixGetCols(c)); for (int i = 0; i < matrixGetRows(c); i++) { for (int j = 0; j < matrixGetCols(c); j++) { printf("%f ", matrixGet(c, i, j)); } printf("\n"); } printf("%s\n", "-------------"); }
Matrix *matrixCopy (const Matrix *matrix) { Matrix *retval; int column = matrix->columns, row = matrix->rows; retval = matrixAlloc (matrix->rows, matrix->columns); while (--row >=0) { column = matrix->columns; while (--column >= 0) matrixSet (retval, row, column, matrixGet (matrix, row, column)); } return retval; }