Debian Codegraph @

contrib/ms-sbvar/utilities_dw/matrix/bmatrix.c

raw
1 /*
2 * Copyright (C) 1996-2011 Daniel Waggoner
3 *
4 * This free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * It 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 * If you did not received a copy of the GNU General Public License
15 * with this software, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "bmatrix.h"
19 #include "dw_error.h"
20 #include "blas_lapack.h"
21 #include "dw_std.h"
22
23 #include <math.h>
24 #include <stdlib.h>
25 #include <memory.h>
26 #include <string.h>
27
28 /********************************************************************************
29 An (n x m) matrix x is in row major format if x(i,j)=x[i*n+j] and is in column
30 major format if x(i,j)=x[i+j*m].
31 ********************************************************************************/
32
33 /******************************************************************************/
34 /***************************** Uniary Operations ******************************/
35 /******************************************************************************/
36 /*
37 Assumes:
38 x : n-vector
39 y : n-vector
40 n : positive
41
42 Results:
43 x[i] = -y[i] for 0 <= i < n
44
45 Returns:
46 0 upon success
47
48 Notes:
49 x and y do not have to be distinct
50 */
51 int bNegative(PRECISION *x, PRECISION *y, int n)
52 {
53 while (--n >= 0) x[n]=-y[n];
54 return NO_ERR;
55 }
56
57 /*
58 Assumes:
59 x : n-vector
60 y : n-vector
61 n : positive
62
63 Results:
64 x[i] = fabs(y[i]) for 0 <= i < n
65
66 Returns:
67 0 upon success
68
69 Notes:
70 x and y do not have to be distinct
71 */
72 int bAbs(PRECISION *x, PRECISION *y, int n)
73 {
74 while (--n >= 0) x[n]=fabs(y[n]);
75 return NO_ERR;
76 }
77
78 /*
79 Assumes:
80 x : n x m matrix
81 y : m x n matrix
82 m : positive
83 n : positive
84 t : 0 or 1
85
86 Results:
87 x = y'
88
89 Returns:
90 0 upon success
91
92 Notes:
93 It t is one, both x and y are in column major format. If t is zero, both x
94 y are in row major format.
95 */
96 int bTranspose(PRECISION *x, PRECISION *y, int m, int n, int t)
97 {
98 int i, j, k;
99 if (t)
100 for (i=k=m*n-1; k >= 0; i--)
101 for (j=i; j >= 0; j-=m)
102 x[k--]=y[j];
103 else
104 for (i=k=m*n-1; k >= 0; i--)
105 for (j=i; j >= 0; j-=n)
106 x[k--]=y[j];
107 return NO_ERR;
108 }
109
110 /*
111 Assumes:
112 x : m x m matrix
113 m : positive
114
115 Results:
116 x = x'
117
118 Returns:
119 0 upon success.
120
121 Notes:
122 The major format (row or column) does not matter.
123 */
124 int bTransposeInPlace(PRECISION *x, int m)
125 {
126 PRECISION tmp;
127 int i, j;
128 for (j=m*m-2; j > 0; j+=i-1)
129 for (i=j-m+1; i >= 0; j--, i-=m)
130 {
131 tmp=x[i];
132 x[i]=x[j];
133 x[j]=tmp;
134 }
135 return NO_ERR;
136 }
137 /******************************************************************************/
138 /******************************************************************************/
139 /******************************************************************************/
140
141
142 /******************************************************************************/
143 /***************************** Addition Routines ******************************/
144 /******************************************************************************/
145 /*
146 Assumes:
147 x : n vector
148 y : n vector
149 z : n vector
150 n : positive
151
152 Results:
153 x = y + z
154
155 Returns:
156 0 upon success
157
158 Notes:
159 The vectors x, y and z do not have to be distinct
160 */
161 int bAddV(PRECISION *x, PRECISION *y, PRECISION *z, int n)
162 {
163 while (--n >= 0) x[n]=y[n]+z[n];
164 return NO_ERR;
165 }
166
167 /*
168 Assumes:
169 x : n-vector
170 y : n-vector
171 z : n-vector
172 n : positive
173
174 Results
175 x = y - z
176
177 Returns:
178 0 upon success
179
180 Notes:
181 The vectors x, y and z do not have to be distinct
182 */
183 int bSubtractV(PRECISION *x, PRECISION *y, PRECISION *z, int n)
184 {
185 while (--n >= 0) x[n]=y[n]-z[n];
186 return NO_ERR;
187 }
188
189 /*
190 Assumes:
191 x : m x n matrix
192 y : m x n matrix
193 z : m x n matrix
194 m : positive
195 n : positive
196 xt: 0 or 1
197 yt: 0 or 1
198 zt: 0 or 1
199
200 Results:
201 x = y + z
202
203 Returns:
204 0 upon success
205
206 Notes:
207 The matrices y and z do not have to be distinct. If xt, yt, or zt is 0 then
208 the matrix is in row major format. If xt, yt, or zt is 1 then the matrix is
209 in column major format.
210 */
211 int bAddM(PRECISION *x, PRECISION *y, PRECISION *z, int m, int n, int xt, int yt, int zt)
212 {
213 int i, j, k, s;
214 if (xt == yt)
215 if (yt == zt)
216 for (k=m*n-1; k >= 0; k--) x[k]=y[k]+z[k];
217 else
218 for (s=zt ? m : n, k=i=m*n-1; k >= 0; i--)
219 for (j=i; j >= 0; k--, j-=s)
220 x[k]=y[k]+z[j];
221 else
222 if (yt == zt)
223 for (s=yt ? m : n, k=i=m*n-1; k >= 0; i--)
224 for (j=i; j >= 0; k--, j-=s)
225 x[k]=y[j]+z[j];
226 else
227 for (s=yt ? m : n, k=i=m*n-1; k >= 0; i--)
228 for (j=i; j >= 0; k--, j-=s)
229 x[k]=y[j]+z[k];
230 return NO_ERR;
231 }
232
233 /*
234 Assumes:
235 x : m x n matrix
236 y : m x n matrix
237 z : m x n matrix
238 m : positive
239 n : positive
240 xt: 0 or 1
241 yt: 0 or 1
242 zt: 0 or 1
243
244 Results:
245 x = y - z
246
247 Returns:
248 0 upon success
249
250 Notes:
251 The matrices y and z do not have to be distinct. If xt, yt, or zt is 0 then
252 x, y, or z is in row major format. If xt, yt, or zt is 1 then x, y, or z is
253 in column major format.
254 */
255 int bSubtractM(PRECISION *x, PRECISION *y, PRECISION *z, int m, int n, int xt, int yt, int zt)
256 {
257 int i, j, k, s;
258 if (xt == yt)
259 if (yt == zt)
260 for (k=m*n-1; k >= 0; k--) x[k]=y[k]-z[k];
261 else
262 for (s=zt ? m : n, k=i=m*n-1; k >= 0; i--)
263 for (j=i; j >= 0; k--, j-=s)
264 x[k]=y[k]-z[j];
265 else
266 if (yt == zt)
267 for (s=yt ? m : n, k=i=m*n-1; k >= 0; i--)
268 for (j=i; j >= 0; k--, j-=s)
269 x[k]=y[j]-z[j];
270 else
271 for (s=yt ? m : n, k=i=m*n-1; k >= 0; i--)
272 for (j=i; j >= 0; k--, j-=s)
273 x[k]=y[j]-z[k];
274 return NO_ERR;
275 }
276
277 /*
278 Assumes:
279 x : m vector
280 a : scalar
281 y : m vector
282 b : scalar
283 z : m vector
284 m : positive
285
286 Results:
287 x = a*y + b*z
288
289 Returns:
290 0 upon success
291
292 Notes:
293 The vectors x, y and z do not have to be distinct
294 */
295 int bLinearCombinationV(PRECISION *x, PRECISION a, PRECISION *y, PRECISION b, PRECISION *z, int m)
296 {
297 while (--m >= 0) x[m]=a*y[m]+b*z[m];
298 return NO_ERR;
299 }
300
301 /*
302 Assumes:
303 x : m x n matrix
304 a : scalar
305 y : m x n matrix
306 b : scalar
307 z : m x n matrix
308 m : positive
309 n : positive
310 xt: 0 or 1
311 yt: 0 or 1
312 zt: 0 or 1
313
314 Results:
315 x = a*y + b*z
316
317 Returns:
318 0 upon success
319
320 Notes:
321 The vector y and z do not have to be distinct. If xt, yt, or zt is 0 then x,
322 y, or z is in row major format. If xt, yt, or zt is 1 then x, y, or z is in
323 column major format.
324 */
325 int bLinearCombinationM(PRECISION *x, PRECISION a, PRECISION *y, PRECISION b, PRECISION *z, int m, int n, int xt, int yt, int zt)
326 {
327 int i, j, k, s;
328 if (xt == yt)
329 if (yt == zt)
330 for (k=m*n-1; k >= 0; k--) x[k]=a*y[k]+b*z[k];
331 else
332 for (s=zt ? m : n, k=i=m*n-1; k >= 0; i--)
333 for (j=i; j >= 0; k--, j-=s)
334 x[k]=a*y[k]+b*z[j];
335 else
336 if (yt == zt)
337 for (s=yt ? m : n, k=i=m*n-1; k >= 0; i--)
338 for (j=i; j >= 0; k--, j-=s)
339 x[k]=a*y[j]+b*z[j];
340 else
341 for (s=yt ? m : n, k=i=m*n-1; k >= 0; i--)
342 for (j=i; j >= 0; k--, j-=s)
343 x[k]=a*y[j]+b*z[k];
344 return NO_ERR;
345 }
346 /******************************************************************************/
347 /******************************************************************************/
348 /******************************************************************************/
349
350
351 /******************************************************************************/
352 /******************************************************************************/
353 /******************************************************************************/
354 /*
355 Assumes:
356 x : n vector
357 y : n vector
358 n : positive
359
360 Results:
361 x[i] = s * y[i] for 0 <= i < n
362
363 Returns:
364 0 upon success
365
366 Notes:
367 x and y do not have to be distinct
368 */
369 int bMultiply(PRECISION *x, PRECISION *y, PRECISION s, int n)
370 {
371 while (--n >= 0) x[n]=s*y[n];
372 return NO_ERR;
373 }
374
375 /*
376 Assumes:
377 x : m x n matrix
378 y : m x p matrix
379 z : p x n matrix
380 a : scalar
381 b : scalar
382 m : positive integer
383 n : positive integer
384 p : positive integer
385 xt: 0 or 1
386 yt: 0 or 1
387 zt: 0 or 1
388
389 Results:
390 x = a*x + b*y*z
391
392 Returns:
393 0 upon success
394
395 Notes:
396 If xt, yt, or zt is one, then x, y, or z is in column major format. If xt,
397 yt, or zt is zero, then x, y, or z is in row major format. The matrices
398 y and z do not have to be distinct.
399 */
400 int bProductMM_Update(PRECISION *x, PRECISION *y, PRECISION *z, PRECISION a, PRECISION b, int m, int n, int p, int xt, int yt, int zt)
401 {
402 #if PRECISION_SIZE == 4
403 #define gemm sgemm
404 #else
405 #define gemm dgemm
406 #endif
407 char transy, transz;
408 blas_int dy, dz, bm=m, bn=n, bp=p;
409 if (xt)
410 {
411 if (yt) {transy='N'; dy=bm;} else {transy='T'; dy=bp;}
412 if (zt) {transz='N'; dz=bp;} else {transz='T'; dz=bn;}
413 gemm(&transy,&transz,&bm,&bn,&bp,&b,y,&dy,z,&dz,&a,x,&bm);
414 }
415 else
416 {
417 if (yt) {transy='T'; dy=bm;} else {transy='N'; dy=bp;}
418 if (zt) {transz='T'; dz=bp;} else {transz='N'; dz=bn;}
419 gemm(&transz,&transy,&bn,&bm,&bp,&b,z,&dz,y,&dy,&a,x,&bn);
420 }
421 return NO_ERR;
422 #undef gemm
423 }
424
425 /*
426 Assumes:
427 x : m vector
428 y : n vector
429 z : m x n matrix
430 a : scalar
431 b : scalar
432 m : positive integer
433 n : positive integer
434 zt: 0 or 1
435
436 Results:
437 x = a*x + b*z*y
438
439 Returns:
440 0 upon success
441
442 Notes:
443 If zt is one, then z is in column major format. If zt is zero, then z is in
444 row major format.
445 */
446 int bProductMV_Update(PRECISION *x, PRECISION *y, PRECISION *z, PRECISION a, PRECISION b, int m, int n, int zt)
447 {
448 #if PRECISION_SIZE == 4
449 #define gemv sgemv
450 #else
451 #define gemv dgemv
452 #endif
453 char trans;
454 blas_int inc=1, bm=m, bn=n;
455 if (zt)
456 {
457 trans='N';
458 gemv(&trans,&bm,&bn,&b,z,&bm,y,&inc,&a,x,&inc);
459 }
460 else
461 {
462 trans='T';
463 gemv(&trans,&bn,&bm,&b,z,&bn,y,&inc,&a,x,&inc);
464 }
465 return NO_ERR;
466 #undef gemv
467 }
468
469 /*
470 Assumes:
471 x : m x n matrix
472 y : n x n triangular matrix
473 m : positive integer
474 n : positive integer
475 u : 0 or 1
476 xt: 0 or 1
477 yt: 0 or 1
478
479 Results:
480
481 x = x*y
482
483 x is overwritten with x*y.
484
485 Returns:
486 0 upon success
487
488 Notes:
489 If u is one, then y is upper triangular. If u is zero, then y is lower
490 triangular. If xt, yt, or zt is one, then x, y, or z is in column major
491 format. If xt, yt, or zt is zero, then x, y, or z is in row major format.
492 */
493 int bProductMT(PRECISION *x, PRECISION *y, int m, int n, int u, int xt, int yt)
494 {
495 #if PRECISION_SIZE == 4
496 #define trmm strmm
497 #else
498 #define trmm dtrmm
499 #endif
500
501 char side, uplo, transa, diag='N';
502 PRECISION alpha=1.0;
503 blas_int bm=m, bn=n;
504
505 if (xt)
506 {
507 side='R';
508 if (yt) {transa='N'; uplo=u ? 'U' : 'L';} else {transa='T'; uplo=u ? 'L' : 'U';}
509 trmm(&side,&uplo,&transa,&diag,&bm,&bn,&alpha,y,&bn,x,&bm);
510 }
511 else
512 {
513 side='L';
514 if (yt) {transa='T'; uplo=u ? 'U' : 'L';} else {transa='N'; uplo=u ? 'L' : 'U';}
515 trmm(&side,&uplo,&transa,&diag,&bn,&bm,&alpha,y,&bn,x,&bn);
516 }
517 return NO_ERR;
518
519 #undef trmm
520 }
521
522 /*
523 Assumes:
524 x : m x n matrix
525 y : m x m triangular matrix
526 m : positive integer
527 n : positive integer
528 u : 0 or 1
529 xt: 0 or 1
530 yt: 0 or 1
531
532 Results:
533
534 x = y*x
535
536 x is overwritten with y*x.
537
538 Returns:
539 0 upon success
540
541 Notes:
542 If u is one, then y is upper triangular. If u is zero, then y is lower
543 triangular. If xt, yt, or zt is one, then x, y, or z is in column major
544 format. If xt, yt, or zt is zero, then x, y, or z is in row major format.
545 */
546 int bProductTM(PRECISION *x, PRECISION *y, int m, int n, int u, int xt, int yt)
547 {
548 #if PRECISION_SIZE == 4
549 #define trmm strmm
550 #else
551 #define trmm dtrmm
552 #endif
553
554 char side, uplo, transa, diag='N';
555 PRECISION alpha=1.0;
556 blas_int bm=m, bn=n;
557
558 if (xt)
559 {
560 side='L';
561 if (yt) {transa='N'; uplo=u ? 'U' : 'L';} else {transa='T'; uplo=u ? 'L' : 'U';}
562 trmm(&side,&uplo,&transa,&diag,&bm,&bn,&alpha,y,&bm,x,&bm);
563 }
564 else
565 {
566 side='R';
567 if (yt) {transa='T'; uplo=u ? 'U' : 'L';} else {transa='N'; uplo=u ? 'L' : 'U';}
568 trmm(&side,&uplo,&transa,&diag,&bn,&bm,&alpha,y,&bm,x,&bn);
569 }
570 return NO_ERR;
571
572 #undef trmm
573 }
574 /******************************************************************************/
575 /******************************************************************************/
576 /******************************************************************************/
577
578 /******************************************************************************/
579 /***************************** LU Decompositions ******************************/
580 /******************************************************************************/
581 /*
582 Assumes
583 b : m x n matrix
584 a : m x m matrix
585 m : positive
586 n : positive
587 bt : 0 or 1
588 at : 0 or 1
589
590 Results:
591 Solves
592
593 a*x = b
594
595 x over writes b.
596
597 Returns
598 NO_ERR - success
599 SING_ERR - a was singular to machine precision.
600
601 Note:
602 If bt or at is one, then b or a is in column major format. If bt or at is
603 zero, then b or a is in row major format. If m is equal to n and bt is
604 equal to at, then b and a do not have to distinct.
605 */
606 int bSolveLU(PRECISION *b, PRECISION *a, int m, int n, int bt, int at)
607 {
608 #if PRECISION_SIZE == 4
609 #define gesv sgesv
610 #else
611 #define gesv dgesv
612 #endif
613
614 PRECISION *pa, *pb;
615 lapack_int info, *p, bm=m, bn=n;
616 if (bt)
617 pb=b;
618 else
619 {
620 if (!(pb=(PRECISION*)dw_malloc(m*n*sizeof(PRECISION)))) return MEM_ERR;
621 bTranspose(pb,b,m,n,bt);
622 }
623 pa=(PRECISION*)dw_malloc(m*m*sizeof(PRECISION));
624 p=(lapack_int*)dw_malloc(m*sizeof(lapack_int));
625 if (pa && p)
626 {
627 if (at)
628 memcpy(pa,a,m*m*sizeof(PRECISION));
629 else
630 bTranspose(pa,a,m,m,at);
631 gesv(&bm,&bn,pa,&bm,p,pb,&bm,&info);
632 if (!bt)
633 {
634 bTranspose(b,pb,m,n,1);
635 dw_free(pb);
636 }
637 dw_free(pa);
638 dw_free(p);
639 return (info < 0) ? BLAS_LAPACK_ERR : (info > 0) ? SING_ERR : NO_ERR;
640 }
641 else
642 {
643 if (pa) dw_free(pa);
644 if (p) dw_free(p);
645 if (b != pb) dw_free(pb);
646 return MEM_ERR;
647 }
648
649 #undef gesv
650 }
651
652 /*
653 Assumes
654 p : integer array of length at least q=min(m,n)
655 x : mn matrix
656 m : positive
657 n : positive
658 xt : 0 or 1
659
660 Results:
661 Computes the LU decomposition of A with partial pivoting. The LU
662 decomposition of a matrix A is
663
664 A = P * L * U
665
666 where P is a (m x m) permutation matrix, L is a (m x q) lower triangular
667 matrix with ones on the diagonal, U is a (q x n) upper triangular matrix,
668 and q=min(m,n). These matrices are stored as follows.
669
670 U is stored in the upper part of x, including the diagonal.
671
672 L is stored in the lower part of x. The diagonal of L is not stored.
673
674 The matrix P is defined by
675
676 P = P(0,p[0])*P(1,p[1])*...*P(q-1,p[q-1])
677
678 where P(r,s) is the (m x m) matrix obtained by permuting the rth and sth
679 rows of the (m x m) identity matrix. It is assumed that i <= p[i] < m.
680
681 Returns
682 NO_ERR - success
683 BLAS_LAPACK_ERR - error in one of the arguments passed to getrf.
684 SING_ERR - x was singular to machine precision. LU decomposition is still
685 computed and returned.
686
687 Notes
688 Only q elements of p are set. The conventions of LAPACK uses base 1 arrays.
689 Thus to convert the permutation returned by getrf, 1 must be subtracted from
690 each element of p. If xt is one, then x is in row major format as is the
691 returned matrix. If xt is zero, then x is in column major format as is the
692 returned matrix.
693 */
694 int bLU(int *p, PRECISION *x, int m, int n, int xt)
695 {
696 #if PRECISION_SIZE == 4
697 #define getrf sgetrf
698 #else
699 #define getrf dgetrf
700 #endif
701
702 PRECISION *y;
703 int i, q=(m < n) ? m : n;
704 lapack_int *pp, info, bm=m, bn=n;
705 if (!(pp=(lapack_int*)dw_malloc(q*sizeof(lapack_int)))) return MEM_ERR;
706
707 if (xt)
708 getrf(&bm,&bn,x,&bm,pp,&info);
709 else
710 {
711 if (!( y=(PRECISION*)dw_malloc(m*n*sizeof(PRECISION)))) return MEM_ERR;
712 bTranspose(y,x,m,n,0);
713 getrf(&bm,&bn,y,&bm,pp,&info);
714 bTranspose(x,y,m,n,1);
715 dw_free(y);
716 }
717 for (i=q-1; i >= 0; i--) p[i]=pp[i]-1;
718 dw_free(pp);
719 return (info < 0) ? BLAS_LAPACK_ERR : (info > 0) ? SING_ERR : NO_ERR;
720
721 #undef getrf
722 }
723
724 /*
725 Assumes
726 a : m x m triangular matrix.
727 b : m x n matrix
728 m : positive integer
729 n : positive integer
730 u : 0 or 1
731 at : 0 or 1
732 bt : 0 or 1
733
734 Results:
735 Solves the system
736
737 a*x = b
738
739 The solution x is stored in b.
740
741 Returns
742 0 (NO_ERR) - success
743 SING_ERR - a is singular
744
745 Notes
746 The matrix a is assumed to be triangular. If u is 1, then a is upper
747 triangular and if u is 0 then a is lower triangular. The elements of a that
748 should be zero are not accessed.
749
750 If at or bt is 1, then a or b is in column major format. If at or bt is 0,
751 then a or b is in row major format.
752 */
753 int bSolveTriangular(PRECISION *a, PRECISION *b, int m, int n, int u, int at, int bt)
754 {
755 #if PRECISION_SIZE == 4
756 #define trsm strsm
757 #else
758 #define trsm dtrsm
759 #endif
760
761 PRECISION alpha=1.0;
762 char diag='N', side, transa, uplo;
763 int j;
764 blas_int bi, bm=m, bn=n;
765
766 // Is exactly singular - trsm does not check
767 for (j=m*m-1; j >= 0; j-=m+1)
768 if (a[j] == 0)
769 return SING_ERR;
770
771 if (bt)
772 {
773 bi=bm; side='L';
774 if (at) { transa='N'; uplo=u ? 'U' : 'L'; } else { transa='T'; uplo=u ? 'L' : 'U';}
775 }
776 else
777 {
778 bi=bn; bn=bm; side='R';
779 if (at) { transa='T'; uplo=u ? 'U' : 'L'; } else { transa='N'; uplo=u ? 'L' : 'U'; }
780 }
781
782 trsm(&side,&uplo,&transa,&diag,&bi,&bn,&alpha,a,&bm,b,&bi);
783
784 return NO_ERR;
785
786 #undef trsm
787 }
788
789 /*
790 Assumes
791 a : m x m triangular matrix with ones along the diagonal.
792 b : m x n matrix
793 m : positive integer
794 n : positive integer
795 u : 0 or 1
796 at : 0 or 1
797 bt : 0 or 1
798
799 Results:
800 Solves the system
801
802 a*x = b
803
804 The solution x is stored in b.
805
806 Returns
807 0 (NO_ERR) - success
808 SING_ERR - a is singular
809
810 Notes
811 The matrix a is assumed to be triangular. If u is 1, then a is upper
812 triangular and if u is 0 then a is lower triangular. The elements of a that
813 should be zero or one are not accessed.
814
815 If at or bt is 1, then a or b is in column major format. If at or bt is 0,
816 then a or b is in row major format.
817 */
818 int bSolveUnitTriangular(PRECISION *a, PRECISION *b, int m, int n, int u, int at, int bt)
819 {
820 #if PRECISION_SIZE == 4
821 #define trsm strsm
822 #else
823 #define trsm dtrsm
824 #endif
825
826 PRECISION alpha=1.0;
827 char diag='U', side, transa, uplo;
828 blas_int bi, bm=m, bn=n;
829
830 if (bt)
831 {
832 bi=bm; side='L';
833 if (at) { transa='N'; uplo=u ? 'U' : 'L'; } else { transa='T'; uplo=u ? 'L' : 'U';}
834 }
835 else
836 {
837 bi=bn; bn=bm; side='R';
838 if (at) { transa='T'; uplo=u ? 'U' : 'L'; } else { transa='N'; uplo=u ? 'L' : 'U'; }
839 }
840
841 trsm(&side,&uplo,&transa,&diag,&bi,&bn,&alpha,a,&bm,b,&bi);
842
843 return NO_ERR;
844
845 #undef trsm
846 }
847
848 /*
849 Assumes
850 x : m x n matrix
851 y : m x n matrix
852 p : integer array of length n representing (n x n) permutation matrix.
853 m : positive
854 n : positive
855 yt: 0 or 1
856 pt: 0 or 1
857
858 Results
859 x = y * p or x = y * p'
860
861 Returns
862 NO_ERR
863
864 Notes:
865 The array p defines the permutation that maps i to p[i].
866
867 If yt is one, then both x and y are in column major format. If yt is zero
868 then both x and y are in row major format.
869
870 If pt is one, then x = y * p' and if pt is zero then x = y * p.
871 */
872 int bPermuteMatrix(PRECISION *x, PRECISION *y, int *p, int m, int n, int yt, int pt)
873 {
874 int j, kx, ky;
875 if (m == 1)
876 if (pt)
877 for (j=n-1; j >= 0; j--) x[p[j]]=y[j];
878 else
879 for (j=n-1; j >= 0; j--) x[j]=y[p[j]];
880 else
881 if (yt)
882 if (pt)
883 for (j=n-1; j >= 0; j--)
884 memcpy(x+p[j]*m,y+j*m,m*sizeof(PRECISION));
885 else
886 for (j=n-1; j >= 0; j--)
887 memcpy(x+j*m,y+p[j]*m,m*sizeof(PRECISION));
888 else
889 if (pt)
890 for (m=(m-1)*n, j=n-1; j >= 0; j--)
891 for (ky=m+j, kx=m+p[j]; kx >= 0; ky-=n, kx-=n) x[kx]=y[ky];
892 else
893 for (m=(m-1)*n, j=n-1; j >= 0; j--)
894 for (ky=m+p[j], kx=m+j; kx >= 0; ky-=n, kx-=n) x[kx]=y[ky];
895 return NO_ERR;
896 }
897
898 /*
899 Assumes
900 p : integer array of length q with 0 <= p[i] < m for all 0 <= i < q.
901 y : m x n matrix
902 m : positive
903 n : positive
904 q : positive
905 pt: 0 or 1
906 yt: 0 or 1
907
908 Results
909 (1) y = p * y or (2) y = p' * y
910
911 Returns
912 NO_ERR
913
914 Notes:
915 The array p defines a permutation matrix by
916
917 P = P(0,p[0])*P(1,p[1])*...*P(q-1,p[q-1])
918
919 where P(r,s) is the m x m matrix obtained by permuting the rth and sth rows
920 of the m x m identity matrix. Note that
921
922 P' = P(q-1,p[q-1])*...*P(0,p[0])*P(1,p[1])
923
924 If yt is one, y is in column major format and if yt is zero, y is in row
925 major format. If pt is one, then y = p * y and if pt is zero then
926 y = p' * y.
927 */
928 int bPermutationMultiply(int *p, PRECISION *y, int m, int n, int q, int pt, int yt)
929 {
930 int i, j, k, pk;
931 PRECISION tmp;
932 if (yt)
933 if (pt)
934 for (j=0; j < q; j++)
935 {
936 if (j != p[j])
937 for (i=(n-1)*m; i >= 0; i-=m)
938 {
939 tmp=y[i+j];
940 y[i+j]=y[i+p[j]];
941 y[i+p[j]]=tmp;
942 }
943 }
944 else
945 for (j=q-1; j >= 0; j--)
946 {
947 if (j != p[j])
948 for (i=(n-1)*m; i >= 0; i-=m)
949 {
950 tmp=y[i+j];
951 y[i+j]=y[i+p[j]];
952 y[i+p[j]]=tmp;
953 }
954 }
955 else
956 if (pt)
957 for (i=0; i < q; i++)
958 {
959 if (i != p[i])
960 {
961 k=i*n;
962 pk=p[i]*n;
963 for (j=n-1; j >= 0; j--)
964 {
965 tmp=y[k+j];
966 y[k+j]=y[pk+j];
967 y[pk+j]=tmp;
968 }
969 }
970 }
971 else
972 for (i=q-1; i >= 0; i--)
973 {
974 if (i != p[i])
975 {
976 k=i*n;
977 pk=p[i]*n;
978 for (j=n-1; j >= 0; j--)
979 {
980 tmp=y[k+j];
981 y[k+j]=y[pk+j];
982 y[pk+j]=tmp;
983 }
984 }
985 }
986 return NO_ERR;
987 }
988
989 /*
990 Assumes
991 x : m x m matrix
992 p : integer array of length q with i <= p[i] < m for all 0 <= i < m.
993 m : positive
994 q : positive
995 xt: 0 or 1
996
997 Results
998 Computes the permutation matrix associated with the array p.
999
1000 Returns
1001 NO_ERR
1002
1003 Notes
1004 The array p defines a permutation matrix by
1005
1006 P = P(0,p[0])*P(1,p[1])*...*P(q-1,p[q-1])
1007
1008 where P(r,s) is the m x m matrix obtained by permuting the rth and sth rows
1009 of the m x m identity matrix. If xt is one, x is in column major format and
1010 if xt is zero, x is in row major format.
1011 */
1012 int bPermutation(PRECISION *x, int *p, int m, int q, int xt)
1013 {
1014 int i, j, k;
1015 for (k=m*m-1; k >= 0; k--) x[k]=0.0;
1016 if (xt)
1017 for (j=m-1; j >= 0; j--)
1018 {
1019 if (j < q)
1020 {
1021 k=j-1;
1022 i=p[j];
1023 }
1024 else
1025 {
1026 k=q-1;
1027 i=j;
1028 }
1029 for ( ; k >= 0; k--) if (i == p[k]) i=k;
1030 x[i+j*m]=1.0;
1031 }
1032 else
1033 for (j=m-1; j >= 0; j--)
1034 {
1035 if (j < q)
1036 {
1037 k=j-1;
1038 i=p[j];
1039 }
1040 else
1041 {
1042 k=q-1;
1043 i=j;
1044 }
1045 for ( ; k >= 0; k--) if (i == p[k]) i=k;
1046 x[i*m+j]=1.0;
1047 }
1048 return NO_ERR;
1049 }
1050 /******************************************************************************/
1051 /******************************************************************************/
1052 /******************************************************************************/
1053
1054
1055 /******************************************************************************/
1056 /************************ Singular Value Decomposition ************************/
1057 /******************************************************************************/
1058 /*
1059 Assumes
1060 U : array of length m*m (compact=0) or m*q (compact=1) or null
1061 d : array of length q=min(m,n)
1062 V : array of length n*n (compact=0) or n*q (compact=1) or null
1063 A : array of length m*n
1064 m : positive
1065 n : positive
1066 ut : 0 or 1
1067 vt : 0 or 1
1068 at : 0 or 1
1069 compact : 0 or 1
1070
1071 Returns
1072 NO_ERR : success
1073 MEM_ERR : out of memory
1074
1075 Results
1076 Finds matrices U and V with orthonormal columns and a diagonal matrix
1077 D=diag(d) with non-negative diagonal such that A = U*D*V'. The matrix D is
1078 m x n if compact = 0 and is q x q if compact = 1. The elements of d are in
1079 descending order. The flags ut, vt, and at determine the format of U, V,
1080 and A. A value of 1 indicates column major format and a value of 0
1081 indicates row major format. If either U or V is null, then it is not
1082 computed.
1083
1084 Notes
1085 If A=U, U and A must be of the same size and ut=at. If A=V, then V and A
1086 must be of the same size and vt=at. It cannot be the case that U=V unless
1087 both are null.
1088
1089 New memory will always be allocated for A, so the contents of this array
1090 will not change. A minimum amount of memory copying will occur if ut == 1
1091 and vt == 0, so that U is in column major format and V is in row major
1092 format.
1093 */
1094 int bSVD(PRECISION *U, PRECISION *d, PRECISION *V, PRECISION *A, int m, int n, int ut, int vt, int at, int compact)
1095 {
1096 #if (PRECISION_SIZE == 4)
1097 #define gesvd sgesvd
1098 #else
1099 #define gesvd dgesvd
1100 #endif
1101
1102 char jobu, jobv;
1103 int qu, qv, err=NO_ERR;
1104 lapack_int k, info, bqv, bm=m, bn=n;
1105 PRECISION *A_, *U_, *V_, *work, opt_size;
1106
1107 if (!(A_=(PRECISION*)dw_malloc(m*n*sizeof(PRECISION)))) return MEM_ERR;
1108 if (at)
1109 memcpy(A_,A,m*n*sizeof(PRECISION));
1110 else
1111 bTranspose(A_,A,m,n,at);
1112
1113 if (!U)
1114 {
1115 jobu='N';
1116 qu=0;
1117 U_=(PRECISION*)NULL;
1118 }
1119 else
1120 {
1121 if (compact && (m > n))
1122 {
1123 jobu='S';
1124 qu=n;
1125 }
1126 else
1127 {
1128 jobu='A';
1129 qu=m;
1130 }
1131 if (ut)
1132 U_=U;
1133 else
1134 if (!(U_=(PRECISION*)dw_malloc(m*qu*sizeof(PRECISION))))
1135 {
1136 dw_free(A_);
1137 return MEM_ERR;
1138 }
1139 }
1140
1141 if (!V)
1142 {
1143 qv=1;
1144 jobv='N';
1145 V_=(PRECISION*)NULL;
1146 }
1147 else
1148 {
1149 if (compact && (m < n))
1150 {
1151 jobv='S';
1152 qv=m;
1153 }
1154 else
1155 {
1156 jobv='A';
1157 qv=n;
1158 }
1159 if (!vt)
1160 V_=V;
1161 else
1162 if (!(V_=(PRECISION*)dw_malloc(n*qv*sizeof(PRECISION))))
1163 {
1164 dw_free(A_);
1165 if (U_ && (U_ != U)) dw_free(U_);
1166 return MEM_ERR;
1167 }
1168 }
1169
1170 // compute singular value decomposition
1171 k=-1;
1172 bqv=qv;
1173 gesvd(&jobu,&jobv,&bm,&bn,A_,&bm,d,U_,&bm,V_,&bqv,&opt_size,&k,&info);
1174 if (info)
1175 err=BLAS_LAPACK_ERR;
1176 else
1177 if (!(work=(PRECISION*)dw_malloc((k=(lapack_int)opt_size)*sizeof(PRECISION))))
1178 err=MEM_ERR;
1179 else
1180 {
1181 gesvd(&jobu,&jobv,&bm,&bn,A_,&bm,d,U_,&bm,V_,&bqv,work,&k,&info);
1182 dw_free(work);
1183 if (info)
1184 err=BLAS_LAPACK_ERR;
1185 else
1186 {
1187 if (U_ != U) bTranspose(U,U_,m,qu,1);
1188 if (V_ != V) bTranspose(V,V_,qv,n,1);
1189 err=NO_ERR;
1190 }
1191 }
1192
1193 dw_free(A_);
1194 if (U_ && (U_ != U)) dw_free(U_);
1195 if (V_ && (V_ != V)) dw_free(V_);
1196 return err;
1197 }
1198 /******************************************************************************/
1199 /******************************************************************************/
1200 /******************************************************************************/
1201
1202 /******************************************************************************/
1203 /***************************** QR Decompositions ******************************/
1204 /******************************************************************************/
1205 /*
1206 Assumes
1207 Q : m x q matrix or null pointer
1208 R : q x n
1209 X : m x n matrix
1210 n : positive
1211 m : positive
1212 q : m or min(m,n)
1213 qt : 0 or 1
1214 rt : 0 or 1
1215 xt : 0 or 1
1216
1217 Returns
1218 NO_ERR : Success
1219 MEM_ERR : Out of memory
1220
1221 Results
1222 Finds an orthogonal matrix Q and an upper triangular matrix R
1223 such that
1224
1225 X = Q * R
1226
1227 The matrix Q is computed only if it is not null.
1228
1229 Notes
1230 The matrices X and R do not have to be distinct. If X == R, then it must
1231 be the case that m == q and rt == xt.
1232 */
1233 int bQR(PRECISION *Q, PRECISION *R, PRECISION *X, int m, int n, int q, int qt, int rt, int xt)
1234 {
1235 #if (PRECISION_SIZE == 4)
1236 #define geqrf sgeqrf
1237 #define orgqr sorgqr
1238 #define gelqf sgelqf
1239 #define orglq sorglq
1240 #else
1241 #define geqrf dgeqrf
1242 #define orgqr dorgqr
1243 #define gelqf dgelqf
1244 #define orglq dorglq
1245 #endif
1246
1247 int i, j, k, l;
1248 lapack_int lwork, info, p=(m < n) ? m : n, bm=m, bn=n, bq=q;
1249 PRECISION *tau, *work, *ptr, opt_size;
1250 if (!(tau=(PRECISION*)dw_malloc(p*sizeof(PRECISION)))) return MEM_ERR;
1251 if (xt)
1252 {
1253 lwork=-1;
1254
1255 geqrf(&bm,&bn,X,&bm,tau,&opt_size,&lwork,&info);
1256
1257 if (!(work=(PRECISION*)dw_malloc((lwork=(lapack_int)opt_size)*sizeof(PRECISION))))
1258 {
1259 dw_free(tau);
1260 return MEM_ERR;
1261 }
1262
1263 geqrf(&bm,&bn,X,&bm,tau,work,&lwork,&info);
1264
1265 /*********************************************/
1266 /*** MKL Problem *****************************/
1267 /*** does not seem to be in latest version ***
1268 if (info)
1269 {
1270 printf("Unexpected error: info= %d lwork= %d m = %d n = %d",info,lwork,m,n);
1271 dw_free(work);
1272 work=(PRECISION*)dw_malloc((lwork=(m > n) ? m*m : n*n)*64*sizeof(PRECISION));
1273 geqrf(&m,&n,X,&m,tau,work,&lwork,&info);
1274 printf(" minimal = %d\n",(int)work[0]);
1275 }
1276 //else
1277 // printf("lwork = %d optimal= %d m %d n %d\n",lwork,(int)work[0],m,n);
1278 *********************************************/
1279
1280 dw_free(work);
1281 if (info)
1282 {
1283 dw_free(tau);
1284 return ARG_ERR;
1285 }
1286 if (Q)
1287 {
1288 if (qt)
1289 ptr=Q;
1290 else
1291 if (!(ptr=(PRECISION*)dw_malloc(m*q*sizeof(PRECISION))))
1292 {
1293 dw_free(tau);
1294 return MEM_ERR;
1295 }
1296 memcpy(ptr,X,m*p*sizeof(PRECISION));
1297 lwork=-1;
1298
1299 orgqr(&bm,&bq,&p,ptr,&bm,tau,&opt_size,&lwork,&info);
1300
1301 if (!(work=(PRECISION*)dw_malloc((lwork=(lapack_int)opt_size)*sizeof(PRECISION))))
1302 {
1303 if (!qt) dw_free(ptr);
1304 dw_free(tau);
1305 return MEM_ERR;
1306 }
1307
1308 orgqr(&bm,&bq,&p,ptr,&bm,tau,work,&lwork,&info);
1309
1310 dw_free(work);
1311 if (!qt)
1312 {
1313 bTranspose(Q,ptr,m,q,1);
1314 dw_free(ptr);
1315 }
1316 dw_free(tau);
1317 if (info) return ARG_ERR;
1318 }
1319 else
1320 dw_free(tau);
1321 if (R != X)
1322 if (rt)
1323 for (k=q*n, j=n-1; j >= 0; j--)
1324 {
1325 for (i=q-1; i > j; i--) R[--k]=0.0;
1326 for (l=i+j*m; i >= 0; i--) R[--k]=X[l--];
1327 }
1328 else
1329 for (k=q*n, i=q-1; i >= 0; i--)
1330 {
1331 for (l=i+n*m, j=n-1; j >= i; j--) R[--k]=X[l-=m];
1332 for ( ; j >= 0; j--) R[--k]=0.0;
1333 }
1334 else
1335 {
1336 for (j=p-1; j >= 0; j--)
1337 for (k=m*(j+1), i=m-1; i > j; i--) X[--k]=0.0;
1338 }
1339 }
1340 else
1341 {
1342 lwork=-1;
1343
1344 gelqf(&bn,&bm,X,&bn,tau,&opt_size,&lwork,&info);
1345
1346 if (!(work=(PRECISION*)dw_malloc((lwork=(lapack_int)opt_size)*sizeof(PRECISION))))
1347 {
1348 dw_free(tau);
1349 return MEM_ERR;
1350 }
1351
1352 gelqf(&bn,&bm,X,&bn,tau,work,&lwork,&info);
1353
1354 dw_free(work);
1355 if (info)
1356 {
1357 dw_free(tau);
1358 return ARG_ERR;
1359 }
1360 if (Q)
1361 {
1362 if (!qt)
1363 ptr=Q;
1364 else
1365 if (!(ptr=(PRECISION*)dw_malloc(m*q*sizeof(PRECISION))))
1366 {
1367 dw_free(tau);
1368 return MEM_ERR;
1369 }
1370 if (q == n)
1371 memcpy(ptr,X,m*n*sizeof(PRECISION));
1372 else
1373 if (m < n)
1374 for (k=q*m, j=m-1; j >= 0; j--)
1375 for (l=p+j*n, i=p-1; i >= 0; i--)
1376 ptr[--k]=X[--l];
1377 else
1378 for (l=n*m, j=m-1; j >= 0; j--)
1379 for (k=p+j*q, i=p-1; i >= 0; i--)
1380 ptr[--k]=X[--l];
1381 lwork=-1;
1382
1383 orglq(&bq,&bm,&p,ptr,&bq,tau,&opt_size,&lwork,&info);
1384
1385 if (!(work=(PRECISION*)dw_malloc((lwork=(lapack_int)opt_size)*sizeof(PRECISION))))
1386 {
1387 if (!qt) dw_free(ptr);
1388 dw_free(tau);
1389 return MEM_ERR;
1390 }
1391
1392 orglq(&bq,&bm,&p,ptr,&bq,tau,work,&lwork,&info);
1393
1394 dw_free(work);
1395 if (qt)
1396 {
1397 bTranspose(Q,ptr,q,m,1);
1398 dw_free(ptr);
1399 }
1400 dw_free(tau);
1401 if (info) return ARG_ERR;
1402 }
1403 else
1404 dw_free(tau);
1405 if (R != X)
1406 if (rt)
1407 for (k=n*q, i=n-1; i >= 0; i--)
1408 {
1409 for (j=q-1; j > i; j--) R[--k]=0.0;
1410 for (l=i+j*n; j >= 0; l-=n, j--) R[--k]=X[l];
1411 }
1412 else
1413 for (k=n*q-1, j=q-1; j >= 0; j--)
1414 {
1415 for (i=n-1; i >= j; k--, i--) R[k]=X[k];
1416 for ( ; i >= 0; k--, i--) R[k]=0.0;
1417 }
1418 else
1419 {
1420 for (i=p-1; i >= 0; i--)
1421 for (k=i+m*n, j=m-1; j > i; j--) X[k-=n]=0.0;
1422 }
1423 }
1424 return NO_ERR;
1425
1426 #undef geqrf
1427 #undef orgqr
1428 #undef gelqf
1429 #undef orglq
1430 }
1431 /******************************************************************************/
1432 /******************************************************************************/
1433 /******************************************************************************/
1434
1435 /******************************************************************************/
1436 /********************** Generalized Schur Decompositions **********************/
1437 /******************************************************************************/
1438 /*
1439 Assumes
1440 Q : array of length n*n or null
1441 Z : array of length n*n or null
1442 S : array of length n*n
1443 T : array of length n*n
1444 A : array of length n*n
1445 B : array of length n*n
1446 n : positive integer
1447 qt : 0 or 1
1448 zt : 0 or 1
1449 st : 0 or 1
1450 tt : 0 or 1
1451 at : 0 or 1
1452 bt : 0 or 1
1453 alpha_i : array of length n or null
1454 alpha_i : array of length n or null
1455 beta : array of length n or null
1456
1457 Returns
1458 NO_ERR : success
1459 MEM_ERR : out of memory
1460 BLAS_LAPACK_ERR : blas or lapack error
1461
1462 Results
1463 Finds orthogonal matrices Q and Z, an block upper triangular matrix S with
1464 1 x 1 or 2 x 2 blocks along the diagonal, an upper triangular matrix T such
1465 that
1466
1467 A = Q*S*Z' and B = Q*T*Z
1468
1469 If either Q or Z is null, then it is not returned.
1470
1471 Notes
1472 The flags qt, zt, st, tt, at, and bt control the format of the matrices Q,
1473 Z, S, T, A, and B. A value of 1 indicate column major format and a value of
1474 0 indictes row major format. All matrices must be distinct with the
1475 exception A=S or B=T.
1476 */
1477 int bGeneralizedSchur_real(PRECISION *Q, PRECISION *Z, PRECISION *S, PRECISION *T, PRECISION *A, PRECISION *B,
1478 int n, int qt, int zt, int st, int tt, int at, int bt,
1479 PRECISION *alpha_r, PRECISION *alpha_i, PRECISION *beta)
1480 {
1481 #if (PRECISION_SIZE == 4)
1482 #define gges sgges
1483 #else
1484 #define gges dgges
1485 #endif
1486
1487 // not yet converted to lapack_int
1488 char jobvsl, jobvsr, sort='N';
1489 lapack_int lwork, simd, info, bn=n;
1490 int rtrn;
1491 PRECISION *work, size, *palpha_r, *palpha_i, *pbeta;
1492
1493 jobvsl=Q ? 'V' : 'N';
1494 jobvsr=Z ? 'V' : 'N';
1495 palpha_r=alpha_r ? alpha_r : (PRECISION*)dw_malloc(n*sizeof(PRECISION));
1496 palpha_i=alpha_i ? alpha_i : (PRECISION*)dw_malloc(n*sizeof(PRECISION));
1497 pbeta=beta ? beta : (PRECISION*)dw_malloc(n*sizeof(PRECISION));
1498
1499 if (palpha_r && palpha_i && pbeta)
1500 {
1501 if (S != A)
1502 if (at)
1503 memcpy(S,A,n*n*sizeof(PRECISION));
1504 else
1505 bTranspose(S,A,n,n,0);
1506 else
1507 if (!at) bTransposeInPlace(A,n);
1508
1509 if (T != B)
1510 if (bt)
1511 memcpy(T,B,n*n*sizeof(PRECISION));
1512 else
1513 bTranspose(T,B,n,n,0);
1514 else
1515 if (!bt) bTransposeInPlace(B,n);
1516
1517 lwork=-1;
1518 gges(&jobvsl,&jobvsr,&sort,(void*)NULL,&bn,S,&bn,T,&bn,&simd,palpha_r,palpha_i,pbeta,Q,&bn,Z,&bn,&size,&lwork,(void*)NULL,&info);
1519 if (!info)
1520 if (!(work=(PRECISION*)dw_malloc((lwork=(lapack_int)size)*sizeof(PRECISION))))
1521 rtrn=MEM_ERR;
1522 else
1523 {
1524 gges(&jobvsl,&jobvsr,&sort,(void*)NULL,&bn,S,&bn,T,&bn,&simd,palpha_r,palpha_i,pbeta,Q,&bn,Z,&bn,work,&lwork,(void*)NULL,&info);
1525 if (!info)
1526 {
1527 if (Q && !qt) bTransposeInPlace(Q,n);
1528 if (Z && !zt) bTransposeInPlace(Z,n);
1529 if (!st) bTransposeInPlace(S,n);
1530 if (!tt) bTransposeInPlace(T,n);
1531 rtrn=NO_ERR;
1532 }
1533 else
1534 rtrn=BLAS_LAPACK_ERR;
1535 dw_free(work);
1536 }
1537 else
1538 rtrn=BLAS_LAPACK_ERR;
1539 }
1540 else
1541 rtrn=MEM_ERR;
1542
1543 if (!alpha_r && palpha_r) dw_free(palpha_r);
1544 if (!alpha_i && palpha_i) dw_free(palpha_i);
1545 if (!beta && pbeta) dw_free(pbeta);
1546
1547 return rtrn;
1548
1549 #undef gges
1550 }
1551
1552 /*
1553 Assumes
1554 select : array of length n
1555 QQ : array of length n*n or null
1556 ZZ : array of length n*n or null
1557 SS : array of length n*n
1558 TT : array of length n*n
1559 Q : array of length n*n or null
1560 Z : array of length n*n or null
1561 S : array of length n*n
1562 T : array of length n*n
1563 n : positive integer
1564 qqt : 0 or 1
1565 zzt : 0 or 1
1566 sst : 0 or 1
1567 ttt : 0 or 1
1568 qt : 0 or 1
1569 zt : 0 or 1
1570 st : 0 or 1
1571 tt : 0 or 1
1572 alpha_i : array of length n or null
1573 alpha_i : array of length n or null
1574 beta : array of length n or null
1575
1576 Returns
1577 NO_ERR : success
1578 MEM_ERR : out of memory
1579 BLAS_LAPACK_ERR : blas or lapack error
1580
1581 Results
1582 Finds orthogonal matrices QQ and ZZ, an block upper triangular matrix SS
1583 with 1 x 1 or 2 x 2 blocks along the diagonal, an upper triangular matrix TT
1584 such that
1585
1586 Q*S*Z' = QQ*SS*ZZ' and Q*T*Z' = QQ*TT*ZZ'
1587
1588 If either Q or QQ are null, then QQ is not computed and if either Z or ZZ is
1589 null, then ZZ is not computed. The matrices S and T are multiplied by
1590 orthogonal matrices in such a manner that their block triangular structure
1591 is retained and the generalized eigenvalues corresponding to value of select
1592 equal to one are transformed to the upper part of SS and TT.
1593
1594 Notes
1595 The flags qqt, zzt, sst, ttt, qt, zt, st, and tt control the format of the
1596 matrices QQ, ZZ, SS, TT, Q, Z, S, and T. A value of 1 indicate column major
1597 format and a value of 0 indictes row major format.
1598
1599 There is a bug in the netlib version of lapack that occasionally requries
1600 the matrix QQ passed to tgsen() to be an n*n matrix even when wantq is zero.
1601 Thus we always ensure that QQ is a n*n matrix.
1602 */
1603 int bReorderGeneralizedSchur_real(int *select, PRECISION *QQ, PRECISION *ZZ, PRECISION *SS, PRECISION *TT,
1604 PRECISION *Q, PRECISION *Z, PRECISION *S, PRECISION *T, int n, int qqt, int zzt, int sst, int ttt,
1605 int qt, int zt, int st, int tt, PRECISION *alpha_r, PRECISION *alpha_i, PRECISION *beta)
1606 {
1607 #if (PRECISION_SIZE == 4)
1608 #define tgsen stgsen
1609 #else
1610 #define tgsen dtgsen
1611 #endif
1612
1613 int i, rtrn=NO_ERR;
1614 lapack_int ijob=0, wantq, wantz, lwork=-1, liwork=-1, isize, info, *iwork, *bselect, bm, bn=n;
1615 PRECISION size, *palpha_r, *palpha_i, *pbeta, *work, pl, pr, dif, *pQQ=QQ;
1616
1617 wantq=(QQ && Q) ? 1 : 0;
1618 wantz=(ZZ && Z) ? 1 : 0;
1619
1620 palpha_r=alpha_r ? alpha_r : (PRECISION*)dw_malloc(n*sizeof(PRECISION));
1621 palpha_i=alpha_i ? alpha_i : (PRECISION*)dw_malloc(n*sizeof(PRECISION));
1622 pbeta=beta ? beta : (PRECISION*)dw_malloc(n*sizeof(PRECISION));
1623 bselect=(lapack_int*)dw_malloc(n*sizeof(lapack_int));
1624
1625 if (palpha_r && palpha_i && pbeta && bselect)
1626 {
1627 if (SS != S)
1628 if (st)
1629 memcpy(SS,S,n*n*sizeof(PRECISION));
1630 else
1631 bTranspose(SS,S,n,n,0);
1632 else
1633 if (!st) bTransposeInPlace(S,n);
1634
1635 if (TT != T)
1636 if (tt)
1637 memcpy(TT,T,n*n*sizeof(PRECISION));
1638 else
1639 bTranspose(TT,T,n,n,0);
1640 else
1641 if (!tt) bTransposeInPlace(T,n);
1642
1643 if (wantq)
1644 {
1645 if (QQ != Q)
1646 if (qt)
1647 memcpy(QQ,Q,n*n*sizeof(PRECISION));
1648 else
1649 bTranspose(QQ,Q,n,n,0);
1650 else
1651 if (!qt) bTransposeInPlace(Q,n);
1652 }
1653 else
1654 if (!QQ) pQQ=(PRECISION*)dw_malloc(n*n*sizeof(PRECISION));
1655
1656 if (wantz)
1657 {
1658 if (ZZ != Z)
1659 if (zt)
1660 memcpy(ZZ,Z,n*n*sizeof(PRECISION));
1661 else
1662 bTranspose(ZZ,Z,n,n,0);
1663 else
1664 if (!zt) bTransposeInPlace(Z,n);
1665 }
1666
1667 for (i=n-1; i >= 0; i--) bselect[i]=select[i];
1668
1669 tgsen(&ijob,&wantq,&wantz,bselect,&bn,SS,&bn,TT,&bn,palpha_r,palpha_i,pbeta,pQQ,&bn,ZZ,&bn,&bm,
1670 &pl,&pr,&dif,&size,&lwork,&isize,&liwork,&info);
1671 if (!info)
1672 if (!(work=(PRECISION*)dw_malloc((lwork=(int)size)*sizeof(PRECISION))))
1673 rtrn=MEM_ERR;
1674 else
1675 {
1676 if (!(iwork=(lapack_int*)dw_malloc((liwork=isize)*sizeof(lapack_int))))
1677 rtrn=MEM_ERR;
1678 else
1679 {
1680 tgsen(&ijob,&wantq,&wantz,bselect,&bn,SS,&bn,TT,&bn,palpha_r,palpha_i,pbeta,pQQ,&bn,ZZ,&bn,&bm,
1681 &pl,&pr,&dif,work,&lwork,iwork,&liwork,&info);
1682 if (info)
1683 rtrn=BLAS_LAPACK_ERR;
1684 dw_free(iwork);
1685 }
1686 dw_free(work);
1687 }
1688 else
1689 rtrn=BLAS_LAPACK_ERR;
1690
1691 if (wantq && !qqt) bTransposeInPlace(QQ,n);
1692 if (wantz && !zzt) bTransposeInPlace(ZZ,n);
1693 if (!sst) bTransposeInPlace(SS,n);
1694 if (!ttt) bTransposeInPlace(TT,n);
1695
1696 if ((pQQ != QQ) && pQQ) dw_free(pQQ);
1697 }
1698 else
1699 rtrn=MEM_ERR;
1700
1701 if (!alpha_r && palpha_r) dw_free(palpha_r);
1702 if (!alpha_i && palpha_i) dw_free(palpha_i);
1703 if (!beta && pbeta) dw_free(pbeta);
1704 if (bselect) dw_free(bselect);
1705
1706 return rtrn;
1707
1708 #undef tgsen
1709 }
1710
1711 /*
1712 Assumes
1713 QQ : array of length n*n or null
1714 ZZ : array of length n*n or null
1715 SS : array of length n*n
1716 TT : array of length n*n
1717 Q : array of length n*n or null
1718 Z : array of length n*n or null
1719 S : array of length n*n
1720 T : array of length n*n
1721 n : positive integer
1722 qqt : 0 or 1
1723 zzt : 0 or 1
1724 sst : 0 or 1
1725 ttt : 0 or 1
1726 qt : 0 or 1
1727 zt : 0 or 1
1728 st : 0 or 1
1729 tt : 0 or 1
1730 alpha_i : array of length n
1731 alpha_i : array of length n
1732 beta : array of length n
1733
1734 Returns
1735 NO_ERR : success
1736 MEM_ERR : out of memory
1737 BLAS_LAPACK_ERR : blas or lapack error
1738
1739 Results
1740 Finds orthogonal matrices QQ and ZZ, an block upper triangular matrix SS
1741 with 1 x 1 or 2 x 2 blocks along the diagonal, an upper triangular matrix TT
1742 such that
1743
1744 Q*S*Z' = QQ*SS*ZZ' and Q*T*Z' = QQ*TT*ZZ'
1745
1746 If either Q or QQ are null, then QQ is not computed and if either Z or ZZ is
1747 null, then ZZ is not computed. The matrices S and T are multiplied by
1748 orthogonal matrices in such a manner that their block triangular structure
1749 retained and the generalized eigenvalues are sorted in descending order if
1750 descend is one and ascending order if descend is zero. So if descend is
1751 one, then upon exit
1752
1753 sqrt(alpha_r[i]^2 + alpha_i[i]^2)/fabs(beta[i])
1754 >= sqrt(alpha_r[i+1]^2 + alpha_i[i+1]^2)/fabs(beta[i+1])
1755
1756 Notes
1757 The flags qqt, zzt, sst, ttt, qt, zt, st, and tt control the format of the
1758 matrices QQ, ZZ, SS, TT, Q, Z, S, and T. A value of 1 indicate column major
1759 format and a value of 0 indictes row major format. All the matrices should be
1760 distinct except that S=SS, T=TT, Q=QQ, or Z=ZZ.
1761 */
1762 int bSortGeneralizedSchur_real(PRECISION *QQ, PRECISION *ZZ, PRECISION *SS, PRECISION *TT, PRECISION *Q,
1763 PRECISION *Z, PRECISION *S, PRECISION *T, int n, int qqt, int zzt, int sst,
1764 int ttt, int qt, int zt, int st, int tt, PRECISION *alpha_r, PRECISION *alpha_i,
1765 PRECISION *beta, int descend)
1766 {
1767 #if (PRECISION_SIZE == 4)
1768 #define tgexc stgexc
1769 #else
1770 #define tgexc dtgexc
1771 #endif
1772
1773 int rtrn=NO_ERR, k, i, j;
1774 lapack_int wantq, wantz, lwork=-1, info, ii=2, jj=1, bn=n;
1775 PRECISION tmp1, tmp2, size, *work;
1776
1777 wantq=(QQ && Q) ? 1 : 0;
1778 wantz=(ZZ && Z) ? 1 : 0;
1779
1780 if (SS != S)
1781 if (st)
1782 memcpy(SS,S,n*n*sizeof(PRECISION));
1783 else
1784 bTranspose(SS,S,n,n,0);
1785 else
1786 if (!st) bTransposeInPlace(S,n);
1787
1788 if (TT != T)
1789 if (tt)
1790 memcpy(TT,T,n*n*sizeof(PRECISION));
1791 else
1792 bTranspose(TT,T,n,n,0);
1793 else
1794 if (!tt) bTransposeInPlace(T,n);
1795
1796 if (wantq)
1797 {
1798 if (QQ != Q)
1799 if (qt)
1800 memcpy(QQ,Q,n*n*sizeof(PRECISION));
1801 else
1802 bTranspose(QQ,Q,n,n,0);
1803 else
1804 if (!qt) bTransposeInPlace(Q,n);
1805 }
1806
1807 if (wantz)
1808 {
1809 if (ZZ != Z)
1810 if (zt)
1811 memcpy(ZZ,Z,n*n*sizeof(PRECISION));
1812 else
1813 bTranspose(ZZ,Z,n,n,0);
1814 else
1815 if (!zt) bTransposeInPlace(Z,n);
1816 }
1817
1818 if (n > 1)
1819 {
1820 tgexc(&wantq,&wantz,&bn,SS,&bn,TT,&bn,QQ,&bn,ZZ,&bn,&ii,&jj,&size,&lwork,&info);
1821 if (!info)
1822 if (!(work=(PRECISION*)dw_malloc((lwork=(lapack_int)size)*sizeof(PRECISION))))
1823 rtrn=MEM_ERR;
1824 else
1825 {
1826 for (i=(SS[1] == 0.0) ? 1 : 2; i < n; )
1827 for (j=i-1; j >= -1; )
1828 {
1829 tmp1=descend ? fabs(beta[i])*sqrt(alpha_r[j]*alpha_r[j] + alpha_i[j]*alpha_i[j])
1830 - fabs(beta[j])*sqrt(alpha_r[i]*alpha_r[i] + alpha_i[i]*alpha_i[i])
1831 : fabs(beta[j])*sqrt(alpha_r[i]*alpha_r[i] + alpha_i[i]*alpha_i[i])
1832 - fabs(beta[i])*sqrt(alpha_r[j]*alpha_r[j] + alpha_i[j]*alpha_i[j]);
1833 if ((j == -1) || (0 <= tmp1))
1834 {
1835 ii=i+1;
1836 jj=(++j)+1;
1837 k=((i == n-1) || (SS[i*n+i+1] == 0.0)) ? 1 : 2;
1838 if (jj < ii)
1839 {
1840 tgexc(&wantq,&wantz,&bn,SS,&bn,TT,&bn,QQ,&bn,ZZ,&bn,&ii,&jj,work,&lwork,&info);
1841 if (info)
1842 {
1843 dw_free(work);
1844 rtrn=BLAS_LAPACK_ERR;
1845 goto EXIT;
1846 }
1847 if (k == 1)
1848 {
1849 tmp1=alpha_r[i]; memmove(alpha_r+j+1,alpha_r+j,(i-j)*sizeof(PRECISION)); alpha_r[j]=tmp1;
1850 tmp1=alpha_i[i]; memmove(alpha_i+j+1,alpha_i+j,(i-j)*sizeof(PRECISION)); alpha_i[j]=tmp1;
1851 tmp1=beta[i]; memmove(beta+j+1,beta+j,(i-j)*sizeof(PRECISION)); beta[j]=tmp1;
1852 }
1853 else
1854 {
1855 tmp1=alpha_r[i]; tmp2=alpha_r[i+1]; memmove(alpha_r+j+2,alpha_r+j,(i-j)*sizeof(PRECISION));
1856 alpha_r[j]=tmp1; alpha_r[j+1]=tmp2;
1857 tmp1=alpha_i[i]; tmp2=alpha_i[i+1]; memmove(alpha_i+j+2,alpha_i+j,(i-j)*sizeof(PRECISION));
1858 alpha_i[j]=tmp1; alpha_i[j+1]=tmp2;
1859 tmp1=beta[i]; tmp2=beta[i+1]; memmove(beta+j+2,beta+j,(i-j)*sizeof(PRECISION));
1860 beta[j]=tmp1; beta[j+1]=tmp2;
1861 }
1862 }
1863 i+=k;
1864 break;
1865 }
1866 else
1867 j-=((j == 0) || (SS[j*n+j-n] == 0.0)) ? 1 : 2;
1868 }
1869 dw_free(work);
1870 }
1871 else
1872 rtrn=BLAS_LAPACK_ERR;
1873 }
1874
1875 EXIT:
1876 if (wantq && !qqt) bTransposeInPlace(QQ,n);
1877 if (wantz && !zzt) bTransposeInPlace(ZZ,n);
1878 if (!sst) bTransposeInPlace(SS,n);
1879 if (!ttt) bTransposeInPlace(TT,n);
1880
1881 return rtrn;
1882
1883 #undef tgexc
1884 }
1885 /******************************************************************************/
1886 /******************************************************************************/
1887 /******************************************************************************/
1888
1889 /******************************************************************************/
1890 /************************** Cholesky Decompositions ***************************/
1891 /******************************************************************************/
1892 /*
1893 Assumes
1894 X : m x m symmetric positive definite matrix
1895 m : positive integer
1896 u : 0 or 1
1897 t : 0 or 1
1898
1899 Returns
1900 0 (NO_ERR) : success
1901 POS_DEF_ERR : X not positive definite
1902
1903 Results
1904 Finds a triangular matrix T such that
1905
1906 X = T' * T
1907
1908 If u is 1, T is upper triangular. If u is 0, T is lower triangular.
1909 T overwrites X. It t is 1, X and T are in column major format. If t
1910 is 0, X and T are in row major format.
1911
1912 Notes
1913 Failure usually indicates X is not positive definite. Only half of X
1914 is accessed. Currently, makes lapack calls only if u is one.
1915 */
1916 int bCholesky(PRECISION *X, int m, int u, int t)
1917 {
1918 #if (PRECISION_SIZE == 4)
1919 #define potrf spotrf
1920 #else
1921 #define potrf dpotrf
1922 #endif
1923
1924 int i, j, k;
1925 lapack_int info, bm=m;
1926 PRECISION scale, *pX, *pXi, *pXj;
1927
1928 if (u)
1929 {
1930 char uplo=t ? 'U' : 'L';
1931 potrf(&uplo,&bm,X,&bm,&info);
1932 if (!info)
1933 {
1934 if (t)
1935 for (i=m-1; i > 0; i--)
1936 for (k=(i-1)*m+i; k >= 0; k-=m)
1937 X[k]=0.0;
1938 else
1939 for (i=m-1; i > 0; i--)
1940 for (k=i*m+(j=i-1); j >= 0; j--)
1941 X[k--]=0.0;
1942 return NO_ERR;
1943 }
1944 else
1945 return (info < 0) ? BLAS_LAPACK_ERR : POSDEF_ERR;
1946 }
1947 else
1948 {
1949 if (!t)
1950 for (i=0, pXi=X; i < m; pXi++, i++)
1951 {
1952 for (j=0, pX=X+i*m; j < i; pX++, j++) *pX=0.0;
1953
1954 for (k=(i-1)*m; k >= 0; k-=m) *pX-=pXi[k]*pXi[k];
1955
1956 if (*pX <= 0.0) return POSDEF_ERR;
1957 scale=1.0/(*pX=sqrt(*pX));
1958
1959 pXj=pXi;
1960 for (j++; j < m; j++)
1961 {
1962 pXj++;
1963 pX++;
1964 for (k=(i-1)*m; k >= 0; k-=m) *pX-=pXi[k]*pXj[k];
1965 *pX*=scale;
1966 }
1967 }
1968 else
1969 for (i=0, pXi=X; i < m; pXi+=m, i++)
1970 {
1971 for (j=0, pX=X+i; j < i; pX+=m, j++) *pX=0.0;
1972
1973 for (k=i-1; k >= 0; k--) *pX-=pXi[k]*pXi[k];
1974
1975 if (*pX <= 0.0) return POSDEF_ERR;
1976 scale=1.0/(*pX=sqrt(*pX));
1977
1978 pXj=pXi;
1979 for (j++; j < m; j++)
1980 {
1981 pX+=m;
1982 pXj+=m;
1983 for (k=i-1; k >= 0; k--) *pX-=pXi[k]*pXj[k];
1984 *pX*=scale;
1985 }
1986 }
1987 return NO_ERR;
1988 }
1989 #undef potrf
1990 }
1991
1992
1993 /******************************************************************************/
1994 /************************** Eigen Values and Vectors **************************/
1995 /******************************************************************************/
1996 /*
1997 Assumes
1998 re : array of length n
1999 im : array of length n
2000 x : n x n matrix
2001 n : positive integer
2002 xt : 0 or 1
2003
2004 Results
2005 re[k] + i*im[k] will be an eigenvalue of x
2006
2007 Returns
2008 0 upon success and non-zero otherwise.
2009 */
2010 int bEigenvalues(PRECISION *re, PRECISION *im, PRECISION *x, int n, int xt)
2011 {
2012 #if (PRECISION_SIZE == 4)
2013 #define geev sgeev
2014 #else
2015 #define geev dgeev
2016 #endif
2017 char job='N';
2018 int rtrn;
2019 lapack_int bm=1, lwork=-1, info, bn=n;
2020 PRECISION size, *work, *tmp;
2021
2022 if (!(tmp=(PRECISION*)dw_malloc(n*n*sizeof(PRECISION))))
2023 rtrn=MEM_ERR;
2024 else
2025 {
2026 if (xt)
2027 memcpy(tmp,x,n*n*sizeof(PRECISION));
2028 else
2029 bTranspose(tmp,x,n,n,0);
2030 geev(&job,&job,&bn,tmp,&bn,re,im,(PRECISION*)NULL,&bm,(PRECISION*)NULL,&bm,&size,&lwork,&info);
2031 if (info)
2032 rtrn=BLAS_LAPACK_ERR;
2033 else
2034 {
2035 if (!(work=(PRECISION*)dw_malloc((lwork=(lapack_int)size)*sizeof(PRECISION))))
2036 rtrn=MEM_ERR;
2037 else
2038 {
2039 geev(&job,&job,&bn,tmp,&bn,re,im,(PRECISION*)NULL,&bm,(PRECISION*)NULL,&bm,work,&lwork,&info);
2040 if (info)
2041 rtrn=BLAS_LAPACK_ERR;
2042 else
2043 rtrn=NO_ERR;
2044 dw_free(work);
2045 }
2046 }
2047 dw_free(tmp);
2048 }
2049 return rtrn;
2050
2051 #undef geev
2052 }
2053
2054 /*
2055 Assumes
2056 reval : array of length n
2057 imval : array of length n
2058 revec : array of length n*n
2059 imvec : array of length n*n
2060 x : array of length n*n
2061 n : positive integer
2062 x_t : 0 or 1
2063 vec_t : 0 or 1
2064
2065 Results
2066 reval[k] + i*imval[k] will be an eigenvalue of x with eigenvector
2067 (revec + i*imvec)*e(k), where e(k) is the kth column of the n x n identity
2068 matrix.
2069
2070 Returns
2071 0 upon success and non-zero otherwise.
2072
2073 Notes
2074 If xt, revect, or imvect is 1, then x, revec, or imvec is in column major
2075 form. If xt , revect, or imvect is 0, then x, revec, or imvec is in row
2076 major form.
2077 */
2078 int bEigen(PRECISION *reval, PRECISION *imval, PRECISION *revec, PRECISION *imvec, PRECISION *x, int n, int xt, int revect, int imvect)
2079 {
2080 #if (PRECISION_SIZE == 4)
2081 #define geev sgeev
2082 #else
2083 #define geev dgeev
2084 #endif
2085
2086 char jobr='V', jobl='N';
2087 int i, j, k, rtrn=0;
2088 lapack_int bm=1, lwork=-1, info, bn=n;
2089 PRECISION *work, size, *tmp;
2090
2091 if (!(tmp=(PRECISION*)dw_malloc(n*n*sizeof(PRECISION))))
2092 rtrn=MEM_ERR;
2093 else
2094 {
2095 if (xt)
2096 memcpy(tmp,x,n*n*sizeof(PRECISION));
2097 else
2098 bTranspose(tmp,x,n,n,0);
2099 geev(&jobl,&jobr,&bn,tmp,&bn,reval,imval,(PRECISION*)NULL,&bm,revec,&bn,&size,&lwork,&info);
2100 if (info)
2101 rtrn=BLAS_LAPACK_ERR;
2102 else
2103 {
2104 if (!(work=(PRECISION*)dw_malloc((lwork=(lapack_int)size)*sizeof(PRECISION))))
2105 rtrn=MEM_ERR;
2106 else
2107 {
2108 geev(&jobl,&jobr,&bn,tmp,&bn,reval,imval,(PRECISION*)NULL,&bm,revec,&bn,work,&lwork,&info);
2109 if (info)
2110 rtrn=BLAS_LAPACK_ERR;
2111 else
2112 {
2113 for (j=0; j < n; j++)
2114 if (imval[j] == 0.0)
2115 for (k=j*n, i=n-1; i >= 0; i--) imvec[k+i]=0.0;
2116 else
2117 {
2118 k=(++j)*n;
2119 memcpy(imvec+k-n,revec+k,n*sizeof(PRECISION));
2120 for (i=n-1; i >= 0; i--) imvec[k+i]=-revec[k+i];
2121 memcpy(revec+k,revec+k-n,n*sizeof(PRECISION));
2122 }
2123 if (!revect) bTransposeInPlace(revec,n);
2124 if (!imvect) bTransposeInPlace(imvec,n);
2125 rtrn=NO_ERR;
2126 }
2127 }
2128 dw_free(work);
2129 }
2130 dw_free(tmp);
2131 }
2132 return rtrn;
2133
2134 #undef geev
2135 }
2136
2137 /*
2138 Assumes
2139 val : array of length n
2140 vec : array of length n*n
2141 x : array of length n*n
2142 n : positive integer
2143 vect : 0 or 1
2144
2145 Results
2146 val[k] will be an eigenvalue of x with eigenvector vec*e(k), where e(k) is
2147 the kth column of the n x n identity matrix.
2148
2149 Returns
2150 0 upon success and non-zero otherwise.
2151
2152 Notes
2153 If vect is 1, then vec is in column major form. If vect is 0, then vec is
2154 in row major form.
2155 */
2156 int bEigen_symmetric(PRECISION *val, PRECISION *vec, PRECISION *x, int n, int vect)
2157 {
2158 #if (PRECISION_SIZE == 4)
2159 #define syevr ssyevr
2160 #else
2161 #define syevr dsyevr
2162 #endif
2163
2164 char jobz=vec ? 'V' : 'N', range='A', uplo='U';
2165 int rtrn=0;
2166 lapack_int i, m, lwork=-1, *iwork, liwork=-1, isize, info, isuppz, bn=n;
2167 PRECISION *work, size, *A, v, abstol=-1.0;
2168
2169 if (!(A=(PRECISION*)dw_malloc(n*n*sizeof(PRECISION))))
2170 rtrn=MEM_ERR;
2171 else
2172 {
2173 memcpy(A,x,n*n*sizeof(PRECISION));
2174 syevr(&jobz,&range,&uplo,&bn,A,&bn,&v,&v,&i,&i,&abstol,&m,val,vec,&bn,&isuppz,&size,&lwork,&isize,&liwork,&info);
2175 if (info)
2176 rtrn=BLAS_LAPACK_ERR;
2177 else
2178 {
2179 if (!(work=(PRECISION*)dw_malloc((lwork=(lapack_int)size)*sizeof(PRECISION))))
2180 rtrn=MEM_ERR;
2181 else
2182 {
2183 if (!(iwork=(lapack_int*)dw_malloc((liwork=isize)*sizeof(lapack_int))))
2184 rtrn=MEM_ERR;
2185 else
2186 {
2187 syevr(&jobz,&range,&uplo,&bn,A,&bn,&v,&v,&i,&i,&abstol,&m,val,vec,&bn,&isuppz,work,&lwork,iwork,&liwork,&info);
2188 if (info)
2189 rtrn=BLAS_LAPACK_ERR;
2190 else
2191 {
2192 if (!vect) bTransposeInPlace(vec,n);
2193 rtrn=NO_ERR;
2194 }
2195 dw_free(iwork);
2196 }
2197 dw_free(work);
2198 }
2199 }
2200 dw_free(A);
2201 }
2202 return rtrn;
2203 #undef syevr
2204 }
2205
2206
2207 /******************************************************************************/
2208 /****************************** Tensor Products *******************************/
2209 /******************************************************************************/
2210 /*
2211 Assumes
2212 x : array of length m*r*n*s
2213 y : array of length m*n
2214 z : array of length r*s
2215 m,n,r,s : positive
2216 xt,yt,zt : 0 or 1
2217
2218 Returns
2219 NO_ERR : success
2220
2221 Results
2222 x y z
2223 xt yt zt (mr x ns) (m x n) (r x s) computes
2224 ---------------------------------------------------------------------
2225 0 0 0 row major row major row major x = y tensor z
2226 1 0 0 col major row major row major x = y tensor z
2227 1 1 0 col major col major row major x = y tensor z
2228 0 1 0 row major col major row major x = y tensor z
2229 0 0 1 row major row major col major x = y tensor z
2230 1 0 1 col major row major col major x = y tensor z
2231 1 1 1 col major col major col major x = y tensor z
2232 0 1 1 row major col major col major x = y tensor z
2233 */
2234 int bMatrixTensor(PRECISION *x, PRECISION *y, PRECISION *z, int m, int n, int r, int s, int xt, int yt, int zt)
2235 {
2236 int iy, jy, iz, jz, k, l, stride;
2237 PRECISION t, *pz=z+r*s-1;
2238 if (xt)
2239 if (zt)
2240 {
2241 stride=m*r;
2242 for (iy=m-1; iy >= 0; iy--)
2243 for (jy=n-1; jy >= 0; jy--)
2244 {
2245 t=y[yt ? iy+m*jy : n*iy+jy];
2246 l=(iy+1)*r-1 + ((jy+1)*s-1)*stride;
2247 z=pz;
2248 for (jz=s-1; jz >= 0; l-=stride, jz--)
2249 for (iz=r-1, k=l; iz >= 0; z--, k--, iz--)
2250 x[k]=t*(*z);
2251 }
2252 }
2253 else
2254 {
2255 stride=m*r;
2256 for (iy=m-1; iy >= 0; iy--)
2257 for (jy=n-1; jy >= 0; jy--)
2258 {
2259 t=y[yt ? iy+m*jy : n*iy+jy];
2260 l=(iy+1)*r-1 + ((jy+1)*s-1)*stride;
2261 z=pz;
2262 for (iz=r-1; iz >= 0; l--, iz--)
2263 for (jz=s-1, k=l; jz >= 0; z--, k-=stride, jz--)
2264 x[k]=t*(*z);
2265 }
2266 }
2267 else
2268 if (zt)
2269 {
2270 stride=n*s;
2271 for (iy=m-1; iy >= 0; iy--)
2272 for (jy=n-1; jy >= 0; jy--)
2273 {
2274 t=y[yt ? iy+m*jy : n*iy+jy];
2275 l=((iy+1)*r-1)*stride + (jy+1)*s-1;
2276 z=pz;
2277 for (jz=s-1; jz >= 0; l--, jz--)
2278 for (iz=r-1, k=l; iz >= 0; z--, k-=stride, iz--)
2279 x[k]=t*(*z);
2280
2281 }
2282 }
2283 else
2284 {
2285 stride=n*s;
2286 for (iy=m-1; iy >= 0; iy--)
2287 for (jy=n-1; jy >= 0; jy--)
2288 {
2289 t=y[yt ? iy+m*jy : n*iy+jy];
2290 l=((iy+1)*r-1)*stride + (jy+1)*s-1;
2291 z=pz;
2292 for (iz=r-1; iz >= 0; l-=stride, iz--)
2293 for (jz=s-1, k=l; jz >= 0; z--, k--, jz--)
2294 x[k]=t*(*z);
2295
2296 }
2297 }
2298 return NO_ERR;
2299 }
2300
2301 int bVectorTensor(PRECISION *x, PRECISION *y, PRECISION *z, int m, int n)
2302 {
2303 int j, k;
2304 PRECISION s;
2305 for (x+=m*n-1, j=m-1; j >= 0; j--)
2306 for (s=y[j], k=n-1; k >= 0; x--, k--)
2307 *x=s*z[k];
2308 return NO_ERR;
2309 }
2310 /******************************************************************************/
2311 /******************************************************************************/
2312 /******************************************************************************/
2313
2314 /******************************************************************************/
2315 /******************************* Inner Product ********************************/
2316 /******************************************************************************/
2317 /*
2318 Assumes
2319 x - array of length n
2320 y - array of length n
2321 S - array of length n*n containing a real symmetric matrix
2322 n - positive integer
2323
2324 Returns
2325 x'*S*y
2326 */
2327 PRECISION bInnerProduct(PRECISION *x, PRECISION *y, PRECISION *S, int n)
2328 {
2329 int i, j;
2330 PRECISION u, v, *px, *pS;
2331 if (x == y)
2332 for (u=(*x)*(*x)*(*S), i=n-1; i > 0; i--)
2333 {
2334 //for (v=(*((px=x)++))*(*((pS=S+i*n)++)), j=i-2; j >= 0; v+=(*(px++))*(*(pS++)), j--);
2335 for (v=(*(px=x))*(*(pS=S+i*n)), px++, pS++, j=i-2; j >= 0; v+=(*(px++))*(*(pS++)), j--);
2336 u+=(*px)*(2.0*v + (*px)*(*pS));
2337 }
2338 else
2339 for (pS=S+n*n-1, u=0.0, i=n-1; i >= 0; i--)
2340 {
2341 //for (v=(*((px=x+(n-1))--))*(*(pS--)), j=n-2; j >= 0; v+=(*(px--))*(*(pS--)), j--);
2342 for (v=(*(px=x+(n-1)))*(*pS), px--, pS--, j=n-2; j >= 0; v+=(*(px--))*(*(pS--)), j--);
2343 u+=y[i]*v;
2344 }
2345 return u;
2346 }