summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJasper2025-09-08 10:05:32 +0200
committerJasper2025-09-08 10:05:32 +0200
commitbf8973c4617c30b1474bed2b7e399d6bf7f68839 (patch)
tree7d70ba956fc68d0e8c720744ceefa60bfed5ad41
parent16175429d26b6803fd0249d5bbab6eb4cfe6e60b (diff)
More functions with '1' suffix
Added more versions of existing functions with suffix '1' that take a matrix as argument and perform the action (e.g. filling a matrix with random entries) to the passed matrix. 'matrix_add' and 'matrix_scale' save the result in the matrix passed first.
-rw-r--r--matrix.c77
-rw-r--r--matrix.h11
2 files changed, 53 insertions, 35 deletions
diff --git a/matrix.c b/matrix.c
index 0c5b590..cdb50ad 100644
--- a/matrix.c
+++ b/matrix.c
@@ -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)
diff --git a/matrix.h b/matrix.h
index c3138e2..75e605e 100644
--- a/matrix.h
+++ b/matrix.h
@@ -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);