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

Initial commit

parents
Branches
No related tags found
No related merge requests found
matrix_test
matrix_compute
*.o
Makefile 0 → 100644
TARGET = matrix_compute
LIBS = -lm
CC = gcc
CFLAGS = -g -Wall -Wextra -pedantic -fsanitize=address -fsanitize=leak
.PHONY: default clean
default: $(TARGET)
%.o: %.c $(HEADERS)
$(CC) $(CFLAGS) -c $< -o $@
$(TARGET): matrix_compute.o matrix.o
$(CC) matrix_compute.o matrix.o $(CFLAGS) $(LIBS) -o $@
matrix_test: matrix_test.o matrix.o
$(CC) matrix_test.o matrix.o $(CFLAGS) $(LIBS) -o $@
clean:
-rm -f *.o
-rm -f $(TARGET)
-rm -f matrix_test
#!/bin/bash
cp ~/HEPIA/ISC_142/tp007/* .
matrix.c 0 → 100644
/**
* @file matrix.c
* @author Florian Burgener
* @brief Implémentation de librairie matrix.
* @version 1.0
* @date 2021-11-16
*
* @copyright Copyright (c) 2021
*
*/
#include "matrix.h"
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
error_code matrix_alloc(matrix *mat, int32_t m, int32_t n) {
mat->m = m;
mat->n = n;
int32_t **data = (int32_t **)malloc(sizeof(int32_t *) * mat->m);
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)
return err;
mat->data[i] = row;
}
return ok;
}
error_code matrix_init(matrix *mat, int32_t m, int32_t n, int32_t val) {
matrix_alloc(mat, m, n);
for (int32_t y = 0; y < m; y += 1) {
for (int32_t x = 0; x < n; x += 1) {
mat->data[y][x] = val;
}
}
return ok;
}
error_code matrix_print(const matrix mat) {
for (int32_t y = 0; y < mat.m; y += 1) {
for (int32_t x = 0; x < mat.n; x += 1) {
printf("%d, ", mat.data[y][x]);
}
printf("\n");
}
return ok;
}
error_code matrix_destroy(matrix *mat) {
for (int32_t y = 0; y < mat->m; y += 1) {
free(mat->data[y]);
}
free(mat->data);
mat->m = -1;
mat->n = -1;
mat->data = NULL;
return ok;
}
error_code matrix_init_from_array(matrix *mat, int32_t m, int32_t n, int32_t data[], int32_t s) {
if (m * n != s)
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) {
mat->data[y][x] = data[y * mat->n + x];
}
}
return ok;
}
error_code matrix_clone(matrix *cloned, const matrix mat) {
matrix_alloc(cloned, mat.m, mat.n);
for (int32_t y = 0; y < mat.m; y += 1) {
for (int32_t x = 0; x < mat.n; x += 1) {
cloned->data[y][x] = mat.data[y][x];
}
}
return ok;
}
error_code matrix_transpose(matrix *transposed, const matrix mat) {
matrix_alloc(transposed, mat.n, mat.m);
for (int32_t y = 0; y < mat.m; y += 1) {
for (int32_t x = 0; x < mat.n; x += 1) {
transposed->data[x][y] = mat.data[y][x];
}
}
return ok;
}
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)
return err;
matrix_alloc(sub, m1 - m0, n1 - n0);
for (int32_t y = m0; y < m1; y += 1) {
for (int32_t x = n0; x < n1; x += 1) {
sub->data[y - m0][x - n0] = mat.data[y][x];
}
}
return ok;
}
bool matrix_is_equal(matrix mat1, matrix mat2) {
if (mat1.m != mat2.m || mat1.n != mat2.n) {
return false;
}
for (int32_t y = 0; y < mat1.m; y += 1) {
for (int32_t x = 0; x < mat2.n; x += 1) {
if (mat1.data[y][x] != mat2.data[y][x]) {
return false;
}
}
}
return true;
}
error_code matrix_get(int32_t *elem, const matrix mat, int32_t ix, int32_t iy) {
if (ix < 0 || ix >= mat.n || iy < 0 || iy >= mat.m) {
return err;
}
*elem = mat.data[iy][ix];
return ok;
}
error_code matrix_set(matrix mat, int32_t ix, int32_t iy, int32_t elem) {
if (ix < 0 || ix >= mat.n || iy < 0 || iy >= mat.m) {
return err;
}
mat.data[iy][ix] = elem;
return ok;
}
matrix.h 0 → 100644
/**
* @file matrix.h
* @author Florian Burgener
* @brief Header de librairie matrix.
* @version 1.0
* @date 2021-11-16
*
* @copyright Copyright (c) 2021
*
*/
#ifndef MATRIX_HEADER
#define MATRIX_HEADER
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
typedef enum _error_code {
ok,
err
} error_code;
typedef struct _matrix {
int32_t m, n;
int32_t **data;
} matrix;
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_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_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, int32_t m0, int32_t m1, int32_t n0, int32_t n1);
bool matrix_is_equal(matrix mat1, matrix mat2);
error_code matrix_get(int32_t *elem, const matrix mat, int32_t ix, int32_t iy);
error_code matrix_set(matrix mat, int32_t ix, int32_t iy, int32_t elem);
#endif
/**
* @file matrix_compute.c
* @author Florian Burgener
* @brief Démonstration de libraire matrix.
* @version 1.0
* @date 2021-11-16
*
* @copyright Copyright (c) 2021
*
*/
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "matrix.h"
int main() {
matrix mat;
matrix_init(&mat, 3, 4, 100);
printf("%d\n", mat.m);
printf("%d\n", mat.n);
matrix_print(mat);
printf("***\n");
matrix mat2;
int32_t values[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
matrix_init_from_array(&mat2, 3, 4, values, 12);
matrix_print(mat2);
printf("****\n");
matrix mat3;
matrix_clone(&mat3, mat2);
matrix_print(mat3);
printf("*****\n");
matrix mat4;
matrix_transpose(&mat4, mat2);
matrix_print(mat4);
printf("******\n");
matrix mat7;
matrix_extract_submatrix(&mat7, mat4, 1, 3, 1, 2);
matrix_print(mat7);
printf("*******\n");
int32_t value;
matrix_get(&value, mat4, 1, 2);
printf("%d\n", value);
printf("********\n");
matrix_set(mat4, 1, 2, 1000);
matrix_print(mat4);
printf("*********\n");
matrix mat5;
matrix_init(&mat5, 3, 4, 10);
matrix mat6;
matrix_init(&mat6, 3, 4, 10);
bool is_equal = matrix_is_equal(mat5, mat6);
printf("%d\n", is_equal);
matrix_destroy(&mat);
matrix_destroy(&mat2);
matrix_destroy(&mat3);
matrix_destroy(&mat4);
matrix_destroy(&mat5);
matrix_destroy(&mat6);
matrix_destroy(&mat7);
return EXIT_SUCCESS;
}
/**
* @file matrix_test.c
* @author Florian Burgener et Quentin Fasler
* @brief Seul les tests ont été fait en commun pour gagner du temps.
* @version 1.0
* @date 2021-11-16
*
* @copyright Copyright (c) 2021
*
*/
#include "matrix.h"
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
// Init
matrix m;
int values[6] = {2, 1, -1, -2, 3, 1};
int values2[6] = {2, 1, -1, -2, 3, 0};
int values3[9] = {2, 1, -1, -2, 3, 0, 6, 8, 10};
// Test : matrix_init
matrix m0;
assert(matrix_init(&m0, 2, 3, 1000) == ok);
assert(m0.m == 2);
assert(m0.n == 3);
assert(m0.data[0][0] == 1000);
assert(m0.data[0][1] == 1000);
assert(m0.data[0][2] == 1000);
assert(m0.data[1][0] == 1000);
assert(m0.data[1][1] == 1000);
assert(m0.data[1][2] == 1000);
// Test : matrix_init_from_array
matrix m1;
assert(matrix_init_from_array(&m1, 2, 3, values, 6) == ok);
assert(m1.m == 2);
assert(m1.n == 3);
assert(m1.data[0][0] == 2);
assert(m1.data[0][1] == 1);
assert(m1.data[0][2] == -1);
assert(m1.data[1][0] == -2);
assert(m1.data[1][1] == 3);
assert(m1.data[1][2] == 1);
// Init matrix m, m will never be changed.
matrix_init_from_array(&m, 2, 3, values, 6);
// Test : matrix_print
assert(ok == matrix_print(m1));
// Test : matrix_destroy
assert(matrix_destroy(&m1) == ok);
assert(m1.m == -1);
assert(m1.n == -1);
assert(m1.data == NULL);
// Test : matrix_clone
matrix m2;
assert(matrix_clone(&m2, m) == ok);
assert(m2.m == 2);
assert(m2.n == 3);
assert(m2.data[0][0] == 2);
assert(m2.data[0][1] == 1);
assert(m2.data[0][2] == -1);
assert(m2.data[1][0] == -2);
assert(m2.data[1][1] == 3);
assert(m2.data[1][2] == 1);
// Test : matrix_transpose
matrix m3;
assert(matrix_transpose(&m3, m) == ok);
assert(m3.m == 3);
assert(m3.n == 2);
assert(m3.data[0][0] == 2);
assert(m3.data[1][0] == 1);
assert(m3.data[2][0] == -1);
assert(m3.data[0][1] == -2);
assert(m3.data[1][1] == 3);
assert(m3.data[2][1] == 1);
// Test : matrix_extract_submatrix
matrix m4;
assert(matrix_extract_submatrix(&m4, m, 0, 2, 0, 2) == ok);
assert(m4.data[0][0] == 2);
assert(m4.data[0][1] == 1);
assert(m4.data[1][0] == -2);
assert(m4.data[1][1] == 3);
// Test : matrix_is_equal
matrix m5_A;
matrix_init_from_array(&m5_A, 2, 3, values, 6);
matrix m5_B;
matrix_init_from_array(&m5_B, 2, 3, values, 6);
matrix m5_C;
matrix_init_from_array(&m5_C, 2, 3, values2, 6);
matrix m5_D;
matrix_init_from_array(&m5_D, 3, 3, values3, 9);
assert(matrix_is_equal(m5_A, m5_B) == true);
assert(matrix_is_equal(m5_A, m5_C) == false);
assert(matrix_is_equal(m5_A, m5_D) == false);
// Test : matrix_get
int32_t elem;
matrix_get(&elem, m, 0, 1);
assert(elem == -2);
// Test : matrix_set
matrix_set(m, 0, 1, 1000);
assert(m.data[1][0] == 1000);
matrix_destroy(&m0);
// m1 is already destroyed.
matrix_destroy(&m2);
matrix_destroy(&m3);
matrix_destroy(&m4);
matrix_destroy(&m5_A);
matrix_destroy(&m5_B);
matrix_destroy(&m5_C);
matrix_destroy(&m5_D);
matrix_destroy(&m);
return EXIT_SUCCESS;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment