Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

563640 views
1
#include "typedef.h"
2
#include "matrix.h"
3
#include "tools.h"
4
5
/**************************************************************************\
6
@---------------------------------------------------------------------------
7
@---------------------------------------------------------------------------
8
@ FILE: kgv2rat.
9
@---------------------------------------------------------------------------
10
@---------------------------------------------------------------------------
11
@
12
|
13
| kgv2rat.c
14
|
15
| exportiert die beiden Funktionen
16
| result = kgv2rat( matrix );
17
| und
18
| result = rat2kgv( matrix );
19
|
20
| Die beiden Funktionen wandeln die Darstellung einer rationalen Matrix
21
| mittels des Hauptnenners mat->kgv und die Darstellung mittels der
22
| Nennermatrix mat->array.N ineinander um.
23
| Das Ergebnis ist jeweils "maximal gekuerzt"
24
|
25
| Leider hat man in C keine Moeglichkeit, einen Integer-ueberlauf
26
| abzufangen, sonst koennte man in so einem Fall eine kgv-Matrix
27
| automatisch in eine Bruchmatrix umwandeln.
28
|
29
|
30
\**************************************************************************/
31
32
/**************************************************************************\
33
@---------------------------------------------------------------------------
34
@ result = kgv2rat( matrix_TYP *mat );
35
@
36
@ changes an integral matrix or a matrix with mat->kgv != 1
37
@ to an rational matrix with allocated mat->array.N
38
@ the integer 'result' is 0, if this worked,
39
@ -1, if no storage for the matrix could be alloceted
40
@ -2, if mat->prime != 0
41
| wandelt eine Integer - oder kgv-Matrix in eine rational Matrix um
42
| "result" ist 0, falls alles glattging und nimmt den Wert
43
| -1 an, falls kein Speicher zur
44
| Erzeugung der Matrix vorhanden war.
45
| -2, falls mat->prime != 0
46
|
47
|
48
@---------------------------------------------------------------------------
49
@
50
\**************************************************************************/
51
int
52
kgv2rat( mat )
53
matrix_TYP *mat;
54
{
55
int i, j, result;
56
57
if ( mat->prime ) /* this would be quite idiotic */
58
{
59
result = -2;
60
}
61
else if ( mat->array.N != NULL )
62
{
63
/*
64
* really nothing to do
65
*/
66
mat->flags.Integral = FALSE;
67
result = 0;
68
}
69
else
70
{
71
mat->array.N = (int **)malloc2dim( mat->rows, mat->cols, sizeof(int) );
72
if ( mat->array.N == NULL )
73
{
74
result = -1;
75
76
}
77
else
78
{
79
mat->flags.Integral = FALSE;
80
if ( mat->kgv == 0 )mat->kgv = 1;
81
if ( mat->kgv == 1 )
82
{
83
for ( i=0; i < mat->rows; i++ )
84
for ( j=0;j < mat->cols; j++ )
85
{
86
mat->array.N[i][j] = mat->kgv;
87
}
88
}
89
else
90
{
91
for ( i=0; i < mat->rows; i++ )
92
for ( j=0;j < mat->cols; j++ )
93
{
94
mat->array.N[i][j] = mat->kgv;
95
Normal2( &mat->array.SZ[i][j], &mat->array.N[i][j]);
96
}
97
}
98
result = 0;
99
}
100
}
101
return result;
102
}
103
104
/**************************************************************************\
105
@---------------------------------------------------------------------------
106
@ result = rat2kgv( matrix_TYP *mat );
107
@
108
| wandelt eine Matrix in Bruchdarstellung in eine
109
| Matrix mit kgv-Darstellung um.
110
| "result" ist 0, falls alles glattging und nimmt den Wert
111
| -2, falls mat->prime != 0,
112
@ changes a matrix with mat->array.N != NULL to a matrix
113
@ with mat->kgv != 0 and mat->array.N = NULL
114
@ The result is 0, if it worked and -2, if mat->prime != 0
115
@---------------------------------------------------------------------------
116
@
117
\**************************************************************************/
118
119
int
120
rat2kgv( mat )
121
matrix_TYP *mat;
122
{
123
int i, j, result;
124
125
if ( mat->prime ) /* this would be quite idiotic */
126
{
127
result = -2;
128
}
129
else if ( mat->array.N == NULL )
130
{
131
/*
132
* really nothing to do
133
*/
134
mat->flags.Integral = mat->kgv == 1;
135
result = 0;
136
}
137
else
138
{
139
/*
140
* kuerzen um Overflow Gefahr zu verringern
141
*/
142
for ( i=0;i < mat->rows; i++)
143
for ( j=0;j < mat->cols; j++)
144
{
145
Normal2( &mat->array.SZ[i][j], &mat->array.N[i][j] );
146
}
147
/*
148
* kgv aller Nenner berechnen.
149
*/
150
mat->kgv= 1;
151
for ( i=0;i < mat->rows; i++)
152
for ( j=0;j < mat->cols; j++)
153
{
154
mat->kgv = KGV( mat->kgv, mat->array.N[i][j] );
155
}
156
/*
157
* alle Brueche erweitern
158
*/
159
if ( mat->kgv > 1 )
160
{
161
for ( i=0;i < mat->rows; i++)
162
for ( j=0;j < mat->cols; j++)
163
{
164
mat->array.SZ[i][j] *= (mat->kgv/mat->array.N[i][j]);
165
}
166
}
167
free2dim ( (char **)mat->array.N, mat->rows );
168
mat->array.N = NULL;
169
result = 0;
170
}
171
return result;
172
}
173
174
/*}}} */
175
176