1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
|
/*
* abelian_group.c
*
* This file contains the following functions which the kernel
* provides for the UI:
*
* void expand_abelian_group(AbelianGroup *g);
* void compress_abelian_group(AbelianGroup *g);
* void free_abelian_group(AbelianGroup *g);
*
* expand_abelian_group() expands an AbelianGroup into its most
* factored form, e.g. Z/2 + Z/2 + Z/4 + Z/3 + Z/9 + Z.
* Each nonzero torsion coefficient is a power of a prime.
* These are the "primary invariants" of the group
* (cf. Hartley & Hawkes' Rings, Modules and Linear Algebra,
* Chapman & Hall 1970, page 154).
*
* compress_abelian_group compresses an AbelianGroup into its least
* factored form, e.g. Z/2 + Z/6 + Z/36 + Z.
* Each torsion coefficient divides all subsequent torsion coefficients.
* These are the "torsion invariants" of the group
* (cf. Hartley & Hawkes' Rings, Modules and Linear Algebra,
* Chapman & Hall 1970, page 153).
*
* free_abelian_group() frees the memory used to hold the AbelianGroup *g.
* As explained in the documentation preceding the definition of an
* AbelianGroup in SnapPea.h, only the kernel may allocate or deallocate
* AbelianGroups.
*/
#include "kernel.h"
#include <stdlib.h> /* needed for qsort() */
typedef struct prime_power
{
int prime,
power;
struct prime_power *next;
} PrimePower;
static int CDECL compare_prime_powers(const void *pp0, const void *pp1);
void expand_abelian_group(
AbelianGroup *g)
{
PrimePower *prime_power_list,
*new_prime_power,
**array_of_pointers,
*this_prime_power;
int num_prime_powers,
torsion_free_rank;
long int m,
p,
this_coefficient;
int i,
count;
/*
* The documentation at the top of this file specifies the behavior
* of expand_abelian_group().
*/
/*
* Ignore nonexistent groups.
*/
if (g == NULL)
return;
/*
* The algorithm is to factor each torsion coefficient into prime
* powers, combine the prime powers for all the torsion coefficients
* into a single list, sort the list, and write the results back
* to the torsion coefficients array (after allocating more memory
* for the array, of course).
*/
prime_power_list = NULL;
num_prime_powers = 0;
torsion_free_rank = 0;
for (i = 0; i < g->num_torsion_coefficients; i++)
{
/*
* For notational convenience, let m be the torsion coefficient
* under consideration.
*/
m = g->torsion_coefficients[i];
/*
* If m is zero (indicating an infinite cyclic factor), make a
* note of it and move on to the next torsion coefficient.
*/
if (m == 0L)
{
torsion_free_rank++;
continue;
}
/*
* Factor m.
* (Much more efficient algorithms could be used to factor m, but at the
* moment I'm assuming the numbers involved will be small, so the
* simplicity of the code is more important than its efficiency.)
*/
/*
* Consider each potential prime divisor p of m.
* (We "accidently" consider composite divisors as well,
* but the wasted effort shouldn't be too significant.)
*/
for (p = 2; m > 1L; p++)
{
/*
* Does p divide m?
* If so, then p must be prime, since otherwise some previous value
* of p would have divided m, and would have been factored out.
* Find the largest power of p which divides m, and record it on
* the prime_power_list.
*/
if (m%p == 0L)
{
new_prime_power = NEW_STRUCT(PrimePower);
new_prime_power->prime = p;
new_prime_power->power = 0;
new_prime_power->next = prime_power_list;
prime_power_list = new_prime_power;
num_prime_powers++;
while (m%p == 0L)
{
m /= p;
new_prime_power->power++;
}
}
/*
* If m is less than p^2, then m must be prime or one.
*
* if m is prime, we set p = m - 1, so that on the next
* pass through the loop (after the "p++"), p will
* equal m, and we'll finish up.
*
* If m is one, then the loop will terminate no matter
* what p is, so there's no harm in setting p = m - 1.
*/
if (m < p * p)
p = m - 1;
}
}
/*
* Set num_torsion_coefficients, and reallocate space
* for the (presumably larger) array.
*/
g->num_torsion_coefficients = num_prime_powers + torsion_free_rank;
if (g->torsion_coefficients != NULL)
my_free(g->torsion_coefficients);
if (g->num_torsion_coefficients > 0)
g->torsion_coefficients = NEW_ARRAY(g->num_torsion_coefficients, long int);
else
g->torsion_coefficients = NULL;
/*
* If the list of PrimePowers is nonempty, sort it and read it
* into the torsion_coefficients array.
*/
if (num_prime_powers > 0)
{
/*
* Create an array of pointers to the PrimePowers.
*/
array_of_pointers = NEW_ARRAY(num_prime_powers, PrimePower *);
for ( i = 0, this_prime_power = prime_power_list;
i < num_prime_powers;
i++, this_prime_power = this_prime_power->next)
array_of_pointers[i] = this_prime_power;
if (this_prime_power != NULL)
uFatalError("expand_abelian_group", "abelian_group");
qsort(array_of_pointers, num_prime_powers, sizeof(PrimePower *), compare_prime_powers);
for (i = 0; i < num_prime_powers; i++)
{
/*
* Multiply out the current torsion coefficient . . .
*/
this_coefficient = 1L;
for (count = 0; count < array_of_pointers[i]->power; count++)
this_coefficient *= array_of_pointers[i]->prime;
/*
* . . . write it into the array . . .
*/
g->torsion_coefficients[i] = this_coefficient;
/*
* . . . and free the PrimePower.
*/
my_free(array_of_pointers[i]);
}
my_free(array_of_pointers);
}
/*
* Write a torsion coefficient of zero for each infinite cyclic factor.
*/
for (i = num_prime_powers; i < g->num_torsion_coefficients; i++)
g->torsion_coefficients[i] = 0L;
}
static int CDECL compare_prime_powers(
const void *pp0,
const void *pp1)
{
if ((*(PrimePower **)pp0)->prime < (*(PrimePower **)pp1)->prime)
return -1;
if ((*(PrimePower **)pp0)->prime > (*(PrimePower **)pp1)->prime)
return +1;
if ((*(PrimePower **)pp0)->power < (*(PrimePower **)pp1)->power)
return -1;
if ((*(PrimePower **)pp0)->power > (*(PrimePower **)pp1)->power)
return +1;
return 0;
}
void compress_abelian_group(
AbelianGroup *g)
{
int i,
ii,
j;
long int m,
n,
d;
/*
* The documentation at the top of this file specifies the behavior
* of compress_abelian_group().
*/
/*
* Ignore nonexistent groups.
*/
if (g == NULL)
return;
/*
* Beginning at the start of the list, adjust each torsion coefficient
* so that it divides all subsequent torsion coefficients.
*/
for (i = 0; i < g->num_torsion_coefficients; i++)
for (j = i + 1; j < g->num_torsion_coefficients; j++)
{
/*
* For clarity, let the torsion coefficients under
* consideration be called m and n.
*/
m = g->torsion_coefficients[i];
n = g->torsion_coefficients[j];
/*
* If both m and n are zero, go on to the next
* iteration of the loop.
*/
if (m == 0L && n == 0L)
continue;
/*
* Compute the greatest common divisor of m and n.
* Note that the greatest common divisor, which is
* defined iff m and n are not both zero, will always
* be nonzero.
*/
d = gcd(m, n);
/*
* Define m' = m/d. Replace m with d, and n with n * m'.
* To convince yourself that this is valid, consider
* the factorizations of m and n into powers of primes.
* In effect, we are swapping the higher power of a given
* prime from m to n, e.g. {m = 8, n = 4} -> {m = 4, n = 8}.
*/
n *= m / d;
m = d;
/*
* Write the values of m and n back into the torsion_coefficients
* array.
*/
g->torsion_coefficients[i] = m;
g->torsion_coefficients[j] = n;
}
/*
* Delete torsion coefficients of one, and adjust
* g->num_torsion_coefficients accordingly.
*/
/*
* First set ii to be the index of the first non-one, if any.
*/
for ( ii = 0;
ii < g->num_torsion_coefficients && g->torsion_coefficients[ii] == 1L;
ii++)
;
/*
* Now shift all the non-ones to the start of the array,
* overwriting the ones. (Note that this algorithm is valid
* even if the array contains all ones, or no ones.)
*/
for (i = 0; ii < g->num_torsion_coefficients; i++, ii++)
g->torsion_coefficients[i] = g->torsion_coefficients[ii];
/*
* Set g->num_torsion_coefficients to its correct value;
*/
g->num_torsion_coefficients = i;
}
void free_abelian_group(
AbelianGroup *g)
{
if (g != NULL)
{
if (g->torsion_coefficients != NULL)
my_free(g->torsion_coefficients);
my_free(g);
}
}
|