commit 6f308ea05e3369fa9be4ac348130ba198a2a89ed
parent 67771b5ca46764f139aa3cd237d1d7990033dd7c
Author: klewer-martin <martin.cachari@gmail.com>
Date: Sun, 18 Apr 2021 18:00:16 -0300
Update;
Diffstat:
4 files changed, 164 insertions(+), 6 deletions(-)
diff --git a/main.c b/main.c
@@ -1,5 +1,5 @@
#include "main.h"
-#include "matrix_struct.h"
+#include "matrix.h"
#define R 3
@@ -32,7 +32,7 @@ int main (void)
m_print(&matrix2);
putchar('\n');
- m_multiply(&matrix, &matrix2, &matrix_r);
+ m_add(&matrix, &matrix2, &matrix_r);
m_print(&matrix_r);
putchar('\n');
diff --git a/matrix.c b/matrix.c
@@ -0,0 +1,144 @@
+#include "matrix.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("%10f ", 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)
+{
+ /* This operation can be only performed if both matrix are squared and has the same dimension*/
+ if((matrixA->rows != matrixB->rows) || (matrixA->columns != matrixB->columns))
+ return;
+
+ for(size_t i = 0; i < result->rows; i++) {
+ for(size_t j = 0; j < result->columns; j++) {
+ result->array[i][j] = matrixA->array[i][j] + matrixB->array[i][j];
+
+ }
+ }
+}
+
+void m_substract(matrix_t *matrixA, matrix_t *matrixB, matrix_t *result)
+{
+ /* This operation can be only performed if both matrix are squared and has the same dimension*/
+ if((matrixA->rows != matrixB->rows) || (matrixA->columns != matrixB->columns))
+ return;
+
+ for(size_t i = 0; i < result->rows; i++) {
+ for(size_t j = 0; j < result->columns; j++) {
+ result->array[i][j] = matrixA->array[i][j] - matrixB->array[i][j];
+
+ }
+ }
+}
+
+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.h
diff --git a/matrix_struct.c b/matrix_struct.c
@@ -1,4 +1,4 @@
-#include "matrix_struct.h"
+#include "matrix.h"
void m_print(matrix_t *matrix)
{
@@ -6,7 +6,7 @@ void m_print(matrix_t *matrix)
putchar('(');
putchar(' ');
for(size_t j = 0; j < matrix->columns; j++) {
- printf("%7.2f ", matrix->array[i][j]);
+ printf("%10f ", matrix->array[i][j]);
}
putchar(')');
putchar('\n');
@@ -46,16 +46,30 @@ void m_transpose(matrix_t *matrix, matrix_t *matrix_transpose)
void m_add(matrix_t *matrixA, matrix_t *matrixB, matrix_t *result)
{
- if(matrixA->columns != matrixB->rows)
+ /* This operation can be only performed if both matrix are squared and has the same dimension*/
+ if((matrixA->rows != matrixB->rows) || (matrixA->columns != matrixB->columns))
return;
+ for(size_t i = 0; i < result->rows; i++) {
+ for(size_t j = 0; j < result->columns; j++) {
+ result->array[i][j] = matrixA->array[i][j] + matrixB->array[i][j];
+
+ }
+ }
}
void m_substract(matrix_t *matrixA, matrix_t *matrixB, matrix_t *result)
{
- if(matrixA->columns != matrixB->rows)
+ /* This operation can be only performed if both matrix are squared and has the same dimension*/
+ if((matrixA->rows != matrixB->rows) || (matrixA->columns != matrixB->columns))
return;
+ for(size_t i = 0; i < result->rows; i++) {
+ for(size_t j = 0; j < result->columns; j++) {
+ result->array[i][j] = matrixA->array[i][j] - matrixB->array[i][j];
+
+ }
+ }
}
void m_multiply(matrix_t *matrixA, matrix_t *matrixB, matrix_t *result)