forked from balintfodor/sba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsba_lapack.c
1678 lines (1461 loc) · 48.7 KB
/
sba_lapack.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
/////////////////////////////////////////////////////////////////////////////////
////
//// Linear algebra operations for the sba package
//// Copyright (C) 2004-2009 Manolis Lourakis (lourakis at ics forth gr)
//// Institute of Computer Science, Foundation for Research & Technology - Hellas
//// Heraklion, Crete, Greece.
////
//// This program is free software; you can redistribute it and/or modify
//// it under the terms of the GNU General Public License as published by
//// the Free Software Foundation; either version 2 of the License, or
//// (at your option) any later version.
////
//// This program is distributed in the hope that it will be useful,
//// but WITHOUT ANY WARRANTY; without even the implied warranty of
//// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//// GNU General Public License for more details.
////
///////////////////////////////////////////////////////////////////////////////////
/* A note on memory alignment:
*
* Several of the functions below use a piece of dynamically allocated memory
* to store variables of different size (i.e., ints and doubles). To avoid
* alignment problems, care must be taken so that elements that are larger
* (doubles) are stored before smaller ones (ints). This ensures proper
* alignment under different alignment choices made by different CPUs:
* For instance, a double variable is aligned on x86 to 4 bytes but
* aligned to 8 bytes on AMD64 despite having the same size of 8 bytes.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <float.h>
#include "compiler.h"
#include "sba.h"
#ifdef SBA_APPEND_UNDERSCORE_SUFFIX
#define F77_FUNC(func) func ## _
#else
#define F77_FUNC(func) func
#endif /* SBA_APPEND_UNDERSCORE_SUFFIX */
/* declarations of LAPACK routines employed */
/* QR decomposition */
extern int F77_FUNC(dgeqrf)(int *m, int *n, double *a, int *lda, double *tau, double *work, int *lwork, int *info);
extern int F77_FUNC(dorgqr)(int *m, int *n, int *k, double *a, int *lda, double *tau, double *work, int *lwork, int *info);
/* solution of triangular system */
extern int F77_FUNC(dtrtrs)(char *uplo, char *trans, char *diag, int *n, int *nrhs, double *a, int *lda, double *b, int *ldb, int *info);
/* Cholesky decomposition, linear system solution and matrix inversion */
extern int F77_FUNC(dpotf2)(char *uplo, int *n, double *a, int *lda, int *info); /* unblocked Cholesky */
extern int F77_FUNC(dpotrf)(char *uplo, int *n, double *a, int *lda, int *info); /* block version of dpotf2 */
extern int F77_FUNC(dpotrs)(char *uplo, int *n, int *nrhs, double *a, int *lda, double *b, int *ldb, int *info);
extern int F77_FUNC(dpotri)(char *uplo, int *n, double *a, int *lda, int *info);
/* LU decomposition, linear system solution and matrix inversion */
extern int F77_FUNC(dgetrf)(int *m, int *n, double *a, int *lda, int *ipiv, int *info); /* blocked LU */
extern int F77_FUNC(dgetf2)(int *m, int *n, double *a, int *lda, int *ipiv, int *info); /* unblocked LU */
extern int F77_FUNC(dgetrs)(char *trans, int *n, int *nrhs, double *a, int *lda, int *ipiv, double *b, int *ldb, int *info);
extern int F77_FUNC(dgetri)(int *n, double *a, int *lda, int *ipiv, double *work, int *lwork, int *info);
/* SVD */
extern int F77_FUNC(dgesvd)(char *jobu, char *jobvt, int *m, int *n,
double *a, int *lda, double *s, double *u, int *ldu,
double *vt, int *ldvt, double *work, int *lwork,
int *info);
/* lapack 3.0 routine, faster than dgesvd() */
extern int F77_FUNC(dgesdd)(char *jobz, int *m, int *n, double *a, int *lda,
double *s, double *u, int *ldu, double *vt, int *ldvt,
double *work, int *lwork, int *iwork, int *info);
/* Bunch-Kaufman factorization of a real symmetric matrix A, solution of linear systems and matrix inverse */
extern int F77_FUNC(dsytrf)(char *uplo, int *n, double *a, int *lda, int *ipiv, double *work, int *lwork, int *info); /* blocked ver. */
extern int F77_FUNC(dsytrs)(char *uplo, int *n, int *nrhs, double *a, int *lda, int *ipiv, double *b, int *ldb, int *info);
extern int F77_FUNC(dsytri)(char *uplo, int *n, double *a, int *lda, int *ipiv, double *work, int *info);
/*
* This function returns the solution of Ax = b
*
* The function is based on QR decomposition with explicit computation of Q:
* If A=Q R with Q orthogonal and R upper triangular, the linear system becomes
* Q R x = b or R x = Q^T b.
*
* A is mxm, b is mx1. Argument iscolmaj specifies whether A is
* stored in column or row major order. Note that if iscolmaj==1
* this function modifies A!
*
* The function returns 0 in case of error, 1 if successfull
*
* This function is often called repetitively to solve problems of identical
* dimensions. To avoid repetitive malloc's and free's, allocated memory is
* retained between calls and free'd-malloc'ed when not of the appropriate size.
* A call with NULL as the first argument forces this memory to be released.
*/
int sba_Axb_QR(double *A, double *B, double *x, int m, int iscolmaj)
{
static double *buf=NULL;
static int buf_sz=0, nb=0;
double *a, *r, *tau, *work;
int a_sz, r_sz, tau_sz, tot_sz;
register int i, j;
int info, worksz, nrhs=1;
register double sum;
if(A==NULL){
if(buf) free(buf);
buf=NULL;
buf_sz=0;
return 1;
}
/* calculate required memory size */
a_sz=(iscolmaj)? 0 : m*m;
r_sz=m*m; /* only the upper triangular part really needed */
tau_sz=m;
if(!nb){
#ifndef SBA_LS_SCARCE_MEMORY
double tmp;
worksz=-1; // workspace query; optimal size is returned in tmp
F77_FUNC(dgeqrf)((int *)&m, (int *)&m, NULL, (int *)&m, NULL, (double *)&tmp, (int *)&worksz, (int *)&info);
nb=((int)tmp)/m; // optimal worksize is m*nb
#else
nb=1; // min worksize is m
#endif /* SBA_LS_SCARCE_MEMORY */
}
worksz=nb*m;
tot_sz=a_sz + r_sz + tau_sz + worksz;
if(tot_sz>buf_sz){ /* insufficient memory, allocate a "big" memory chunk at once */
if(buf) free(buf); /* free previously allocated memory */
buf_sz=tot_sz;
buf=(double *)malloc(buf_sz*sizeof(double));
if(!buf){
fprintf(stderr, "memory allocation in sba_Axb_QR() failed!\n");
exit(1);
}
}
if(!iscolmaj){
a=buf;
/* store A (column major!) into a */
for(i=0; i<m; ++i)
for(j=0; j<m; ++j)
a[i+j*m]=A[i*m+j];
}
else a=A; /* no copying required */
r=buf+a_sz;
tau=r+r_sz;
work=tau+tau_sz;
/* QR decomposition of A */
F77_FUNC(dgeqrf)((int *)&m, (int *)&m, a, (int *)&m, tau, work, (int *)&worksz, (int *)&info);
/* error treatment */
if(info!=0){
if(info<0){
fprintf(stderr, "LAPACK error: illegal value for argument %d of dgeqrf in sba_Axb_QR()\n", -info);
exit(1);
}
else{
fprintf(stderr, "Unknown LAPACK error %d for dgeqrf in sba_Axb_QR()\n", info);
return 0;
}
}
/* R is now stored in the upper triangular part of a; copy it in r so that dorgqr() below won't destroy it */
for(i=0; i<r_sz; ++i)
r[i]=a[i];
/* compute Q using the elementary reflectors computed by the above decomposition */
F77_FUNC(dorgqr)((int *)&m, (int *)&m, (int *)&m, a, (int *)&m, tau, work, (int *)&worksz, (int *)&info);
if(info!=0){
if(info<0){
fprintf(stderr, "LAPACK error: illegal value for argument %d of dorgqr in sba_Axb_QR()\n", -info);
exit(1);
}
else{
fprintf(stderr, "Unknown LAPACK error (%d) in sba_Axb_QR()\n", info);
return 0;
}
}
/* Q is now in a; compute Q^T b in x */
for(i=0; i<m; ++i){
for(j=0, sum=0.0; j<m; ++j)
sum+=a[i*m+j]*B[j];
x[i]=sum;
}
/* solve the linear system R x = Q^t b */
F77_FUNC(dtrtrs)("U", "N", "N", (int *)&m, (int *)&nrhs, r, (int *)&m, x, (int *)&m, &info);
/* error treatment */
if(info!=0){
if(info<0){
fprintf(stderr, "LAPACK error: illegal value for argument %d of dtrtrs in sba_Axb_QR()\n", -info);
exit(1);
}
else{
fprintf(stderr, "LAPACK error: the %d-th diagonal element of A is zero (singular matrix) in sba_Axb_QR()\n", info);
return 0;
}
}
return 1;
}
/*
* This function returns the solution of Ax = b
*
* The function is based on QR decomposition without computation of Q:
* If A=Q R with Q orthogonal and R upper triangular, the linear system becomes
* (A^T A) x = A^T b or (R^T Q^T Q R) x = A^T b or (R^T R) x = A^T b.
* This amounts to solving R^T y = A^T b for y and then R x = y for x
* Note that Q does not need to be explicitly computed
*
* A is mxm, b is mx1. Argument iscolmaj specifies whether A is
* stored in column or row major order. Note that if iscolmaj==1
* this function modifies A!
*
* The function returns 0 in case of error, 1 if successfull
*
* This function is often called repetitively to solve problems of identical
* dimensions. To avoid repetitive malloc's and free's, allocated memory is
* retained between calls and free'd-malloc'ed when not of the appropriate size.
* A call with NULL as the first argument forces this memory to be released.
*/
int sba_Axb_QRnoQ(double *A, double *B, double *x, int m, int iscolmaj)
{
static double *buf=NULL;
static int buf_sz=0, nb=0;
double *a, *tau, *work;
int a_sz, tau_sz, tot_sz;
register int i, j;
int info, worksz, nrhs=1;
register double sum;
if(A==NULL){
if(buf) free(buf);
buf=NULL;
buf_sz=0;
return 1;
}
/* calculate required memory size */
a_sz=(iscolmaj)? 0 : m*m;
tau_sz=m;
if(!nb){
#ifndef SBA_LS_SCARCE_MEMORY
double tmp;
worksz=-1; // workspace query; optimal size is returned in tmp
F77_FUNC(dgeqrf)((int *)&m, (int *)&m, NULL, (int *)&m, NULL, (double *)&tmp, (int *)&worksz, (int *)&info);
nb=((int)tmp)/m; // optimal worksize is m*nb
#else
nb=1; // min worksize is m
#endif /* SBA_LS_SCARCE_MEMORY */
}
worksz=nb*m;
tot_sz=a_sz + tau_sz + worksz;
if(tot_sz>buf_sz){ /* insufficient memory, allocate a "big" memory chunk at once */
if(buf) free(buf); /* free previously allocated memory */
buf_sz=tot_sz;
buf=(double *)malloc(buf_sz*sizeof(double));
if(!buf){
fprintf(stderr, "memory allocation in sba_Axb_QRnoQ() failed!\n");
exit(1);
}
}
if(!iscolmaj){
a=buf;
/* store A (column major!) into a */
for(i=0; i<m; ++i)
for(j=0; j<m; ++j)
a[i+j*m]=A[i*m+j];
}
else a=A; /* no copying required */
tau=buf+a_sz;
work=tau+tau_sz;
/* compute A^T b in x */
for(i=0; i<m; ++i){
for(j=0, sum=0.0; j<m; ++j)
sum+=a[i*m+j]*B[j];
x[i]=sum;
}
/* QR decomposition of A */
F77_FUNC(dgeqrf)((int *)&m, (int *)&m, a, (int *)&m, tau, work, (int *)&worksz, (int *)&info);
/* error treatment */
if(info!=0){
if(info<0){
fprintf(stderr, "LAPACK error: illegal value for argument %d of dgeqrf in sba_Axb_QRnoQ()\n", -info);
exit(1);
}
else{
fprintf(stderr, "Unknown LAPACK error %d for dgeqrf in sba_Axb_QRnoQ()\n", info);
return 0;
}
}
/* R is stored in the upper triangular part of a */
/* solve the linear system R^T y = A^t b */
F77_FUNC(dtrtrs)("U", "T", "N", (int *)&m, (int *)&nrhs, a, (int *)&m, x, (int *)&m, &info);
/* error treatment */
if(info!=0){
if(info<0){
fprintf(stderr, "LAPACK error: illegal value for argument %d of dtrtrs in sba_Axb_QRnoQ()\n", -info);
exit(1);
}
else{
fprintf(stderr, "LAPACK error: the %d-th diagonal element of A is zero (singular matrix) in sba_Axb_QRnoQ()\n", info);
return 0;
}
}
/* solve the linear system R x = y */
F77_FUNC(dtrtrs)("U", "N", "N", (int *)&m, (int *)&nrhs, a, (int *)&m, x, (int *)&m, &info);
/* error treatment */
if(info!=0){
if(info<0){
fprintf(stderr, "LAPACK error: illegal value for argument %d of dtrtrs in sba_Axb_QRnoQ()\n", -info);
exit(1);
}
else{
fprintf(stderr, "LAPACK error: the %d-th diagonal element of A is zero (singular matrix) in sba_Axb_QRnoQ()\n", info);
return 0;
}
}
return 1;
}
/*
* This function returns the solution of Ax=b
*
* The function assumes that A is symmetric & positive definite and employs
* the Cholesky decomposition:
* If A=U^T U with U upper triangular, the system to be solved becomes
* (U^T U) x = b
* This amounts to solving U^T y = b for y and then U x = y for x
*
* A is mxm, b is mx1. Argument iscolmaj specifies whether A is
* stored in column or row major order. Note that if iscolmaj==1
* this function modifies A!
*
* The function returns 0 in case of error, 1 if successfull
*
* This function is often called repetitively to solve problems of identical
* dimensions. To avoid repetitive malloc's and free's, allocated memory is
* retained between calls and free'd-malloc'ed when not of the appropriate size.
* A call with NULL as the first argument forces this memory to be released.
*/
int sba_Axb_Chol(double *A, double *B, double *x, int m, int iscolmaj)
{
static double *buf=NULL;
static int buf_sz=0;
double *a;
int a_sz, tot_sz;
register int i, j;
int info, nrhs=1;
if(A==NULL){
if(buf) free(buf);
buf=NULL;
buf_sz=0;
return 1;
}
/* calculate required memory size */
a_sz=(iscolmaj)? 0 : m*m;
tot_sz=a_sz;
if(tot_sz>buf_sz){ /* insufficient memory, allocate a "big" memory chunk at once */
if(buf) free(buf); /* free previously allocated memory */
buf_sz=tot_sz;
buf=(double *)malloc(buf_sz*sizeof(double));
if(!buf){
fprintf(stderr, "memory allocation in sba_Axb_Chol() failed!\n");
exit(1);
}
}
if(!iscolmaj){
a=buf;
/* store A into a and B into x; A is assumed to be symmetric, hence
* the column and row major order representations are the same
*/
for(i=0; i<m; ++i){
a[i]=A[i];
x[i]=B[i];
}
for(j=m*m; i<j; ++i) // copy remaining rows; note that i is not re-initialized
a[i]=A[i];
}
else{ /* no copying is necessary for A */
a=A;
for(i=0; i<m; ++i)
x[i]=B[i];
}
/* Cholesky decomposition of A */
//F77_FUNC(dpotf2)("U", (int *)&m, a, (int *)&m, (int *)&info);
F77_FUNC(dpotrf)("U", (int *)&m, a, (int *)&m, (int *)&info);
/* error treatment */
if(info!=0){
if(info<0){
fprintf(stderr, "LAPACK error: illegal value for argument %d of dpotf2/dpotrf in sba_Axb_Chol()\n", -info);
exit(1);
}
else{
fprintf(stderr, "LAPACK error: the leading minor of order %d is not positive definite,\nthe factorization could not be completed for dpotf2/dpotrf in sba_Axb_Chol()\n", info);
return 0;
}
}
/* below are two alternative ways for solving the linear system: */
#if 1
/* use the computed Cholesky in one lapack call */
F77_FUNC(dpotrs)("U", (int *)&m, (int *)&nrhs, a, (int *)&m, x, (int *)&m, &info);
if(info<0){
fprintf(stderr, "LAPACK error: illegal value for argument %d of dpotrs in sba_Axb_Chol()\n", -info);
exit(1);
}
#else
/* solve the linear systems U^T y = b, U x = y */
F77_FUNC(dtrtrs)("U", "T", "N", (int *)&m, (int *)&nrhs, a, (int *)&m, x, (int *)&m, &info);
/* error treatment */
if(info!=0){
if(info<0){
fprintf(stderr, "LAPACK error: illegal value for argument %d of dtrtrs in sba_Axb_Chol()\n", -info);
exit(1);
}
else{
fprintf(stderr, "LAPACK error: the %d-th diagonal element of A is zero (singular matrix) in sba_Axb_Chol()\n", info);
return 0;
}
}
/* solve U x = y */
F77_FUNC(dtrtrs)("U", "N", "N", (int *)&m, (int *)&nrhs, a, (int *)&m, x, (int *)&m, &info);
/* error treatment */
if(info!=0){
if(info<0){
fprintf(stderr, "LAPACK error: illegal value for argument %d of dtrtrs in sba_Axb_Chol()\n", -info);
exit(1);
}
else{
fprintf(stderr, "LAPACK error: the %d-th diagonal element of A is zero (singular matrix) in sba_Axb_Chol()\n", info);
return 0;
}
}
#endif /* 1 */
return 1;
}
/*
* This function returns the solution of Ax = b
*
* The function employs LU decomposition:
* If A=L U with L lower and U upper triangular, then the original system
* amounts to solving
* L y = b, U x = y
*
* A is mxm, b is mx1. Argument iscolmaj specifies whether A is
* stored in column or row major order. Note that if iscolmaj==1
* this function modifies A!
*
* The function returns 0 in case of error,
* 1 if successfull
*
* This function is often called repetitively to solve problems of identical
* dimensions. To avoid repetitive malloc's and free's, allocated memory is
* retained between calls and free'd-malloc'ed when not of the appropriate size.
* A call with NULL as the first argument forces this memory to be released.
*/
int sba_Axb_LU(double *A, double *B, double *x, int m, int iscolmaj)
{
static double *buf=NULL;
static int buf_sz=0;
int a_sz, ipiv_sz, tot_sz;
register int i, j;
int info, *ipiv, nrhs=1;
double *a;
if(A==NULL){
if(buf) free(buf);
buf=NULL;
buf_sz=0;
return 1;
}
/* calculate required memory size */
ipiv_sz=m;
a_sz=(iscolmaj)? 0 : m*m;
tot_sz=a_sz*sizeof(double) + ipiv_sz*sizeof(int); /* should be arranged in that order for proper doubles alignment */
if(tot_sz>buf_sz){ /* insufficient memory, allocate a "big" memory chunk at once */
if(buf) free(buf); /* free previously allocated memory */
buf_sz=tot_sz;
buf=(double *)malloc(buf_sz);
if(!buf){
fprintf(stderr, "memory allocation in sba_Axb_LU() failed!\n");
exit(1);
}
}
if(!iscolmaj){
a=buf;
ipiv=(int *)(a+a_sz);
/* store A (column major!) into a and B into x */
for(i=0; i<m; ++i){
for(j=0; j<m; ++j)
a[i+j*m]=A[i*m+j];
x[i]=B[i];
}
}
else{ /* no copying is necessary for A */
a=A;
for(i=0; i<m; ++i)
x[i]=B[i];
ipiv=(int *)buf;
}
/* LU decomposition for A */
F77_FUNC(dgetrf)((int *)&m, (int *)&m, a, (int *)&m, ipiv, (int *)&info);
if(info!=0){
if(info<0){
fprintf(stderr, "argument %d of dgetrf illegal in sba_Axb_LU()\n", -info);
exit(1);
}
else{
fprintf(stderr, "singular matrix A for dgetrf in sba_Axb_LU()\n");
return 0;
}
}
/* solve the system with the computed LU */
F77_FUNC(dgetrs)("N", (int *)&m, (int *)&nrhs, a, (int *)&m, ipiv, x, (int *)&m, (int *)&info);
if(info!=0){
if(info<0){
fprintf(stderr, "argument %d of dgetrs illegal in sba_Axb_LU()\n", -info);
exit(1);
}
else{
fprintf(stderr, "unknown error for dgetrs in sba_Axb_LU()\n");
return 0;
}
}
return 1;
}
/*
* This function returns the solution of Ax = b
*
* The function is based on SVD decomposition:
* If A=U D V^T with U, V orthogonal and D diagonal, the linear system becomes
* (U D V^T) x = b or x=V D^{-1} U^T b
* Note that V D^{-1} U^T is the pseudoinverse A^+
*
* A is mxm, b is mx1. Argument iscolmaj specifies whether A is
* stored in column or row major order. Note that if iscolmaj==1
* this function modifies A!
*
* The function returns 0 in case of error, 1 if successfull
*
* This function is often called repetitively to solve problems of identical
* dimensions. To avoid repetitive malloc's and free's, allocated memory is
* retained between calls and free'd-malloc'ed when not of the appropriate size.
* A call with NULL as the first argument forces this memory to be released.
*/
int sba_Axb_SVD(double *A, double *B, double *x, int m, int iscolmaj)
{
static double *buf=NULL;
static int buf_sz=0;
static double eps=-1.0;
register int i, j;
double *a, *u, *s, *vt, *work;
int a_sz, u_sz, s_sz, vt_sz, tot_sz;
double thresh, one_over_denom;
register double sum;
int info, rank, worksz, *iwork, iworksz;
if(A==NULL){
if(buf) free(buf);
buf=NULL;
buf_sz=0;
return 1;
}
/* calculate required memory size */
#ifndef SBA_LS_SCARCE_MEMORY
worksz=-1; // workspace query. Keep in mind that dgesdd requires more memory than dgesvd
/* note that optimal work size is returned in thresh */
F77_FUNC(dgesdd)("A", (int *)&m, (int *)&m, NULL, (int *)&m, NULL, NULL, (int *)&m, NULL, (int *)&m,
(double *)&thresh, (int *)&worksz, NULL, &info);
/* F77_FUNC(dgesvd)("A", "A", (int *)&m, (int *)&m, NULL, (int *)&m, NULL, NULL, (int *)&m, NULL, (int *)&m,
(double *)&thresh, (int *)&worksz, &info); */
worksz=(int)thresh;
#else
worksz=m*(7*m+4); // min worksize for dgesdd
//worksz=5*m; // min worksize for dgesvd
#endif /* SBA_LS_SCARCE_MEMORY */
iworksz=8*m;
a_sz=(!iscolmaj)? m*m : 0;
u_sz=m*m; s_sz=m; vt_sz=m*m;
tot_sz=(a_sz + u_sz + s_sz + vt_sz + worksz)*sizeof(double) + iworksz*sizeof(int); /* should be arranged in that order for proper doubles alignment */
if(tot_sz>buf_sz){ /* insufficient memory, allocate a "big" memory chunk at once */
if(buf) free(buf); /* free previously allocated memory */
buf_sz=tot_sz;
buf=(double *)malloc(buf_sz);
if(!buf){
fprintf(stderr, "memory allocation in sba_Axb_SVD() failed!\n");
exit(1);
}
}
if(!iscolmaj){
a=buf;
u=a+a_sz;
/* store A (column major!) into a */
for(i=0; i<m; ++i)
for(j=0; j<m; ++j)
a[i+j*m]=A[i*m+j];
}
else{
a=A; /* no copying required */
u=buf;
}
s=u+u_sz;
vt=s+s_sz;
work=vt+vt_sz;
iwork=(int *)(work+worksz);
/* SVD decomposition of A */
F77_FUNC(dgesdd)("A", (int *)&m, (int *)&m, a, (int *)&m, s, u, (int *)&m, vt, (int *)&m, work, (int *)&worksz, iwork, &info);
//F77_FUNC(dgesvd)("A", "A", (int *)&m, (int *)&m, a, (int *)&m, s, u, (int *)&m, vt, (int *)&m, work, (int *)&worksz, &info);
/* error treatment */
if(info!=0){
if(info<0){
fprintf(stderr, "LAPACK error: illegal value for argument %d of dgesdd/dgesvd in sba_Axb_SVD()\n", -info);
exit(1);
}
else{
fprintf(stderr, "LAPACK error: dgesdd (dbdsdc)/dgesvd (dbdsqr) failed to converge in sba_Axb_SVD() [info=%d]\n", info);
return 0;
}
}
if(eps<0.0){
double aux;
/* compute machine epsilon. DBL_EPSILON should do also */
for(eps=1.0; aux=eps+1.0, aux-1.0>0.0; eps*=0.5)
;
eps*=2.0;
}
/* compute the pseudoinverse in a */
memset(a, 0, m*m*sizeof(double)); /* initialize to zero */
for(rank=0, thresh=eps*s[0]; rank<m && s[rank]>thresh; ++rank){
one_over_denom=1.0/s[rank];
for(j=0; j<m; ++j)
for(i=0; i<m; ++i)
a[i*m+j]+=vt[rank+i*m]*u[j+rank*m]*one_over_denom;
}
/* compute A^+ b in x */
for(i=0; i<m; ++i){
for(j=0, sum=0.0; j<m; ++j)
sum+=a[i*m+j]*B[j];
x[i]=sum;
}
return 1;
}
/*
* This function returns the solution of Ax = b for a real symmetric matrix A
*
* The function is based on UDUT factorization with the pivoting
* strategy of Bunch and Kaufman:
* A is factored as U*D*U^T where U is upper triangular and
* D symmetric and block diagonal (aka spectral decomposition,
* Banachiewicz factorization, modified Cholesky factorization)
*
* A is mxm, b is mx1. Argument iscolmaj specifies whether A is
* stored in column or row major order. Note that if iscolmaj==1
* this function modifies A!
*
* The function returns 0 in case of error,
* 1 if successfull
*
* This function is often called repetitively to solve problems of identical
* dimensions. To avoid repetitive malloc's and free's, allocated memory is
* retained between calls and free'd-malloc'ed when not of the appropriate size.
* A call with NULL as the first argument forces this memory to be released.
*/
int sba_Axb_BK(double *A, double *B, double *x, int m, int iscolmaj)
{
static double *buf=NULL;
static int buf_sz=0, nb=0;
int a_sz, ipiv_sz, work_sz, tot_sz;
register int i, j;
int info, *ipiv, nrhs=1;
double *a, *work;
if(A==NULL){
if(buf) free(buf);
buf=NULL;
buf_sz=0;
return 1;
}
/* calculate required memory size */
ipiv_sz=m;
a_sz=(iscolmaj)? 0 : m*m;
if(!nb){
#ifndef SBA_LS_SCARCE_MEMORY
double tmp;
work_sz=-1; // workspace query; optimal size is returned in tmp
F77_FUNC(dsytrf)("U", (int *)&m, NULL, (int *)&m, NULL, (double *)&tmp, (int *)&work_sz, (int *)&info);
nb=((int)tmp)/m; // optimal worksize is m*nb
#else
nb=-1; // min worksize is 1
#endif /* SBA_LS_SCARCE_MEMORY */
}
work_sz=(nb!=-1)? nb*m : 1;
tot_sz=(a_sz + work_sz)*sizeof(double) + ipiv_sz*sizeof(int); /* should be arranged in that order for proper doubles alignment */
if(tot_sz>buf_sz){ /* insufficient memory, allocate a "big" memory chunk at once */
if(buf) free(buf); /* free previously allocated memory */
buf_sz=tot_sz;
buf=(double *)malloc(buf_sz);
if(!buf){
fprintf(stderr, "memory allocation in sba_Axb_BK() failed!\n");
exit(1);
}
}
if(!iscolmaj){
a=buf;
work=a+a_sz;
/* store A into a and B into x; A is assumed to be symmetric, hence
* the column and row major order representations are the same
*/
for(i=0; i<m; ++i){
a[i]=A[i];
x[i]=B[i];
}
for(j=m*m; i<j; ++i) // copy remaining rows; note that i is not re-initialized
a[i]=A[i];
}
else{ /* no copying is necessary for A */
a=A;
for(i=0; i<m; ++i)
x[i]=B[i];
work=buf;
}
ipiv=(int *)(work+work_sz);
/* factorization for A */
F77_FUNC(dsytrf)("U", (int *)&m, a, (int *)&m, ipiv, work, (int *)&work_sz, (int *)&info);
if(info!=0){
if(info<0){
fprintf(stderr, "argument %d of dsytrf illegal in sba_Axb_BK()\n", -info);
exit(1);
}
else{
fprintf(stderr, "singular block diagonal matrix D for dsytrf in sba_Axb_BK() [D(%d, %d) is zero]\n", info, info);
return 0;
}
}
/* solve the system with the computed factorization */
F77_FUNC(dsytrs)("U", (int *)&m, (int *)&nrhs, a, (int *)&m, ipiv, x, (int *)&m, (int *)&info);
if(info!=0){
if(info<0){
fprintf(stderr, "argument %d of dsytrs illegal in sba_Axb_BK()\n", -info);
exit(1);
}
else{
fprintf(stderr, "unknown error for dsytrs in sba_Axb_BK()\n");
return 0;
}
}
return 1;
}
/*
* This function computes the inverse of a square matrix whose upper triangle
* is stored in A into its lower triangle using LU decomposition
*
* The function returns 0 in case of error (e.g. A is singular),
* 1 if successfull
*
* This function is often called repetitively to solve problems of identical
* dimensions. To avoid repetitive malloc's and free's, allocated memory is
* retained between calls and free'd-malloc'ed when not of the appropriate size.
* A call with NULL as the first argument forces this memory to be released.
*/
int sba_symat_invert_LU(double *A, int m)
{
static double *buf=NULL;
static int buf_sz=0, nb=0;
int a_sz, ipiv_sz, work_sz, tot_sz;
register int i, j;
int info, *ipiv;
double *a, *work;
if(A==NULL){
if(buf) free(buf);
buf=NULL;
buf_sz=0;
return 1;
}
/* calculate required memory size */
ipiv_sz=m;
a_sz=m*m;
if(!nb){
#ifndef SBA_LS_SCARCE_MEMORY
double tmp;
work_sz=-1; // workspace query; optimal size is returned in tmp
F77_FUNC(dgetri)((int *)&m, NULL, (int *)&m, NULL, (double *)&tmp, (int *)&work_sz, (int *)&info);
nb=((int)tmp)/m; // optimal worksize is m*nb
#else
nb=1; // min worksize is m
#endif /* SBA_LS_SCARCE_MEMORY */
}
work_sz=nb*m;
tot_sz=(a_sz + work_sz)*sizeof(double) + ipiv_sz*sizeof(int); /* should be arranged in that order for proper doubles alignment */
if(tot_sz>buf_sz){ /* insufficient memory, allocate a "big" memory chunk at once */
if(buf) free(buf); /* free previously allocated memory */
buf_sz=tot_sz;
buf=(double *)malloc(buf_sz);
if(!buf){
fprintf(stderr, "memory allocation in sba_symat_invert_LU() failed!\n");
exit(1);
}
}
a=buf;
work=a+a_sz;
ipiv=(int *)(work+work_sz);
/* store A (column major!) into a */
for(i=0; i<m; ++i)
for(j=i; j<m; ++j)
a[i+j*m]=a[j+i*m]=A[i*m+j]; // copy A's upper part to a's upper & lower
/* LU decomposition for A */
F77_FUNC(dgetrf)((int *)&m, (int *)&m, a, (int *)&m, ipiv, (int *)&info);
if(info!=0){
if(info<0){
fprintf(stderr, "argument %d of dgetrf illegal in sba_symat_invert_LU()\n", -info);
exit(1);
}
else{
fprintf(stderr, "singular matrix A for dgetrf in sba_symat_invert_LU()\n");
return 0;
}
}
/* (A)^{-1} from LU */
F77_FUNC(dgetri)((int *)&m, a, (int *)&m, ipiv, work, (int *)&work_sz, (int *)&info);
if(info!=0){
if(info<0){
fprintf(stderr, "argument %d of dgetri illegal in sba_symat_invert_LU()\n", -info);
exit(1);
}
else{
fprintf(stderr, "singular matrix A for dgetri in sba_symat_invert_LU()\n");
return 0;
}
}
/* store (A)^{-1} in A's lower triangle */
for(i=0; i<m; ++i)
for(j=0; j<=i; ++j)
A[i*m+j]=a[i+j*m];
return 1;
}
/*
* This function computes the inverse of a square symmetric positive definite
* matrix whose upper triangle is stored in A into its lower triangle using
* Cholesky factorization
*
* The function returns 0 in case of error (e.g. A is not positive definite or singular),
* 1 if successfull
*
* This function is often called repetitively to solve problems of identical
* dimensions. To avoid repetitive malloc's and free's, allocated memory is
* retained between calls and free'd-malloc'ed when not of the appropriate size.
* A call with NULL as the first argument forces this memory to be released.
*/
int sba_symat_invert_Chol(double *A, int m)
{
static double *buf=NULL;
static int buf_sz=0;
int a_sz, tot_sz;
register int i, j;
int info;
double *a;
if(A==NULL){
if(buf) free(buf);
buf=NULL;
buf_sz=0;
return 1;
}
/* calculate required memory size */
a_sz=m*m;
tot_sz=a_sz;
if(tot_sz>buf_sz){ /* insufficient memory, allocate a "big" memory chunk at once */
if(buf) free(buf); /* free previously allocated memory */
buf_sz=tot_sz;
buf=(double *)malloc(buf_sz*sizeof(double));
if(!buf){
fprintf(stderr, "memory allocation in sba_symat_invert_Chol() failed!\n");
exit(1);
}
}
a=(double *)buf;
/* store A into a; A is assumed symmetric, hence no transposition is needed */
for(i=0, j=a_sz; i<j; ++i)
a[i]=A[i];
/* Cholesky factorization for A; a's lower part corresponds to A's upper */
//F77_FUNC(dpotrf)("L", (int *)&m, a, (int *)&m, (int *)&info);
F77_FUNC(dpotf2)("L", (int *)&m, a, (int *)&m, (int *)&info);
/* error treatment */
if(info!=0){
if(info<0){
fprintf(stderr, "LAPACK error: illegal value for argument %d of dpotrf in sba_symat_invert_Chol()\n", -info);
exit(1);
}
else{
fprintf(stderr, "LAPACK error: the leading minor of order %d is not positive definite,\nthe factorization could not be completed for dpotrf in sba_symat_invert_Chol()\n", info);
return 0;
}