diff --git a/matrix.c b/matrix.c index 306733fd31f39a55657543b9fbc77973e889f242..d5846855f2e883c2c493e787c172408fc9965d4c 100644 --- a/matrix.c +++ b/matrix.c @@ -1,6 +1,6 @@ /** * @file matrix.c - * @author Florian Burgener + * @author Florian Burgener (florian.burgener@etu.hesge.ch) * @brief Implémentation de librairie matrix. * @version 1.0 * @date 2021-11-16 @@ -11,7 +11,6 @@ #include "matrix.h" -#include <math.h> #include <stdbool.h> #include <stdint.h> #include <stdio.h> @@ -22,15 +21,19 @@ error_code matrix_alloc(matrix *mat, int32_t m, int32_t n) { mat->n = n; int32_t **data = (int32_t **)malloc(sizeof(int32_t *) * mat->m); - if (data == NULL) + if (data == NULL) { return err; + } + mat->data = data; for (int32_t i = 0; i < m; i += 1) { int32_t *row = (int32_t *)malloc(sizeof(int32_t) * n); - if (row == NULL) + if (row == NULL) { return err; + } + mat->data[i] = row; } @@ -38,7 +41,9 @@ error_code matrix_alloc(matrix *mat, int32_t m, int32_t n) { } error_code matrix_init(matrix *mat, int32_t m, int32_t n, int32_t val) { - matrix_alloc(mat, m, n); + if (matrix_alloc(mat, m, n) == err) { + return err; + } for (int32_t y = 0; y < m; y += 1) { for (int32_t x = 0; x < n; x += 1) { @@ -75,9 +80,13 @@ error_code matrix_destroy(matrix *mat) { } error_code matrix_init_from_array(matrix *mat, int32_t m, int32_t n, int32_t data[], int32_t s) { - if (m * n != s) + if (m * n != s) { + return err; + } + + if (matrix_alloc(mat, m, n) == err) { return err; - matrix_alloc(mat, m, n); + } for (int32_t y = 0; y < mat->m; y += 1) { for (int32_t x = 0; x < mat->n; x += 1) { @@ -89,7 +98,9 @@ error_code matrix_init_from_array(matrix *mat, int32_t m, int32_t n, int32_t dat } error_code matrix_clone(matrix *cloned, const matrix mat) { - matrix_alloc(cloned, mat.m, mat.n); + if (matrix_alloc(cloned, mat.m, mat.n) == err) { + return err; + } for (int32_t y = 0; y < mat.m; y += 1) { for (int32_t x = 0; x < mat.n; x += 1) { @@ -101,7 +112,9 @@ error_code matrix_clone(matrix *cloned, const matrix mat) { } error_code matrix_transpose(matrix *transposed, const matrix mat) { - matrix_alloc(transposed, mat.n, mat.m); + if (matrix_alloc(transposed, mat.n, mat.m) == err) { + return err; + } for (int32_t y = 0; y < mat.m; y += 1) { for (int32_t x = 0; x < mat.n; x += 1) { @@ -113,9 +126,13 @@ error_code matrix_transpose(matrix *transposed, const matrix mat) { } error_code matrix_extract_submatrix(matrix *sub, const matrix mat, int32_t m0, int32_t m1, int32_t n0, int32_t n1) { - if (m1 < m0 || n1 < n0 || m0 < 0 || n0 < 0 || m1 > mat.m || n1 > mat.n) + if (m1 <= m0 || n1 <= n0 || m0 < 0 || n0 < 0 || m1 > mat.m || n1 > mat.n) { return err; - matrix_alloc(sub, m1 - m0, n1 - n0); + } + + if (matrix_alloc(sub, m1 - m0, n1 - n0) == err) { + return err; + } for (int32_t y = m0; y < m1; y += 1) { for (int32_t x = n0; x < n1; x += 1) { diff --git a/matrix.h b/matrix.h index d202555d4b75d74a3d6f2448c60f783eabfb33ea..7de4b7241eb78cb4e475fe6a52a5b88211147585 100644 --- a/matrix.h +++ b/matrix.h @@ -1,6 +1,6 @@ /** * @file matrix.h - * @author Florian Burgener + * @author Florian Burgener (florian.burgener@etu.hesge.ch) * @brief Header de librairie matrix. * @version 1.0 * @date 2021-11-16 @@ -11,7 +11,7 @@ #ifndef MATRIX_HEADER #define MATRIX_HEADER -#include <math.h> + #include <stdbool.h> #include <stdint.h> diff --git a/matrix_compute.c b/matrix_compute.c index a18c1848588bfef294a21d22c3c323e04606abb8..d01f29a52a1555122e9666ae0ea21d00e39aeec2 100644 --- a/matrix_compute.c +++ b/matrix_compute.c @@ -1,6 +1,6 @@ /** * @file matrix_compute.c - * @author Florian Burgener + * @author Florian Burgener (florian.burgener@etu.hesge.ch) * @brief Démonstration de libraire matrix. * @version 1.0 * @date 2021-11-16 @@ -9,11 +9,9 @@ * */ -#include <math.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> -#include <time.h> #include "matrix.h" @@ -42,7 +40,7 @@ int main() { printf("******\n"); matrix mat7; - matrix_extract_submatrix(&mat7, mat4, 1, 3, 1, 2); + matrix_extract_submatrix(&mat7, mat4, 1, 4, 1, 3); matrix_print(mat7); printf("*******\n"); diff --git a/matrix_test.c b/matrix_test.c index 4350a5ca01b6819774a5ffaaf93dc7aa300d71d4..c82db06058d897c003e64b6a538d0322a75dfb24 100644 --- a/matrix_test.c +++ b/matrix_test.c @@ -1,6 +1,6 @@ /** * @file matrix_test.c - * @author Florian Burgener et Quentin Fasler + * @author Florian Burgener (florian.burgener@etu.hesge.ch) et Quentin Fasler * @brief Seul les tests ont été fait en commun pour gagner du temps. * @version 1.0 * @date 2021-11-16