GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
#include "typedef.h"1#include "tools.h"23/**************************************************************************\4@---------------------------------------------------------------------------5@---------------------------------------------------------------------------6@ FILE: p_mat_det.c7@---------------------------------------------------------------------------8@---------------------------------------------------------------------------9@10\**************************************************************************/111213/**************************************************************************\14@---------------------------------------------------------------------------15@ int p_mat_det(Mat, prime)16@ matrix_TYP *Mat;17@ int prime;18@19@ Calculates the determinant of Mat modulo prime.20@ The entries of Mat are not changed.21@---------------------------------------------------------------------------22@23\**************************************************************************/24int p_mat_det(Mat, prime)25matrix_TYP *Mat;26int prime;27{28int i,j;29int step, dim;30int **M, *tmp;31int det, msi;3233dim = Mat->cols;34if(prime < 0)35prime = -prime;36if(Mat->rows != Mat->cols)37{38fprintf(stderr, "error: can't calculate the determinante of a non-square matrix\n");39exit(3);40}41if((M = (int **)malloc(dim *sizeof(int *))) == 0)42{43fprintf(stderr, "malloc of M in 'p_mat_det' failed\n");44exit(2);45}46for(i=0;i<dim;i++)47{48if((M[i] = (int *)malloc(dim *sizeof(int))) == 0)49{50fprintf(stderr, "malloc of M[%d] in 'p_mat_det' failed\n", i);51exit(2);52}53}54for(i=0;i<dim;i++)55for(j=0;j<dim;j++)56M[i][j] = Mat->array.SZ[i][j] % prime;57det = 1;58for(step = 0; step < dim;step++)59{60/************************************************************************\61| search for non zero entry in the colmn no. step and62| swap it to M[step][step]63\************************************************************************/64for(i=step; i<dim && M[i][step] == 0; i++);65if(i == dim)66{67for(j=0;j<dim;j++)68free(M[j]);69free(M);70return(0);71}72if(i != step){73tmp = M[step]; M[step] = M[i]; M[i] = tmp; det = -det;74}75msi = p_inv(M[step][step], prime);76i = (msi * M[step][step])%prime;77if(i<0) i+=prime;78if(i != 1)79printf("TEST %d\n", i);8081/* multiplying the step-th row with msi */82for(i=step+1;i<dim;i++){83M[step][i] *= msi;84M[step][i] %=prime;85}8687/* changed tilman 9/12/96 from:88det *= msi; det %=prime;89to :*/9091det *= M[step][step]; det %=prime;9293M[step][step] = 1;9495/************************************************************************\96| clear column no. step97\************************************************************************/98for(i=step+1;i<dim;i++)99{100if(M[i][step] != 0)101{102for(j=step+1;j<dim;j++)103{104M[i][j] -= M[i][step] * M[step][j];105M[i][j] %= prime;106}107M[i][step] = 0;108}109}110}111for(i=0;i<dim;i++)112free(M[i]);113free(M);114det %= prime;115if(det < -(prime/2))116det += prime;117if(det > (prime/2))118det -=prime;119if(prime == 2 && det == -1)120det = 1;121return(det);122}123124125