GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
#include "typedef.h"1#include "tools.h"2#include "matrix.h"3#include "getput.h"45/**************************************************************************\6@---------------------------------------------------------------------------7@---------------------------------------------------------------------------8@ FILE: put_mat.c9@---------------------------------------------------------------------------10@---------------------------------------------------------------------------11@12\**************************************************************************/13/*{{{}}}*/14/*{{{ put_mat*/15/*16@17@ void put_mat( mat, file_name, comment, options );18@ void fput_mat (outfile, mat, comment, options)19@20@ result: void21@ arguments:22@ matrix_TYP *mat -- the matrix to be written23@ char * file_name -- the name of the file the matrix will be written to24@ if file_name == NULL, we write to stdout25@ char * comment -- optional comment written to the output-file26@ unsigned int options -- options to controll the output of the matrix27@28@ Currently, there are two options. The one is called PM_RATIONAL and29@ has a value of 0x00000001 (defined in ../../include/matrix.h ). It30@ causes the matrix to be written with numerator and denominator for31@ each entry if it isn't32@ integral. The flag is still ignored for matrices that have array.N33@ allocated. If PM_NORMALIZED is not set, even rational matrices are34@ written in lcd-representation]35@ The other flag is called PM_SHORTCUT and causes the flag-entries36@ of the matrix not to be ignored, i.e. the matrix is not printed as37@ an NxM array, if it is symmetric, diagonal or even scalar.38@39@ This is the first time since I started to compet with this confused40@ source-code that I discover that someone used bit-flags, wich is a41@ nice thing, though the one forgot to #define and document them.42@43@ These flags should be used the following way:44@45@ put_mat( ... , PM_RATIONAL );46@ or put_mat( ... , PM_SHORTCUT );47@ or put_mat( ... , PM_SHORTCUT | PM_RATIONAL );48@ or just put_mat( ... , 0UL );49@50@ Note: "0UL" means just "(unsigned long)0"51@-------------------------------------------------------------------------52@53*/54void put_mat (mat, file_name, comment, options)55matrix_TYP *mat;56char file_name[], comment[] ;57unsigned long options;58{59FILE *outfile;6061626364/*65* Open output file66*/67if ( file_name != NULL ) {68if ( (outfile = fopen (file_name, "w")) == NULL ) {69perror("put_mat: Error in fopen()");70exit (4);71}72} else {73outfile = stdout;74}75fput_mat (outfile, mat, comment, options);76if ( outfile != stdout ) {77fclose (outfile);78#if 079fprintf (stderr, "%s written to %s\n", comment, file_name);80#endif81}82fflush(stdout);83}8485/*}}} */86/*{{{ fput_mat*/878889/*--------------------------------------------------------------------*\90| the same as put_mat, but output_file must already have been |91| opened |92\*--------------------------------------------------------------------*/9394void fput_mat (outfile, mat, comment, options)95FILE *outfile;96matrix_TYP *mat;97char comment[] ;98unsigned long options;99{100int *max_lenZ;101flag_TYP flags;102rational d, *max_lenQ;103boolean Normalized;104int i, j, str_len;105char string[132];106char format[32];107108Normalized = !(options & PM_RATIONAL);109/*110* claus: I've got to correct that one day! Normalized means to111* print out a matrix with kgv.112*/113if ( mat->array.N != NULL ) Normalized = 0;114115flags = mat->flags;116if ( !(options & PM_SHORTCUT ) )117{118flags.Symmetric =119flags.Diagonal =120flags.Scalar = FALSE;121}122123/*124* Print header line125*/126fprintf (outfile, "%d", mat->rows);127if ( mat->rows != mat->cols ) {128fprintf (outfile, "x%d", mat->cols);129} else {130if ( flags.Symmetric ) {131if ( flags.Diagonal ) {132fprintf (outfile, "d");133if ( flags.Scalar ) {134fprintf (outfile, "0");135} else {136fprintf (outfile, "1"); /* ! Scalar */137}138} else {139fprintf (outfile, "x0"); /* !Diagonal */140}141} /* Symmetric */142}143if ( !flags.Integral && mat->array.N == NULL ) {144fprintf (outfile, "\t/");145if ( Normalized ) {146fprintf (outfile, "%d\t", mat->kgv);147} else {148fprintf (outfile, "0\t" );149}150}151fprintf (outfile, "\t%% %s\n", comment);152153/*154* print matrix155*/156if ( flags.Integral || Normalized ) {157if ( flags.Diagonal ) {158if ( flags.Scalar ) {159fprintf (outfile, "%d\n", mat->array.SZ[0][0]);160} else {161for ( i = 0 ; i < mat->rows; i++ ) {162fprintf (outfile, "%d ", mat->array.SZ[i][i]);163}164fprintf (outfile, "\n");165} /* !Scalar */166} else { /* !Diagonal */167max_lenZ = (int *)malloc((unsigned)mat->cols*sizeof(int));168for ( i = 0 ; i < mat->cols; i++ ) {169max_lenZ[i] = 0;170}171for ( i = 0; i < mat->rows; i++ ) {172for(j=0; j<(flags.Symmetric ? i+1 : mat->cols); j++) {173str_len=sprintf(string,"%d",mat->array.SZ[i][j]);174if ( str_len >= max_lenZ[j] ) {175max_lenZ[j] = str_len + 1;176}177}178}179for (i = 0; i < mat->rows; i++) {180for (j=0; j < (flags.Symmetric ? i+1 : mat->cols); j++)181fprintf(outfile,"%*d",max_lenZ[j],mat->array.SZ[i][j]);182fprintf (outfile, "\n");183}184free (max_lenZ);185}186} else { /* !Integral && !Normalized */187if ( flags.Diagonal ) {188if ( flags.Scalar ) {189d.z = mat->array.SZ[0][0];190if ( mat->array.N != NULL ) {191d.n = mat->array.N[0][0];192} else {193d.n = mat->kgv;194}195Normal (&d);196fprintf (outfile, "%d/%d\n", d.z, d.n);197} else {198for ( i = 0 ; i < mat->cols; i++ ) {199d.z = mat->array.SZ[i][i];200if ( mat->array.N != NULL ) {201d.n = mat->array.N[i][i];202} else {203d.n = mat->kgv;204}205Normal (&d);206if ( d.n == 1 ) {207fprintf (outfile, " %d", d.z);208} else {209fprintf (outfile, " %d/%d", d.z, d.n );210}211}212fprintf (outfile, "\n");213}214} else { /* !Diagonal */215max_lenQ=(rational *)malloc(mat->cols*sizeof(rational));216for (i = 0; i < mat->cols; i++ ) {217max_lenQ[i] = Zero;218}219for (i = 0; i < mat->rows; i++) {220for( j = 0; j < (flags.Symmetric ? i+1 : mat->cols); j++ ) {221d.z = mat->array.SZ[i][j];222if ( mat->array.N != NULL ) {223d.n = mat->array.N[i][j];224} else {225d.n = mat->kgv;226}227Normal (&d);228if((str_len=sprintf(string,"%d",d.z))>=max_lenQ[j].z) {229max_lenQ[j].z = str_len + 1;230}231if((str_len=sprintf(string,"%d",d.n))>max_lenQ[j].n ) {232max_lenQ[j].n = str_len;233}234}235}236for ( i = 0; i < mat->rows; i++) {237for(j=0;j<(flags.Symmetric ? i+1 : mat->cols); j++ ) {238d.z = mat->array.SZ[i][j];239if ( mat->array.N != NULL ) {240d.n = mat->array.N[i][j];241} else {242d.n = mat->kgv;243}244Normal (&d);245if ( d.n == 1 ) {246sprintf(format,"%%%dd",max_lenQ[j].z);247fprintf(outfile,"%*d",max_lenQ[j].z+max_lenQ[j].n+1,d.z);248} else {249sprintf(format,"%%%dd/%%-%dd",max_lenQ[j].z,max_lenQ[j].n);250fprintf(outfile,format,d.z,d.n);251}252}253fprintf (outfile, "\n");254}255free (max_lenQ);256}257}258}259/*}}} */260261262