GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
#include "typedef.h"1#include "gmp.h"2/* #include "gmp-impl.h" */3#include "longtools.h"4#include "matrix.h"56/**************************************************************************\7@---------------------------------------------------------------------------8@---------------------------------------------------------------------------9@ FILE: long_gauss.c10@---------------------------------------------------------------------------11@---------------------------------------------------------------------------12@13\**************************************************************************/141516/**************************************************************************\17@---------------------------------------------------------------------------18@ int long_row_gauss(Mat)19@ matrix_TYP *Mat;20@21@ applies an integral gaussian algorithm to the rows of mat.22@ the rank is returned23@ the function uses GNU MP to avoid overflow24@---------------------------------------------------------------------------25@26\**************************************************************************/27int long_row_gauss(Mat)28matrix_TYP *Mat;29{30int rang;31MP_INT **M;3233M = matrix_to_MP_mat(Mat);34rang = MP_row_gauss(M, Mat->rows, Mat->cols);35write_MP_mat_to_matrix(Mat, M);36free_MP_mat(M, Mat->rows, Mat->cols);37free(M);38return(rang);39}4041/************************************************************************42@43@------------------------------------------------------------------------44@45@ int long_row_basis(Mat,int flag)46@47@ returns the rank of the INTEGRAL matrix Mat.48@ The matrix Mat is changed in such a way that the first rank rows49@ of it form an integral basis of the Lattices spanned by the rows50@ of the matrix Mat.51@52@------------------------------------------------------------------------53@54*************************************************************************/55int long_row_basis(Mat,flag)56matrix_TYP *Mat;57int flag;58{59int rang,60cols = Mat->cols,61i,62j,63k;6465MP_INT merk,66**M,67**M3,68**M4,69**T1;707172M = matrix_to_MP_mat(Mat);73rang = MP_row_gauss(M, Mat->rows, Mat->cols);7475/* write this matrix back and check if it fits into int's */76k = FALSE;77for (i=0;i<Mat->rows;i++)78for (j=0;j<Mat->cols;j++)79if (abs(M[i][j]._mp_size)>1)80k = TRUE;81else82Mat->array.SZ[i][j] = mpz_get_si(&M[i][j]);8384if (k || flag){85/***************************************************************\86| make a pair_reduction to the rows of M if needed87\***************************************************************/88mpz_init_set_ui(&merk,0);89M3 = init_MP_mat(rang, rang);90for(i=0;i<rang;i++)91for(j=0;j<=i;j++)92{93for(k=0;k<cols;k++)94{95mpz_mul(&merk, &M[i][k], &M[j][k]);96mpz_add(&M3[i][j], &merk, &M3[i][j]);97}98}99for(i=0;i<rang;i++)100for(j=0;j<i;j++)101mpz_set(&M3[j][i], &M3[i][j]);102103/* output for debugging purposes104dump_MP_mat(M3,rows,rows,"M3"); */105106T1 = init_MP_mat(rang, rang);107for(i=0;i<rang;i++)108mpz_set_si(&T1[i][i], 1);109MP_pair_red(M3, T1, rang);110111/* output for debugging purposes112dump_MP_mat(T1,rows,rows,"T1"); */113114M4 = init_MP_mat(Mat->rows, cols);115for(i=0;i<rang;i++)116for(j=0;j<cols;j++)117for(k=0;k<rang;k++)118{119mpz_mul(&merk, &T1[i][k], &M[k][j]);120mpz_add(&M4[i][j], &M4[i][j], &merk);121}122123/* output for debugging purposes124dump_MP_mat(M4,rows,rows,"M4"); */125126write_MP_mat_to_matrix(Mat, M4);127128free_MP_mat(M, Mat->rows, Mat->cols);129free(M);130free_MP_mat(M3,rang,rang);131free(M3);132free_MP_mat(M4,Mat->rows,cols);133free(M4);134free_MP_mat(T1, rang,rang);135free(T1);136mpz_clear(&merk);137}138else{139free_MP_mat(M, Mat->rows, Mat->cols);140free(M);141}142143return(rang);144}145146/**************************************************************************\147@---------------------------------------------------------------------------148@ int long_row_trf_gauss(Mat,T)149@ matrix_TYP *Mat, *T;150@151@ applies an integral gaussian algorithm to the rows of mat.152@ the rank is returned153@ the function uses GNU MP to avoid overflow.154@ The tranformation is written to the matrix T.155@---------------------------------------------------------------------------156@157\**************************************************************************/158int long_row_trf_gauss(M, T)159matrix_TYP *M, *T;160{161int rang;162MP_INT **N, **S;163164N = matrix_to_MP_mat(M);165S = init_MP_mat(M->rows, M->rows);166rang = MP_trf_gauss(N, S, M->rows, M->cols);167write_MP_mat_to_matrix(M, N);168write_MP_mat_to_matrix(T, S);169free_MP_mat(N, M->rows, M->cols); free_MP_mat(S,M->rows, M->rows);170free(N); free(S);171return(rang);172}173174175176/**************************************************************************\177@---------------------------------------------------------------------------178@ int long_row_gauss_simultaneous(A, B)179@ matrix_TYP *A, *B;180@181@ Applies an integral gaussian algorithm to the rows of mat.182@ The rank is returned183@ The function uses GNU MP to avoid overflow.184@ The tranformations are applied simultaneously to the matrix B.185@---------------------------------------------------------------------------186@187\**************************************************************************/188int long_row_gauss_simultaneous(A, B)189matrix_TYP *A, *B;190{191int rang;192MP_INT **MA, **MB;193194MA = matrix_to_MP_mat(A);195MB = matrix_to_MP_mat(B);196rang = MP_row_gauss_simultaneous(MA, A->rows, A->cols, MB, B->cols);197write_MP_mat_to_matrix(A, MA);198write_MP_mat_to_matrix(B, MB);199free_MP_mat(MA, A->rows, A->cols); free_MP_mat(MB, B->rows, B->cols);200free(MA); free(MB);201return(rang);202}203204205