Skip to content
Snippets Groups Projects
Commit d0b60da6 authored by Florian Burgener's avatar Florian Burgener
Browse files

Refactoring

parent 70851c3c
Branches
No related tags found
No related merge requests found
/** /**
* @file matrix.c * @file matrix.c
* @author Florian Burgener * @author Florian Burgener (florian.burgener@etu.hesge.ch)
* @brief Implémentation de librairie matrix. * @brief Implémentation de librairie matrix.
* @version 1.0 * @version 1.0
* @date 2021-11-16 * @date 2021-11-16
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
#include "matrix.h" #include "matrix.h"
#include <math.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
...@@ -22,15 +21,19 @@ error_code matrix_alloc(matrix *mat, int32_t m, int32_t n) { ...@@ -22,15 +21,19 @@ error_code matrix_alloc(matrix *mat, int32_t m, int32_t n) {
mat->n = n; mat->n = n;
int32_t **data = (int32_t **)malloc(sizeof(int32_t *) * mat->m); int32_t **data = (int32_t **)malloc(sizeof(int32_t *) * mat->m);
if (data == NULL) if (data == NULL) {
return err; return err;
}
mat->data = data; mat->data = data;
for (int32_t i = 0; i < m; i += 1) { for (int32_t i = 0; i < m; i += 1) {
int32_t *row = (int32_t *)malloc(sizeof(int32_t) * n); int32_t *row = (int32_t *)malloc(sizeof(int32_t) * n);
if (row == NULL) if (row == NULL) {
return err; return err;
}
mat->data[i] = row; mat->data[i] = row;
} }
...@@ -38,7 +41,9 @@ error_code matrix_alloc(matrix *mat, int32_t m, int32_t n) { ...@@ -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) { 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 y = 0; y < m; y += 1) {
for (int32_t x = 0; x < n; x += 1) { for (int32_t x = 0; x < n; x += 1) {
...@@ -75,9 +80,13 @@ error_code matrix_destroy(matrix *mat) { ...@@ -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) { 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; return err;
matrix_alloc(mat, m, n); }
for (int32_t y = 0; y < mat->m; y += 1) { for (int32_t y = 0; y < mat->m; y += 1) {
for (int32_t x = 0; x < mat->n; x += 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 ...@@ -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) { 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 y = 0; y < mat.m; y += 1) {
for (int32_t x = 0; x < mat.n; x += 1) { for (int32_t x = 0; x < mat.n; x += 1) {
...@@ -101,7 +112,9 @@ error_code matrix_clone(matrix *cloned, const matrix mat) { ...@@ -101,7 +112,9 @@ error_code matrix_clone(matrix *cloned, const matrix mat) {
} }
error_code matrix_transpose(matrix *transposed, 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 y = 0; y < mat.m; y += 1) {
for (int32_t x = 0; x < mat.n; x += 1) { for (int32_t x = 0; x < mat.n; x += 1) {
...@@ -113,9 +126,13 @@ error_code matrix_transpose(matrix *transposed, const matrix mat) { ...@@ -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) { 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; 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 y = m0; y < m1; y += 1) {
for (int32_t x = n0; x < n1; x += 1) { for (int32_t x = n0; x < n1; x += 1) {
......
/** /**
* @file matrix.h * @file matrix.h
* @author Florian Burgener * @author Florian Burgener (florian.burgener@etu.hesge.ch)
* @brief Header de librairie matrix. * @brief Header de librairie matrix.
* @version 1.0 * @version 1.0
* @date 2021-11-16 * @date 2021-11-16
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
#ifndef MATRIX_HEADER #ifndef MATRIX_HEADER
#define MATRIX_HEADER #define MATRIX_HEADER
#include <math.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
......
/** /**
* @file matrix_compute.c * @file matrix_compute.c
* @author Florian Burgener * @author Florian Burgener (florian.burgener@etu.hesge.ch)
* @brief Démonstration de libraire matrix. * @brief Démonstration de libraire matrix.
* @version 1.0 * @version 1.0
* @date 2021-11-16 * @date 2021-11-16
...@@ -9,11 +9,9 @@ ...@@ -9,11 +9,9 @@
* *
*/ */
#include <math.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h>
#include "matrix.h" #include "matrix.h"
...@@ -42,7 +40,7 @@ int main() { ...@@ -42,7 +40,7 @@ int main() {
printf("******\n"); printf("******\n");
matrix mat7; matrix mat7;
matrix_extract_submatrix(&mat7, mat4, 1, 3, 1, 2); matrix_extract_submatrix(&mat7, mat4, 1, 4, 1, 3);
matrix_print(mat7); matrix_print(mat7);
printf("*******\n"); printf("*******\n");
......
/** /**
* @file matrix_test.c * @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. * @brief Seul les tests ont été fait en commun pour gagner du temps.
* @version 1.0 * @version 1.0
* @date 2021-11-16 * @date 2021-11-16
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment