Debian Codegraph @

common/core_linalg2.cc

raw
1 /*****************************************************************************
2 * Free42 -- an HP-42S calculator simulator
3 * Copyright (C) 2004-2026 Thomas Okken
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/licenses/.
16 *****************************************************************************/
17
18 #include <stdlib.h>
19
20 #include "core_linalg2.h"
21 #include "core_globals.h"
22 #include "core_main.h"
23
24
25 #define STATE(s) \
26 if (--count <= 0) { \
27 dat->state = s; \
28 goto suspend; \
29 } \
30 state##s: \
31 ;
32
33
34 /****************************/
35 /***** LU decomposition *****/
36 /****************************/
37
38 struct lu_r_data_struct {
39 vartype_realmatrix *a;
40 int4 *perm;
41 phloat det;
42 int4 i, imax, j, k;
43 phloat max, tmp, sum, *scale;
44 int state;
45 int (*completion)(int, vartype_realmatrix *, int4 *, phloat);
46 };
47
48 lu_r_data_struct *lu_r_data;
49
50 static int lu_decomp_r_worker(bool interrupted);
51
52 int lu_decomp_r(vartype_realmatrix *a, int4 *perm,
53 int (*completion)(int, vartype_realmatrix *, int4 *, phloat)) {
54 lu_r_data_struct *dat =
55 (lu_r_data_struct *) malloc(sizeof(lu_r_data_struct));
56
57 if (dat == NULL)
58 return completion(ERR_INSUFFICIENT_MEMORY, a, perm, 0);
59
60 dat->scale = (phloat *) malloc(a->rows * sizeof(phloat));
61 if (dat->scale == NULL) {
62 free(dat);
63 return completion(ERR_INSUFFICIENT_MEMORY, a, perm, 0);
64 }
65
66 dat->a = a;
67 dat->perm = perm;
68 dat->completion = completion;
69
70 dat->state = 0;
71
72 lu_r_data = dat;
73 mode_interruptible = lu_decomp_r_worker;
74 mode_stoppable = false;
75 return ERR_INTERRUPTIBLE;
76 }
77
78 static int lu_decomp_r_worker(bool interrupted) {
79
80 lu_r_data_struct *dat = lu_r_data;
81
82 phloat *a = dat->a->array->data;
83 int4 n = dat->a->rows;
84 phloat *scale = dat->scale;
85 int4 *perm = dat->perm;
86 int count = 1000;
87 int err;
88
89 int4 i = dat->i;
90 int4 imax = dat->imax;
91 int4 j = dat->j;
92 int4 k = dat->k;
93 phloat max = dat->max;
94 phloat tmp = dat->tmp;
95 phloat sum = dat->sum;
96
97 if (interrupted) {
98 free(scale);
99 err = dat->completion(ERR_INTERRUPTED, dat->a, perm, 0);
100 free(dat);
101 return err;
102 }
103
104 switch (dat->state) {
105 case 0: break;
106 case 1: goto state1;
107 case 2: goto state2;
108 case 3: goto state3;
109 case 4: goto state4;
110 case 5: goto state5;
111 }
112
113 dat->det = 1;
114
115 for (i = 0; i < n; i++) {
116 max = 0;
117 for (j = 0; j < n; j++) {
118 tmp = a[i * n + j];
119 if (tmp < 0)
120 tmp = -tmp;
121 if (tmp > max)
122 max = tmp;
123 STATE(1);
124 }
125 scale[i] = max;
126 }
127
128 for (j = 0; j < n; j++) {
129 for (i = 0; i < j; i++) {
130 sum = a[i * n + j];
131 for (k = 0; k < i; k++) {
132 sum -= a[i * n + k] * a[k * n + j];
133 STATE(2);
134 }
135 a[i * n + j] = sum;
136 }
137
138 max = 0;
139 imax = j;
140 for (i = j; i < n; i++) {
141 sum = a[i * n + j];
142 for (k = 0; k < j; k++) {
143 sum -= a[i * n + k] * a[k * n + j];
144 STATE(3);
145 }
146 a[i * n + j] = sum;
147 if (scale[i] == 0) {
148 imax = i;
149 break;
150 }
151 tmp = (sum < 0 ? -sum : sum) / scale[i];
152 if (tmp > max) {
153 imax = i;
154 max = tmp;
155 }
156 }
157
158 if (j != imax) {
159 for (k = 0; k < n; k++) {
160 tmp = a[imax * n + k];
161 a[imax * n + k] = a[j * n + k];
162 a[j * n + k] = tmp;
163 STATE(4);
164 }
165 dat->det = -dat->det;
166 scale[imax] = scale[j];
167 }
168
169 perm[j] = imax;
170 if (a[j * n + j] == 0) {
171 if (core_settings.matrix_singularmatrix) {
172 free(scale);
173 err = dat->completion(ERR_SINGULAR_MATRIX, dat->a, perm, 0);
174 free(dat);
175 return err;
176 } else {
177 /* For a zero pivot, substitute a small positive number.
178 * I use a number that's about 10^-20 times the size of
179 * the maximum of the original column, with a minimum of
180 * 10^20 / POS_HUGE_PHLOAT.
181 */
182 phloat tiniest = 1e20 / POS_HUGE_PHLOAT;
183 phloat tiny;
184 if (scale[j] == 0)
185 tiny = tiniest;
186 else {
187 tiny = pow(10, floor(log10(scale[j])) - 20);
188 if (tiny < tiniest)
189 tiny = tiniest;
190 }
191 a[j * n + j] = tiny;
192 }
193 }
194 dat->det *= a[j * n + j];
195 if (j != n - 1) {
196 tmp = 1 / a[j * n + j];
197 for (i = j + 1; i < n; i++) {
198 a[i * n + j] *= tmp;
199 STATE(5);
200 }
201 }
202 }
203
204 free(scale);
205 err = dat->completion(ERR_NONE, dat->a, perm, dat->det);
206 free(dat);
207 return err;
208
209 suspend:
210 dat->i = i;
211 dat->imax = imax;
212 dat->j = j;
213 dat->k = k;
214 dat->max = max;
215 dat->tmp = tmp;
216 dat->sum = sum;
217 return ERR_INTERRUPTIBLE;
218 }
219
220
221 struct lu_c_data_struct {
222 vartype_complexmatrix *a;
223 int4 *perm;
224 phloat det_re, det_im;
225 int4 i, imax, j, k;
226 phloat max, tmp, tmp_re, tmp_im, sum_re, sum_im, s_re, s_im, *scale;
227 int state;
228 int (*completion)(int, vartype_complexmatrix *, int4 *, phloat, phloat);
229 };
230
231 lu_c_data_struct *lu_c_data;
232
233 static int lu_decomp_c_worker(bool interrupted);
234
235 int lu_decomp_c(vartype_complexmatrix *a, int4 *perm,
236 int (*completion)(int, vartype_complexmatrix *,
237 int4 *, phloat, phloat)) {
238 lu_c_data_struct *dat =
239 (lu_c_data_struct *) malloc(sizeof(lu_c_data_struct));
240
241 if (dat == NULL)
242 return completion(ERR_INSUFFICIENT_MEMORY, a, perm, 0, 0);
243
244 dat->scale = (phloat *) malloc(a->rows * sizeof(phloat));
245 if (dat->scale == NULL) {
246 free(dat);
247 return completion(ERR_INSUFFICIENT_MEMORY, a, perm, 0, 0);
248 }
249
250 dat->a = a;
251 dat->perm = perm;
252 dat->completion = completion;
253
254 dat->state = 0;
255
256 lu_c_data = dat;
257 mode_interruptible = lu_decomp_c_worker;
258 mode_stoppable = false;
259 return ERR_INTERRUPTIBLE;
260 }
261
262 static int lu_decomp_c_worker(bool interrupted) {
263
264 lu_c_data_struct *dat = lu_c_data;
265
266 phloat *a = dat->a->array->data;
267 int4 n = dat->a->rows;
268 phloat *scale = dat->scale;
269 int4 *perm = dat->perm;
270 int count = 1000;
271 int err;
272
273 int4 i = dat->i;
274 int4 imax = dat->imax;
275 int4 j = dat->j;
276 int4 k = dat->k;
277 phloat max = dat->max;
278 phloat tmp = dat->tmp;
279 phloat tmp_re = dat->tmp_re;
280 phloat tmp_im = dat->tmp_im;
281 phloat sum_re = dat->sum_re;
282 phloat sum_im = dat->sum_im;
283 phloat s_re = dat->s_re;
284 phloat s_im = dat->s_im;
285
286 phloat xre, xim, yre, yim;
287 phloat tiniest = 1e20 / POS_HUGE_PHLOAT;
288 phloat tiny;
289
290 if (interrupted) {
291 free(scale);
292 err = dat->completion(ERR_INTERRUPTED, dat->a, perm, 0, 0);
293 free(dat);
294 return err;
295 }
296
297 switch (dat->state) {
298 case 0: break;
299 case 1: goto state1;
300 case 2: goto state2;
301 case 3: goto state3;
302 case 4: goto state4;
303 case 5: goto state5;
304 }
305
306 dat->det_re = 1;
307 dat->det_im = 0;
308
309 for (i = 0; i < n; i++) {
310 max = 0;
311 for (j = 0; j < n; j++) {
312 tmp = hypot(a[2 * (i * n + j)], a[2 * (i * n + j) + 1]);
313 if (tmp > max)
314 max = tmp;
315 STATE(1);
316 }
317 scale[i] = max;
318 }
319
320 for (j = 0; j < n; j++) {
321 for (i = 0; i < j; i++) {
322 sum_re = a[2 * (i * n + j)];
323 sum_im = a[2 * (i * n + j) + 1];
324 for (k = 0; k < i; k++) {
325 xre = a[2 * (i * n + k)];
326 xim = a[2 * (i * n + k) + 1];
327 yre = a[2 * (k * n + j)];
328 yim = a[2 * (k * n + j) + 1];
329 sum_re -= xre * yre - xim * yim;
330 sum_im -= xim * yre + xre * yim;
331 STATE(2);
332 }
333 a[2 * (i * n + j)] = sum_re;
334 a[2 * (i * n + j) + 1] = sum_im;
335 }
336
337 max = 0;
338 imax = j;
339 for (i = j; i < n; i++) {
340 sum_re = a[2 * (i * n + j)];
341 sum_im = a[2 * (i * n + j) + 1];
342 for (k = 0; k < j; k++) {
343 xre = a[2 * (i * n + k)];
344 xim = a[2 * (i * n + k) + 1];
345 yre = a[2 * (k * n + j)];
346 yim = a[2 * (k * n + j) + 1];
347 sum_re -= xre * yre - xim * yim;
348 sum_im -= xim * yre + xre * yim;
349 STATE(3);
350 }
351 a[2 * (i * n + j)] = sum_re;
352 a[2 * (i * n + j) + 1] = sum_im;
353 if (scale[i] == 0) {
354 imax = i;
355 break;
356 }
357 tmp = hypot(sum_re, sum_im) / scale[i];
358 if (tmp > max) {
359 imax = i;
360 max = tmp;
361 }
362 }
363
364 if (j != imax) {
365 for (k = 0; k < n; k++) {
366 tmp = a[2 * (imax * n + k)];
367 a[2 * (imax * n + k)] = a[2 * (j * n + k)];
368 a[2 * (j * n + k)] = tmp;
369 tmp = a[2 * (imax * n + k) + 1];
370 a[2 * (imax * n + k) + 1] = a[2 * (j * n + k) + 1];
371 a[2 * (j * n + k) + 1] = tmp;
372 STATE(4);
373 }
374 dat->det_re = -dat->det_re;
375 dat->det_im = -dat->det_im;
376 scale[imax] = scale[j];
377 }
378
379 perm[j] = imax;
380 tmp_re = a[2 * (j * n + j)];
381 tmp_im = a[2 * (j * n + j) + 1];
382 if (tmp_re == 0 && tmp_im == 0) {
383 if (core_settings.matrix_singularmatrix) {
384 free(scale);
385 err = dat->completion(ERR_NONE, dat->a, perm, 0, 0);
386 free(dat);
387 return err;
388 } else {
389 /* For a zero pivot, substitute a small positive number.
390 * I use a number that's about 10^-20 times the size of
391 * the maximum of the original column, with a minimum of
392 * 10^20 / POS_HUGE_PHLOAT.
393 */
394 if (scale[j] == 0)
395 tiny = tiniest;
396 else {
397 tiny = pow(10, floor(log10(scale[j])) - 20);
398 if (tiny < tiniest)
399 tiny = tiniest;
400 }
401 a[2 * (j * n + j)] = tmp_re = tiny;
402 a[2 * (j * n + j) + 1] = tmp_im = 0;
403 }
404 }
405 tmp = dat->det_re * tmp_re - dat->det_im * tmp_im;
406 dat->det_im = dat->det_im * tmp_re + dat->det_re * tmp_im;
407 dat->det_re = tmp;
408 if (j != n - 1) {
409 tmp = hypot(tmp_re, tmp_im);
410 s_re = tmp_re / tmp / tmp;
411 s_im = -tmp_im / tmp / tmp;
412 for (i = j + 1; i < n; i++) {
413 tmp_re = a[2 * (i * n + j)];
414 tmp_im = a[2 * (i * n + j) + 1];
415 a[2 * (i * n + j)] = tmp_re * s_re - tmp_im * s_im;
416 a[2 * (i * n + j) + 1] = tmp_im * s_re + tmp_re * s_im;
417 STATE(5);
418 }
419 }
420 }
421
422 free(scale);
423 err = dat->completion(ERR_NONE, dat->a, perm, dat->det_re, dat->det_im);
424 free(dat);
425 return err;
426
427 suspend:
428 dat->i = i;
429 dat->imax = imax;
430 dat->j = j;
431 dat->k = k;
432 dat->max = max;
433 dat->tmp = tmp;
434 dat->tmp_re = tmp_re;
435 dat->tmp_im = tmp_im;
436 dat->sum_re = sum_re;
437 dat->sum_im = sum_im;
438 dat->s_re = s_re;
439 dat->s_im = s_im;
440 return ERR_INTERRUPTIBLE;
441 }
442
443
444 /*****************************/
445 /***** Back-substitution *****/
446 /*****************************/
447
448 struct backsub_rr_data_struct {
449 vartype_realmatrix *a;
450 int4 *perm;
451 vartype_realmatrix *b;
452 int4 i, ii, j, ll, k;
453 phloat sum;
454 int state;
455 int (*completion)(int, vartype_realmatrix *, int4 *, vartype_realmatrix *);
456 };
457
458 static backsub_rr_data_struct *backsub_rr_data;
459
460 static int lu_backsubst_rr_worker(bool interrupted);
461
462 int lu_backsubst_rr(vartype_realmatrix *a, int4 *perm, vartype_realmatrix *b,
463 int (*completion)(int, vartype_realmatrix *,
464 int4 *, vartype_realmatrix *)) {
465 backsub_rr_data_struct *dat =
466 (backsub_rr_data_struct *) malloc(sizeof(backsub_rr_data_struct));
467
468 if (dat == NULL)
469 return completion(ERR_INSUFFICIENT_MEMORY, a, perm, b);
470
471 dat->a = a;
472 dat->perm = perm;
473 dat->b = b;
474 dat->completion = completion;
475
476 dat->state = 0;
477
478 backsub_rr_data = dat;
479 mode_interruptible = lu_backsubst_rr_worker;
480 mode_stoppable = false;
481 return ERR_INTERRUPTIBLE;
482 }
483
484 static int lu_backsubst_rr_worker(bool interrupted) {
485 backsub_rr_data_struct *dat = backsub_rr_data;
486 phloat *a = dat->a->array->data;
487 int4 n = dat->a->rows;
488 phloat *b = dat->b->array->data;
489 int4 q = dat->b->columns;
490 int4 *perm = dat->perm;
491 int count = 1000;
492
493 int4 i = dat->i;
494 int4 ii = dat->ii;
495 int4 j = dat->j;
496 int4 ll = dat->ll;
497 int4 k = dat->k;
498 phloat sum = dat->sum;
499
500 phloat t;
501
502 if (interrupted) {
503 int err = dat->completion(ERR_INTERRUPTED, dat->a, perm, dat->b);
504 free(dat);
505 return err;
506 }
507
508 switch (dat->state) {
509 case 0: break;
510 case 1: goto state1;
511 case 2: goto state2;
512 }
513
514 for (k = 0; k < q; k++) {
515 ii = -1;
516 for (i = 0; i < n; i++) {
517 ll = perm[i];
518 sum = b[ll * q + k];
519 b[ll * q + k] = b[i * q + k];
520 if (ii != -1) {
521 for (j = ii; j < i; j++) {
522 sum -= a[i * n + j] * b[j * q + k];
523 STATE(1);
524 }
525 } else if (sum != 0)
526 ii = i;
527 b[i * q + k] = sum;
528 }
529 for (i = n - 1; i >= 0; i--) {
530 sum = b[i * q + k];
531 for (j = i + 1; j < n; j++) {
532 sum -= a[i * n + j] * b[j * q + k];
533 STATE(2);
534 }
535 t = sum / a[i * n + i];
536 if (p_isinf(t) || p_isnan(t)) {
537 if (core_settings.matrix_outofrange
538 && !flags.f.range_error_ignore)
539 return ERR_OUT_OF_RANGE;
540 else
541 t = p_isinf(t) < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT;
542 }
543 b[i * q + k] = t;
544 }
545 }
546
547 int err;
548 err = dat->completion(ERR_NONE, dat->a, perm, dat->b);
549 free(dat);
550 return err;
551
552 suspend:
553 dat->i = i;
554 dat->ii = ii;
555 dat->j = j;
556 dat->ll = ll;
557 dat->k = k;
558 dat->sum = sum;
559 return ERR_INTERRUPTIBLE;
560 }
561
562 struct backsub_rc_data_struct {
563 vartype_realmatrix *a;
564 int4 *perm;
565 vartype_complexmatrix *b;
566 int4 i, ii, j, ll, k;
567 phloat sum_re, sum_im;
568 int state;
569 int (*completion)(int, vartype_realmatrix *, int4 *,
570 vartype_complexmatrix *);
571 };
572
573 static backsub_rc_data_struct *backsub_rc_data;
574
575 static int lu_backsubst_rc_worker(bool interrupted);
576
577 int lu_backsubst_rc(vartype_realmatrix *a, int4 *perm, vartype_complexmatrix *b,
578 int (*completion)(int, vartype_realmatrix *,
579 int4 *, vartype_complexmatrix *)) {
580 backsub_rc_data_struct *dat =
581 (backsub_rc_data_struct *) malloc(sizeof(backsub_rc_data_struct));
582
583 if (dat == NULL)
584 return completion(ERR_INSUFFICIENT_MEMORY, a, perm, b);
585
586 dat->a = a;
587 dat->perm = perm;
588 dat->b = b;
589 dat->completion = completion;
590
591 dat->state = 0;
592
593 backsub_rc_data = dat;
594 mode_interruptible = lu_backsubst_rc_worker;
595 mode_stoppable = false;
596 return ERR_INTERRUPTIBLE;
597 }
598
599 static int lu_backsubst_rc_worker(bool interrupted) {
600 backsub_rc_data_struct *dat = backsub_rc_data;
601 phloat *a = dat->a->array->data;
602 int4 n = dat->a->rows;
603 phloat *b = dat->b->array->data;
604 int4 q = dat->b->columns;
605 int4 *perm = dat->perm;
606 int count = 1000;
607
608 int4 i = dat->i;
609 int4 ii = dat->ii;
610 int4 j = dat->j;
611 int4 ll = dat->ll;
612 int4 k = dat->k;
613 phloat sum_re = dat->sum_re;
614 phloat sum_im = dat->sum_im;
615 phloat tmp;
616
617 phloat t_re, t_im;
618
619 if (interrupted) {
620 int err = dat->completion(ERR_INTERRUPTED, dat->a, perm, dat->b);
621 free(dat);
622 return err;
623 }
624
625 switch (dat->state) {
626 case 0: break;
627 case 1: goto state1;
628 case 2: goto state2;
629 }
630
631 for (k = 0; k < q; k++) {
632 ii = -1;
633 for (i = 0; i < n; i++) {
634 ll = perm[i];
635 sum_re = b[2 * (ll * q + k)];
636 sum_im = b[2 * (ll * q + k) + 1];
637 b[2 * (ll * q + k)] = b[2 * (i * q + k)];
638 b[2 * (ll * q + k) + 1] = b[2 * (i * q + k) + 1];
639 if (ii != -1) {
640 for (j = ii; j < i; j++) {
641 tmp = a[i * n + j];
642 sum_re -= tmp * b[2 * (j * q + k)];
643 sum_im -= tmp * b[2 * (j * q + k) + 1];
644 STATE(1);
645 }
646 } else if (sum_re != 0 || sum_im != 0)
647 ii = i;
648 b[2 * (i * q + k)] = sum_re;
649 b[2 * (i * q + k) + 1] = sum_im;
650 }
651 for (i = n - 1; i >= 0; i--) {
652 sum_re = b[2 * (i * q + k)];
653 sum_im = b[2 * (i * q + k) + 1];
654 for (j = i + 1; j < n; j++) {
655 tmp = a[i * n + j];
656 sum_re -= tmp * b[2 * (j * q + k)];
657 sum_im -= tmp * b[2 * (j * q + k) + 1];
658 STATE(2);
659 }
660 tmp = a[i * n + i];
661 t_re = sum_re / tmp;
662 t_im = sum_im / tmp;
663 if (p_isinf(t_re) || p_isnan(t_re)) {
664 if (core_settings.matrix_outofrange
665 && !flags.f.range_error_ignore)
666 return ERR_OUT_OF_RANGE;
667 else
668 t_re = p_isinf(t_re) < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT;
669 }
670 if (p_isinf(t_im) || p_isnan(t_im)) {
671 if (core_settings.matrix_outofrange
672 && !flags.f.range_error_ignore)
673 return ERR_OUT_OF_RANGE;
674 else
675 t_im = p_isinf(t_im) < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT;
676 }
677 b[2 * (i * q + k)] = t_re;
678 b[2 * (i * q + k) + 1] = t_im;
679 }
680 }
681
682 int err;
683 err = dat->completion(ERR_NONE, dat->a, perm, dat->b);
684 free(dat);
685 return err;
686
687 suspend:
688 dat->i = i;
689 dat->ii = ii;
690 dat->j = j;
691 dat->ll = ll;
692 dat->k = k;
693 dat->sum_re = sum_re;
694 dat->sum_im = sum_im;
695 return ERR_INTERRUPTIBLE;
696 }
697
698 struct backsub_cc_data_struct {
699 vartype_complexmatrix *a;
700 int4 *perm;
701 vartype_complexmatrix *b;
702 int4 i, ii, j, ll, k;
703 phloat sum_re, sum_im;
704 int state;
705 int (*completion)(int, vartype_complexmatrix *, int4 *,
706 vartype_complexmatrix *);
707 };
708
709 static backsub_cc_data_struct *backsub_cc_data;
710
711 static int lu_backsubst_cc_worker(bool interrupted);
712
713 int lu_backsubst_cc(vartype_complexmatrix *a, int4 *perm, vartype_complexmatrix *b,
714 int (*completion)(int, vartype_complexmatrix *,
715 int4 *, vartype_complexmatrix *)) {
716 backsub_cc_data_struct *dat =
717 (backsub_cc_data_struct *) malloc(sizeof(backsub_cc_data_struct));
718
719 if (dat == NULL)
720 return completion(ERR_INSUFFICIENT_MEMORY, a, perm, b);
721
722 dat->a = a;
723 dat->perm = perm;
724 dat->b = b;
725 dat->completion = completion;
726
727 dat->state = 0;
728
729 backsub_cc_data = dat;
730 mode_interruptible = lu_backsubst_cc_worker;
731 mode_stoppable = false;
732 return ERR_INTERRUPTIBLE;
733 }
734
735 static int lu_backsubst_cc_worker(bool interrupted) {
736 backsub_cc_data_struct *dat = backsub_cc_data;
737 phloat *a = dat->a->array->data;
738 int4 n = dat->a->rows;
739 phloat *b = dat->b->array->data;
740 int4 q = dat->b->columns;
741 int4 *perm = dat->perm;
742 int count = 1000;
743
744 int4 i = dat->i;
745 int4 ii = dat->ii;
746 int4 j = dat->j;
747 int4 ll = dat->ll;
748 int4 k = dat->k;
749 phloat sum_re = dat->sum_re;
750 phloat sum_im = dat->sum_im;
751 phloat tmp, tmp_re, tmp_im;
752
753 phloat bre, bim;
754 phloat t_re, t_im;
755
756 if (interrupted) {
757 int err = dat->completion(ERR_INTERRUPTED, dat->a, perm, dat->b);
758 free(dat);
759 return err;
760 }
761
762 switch (dat->state) {
763 case 0: break;
764 case 1: goto state1;
765 case 2: goto state2;
766 }
767
768 for (k = 0; k < q; k++) {
769 ii = -1;
770 for (i = 0; i < n; i++) {
771 ll = perm[i];
772 sum_re = b[2 * (ll * q + k)];
773 sum_im = b[2 * (ll * q + k) + 1];
774 b[2 * (ll * q + k)] = b[2 * (i * q + k)];
775 b[2 * (ll * q + k) + 1] = b[2 * (i * q + k) + 1];
776 if (ii != -1) {
777 for (j = ii; j < i; j++) {
778 bre = b[2 * (j * q + k)];
779 bim = b[2 * (j * q + k) + 1];
780 tmp_re = a[2 * (i * n + j)];
781 tmp_im = a[2 * (i * n + j) + 1];
782 sum_re -= bre * tmp_re - bim * tmp_im;
783 sum_im -= bim * tmp_re + bre * tmp_im;
784 STATE(1);
785 }
786 } else if (sum_re != 0 || sum_im != 0)
787 ii = i;
788 b[2 * (i * q + k)] = sum_re;
789 b[2 * (i * q + k) + 1] = sum_im;
790 }
791 for (i = n - 1; i >= 0; i--) {
792 sum_re = b[2 * (i * q + k)];
793 sum_im = b[2 * (i * q + k) + 1];
794 for (j = i + 1; j < n; j++) {
795 bre = b[2 * (j * q + k)];
796 bim = b[2 * (j * q + k) + 1];
797 tmp_re = a[2 * (i * n + j)];
798 tmp_im = a[2 * (i * n + j) + 1];
799 sum_re -= bre * tmp_re - bim * tmp_im;
800 sum_im -= bim * tmp_re + bre * tmp_im;
801 STATE(2);
802 }
803 tmp_re = a[2 * (i * n + i)];
804 tmp_im = a[2 * (i * n + i) + 1];
805 tmp = hypot(tmp_re, tmp_im);
806 tmp_re = tmp_re / tmp / tmp;
807 tmp_im = -tmp_im / tmp / tmp;
808 t_re = sum_re * tmp_re - sum_im * tmp_im;
809 t_im = sum_im * tmp_re + sum_re * tmp_im;
810 if (p_isinf(t_re) || p_isnan(t_re)) {
811 if (core_settings.matrix_outofrange
812 && !flags.f.range_error_ignore)
813 return ERR_OUT_OF_RANGE;
814 else
815 t_re = p_isinf(t_re) < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT;
816 }
817 if (p_isinf(t_im) || p_isnan(t_im)) {
818 if (core_settings.matrix_outofrange
819 && !flags.f.range_error_ignore)
820 return ERR_OUT_OF_RANGE;
821 else
822 t_im = p_isinf(t_im) < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT;
823 }
824 b[2 * (i * q + k)] = t_re;
825 b[2 * (i * q + k) + 1] = t_im;
826 }
827 }
828
829 int err;
830 err = dat->completion(ERR_NONE, dat->a, perm, dat->b);
831 free(dat);
832 return err;
833
834 suspend:
835 dat->i = i;
836 dat->ii = ii;
837 dat->j = j;
838 dat->ll = ll;
839 dat->k = k;
840 dat->sum_re = sum_re;
841 dat->sum_im = sum_im;
842 return ERR_INTERRUPTIBLE;
843 }