Skip to content

Commit

Permalink
implement matrix_subtract with 1 example in README
Browse files Browse the repository at this point in the history
  • Loading branch information
KaisenAmin committed Feb 14, 2024
1 parent 0104dda commit 5a900cf
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 29 deletions.
30 changes: 1 addition & 29 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,36 +1,8 @@
#include "matrix/matrix.h"
#include "fmt/fmt.h"
#include <time.h>

void fillMatrix(Matrix *mat) {
matrix_set(mat, 0, 0, rand() % 10 + 1);
matrix_set(mat, 0, 1, rand() % 10 + 1);
matrix_set(mat, 1, 0, rand() % 10 + 1);
matrix_set(mat, 1, 1, rand() % 10 + 1);
}

int main() {
srand(time(NULL));
Matrix* matrix1 = matrix_create(2, 2);
Matrix* matrix2 = matrix_create(2, 2);

if (!matrix1 || !matrix2) {
fmt_fprintf(stderr, "Can not create matrix object");
exit(-1);
}

fillMatrix(matrix1);
fillMatrix(matrix2);

matrix_print(matrix1);
fmt_printf("\n");
matrix_print(matrix2);

Matrix* sum = matrix_add(matrix1, matrix2);

fmt_printf("\n");
matrix_print(sum);

matrix_deallocate(matrix1);

return 0;
}
84 changes: 84 additions & 0 deletions matrix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,88 @@ int main() {
matrix_deallocate(matrix);
return 0;
}
```

## Example 2 : how to add two Matrix with `matrix_add`

```c
#include "matrix/matrix.h"
#include "fmt/fmt.h"
#include <time.h>

void fillMatrix(Matrix *mat) {
matrix_set(mat, 0, 0, rand() % 10 + 1);
matrix_set(mat, 0, 1, rand() % 10 + 1);
matrix_set(mat, 1, 0, rand() % 10 + 1);
matrix_set(mat, 1, 1, rand() % 10 + 1);
}

int main() {
srand(time(NULL));
Matrix* matrix1 = matrix_create(2, 2);
Matrix* matrix2 = matrix_create(2, 2);

if (!matrix1 || !matrix2) {
fmt_fprintf(stderr, "Can not create matrix object");
exit(-1);
}

fillMatrix(matrix1);
fillMatrix(matrix2);

matrix_print(matrix1);
fmt_printf("\n");
matrix_print(matrix2);

Matrix* sum = matrix_add(matrix1, matrix2);

fmt_printf("\n");
matrix_print(sum);

matrix_deallocate(sum);
matrix_deallocate(matrix1);
matrix_deallocate(matrix2);

return 0;
}
```
## Example 3 : subtract two matrix with `matrix_subtract`
```c
#include "matrix/matrix.h"
#include "fmt/fmt.h"
#include <time.h>
void fillMatrix(Matrix *mat) {
matrix_set(mat, 0, 0, rand() % 10 + 1);
matrix_set(mat, 0, 1, rand() % 10 + 1);
matrix_set(mat, 1, 0, rand() % 10 + 1);
matrix_set(mat, 1, 1, rand() % 10 + 1);
}
int main() {
srand(time(NULL));
Matrix* matrix1 = matrix_create(2, 2);
Matrix* matrix2 = matrix_create(2, 2);
if (!matrix1 || !matrix2) {
fmt_fprintf(stderr, "Can not create matrix object");
exit(-1);
}
fillMatrix(matrix1);
fillMatrix(matrix2);
Matrix* subtraction = matrix_subtract(matrix1, matrix2);
fmt_printf("\n");
matrix_print(subtraction);
matrix_deallocate(subtraction);
matrix_deallocate(matrix1);
matrix_deallocate(matrix2);
return 0;
}
```
37 changes: 37 additions & 0 deletions matrix/matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,43 @@ Matrix* matrix_add(const Matrix* matrix1, const Matrix* matrix2) {

return addition;
}

Matrix* matrix_subtract(const Matrix* matrix1, const Matrix* matrix2) {
if (!matrix1) {
#ifdef MATRIX_LOGGING_ENABLE
fmt_fprintf(stderr, "Error: matrix1 object is null and invalid in matrix_subtract.\n");
#endif
return NULL;
}
else if (!matrix2) {
#ifdef MATRIX_LOGGING_ENABLE
fmt_fprintf(stderr, "Error: matrix2 object is null and invalid in matrix_subtract.\n");
#endif
return NULL;
}
else if ((matrix1->rows != matrix2->rows) || (matrix1->cols != matrix2->cols)) {
#ifdef MATRIX_LOGGING_ENABLE
fmt_fprintf(stderr, "Error: The two Matrix are not of the same order in matrix_subtract.\n");
#endif
return NULL;
}

Matrix* subtraction = matrix_create(matrix1->rows, matrix1->cols);
if (!subtraction) {
fmt_fprintf(stderr, "Error: object creation failed for subtraction to matrix in matrix_subtract.\n");
return NULL;
}

for (size_t i = 0; i < matrix1->rows; i++) {
for (size_t j = 0; j < matrix1->cols; j++) {
size_t index = i * matrix1->cols + j;
subtraction->data[index] = matrix1->data[index] - matrix2->data[index];
}
}

return subtraction;
}

void matrix_deallocate(Matrix* matrix) {
if (!matrix) {
#ifdef MATRIX_LOGGING_ENABLE
Expand Down
1 change: 1 addition & 0 deletions matrix/matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ typedef struct Matrix {

Matrix* matrix_create(size_t rows, size_t cols);
Matrix* matrix_add(const Matrix* matrix1, const Matrix* matrix2);
Matrix* matrix_subtract(const Matrix* matrix1, const Matrix* matrix2);

void matrix_deallocate(Matrix* matrix);
void matrix_print(Matrix* matrix);
Expand Down

0 comments on commit 5a900cf

Please sign in to comment.