GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
#include "typedef.h"1#include "matrix.h"2#include "tools.h"34static int n;56/************************************************************************\7| The Matrix M is transformed to a matrix M' such that8| M' = Trf * M is Gauss-reduced, with Trf integral with determinante +-1.9\************************************************************************/1011int Trf_gauss(M, Trf)12matrix_TYP *M, *Trf;13{14int i,j;15int step;16int a1,a2,gcd;17int tester, *v;18int spos = 0;19int x,y;20int Mi, Ms;2122if(Trf->cols != M->rows)23{24printf("Error in Trf_gauss: matrices M and Trf are not compatible\n");25exit(3);26}27if(Trf->cols != Trf->rows)28{29printf("Error in Trf_gauss: Trf must be a square matrix\n");30exit(3);31}32n = Trf->rows;33for(i=0;i<n;i++)34for(j=0;j<n; j++)35Trf->array.SZ[i][j] = 0;36for(i=0;i<n;i++)37Trf->array.SZ[i][i] = 1;38for(step = 0; step < n; step++)39{40tester = FALSE;41while(tester == FALSE)42{43i = step;44while(i<n && M->array.SZ[i][spos] == 0)45i++;46if(i<n)47{48tester = TRUE;49if(i != step)50{51v = M->array.SZ[i];52M->array.SZ[i] = M->array.SZ[step];53M->array.SZ[step] = v;54v = Trf->array.SZ[i];55Trf->array.SZ[i] = Trf->array.SZ[step];56Trf->array.SZ[step] = v;57}58}59else60spos++;61if(spos == M->cols)62return(step);63}64for(i=step+1;i<n;i++)65{66if(M->array.SZ[i][spos] != 0)67{68gcd_darstell(M->array.SZ[step][spos], M->array.SZ[i][spos], &a1, &a2, &gcd);69if(a1 == 0)70{71v = M->array.SZ[step];72M->array.SZ[step] = M->array.SZ[i];73M->array.SZ[i] = v;74v = Trf->array.SZ[step];75Trf->array.SZ[step] = Trf->array.SZ[i];76Trf->array.SZ[i] = v;77j = a1; a1 = a2; a2 = j;78}79Ms = M->array.SZ[step][spos] / gcd;80Mi = M->array.SZ[i][spos] / gcd;81M->array.SZ[step][spos] /= gcd;82M->array.SZ[i][spos] /= gcd;83for(j=0;j<n;j++)84{85x = Trf->array.SZ[step][j];86y = Trf->array.SZ[i][j];87Trf->array.SZ[step][j] = a1 * x + a2 * y;88Trf->array.SZ[i][j] = Ms * y - Mi * x;89}90for(j=spos + 1; j<M->cols; j++)91{92x = M->array.SZ[step][j];93y = M->array.SZ[i][j];94M->array.SZ[step][j] = a1 * x + a2 * y;95M->array.SZ[i][j] = Ms * y - Mi * x;96}97M->array.SZ[step][spos] = gcd;98M->array.SZ[i][spos] = 0;99}100}101}102return(n);103}104105/************************************************************************\106| solve_mat(M) calculates an Matrix X with MX^{tr} = 0, such that107| the rows of X are a Z-basis of the solution space.108\************************************************************************/109matrix_TYP *solve_mat(M)110matrix_TYP *M;111{112matrix_TYP *M1, *M1t, *Trf, *X, *erg;113int i,rang, n;114115extern matrix_TYP *copy_mat();116extern matrix_TYP *tr_pose();117extern matrix_TYP *init_mat();118extern matrix_TYP *mat_mul();119120M1 = copy_mat(M);121row_gauss(M1);122M1t = tr_pose(M1);123n = M1t->rows;124Trf = init_mat(n, n, "");125rang = Trf_gauss(M1t, Trf);126X = init_mat(n-rang, n, "");127for(i=0;i<n-rang;i++)128X->array.SZ[i][rang+i] = 1;129erg = mat_mul(X, Trf);130free_mat(X); free_mat(Trf);131free_mat(M1); free_mat(M1t);132return(erg);133}134135136