-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathklsupport.cpp
More file actions
574 lines (430 loc) · 12.7 KB
/
klsupport.cpp
File metadata and controls
574 lines (430 loc) · 12.7 KB
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
This is klsupport.cpp
Coxeter version 3.0 Copyright (C) 2002 Fokko du Cloux
See file main.cpp for full copyright notice
*/
#include "klsupport.h"
/*
This module contains code for the operations that can be "factored out"
amongst the various k-l tables : the ordinary one, the inverse one, and
the one with unequal parameters. Foremost, this is the extremal list.
In all these cases, k-l polynomials can be readily reduced to the case
of "extremal pairs".
*/
/*****************************************************************************
Chapter I -- The KLSupport class.
This class can be seen as an extension of the schubert context, oriented
towards k-l computations. Its main function is to construct and maintain
extrList. Recall that this is a list of rows of CoxNbr. The row for y is
never allocated if inverse[y] < y (this becomes a bit cumbersome, and
I'm really tempted to drop it! the main reason for keeping it is that
it really speeds up and reduces the computations by pushing y down faster,
by an important factor (at least two in my trials.))
If the row is allocated, then it always contains the full list of extremal
pairs for y, i.e. those x <= y for which LR(x) contains LR(y).
The other stuff is trivial : inverse is the (partially defined) table of
inverses, last is the last term in the internal normal form (also kept
because it provides a better descent strategy), involution flags the
involutions in the context.
The interface field is here because it was in KLContext, but it should
probably go away, and be present as a parameter in the functions that
need it.
The idea in this program is that schubert is really an auxiliary to
klsupport; the updating of schubert should go only through klsupport,
and the two will always be kept consistent.
The following functions are provided :
- constructors and destructors :
- KLSupport(SchubertContext*, const Interface*);
- ~KLSupport();
- accessors :
- inverseMin(const CoxNbr& x) : returns min(x,x_inverse);
- standardPath(List<Generator>& g, const CoxNbr& x) : returns in g the
standard descent path from x
- manipulators :
- applyInverse(const CoxNbr& y) : writes the row from inverse in y;
- applyIPermutation(const CoxNbr& y, const Permutation& a) : applies
the (inverse) permutation to extrList(y); (inlined)
- extendContext(const CoxWord& g) : extends the context to hold g;
- permute(const Permutation& a) : applies the permutation to the data;
- revertSize(const Ulong& n) : reverts to previous size after a
failed extension;
*****************************************************************************/
namespace klsupport {
KLSupport::KLSupport(SchubertContext* p)
:d_schubert(p), d_extrList(1), d_inverse(1), d_last(1), d_involution(1)
{
/* make first row of d_extrList, for the identity element */
d_extrList[0] = new ExtrRow(1);
d_extrList.setSizeValue(1);
ExtrRow& e = *d_extrList[0];
e.setSizeValue(1);
d_inverse.setSizeValue(1);
d_last.setSizeValue(1);
d_last[0] = undef_generator;
d_involution.setBit(0);
}
KLSupport::~KLSupport()
/*
Delete the pointers that were allocated by klsupport.
*/
{
for (Ulong j = 0; j < d_extrList.size(); ++j) {
delete d_extrList[j];
}
delete d_schubert;
return;
}
/******** accessors *********************************************************/
CoxNbr KLSupport::inverseMin(const CoxNbr& x) const
/*
Returns the minimum of x and x_inverse.
*/
{
if (x <= inverse(x))
return x;
else
return inverse(x);
}
void KLSupport::standardPath(List<Generator>& g, const CoxNbr& x) const
{
const SchubertContext& p = schubert();
/* find sequence of shifts */
Length j = p.length(x);
g.setSize(j);
CoxNbr x1 = x;
while (j) {
--j;
if (inverse(x1) < x1) { /* left shift */
Generator s = last(inverse(x1));
g[j] = s + rank();
x1 = p.lshift(x1,s);
}
else {
Generator s = last(x1);
g[j] = last(x1);
x1 = p.rshift(x1,s);
}
}
return;
}
/******** manipulators ******************************************************/
void KLSupport::allocExtrRow(const CoxNbr& y)
/*
Allocates one row in d_extrList. The row contains the list of elements
x <= y s.t. LR(y) is contained in LR(x), i.e., which cannot be taken
further up by the application of a generator in LR(y).
Forwards the error MEMORY_WARNING if CATCH_MEMORY_ERROR is set.
*/
{
const SchubertContext& p = schubert();
BitMap b(size());
p.extractClosure(b,y);
if (ERRNO)
return;
maximize(p,b,p.descent(y));
d_extrList[y] = new ExtrRow(b.begin(),b.end());
/* an error may be set here */
return;
}
void KLSupport::allocRowComputation(const CoxNbr& y)
/*
This function makes sure that all the extremal rows in the standard
descent path from y are allocated. The idea is that all these rows
will come up when the full row for y is computed, so one might as
well fill them anyway; doing them all at the same time will save
many Bruhat closure computations, which are relatively expensive.
Still, this function looks like overkill to me. I'm leaving it in
because it is working and it was a pain to write!
Things wouldn't be so bad if there wasn't also the passage to inverses!
*/
{
static List<Generator> e(0);
const SchubertContext& p = schubert();
/* find sequence of shifts */
standardPath(e,y);
SubSet q(size());
q.reset();
q.add(0);
if (ERRNO)
goto abort;
{
CoxNbr y1 = 0;
for (Ulong j = 0; j < e.size(); ++j) {
Generator s = e[j];
p.extendSubSet(q,s); /* extend the subset */
if (ERRNO)
goto abort;
y1 = p.shift(y1,s);
CoxNbr y2 = inverseMin(y1);
if (!isExtrAllocated(y2)) { /* allocate row */
/* copy bitmap of subset to buffer */
BitMap b = q.bitMap();
if (ERRNO)
goto abort;
/* extremalize */
maximize(p,b,p.descent(y1));
d_extrList[y1] = new ExtrRow(b.begin(),b.end());
/* go over to inverses if necessary */
if (s >= rank()) {
applyInverse(y2);
d_extrList[y2]->sort();
}
}
}
}
return;
abort:
Error(ERRNO);
ERRNO = ERROR_WARNING;
return;
}
void KLSupport::applyInverse(const CoxNbr& y)
/*
This function puts in d_extrList[y] the row of inverses of d_extrList[yi],
where yi is the inverse of y, and sets the row of yi to zero. The row is
not sorted (this can be done with sortIRow).
*/
{
CoxNbr yi = inverse(y);
d_extrList[y] = d_extrList[yi];
d_extrList[yi] = 0;
ExtrRow& e = *d_extrList[y];
for (Ulong j = 0; j < e.size(); ++j) {
e[j] = inverse(e[j]);
}
return;
}
CoxNbr KLSupport::extendContext(const CoxWord& g)
/*
Extends the context to accomodate g.
The return value is the context number of g in case of success, and
undef_coxnbr in case of failure.
Forwards the error EXTENSION_FAIL in case of error.
*/
{
CoxNbr prev_size = size();
SchubertContext& p = *d_schubert;
CoxNbr x = p.extendContext(g);
if (ERRNO) /* ERRNO is EXTENSION_FAIL */
return undef_coxnbr;
CATCH_MEMORY_OVERFLOW = true;
d_extrList.setSize(size());
if (ERRNO)
goto revert;
d_inverse.setSize(size());
if (ERRNO)
goto revert;
d_last.setSize(size());
if (ERRNO)
goto revert;
d_involution.setSize(size());
if (ERRNO)
goto revert;
CATCH_MEMORY_OVERFLOW = false;
/* extend the list of inverses */
for (CoxNbr x = 0; x < prev_size; ++x) {
if (inverse(x) == undef_coxnbr) { /* try to extend */
Generator s = p.firstRDescent(x);
CoxNbr xs = p.rshift(x,s);
if (inverse(xs) == undef_coxnbr)
d_inverse[x] = undef_coxnbr;
else
d_inverse[x] = p.lshift(inverse(xs),s);
}
}
for (CoxNbr x = prev_size; x < size(); ++x) {
Generator s = p.firstRDescent(x);
CoxNbr xs = p.rshift(x,s);
if (inverse(xs) == undef_coxnbr)
d_inverse[x] = undef_coxnbr;
else
d_inverse[x] = p.lshift(inverse(xs),s);
}
for (CoxNbr x = prev_size; x < size(); ++x) {
if (inverse(x) == x)
d_involution.setBit(x);
}
/* extend list of last elements */
for (CoxNbr x = prev_size; x < size(); ++x) {
Generator s = p.firstLDescent(x);
CoxNbr sx = p.lshift(x,s);
if (sx)
d_last[x] = d_last[sx];
else /* x = s */
d_last[x] = s;
}
return x;
revert:
CATCH_MEMORY_OVERFLOW = false;
revertSize(prev_size);
return undef_coxnbr;
}
void KLSupport::permute(const Permutation& a)
/*
Applies the permutation a to the data in the context. The meaning of a
is that it takes element number x in the context to element number a(x).
The procedure is explained in full in kl.h.
*/
{
/* permute schubert context */
d_schubert->permute(a);
/* permute values */
for (CoxNbr y = 0; y < size(); ++y) {
if (d_extrList[y] == 0)
continue;
ExtrRow& e = *d_extrList[y];
for (Ulong j = 0; j < e.size(); ++j) {
e[j] = a[e[j]];
}
}
for (CoxNbr y = 0; y < size(); ++y) {
if (inverse(y) != undef_coxnbr)
d_inverse[y] = a[inverse(y)];
}
/* permute ranges */
BitMap b(a.size());
for (CoxNbr x = 0; x < size(); ++x) {
if (b.getBit(x))
continue;
if (a[x] == x) {
b.setBit(x);
continue;
}
for (CoxNbr y = a[x]; y != x; y = a[y]) {
/* back up values for y */
ExtrRow* extr_buf = d_extrList[y];
CoxNbr inverse_buf = inverse(y);
Generator last_buf = last(y);
bool involution_buf = isInvolution(y);
/* put values for x in y */
d_extrList[y] = d_extrList[x];
d_inverse[y] = inverse(x);
d_last[y] = last(x);
if (isInvolution(x))
d_involution.setBit(y);
else
d_involution.clearBit(y);
/* store backup values in x */
d_extrList[x] = extr_buf;
d_inverse[x] = inverse_buf;
d_last[x] = last_buf;
if (involution_buf)
d_involution.setBit(x);
else
d_involution.clearBit(x);
/* set bit*/
b.setBit(y);
}
b.setBit(x);
}
return;
}
void KLSupport::revertSize(const Ulong& n)
/*
This function reverts the size of the context to a previous value n. Note
that the allocated sizes of the lists are not changed; we simply preserve
the consistency of the various size values.
*/
{
d_schubert->revertSize(n);
d_extrList.setSize(n);
d_inverse.setSize(n);
d_last.setSize(n);
return;
}
};
/*****************************************************************************
Chapter II -- Utilities.
This section defines some utility functions declared in klsupport.h :
- safeAdd(const KLCoeff&, const KLCoeff&) : safe addition;
- safeAdd(SKLCoeff&, const SKLCoeff&) : safe addition;
*****************************************************************************/
namespace klsupport {
KLCoeff& safeAdd(KLCoeff& a, const KLCoeff& b)
/*
This function increments a with b, if the result does not exceed
KLCOEFF_MAX; otherwise it sets the error KLCOEFF_OVERFLOW and leaves
a unchanged.
*/
{
if (b <= KLCOEFF_MAX - a)
a += b;
else
ERRNO = KLCOEFF_OVERFLOW;
return a;
}
SKLCoeff& safeAdd(SKLCoeff& a, const SKLCoeff& b)
/*
This function increments a with b if the result lies in the interval
[SKLCOEFF_MIN,SKLCOEFF_MAX]; sets the error SKLCOEFF_OVERFLOW if we
exceed SKLCOEFF_MAX, and SKLCOEFF_UNDERFLOW if we are less than
SKLCOEFF_MIN.
Note that overflow can occur only if b is positive, underflow only
if b is negative.
*/
{
if ((b > 0) && (a > SKLCOEFF_MAX - b)) {
ERRNO = SKLCOEFF_OVERFLOW;
}
else if ((b < 0) && (a < SKLCOEFF_MIN - b)) {
ERRNO = SKLCOEFF_UNDERFLOW;
}
else
a += b;
return a;
}
KLCoeff& safeMultiply(KLCoeff& a, const KLCoeff& b)
/*
This function multiplies a with b, if the result does not exceed
KLCOEFF_MAX; otherwise it sets the error KLCOEFF_OVERFLOW and leaves
a unchanged.
*/
{
if (a == 0)
return a;
if (b <= KLCOEFF_MAX/a)
a *= b;
else
ERRNO = KLCOEFF_OVERFLOW;
return a;
}
SKLCoeff& safeMultiply(SKLCoeff& a, const SKLCoeff& b)
/*
This function multiplies a with b, if the result lies between SKLCOEFF_MIN
and SKLCOEFF_MAX. Otherwise it sets the error SKLCOEFF_UNDERFLOW or
SKLCOEFF_OVERFLOW as appropriate.
*/
{
if (a == 0)
return a;
if (a > 0) {
if (b > SKLCOEFF_MAX/a)
ERRNO = SKLCOEFF_OVERFLOW;
else if (b < SKLCOEFF_MIN/a)
ERRNO = SKLCOEFF_UNDERFLOW;
else
a *= b;
}
else {
if (b > SKLCOEFF_MIN/a)
ERRNO = SKLCOEFF_UNDERFLOW;
else if (b < SKLCOEFF_MAX/a)
ERRNO = SKLCOEFF_OVERFLOW;
else
a *= b;
}
return a;
}
KLCoeff& safeSubtract(KLCoeff& a, const KLCoeff& b)
/*
This function subtracts b from a, if the result is non-negative; sets
the error KLCOEFF_UNDERFLOW otherwise, and leaves a unchanged.
*/
{
if (b <= a)
a -= b;
else
ERRNO = KLCOEFF_UNDERFLOW;
return a;
}
};