diff --git a/TP6-matrix/.gitignore b/TP6-matrix/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..0fdbbc420417761bb95afa682a1454d8c53b2964 --- /dev/null +++ b/TP6-matrix/.gitignore @@ -0,0 +1,2 @@ +*.o +*.x \ No newline at end of file diff --git a/TP6-matrix/Makefile b/TP6-matrix/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..7bba2e11707c4fc7155f58b5a56f63117e457be9 --- /dev/null +++ b/TP6-matrix/Makefile @@ -0,0 +1,17 @@ + +matrix: matrix.o main.o matrix_compute.o + gcc matrix.o main.o -o matrix.x -lm -g -Wall -Wextra -pedantic -fsanitize=address -fsanitize=leak + +matrix_compute.o: matrix_compute.c + gcc -Wall -Wextra -c matrix_compute.c -g -Wall -Wextra -pedantic -fsanitize=address -fsanitize=leak + +matrix.o: matrix.c matrix.h + gcc -Wall -Wextra -c matrix.c -g -Wall -Wextra -pedantic -fsanitize=address -fsanitize=leak + +main.o: main.c + gcc -Wall -Wextra -c main.c -g -Wall -Wextra -pedantic -fsanitize=address -fsanitize=leak + +clean: + rm -f *.o matrix + +rebuild: clean matrix \ No newline at end of file diff --git a/TP6-matrix/main.c b/TP6-matrix/main.c new file mode 100644 index 0000000000000000000000000000000000000000..0cdf2b9712666d38e7099d0efa6a9205f84d7929 --- /dev/null +++ b/TP6-matrix/main.c @@ -0,0 +1,12 @@ +#include "matrix.h" +#include "matrix_compute.c" +#include <math.h> +#include <stdio.h> +#include <stdlib.h> + +int main() { + + compute(); + + return EXIT_SUCCESS; +} diff --git a/TP6-matrix/matrix.c b/TP6-matrix/matrix.c new file mode 100644 index 0000000000000000000000000000000000000000..38947e64d391d997c1cc357803e02980e1e4f635 --- /dev/null +++ b/TP6-matrix/matrix.c @@ -0,0 +1,210 @@ +#include "matrix.h" +#include <math.h> +#include <stdio.h> +#include <stdlib.h> + +// typedef struct _matrix { +// int m, n; +// int **data; +// } matrix; + +// m nb ligne n nb col + +error_code matrix_alloc(matrix *mat, int m, int n) { + + if (m < 0 || n < 0) { + return out_of_bounds; + } + + mat->data = malloc(m * sizeof(int *)); + if(mat->data == NULL){ + return memory_error; + } + + for (int i = 0; i < m; i++) { + mat->data[i] = malloc(n * sizeof(int)); + if(mat->data[i] == NULL){ + return memory_error; + } + } + + mat->n = n; + mat->m = m; + + return ok; +} + +error_code matrix_init(matrix *mat, int m, int n, int val) { + +if (m < 0 || n < 0) { + return out_of_bounds; + } + + matrix_alloc(mat, m, n); + + for (int i = 0; i < m; i++) { // ligne + for (int j = 0; j < n; j++) { // col + + mat->data[i][j] = val; + } + } + return ok; +} + +error_code matrix_init_from_array(matrix *mat, int m, int n, int data[]) { + +if (m < 0 || n < 0) { + return out_of_bounds; + } + + mat->data = malloc(m * sizeof(int *)); + + for (int i = 0; i < m; i++) { + mat->data[i] = malloc(n * sizeof(int)); + } + + int k = 0; + + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + + mat->data[i][j] = data[k++]; + } + } + + mat->n = n; + mat->m = m; + return ok; +} + +error_code matrix_destroy(matrix *mat) { + + for (int i = 0; i < mat->m; i++) { + free(mat->data[i]); + } + free(mat->data); + + mat->n = -1; + mat->m = -1; + + return ok; +} + +error_code matrix_clone(matrix *cloned, const matrix mat) { + + matrix_alloc(cloned, mat.m, mat.n); + + for (int i = 0; i < cloned->m; i++) { // ligne + for (int j = 0; j < cloned->n; j++) { // col + + cloned->data[i][j] = mat.data[i][j]; + } + } + return ok; +} + +error_code matrix_transpose(matrix *transposed, const matrix mat) { + + matrix_alloc(transposed, mat.n, mat.m); + + for (int i = 0; i < transposed->m; i++) { // ligne + for (int j = 0; j < transposed->n; j++) { // col + + transposed->data[i][j] = mat.data[j][i]; + } + } + return ok; +} + +error_code matrix_print(const matrix mat) { + + for (int i = 0; i < mat.m; i++) { + for (int j = 0; j < mat.n; j++) { + + printf("%d ", mat.data[i][j]); + } + printf("\n"); + } + printf("\n"); + return ok; +} + +error_code matrix_extract_submatrix(matrix *sub, const matrix mat, int m0,int m1, int n0, int n1) { + +if (m1-m0 < 0 || n1-n0 < 0) { + return out_of_bounds; + } + + matrix_alloc(sub, m1 - m0, n1 - n0); + + for (int i = 0; i < m1 - m0; i++) { // ligne + for (int j = 0; j < n1 - n0; j++) { // col + + sub->data[i][j] = mat.data[i][j]; + } + } + + return ok; +} + +bool matrix_is_equal(matrix mat1, matrix mat2) { + + if (mat1.m != mat2.m || mat1.n != mat2.n) { + return false; + } + + for (int i = 0; i < mat1.m; i++) { // ligne + for (int j = 0; j < mat1.n; j++) { // col + if (mat1.data[i][j] != mat2.data[i][j]) + return false; + } + } + return true; +} + +error_code matrix_get(int *elem, const matrix mat, int ix, int iy) { + + if (mat.m < ix || mat.n < iy) { + return out_of_bounds; + } + *elem = mat.data[ix][iy]; + return ok; +} + +error_code matrix_set(const matrix mat, int ix, int iy, int elem) { + + if (mat.m < ix || mat.n < iy) { + return out_of_bounds; + } + mat.data[ix][iy] = elem; + return ok; +} + +void mult2(int *elem){ + *elem*=2; +} + +error_code matrix_map_ip(matrix mat, void (*foo)(int *)) { + + for (int i = 0; i < mat.m; i++) { // ligne + for (int j = 0; j < mat.n; j++) { // col + + foo(&mat.data[i][j]); + } + } + return ok; +} + +error_code matrix_map(matrix *mapped, const matrix mat, void (*foo)(int *)) { + +matrix_clone(mapped, mat); + +for (int i = 0; i < mat.m; i++) { // ligne + for (int j = 0; j < mat.n; j++) { // col + + foo(&mapped->data[i][j]); + } + } + return ok; + +} \ No newline at end of file diff --git a/TP6-matrix/matrix.h b/TP6-matrix/matrix.h new file mode 100644 index 0000000000000000000000000000000000000000..ed953a02a552cb2a996322e2864e465e2e49ca57 --- /dev/null +++ b/TP6-matrix/matrix.h @@ -0,0 +1,48 @@ +#ifndef _MATRIX_H_ +#define _MATRIX_H_ + +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> + +typedef enum _error_code { + ok, + out_of_bounds, + memory_error, + uninitialized, +} error_code; + +typedef struct _matrix { + int m, n; + int **data; +} matrix; + +error_code matrix_alloc(matrix *mat, int m, int n); + +error_code matrix_init(matrix *mat, int m, int n, int val); + +error_code matrix_destroy(matrix *mat); + +error_code matrix_init_from_array(matrix *mat, int m, int n, int data[]); + +error_code matrix_clone(matrix *cloned, const matrix mat); + +error_code matrix_transpose(matrix *transposed, const matrix mat); + +error_code matrix_print(const matrix mat); + +error_code matrix_extract_submatrix(matrix *sub, const matrix mat, int m0, int m1, int n0, int n1); + +bool matrix_is_equal(matrix mat1, matrix mat2); + +error_code matrix_get(int *elem, const matrix mat, int ix, int iy); + +error_code matrix_set(matrix mat, int ix, int iy, int elem); + +void mult2(int *elem); + +error_code matrix_map_ip(matrix mat, void (*foo)(int *)); + +error_code matrix_map(matrix *mapped, const matrix mat, void (*foo)(int *)); + +#endif diff --git a/TP6-matrix/matrix_compute.c b/TP6-matrix/matrix_compute.c new file mode 100644 index 0000000000000000000000000000000000000000..efbb45cacbcd1e808ab23e7aeb57dc300b7928c9 --- /dev/null +++ b/TP6-matrix/matrix_compute.c @@ -0,0 +1,52 @@ +#include "matrix.h" + +void compute() { + + matrix mat1; + matrix mat2; + matrix mat3; + matrix mat4; + + int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + + printf("init\n"); + // matrix_init(&mat1, 8, 6, 1); + matrix_print(mat1); + + printf("init from array\n"); + matrix_init_from_array(&mat2, 3, 3, arr); + matrix_print(mat2); + + printf("clone\n"); + matrix_clone(&mat3, mat1); + matrix_print(mat3); + + printf("compare cloned matrix :"); + if (matrix_is_equal(mat1, mat3)) { + printf(" equal\n\n"); + } else { + printf("not equal\n\n"); + } + + printf("transpose\n"); + matrix_transpose(&mat4, mat3); + matrix_print(mat4); + + matrix_destroy(&mat1); + printf("submatrix\n"); + matrix_extract_submatrix(&mat1, mat4, 2, mat4.m, 2, mat4.n); + matrix_print(mat1); + + printf("set element [1][2] to 0\n"); + matrix_set(mat1, 1, 2, 0); + matrix_print(mat1); + + printf("double each item\n"); + matrix_map_ip(mat1, mult2); + matrix_print(mat1); + + matrix_destroy(&mat1); + matrix_destroy(&mat2); + matrix_destroy(&mat3); + matrix_destroy(&mat4); +} \ No newline at end of file diff --git a/matrix-tp6 b/matrix-tp6 deleted file mode 160000 index 833af4b7a554b83954d37e7c093a6b612035080b..0000000000000000000000000000000000000000 --- a/matrix-tp6 +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 833af4b7a554b83954d37e7c093a6b612035080b