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"34/**************************************************************************\5@---------------------------------------------------------------------------6@---------------------------------------------------------------------------7@ FILE: kgv2rat.8@---------------------------------------------------------------------------9@---------------------------------------------------------------------------10@11|12| kgv2rat.c13|14| exportiert die beiden Funktionen15| result = kgv2rat( matrix );16| und17| result = rat2kgv( matrix );18|19| Die beiden Funktionen wandeln die Darstellung einer rationalen Matrix20| mittels des Hauptnenners mat->kgv und die Darstellung mittels der21| Nennermatrix mat->array.N ineinander um.22| Das Ergebnis ist jeweils "maximal gekuerzt"23|24| Leider hat man in C keine Moeglichkeit, einen Integer-ueberlauf25| abzufangen, sonst koennte man in so einem Fall eine kgv-Matrix26| automatisch in eine Bruchmatrix umwandeln.27|28|29\**************************************************************************/3031/**************************************************************************\32@---------------------------------------------------------------------------33@ result = kgv2rat( matrix_TYP *mat );34@35@ changes an integral matrix or a matrix with mat->kgv != 136@ to an rational matrix with allocated mat->array.N37@ the integer 'result' is 0, if this worked,38@ -1, if no storage for the matrix could be alloceted39@ -2, if mat->prime != 040| wandelt eine Integer - oder kgv-Matrix in eine rational Matrix um41| "result" ist 0, falls alles glattging und nimmt den Wert42| -1 an, falls kein Speicher zur43| Erzeugung der Matrix vorhanden war.44| -2, falls mat->prime != 045|46|47@---------------------------------------------------------------------------48@49\**************************************************************************/50int51kgv2rat( mat )52matrix_TYP *mat;53{54int i, j, result;5556if ( mat->prime ) /* this would be quite idiotic */57{58result = -2;59}60else if ( mat->array.N != NULL )61{62/*63* really nothing to do64*/65mat->flags.Integral = FALSE;66result = 0;67}68else69{70mat->array.N = (int **)malloc2dim( mat->rows, mat->cols, sizeof(int) );71if ( mat->array.N == NULL )72{73result = -1;7475}76else77{78mat->flags.Integral = FALSE;79if ( mat->kgv == 0 )mat->kgv = 1;80if ( mat->kgv == 1 )81{82for ( i=0; i < mat->rows; i++ )83for ( j=0;j < mat->cols; j++ )84{85mat->array.N[i][j] = mat->kgv;86}87}88else89{90for ( i=0; i < mat->rows; i++ )91for ( j=0;j < mat->cols; j++ )92{93mat->array.N[i][j] = mat->kgv;94Normal2( &mat->array.SZ[i][j], &mat->array.N[i][j]);95}96}97result = 0;98}99}100return result;101}102103/**************************************************************************\104@---------------------------------------------------------------------------105@ result = rat2kgv( matrix_TYP *mat );106@107| wandelt eine Matrix in Bruchdarstellung in eine108| Matrix mit kgv-Darstellung um.109| "result" ist 0, falls alles glattging und nimmt den Wert110| -2, falls mat->prime != 0,111@ changes a matrix with mat->array.N != NULL to a matrix112@ with mat->kgv != 0 and mat->array.N = NULL113@ The result is 0, if it worked and -2, if mat->prime != 0114@---------------------------------------------------------------------------115@116\**************************************************************************/117118int119rat2kgv( mat )120matrix_TYP *mat;121{122int i, j, result;123124if ( mat->prime ) /* this would be quite idiotic */125{126result = -2;127}128else if ( mat->array.N == NULL )129{130/*131* really nothing to do132*/133mat->flags.Integral = mat->kgv == 1;134result = 0;135}136else137{138/*139* kuerzen um Overflow Gefahr zu verringern140*/141for ( i=0;i < mat->rows; i++)142for ( j=0;j < mat->cols; j++)143{144Normal2( &mat->array.SZ[i][j], &mat->array.N[i][j] );145}146/*147* kgv aller Nenner berechnen.148*/149mat->kgv= 1;150for ( i=0;i < mat->rows; i++)151for ( j=0;j < mat->cols; j++)152{153mat->kgv = KGV( mat->kgv, mat->array.N[i][j] );154}155/*156* alle Brueche erweitern157*/158if ( mat->kgv > 1 )159{160for ( i=0;i < mat->rows; i++)161for ( j=0;j < mat->cols; j++)162{163mat->array.SZ[i][j] *= (mat->kgv/mat->array.N[i][j]);164}165}166free2dim ( (char **)mat->array.N, mat->rows );167mat->array.N = NULL;168result = 0;169}170return result;171}172173/*}}} */174175176