-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobust.c
1454 lines (1151 loc) · 39.8 KB
/
robust.c
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "robust.h"
#include "utils.h"
#include <math.h>
#include <stdio.h>
#include <float.h>
#include <math.h>
int MIN_EXP = DBL_MIN_EXP - 1; // -1022
int MAX_EXP = DBL_MAX_EXP - 1; // 1023
// Global threshold.
double g_omega = 1.e+300;
double g_omega_inv = 1.e-300;
#define NO_RESCALE 0
#define RESCALE 1
////////////////////////////////////////////////////////////////////////////////
// initialize scaling factors
////////////////////////////////////////////////////////////////////////////////
void init_scaling_factor(int n, scaling_t *restrict const alpha)
{
#ifdef INTSCALING
for (int i = 0; i < n; i++)
alpha[i] = 0;
#else
for (int i = 0; i < n; i++)
alpha[i] = 1.0;
#endif
}
////////////////////////////////////////////////////////////////////////////////
// manipulation of scaling factors
////////////////////////////////////////////////////////////////////////////////
void scale(
int n, double *restrict const x, const scaling_t *beta)
{
#ifdef INTSCALING
double alpha = ldexp(1.0, beta[0]);
#else
double alpha = beta[0];
#endif
// Scale vector, if necessary.
if (alpha != 1.0) {
for (int i = 0; i < n; i++) {
x[i] = alpha * x[i];
}
}
}
////////////////////////////////////////////////////////////////////////////////
// minimum scaling factor
////////////////////////////////////////////////////////////////////////////////
void reduce_scaling_factors(
int num_rhs, int num_tile_rows,
const scaling_t *restrict const scales, const int ldscales,
scaling_t *restrict const smin)
{
#define scales(col, blkrow) scales[(col) + (blkrow) * ldscales]
for (int k = 0; k < num_rhs; k++)
smin[k] = scales(k,0);
for (int tli = 1; tli < num_tile_rows; tli++) {
for (int k = 0; k < num_rhs; k++) {
#ifdef INTSCALING
if(smin[k] < scales(k, tli))
smin[k] = smin[k];
else
smin[k] = scales(k, tli);
#else
smin[k] = fmin(smin[k], scales(k, tli));
#endif
}
}
#undef scales
}
void update_global_scaling(scaling_t *global, scaling_t phi)
{
#ifdef INTSCALING
*global = phi + (*global);
#else
*global = phi * (*global);
#endif
}
double compute_upscaling(scaling_t alpha_min, scaling_t alpha)
{
double scaling;
#ifdef INTSCALING
// Common scaling is 2^alpha_min / 2^alpha.
scaling_t exp = alpha_min - alpha;
scaling = ldexp(1.0, exp);
#else
scaling = alpha_min / alpha;
#endif
return scaling;
}
double convert_scaling(scaling_t alpha)
{
#ifdef INTSCALING
double scaling = ldexp(1.0, alpha);
#else
double scaling = alpha;
#endif
return scaling;
}
////////////////////////////////////////////////////////////////////////////////
// protect real division
////////////////////////////////////////////////////////////////////////////////
/**
* @brief Compute scaling such that the division b / t cannot overflow
* where b, t are real-valued.
*
* If the return type is double-prevision, this routine returns a scaling alpha
* such that x = (alpha * b) / t cannot overflow.
*
* If the return type is int, this routine returns a scaling alpha such that
* x = (2^alpha * b) / t cannot overflow.
*
* Assume |b|, |t| are bounded by Omega.
*
* Credits: Carl Christian Kjelgaard Mikkelsen.
*/
static double protect_real_division(double b, double t)
{
// Initialize scaling factor.
double scale = 1.0;
// Find scaling alpha such that x = (alpha * b) / t cannot overflow.
if (fabs(t) < g_omega_inv) {
if (fabs(b) > fabs(t) * g_omega) {
// Please observe that scales will be strictly less than 1.
scale = (fabs(t) * g_omega) / fabs(b);
}
}
else { // fabs(t) >= g_omega_inv
// Exploit short circuiting, i.e., the left side is evaluated first.
// If 1.0 > abs(t) holds, then it is safe to compute
// fabs(t) * g_omega.
if (1.0 > fabs(t) && fabs(b) > fabs(t) * g_omega) {
scale = 1.0 / fabs(b);
}
}
return scale;
}
////////////////////////////////////////////////////////////////////////////////
// protect sum
////////////////////////////////////////////////////////////////////////////////
// Returns scaling such that sum := (alpha * x) + (alpha * y) cannot overflow.
static double protect_sum(double x, double y)
{
double scale = 1.0;
// Protect against overflow if x and y have the same sign.
if ((x > 0 && y > 0) || (x < 0 && y < 0))
if (fabs(x) > g_omega - fabs(y))
scale = 0.5;
return scale;
}
////////////////////////////////////////////////////////////////////////////////
// protect multiplication (internal)
////////////////////////////////////////////////////////////////////////////////
// Returns scaling alpha such that y := t * (alpha * x) cannot overflow.
static double protect_mul(double tnorm, double xnorm)
{
// Initialize scaling factor.
double scale = 1.0;
// Process simplified decision tree of protect_update().
if (fabs(xnorm) <= 1.0) {
if (fabs(tnorm) * fabs(xnorm) > g_omega) {
scale = 0.5;
}
}
else { // xnorm > 1.0
if (fabs(tnorm) > g_omega / fabs(xnorm)) {
scale = 0.5 / fabs(xnorm);
}
}
return scale;
}
////////////////////////////////////////////////////////////////////////////////
// protect update
////////////////////////////////////////////////////////////////////////////////
#ifdef INTSCALING
scaling_t /* == int*/ protect_update(double tnorm, double xnorm, double ynorm)
{
// Initialize scaling factor.
double scale = 1.0;
// Process decision tree.
if (xnorm <= 1.0) {
if (tnorm * xnorm > g_omega - ynorm) {
scale = 0.5;
}
}
else { // xnorm > 1.0
if (tnorm > (g_omega - ynorm) / xnorm) {
scale = 0.5 / xnorm;
}
}
return ilogb(scale);
}
#else
// Returns scaling alpha such that y := (alpha * y) - t * (alpha * x) cannot
// overflow.
scaling_t /* == double*/ protect_update(double tnorm, double xnorm, double ynorm)
{
// Initialize scaling factor.
double scale = 1.0;
// Process decision tree.
if (xnorm <= 1.0) {
if (tnorm * xnorm > g_omega - ynorm) {
scale = 0.5;
}
}
else { // xnorm > 1.0
if (tnorm > (g_omega - ynorm) / xnorm) {
scale = 0.5 / xnorm;
}
}
return scale;
}
#endif
// Returns scaling alpha such that
// y := (alpha * y) - t1 * (alpha * x1) - t2 * (alpha * x2)
// cannot overflow.
scaling_t protect_double_update(double t1, double x1, double t2, double x2,
double y)
{
// Initialize scaling factor.
scaling_t alpha1 = protect_update(t1, x1, y);
scaling_t alpha2 = protect_update(t2, x2, y);
#ifdef INTSCALING
if (alpha1 < alpha2)
return alpha1;
else
return alpha2;
#else
return fmin(alpha1, alpha2);
#endif
}
////////////////////////////////////////////////////////////////////////////////
// protect update scalar
////////////////////////////////////////////////////////////////////////////////
static double protect_update_scalar(double t, double x, double y)
{
double scale = 1.0;
// Protect p = x * y.
double alpha1 = protect_mul(x, t);
double p = t * (alpha1 * x);
if (abs(ilogb(y) - ilogb(p)) > 52) {
// The factors are far apart. Either y or p is the final result.
if (ilogb(p) > ilogb(y))
scale = alpha1;
}
else {
// Scale y consistently.
y = y / alpha1;
double alpha2 = protect_sum(y, -p);
scale = alpha1 * alpha2;
}
return scale;
}
////////////////////////////////////////////////////////////////////////////////
// protect multi-rhs update
////////////////////////////////////////////////////////////////////////////////
#ifdef INTSCALING
int protect_multi_rhs_update_real(
const double *restrict const Xnorms, int num_rhs,
const double tnorm,
const double *restrict const Ynorms,
scaling_t *restrict const scales) // == int
{
// Status flag to indicate if rescaling is necessary.
int status = NO_RESCALE;
for (int k = 0; k < num_rhs; k++) {
// Compute scaling factor for the k-th eigenvector.
scales[k] = protect_update(tnorm, Xnorms[k], Ynorms[k]);
if (scales[k] != 0)
status = RESCALE;
}
return status;
}
#else
int protect_multi_rhs_update_real(
const double *restrict const Xnorms, int num_rhs,
const double tnorm,
const double *restrict const Ynorms,
scaling_t *restrict const scales) // == double
{
// Status flag to indicate if rescaling is necessary.
int status = NO_RESCALE;
for (int k = 0; k < num_rhs; k++) {
// Compute scaling factor for the k-th eigenvector.
scales[k] = protect_update(tnorm, Xnorms[k], Ynorms[k]);
if (scales[k] != 1.0)
status = RESCALE;
}
return status;
}
#endif
#ifdef INTSCALING
int protect_multi_rhs_update_cmplx(
const double *restrict const Xnorms, int num_rhs,
const double tnorm,
const double *restrict const Ynorms,
scaling_t *restrict const scales) // == int
{
// Status flag to indicate if rescaling is necessary.
int status = NO_RESCALE;
for (int k = 0; k < num_rhs; k+=2) {
// Compute scaling factor for the k-th eigenvector.
scales[k] = protect_update(tnorm, Xnorms[k], Ynorms[k]);
scales[k+1] = scales[k];
if (scales[k] != 0)
status = RESCALE;
}
return status;
}
#else
int protect_multi_rhs_update_cmplx(
const double *restrict const Xnorms, int num_rhs,
const double tnorm,
const double *restrict const Ynorms,
scaling_t *restrict const scales) // == double
{
// Status flag to indicate if rescaling is necessary.
int status = NO_RESCALE;
for (int k = 0; k < num_rhs; k+=2) {
// Compute scaling factor for the k-th eigenvector.
scales[k] = protect_update(tnorm, Xnorms[k], Ynorms[k]);
scales[k+1] = scales[k];
if (scales[k] != 1.0)
status = RESCALE;
}
return status;
}
#endif
////////////////////////////////////////////////////////////////////////////////
// solve 1x1 real system
// [t - lambda] * [x] = scale * [x]
////////////////////////////////////////////////////////////////////////////////
#ifdef INTSCALING
int solve_1x1_real_system(double smin, double t, double lambda, double *x, scaling_t /* == int*/ *scale)
{
int info = 0;
// Compute csr := t + (-lambda) robustly. Note that the scaling contributes
// as reciprocal to the global scaling.
double s = protect_sum(t, -lambda);
double csr = (s * t) - (s * lambda);
if (fabs(csr) < smin) {
csr = smin;
info = 1;
}
// Compute a scaling to survive the real-valued division.
double alpha = protect_real_division(x[0], csr);
// Execute the division safely.
x[0] = (alpha * x[0]) / csr;
// Return scaling factor.
scale[0] = ilogb(alpha / s);
return info;
}
#else
/// Solves the real 1x1 system (t - lambda) x = b robustly.
/// x = x / (t - lambda)
int solve_1x1_real_system(double smin, double t, double lambda, double *x, scaling_t /* == double*/ *scale)
{
int info = 0;
// Compute csr := t + (-lambda) robustly. Note that the scaling contributes
// as reciprocal to the global scaling.
double s = protect_sum(t, -lambda);
double csr = (s * t) - (s * lambda);
if (fabs(csr) < smin) {
csr = smin;
info = 1;
}
// Compute a scaling to survive the real-valued division.
double alpha = protect_real_division(x[0], csr);
// Execute the division safely.
x[0] = (alpha * x[0]) / csr;
// Return scaling factor.
scale[0] = alpha / s;
return info;
}
#endif
////////////////////////////////////////////////////////////////////////////////
// complex division in real arithmetic
////////////////////////////////////////////////////////////////////////////////
static void dladiv2(double a, double b, double c, double d, double r, double t,
double *ret, double *scale)
{
volatile double res;
double alpha = 1.0;
if (r != 0.0) {
// Since r is in [0, 1], the multiplication is safe to execute.
volatile double br = b * r;
if (br != 0.0) {
// res = (a + br) * t
double s = protect_sum(a, br);
res = (s * a) + (s * br);
alpha = s * alpha;
// WARNING: If optimization flags activate associative math, the
// brackets in the computation of res is ignored. This problem has
// been observed with -Ofast (GCC) and -O3 (Intel). The computation
// overflows and produces NaNs in the solution.
// The crude fix is as follows:
// volatile double sres = s * res;
// res = sres * t;
s = protect_mul(fabs(t), fabs(res));
res = (s * res) * t;
alpha = s * alpha;
}
else {
// res = a * t + (b * t) * r
// Left term.
double s1 = protect_mul(fabs(t), fabs(a));
volatile double tmp1 = (s1 * a) * t;
// Right term.
double s2 = protect_mul(fabs(t), fabs(b));
volatile double tmp2 = (s2 * b) * t;
// The multiplication with r is safe.
tmp2 = tmp2 * r;
// Scale summands consistently.
double smin = fmin(s1, s2);
tmp1 = tmp1 * (s1 / smin);
tmp2 = tmp2 * (s2 / smin);
alpha = smin * alpha;
// Add both terms.
double s = protect_sum(tmp1, tmp2);
res = (s * tmp1) + (s * tmp2);
alpha = s * alpha;
}
}
else {
// res = (a + d * (b / c)) * t
// tmp = b / c
double s1 = protect_real_division(b, c);
alpha = s1 * alpha;
volatile double tmp = (s1 * b) / c;
// tmp = d * tmp
double s2 = protect_mul(fabs(d), fabs(tmp));
alpha = s2 * alpha;
tmp = d * (s2 * tmp);
// Apply scaling to left term 'a' in the sum so that both summands
// are consistently scaled.
a = (s1 * s2) * a;
// tmp = a + tmp
double s = protect_sum(a, tmp);
alpha = s * alpha;
tmp = (s * a) + (s * tmp);
// res = tmp * t
s = protect_mul(fabs(tmp), fabs(t));
alpha = s * alpha;
res = (s * tmp) * t;
}
// Return augmented vector (alpha, res).
*scale = alpha;
*ret = res;
}
static void dladiv1(double a, double b, double c, double d,
double *p, double *q, double *scale)
{
// a + ib
// p + i q = -------
// c + id
// Since |d| < |c|, this division is safe to execute.
volatile double r = d / c;
// t = 1 / (c + d * r)
// Since r is in [0, 1], the multiply is safe.
volatile double dr = d * r;
double s1 = protect_sum(c, dr);
volatile double sum = (s1 * c) + (s1 * dr);
double s2 = protect_real_division(1.0, sum);
volatile double t = 1.0 / (s2 * sum);
volatile double alpha = 1.0 / (s1 * s2);
// Introduce local scaling factors for dladiv2.
double beta1 = 1.0, beta2 = 1.0;
// Compute (beta1, p).
dladiv2(a, b, c, d, r, t, p, &beta1);
// Compute (beta2, q).
dladiv2(b, -a, c, d, r, t, q, &beta2);
// Scale real and imaginary part consistently.
double beta = 1.0;
if ((beta1 > 1.0 && beta2 < 1.0) || (beta1 < 1.0 && beta2 > 1.0)) {
printf("ERROR: The scalings cannot be consolidated without overflow or underflow.\n");
// A complex eigenvector has a real part that under/overflowed and an
// imaginary part that over/underflowed. LAPACK cannot capture this
// case either (they, too, have only one scaling factor per eigenvector)
}
else {
// Find the more extreme scaling factor.
beta = fmin(beta1, beta2);
// Apply scaling.
*p = (*p) * (beta / beta1);
*q = (*q) * (beta / beta2);
}
// Record global scaling factor.
*scale = alpha * beta;
}
static void dladiv(double a, double b, double c, double d,
double *x_re, double *x_im, double *scale)
{
// a + ib
// x_re + i x_im = -------
// c + id
if (fabs(d) < fabs(c)) {
dladiv1(a, b, c, d, x_re, x_im, scale);
}
else {
dladiv1(b, a, d, c, x_re, x_im, scale);
*x_im = -(*x_im);
}
}
int robust_cmplx_div(double smin, double a, double b, double c, double d,
double *restrict x_re, double *restrict x_im, scaling_t *scale)
{
int info = 0;
if (fabs(c) + fabs(d) < smin) {
c = smin;
d = 0.0;
info = 1;
}
// Compute the complex division in real arithmetic.
// a + ib
// x_re + i x_im = -------
// c + id
#ifdef INTSCALING
// Local scaling factor generated in the process of the complex division.
double alpha = 1.0;
dladiv(a, b, c, d, x_re, x_im, &alpha);
*scale = ilogb(alpha);
#else
dladiv(a, b, c, d, x_re, x_im, scale);
#endif
return info;
}
#ifdef INTSCALING
int solve_1x1_cmplx_system(double smin, double t, double lambda_re, double lambda_im,
double* x_re, double *x_im, scaling_t /* == int*/ *scale)
{
int info = 0;
// Solve (t - (lambda_re + i * lambda_im)) (p + i * q) = x_re + i * x_im.
// Compute csr := (t + (-lambda_re)) robustly.
double s = protect_sum(t, -lambda_re);
double csr = (s * t) - (s * lambda_re);
// Scale consistently csi := s * (-lambda_im).
double csi = s * (-lambda_im);
// Note that the scaling is applied to the rhs (x_re + i * x_im) after
// the complex division.
if (fabs(csr) + fabs(csi) < smin) {
csr = smin;
csi = 0.0;
info = 1;
}
// The scaling check for X = B / C in LAPACK is covered in protect_division.
// Local scaling factor generated in the process of the complex division.
double alpha = 1.0;
// Compute the complex division in real arithmetic.
// a + ib
// x_re + i x_im = -------
// c + id
double a = *x_re;
double b = *x_im;
double c = csr;
double d = csi;
dladiv(a, b, c, d, x_re, x_im, &alpha);
// Combine scaling factors and convert to int scaling factor.
*scale = ilogb((1.0 / s) * (alpha));
return info;
}
#else
int solve_1x1_cmplx_system(double smin, double t, double lambda_re, double lambda_im,
double* x_re, double *x_im, scaling_t /* == double*/ *scale)
{
int info = 0;
// Solve (t - (lambda_re + i * lambda_im)) (p + i * q) = x_re + i * x_im.
// Compute csr := (t + (-lambda_re)) robustly.
double s = protect_sum(t, -lambda_re);
double csr = (s * t) - (s * lambda_re);
// Scale consistently csi := s * (-lambda_im).
double csi = s * (-lambda_im);
// Note that the scaling is applied to the rhs (x_re + i * x_im) after
// the complex division.
if (fabs(csr) + fabs(csi) < smin) {
csr = smin;
csi = 0.0;
info = 1;
}
// The scaling check for X = B / C in LAPACK is covered in protect_division.
// Local scaling factor generated in the process of the complex division.
double alpha = 1.0;
// Compute the complex division in real arithmetic.
// a + ib
// x_re + i x_im = -------
// c + id
double a = *x_re;
double b = *x_im;
double c = csr;
double d = csi;
dladiv(a, b, c, d, x_re, x_im, &alpha);
// Combine scaling factors.
*scale = (1.0 / s) * (alpha);
return info;
}
#endif
////////////////////////////////////////////////////////////////////////////////
// solve 2x2 real system
////////////////////////////////////////////////////////////////////////////////
// Credits: Carl Christian Kjelgaard Mikkelsen
static double backsolve_real_2x2_system(double *T, int ldT, double *b)
{
#define T(i,j) T[(i) + (j) * ldT]
// Global scaling factor.
double alpha = 1.0;
double xnorm = fmax(fabs(b[0]), fabs(b[1]));
double s = protect_real_division(b[1], T(1,1));
if (s != 1.0) {
// Apply scaling to right-hand side.
b[0] = s * b[0];
b[1] = s * b[1];
// Update global scaling.
alpha = s * alpha;
// Update the infinity norm of the solution.
xnorm = s * xnorm;
}
// Execute the division.
b[1] = b[1] / T(1,1);
#ifdef INTSCALING
s = ldexp(1.0, protect_update(fabs(T(0,1)), fabs(b[1]), xnorm));
#else
s = protect_update(fabs(T(0,1)), fabs(b[1]), xnorm);
#endif
if (s != 1.0) {
// Apply scaling to right-hand side.
b[0] = s * b[0];
b[1] = s * b[1];
// Update global scaling.
alpha = s * alpha;
}
// Execute the linear update.
b[0] = b[0] - b[1] * T(0,1);
// Recompute norm.
xnorm = fmax(fabs(b[0]), fabs(b[1]));
s = protect_real_division(b[0], T(0,0));
if (s != 1.0) {
// Apply scaling to right-hand side.
b[0] = s * b[0];
b[1] = s * b[1];
// Update global scaling.
alpha = s * alpha;
// Update the infinity norm of the solution.
xnorm = s * xnorm;
}
// Execute the division.
b[0] = b[0] / T(0,0);
return alpha;
#undef T
}
// Swap row 0 and row 1.
static void swap_rows(int n, double *C)
{
#define C(i,j) C[(i) + (j) * 2]
// Swap row 0 and row 1.
for (int j = 0; j < n; j++) {
double swap = C(0,j);
C(0,j) = C(1,j);
C(1,j) = swap;
}
#undef C
}
static void find_real_pivot(double *C, int *pivot_row, int *pivot_col)
{
#define C(i,j) C[(i) + (j) * 2]
// Find the coordinates of the pivot element.
int row = 0;
int col = 0;
double cmax = 0.0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
double lmax = fabs(C(i,j));
if (lmax > cmax) {
row = i;
col = j;
cmax = lmax;
}
}
}
*pivot_row = row;
*pivot_col = col;
#undef C
}
// Complete pivoting.
static int solve_2x2_real_system_internal(
double smin, const double *restrict const T, int ldT,
double lambda,
double *restrict const b, double *restrict const scale)
{
#define T(i,j) T[(i) + (j) * (size_t)ldT]
#define C(i,j) C[(i) + (j) * 2]
// Solve
// (T - lambda I)x = b.
int info = 0;
// C = [(T - lambda * I) | b]
double C[2 * 3];
// Compute t + (-lambda) robustly. Recall the diagonals in the the 2-by-2
// T block are equal, so that we have to protect only one subtraction.
double s = protect_sum(T(0,0), -lambda);
double csr = (s * T(0,0)) - (s * lambda);
// Apply scaling to T. Note that scaling of b is not safe. Therefore s is
// incorporated into the global scaling at the very end of this routine.
// C := [s * (T - lambda I) | b].
C(0,0) = csr; C(0,1) = s * T(0,1); C(0,2) = b[0];
C(1,0) = s * T(1,0); C(1,1) = csr; C(1,2) = b[1];
////////////////////////////////////////////////////////////////////////////
// Transform A to echelon form with complete pivoting.
////////////////////////////////////////////////////////////////////////////
// Find pivot element in entire matrix.
int pivot_row = 0, pivot_col = 0;
find_real_pivot(C, &pivot_row, &pivot_col);
// Permute pivot to the top-left corner.
if (pivot_row == 1) {
// Swap row 0 and row 1.
swap_rows(3, C);
}
if (pivot_col == 1) {
// Swap column 0 and column 1.
for (int i = 0; i < 2; i++) {
double swap = C(i,0);
C(i,0) = C(i,1);
C(i,1) = swap;
}
}
// If the largest entry is 0.0, perturb.
if (C(0,0) == 0.0) {
C(0,0) = smin;
info = 1;
}
// Compute multiplier, the reciprocal of the pivot.
double ur11r = 1.0 / C(0,0);
// Multiply first row with reciprocal of C(0,0).
{
C(0,0) = 1.0;
C(0,1) = C(0,1) * ur11r; // Safe multiplication.
// Treat rhs.
double beta = protect_mul(C(0,2), ur11r);
*scale = beta;
C(0,2) = C(0,2) * beta;
C(1,2) = C(1,2) * beta;
C(0,2) = C(0,2) * ur11r;
}
// Second row - CR(1,0) * first_row.
{
double beta = protect_update_scalar(C(1,0), C(0,1), C(1,1));
// Scale C and the rhs. Note that C is a local copy and does not
// alter the system matrix.
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
C(i,j) = C(i,j) * beta;
*scale = (*scale) * beta;
C(1,1) = C(1,1) - C(1,0) * C(0,1);
// Perturb C(1,1), if too small.
if (fabs(C(1,1)) < smin) {
C(1,1) = smin;
info = 1;
}
// Treat rhs.
beta = protect_update_scalar(C(1,0), C(0,2), C(1,2));
*scale = (*scale) * beta;
C(0,2) = C(0,2) * beta;
C(1,2) = C(1,2) * beta;
C(1,2) = C(1,2) - C(1,0) * C(0,2);
// (1,0) has been annihilated.
C(1,0) = 0.0;
}
// The system is now in upper triangular form.
////////////////////////////////////////////////////////////////////////////
// Backward substitution.
////////////////////////////////////////////////////////////////////////////
double alpha = backsolve_real_2x2_system(&C(0,0), 2, &C(0,2));
*scale = (*scale) * alpha;
// Copy the solution back.
if (pivot_col == 1) {
b[0] = C(1,2);
b[1] = C(0,2);
}
else {