Added comparison code; not working, but a good skeleton

This commit is contained in:
Miguel M 2023-05-01 15:26:36 +01:00
parent 678d1e1008
commit 94c1aa1ae8
3 changed files with 123 additions and 15 deletions

View File

@ -31,11 +31,11 @@ static inline size_t PIndex(size_t a_out, size_t b_out,
return b_out_i + b_out * (b_in_i + b_in * (a_out_i + a_out * a_in_i));
}
static inline size_t AInPermuted(size_t a_in_i, size_t *a_in_perm) {
static inline size_t AInPermuted(size_t a_in_i, const size_t *a_in_perm) {
return a_in_perm[a_in_i];
}
static inline size_t BInPermuted(size_t b_in_i, size_t *b_in_perm) {
static inline size_t BInPermuted(size_t b_in_i, const size_t *b_in_perm) {
return b_in_perm[b_in_i];
}

View File

@ -1,18 +1,37 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "../includes/cg.h"
#include "../includes/matrix.h"
#include "../includes/permutation.h"
#include "../includes/cg.h"
static void ReadArguments(char *from, size_t *restrict into,
const char *varname) {
static void ReadArguments(char *from, size_t *into, const char *varname) {
if (!sscanf(from, "%zu", into)) {
fprintf(stderr, "Failed to read %s.", varname);
exit(EXIT_FAILURE);
}
}
static inline void AdvanceConditionalPermutations(
permutation_generator_t *generators, size_t count) {
PermutationNext(generators);
for (size_t i = 0; i < count - 1; i++) {
if (generators[i].exhausted) {
PermutationReset(generators + i);
PermutationNext(generators + i + 1);
}
}
// If generators[-1] is exhausted, the conditional permutations are exhausted.
}
static inline void ResetConditionalPermutations(
permutation_generator_t *generators, size_t count) {
for (size_t i = 0; i < count; i++) {
PermutationReset(generators + i);
}
}
int main(int argc, char *argv[]) {
if (argc < 5) {
fprintf(
@ -30,18 +49,93 @@ int main(int argc, char *argv[]) {
size_t row_len = (a_out - 1) * (b_out - 1) * a_in * b_in +
(a_out - 1) * a_in + (b_out - 1) * b_in + 1;
matrix_t matrix = ParseMatrix(row_len);
//DebugPrintMatrix(&matrix);
// DebugPrintMatrix(&matrix);
size_t row_count = matrix.len / matrix.row_len;
data_t *p = malloc(a_out * b_out * a_in * b_in * sizeof(data_t));
data_t *p_buf = malloc(a_out * b_out * a_in * b_in * sizeof(data_t));
data_t *cg_buf =
malloc((a_out - 1) * (b_out - 1) * a_in * b_in * sizeof(data_t));
_Bool *seen = calloc(row_count, sizeof(_Bool));
// This is a lot of separate allocations, but this section isn't expected to
// be hot.
permutation_generator_t a_in_perm = PermutationNewGenerator(a_in);
permutation_generator_t b_in_perm = PermutationNewGenerator(b_in);
permutation_generator_t *a_out_perms =
malloc(a_in * sizeof(permutation_generator_t));
permutation_generator_t *b_out_perms =
malloc(b_in * sizeof(permutation_generator_t));
for (size_t i = 0; i < a_in; i++) {
a_out_perms[i] = PermutationNewGenerator(a_out);
}
for (size_t i = 0; i < b_in; i++) {
b_out_perms[i] = PermutationNewGenerator(b_out);
}
// Start testing for equivalences.
for (size_t lhs_i = 0; lhs_i < row_count - 1; lhs_i++) {
data_t *lhs = matrix.head + lhs_i * matrix.row_len;
if (seen[lhs_i]) {
continue;
}
for (size_t rhs_i = lhs_i + 1; rhs_i < row_count; rhs_i++) {
if (seen[rhs_i]) {
continue;
}
data_t *rhs = matrix.head + rhs_i * matrix.row_len;
PermutationReset(&a_in_perm);
while (!a_in_perm.exhausted) {
PermutationReset(&b_in_perm);
while (!b_in_perm.exhausted) {
ResetConditionalPermutations(a_out_perms, a_out);
while (!a_out_perms[a_out - 1].exhausted) {
ResetConditionalPermutations(b_out_perms, b_out);
while (!b_out_perms[b_out - 1].exhausted) {
// Compare the two rows
_Bool equivalent = 1;
FromCgToP(a_out, b_out, a_in, b_in, rhs, p_buf,
a_in_perm.permutation, b_in_perm.permutation,
a_out_perms, b_out_perms);
FromPToCg(a_out, b_out, a_in, b_in, cg_buf, p_buf);
for (size_t i = 0; i < row_len; i++) {
if (lhs[i] != cg_buf[i]) {
equivalent = 0;
break;
}
}
if (equivalent) {
seen[rhs_i] = 1;
goto skip_permutations;
}
AdvanceConditionalPermutations(b_out_perms, b_out);
}
AdvanceConditionalPermutations(a_out_perms, a_out);
}
PermutationNext(&b_in_perm);
}
PermutationNext(&a_in_perm);
}
skip_permutations:;
} // For loop over rhs_i
} // For loop over lhs_i
// Print every unique row
for (size_t i = 0; i < row_count; i++) {
data_t *row = matrix.head + i * row_len;
printf("%zu:--------------------------------\n", i);
PrintCg(a_out, b_out, a_in, b_in, row);
FromCgToP(a_out, b_out, a_in, b_in, row, p);
PrintP(a_out, b_out, a_in, b_in, p);
FromPToCg(a_out, b_out, a_in, b_in, row, p);
PrintCg(a_out, b_out, a_in, b_in, row);
if (!seen[i]) {
PrintMatrixRow(&matrix, i);
}
}
return EXIT_SUCCESS;

View File

@ -1,5 +1,6 @@
#include "../includes/permutation.h"
#include <stdio.h>
#include <stdlib.h>
permutation_generator_t PermutationNewGenerator(size_t len) {
@ -74,4 +75,17 @@ void PermutationNext(permutation_generator_t *permutation_generator) {
void PermutationFree(permutation_generator_t *permutation_generator) {
free(permutation_generator->permutation);
// No need to free permutation_generator->stack, since it's contiguous.
}
void PrintPermutation(permutation_generator_t *permutation_generator) {
printf("Permutation{");
for (size_t i = 0; i < permutation_generator->len - 1; i++) {
printf("%zu, ", permutation_generator->permutation[i]);
}
if (permutation_generator->len > 0) {
printf("%zu}\n",
permutation_generator->permutation[permutation_generator->len - 1]);
} else {
printf("}\n");
}
}