diff options
-rw-r--r-- | matrix.c | 77 | ||||
-rw-r--r-- | matrix.h | 11 |
2 files changed, 53 insertions, 35 deletions
@@ -105,13 +105,23 @@ Matrix *matrix_const(size_t m, size_t n, double x) return mat; } -Matrix *matrix_copy (const Matrix *mat) +void matrix_const1(Matrix *mat, double x) +{ + matrix_foreach(mat, it) *it = x; +} + +Matrix *matrix_copy(const Matrix *mat) { Matrix *tmp = matrix_alloc(mat->m, mat->n); matrix_foreach_idx(tmp, it, i, j) *it = matrix_at(mat, i, j); return tmp; } +void matrix_copy1(Matrix *A, const Matrix *B) +{ + matrix_foreach_idx(A, a, i, j) *a = matrix_at(B, i, j); +} + Matrix *matrix_swap(const Matrix *mat, MatrixSwapType t, size_t i, size_t j) { Matrix *tmp = matrix_copy(mat); @@ -189,6 +199,11 @@ Matrix *matrix_add(const Matrix *A, const Matrix *B) return C; } +void matrix_add1(Matrix *A, const Matrix *B) +{ + matrix_foreach_idx(A, a, i, j) *a += matrix_at(B, i, j); +} + Matrix *matrix_scale(const Matrix *A, double x) { Matrix *tmp = matrix_alloc(A->m, A->n); @@ -197,6 +212,11 @@ Matrix *matrix_scale(const Matrix *A, double x) return tmp; } +void matrix_scale1(Matrix *A, double x) +{ + matrix_foreach(A, a) *a *= x; +} + Matrix *matrix_sub(const Matrix *A, const Matrix *B) { assert(A->m == B->m && A->n == B->n && "Dimension mismatch"); @@ -207,6 +227,11 @@ Matrix *matrix_sub(const Matrix *A, const Matrix *B) return C; } +void matrix_sub1(Matrix *A, const Matrix *B) +{ + matrix_foreach_idx(A, a, i, j) *a -= matrix_at(B, i, j); +} + Matrix *matrix_mult(const Matrix *A, const Matrix *B) { assert(A->n == B->m && "Dimension mismatch"); @@ -222,48 +247,38 @@ Matrix *matrix_mult(const Matrix *A, const Matrix *B) Matrix *matrix_rand(size_t m, size_t n, int bound_l, int bound_u, MatrixType type) { + Matrix *mat = matrix_alloc(m, n); + matrix_rand1(mat, bound_l, bound_u, type); + return mat; +} + +void matrix_rand1(Matrix *mat, int bound_l, int bound_u, MatrixType type) +{ assert(bound_l <= bound_u && "Lower bound is expected to be less or equal than upper bound"); if (bound_l == bound_u) - { - return matrix_const(m, n, bound_l); - } - - Matrix *mat = matrix_const(m, n, 0); + matrix_const1(mat, bound_l); + bool zero; matrix_foreach_idx(mat, it, i, j) { - switch (type) - { - case MATRIX_DIAG: - if (i != j) continue; - break; - case MATRIX_TRIAG_UPPER: - if (i > j) continue; - break; - case MATRIX_TRIAG_LOWER: - if (i < j) continue; - break; - case MATRIX_TRIAG_SUPPER: - if (i >= j) continue; - break; - case MATRIX_TRIAG_SLOWER: - if (i <= j) continue; - break; - case MATRIX_NONE: break; - } - *it = int_rand(bound_l, bound_u); + zero = + (type == MATRIX_DIAG && i != j) || + (type == MATRIX_TRIAG_UPPER && i > j) || + (type == MATRIX_TRIAG_LOWER && i < j) || + (type == MATRIX_TRIAG_SUPPER && i >= j) || + (type == MATRIX_TRIAG_SLOWER && i <= j); + + *it = zero ? 0 : int_rand(bound_l, bound_u); } - - return mat; } -int matrix_eq(const Matrix *A, const Matrix *B, double tol) +bool matrix_eq(const Matrix *A, const Matrix *B, double tol) { assert(A->m == B->m && A->n == B->n && "Dimension mismatch"); matrix_foreach_idx(A, a, i, j) - if (ABS(*a - matrix_at(B, i, j)) > tol) return FALSE; - return TRUE; + if (ABS(*a - matrix_at(B, i, j)) > tol) return false; + return true; } double matrix_norm_frob(const Matrix *mat) @@ -19,9 +19,6 @@ #define matrix_is_square(mat) ((mat)->m == (mat)->n) -#define TRUE 1 -#define FALSE 0 - typedef enum { MATRIX_DIAG, MATRIX_TRIAG_UPPER, @@ -51,17 +48,23 @@ Matrix *matrix_from_arr(double arr[], size_t m, size_t n); char *matrix_to_str(const Matrix *mat); Matrix *matrix_id(size_t n); Matrix *matrix_const(size_t m, size_t n, double x); +void matrix_const1(Matrix *mat, double x); Matrix *matrix_copy (const Matrix *mat); +void matrix_copy1(Matrix *A, const Matrix *B); Matrix *matrix_swap(const Matrix *mat, MatrixSwapType t, size_t i, size_t j); void matrix_swap1(Matrix *mat, MatrixSwapType t, size_t i, size_t j); double matrix_trace(const Matrix *mat); Matrix *matrix_transpose(const Matrix *mat); Matrix *matrix_add(const Matrix *A, const Matrix *B); +void matrix_add1(Matrix *A, const Matrix *B); Matrix *matrix_scale(const Matrix *A, double x); +void matrix_scale1(Matrix *A, double x); Matrix *matrix_sub(const Matrix *A, const Matrix *B); +void matrix_sub1(Matrix *A, const Matrix *B); Matrix *matrix_mult(const Matrix *A, const Matrix *B); Matrix *matrix_rand(size_t m, size_t n, int bound_l, int bound_u, MatrixType type); -int matrix_eq(const Matrix *A, const Matrix *B, double tol); +void matrix_rand1(Matrix *mat, int bound_l, int bound_u, MatrixType type); +bool matrix_eq(const Matrix *A, const Matrix *B, double tol); double matrix_norm_frob(const Matrix *mat); Matrix **matrix_LR(const Matrix *A, const Matrix *b); Matrix *matrix_forwardel(const Matrix *L, const Matrix *b); |