GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
#include <typedef.h>1#include <getput.h>2#include <matrix.h>3#include <base.h>4#include <longtools.h>5#include <presentation.h>6789/* -------------------------------------------------------------------------- */10/* tests if M == Id */11/* -------------------------------------------------------------------------- */12static boolean equal_id(matrix_TYP *M)13{14int i, j;1516if (M->rows != M->cols)17return(FALSE);1819for (i = 0; i < M->rows; i++){20for (j = 0; j < M->cols; j++){21if (i == j){22if (M->array.SZ[i][j] != 1)23return(FALSE);24}25else{26if (M->array.SZ[i][j] != 0)27return(FALSE);28}29}30}31return(TRUE);32}33343536/* -------------------------------------------------------------------------- */37/* transform a matrix A to standard form */38/* -------------------------------------------------------------------------- */39void standard_form(matrix_TYP *A,40matrix_TYP *D,41int first)42{43int i, j;4445for (i = 0; i < A->rows; i++){46for (j = 0; j < A->cols; j++){47A->array.SZ[j][i] %= D->array.SZ[j + first][j + first];48if (A->array.SZ[j][i] < 0)49A->array.SZ[j][i] += D->array.SZ[j + first][j + first];50}51}52}53545556/* -------------------------------------------------------------------------- */57/* invert A : C_a x C_b x ... -> C_a x C_b x ... */58/* -------------------------------------------------------------------------- */59matrix_TYP *graph_mat_inv(matrix_TYP *A,60matrix_TYP *D,61int first)62{63int i;6465matrix_TYP *B, *C;6667C = copy_mat(A);68B = init_mat(A->rows, A->cols, "1");6970while (equal_id(C) != TRUE){71free_mat(B);72B = C;73C = NULL;74C = mat_mul(B, A);75standard_form(C, D, first);76}77free_mat(C);78return(B);79}80818283/* -------------------------------------------------------------------------- */84/* mapped word for A[i] : C_a x C_b x ... -> C_a x C_b x ... */85/* -------------------------------------------------------------------------- */86matrix_TYP *graph_mapped_word(int *w,87matrix_TYP **A,88matrix_TYP **AINV,89matrix_TYP *D)90{9192int i, first;9394matrix_TYP *M;959697for (first = 0; first < D->cols && D->array.SZ[first][first] == 1; first++);9899if (w[0] == 0)100return init_mat(A[0]->cols,A[0]->cols,"i1");101102if (w[1] < 0) {103if (AINV[-w[1]-1] == NULL)104AINV[-w[1]-1] = graph_mat_inv(A[-w[1]-1], D, first);105M = copy_mat(AINV[-w[1]-1]);106}107else{108M = copy_mat(A[w[1]-1]);109}110111for (i=2;i<=w[0];i++){112if (w[i] < 0){113if (AINV[-w[i]-1] == NULL)114AINV[-w[i]-1] = graph_mat_inv(A[-w[i]-1], D, first);115mat_muleq(M,AINV[-w[i]-1]);116}117else{118mat_muleq(M,A[w[i]-1]);119}120standard_form(M, D, first);121}122123return M;124125} /* graph_mapped_word(...) */126127128129130