aboutsummaryrefslogtreecommitdiff
path: root/libcrystfel/src/integer_matrix.c
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2012-10-10 16:08:52 +0200
committerThomas White <taw@physics.org>2012-10-10 16:09:32 +0200
commit9b7696c48970b09d5852ea7515f362688afcfcb7 (patch)
treed261a9eb6d3105b63157d3b1da747cf5ad0db34c /libcrystfel/src/integer_matrix.c
parent0fb7efdacfc2822587f9d20bdb8958d05086fbe7 (diff)
Split cofactor calculation
Diffstat (limited to 'libcrystfel/src/integer_matrix.c')
-rw-r--r--libcrystfel/src/integer_matrix.c58
1 files changed, 24 insertions, 34 deletions
diff --git a/libcrystfel/src/integer_matrix.c b/libcrystfel/src/integer_matrix.c
index 5aefb958..477d16a6 100644
--- a/libcrystfel/src/integer_matrix.c
+++ b/libcrystfel/src/integer_matrix.c
@@ -235,6 +235,28 @@ static IntegerMatrix *delete_row_and_column(IntegerMatrix *m,
}
+static signed int cofactor(IntegerMatrix *m,
+ unsigned int i, unsigned int j)
+{
+ IntegerMatrix *n;
+ signed int t, C;
+
+ n = delete_row_and_column(m, i, j);
+ if ( n == NULL ) {
+ fprintf(stderr, "Failed to allocate matrix.\n");
+ return 0;
+ }
+
+ /* -1 if odd, +1 if even */
+ t = (i+j) & 0x1 ? -1 : +1;
+
+ C = t * intmat_det(n);
+ intmat_free(n);
+
+ return C;
+}
+
+
/**
* intmat_det:
* @m: An %IntegerMatrix
@@ -258,23 +280,7 @@ signed int intmat_det(IntegerMatrix *m)
i = 0; /* Fixed */
for ( j=0; j<m->cols; j++ ) {
- signed int C;
- IntegerMatrix *n;
- signed int t;
-
- n = delete_row_and_column(m, i, j);
- if ( n == NULL ) {
- fprintf(stderr, "Failed to allocate matrix.\n");
- return 0;
- }
-
- /* -1 if odd, +1 if even */
- t = (i+j) & 0x1 ? -1 : +1;
-
- C = t * intmat_det(n);
- intmat_free(n);
-
- det += intmat_get(m, i, j) * C;
+ det += intmat_get(m, i, j) * cofactor(m, i, j);
}
@@ -293,23 +299,7 @@ static IntegerMatrix *intmat_cofactors(IntegerMatrix *m)
for ( i=0; i<n->rows; i++ ) {
for ( j=0; j<n->cols; j++ ) {
- signed int C;
- IntegerMatrix *M;
- signed int t;
-
- M = delete_row_and_column(m, i, j);
- if ( M == NULL ) {
- fprintf(stderr, "Failed to allocate matrix.\n");
- return 0;
- }
-
- /* -1 if odd, +1 if even */
- t = (i+j) & 0x1 ? -1 : +1;
-
- C = t * intmat_det(M);
- intmat_free(M);
-
- intmat_set(n, i, j, C);
+ intmat_set(n, i, j, cofactor(m, i, j));
}
}