GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
/*{{{}}}*/1/*{{{ include*/2#include "typedef.h"3#include "tools.h"4#include "matrix.h"5#include "getput.h"6#include <string.h>78/*}}} */910/**************************************************************************\11@---------------------------------------------------------------------------12@---------------------------------------------------------------------------13@ FILE: get_mat.c14@---------------------------------------------------------------------------15@---------------------------------------------------------------------------16@17\**************************************************************************/18/*-----------------------------------------------------------*\19| Read matrix from infile |20\*-----------------------------------------------------------*/2122/*{{{ static alloc_N_if_neccessary*/23static void24alloc_N_if_neccessary( mat, flags )25matrix_TYP *mat;26flag_TYP *flags;27{2829if ( mat->array.N == NULL ) {30if ( mat->prime != 0 ) {31fprintf(stderr,"get_mat: Error: matrix over GF(%d) tries to be rational\n",32mat->prime);33exit( 3 );34} else if ( mat->kgv != 1 ) {35fprintf(stderr,"get_mat: Error: matrix tries to use kgv %d and rational representation.\n",36mat->kgv );37exit( 3 );38} else {39flags->Integral = FALSE;40mat->array.N = (int **)malloc2dim( mat->rows, mat->cols,41sizeof(int) );42memset2dim( (char **) mat->array.N,43mat->rows, mat->cols, sizeof(int), (char *)&Zero.n );44}45}46}4748/*}}} */49/*{{{ fget_mat*/5051/**************************************************************************\52@---------------------------------------------------------------------------53@ matrix_TYP *fget_mat (infile)54@ FILE *infile;55@ reads a matrix from infile56@---------------------------------------------------------------------------57@58\**************************************************************************/5960matrix_TYP *fget_mat (infile)61FILE *infile;6263{64int rM, cM;65int **M;66matrix_TYP *mat;67flag_TYP flags;68char string[256], *str ;69int i, j ;7071727374flags.Integral =75flags.Symmetric =76flags.Diagonal =77flags.Scalar = FALSE;78/*------------------------------------------------------------*\79| Read and scan header line |80\*------------------------------------------------------------*/81fscanf (infile, "%[ \t\n]", string);82fscanf (infile, "%[^\n]",string);83strtok (string, "%");84sscanf (string, "%d", &rM);85if ( (str = strpbrk (string, "xd")) != NULL ) {86sscanf ( ++str, "%d", &cM);87if ( str[-1] == 'x' ) {88if ( cM == 0 ) {89cM = rM;90flags.Symmetric = TRUE;91}92} else {93flags.Symmetric = flags.Diagonal = TRUE;94flags.Scalar = (cM == 0);95cM = rM;96}97}98else {99cM = rM;100}101/*102* WARNING: You can read matrizes either of prime_Typ or with103* kgv. If both are set in the headline, the second entry104* will be ignored.105*/106mat = init_mat(rM,cM,"");107if ( (str = strchr (string, '/')) != NULL ) {108if(str[1] == 'p') {109str += 2;110sscanf(str, "%d", &mat->prime);111flags.Integral = TRUE;112} else {113sscanf(++str, "%d", &mat->kgv);114flags.Integral = FALSE;115}116} else {117flags.Integral = TRUE;118}119M = mat->array.SZ;120for ( i = 0; i < rM; i++) {121/*122* Read the matrix123*/124if ( flags.Scalar ) {125fscanf( infile, "%s", string );126if ( strchr( string, '/') == NULL ) {127sscanf(string, "%d", &M[0][0]);128for ( i = 1; i < rM; i++ )M[i][i]= M[0][0];129} else {130alloc_N_if_neccessary( mat, &flags );131sscanf(string, "%d/%d", &M[0][0],&mat->array.N[0][0]);132for ( i = 1; i < rM; i++ ) {133M[i][i]= M[0][0];134mat->array.N[i][i] = mat->array.N[0][0];135}136}137} else {138if ( flags.Diagonal ) {139for ( i = 0; i < cM; i++ ) {140fscanf( infile, "%s", string );141if ( strchr( string, '/') == NULL ) {142sscanf(string, "%d", &M[i][i]);143} else {144alloc_N_if_neccessary( mat, &flags );145sscanf(string, "%d/%d", &M[i][i],&mat->array.N[i][i]);146}147}148} else {149for ( i = 0; i < rM; i++ ) {150for ( j = 0; j < ( flags.Symmetric ? i+1 : cM ); j++ ) {151fscanf( infile, "%s", string );152if ( strchr( string, '/') == NULL ) {153sscanf(string, "%d", &M[i][j]);154} else {155alloc_N_if_neccessary( mat, &flags );156sscanf(string, "%d/%d", &M[i][j],&mat->array.N[i][j]);157}158}159}160if ( flags.Symmetric ) {161if ( mat->array.N != NULL ) {162for ( i = 0; i < rM; i++) {163for ( j = 0; j < i; j++) {164M[j][i]= M[i][j];165mat->array.N[j][i] = mat->array.N[i][j];166}167}168} else {169for ( i = 0; i < rM; i++) {170for ( j = 0; j < i; j++) {171M[j][i]= M[i][j];172}173}174}175}176}177}178}179mat->flags.Integral = flags.Integral;180mat->flags.Symmetric = flags.Symmetric;181mat->flags.Diagonal = flags.Diagonal;182mat->flags.Scalar = flags.Scalar;183Check_mat(mat);184return ( mat );185}186187188/*}}} */189/*{{{ get_mat*/190191/**************************************************************************\192@---------------------------------------------------------------------------193@ matrix_TYP *get_mat (file_name)194@ char *file_name;195@ reads a matrix from the file with name 'file_name'196@---------------------------------------------------------------------------197@198\**************************************************************************/199matrix_TYP *get_mat (file_name)200char *file_name;201202{203matrix_TYP *mat;204FILE *infile;205206/*------------------------------------------------------------*\207| Open input file |208\*------------------------------------------------------------*/209if ( file_name == NULL )210infile = stdin;211else212if ( (infile = fopen (file_name, "r")) == NULL ) {213214fprintf (stderr, "Could not open input-file %s\n", file_name);215exit (4);216}217/*------------------------------------------------------------*\218| Call fget_mat |219\*------------------------------------------------------------*/220mat = fget_mat(infile);221/*------------------------------------------------------------*\222| Close input file |223\*------------------------------------------------------------*/224if ( infile != stdin )225fclose (infile);226return ( mat );227}228229/*}}} */230/*{{{ mget_mat*/231/*232@-------------------------------------------------------------------------233@234@ mat_array = mget_mat( file_name, anz );235@236@ differs from fmget_mat() only in specifying the file name instead of the237@ file-pointer.238@239@-------------------------------------------------------------------------240*/241matrix_TYP **mget_mat (file_name, anz)242char *file_name;243int *anz;244{245matrix_TYP **mat;246FILE *infile;247248249250251/*252* Open input file253*/254if ( file_name == NULL ) {255infile = stdin;256} else {257if ( (infile = fopen (file_name, "r")) == NULL ) {258perror("mget_mat: Error: Could not open input-file");259exit (4);260}261}262mat = fmget_mat( infile, anz );263/*264* Close input file265*/266if ( infile != stdin ) {267fclose (infile);268}269return ( mat );270}271272/*}}} */273/*{{{ fmget_mat*/274/*275@-------------------------------------------------------------------------276@277@ mat_array = fmget_mat( infile, anz );278@279@ matrix_TYP **mat_array: array of matrices read from infile280@281@ FILE *infile: the file the matrices are read from282@ int *anz: the number of matrices that have been read from infile.283@ is set by the function.284@-------------------------------------------------------------------------285@286*/287matrix_TYP **fmget_mat (infile, anz)288FILE *infile;289int *anz;290{291matrix_TYP **mat;292char string[512];293int k ;294295/*296* Open input file297*/298fscanf (infile, "%[^\n]",string);299if ( string[0] != '#' ) {300*anz = 1;301mat = (matrix_TYP **)malloc(sizeof(matrix_TYP *));302rewind(infile);303mat[0] = fget_mat(infile);304} else {305sscanf (string, "#%u", anz);306mat = (matrix_TYP **)malloc(*anz*sizeof(matrix_TYP *));307for ( k = 0; k < *anz; k++) {308mat[k] = fget_mat(infile);309}310}311return ( mat );312}313/*}}} */314315316