mcalc

simple interactive program to perform matrix operations. (WIP)
Index Commits Files Refs README LICENSE
commit 67771b5ca46764f139aa3cd237d1d7990033dd7c
parent 5c10c69c313794f9b120aad50e8d53df12433105
Author: klewer-martin <martin.cachari@gmail.com>
Date:   Sun, 18 Apr 2021 16:15:46 -0300

Update: reorganized directories and source code improvements

Diffstat:
AMakefile | 14++++++++++++++
Amain.c | 45+++++++++++++++++++++++++++++++++++++++++++++
Amain.h | 9+++++++++
Amatrix_struct.c | 130+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Amatrix_struct.h | 30++++++++++++++++++++++++++++++
5 files changed, 228 insertions(+), 0 deletions(-)
diff --git a/Makefile b/Makefile
@@ -0,0 +1,14 @@
+CC = gcc
+FLAGS = -Wall -pedantic
+
+all: clean main.o matrix_struct.o
+    $(CC) $(FLAGS) main.o matrix_struct.o
+
+main.o:
+    $(CC) $(FLAGS) -c main.c
+
+matrix_struct.o:
+    $(CC) $(FLAGS) -c matrix_struct.c
+
+clean:
+    rm -rf *.o
diff --git a/main.c b/main.c
@@ -0,0 +1,45 @@
+#include "main.h"
+#include "matrix_struct.h"
+
+#define R 3
+
+int main (void)
+{
+    matrix_t matrix;
+    matrix_t matrix2;
+    matrix_t matrix_r;
+
+    m_create(R, R, &matrix);
+    m_create(R, R, &matrix2);
+    m_create(R, R, &matrix_r);
+
+    /*
+    m_load(N, M, matrix);
+    m_print(N, M, matrix);
+
+    putchar('\n');
+    
+    m_transpose(&matrix, &matrix_transpose);
+    m_print(&matrix_transpose);
+
+    */
+
+    m_initrand(&matrix);
+    m_print(&matrix);
+    putchar('\n');
+
+    m_initrand(&matrix2);
+    m_print(&matrix2);
+    putchar('\n');
+
+    m_multiply(&matrix, &matrix2, &matrix_r);
+    m_print(&matrix_r);
+    putchar('\n');
+
+    m_destroy(&matrix);
+    m_destroy(&matrix2);
+    m_destroy(&matrix_r);
+
+
+    return 0;
+}
diff --git a/main.h b/main.h
@@ -0,0 +1,9 @@
+#ifndef _MAIN_H
+#define _MAIN_H
+
+#define N 10
+#define M 10
+
+#include <stdio.h>
+
+#endif
diff --git a/matrix_struct.c b/matrix_struct.c
@@ -0,0 +1,130 @@
+#include "matrix_struct.h"
+
+void m_print(matrix_t *matrix)
+{
+    for(size_t i = 0; i < matrix->rows; i++) {
+        putchar('(');
+        putchar(' ');
+        for(size_t j = 0; j < matrix->columns; j++) {
+            printf("%7.2f ", matrix->array[i][j]);
+        }
+        putchar(')');
+        putchar('\n');
+    }
+}
+
+void m_initrand(matrix_t *matrix)
+{
+    srand((unsigned int)time(NULL));
+    
+    for(size_t i = 0; i < matrix->rows; i++) {
+        for(size_t j = 0; j < matrix->columns; j++) {
+            matrix->array[i][j] = ((double)rand()/(double)(RAND_MAX)) * 20;
+        }
+    }
+}
+
+void m_load(matrix_t *matrix)
+{
+    char buf[20];
+    for(size_t i = 0; i < matrix->rows; i++) {
+        for(size_t j = 0; j < matrix->columns; j++) {
+            fgets(buf, 20, stdin);
+            matrix->array[i][j] = strtod(buf, NULL);
+        }
+    }
+}
+
+void m_transpose(matrix_t *matrix, matrix_t *matrix_transpose)
+{
+    for(size_t i = 0; i < matrix->rows; i++) {
+        for(size_t j = 0; j < matrix->columns; j++)
+            matrix_transpose->array[j][i] = matrix->array[i][j];
+
+    }
+}
+
+void m_add(matrix_t *matrixA, matrix_t *matrixB, matrix_t *result)
+{
+    if(matrixA->columns != matrixB->rows)
+        return;
+
+}
+
+void m_substract(matrix_t *matrixA, matrix_t *matrixB, matrix_t *result)
+{
+    if(matrixA->columns != matrixB->rows)
+        return;
+
+}
+
+void m_multiply(matrix_t *matrixA, matrix_t *matrixB, matrix_t *result)
+{
+    if(matrixA->columns != matrixB->rows)
+        return;
+
+    double aux;
+    for(size_t i = 0; i < matrixA->rows; i++) {
+        for(size_t j = 0; j < matrixB->columns; j++) {
+            for(size_t k = aux = 0; k < matrixA->columns; k++)
+                aux += ((matrixA->array[i][k])*(matrixB->array[k][j]));
+            
+            result->array[i][j] = aux;
+        }    
+    }
+}
+
+void m_divide(matrix_t *matrixA, matrix_t *matrixB, matrix_t *result)
+{
+    if(matrixA->columns != matrixB->rows)
+        return;
+
+    double aux;
+    for(size_t i = 0; i < matrixA->rows; i++) {
+        for(size_t j = 0; j < matrixB->columns; j++) {
+            for(size_t k = aux = 0; k < matrixA->columns; k++)
+                aux += ((matrixA->array[i][k])*(matrixB->array[k][j]));
+            
+            result->array[i][j] = aux;
+        }    
+    }
+}
+
+void m_create(size_t rows, size_t columns, matrix_t *matrix)
+{
+    matrix->rows = rows;
+    matrix->columns = columns;
+
+    matrix->array = (double **)malloc((rows) * sizeof(double));
+
+    for (size_t i = 0; i < rows; i++)
+         matrix->array[i] = (double *)malloc(columns * sizeof(double));
+}
+
+void m_destroy(matrix_t *matrix)
+{
+    for (size_t i = 0; i < matrix->rows; i++)
+         free(matrix->array[i]);
+
+    free(matrix->array);
+}
+
+bool m_isSimetric(matrix_t *matrix)
+{
+    matrix_t matrix_transpose;
+    m_create(matrix->columns, matrix->rows, &matrix_transpose);
+    m_transpose(matrix, &matrix_transpose);
+
+    size_t aux = 0;
+    for(size_t i = 0; i < matrix->rows; i++)
+        for(size_t j = 0; j < matrix->columns; j++)
+            if(matrix->array[i][j] == matrix_transpose.array[i][j])
+                aux++;
+
+    //if(aux == (r * c))
+    //    return true;
+
+    //return false;
+    return true * (aux == (matrix->rows * matrix->columns)) + false * (aux != (matrix->rows * matrix->columns));
+}
+
diff --git a/matrix_struct.h b/matrix_struct.h
@@ -0,0 +1,30 @@
+#ifndef _MATRIX_STRUCT_H
+#define _MATRIX_STRUCT_H
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <time.h>
+
+typedef struct {
+    size_t rows, columns;
+    double **array;
+} matrix_t;
+
+void m_print(matrix_t *matrix);
+void m_initrand(matrix_t *matrix);
+void m_load(matrix_t *matrix);
+void m_transpose(matrix_t *matrix, matrix_t *matrix_transpose);
+
+void m_add(matrix_t *matrixA, matrix_t *matrixB, matrix_t *result);
+void m_substract(matrix_t *matrixA, matrix_t *matrixB, matrix_t *result);
+void m_multiply(matrix_t *matrixA, matrix_t *matrixB, matrix_t *result);
+void m_divide(matrix_t *matrixA, matrix_t *matrixB, matrix_t *result);
+
+void m_create(size_t rows, size_t columns, matrix_t *matrix);
+void m_destroy(matrix_t *matrix);
+
+
+bool m_isSimetric(matrix_t *matrix);
+
+#endif