This commit is contained in:
Elizabeth Hunt 2023-10-11 14:09:59 -06:00
parent 1ce75ab5fe
commit faaef032d8
Signed by: simponic
GPG Key ID: 52B3774857EB24B1
7 changed files with 68 additions and 7 deletions

10
homeworks/hw-4.org Normal file
View File

@ -0,0 +1,10 @@
* My Basic Routines
* Linear Solution Routines
** Gaussian Elimination with Backwards Substitution
** LU factorization
* Matrix Routines
** dotproduct
** crossproduct
** matrixvector
** matrixmatrix

View File

@ -25,6 +25,9 @@ extern double linf_distance(Array_double *v1, Array_double *v2);
extern void format_vector_into(Array_double *v, char *s);
extern void format_matrix_into(Matrix_double *m, char *s);
extern void put_identity_diagonal(Matrix_double *m);
extern Line *least_squares_lin_reg(Array_double *x, Array_double *y);
#endif // LIZFCM_H

View File

@ -11,15 +11,20 @@
size_t size; \
} Array_##TYPE
#define DEFINE_MATRIX(TYPE) \
typedef struct { \
Array_##TYPE **data; \
size_t cols; \
size_t rows; \
} Matrix_##TYPE
#define InitArray(TYPE, ...) \
({ \
TYPE temp[] = __VA_ARGS__; \
Array_##TYPE *arr = malloc(sizeof(Array_##TYPE)); \
arr->size = sizeof(temp) / sizeof(temp[0]); \
arr->data = malloc(arr->size * sizeof(TYPE)); \
if (arr->data) { \
memcpy(arr->data, temp, arr->size * sizeof(TYPE)); \
} \
arr; \
})
@ -28,13 +33,22 @@
Array_##TYPE *arr = malloc(sizeof(Array_##TYPE)); \
arr->size = SIZE; \
arr->data = malloc(arr->size * sizeof(TYPE)); \
if (arr->data) { \
for (size_t i = 0; i < arr->size; i++) \
arr->data[i] = INIT_VALUE; \
} \
arr; \
})
#define InitMatrixWithSize(TYPE, ROWS, COLS, INIT_VALUE) \
({ \
Matrix_##TYPE *matrix = malloc(sizeof(Matrix_##TYPE)); \
matrix->rows = ROWS; \
matrix->cols = COLS; \
matrix->data = malloc(matrix->rows * sizeof(Array_##TYPE *)); \
for (size_t y = 0; y < matrix->rows; y++) \
matrix->data[y] = InitArrayWithSize(TYPE, COLS, INIT_VALUE); \
matrix; \
})
#define c_max(x, y) (((x) >= (y)) ? (x) : (y))
#define c_min(x, y) (((x) <= (y)) ? (x) : (y))

View File

@ -10,6 +10,12 @@ DEFINE_ARRAY(int32_t);
DEFINE_ARRAY(float);
DEFINE_ARRAY(double);
DEFINE_MATRIX(int);
DEFINE_MATRIX(uint32_t);
DEFINE_MATRIX(int32_t);
DEFINE_MATRIX(float);
DEFINE_MATRIX(double);
typedef struct Line {
double m;
double a;

Binary file not shown.

22
src/matrix.c Normal file
View File

@ -0,0 +1,22 @@
#include "lizfcm.h"
#include <assert.h>
#include <stdio.h>
void put_identity_diagonal(Matrix_double *m) {
assert(m->rows == m->cols);
for (size_t y = 0; y < m->rows; ++y)
m->data[y]->data[y] = 1.0;
}
void format_matrix_into(Matrix_double *m, char *s) {
sprintf(s, "");
if (m->rows == 0)
sprintf(s, "empty");
for (size_t y = 0; y < m->rows; ++y) {
char row_s[256];
format_vector_into(m->data[y], row_s);
sprintf(s, "%s %s \n", s, row_s);
}
}

View File

@ -50,5 +50,11 @@ int main() {
line = least_squares_lin_reg(v, w);
printf("least_squares_lin_reg(v, w): (%f)x + %f\n", line->m, line->a);
char m_s[256];
Matrix_double *m = InitMatrixWithSize(double, 8, 8, 0.0);
put_identity_diagonal(m);
format_matrix_into(m, m_s);
printf("%s", m_s);
return 0;
}