Debian Codegraph @

contrib/ms-sbvar/utilities_dw/matrix/dw_matrix.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 "dw_matrix.h"
19 #include "bmatrix.h"
20 #include "dw_error.h"
21 #include "dw_std.h"
22
23 #include <stdlib.h>
24 #include <memory.h>
25 #include <math.h>
26
27 /*******************************************************************************/
28 /********************** Allocation/Deallocation Routines ***********************/
29 /*******************************************************************************/
30 /*
31 TVector CreateVector(int m)
32 Allocates memory for a m-dimensional vector. On success, returns a pointer
33 to the vector. On failure, calls dw_Error() and returns null. The
34 routine will fail if m <= 0 (SIZE_ERR) or is unable to allocate required
35 memory (MEM_ERR).
36
37 TMatrix CreateMatrix(int m, int n)
38 Allocates memory for a (m x n) matrix. On success, returns a pointer to the
39 matrix. On failure, calls dw_Error() and returns null. The routine will
40 fail if m <= 0 or n <= 0 (SIZE_ERR) or is unable to allocate required memory
41 (MEM_ERR).
42
43 void FreeVector(TVector *x)
44 Assumes
45 x : valid vector or null pointer
46 Results
47 Deallocates memory for vector if x is not null.
48 Notes
49 The vector x MUST have been previously allocated with a call to
50 CreateVector() or be null.
51
52 void FreeMatrix(TMatrix *X)
53 Assumes
54 X : valid matrix or null pointer
55 Results
56 Deallocates memory for matrix if X is not null.
57 Notes
58 The matrix X MUST have been previously allocated with a call to
59 CreateMatrix() or be null.
60 */
61 #if (defined(STANDARD_ROW_MAJOR) || defined(STANDARD_COLUMN_MAJOR))
62 /**/
63 TVector CreateVector(int m)
64 {
65 TVector x;
66 if (m <= 0)
67 {
68 dw_Error(SIZE_ERR);
69 return (TVector)NULL;
70 }
71 if (x=(TVector)dw_malloc(sizeof(TVectorStructure) + (m-1)*sizeof(PRECISION)))
72 DimV(x)=m;
73 else
74 dw_Error(MEM_ERR);
75 return x;
76 }
77 /**/
78 void FreeVector(TVector x)
79 {
80 if (x) dw_free(x);
81 }
82 /**/
83 TMatrix CreateMatrix(int m, int n)
84 {
85 TMatrix X;
86 if ((m <= 0) || (n <= 0))
87 {
88 dw_Error(SIZE_ERR);
89 return (TMatrix)NULL;
90 }
91 if (X=(TMatrix)dw_malloc(sizeof(TMatrixStructure) + (m*n-1)*sizeof(PRECISION)))
92 {
93 RowM(X)=m;
94 ColM(X)=n;
95 }
96 else
97 dw_Error(MEM_ERR);
98 return X;
99 }
100 /**/
101 void FreeMatrix(TMatrix X)
102 {
103 if (X) dw_free(X);
104 }
105 #endif
106 /*-----------------------------------------------------------------------------*/
107 #if (defined(STRUCTURED_ROW_MAJOR) || defined(STRUCTURED_COLUMN_MAJOR) || defined(STRUCTURED_MAJOR_FORM))
108 /**/
109 TVector CreateVector(int m)
110 {
111 TVector x;
112 if (m <= 0)
113 {
114 dw_Error(SIZE_ERR);
115 return (TVector)NULL;
116 }
117 if ((x=(TVector)dw_malloc(sizeof(TVectorStructure))))
118 if ((x->x=(PRECISION*)dw_malloc(m*sizeof(PRECISION))))
119 DimV(x)=m;
120 else
121 {
122 dw_free(x);
123 dw_Error(MEM_ERR);
124 return (TVector)NULL;
125 }
126 else
127 dw_Error(MEM_ERR);
128 return x;
129 }
130 /**/
131 void FreeVector(TVector x)
132 {
133 if (x)
134 {
135 if (x->x) dw_free(x->x);
136 dw_free(x);
137 }
138 }
139 /**/
140 TMatrix CreateMatrix(int m, int n)
141 {
142 TMatrix X;
143 if ((m <= 0) || (n <= 0))
144 {
145 dw_Error(SIZE_ERR);
146 return (TMatrix)NULL;
147 }
148 if ((X=(TMatrix)dw_malloc(sizeof(TMatrixStructure))))
149 if ((X->x=(PRECISION*)dw_malloc(m*n*sizeof(PRECISION))))
150 {
151 RowM(X)=m;
152 ColM(X)=n;
153 #if defined STRUCTURED_MAJOR_FORM
154 SetMajorForm(X,(rand() % 2) ? 0 : 1);
155 #endif
156 }
157 else
158 {
159 dw_free(X);
160 dw_Error(MEM_ERR);
161 return (TMatrix)NULL;
162 }
163 else
164 dw_Error(MEM_ERR);
165 return X;
166 }
167 /**/
168 void FreeMatrix(TMatrix X)
169 {
170 if (X)
171 {
172 if (X->x) dw_free(X->x);
173 dw_free(X);
174 }
175 }
176 #endif
177 /*-----------------------------------------------------------------------------*/
178 #if (defined(LEGACY_ROW_MAJOR))
179 /**/
180 TVector CreateVector(int m)
181 {
182 TVector x;
183 if (m <= 0)
184 {
185 dw_Error(SIZE_ERR);
186 return (TVector)NULL;
187 }
188 if (x=(TVector)((int *)dw_malloc(m*sizeof(PRECISION)+sizeof(int))+1))
189 V_DIM(x)=m;
190 else
191 dw_Error(MEM_ERR);
192 return x;
193 }
194 /**/
195 void FreeVector(TVector x)
196 {
197 if (x) dw_free((int *)x-1);
198 }
199 /**/
200 TMatrix CreateMatrix(int m, int n)
201 {
202 TMatrix X;
203 int i;
204 if ((m <= 0) || (n <= 0))
205 {
206 dw_Error(SIZE_ERR);
207 return (TMatrix)NULL;
208 }
209 if (X=(TMatrix)((int *)dw_malloc(m*(sizeof(PRECISION *)+n*sizeof(PRECISION))+2*sizeof(int))+2))
210 {
211 M_ROW(X)=m;
212 M_COL(X)=n;
213 X[0]=(PRECISION *)(X+m);
214 for (i=1; i < m; i++) X[i]=X[i-1]+n;
215 }
216 else
217 dw_Error(MEM_ERR);
218 return X;
219 }
220 /**/
221 void FreeMatrix(TMatrix X)
222 {
223 if (X) dw_free((int *)X-2);
224 }
225 #endif
226 /*-----------------------------------------------------------------------------*/
227 #if (defined(TZ_COLUMN_MAJOR))
228 /**/
229 TVector CreateVector(int m)
230 {
231 TVector x;
232 if (m <= 0)
233 {
234 dw_Error(SIZE_ERR);
235 return (TVector)NULL;
236 }
237 if (x=(TVector)dw_malloc(sizeof(TSdvector)))
238 if (!(pElementV(x)=(PRECISION*)dw_malloc(m*sizeof(PRECISION))))
239 {
240 dw_free(x);
241 dw_Error(MEM_ERR);
242 return (TVector)NULL;
243 }
244 else
245 {
246 DimV(x)=m;
247 x->flag=V_DEF;
248 }
249 else
250 dw_Error(MEM_ERR);
251 return x;
252 }
253 /**/
254 void FreeVector(TVector x)
255 {
256 if (x)
257 {
258 if (pElementV(x)) dw_free(pElementV(x));
259 dw_free(x);
260 }
261 }
262 /**/
263 TMatrix CreateMatrix(int m, int n)
264 {
265 TMatrix X;
266 if ((m <= 0) || (n <= 0))
267 {
268 dw_Error(SIZE_ERR);
269 return (TMatrix)NULL;
270 }
271 if (X=(TMatrix)dw_malloc(sizeof(TSdmatrix)))
272 if (!(pElementM(X)=(PRECISION*)dw_malloc(m*n*sizeof(PRECISION))))
273 {
274 dw_free(X);
275 dw_Error(MEM_ERR);
276 return (TMatrix)NULL;
277 }
278 else
279 {
280 RowM(X)=m;
281 ColM(X)=n;
282 X->flag=M_GE;
283 }
284 else
285 dw_Error(MEM_ERR);
286 return X;
287 }
288 /**/
289 void FreeMatrix(TMatrix X)
290 {
291 if (X)
292 {
293 if (pElementM(X)) dw_free(pElementM(X));
294 dw_free(X);
295 }
296 }
297 #endif
298 /*-----------------------------------------------------------------------------*/
299 /*******************************************************************************/
300 /*******************************************************************************/
301
302
303 /*******************************************************************************/
304 /************************** Initialization Routines ****************************/
305 /*******************************************************************************/
306 /*
307 Assumes
308 x : m vector
309
310 Results
311 x(i) = c
312
313 Returns
314 x
315 */
316 TVector InitializeVector(TVector x, PRECISION c)
317 {
318 int i;
319 if (!x)
320 dw_Error(NULL_ERR);
321 else
322 for (i=DimV(x); --i >= 0; ElementV(x,i)=c);
323 return x;
324 }
325
326 /*
327 Assumes
328 X : m x n matrix
329
330 Results
331 X(i,j) = c
332
333 Returns
334 X
335 */
336 TMatrix InitializeMatrix(TMatrix X, PRECISION c)
337 {
338 int i;
339 PRECISION *pX;
340 if (!X)
341 dw_Error(NULL_ERR);
342 else
343 for (pX=pElementM(X), i=RowM(X)*ColM(X)-1; i >= 0; i--) pX[i]=c;
344 return X;
345 }
346 /*******************************************************************************/
347 /*******************************************************************************/
348
349
350 /*******************************************************************************/
351 /**************************** Assignment Routines ******************************/
352 /*******************************************************************************/
353 /*
354 Assumes
355 x : m-vector or null pointer
356 y : m-vector
357
358 Results
359 x = y. If x is null pointer, x is created.
360
361 Returns
362 Returns x upon success and null on failure. Call dw_GetError() to
363 determine the cause of failure.
364 */
365 TVector EquateVector(TVector x, TVector y)
366 {
367 if (!y)
368 {
369 dw_Error(NULL_ERR);
370 return (TVector)NULL;
371 }
372 if (!x)
373 {
374 if (!(x=CreateVector(DimV(y))))
375 return (TVector)NULL;
376 }
377 else
378 if (x == y)
379 return x;
380 else
381 if (DimV(x) != DimV(y))
382 {
383 dw_Error(SIZE_ERR);
384 return (TVector)NULL;
385 }
386 memcpy(pElementV(x),pElementV(y),DimV(y)*sizeof(PRECISION));
387 return x;
388 }
389
390 /*
391 Assumes
392 X : m x n matrix or null pointer
393 Y : m x n matrix
394
395 Results
396 X = Y. If X is null pointer, X is created.
397
398 Returns
399 Returns X upon success and null on failure. Call GetError() to
400 determine the cause of failure.
401 */
402 TMatrix EquateMatrix(TMatrix X, TMatrix Y)
403 {
404 if (!Y)
405 {
406 dw_Error(NULL_ERR);
407 return (TMatrix)NULL;
408 }
409 if (!X)
410 {
411 if (!(X=CreateMatrix(RowM(Y),ColM(Y))))
412 return (TMatrix)NULL;
413 }
414 else
415 if (X == Y)
416 return X;
417 else
418 if ((RowM(Y) != RowM(X)) || (ColM(Y) != ColM(X)))
419 {
420 dw_Error(SIZE_ERR);
421 return (TMatrix)NULL;
422 }
423 memcpy(pElementM(X),pElementM(Y),RowM(Y)*ColM(Y)*sizeof(PRECISION));
424 SetMajorForm(X,MajorForm(Y));
425 return X;
426 }
427
428 /*
429 Assumes
430 X : m x m matrix or null pointer
431
432 Results
433 X is set to the m x m identity matrix. If X is null pointer, X
434 is created.
435
436 Returns
437 Returns X upon success and null on failure. Call GetError() to
438 determine the cause of failure.
439 */
440 TMatrix IdentityMatrix(TMatrix X, int m)
441 {
442 int i;
443 PRECISION *pX;
444 if (!X)
445 {
446 if (!(X=CreateMatrix(m,m)))
447 return (TMatrix)NULL;
448 }
449 else
450 if ((m != RowM(X)) || (m != ColM(X)))
451 {
452 dw_Error(SIZE_ERR);
453 return (TMatrix)NULL;
454 }
455 for (pX=pElementM(X), i=m*m-1; i >= 0; i--) pX[i]=0.0;
456 for (i=m*m-1; i >= 0; i-=m+1) pX[i]=1.0;
457 return X;
458 }
459
460 /*
461 Assumes
462 X : m x n matrix or null pointer
463
464 Results
465 X is set to the m x n zero matrix. If X is null pointer, X is created.
466
467 Returns
468 Returns X upon success and null on failure. Call GetError() to
469 determine the cause of failure.
470 */
471 TMatrix ZeroMatrix(TMatrix X, int m, int n)
472 {
473 int i;
474 PRECISION *pX;
475 if (!X)
476 {
477 if (!(X=CreateMatrix(m,n)))
478 return (TMatrix)NULL;
479 }
480 else
481 if ((m != RowM(X)) || (n != ColM(X)))
482 {
483 dw_Error(SIZE_ERR);
484 return (TMatrix)NULL;
485 }
486 for (pX=pElementM(X), i=m*m-1; i >= 0; i--) pX[i]=0.0;
487 return X;
488 }
489
490 /*
491 Assumes
492 X : a null pointer or r x s matrix, where r = m and s >= m or r >= m and s = m.
493 y : m-vector
494
495 Results
496 X = diag(y). If X is null pointer, a square matrix X is created.
497
498 Returns
499 Returns x upon success and null on failure. Call GetError() to
500 determine the cause of failure.
501 */
502 TMatrix DiagonalMatrix(TMatrix X, TVector y)
503 {
504 int i, j, k;
505 PRECISION *pX;
506 if (!y)
507 {
508 dw_Error(NULL_ERR);
509 return (TMatrix)NULL;
510 }
511 if (!X)
512 {
513 if (!(X=CreateMatrix(DimV(y),DimV(y))))
514 return (TMatrix)NULL;
515 }
516 else
517 if (DimV(y) != ((RowM(X) < ColM(X)) ? RowM(X) : ColM(X)))
518 {
519 dw_Error(SIZE_ERR);
520 return (TMatrix)NULL;
521 }
522 for (pX=pElementM(X), i=RowM(X)*ColM(X)-1; i >= 0; i--) pX[i]=0.0;
523 for (k=(MajorForm(X) ? RowM(X) : ColM(X))+1, j=DimV(y)-1, pX=&ElementM(X,j,j); j >= 0; pX-=k, j--) *pX=ElementV(y,j);
524 return X;
525 }
526
527 /*
528 Assumes
529 X : m x 1 matrix or null pointer
530 y : m-vector
531
532 Results
533 X is equal to the column vector y. If X is null pointer, X is
534 created.
535
536 Returns
537 Returns X upon success and null on failure. Call GetError() to
538 determine the cause of failure.
539 */
540 TMatrix ColumnMatrix(TMatrix X, TVector y)
541 {
542 if (!y)
543 {
544 dw_Error(NULL_ERR);
545 return (TMatrix)NULL;
546 }
547 if (!X)
548 {
549 if (!(X=CreateMatrix(DimV(y),1)))
550 return (TMatrix)NULL;
551 }
552 else
553 if ((DimV(y) != RowM(X)) || (1 != ColM(X)))
554 {
555 dw_Error(SIZE_ERR);
556 return (TMatrix)NULL;
557 }
558 memcpy(pElementM(X),pElementV(y),DimV(y)*sizeof(PRECISION));
559 return X;
560 }
561
562 /*
563 Assumes
564 X : 1 x m matrix or null pointer
565 y : m-vector
566
567 Results
568 X is equal to the row vector y. If X is null pointer, X is
569 created.
570
571 Returns
572 Returns X upon success and null on failure. Call GetError() to
573 determine the cause of failure.
574 */
575 TMatrix RowMatrix(TMatrix X, TVector y)
576 {
577 if (!y)
578 {
579 dw_Error(NULL_ERR);
580 return (TMatrix)NULL;
581 }
582 if (!X)
583 {
584 if (!(X=CreateMatrix(1,DimV(y))))
585 return (TMatrix)NULL;
586 }
587 else
588 if ((1 != RowM(X)) || (DimV(y) != ColM(X)))
589 {
590 dw_Error(SIZE_ERR);
591 return (TMatrix)NULL;
592 }
593 memcpy(pElementM(X),pElementV(y),DimV(y)*sizeof(PRECISION));
594 return X;
595 }
596
597 /*
598 Assumes
599 x : m vector or null pointer
600 y : m vector
601
602 Results
603 x(i) = abs(y(i)). If x is null pointer, x is created.
604
605 Returns
606 Returns x upon success and null on failure. Call GetError() to
607 determine the cause of failure.
608
609 Notes
610 x and y do not have to be distinct vectors.
611 */
612 TVector AbsV(TVector x, TVector y)
613 {
614 if (!y)
615 {
616 dw_Error(NULL_ERR);
617 return (TVector)NULL;
618 }
619 if (!x)
620 {
621 if (!(x=CreateVector(DimV(y))))
622 return (TVector)NULL;
623 }
624 else
625 if (DimV(x) != DimV(y))
626 {
627 dw_Error(SIZE_ERR);
628 return (TVector)NULL;
629 }
630 bAbs(pElementV(x),pElementV(y),DimV(y));
631 return x;
632 }
633
634 /*
635 Assumes
636 X : m x n matrix or null pointer
637 Y : m x n matrix
638
639 Results
640 X(i,j) = abs(Y(i,j)). If X is null pointer, X is created.
641
642 Returns
643 Returns X upon success and null on failure. Call GetError() to
644 determine the cause of failure.
645
646 Notes
647 X and Y do not have to be distinct matrices
648 */
649 TMatrix AbsM(TMatrix X, TMatrix Y)
650 {
651 if (!Y)
652 {
653 dw_Error(NULL_ERR);
654 return (TMatrix)NULL;
655 }
656 if (!X)
657 {
658 if (!(X=CreateMatrix(RowM(Y),ColM(Y))))
659 return (TMatrix)NULL;
660 }
661 else
662 if ((RowM(X) != RowM(Y)) || (ColM(X) != ColM(Y)))
663 {
664 dw_Error(SIZE_ERR);
665 return (TMatrix)NULL;
666 }
667 bAbs(pElementM(X),pElementM(Y),RowM(Y)*ColM(Y));
668 SetMajorForm(X,MajorForm(Y));
669 return X;
670 }
671
672 /*
673 Assumes
674 x : m vector or null pointer
675 y : m vector
676
677 Results
678 x = -x. If x is null pointer, x is created.
679
680 Returns
681 Returns x upon success and null on failure. Call GetError() to
682 determine the cause of failure.
683
684 Notes
685 x and y do not have to be distinct vectors.
686 */
687 TVector MinusV(TVector x, TVector y)
688 {
689 if (!y)
690 {
691 dw_Error(NULL_ERR);
692 return (TVector)NULL;
693 }
694 if (!x)
695 {
696 if (!(x=CreateVector(DimV(y))))
697 return (TVector)NULL;
698 }
699 else
700 if (DimV(x) != DimV(y))
701 {
702 dw_Error(SIZE_ERR);
703 return (TVector)NULL;
704 }
705 bNegative(pElementV(x),pElementV(y),DimV(y));
706 return x;
707 }
708
709 /*
710 Assumes
711 X : m x n matrix or null pointer
712 Y : m x n matrix
713
714 Results
715 X = -Y. If X is null pointer, X is created.
716
717 Returns
718 Returns X upon success and null on failure. Call GetError() to
719 determine the cause of failure.
720
721 Notes
722 X and Y do not have to be distinct matrices.
723 */
724 TMatrix MinusM(TMatrix X, TMatrix Y)
725 {
726 if (!Y)
727 {
728 dw_Error(NULL_ERR);
729 return (TMatrix)NULL;
730 }
731 if (!X)
732 {
733 if (!(X=CreateMatrix(RowM(Y),ColM(Y))))
734 return (TMatrix)NULL;
735 }
736 else
737 if ((RowM(X) != RowM(Y)) || (ColM(X) != ColM(Y)))
738 {
739 dw_Error(SIZE_ERR);
740 return (TMatrix)NULL;
741 }
742 bNegative(pElementM(X),pElementM(Y),RowM(Y)*ColM(Y));
743 SetMajorForm(X,MajorForm(Y));
744 return X;
745 }
746
747 /*
748 Assumes
749 X : n x m matrix or null pointer
750 Y : m x n matrix
751
752 Results
753 X = Y'. If X is null pointer, X is created.
754
755 Returns
756 Returns X upon success and null on failure. Call GetError() to
757 determine the cause of failure.
758
759 Notes
760 If Y is square, X and Y do not have to be distinct matrices
761 */
762 TMatrix Transpose(TMatrix X, TMatrix Y)
763 {
764 if (!Y)
765 {
766 dw_Error(NULL_ERR);
767 return (TMatrix)NULL;
768 }
769 if (X == Y)
770 {
771 if (RowM(Y) != ColM(Y))
772 {
773 dw_Error(SIZE_ERR);
774 return (TMatrix)NULL;
775 }
776 bTransposeInPlace(pElementM(Y),RowM(Y));
777 }
778 else
779 {
780 if (!X)
781 {
782 if (!(X=CreateMatrix(ColM(Y),RowM(Y))))
783 return (TMatrix)NULL;
784 }
785 else
786 if ((RowM(X) != ColM(Y)) || (ColM(X) != RowM(Y)))
787 {
788 dw_Error(SIZE_ERR);
789 return (TMatrix)NULL;
790 }
791 if (MajorForm(X) == MajorForm(Y))
792 bTranspose(pElementM(X),pElementM(Y),RowM(Y),ColM(Y),MajorForm(Y));
793 else
794 memcpy(pElementM(X),pElementM(Y),RowM(Y)*ColM(Y)*sizeof(PRECISION));
795 }
796 return X;
797 }
798
799 /*
800 Assumes
801 X : (rows x cols) matrix or null pointer
802 Y : (r x c) matrix, where r >= brow+rows and c >= bcol+cols
803
804 Results
805 X(i,j) = Y(brow+i,bcol+j), for 0 <= i < rows and 0 <= j < cols. If
806 X is null pointer, then the matrix X is created.
807
808 Returns
809 Returns X upon success and null on failure. Call GetError() to
810 determine the cause of failure.
811 */
812 TMatrix SubMatrix(TMatrix X, TMatrix Y, int brow, int bcol, int rows, int cols)
813 {
814 int i, j, k, s, r, m;
815 PRECISION *pX, *pY;
816 if (!Y)
817 {
818 dw_Error(NULL_ERR);
819 return (TMatrix)NULL;
820 }
821 if ((brow+rows > RowM(Y)) || (bcol+cols > ColM(Y)))
822 {
823 dw_Error(SIZE_ERR);
824 return (TMatrix)NULL;
825 }
826 if (Y == X)
827 {
828 dw_Error(ARG_ERR);
829 return (TMatrix)NULL;
830 }
831 if (!X)
832 {
833 if (!(X=CreateMatrix(rows,cols)))
834 return (TMatrix)NULL;
835 }
836 else
837 if ((rows != RowM(X)) || (cols != ColM(X)))
838 {
839 dw_Error(SIZE_ERR);
840 return (TMatrix)NULL;
841 }
842
843 if (MajorForm(Y) == MajorForm(X))
844 if (MajorForm(Y))
845 {
846 if (rows == RowM(Y))
847 memcpy(pElementM(X),&ElementM(Y,0,bcol),rows*cols*sizeof(PRECISION));
848 else
849 for (s=rows*sizeof(PRECISION), j=cols-1; j >= 0; j--)
850 memcpy(&ElementM(X,0,j),&ElementM(Y,brow,bcol+j),s);
851 }
852 else
853 {
854 if (cols == ColM(Y))
855 memcpy(pElementM(X),&ElementM(Y,brow,0),rows*cols*sizeof(PRECISION));
856 else
857 for (s=cols*sizeof(PRECISION), j=rows-1; j >= 0; j--)
858 memcpy(&ElementM(X,j,0),&ElementM(Y,brow+j,bcol),s);
859 }
860 else
861 if (MajorForm(X))
862 {
863 s=ColM(Y), m=brow*s, r=(brow+rows-1)*s, pX=pElementM(X), pY=pElementM(Y);
864 for (k=rows*cols-1, j=bcol+cols-1; j >= bcol; j--)
865 for (i=r+j; i >= m ; k--, i-=s)
866 pX[k]=pY[i];
867 }
868 else
869 {
870 s=RowM(Y), m=bcol*s, r=(bcol+cols-1)*s, pX=pElementM(X), pY=pElementM(Y), k=rows*cols-1;
871 for (i=brow+rows-1; i >= brow; i--)
872 for (j=r+i; j >= m ; k--, j-=s)
873 pX[k]=pY[j];
874 }
875
876 return X;
877 }
878
879 /*
880 Assumes
881 x : d-dimensional vector or null pointer
882 y : n-dimensional vector, where n >= b+d
883
884 Results
885 x(i) = y(b+i), for 0 <= i < d. If x is a null pointer, then the vector x is
886 created.
887
888 Returns
889 Returns x upon success and null on failure. Call GetError() to determine
890 the cause of failure.
891 */
892 TVector SubVector(TVector x, TVector y, int b, int d)
893 {
894 if (!y)
895 {
896 dw_Error(NULL_ERR);
897 return (TVector)NULL;
898 }
899 if (b+d > DimV(y))
900 {
901 dw_Error(SIZE_ERR);
902 return (TVector)NULL;
903 }
904 if (!x)
905 {
906 if (!(x=CreateVector(d)))
907 return (TVector)NULL;
908 }
909 else
910 if (d != DimV(x))
911 {
912 dw_Error(SIZE_ERR);
913 return (TVector)NULL;
914 }
915 if (x != y)
916 memcpy(pElementV(x),pElementV(y)+b,d*sizeof(PRECISION));
917 return x;
918 }
919
920 /*
921 Assumes
922 X : (rX x cX) matrix, where rX >= brow_X+rows and cX >= bcol_X+cols
923 Y : (rY x cY) matrix, where rY >= brow_Y+rows and cY >= bcol_Y+cols
924
925 Results
926 X(brow_X+i,bcol_X+j) = Y(brow_Y+i,bcol_Y+j), for 0 <= i < rows and
927 0 <= j < cols.
928
929 Returns
930 Returns X upon success and null on failure. Call GetError() to
931 determine the cause of failure.
932 */
933 TMatrix InsertSubMatrix(TMatrix X, TMatrix Y, int brow_X, int bcol_X, int brow_Y, int bcol_Y, int rows, int cols)
934 {
935 int i, j, s;
936 if (!X || !Y)
937 {
938 dw_Error(NULL_ERR);
939 return (TMatrix)NULL;
940 }
941 if ((brow_X < 0) || (bcol_X < 0) || (brow_Y < 0) || (bcol_Y < 0) || (rows <= 0) || (cols <= 0)
942 || (brow_Y+rows > RowM(Y)) || (bcol_Y+cols > ColM(Y)) || (brow_X+rows > RowM(X)) || (bcol_X+cols > ColM(X)))
943 {
944 dw_Error(SIZE_ERR);
945 return (TMatrix)NULL;
946 }
947 if (Y == X)
948 {
949 dw_Error(ARG_ERR);
950 return (TMatrix)NULL;
951 }
952 if (MajorForm(Y) == MajorForm(X))
953 if (MajorForm(Y))
954 {
955 if ((rows == RowM(Y)) && (rows == RowM(X)))
956 memcpy(&ElementM(X,0,bcol_X),&ElementM(Y,0,bcol_Y),rows*cols*sizeof(PRECISION));
957 else
958 for (s=rows*sizeof(PRECISION), j=cols-1; j >= 0; j--)
959 memcpy(&ElementM(X,brow_X,bcol_X+j),&ElementM(Y,brow_Y,bcol_Y+j),s);
960 }
961 else
962 {
963 if ((cols == ColM(Y)) && (cols == ColM(X)))
964 memcpy(&ElementM(X,brow_X,0),&ElementM(Y,brow_Y,0),rows*cols*sizeof(PRECISION));
965 else
966 for (s=cols*sizeof(PRECISION), i=rows-1; i >= 0; i--)
967 memcpy(&ElementM(X,brow_X+i,bcol_X),&ElementM(Y,brow_Y+i,bcol_Y),s);
968 }
969 else
970 for (i=rows-1; i >= 0; i--)
971 for (j=cols-1; j >= 0; j--)
972 ElementM(X,brow_X+i,bcol_X+j)=ElementM(Y,brow_Y+i,bcol_Y+j);
973
974 return X;
975 }
976
977 /*
978 Assumes
979 X : m x n matrix
980 y : m-vector
981 col : 0 <= col < n
982
983 Results
984 X(i,col) = y(i), for 0 <= i < m.
985
986 Returns
987 Returns X upon success and null on failure. Call GetError() to
988 determine the cause of failure.
989 */
990 TMatrix InsertColumnVector(TMatrix X, TVector y, int col)
991 {
992 int i, n;
993 PRECISION *pX;
994 if (!X || !y)
995 {
996 dw_Error(NULL_ERR);
997 return (TMatrix)NULL;
998 }
999 if ((DimV(y) != RowM(X)) || (col < 0) || (ColM(X) <= col))
1000 {
1001 dw_Error(SIZE_ERR);
1002 return (TMatrix)NULL;
1003 }
1004 if (MajorForm(X))
1005 memcpy(pElementM(X)+col*DimV(y),pElementV(y),DimV(y)*sizeof(PRECISION));
1006 else
1007 for (pX=pElementM(X)+(n=ColM(X))*(i=DimV(y)-1)+col; i >= 0; pX-=n, i--) *pX=ElementV(y,i);
1008 return X;
1009 }
1010
1011 /*
1012 Assumes
1013 X : m x n matrix
1014 y : n-vector
1015 row : 0 <= row < n
1016
1017 Results
1018 X(row,j) = y(j), for 0 <= j < n.
1019
1020 Returns
1021 Returns X upon success and null on failure. Call GetError() to
1022 determine the cause of failure.
1023 */
1024 TMatrix InsertRowVector(TMatrix X, TVector y, int row)
1025 {
1026 int j, m;
1027 PRECISION *pX;
1028 if (!X || !y)
1029 {
1030 dw_Error(NULL_ERR);
1031 return (TMatrix)NULL;
1032 }
1033 if ((DimV(y) != ColM(X)) || (row < 0) || (RowM(X) <= row))
1034 {
1035 dw_Error(SIZE_ERR);
1036 return (TMatrix)NULL;
1037 }
1038 if (MajorForm(X))
1039 for (pX=pElementM(X)+(m=RowM(X))*(j=DimV(y)-1)+row; j >= 0; pX-=m, j--) *pX=ElementV(y,j);
1040 else
1041 memcpy(pElementM(X)+row*DimV(y),pElementV(y),DimV(y)*sizeof(PRECISION));
1042 return X;
1043 }
1044
1045 /*
1046 Assumes
1047 x : m-vector or null pointer
1048 Y : m x n matrix
1049 col : 0 <= col < n
1050
1051 Results
1052 x(i) = Y(i,col), for 0 <= i < m. If x is null pointer, then the vector x
1053 is created.
1054
1055 Returns
1056 Returns x upon success and null on failure. Call GetError() to
1057 determine the cause of failure.
1058 */
1059 TVector ColumnVector(TVector x, TMatrix Y, int col)
1060 {
1061 int i;
1062 PRECISION *pY;
1063 if (!Y)
1064 {
1065 dw_Error(NULL_ERR);
1066 return (TVector)NULL;
1067 }
1068 if ((col < 0) || (ColM(Y) <= col))
1069 {
1070 dw_Error(SIZE_ERR);
1071 return (TVector)NULL;
1072 }
1073 if (!x)
1074 {
1075 if (!(x=CreateVector(RowM(Y))))
1076 return (TVector)NULL;
1077 }
1078 else
1079 if (DimV(x) != RowM(Y))
1080 {
1081 dw_Error(SIZE_ERR);
1082 return (TVector)NULL;
1083 }
1084 if (MajorForm(Y))
1085 memcpy(pElementV(x),&ElementM(Y,0,col),DimV(x)*sizeof(PRECISION));
1086 else
1087 for (pY=&ElementM(Y,i=DimV(x)-1,col); i >= 0; pY-=ColM(Y), i--) ElementV(x,i)=*pY;
1088 return x;
1089 }
1090
1091 /*
1092 Assumes
1093 x : n-vector or null pointer
1094 Y : m x n matrix
1095 row : 0 <= row < m
1096
1097 Results
1098 x(j) = Y(row,j), for 0 <= j < n. If x is null pointer, then the vector x
1099 is created.
1100
1101 Returns
1102 Returns x upon success and null on failure. Call GetError() to
1103 determine the cause of failure.
1104 */
1105 TVector RowVector(TVector x, TMatrix Y, int row)
1106 {
1107 int j, m;
1108 PRECISION *pY;
1109 if (!Y)
1110 {
1111 dw_Error(NULL_ERR);
1112 return (TVector)NULL;
1113 }
1114 if ((row < 0) || (RowM(Y) <= row))
1115 {
1116 dw_Error(SIZE_ERR);
1117 return (TVector)NULL;
1118 }
1119 if (!x)
1120 {
1121 if (!(x=CreateVector(ColM(Y))))
1122 return (TVector)NULL;
1123 }
1124 else
1125 if (DimV(x) != ColM(Y))
1126 {
1127 dw_Error(SIZE_ERR);
1128 return (TVector)NULL;
1129 }
1130 if (MajorForm(Y))
1131 for (pY=pElementM(Y)+row+(m=RowM(Y))*(j=DimV(x)-1); j >= 0; pY-=m, j--) ElementV(x,j)=*pY;
1132 else
1133 memcpy(pElementV(x),pElementM(Y)+row*DimV(x),DimV(x)*sizeof(PRECISION));
1134 return x;
1135 }
1136 /*******************************************************************************/
1137 /*******************************************************************************/
1138 /*******************************************************************************/
1139
1140
1141 /*******************************************************************************/
1142 /***************************** Addition Routines *******************************/
1143 /*******************************************************************************/
1144 /*
1145 Assumes
1146 x : m-vector or null pointer
1147 y : m-vector
1148 z : m-vector
1149
1150 Results
1151 x = y + z. If x is null pointer, x is created.
1152
1153 Returns
1154 Returns x upon success and null on failure. Call GetError() to
1155 determine the cause of failure.
1156
1157 Notes
1158 x, y, and z do not have to be distinct vectors
1159 */
1160 TVector AddVV(TVector x, TVector y, TVector z)
1161 {
1162 if (!y || !z)
1163 {
1164 dw_Error(NULL_ERR);
1165 return (TVector)NULL;
1166 }
1167 if (DimV(y) != DimV(z))
1168 {
1169 dw_Error(SIZE_ERR);
1170 return (TVector)NULL;
1171 }
1172 if (!x)
1173 {
1174 if (!(x=CreateVector(DimV(y))))
1175 return (TVector)NULL;
1176 }
1177 else
1178 if (DimV(x) != DimV(y))
1179 {
1180 dw_Error(SIZE_ERR);
1181 return (TVector)NULL;
1182 }
1183 bAddV(pElementV(x),pElementV(y),pElementV(z),DimV(y));
1184 return x;
1185 }
1186
1187 /*
1188 Assumes
1189 x : m-vector or null pointer
1190 y : m-vector
1191 z : m-vector
1192
1193 Results
1194 x = y - z. If x is null pointer, x is created.
1195
1196 Returns
1197 Returns x upon success and null on failure. Call GetError() to
1198 determine the cause of failure.
1199
1200 Notes
1201 x, y, and z do not have to be distinct vectors
1202 */
1203 TVector SubtractVV(TVector x, TVector y, TVector z)
1204 {
1205 if (!y || !z)
1206 {
1207 dw_Error(NULL_ERR);
1208 return (TVector)NULL;
1209 }
1210 if (DimV(y) != DimV(z))
1211 {
1212 dw_Error(SIZE_ERR);
1213 return (TVector)NULL;
1214 }
1215 if (!x)
1216 {
1217 if (!(x=CreateVector(DimV(y))))
1218 return (TVector)NULL;
1219 }
1220 else
1221 if (DimV(x) != DimV(y))
1222 {
1223 dw_Error(SIZE_ERR);
1224 return (TVector)NULL;
1225 }
1226 bSubtractV(pElementV(x),pElementV(y),pElementV(z),DimV(y));
1227 return x;
1228 }
1229
1230 /*
1231 Assumes
1232 X : m x n matrix or null pointer
1233 Y : m x n matrix
1234 Z : m x n matrix
1235
1236 Results
1237 X = Y + Z. If X is null pointer, x is created.
1238
1239 Returns
1240 Returns X upon success and null on failure. Call GetError() to
1241 determine the cause of failure.
1242
1243 Notes
1244 X, Y, and Z do not have to be distinct matrices
1245 */
1246 TMatrix AddMM(TMatrix X, TMatrix Y, TMatrix Z)
1247 {
1248 if (!Y || !Z)
1249 {
1250 dw_Error(NULL_ERR);
1251 return (TMatrix)NULL;
1252 }
1253 if ((RowM(Y) != RowM(Z)) || (ColM(Y) != ColM(Z)))
1254 {
1255 dw_Error(SIZE_ERR);
1256 return (TMatrix)NULL;
1257 }
1258 if (!X)
1259 {
1260 if (!(X=CreateMatrix(RowM(Z),ColM(Z))))
1261 return (TMatrix)NULL;
1262 }
1263 else
1264 if ((RowM(X) != RowM(Z)) || (ColM(X) != ColM(Z)))
1265 {
1266 dw_Error(SIZE_ERR);
1267 return (TMatrix)NULL;
1268 }
1269 bAddM(pElementM(X),pElementM(Y),pElementM(Z),RowM(Z),ColM(Z),MajorForm(X),MajorForm(Y),MajorForm(Z));
1270 return X;
1271 }
1272
1273 /*
1274 Assumes
1275 X : m x n matrix or null pointer
1276 Y : m x n matrix
1277 Z : m x n matrix
1278
1279 Results
1280 X = Y - Z. If X is null pointer, X is created.
1281
1282 Returns
1283 Returns X upon success and null on failure. Call GetError() to
1284 determine the cause of failure.
1285
1286 Notes
1287 X, Y, and Z do not have to be distinct matrices
1288 */
1289 TMatrix SubtractMM(TMatrix X, TMatrix Y, TMatrix Z)
1290 {
1291 if (!Y || !Z)
1292 {
1293 dw_Error(NULL_ERR);
1294 return (TMatrix)NULL;
1295 }
1296 if ((RowM(Y) != RowM(Z)) || (ColM(Y) != ColM(Z)))
1297 {
1298 dw_Error(SIZE_ERR);
1299 return (TMatrix)NULL;
1300 }
1301 if (!X)
1302 {
1303 if (!(X=CreateMatrix(RowM(Z),ColM(Z))))
1304 return (TMatrix)NULL;
1305 }
1306 else
1307 if ((RowM(X) != RowM(Z)) || (ColM(X) != ColM(Z)))
1308 {
1309 dw_Error(SIZE_ERR);
1310 return (TMatrix)NULL;
1311 }
1312 bSubtractM(pElementM(X),pElementM(Y),pElementM(Z),RowM(Z),ColM(Z),MajorForm(X),MajorForm(Y),MajorForm(Z));
1313 return X;
1314 }
1315
1316 /*
1317 Assumes
1318 x : m vector or null pointer
1319 a : scalar
1320 y : m vector
1321 b : scalar
1322 z : m vector
1323
1324 Results
1325 x = a*y + b*z. If X is null pointer, X is created.
1326
1327 Returns
1328 Returns X upon success and null on failure. Call GetError() to
1329 determine the cause of failure.
1330
1331 Notes
1332 x, y, and z do not have to be distinct vectors
1333 */
1334 TVector LinearCombinationV(TVector x, PRECISION a, TVector y, PRECISION b, TVector z)
1335 {
1336 if (!y || !z)
1337 {
1338 dw_Error(NULL_ERR);
1339 return (TVector)NULL;
1340 }
1341 if (DimV(y) != DimV(z))
1342 {
1343 dw_Error(SIZE_ERR);
1344 return (TVector)NULL;
1345 }
1346 if (!x)
1347 {
1348 if (!(x=CreateVector(DimV(z))))
1349 return (TVector)NULL;
1350 }
1351 else
1352 if (DimV(x) != DimV(z))
1353 {
1354 dw_Error(SIZE_ERR);
1355 return (TVector)NULL;
1356 }
1357 bLinearCombinationV(pElementV(x),a,pElementV(y),b,pElementV(z),DimV(z));
1358 return x;
1359 }
1360
1361 /*
1362 Assumes
1363 X : m x n matrix or null pointer
1364 a : scalar
1365 Y : m x n matrix
1366 b : scalar
1367 Z : m x n matrix
1368
1369 Results
1370 X = a*Y + b*Z. If X is null pointer, X is created.
1371
1372 Returns
1373 Returns X upon success and null on failure. Call GetError() to
1374 determine the cause of failure.
1375
1376 Notes
1377 X, Y, and Z do not have to be distinct matrices
1378 */
1379 TMatrix LinearCombinationM(TMatrix X, PRECISION a, TMatrix Y, PRECISION b, TMatrix Z)
1380 {
1381 PRECISION *ptr;
1382 if (!Y || !Z)
1383 {
1384 dw_Error(NULL_ERR);
1385 return (TMatrix)NULL;
1386 }
1387 if ((RowM(Y) != RowM(Z)) || (ColM(Y) != ColM(Z)))
1388 {
1389 dw_Error(SIZE_ERR);
1390 return (TMatrix)NULL;
1391 }
1392 if (!X)
1393 {
1394 if (!(X=CreateMatrix(RowM(Z),ColM(Z))))
1395 return (TMatrix)NULL;
1396 }
1397 else
1398 if ((RowM(X) != RowM(Z)) || (ColM(X) != ColM(Z)))
1399 {
1400 dw_Error(SIZE_ERR);
1401 return (TMatrix)NULL;
1402 }
1403 if ((X == Z) || (X == Y))
1404 {
1405 if (!(ptr=(PRECISION*)dw_malloc(RowM(X)*ColM(X)*sizeof(PRECISION))))
1406 {
1407 dw_Error(MEM_ERR);
1408 return (TMatrix)NULL;
1409 }
1410 memcpy(ptr,pElementM(X),RowM(X)*ColM(X)*sizeof(PRECISION));
1411 bLinearCombinationM(pElementM(X),a,(Y == X) ? ptr : pElementM(Y),b,(Z == X) ? ptr : pElementM(Z),
1412 RowM(X),ColM(X),MajorForm(X),MajorForm(Y),MajorForm(Z));
1413 dw_free(ptr);
1414 }
1415 else
1416 bLinearCombinationM(pElementM(X),a,pElementM(Y),b,pElementM(Z),RowM(X),ColM(X),MajorForm(X),MajorForm(Y),MajorForm(Z));
1417 return X;
1418 }
1419
1420 /*
1421 Assumes
1422 a : scalar
1423 x : m-vector
1424 b : scalar
1425 Y : m x n matrix
1426 z : n-vector
1427
1428 Results
1429 x = a*x + b*Y*z
1430
1431 Returns
1432 Returns x upon success and null on failure. Call GetError() to
1433 determine the cause of failure.
1434
1435 Notes
1436 x and z do not have to be distinct vectors.
1437 */
1438 TVector UpdateProductMV(PRECISION a, TVector x, PRECISION b, TMatrix Y, TVector z)
1439 {
1440 PRECISION *ptr;
1441 if (!x || !Y || ! z)
1442 {
1443 dw_Error(NULL_ERR);
1444 return (TVector)NULL;
1445 }
1446 if ((DimV(x) != RowM(Y)) || (ColM(Y) != DimV(z)))
1447 {
1448 dw_Error(SIZE_ERR);
1449 return (TVector)NULL;
1450 }
1451 if (x == z)
1452 {
1453 if (!(ptr=(PRECISION*)dw_malloc(DimV(x)*sizeof(PRECISION))))
1454 {
1455 dw_Error(MEM_ERR);
1456 return (TVector)NULL;
1457 }
1458 memcpy(ptr,pElementV(x),DimV(x)*sizeof(PRECISION));
1459 bProductMV_Update(pElementV(x),ptr,pElementM(Y),a,b,DimV(x),DimV(x),MajorForm(Y));
1460 dw_free(ptr);
1461 }
1462 else
1463 bProductMV_Update(pElementV(x),pElementV(z),pElementM(Y),a,b,DimV(x),DimV(z),MajorForm(Y));
1464 return x;
1465 }
1466
1467 /*
1468 Assumes
1469 a : scalar
1470 x : m-vector
1471 b : scalar
1472 y : n-vector
1473 Z : n x m matrix
1474
1475 Results
1476 x = a*x + b*y*Z
1477
1478 Returns
1479 Returns x upon success and null on failure. Call GetError() to
1480 determine the cause of failure.
1481
1482 Notes
1483 x and y do not have to be distinct vectors.
1484 */
1485 TVector UpdateProductVM(PRECISION a, TVector x, PRECISION b, TVector y, TMatrix Z)
1486 {
1487 PRECISION *ptr;
1488 if (!x || !y || ! Z)
1489 {
1490 dw_Error(NULL_ERR);
1491 return (TVector)NULL;
1492 }
1493 if ((DimV(x) != ColM(Z)) || (RowM(Z) != DimV(y)))
1494 {
1495 dw_Error(SIZE_ERR);
1496 return (TVector)NULL;
1497 }
1498 if (x == y)
1499 {
1500 if (!(ptr=(PRECISION*)dw_malloc(DimV(x)*sizeof(PRECISION))))
1501 {
1502 dw_Error(MEM_ERR);
1503 return (TVector)NULL;
1504 }
1505 memcpy(ptr,pElementV(x),DimV(x)*sizeof(PRECISION));
1506 bProductMV_Update(pElementV(x),ptr,pElementM(Z),a,b,DimV(x),DimV(x),1^MajorForm(Y));
1507 dw_free(ptr);
1508 }
1509 else
1510 bProductMV_Update(pElementV(x),pElementV(y),pElementM(Z),a,b,DimV(x),DimV(y),1^MajorForm(Y));
1511 return x;
1512 }
1513
1514 /*
1515 Assumes
1516 a : scalar
1517 X : m x n matrix
1518 b : scalar
1519 Y : m x k matrix
1520 Z : k x n matrix
1521
1522 Results
1523 X = a*X + b*Y*Z
1524
1525 Returns
1526 Returns X upon success and null on failure. Call GetError() to
1527 determine the cause of failure.
1528
1529 Notes
1530 X, Y, and Z do not have to be distinct matrices.
1531 */
1532 TMatrix UpdateProductMM(PRECISION a, TMatrix X, PRECISION b, TMatrix Y, TMatrix Z)
1533 {
1534 PRECISION *ptr;
1535 if (!X || !Y || ! Z)
1536 {
1537 dw_Error(NULL_ERR);
1538 return (TMatrix)NULL;
1539 }
1540 if ((RowM(X) != RowM(Y)) || (ColM(X) != ColM(Z)) || (ColM(Y) != RowM(Z)))
1541 {
1542 dw_Error(SIZE_ERR);
1543 return (TMatrix)NULL;
1544 }
1545 if ((X == Y) || (X == Z))
1546 {
1547 if (!(ptr=(PRECISION*)dw_malloc(RowM(X)*ColM(X)*sizeof(PRECISION))))
1548 {
1549 dw_Error(MEM_ERR);
1550 return (TMatrix)NULL;
1551 }
1552 memcpy(ptr,pElementM(X),RowM(X)*ColM(X)*sizeof(PRECISION));
1553 bProductMM_Update(pElementM(X),(X == Y) ? ptr : pElementM(Y),(X == Z) ? ptr : pElementM(Z),a,b,RowM(X),
1554 ColM(X),ColM(Y),MajorForm(X),MajorForm(Y),MajorForm(Z));
1555 dw_free(ptr);
1556 }
1557 else
1558 bProductMM_Update(pElementM(X),pElementM(Y),pElementM(Z),a,b,RowM(X),ColM(X),ColM(Y),MajorForm(X),MajorForm(Y),MajorForm(Z));
1559 return X;
1560 }
1561
1562 /*
1563 Assumes
1564 a : scalar
1565 X : m x n matrix
1566 b : scalar
1567 Y : m x k matrix
1568 Z : k x n matrix
1569
1570 Results
1571 X = a*X + b*Y'*Z
1572
1573 Returns
1574 Returns X upon success and null on failure. Call GetError() to
1575 determine the cause of failure.
1576
1577 Notes
1578 X, Y, and Z do not have to be distinct matrices.
1579 */
1580 TMatrix UpdateTransposeProductMM(PRECISION a, TMatrix X, PRECISION b, TMatrix Y, TMatrix Z)
1581 {
1582 PRECISION *ptr;
1583 if (!X || !Y || ! Z)
1584 {
1585 dw_Error(NULL_ERR);
1586 return (TMatrix)NULL;
1587 }
1588 if ((RowM(X) != RowM(Y)) || (ColM(X) != ColM(Z)) || (ColM(Y) != RowM(Z)))
1589 {
1590 dw_Error(SIZE_ERR);
1591 return (TMatrix)NULL;
1592 }
1593 if ((X == Y) || (X == Z))
1594 {
1595 if (!(ptr=(PRECISION*)dw_malloc(RowM(X)*ColM(X)*sizeof(PRECISION))))
1596 {
1597 dw_Error(MEM_ERR);
1598 return (TMatrix)NULL;
1599 }
1600 memcpy(ptr,pElementM(X),RowM(X)*ColM(X)*sizeof(PRECISION));
1601 bProductMM_Update(pElementM(X),(X == Y) ? ptr : pElementM(Y),(X == Z) ? ptr : pElementM(Z),a,b,RowM(X),
1602 ColM(X),ColM(Y),MajorForm(X),1^MajorForm(Y),MajorForm(Z));
1603 dw_free(ptr);
1604 }
1605 else
1606 bProductMM_Update(pElementM(X),pElementM(Y),pElementM(Z),a,b,RowM(X),ColM(X),ColM(Y),MajorForm(X),1^MajorForm(Y),MajorForm(Z));
1607 return X;
1608 }
1609 /*******************************************************************************/
1610 /*******************************************************************************/
1611 /*******************************************************************************/
1612
1613
1614 /*******************************************************************************/
1615 /************************** Multiplication Routines ****************************/
1616 /*******************************************************************************/
1617 /*
1618 Assumes
1619 x : m-vector or null pointer
1620 y : m-vector
1621 s : scalar
1622
1623 Results
1624 x = s*y. If x is null pointer, x is created.
1625
1626 Returns
1627 Returns x upon success and null on failure. Call GetError() to
1628 determine the cause of failure.
1629
1630 Notes
1631 x and y do not have to be distinct
1632 */
1633 TVector ProductSV(TVector x, PRECISION s, TVector y)
1634 {
1635 if (!y)
1636 {
1637 dw_Error(NULL_ERR);
1638 return (TVector)NULL;
1639 }
1640 if (!x)
1641 {
1642 if (!(x=CreateVector(DimV(y))))
1643 return (TVector)NULL;
1644 }
1645 else
1646 if (DimV(x) != DimV(y))
1647 {
1648 dw_Error(SIZE_ERR);
1649 return (TVector)NULL;
1650 }
1651 bMultiply(pElementV(x),pElementV(y),s,DimV(y));
1652 return x;
1653 }
1654
1655 /*
1656 Assumes
1657 X : m x n matrix or null pointer
1658 y : m x n matix
1659 s : scalar
1660
1661 Results
1662 X = s*Y. If X is null pointer, X is created.
1663
1664 Returns
1665 Returns X upon success and null on failure. Call GetError() to
1666 determine the cause of failure.
1667
1668 Notes
1669 X and Y do not have to be distinct
1670 */
1671 TMatrix ProductSM(TMatrix X, PRECISION s, TMatrix Y)
1672 {
1673 if (!Y)
1674 {
1675 dw_Error(NULL_ERR);
1676 return (TMatrix)NULL;
1677 }
1678 if (!X)
1679 {
1680 if (!(X=CreateMatrix(RowM(Y),ColM(Y))))
1681 return (TMatrix)NULL;
1682 }
1683 else
1684 if ((RowM(X) != RowM(Y)) || (ColM(X) != ColM(Y)))
1685 {
1686 dw_Error(SIZE_ERR);
1687 return (TMatrix)NULL;
1688 }
1689 bMultiply(pElementM(X),pElementM(Y),s,RowM(Y)*ColM(Y));
1690 SetMajorForm(X,MajorForm(Y));
1691 return X;
1692 }
1693
1694 /*
1695 Assumes
1696 x : n-vector or null pointer
1697 y : m-vector
1698 Z : m x n matrix
1699
1700 Results
1701 x = y * Z. If x is null pointer, x is created.
1702
1703 Returns
1704 Returns x upon success and null on failure. Call GetError() to
1705 determine the cause of failure.
1706
1707 Notes
1708 If Z is square, x and y do not have to be distinct
1709 */
1710 TVector ProductVM(TVector x, TVector y, TMatrix Z)
1711 {
1712 PRECISION *ptr;
1713 if (!y || !Z)
1714 {
1715 dw_Error(NULL_ERR);
1716 return (TVector)NULL;
1717 }
1718 if (DimV(y) != RowM(Z))
1719 {
1720 dw_Error(SIZE_ERR);
1721 return (TVector)NULL;
1722 }
1723 if (!x)
1724 {
1725 if (!(x=CreateVector(ColM(Z))))
1726 return (TVector)NULL;
1727 }
1728 else
1729 {
1730 if (DimV(x) != ColM(Z))
1731 {
1732 dw_Error(SIZE_ERR);
1733 return (TVector)NULL;
1734 }
1735 if (x == y)
1736 {
1737 if (!(ptr=(PRECISION*)dw_malloc(DimV(x)*sizeof(PRECISION))))
1738 {
1739 dw_Error(MEM_ERR);
1740 return (TVector)NULL;
1741 }
1742 //bMatrixMultiply(ptr,pElementV(y),pElementM(Z),1,DimV(x),DimV(y),0,0,MajorForm(Z));
1743 bProductVM(ptr,pElementV(y),pElementM(Z),RowM(Z),ColM(Z),MajorForm(Z));
1744 memcpy(pElementV(x),ptr,DimV(x)*sizeof(PRECISION));
1745 dw_free(ptr);
1746 return x;
1747 }
1748 }
1749 //bMatrixMultiply(pElementV(x),pElementV(y),pElementM(Z),1,DimV(x),DimV(y),0,0,MajorForm(Z));
1750 bProductVM(pElementV(x),pElementV(y),pElementM(Z),RowM(Z),ColM(Z),MajorForm(Z));
1751 return x;
1752 }
1753
1754 /*
1755 Assumes
1756 x : m-vector or null pointer
1757 Y : m x n matrix
1758 z : n-vector
1759
1760 Results
1761 x = Y * z. If x is null pointer, x is created.
1762
1763 Returns
1764 Returns x upon success and null on failure. Call GetError() to
1765 determine the cause of failure.
1766
1767 Notes
1768 If Y is square, x and z do not have to be distinct
1769 */
1770 TVector ProductMV(TVector x, TMatrix Y, TVector z)
1771 {
1772 PRECISION *ptr;
1773 if (!Y || !z)
1774 {
1775 dw_Error(NULL_ERR);
1776 return (TVector)NULL;
1777 }
1778 if (ColM(Y) != DimV(z))
1779 {
1780 dw_Error(SIZE_ERR);
1781 return (TVector)NULL;
1782 }
1783 if (!x)
1784 {
1785 if (!(x=CreateVector(RowM(Y))))
1786 return (TVector)NULL;
1787 }
1788 else
1789 {
1790 if (DimV(x) != RowM(Y))
1791 {
1792 dw_Error(SIZE_ERR);
1793 return (TVector)NULL;
1794 }
1795 if (x == z)
1796 {
1797 if (!(ptr=(PRECISION*)dw_malloc(DimV(x)*sizeof(PRECISION))))
1798 {
1799 dw_Error(MEM_ERR);
1800 return (TVector)NULL;
1801 }
1802 bProductMV(ptr,pElementV(z),pElementM(Y),RowM(Y),ColM(Y),MajorForm(Y));
1803 memcpy(pElementV(x),ptr,DimV(x)*sizeof(PRECISION));
1804 dw_free(ptr);
1805 return x;
1806 }
1807 }
1808 bProductMV(pElementV(x),pElementV(z),pElementM(Y),RowM(Y),ColM(Y),MajorForm(Y));
1809 return x;
1810 }
1811
1812
1813 /*
1814 Assumes
1815 X : m x n matrix or null pointer
1816 Y : m x r matrix
1817 Z : r x n matrix
1818
1819 Results
1820 X = Y * Z. If X is null pointer, X is created.
1821
1822 Returns
1823 Returns X upon success and null on failure. Call GetError() to
1824 determine the cause of failure.
1825
1826 Notes
1827 Size permitting, X, Y and Z do not have to be distinct.
1828 */
1829 TMatrix ProductMM(TMatrix X, TMatrix Y, TMatrix Z)
1830 {
1831 PRECISION *ptr;
1832 if (!Y || !Z)
1833 {
1834 dw_Error(NULL_ERR);
1835 return (TMatrix)NULL;
1836 }
1837 if (ColM(Y) != RowM(Z))
1838 {
1839 dw_Error(SIZE_ERR);
1840 return (TMatrix)NULL;
1841 }
1842 if (!X)
1843 {
1844 if (!(X=CreateMatrix(RowM(Y),ColM(Z))))
1845 return (TMatrix)NULL;
1846 }
1847 else
1848 {
1849 if ((RowM(X) != RowM(Y)) || (ColM(X) != ColM(Z)))
1850 {
1851 dw_Error(SIZE_ERR);
1852 return (TMatrix)NULL;
1853 }
1854 if ((X == Y) || (X == Z))
1855 {
1856 if (!(ptr=(PRECISION*)dw_malloc(RowM(X)*ColM(X)*sizeof(PRECISION))))
1857 {
1858 dw_Error(MEM_ERR);
1859 return (TMatrix)NULL;
1860 }
1861 bProductMM(ptr,pElementM(Y),pElementM(Z),RowM(X),ColM(X),ColM(Y),MajorForm(X),MajorForm(Y),MajorForm(Z));
1862 memcpy(pElementM(X),ptr,RowM(X)*ColM(X)*sizeof(PRECISION));
1863 dw_free(ptr);
1864 return X;
1865 }
1866 }
1867 bProductMM(pElementM(X),pElementM(Y),pElementM(Z),RowM(X),ColM(X),ColM(Y),MajorForm(X),MajorForm(Y),MajorForm(Z));
1868 return X;
1869 }
1870
1871 /*
1872 Assumes
1873 X : m x n matrix or null pointer
1874 Y : m x n matrix
1875 Z : n x n upper triangular matrix
1876
1877 Results
1878 X = Y * Z. If X is null pointer, X is created.
1879
1880 Returns
1881 Returns X upon success and null on failure. Call GetError() to
1882 determine the cause of failure.
1883
1884 Notes
1885 Size permitting, X, Y and Z do not have to be distinct.
1886
1887 When using MKL, for large matrices (m and n more than 50) it is may be more
1888 efficient to use ProductMM().
1889 */
1890 TMatrix ProductMU(TMatrix X, TMatrix Y, TMatrix Z)
1891 {
1892 PRECISION *ptr;
1893 if (!Y || !Z)
1894 {
1895 dw_Error(NULL_ERR);
1896 return (TMatrix)NULL;
1897 }
1898 if ((ColM(Y) != RowM(Z)) || (RowM(Z) != ColM(Z)))
1899 {
1900 dw_Error(SIZE_ERR);
1901 return (TMatrix)NULL;
1902 }
1903 if (!X)
1904 {
1905 if (!(X=CreateMatrix(RowM(Y),ColM(Z))))
1906 return (TMatrix)NULL;
1907 }
1908 else
1909 if ((RowM(X) != RowM(Y)) || (ColM(X) != ColM(Z)))
1910 {
1911 dw_Error(SIZE_ERR);
1912 return (TMatrix)NULL;
1913 }
1914 if (X == Z)
1915 if (!(ptr=(PRECISION*)dw_malloc(RowM(X)*ColM(X)*sizeof(PRECISION))))
1916 {
1917 dw_Error(MEM_ERR);
1918 return (TMatrix)NULL;
1919 }
1920 else
1921 memcpy(ptr,pElementM(Z),RowM(Z)*ColM(Z)*sizeof(PRECISION));
1922 else
1923 ptr=pElementM(Z);
1924 if (X != Y)
1925 {
1926 if (MajorForm(X) == MajorForm(Y))
1927 memcpy(pElementM(X),pElementM(Y),RowM(Y)*ColM(Y)*sizeof(PRECISION));
1928 else
1929 bTranspose(pElementM(X),pElementM(Y),RowM(Y),ColM(Y),MajorForm(Y));
1930 }
1931 bProductMT(pElementM(X),ptr,RowM(X),ColM(X),1,MajorForm(X),MajorForm(Z));
1932 if (ptr != pElementM(Z)) dw_free(ptr);
1933 return X;
1934 }
1935
1936 /*
1937 Assumes
1938 X : m x n matrix or null pointer
1939 Y : m x n matrix
1940 Z : n x n upper triangular matrix
1941
1942 Results
1943 X = Y * Z. If X is null pointer, X is created.
1944
1945 Returns
1946 Returns X upon success and null on failure. Call GetError() to
1947 determine the cause of failure.
1948
1949 Notes
1950 Size permitting, X, Y and Z do not have to be distinct.
1951
1952 When using MKL, for large matrices (m and n more than 50) it is may be more
1953 efficient to use ProductMM().
1954 */
1955 TMatrix ProductML(TMatrix X, TMatrix Y, TMatrix Z)
1956 {
1957 PRECISION *ptr;
1958 if (!Y || !Z)
1959 {
1960 dw_Error(NULL_ERR);
1961 return (TMatrix)NULL;
1962 }
1963 if ((ColM(Y) != RowM(Z)) || (RowM(Z) != ColM(Z)))
1964 {
1965 dw_Error(SIZE_ERR);
1966 return (TMatrix)NULL;
1967 }
1968 if (!X)
1969 {
1970 if (!(X=CreateMatrix(RowM(Y),ColM(Z))))
1971 return (TMatrix)NULL;
1972 }
1973 else
1974 if ((RowM(X) != RowM(Y)) || (ColM(X) != ColM(Z)))
1975 {
1976 dw_Error(SIZE_ERR);
1977 return (TMatrix)NULL;
1978 }
1979 if (X == Z)
1980 if (!(ptr=(PRECISION*)dw_malloc(RowM(X)*ColM(X)*sizeof(PRECISION))))
1981 {
1982 dw_Error(MEM_ERR);
1983 return (TMatrix)NULL;
1984 }
1985 else
1986 memcpy(ptr,pElementM(Z),RowM(Z)*ColM(Z)*sizeof(PRECISION));
1987 else
1988 ptr=pElementM(Z);
1989 if (X != Y)
1990 {
1991 if (MajorForm(X) == MajorForm(Y))
1992 memcpy(pElementM(X),pElementM(Y),RowM(Y)*ColM(Y)*sizeof(PRECISION));
1993 else
1994 bTranspose(pElementM(X),pElementM(Y),RowM(Y),ColM(Y),MajorForm(Y));
1995 }
1996 bProductMT(pElementM(X),ptr,RowM(X),ColM(X),0,MajorForm(X),MajorForm(Z));
1997 if (ptr != pElementM(Z)) dw_free(ptr);
1998 return X;
1999 }
2000
2001 /*
2002 Assumes
2003 X : m x n matrix or null pointer
2004 Y : m x m lower triangular matrix
2005 Z : m x n matrix
2006
2007 Results
2008 X = Y * Z. If X is a null pointer, X is created.
2009
2010 Returns
2011 Returns X upon success and null on failure. Call GetError() to
2012 determine the cause of failure.
2013
2014 Notes
2015 Size permitting, X, Y and Z do not have to be distinct.
2016
2017 When using MKL, for large matrices (m and n more than 50) it is may be more
2018 efficient to use ProductMM().
2019 */
2020 TMatrix ProductUM(TMatrix X, TMatrix Y, TMatrix Z)
2021 {
2022 PRECISION *ptr;
2023 if (!Y || !Z)
2024 {
2025 dw_Error(NULL_ERR);
2026 return (TMatrix)NULL;
2027 }
2028 if ((ColM(Y) != RowM(Z)) || (RowM(Y) != ColM(Y)))
2029 {
2030 dw_Error(SIZE_ERR);
2031 return (TMatrix)NULL;
2032 }
2033 if (!X)
2034 {
2035 if (!(X=CreateMatrix(RowM(Y),ColM(Z))))
2036 return (TMatrix)NULL;
2037 }
2038 else
2039 if ((RowM(X) != RowM(Y)) || (ColM(X) != ColM(Z)))
2040 {
2041 dw_Error(SIZE_ERR);
2042 return (TMatrix)NULL;
2043 }
2044 if (X == Y)
2045 if (!(ptr=(PRECISION*)dw_malloc(RowM(Y)*ColM(Y)*sizeof(PRECISION))))
2046 {
2047 dw_Error(MEM_ERR);
2048 return (TMatrix)NULL;
2049 }
2050 else
2051 memcpy(ptr,pElementM(Y),RowM(Y)*ColM(Y)*sizeof(PRECISION));
2052 else
2053 ptr=pElementM(Y);
2054 if (X != Z)
2055 {
2056 if (MajorForm(X) == MajorForm(Z))
2057 memcpy(pElementM(X),pElementM(Z),RowM(Z)*ColM(Z)*sizeof(PRECISION));
2058 else
2059 bTranspose(pElementM(X),pElementM(Z),RowM(Z),ColM(Z),MajorForm(Z));
2060 }
2061 bProductTM(pElementM(X),ptr,RowM(X),ColM(X),1,MajorForm(X),MajorForm(Y));
2062 if (ptr != pElementM(Y)) dw_free(ptr);
2063 return X;
2064 }
2065
2066 /*
2067 Assumes
2068 X : m x n matrix or null pointer
2069 Y : m x m lower triangular matrix
2070 Z : m x n matrix
2071
2072 Results
2073 X = Y * Z. If X is a null pointer, X is created.
2074
2075 Returns
2076 Returns X upon success and null on failure. Call GetError() to
2077 determine the cause of failure.
2078
2079 Notes
2080 Size permitting, X, Y and Z do not have to be distinct.
2081
2082 When using MKL, for large matrices (m and n more than 50) it is may be more
2083 efficient to use ProductMM().
2084 */
2085 TMatrix ProductLM(TMatrix X, TMatrix Y, TMatrix Z)
2086 {
2087 PRECISION *ptr;
2088 if (!Y || !Z)
2089 {
2090 dw_Error(NULL_ERR);
2091 return (TMatrix)NULL;
2092 }
2093 if ((ColM(Y) != RowM(Z)) || (RowM(Y) != ColM(Y)))
2094 {
2095 dw_Error(SIZE_ERR);
2096 return (TMatrix)NULL;
2097 }
2098 if (!X)
2099 {
2100 if (!(X=CreateMatrix(RowM(Y),ColM(Z))))
2101 return (TMatrix)NULL;
2102 }
2103 else
2104 if ((RowM(X) != RowM(Y)) || (ColM(X) != ColM(Z)))
2105 {
2106 dw_Error(SIZE_ERR);
2107 return (TMatrix)NULL;
2108 }
2109 if (X == Y)
2110 if (!(ptr=(PRECISION*)dw_malloc(RowM(Y)*ColM(Y)*sizeof(PRECISION))))
2111 {
2112 dw_Error(MEM_ERR);
2113 return (TMatrix)NULL;
2114 }
2115 else
2116 memcpy(ptr,pElementM(Y),RowM(Y)*ColM(Y)*sizeof(PRECISION));
2117 else
2118 ptr=pElementM(Y);
2119 if (X != Z)
2120 {
2121 if (MajorForm(X) == MajorForm(Z))
2122 memcpy(pElementM(X),pElementM(Z),RowM(Z)*ColM(Z)*sizeof(PRECISION));
2123 else
2124 bTranspose(pElementM(X),pElementM(Z),RowM(Z),ColM(Z),MajorForm(Z));
2125 }
2126 bProductTM(pElementM(X),ptr,RowM(X),ColM(X),0,MajorForm(X),MajorForm(Y));
2127 if (ptr != pElementM(Y)) dw_free(ptr);
2128 return X;
2129 }
2130 /*******************************************************************************/
2131 /*******************************************************************************/
2132
2133
2134 /*******************************************************************************/
2135 /********************* Multiplication/Transpose Routines ***********************/
2136 /*******************************************************************************/
2137 /*
2138 Assumes
2139 X : m x n matrix or null pointer
2140 Y : r x m matrix
2141 Z : r x n matrix
2142
2143 Results
2144 X = Y'*Z. If X is null pointer, X is created.
2145
2146 Returns
2147 Returns X upon success and null on failure. Call GetError() to
2148 determine the cause of failure.
2149
2150 Notes
2151 Y and Z do not have to be distinct matrices.
2152 If Y is square, X and Z do not have to be distinct matrices.
2153 If Z is square, X and Y do not have to be distinct matrices.
2154 */
2155 TMatrix TransposeProductMM(TMatrix X, TMatrix Y, TMatrix Z)
2156 {
2157 PRECISION *ptr;
2158 if (!Y || !Z)
2159 {
2160 dw_Error(NULL_ERR);
2161 return (TMatrix)NULL;
2162 }
2163 if (RowM(Y) != RowM(Z))
2164 {
2165 dw_Error(SIZE_ERR);
2166 return (TMatrix)NULL;
2167 }
2168 if (!X)
2169 {
2170 if (!(X=CreateMatrix(ColM(Y),ColM(Z))))
2171 return (TMatrix)NULL;
2172 }
2173 else
2174 {
2175 if ((RowM(X) != ColM(Y)) || (ColM(X) != ColM(Z)))
2176 {
2177 dw_Error(SIZE_ERR);
2178 return (TMatrix)NULL;
2179 }
2180 if ((X == Y) || (X == Z))
2181 {
2182 if (!(ptr=(PRECISION*)dw_malloc(RowM(X)*ColM(X)*sizeof(PRECISION))))
2183 {
2184 dw_Error(MEM_ERR);
2185 return (TMatrix)NULL;
2186 }
2187 bProductMM(ptr,pElementM(Y),pElementM(Z),RowM(X),ColM(X),RowM(Y),MajorForm(X),1^MajorForm(Y),MajorForm(Z));
2188 memcpy(pElementM(X),ptr,RowM(X)*ColM(X)*sizeof(PRECISION));
2189 dw_free(ptr);
2190 return X;
2191 }
2192 }
2193 bProductMM(pElementM(X),pElementM(Y),pElementM(Z),RowM(X),ColM(X),RowM(Y),MajorForm(X),1^MajorForm(Y),MajorForm(Z));
2194 return X;
2195 }
2196
2197 /*
2198 Assumes
2199 X : m x n matrix or null pointer
2200 Y : m x p matrix
2201 Z : n x p matrix
2202
2203 Results
2204 X = Y*Z'. If X is null pointer, X is created.
2205
2206 Returns
2207 Returns X upon success and null on failure. Call GetError() to
2208 determine the cause of failure.
2209
2210 Notes
2211 Y and Z do not have to be distinct matrices.
2212 If Y is square, X and Z do not have to be distinct matrices.
2213 If Z is square, X and Y do not have to be distinct matrices.
2214 */
2215 TMatrix ProductTransposeMM(TMatrix X, TMatrix Y, TMatrix Z)
2216 {
2217 PRECISION *ptr;
2218 if (!Y || !Z)
2219 {
2220 dw_Error(NULL_ERR);
2221 return (TMatrix)NULL;
2222 }
2223 if (ColM(Y) != ColM(Z))
2224 {
2225 dw_Error(SIZE_ERR);
2226 return (TMatrix)NULL;
2227 }
2228 if (!X)
2229 {
2230 if (!(X=CreateMatrix(RowM(Y),RowM(Z))))
2231 return (TMatrix)NULL;
2232 }
2233 else
2234 {
2235 if ((RowM(X) != RowM(Y)) || (ColM(X) != RowM(Z)))
2236 {
2237 dw_Error(SIZE_ERR);
2238 return (TMatrix)NULL;
2239 }
2240 if ((X == Y) || (X == Z))
2241 {
2242 if (!(ptr=(PRECISION*)dw_malloc(RowM(X)*ColM(X)*sizeof(PRECISION))))
2243 {
2244 dw_Error(MEM_ERR);
2245 return (TMatrix)NULL;
2246 }
2247 bProductMM(ptr,pElementM(Y),pElementM(Z),RowM(X),ColM(X),ColM(Y),MajorForm(X),MajorForm(Y),1^MajorForm(Z));
2248 memcpy(pElementM(X),ptr,RowM(X)*ColM(X)*sizeof(PRECISION));
2249 dw_free(ptr);
2250 return X;
2251 }
2252 }
2253 bProductMM(pElementM(X),pElementM(Y),pElementM(Z),RowM(X),ColM(X),ColM(Y),MajorForm(X),MajorForm(Y),1^MajorForm(Z));
2254 return X;
2255 }
2256 /*******************************************************************************/
2257 /*******************************************************************************/
2258
2259
2260 /*******************************************************************************/
2261 /********************** Multiplication/Inverse Routines ************************/
2262 /*******************************************************************************/
2263 /*
2264 Assumes
2265 x : m vector or null
2266 Y : m x m matrix
2267 z : m vector
2268
2269 Results
2270 x = Inverse(Y) * z.
2271
2272 Returns
2273 Returns x upon success, null otherwise.
2274
2275 Notes
2276 The vectors x and z do not have to be distinct. Uses Gaussian elimination
2277 with partial pivoting and back substitution. A null return indicates
2278 that either Y or z was null, Y was singular, the matrices were not of the
2279 required size, or the routine was unable to allocate needed memory. Call
2280 GetError() to determine which of these occured.
2281 */
2282 TVector InverseProductMV(TVector x, TMatrix Y, TVector z)
2283 {
2284 int err;
2285 TVector rtrn=(TVector)NULL;
2286 if (!Y || !z)
2287 dw_Error(NULL_ERR);
2288 else
2289 if ((RowM(Y) != ColM(Y)) || (ColM(Y) != DimV(z)))
2290 dw_Error(SIZE_ERR);
2291 else
2292 {
2293 if ((rtrn=((x == z) ? x : EquateVector(x,z))))
2294 if ((err=bSolveLU(pElementV(rtrn),pElementM(Y),DimV(rtrn),1,1,MajorForm(Y))))
2295 {
2296 if (x != rtrn) FreeVector(rtrn);
2297 rtrn=(TVector)NULL;
2298 dw_Error(err);
2299 }
2300 }
2301 return rtrn;
2302 }
2303
2304 /*
2305 Assumes
2306 x : m vector or null
2307 Y : m x m matrix upper triangular matrix
2308 z : m vector
2309
2310 Results
2311 x = Inverse(Y) * z.
2312
2313 Returns
2314 Returns x upon success, null otherwise.
2315
2316 Notes
2317 The vectors x and z do not have to be distinct. A null return indicates
2318 that either Y or z was null, Y was singular, or the matrices were not of the
2319 required size. Call GetError() to determine which of these occured.
2320 */
2321 TVector InverseProductUV(TVector x, TMatrix Y, TVector z)
2322 {
2323 TVector rtrn=(TVector)NULL;
2324 int err;
2325 if (!Y || !z)
2326 dw_Error(NULL_ERR);
2327 else
2328 if ((RowM(Y) != ColM(Y)) || (ColM(Y) != DimV(z)))
2329 dw_Error(SIZE_ERR);
2330 else
2331 if ((rtrn=((x == z) ? x : EquateVector(x,z))))
2332 if ((err=bSolveTriangular(pElementM(Y),pElementV(rtrn),DimV(rtrn),1,1,MajorForm(Y),1)))
2333 {
2334 if (rtrn != x) FreeVector(rtrn);
2335 rtrn=(TVector)NULL;
2336 dw_Error(err);
2337 }
2338 return rtrn;
2339 }
2340
2341 /*
2342 Assumes
2343 x : m vector or null
2344 Y : m x m matrix lower triangular matrix
2345 z : m vector
2346
2347 Results
2348 x = Inverse(Y) * z.
2349
2350 Returns
2351 Returns x upon success, null otherwise.
2352
2353 Notes
2354 The vectors x and z do not have to be distinct. Uses back substitution.
2355 A null return indicates that either Y or z was null, Y was singular, or
2356 the matrices were not of the required size. Call GetError() to
2357 determine which of these occured.
2358 */
2359 TVector InverseProductLV(TVector x, TMatrix Y, TVector z)
2360 {
2361 TVector rtrn=(TVector)NULL;
2362 int err;
2363 if (!Y || !z)
2364 dw_Error(NULL_ERR);
2365 else
2366 if ((RowM(Y) != ColM(Y)) || (ColM(Y) != DimV(z)))
2367 dw_Error(SIZE_ERR);
2368 else
2369 if ((rtrn=((x == z) ? x : EquateVector(x,z))))
2370 if ((err=bSolveTriangular(pElementM(Y),pElementV(rtrn),DimV(rtrn),1,0,MajorForm(Y),1)))
2371 {
2372 if (rtrn != x) FreeVector(rtrn);
2373 rtrn=(TVector)NULL;
2374 dw_Error(err);
2375 }
2376 return rtrn;
2377 }
2378
2379 /*
2380 Assumes
2381 X : m x n matrix or null
2382 Y : m x m matrix
2383 Z : m x n matrix
2384
2385 Results
2386 X = Inverse(Y) * Z.
2387
2388 Returns
2389 Returns X upon success, null otherwise.
2390
2391 Notes
2392 Size permitting, X, Y and Z do not have to be distinct. Uses Gaussian
2393 elimination with partial pivoting and back substitution. A null return
2394 indicates that either Y or Z was null, Y was singular, the matrices were
2395 not of the required size, or the routine was unable to allocate needed
2396 memory. Call GetError() to determine which of these occured.
2397 */
2398 TMatrix InverseProductMM(TMatrix X, TMatrix Y, TMatrix Z)
2399 {
2400 int err, yt;
2401 TMatrix rtrn=(TMatrix)NULL;
2402 PRECISION *pY;
2403 if (!Y || !Z)
2404 dw_Error(NULL_ERR);
2405 else
2406 if ((RowM(Y) != ColM(Y)) || (ColM(Y) != RowM(Z)))
2407 dw_Error(SIZE_ERR);
2408 else
2409 {
2410 yt=MajorForm(Y);
2411 if (X != Y)
2412 pY=pElementM(Y);
2413 else
2414 if (!(pY=(PRECISION*)dw_malloc(RowM(Y)*RowM(Y)*sizeof(PRECISION))))
2415 {
2416 dw_Error(MEM_ERR);
2417 return (TMatrix)NULL;
2418 }
2419 else
2420 memcpy(pY,pElementM(Y),RowM(Y)*RowM(Y)*sizeof(PRECISION));
2421 if ((rtrn=((X == Z) ? X : EquateMatrix(X,Z))))
2422 if ((err=bSolveLU(pElementM(rtrn),pY,RowM(rtrn),ColM(rtrn),MajorForm(rtrn),yt)))
2423 {
2424 if (X != rtrn) FreeMatrix(rtrn);
2425 rtrn=(TMatrix)NULL;
2426 dw_Error(err);
2427 }
2428 if (pY != pElementM(Y)) dw_free(pY);
2429 }
2430 return rtrn;
2431 }
2432
2433 /*
2434 Assumes
2435 X : m x n matrix or null
2436 Y : m x m upper triangular matrix
2437 Z : m x n matrix
2438
2439 Results
2440 X = Inverse(Y) * Z.
2441
2442 Returns
2443 Returns X upon success, null otherwise.
2444
2445 Notes
2446 Size permitting, X, Y and Z do not have to be distinct. Uses back
2447 substitution. A null return indicates that either Y or Z was null,
2448 Y was singular, or the matrices were not of the required size. Call
2449 GetError() to determine which of these occured.
2450 */
2451 TMatrix InverseProductUM(TMatrix X, TMatrix Y, TMatrix Z)
2452 {
2453 int err, yt;
2454 TMatrix rtrn=(TMatrix)NULL;
2455 PRECISION *pY;
2456 if (!Y || !Z)
2457 dw_Error(NULL_ERR);
2458 else
2459 if ((RowM(Y) != ColM(Y)) || (ColM(Y) != RowM(Z)))
2460 dw_Error(SIZE_ERR);
2461 else
2462 {
2463 yt=MajorForm(Y);
2464 if (X != Y)
2465 pY=pElementM(Y);
2466 else
2467 if (!(pY=(PRECISION*)dw_malloc(RowM(Y)*RowM(Y)*sizeof(PRECISION))))
2468 {
2469 dw_Error(MEM_ERR);
2470 return (TMatrix)NULL;
2471 }
2472 else
2473 memcpy(pY,pElementM(Y),RowM(Y)*RowM(Y)*sizeof(PRECISION));
2474 if ((rtrn=((X == Z) ? X : EquateMatrix(X,Z))))
2475 if ((err=bSolveTriangular(pY,pElementM(rtrn),RowM(rtrn),ColM(rtrn),1,yt,MajorForm(rtrn))))
2476 {
2477 if (X != rtrn) FreeMatrix(rtrn);
2478 rtrn=(TMatrix)NULL;
2479 dw_Error(err);
2480 }
2481 if (pY != pElementM(Y)) dw_free(pY);
2482 }
2483 return rtrn;
2484 }
2485
2486 /*
2487 Assumes
2488 X : m x n matrix or null
2489 Y : m x m lower triangular matrix
2490 Z : m x n matrix
2491
2492 Results
2493 X = Inverse(Y) * Z.
2494
2495 Returns
2496 Returns X upon success, null otherwise.
2497
2498 Notes
2499 Size permitting, X, Y and Z do not have to be distinct. Uses back
2500 substitution. A null return indicates that either Y or Z was null,
2501 Y was singular, or the matrices were not of the required size. Call
2502 GetError() to determine which of these occured.
2503 */
2504 TMatrix InverseProductLM(TMatrix X, TMatrix Y, TMatrix Z)
2505 {
2506 int err, yt;
2507 TMatrix rtrn=(TMatrix)NULL;
2508 PRECISION *pY;
2509 if (!Y || !Z)
2510 dw_Error(NULL_ERR);
2511 else
2512 if ((RowM(Y) != ColM(Y)) || (ColM(Y) != RowM(Z)))
2513 dw_Error(SIZE_ERR);
2514 else
2515 {
2516 yt=MajorForm(Y);
2517 if (X != Y)
2518 pY=pElementM(Y);
2519 else
2520 if (!(pY=(PRECISION*)dw_malloc(RowM(Y)*RowM(Y)*sizeof(PRECISION))))
2521 {
2522 dw_Error(MEM_ERR);
2523 return (TMatrix)NULL;
2524 }
2525 else
2526 memcpy(pY,pElementM(Y),RowM(Y)*RowM(Y)*sizeof(PRECISION));
2527 if ((rtrn=((X == Z) ? X : EquateMatrix(X,Z))))
2528 if ((err=bSolveTriangular(pY,pElementM(rtrn),RowM(rtrn),ColM(rtrn),0,yt,MajorForm(rtrn))))
2529 {
2530 if (X != rtrn) FreeMatrix(rtrn);
2531 rtrn=(TMatrix)NULL;
2532 dw_Error(err);
2533 }
2534 if (pY != pElementM(Y)) dw_free(pY);
2535 }
2536 return rtrn;
2537 }
2538
2539 /*
2540 Assumes
2541 x : n vector or null pointer
2542 y : n vector
2543 Z : n x n invertible matrix
2544
2545 Results
2546 x = y * Inverse(Z).
2547
2548 Returns
2549 Returns x upon success, null otherwise.
2550
2551 Notes
2552 The vectors x and y do not have to be distinct. Uses Gaussian elimination
2553 with partial pivoting and back substitution. A null return indicates
2554 that either y or Z was null, Z was singular, the matrices were not of the
2555 required size, or the routine was unable to allocate needed memory. Call
2556 GetError() to determine which of these occured.
2557 */
2558 TVector ProductInverseVM(TVector x, TVector y, TMatrix Z)
2559 {
2560 int err;
2561 TVector rtrn=(TVector)NULL;
2562 if (!y || !Z)
2563 dw_Error(NULL_ERR);
2564 else
2565 if ((RowM(Z) != ColM(Z)) || (DimV(y) != RowM(Z)))
2566 dw_Error(SIZE_ERR);
2567 else
2568 {
2569 if ((rtrn=((x == y) ? x : EquateVector(x,y))))
2570 if ((err=bSolveLU(pElementV(rtrn),pElementM(Z),DimV(rtrn),1,1,1^MajorForm(Z))))
2571 {
2572 if (x != rtrn) FreeVector(rtrn);
2573 rtrn=(TVector)NULL;
2574 dw_Error(err);
2575 }
2576 }
2577 return rtrn;
2578 }
2579
2580 /*
2581 Assumes
2582 x : n vector or null pointer
2583 y : n vector
2584 Z : n x n upper triangular matrix
2585
2586 Results
2587 x = y * Inverse(Z).
2588
2589 Returns
2590 Returns x upon success, null otherwise.
2591
2592 Notes
2593 The vectors x and y do not have to be distinct. Uses back substitution.
2594 A null return indicates that either y or Z was null, Z was singular, or
2595 the matrices were not of the required size. Call GetError() to
2596 determine which of these occured.
2597 */
2598 TVector ProductInverseVU(TVector x, TVector y, TMatrix Z)
2599 {
2600 TVector rtrn=(TVector)NULL;
2601 int err;
2602 if (!y || !Z)
2603 dw_Error(NULL_ERR);
2604 else
2605 if ((RowM(Z) != ColM(Z)) || (DimV(y) != RowM(Z)))
2606 dw_Error(SIZE_ERR);
2607 else
2608 if ((rtrn=((x == y) ? x : EquateVector(x,y))))
2609 if ((err=bSolveTriangular(pElementM(Z),pElementV(rtrn),DimV(rtrn),1,0,1^MajorForm(Z),1)))
2610 {
2611 if (rtrn != x) FreeVector(rtrn);
2612 rtrn=(TVector)NULL;
2613 dw_Error(err);
2614 }
2615 return rtrn;
2616 }
2617
2618 /*
2619 Assumes
2620 x : n vector or null pointer
2621 y : n vector
2622 Z : n x n lower triangular matrix
2623
2624 Results
2625 x = y * Inverse(Z).
2626
2627 Returns
2628 Returns x upon success, null otherwise.
2629
2630 Notes
2631 The vectors x and y do not have to be distinct. Uses back substitution.
2632 A null return indicates that either y or Z was null, Z was singular, or
2633 the matrices were not of the required size. Call GetError() to
2634 determine which of these occured.
2635 */
2636 TVector ProductInverseVL(TVector x, TVector y, TMatrix Z)
2637 {
2638 TVector rtrn=(TVector)NULL;
2639 int err;
2640 if (!y || !Z)
2641 dw_Error(NULL_ERR);
2642 else
2643 if ((RowM(Z) != ColM(Z)) || (DimV(y) != RowM(Z)))
2644 dw_Error(SIZE_ERR);
2645 else
2646 if ((rtrn=((x == y) ? x : EquateVector(x,y))))
2647 if ((err=bSolveTriangular(pElementM(Z),pElementV(rtrn),DimV(rtrn),1,1,1^MajorForm(Z),1)))
2648 {
2649 if (rtrn != x) FreeVector(rtrn);
2650 rtrn=(TVector)NULL;
2651 dw_Error(err);
2652 }
2653 return rtrn;
2654 }
2655
2656 /*
2657 Assumes
2658 X : m x n matrix
2659 Y : m x n matrix
2660 Z : n x n matrix
2661
2662 Results
2663 X = Y * Inverse(Z).
2664
2665 Returns
2666 Returns X upon success, null otherwise.
2667
2668 Notes
2669 Size permitting, X, Y and Z do not have to be distinct. Uses Gaussian
2670 elimination with partial pivoting and back substitution. A null return
2671 indicates that either Y or Z was null, Z was singular, the matrices were
2672 not of the required size, or the routine was unable to allocate needed
2673 memory. Call GetError() to determine which of these occured.
2674 */
2675 TMatrix ProductInverseMM(TMatrix X, TMatrix Y, TMatrix Z)
2676 {
2677 int err, zt;
2678 TMatrix rtrn=(TMatrix)NULL;
2679 PRECISION *pZ;
2680 if (!Y || !Z)
2681 dw_Error(NULL_ERR);
2682 else
2683 if ((RowM(Z) != ColM(Z)) || (ColM(Y) != RowM(Z)))
2684 dw_Error(SIZE_ERR);
2685 else
2686 {
2687 zt=1^MajorForm(Z);
2688 if (X != Z)
2689 pZ=pElementM(Z);
2690 else
2691 if (!(pZ=(PRECISION*)dw_malloc(RowM(Z)*RowM(Z)*sizeof(PRECISION))))
2692 {
2693 dw_Error(MEM_ERR);
2694 return (TMatrix)NULL;
2695 }
2696 else
2697 memcpy(pZ,pElementM(Z),RowM(Z)*RowM(Z)*sizeof(PRECISION));
2698 if ((rtrn=((X == Y) ? X : EquateMatrix(X,Y))))
2699 if ((err=bSolveLU(pElementM(rtrn),pZ,ColM(rtrn),RowM(rtrn),1^MajorForm(rtrn),zt)))
2700 {
2701 if (X != rtrn) FreeMatrix(rtrn);
2702 rtrn=(TMatrix)NULL;
2703 dw_Error(err);
2704 }
2705 if (pZ != pElementM(Z)) dw_free(pZ);
2706 }
2707 return rtrn;
2708 }
2709
2710 /*
2711 Assumes
2712 X : m x n matrix
2713 Y : m x n matrix
2714 Z : n x n upper triangular matrix
2715
2716 Results
2717 X = Y * Inverse(Z).
2718
2719 Returns
2720 Returns X upon success, null otherwise.
2721
2722 Notes
2723 Size permitting, X, Y and Z do not have to be distinct. Uses back
2724 substitution. A null return indicates that either Y or Z was null,
2725 Z was singular, or the matrices were not of the required size. Call
2726 Getdw_Error() to determine which of these occured.
2727 */
2728 TMatrix ProductInverseMU(TMatrix X, TMatrix Y, TMatrix Z)
2729 {
2730 int err, zt;
2731 TMatrix rtrn=(TMatrix)NULL;
2732 PRECISION *pZ;
2733 if (!Y || !Z)
2734 dw_Error(NULL_ERR);
2735 else
2736 if ((RowM(Z) != ColM(Z)) || (ColM(Y) != RowM(Z)))
2737 dw_Error(SIZE_ERR);
2738 else
2739 {
2740 zt=1^MajorForm(Z);
2741 if (X != Z)
2742 pZ=pElementM(Z);
2743 else
2744 if (!(pZ=(PRECISION*)dw_malloc(RowM(Z)*RowM(Z)*sizeof(PRECISION))))
2745 {
2746 dw_Error(MEM_ERR);
2747 return (TMatrix)NULL;
2748 }
2749 else
2750 memcpy(pZ,pElementM(Z),RowM(Z)*RowM(Z)*sizeof(PRECISION));
2751 if ((rtrn=((X == Y) ? X : EquateMatrix(X,Y))))
2752 if ((err=bSolveTriangular(pZ,pElementM(rtrn),ColM(rtrn),RowM(rtrn),0,zt,1^MajorForm(rtrn))))
2753 {
2754 if (X != rtrn) FreeMatrix(rtrn);
2755 rtrn=(TMatrix)NULL;
2756 dw_Error(err);
2757 }
2758 if (pZ != pElementM(Z)) dw_free(pZ);
2759 }
2760 return rtrn;
2761 }
2762
2763 /*
2764 Assumes
2765 X : m x n matrix
2766 Y : m x n matrix
2767 Z : n x n upper triangular matrix
2768
2769 Results
2770 X = Y * Inverse(Z).
2771
2772 Returns
2773 Returns X upon success, null otherwise.
2774
2775 Notes
2776 Size permitting, X, Y and Z do not have to be distinct. Uses back
2777 substitution. A null return indicates that either Y or Z was null,
2778 Z was singular, or the matrices were not of the required size. Call
2779 GetError() to determine which of these occured.
2780 */
2781 TMatrix ProductInverseML(TMatrix X, TMatrix Y, TMatrix Z)
2782 {
2783 int err, zt;
2784 TMatrix rtrn=(TMatrix)NULL;
2785 PRECISION *pZ;
2786 if (!Y || !Z)
2787 dw_Error(NULL_ERR);
2788 else
2789 if ((RowM(Z) != ColM(Z)) || (ColM(Y) != RowM(Z)))
2790 dw_Error(SIZE_ERR);
2791 else
2792 {
2793 zt=1^MajorForm(Z);
2794 if (X != Z)
2795 pZ=pElementM(Z);
2796 else
2797 if (!(pZ=(PRECISION*)dw_malloc(RowM(Z)*RowM(Z)*sizeof(PRECISION))))
2798 {
2799 dw_Error(MEM_ERR);
2800 return (TMatrix)NULL;
2801 }
2802 else
2803 memcpy(pZ,pElementM(Z),RowM(Z)*RowM(Z)*sizeof(PRECISION));
2804 if ((rtrn=((X == Y) ? X : EquateMatrix(X,Y))))
2805 if ((err=bSolveTriangular(pZ,pElementM(rtrn),ColM(rtrn),RowM(rtrn),1,zt,1^MajorForm(rtrn))))
2806 {
2807 if (X != rtrn) FreeMatrix(rtrn);
2808 rtrn=(TMatrix)NULL;
2809 dw_Error(err);
2810 }
2811 if (pZ != pElementM(Z)) dw_free(pZ);
2812 }
2813 return rtrn;
2814 }
2815 /*******************************************************************************/
2816 /*******************************************************************************/
2817
2818 /*******************************************************************************/
2819 /************************** Matrix Inverse Routines ****************************/
2820 /*******************************************************************************/
2821 /*
2822 Assumes
2823 X : m x m matrix or null pointer
2824 Y : m x m matrix
2825
2826 Results
2827 X = inverse(Y)
2828
2829 Returns
2830 Returns X upon success, null otherwise
2831
2832 Notes
2833 The matrices X and Y do not have to be distinct. Uses Gaussian
2834 elimination with partial pivoting and back substitution to form the
2835 inverse. A null return indicates that either Y or Z was null, Y was
2836 singular, or unable to allocate required memory. Call GetError()
2837 to determine which of these occured.
2838 */
2839 TMatrix Inverse_LU(TMatrix X, TMatrix Y)
2840 {
2841 PRECISION *LU;
2842 int *p;
2843 TMatrix rtrn=(TMatrix)NULL;
2844 if (!Y)
2845 dw_Error(NULL_ERR);
2846 else
2847 if (RowM(Y) != ColM(Y))
2848 dw_Error(SIZE_ERR);
2849 else
2850 if (!(p=(int*)dw_malloc(RowM(Y)*sizeof(int))))
2851 dw_Error(MEM_ERR);
2852 else
2853 {
2854 if (!(LU=(PRECISION*)dw_malloc(RowM(Y)*RowM(Y)*sizeof(PRECISION))))
2855 dw_Error(MEM_ERR);
2856 else
2857 {
2858 memcpy(LU,pElementM(Y),RowM(Y)*RowM(Y)*sizeof(PRECISION));
2859 if (bLU(p,LU,RowM(Y),RowM(Y),MajorForm(Y)))
2860 dw_Error(SING_ERR);
2861 else
2862 if ((rtrn=IdentityMatrix(X,RowM(Y))))
2863 {
2864 bPermutationMultiply(p,pElementM(rtrn),RowM(rtrn),ColM(rtrn),RowM(Y),1,MajorForm(rtrn));
2865 bSolveUnitTriangular(LU,pElementM(rtrn),RowM(rtrn),ColM(rtrn),0,MajorForm(Y),MajorForm(rtrn));
2866 bSolveTriangular(LU,pElementM(rtrn),RowM(rtrn),ColM(rtrn),1,MajorForm(Y),MajorForm(rtrn));
2867 }
2868 dw_free(LU);
2869 }
2870 dw_free(p);
2871 }
2872 return rtrn;
2873 }
2874
2875 /*
2876 Assumes
2877 X : m x m matrix or null pointer
2878 Y : m x m matrix
2879
2880 Results
2881 X = inverse(Y)
2882
2883 Returns
2884 Returns X upon success, null otherwise
2885
2886 Notes
2887 The matrices X and Y do not have to be distinct. Uses singular value
2888 decomposition to form the inverse. A null return indicates that either
2889 Y or Z was null, Y was singular, or unable to allocate required memory.
2890 Call GetError() to determine which of these occured.
2891 */
2892 TMatrix Inverse_SVD(TMatrix X, TMatrix Y)
2893 {
2894 int i, j, k, err;
2895 TMatrix rtrn=(TMatrix)NULL;
2896 PRECISION scale, tolerance, *U, *V, *d;
2897 if (!Y)
2898 dw_Error(NULL_ERR);
2899 else
2900 if (RowM(Y) != ColM(Y))
2901 dw_Error(SIZE_ERR);
2902 else
2903 if (!(U=(PRECISION*)dw_malloc(RowM(Y)*RowM(Y)*sizeof(PRECISION))))
2904 dw_Error(MEM_ERR);
2905 else
2906 {
2907 if (!(V=(PRECISION*)dw_malloc(RowM(Y)*RowM(Y)*sizeof(PRECISION))))
2908 dw_Error(MEM_ERR);
2909 else
2910 {
2911 if (!(d=(PRECISION*)dw_malloc(RowM(Y)*sizeof(PRECISION))))
2912 dw_Error(MEM_ERR);
2913 else
2914 {
2915 if ((err=bSVD(U,d,V,pElementM(Y),RowM(Y),ColM(Y),COLUMN_MAJOR,ROW_MAJOR,MajorForm(Y),1)))
2916 dw_Error(err);
2917 else
2918 {
2919 tolerance=d[0]*MACHINE_EPSILON*RowM(Y);
2920 for (k=RowM(Y)*RowM(Y), j=RowM(Y)-1; j >= 0; j--)
2921 {
2922 if (d[j] < tolerance)
2923 {
2924 dw_Error(SING_ERR);
2925 dw_free(U);
2926 dw_free(V);
2927 dw_free(d);
2928 return (TMatrix)NULL;
2929 }
2930 scale=1.0/d[j];
2931 for (i=--k; i >= 0; i-=RowM(Y)) V[i]*=scale;
2932 //for (i=RowM(Y)-1; i >= 0; i--) V[--k]*=scale;
2933 }
2934 if (X)
2935 if ((RowM(X) != RowM(Y)) || (ColM(X) != RowM(Y)))
2936 dw_Error(SIZE_ERR);
2937 else
2938 bProductMM(pElementM(rtrn=X),V,U,RowM(Y),RowM(Y),RowM(Y),MajorForm(X),ROW_MAJOR,ROW_MAJOR);
2939 else
2940 if ((rtrn=CreateMatrix(RowM(Y),RowM(Y))))
2941 bProductMM(pElementM(rtrn),V,U,RowM(Y),RowM(Y),RowM(Y),MajorForm(rtrn),ROW_MAJOR,ROW_MAJOR);
2942 }
2943 dw_free(d);
2944 }
2945 dw_free(V);
2946 }
2947 dw_free(U);
2948 }
2949 return rtrn;
2950 }
2951
2952 /*
2953 Assumes
2954 X : m x m matrix or null pointer
2955 Y : m x m symmetric matrix
2956
2957 Results
2958 X = inverse(Y)
2959
2960 Returns
2961 Returns X upon success, null otherwise
2962
2963 Notes
2964 The matrices X and Y do not have to be distinct. Uses Cholesky
2965 decomposition to form the inverse. Only the upper half of Y is used.
2966 A null return indicates that either Y or Z was null, Y was singular,
2967 or unable to allocate required memory. Call GetError() to
2968 determine which of these occured.
2969 */
2970 TMatrix Inverse_Cholesky(TMatrix X, TMatrix Y)
2971 {
2972 TMatrix rtrn=(TMatrix)NULL;
2973 PRECISION *ptr;
2974 if (!Y)
2975 dw_Error(NULL_ERR);
2976 else
2977 if (RowM(Y) != ColM(Y))
2978 dw_Error(SIZE_ERR);
2979 else
2980 if (!(ptr=(PRECISION*)dw_malloc(RowM(Y)*RowM(Y)*sizeof(PRECISION))))
2981 dw_Error(MEM_ERR);
2982 else
2983 {
2984 memcpy(ptr,pElementM(Y),RowM(Y)*RowM(Y)*sizeof(PRECISION));
2985 if (bCholesky(ptr,RowM(Y),1,MajorForm(Y)))
2986 dw_Error(SING_ERR);
2987 else
2988 {
2989 if ((rtrn=IdentityMatrix(X,RowM(Y))))
2990 {
2991 bSolveTriangular(ptr,pElementM(rtrn),RowM(Y),RowM(Y),1,MajorForm(Y),MajorForm(rtrn));
2992 bProductMM(ptr,pElementM(rtrn),pElementM(rtrn),RowM(Y),RowM(Y),RowM(Y),MajorForm(rtrn),MajorForm(rtrn),1^MajorForm(rtrn));
2993 memcpy(pElementM(rtrn),ptr,RowM(Y)*RowM(Y)*sizeof(PRECISION));
2994 }
2995 }
2996 dw_free(ptr);
2997 }
2998 return rtrn;
2999 }
3000
3001 /*
3002 Assumes
3003 X : m x m matrix or null pointer
3004 T : m x m upper triangular matrix
3005
3006 Results
3007 X = inverse(T)
3008
3009 Returns
3010 Returns X upon success, null otherwise
3011
3012 Notes
3013 The matrices X and T do not have to be distinct. Uses back substitution
3014 to form the inverse. A null return indicates that T was null or
3015 singular, or unable to allocate required memory. Call
3016 GetError() to determine which of these occured.
3017 */
3018 TMatrix Inverse_UT(TMatrix X, TMatrix T)
3019 {
3020 PRECISION *ptr;
3021 int err;
3022 if (!T)
3023 {
3024 dw_Error(NULL_ERR);
3025 return (TMatrix)NULL;
3026 }
3027 if (RowM(T) != ColM(T))
3028 {
3029 dw_Error(SIZE_ERR);
3030 return (TMatrix)NULL;
3031 }
3032 if (!X)
3033 {
3034 if ((X=IdentityMatrix(X,RowM(T))))
3035 if ((err=bSolveTriangular(pElementM(T),pElementM(X),RowM(T),RowM(T),1,MajorForm(T),MajorForm(X))))
3036 {
3037 dw_Error(err);
3038 FreeMatrix(X);
3039 return (TMatrix)NULL;
3040 }
3041 }
3042 else
3043 if (X == T)
3044 {
3045 if (!(ptr=(PRECISION*)dw_malloc(RowM(T)*RowM(T)*sizeof(PRECISION))))
3046 {
3047 dw_Error(MEM_ERR);
3048 return (TMatrix)NULL;
3049 }
3050 memcpy(ptr,pElementM(T),RowM(T)*RowM(T)*sizeof(PRECISION));
3051 if ((X=IdentityMatrix(X,RowM(T))) )
3052 if ((err=bSolveTriangular(ptr,pElementM(X),RowM(T),RowM(T),1,MajorForm(T),MajorForm(X))))
3053 {
3054 dw_Error(err);
3055 dw_free(ptr);
3056 return (TMatrix)NULL;
3057 }
3058 dw_free(ptr);
3059 }
3060 else
3061 if ((X=IdentityMatrix(X,RowM(T))))
3062 if ((err=bSolveTriangular(pElementM(T),pElementM(X),RowM(T),RowM(T),1,MajorForm(T),MajorForm(X))))
3063 {
3064 dw_Error(err);
3065 return (TMatrix)NULL;
3066 }
3067 return X;
3068 }
3069
3070 /*
3071 Assumes
3072 X : m x m matrix or null pointer
3073 T : m x m lower triangular matrix
3074
3075 Results
3076 X = inverse(T)
3077
3078 Returns
3079 Returns X upon success, null otherwise
3080
3081 Notes
3082 The matrices X and T do not have to be distinct. Uses back substitution
3083 to form the inverse. A null return indicates that either T was null,
3084 Y was singular, or unable to allocate required memory. Call
3085 GetError() to determine which of these occured.
3086 */
3087 TMatrix Inverse_LT(TMatrix X, TMatrix T)
3088 {
3089 PRECISION *ptr;
3090 int err;
3091 if (!T)
3092 {
3093 dw_Error(NULL_ERR);
3094 return (TMatrix)NULL;
3095 }
3096 if (RowM(T) != ColM(T))
3097 {
3098 dw_Error(SIZE_ERR);
3099 return (TMatrix)NULL;
3100 }
3101 if (!X)
3102 {
3103 if ((X=IdentityMatrix(X,RowM(T))))
3104 if ((err=bSolveTriangular(pElementM(T),pElementM(X),RowM(T),RowM(T),0,MajorForm(T),MajorForm(X))))
3105 {
3106 dw_Error(err);
3107 FreeMatrix(X);
3108 return (TMatrix)NULL;
3109 }
3110 }
3111 else
3112 if (X == T)
3113 {
3114 if (!(ptr=(PRECISION*)dw_malloc(RowM(T)*RowM(T)*sizeof(PRECISION))))
3115 {
3116 dw_Error(MEM_ERR);
3117 return (TMatrix)NULL;
3118 }
3119 memcpy(ptr,pElementM(T),RowM(T)*RowM(T)*sizeof(PRECISION));
3120 if ((X=IdentityMatrix(X,RowM(T))))
3121 if ((err=bSolveTriangular(ptr,pElementM(X),RowM(T),RowM(T),0,MajorForm(T),MajorForm(X))))
3122 {
3123 dw_Error(err);
3124 dw_free(ptr);
3125 return (TMatrix)NULL;
3126 }
3127 dw_free(ptr);
3128 }
3129 else
3130 if ((X=IdentityMatrix(X,RowM(T))))
3131 if ((err=bSolveTriangular(pElementM(T),pElementM(X),RowM(T),RowM(T),0,MajorForm(T),MajorForm(X))))
3132 {
3133 dw_Error(err);
3134 return (TMatrix)NULL;
3135 }
3136 return X;
3137 }
3138 /*******************************************************************************/
3139 /*******************************************************************************/
3140 /*******************************************************************************/
3141
3142
3143 /*******************************************************************************/
3144 /****************************** Complex Routines *******************************/
3145 /*******************************************************************************/
3146 /*
3147 Assumes
3148 ReX : m x n matrix
3149 ImX : m x n matrix
3150 ReY : m x k matrix
3151 ImY : m x k matrix
3152 ReZ : k x n matrix
3153 ImZ : k x n matrix
3154
3155 Results
3156 Computes X = Y*Z, where X = ReX + i*ImX, Y = ReY + i*ImY, and
3157 Z = ReZ + i*ImZ are complex matrices.
3158
3159 Returns
3160 Returns 1 upon success, 0 otherwise
3161
3162 Notes
3163 ReX and ImX must be distinct, but none of the other matrices must be so.
3164 */
3165 int ComplexProductMM(TMatrix ReX, TMatrix ImX, TMatrix ReY, TMatrix ImY, TMatrix ReZ, TMatrix ImZ)
3166 {
3167 PRECISION *ptr1=(PRECISION*)NULL, *ptr2=(PRECISION*)NULL;
3168 if (!ReX || !ImX || !ReY || !ImY || !ReZ || !ImZ)
3169 {
3170 dw_Error(NULL_ERR);
3171 return 0;
3172 }
3173 if (ReX == ImX)
3174 {
3175 dw_Error(ARG_ERR);
3176 return 0;
3177 }
3178 if ((RowM(ReX) != RowM(ImX)) || (RowM(ReX) != RowM(ReY)) || (RowM(ReX) != RowM(ImY)) ||
3179 (ColM(ReX) != ColM(ImX)) || (ColM(ReX) != ColM(ReZ)) || (ColM(ReX) != ColM(ImZ)) ||
3180 (ColM(ReY) != ColM(ImY)) || (ColM(ReY) != RowM(ReZ)) || (ColM(ReY) != RowM(ImZ)))
3181 {
3182 dw_Error(SIZE_ERR);
3183 return 0;
3184 }
3185
3186 if ((ReX != ReY) && (ReX != ImY) && (ReX != ReZ) && (ReX != ImZ))
3187 {
3188 bProductMM(pElementM(ReX),pElementM(ReY),pElementM(ReZ),RowM(ReX),ColM(ReX),ColM(ReY),
3189 MajorForm(ReX),MajorForm(ReY),MajorForm(ReZ));
3190 bProductMM_Update(pElementM(ReX),pElementM(ImY),pElementM(ImZ),1.0,-1.0,RowM(ReX),ColM(ReX),ColM(ReY),
3191 MajorForm(ReX),MajorForm(ImY),MajorForm(ImZ));
3192 }
3193 else
3194 {
3195 ptr1=(PRECISION*)dw_malloc(RowM(ReX)*ColM(ReX)*sizeof(PRECISION));
3196 bProductMM(ptr1,pElementM(ReY),pElementM(ReZ),RowM(ReX),ColM(ReX),ColM(ReY),
3197 MajorForm(ReX),MajorForm(ReY),MajorForm(ReZ));
3198 bProductMM_Update(ptr1,pElementM(ImY),pElementM(ImZ),1.0,-1.0,RowM(ReX),ColM(ReX),ColM(ReY),
3199 MajorForm(ReX),MajorForm(ImY),MajorForm(ImZ));
3200 }
3201
3202 if ((ImX != ReY) && (ImX != ImY) && (ImX != ReZ) && (ImX != ImZ))
3203 {
3204 bProductMM(pElementM(ImX),pElementM(ReY),pElementM(ImZ),RowM(ReX),ColM(ReX),ColM(ReY),
3205 MajorForm(ImX),MajorForm(ReY),MajorForm(ImZ));
3206 bProductMM_Update(pElementM(ImX),pElementM(ImY),pElementM(ReZ),1.0,1.0,RowM(ReX),ColM(ReX),ColM(ReY),
3207 MajorForm(ImX),MajorForm(ImY),MajorForm(ReZ));
3208 }
3209 else
3210 {
3211 ptr2=(PRECISION*)dw_malloc(RowM(ReX)*ColM(ReX)*sizeof(PRECISION));
3212 bProductMM(ptr2,pElementM(ReY),pElementM(ImZ),RowM(ReX),ColM(ReX),ColM(ReY),
3213 MajorForm(ImX),MajorForm(ReY),MajorForm(ImZ));
3214 bProductMM_Update(ptr2,pElementM(ImY),pElementM(ReZ),1.0,1.0,RowM(ReX),ColM(ReX),ColM(ReY),
3215 MajorForm(ImX),MajorForm(ImY),MajorForm(ReZ));
3216 }
3217 if (ptr1)
3218 {
3219 memcpy(pElementM(ReX),ptr1,RowM(ReX)*ColM(ReX)*sizeof(PRECISION));
3220 dw_free(ptr1);
3221 }
3222 if (ptr2)
3223 {
3224 memcpy(pElementM(ImX),ptr2,RowM(ReX)*ColM(ReX)*sizeof(PRECISION));
3225 dw_free(ptr2);
3226 }
3227 return 1;
3228 }
3229 /*******************************************************************************/
3230 /*******************************************************************************/
3231 /*******************************************************************************/
3232
3233 /*******************************************************************************/
3234 /*************************** Miscellaneous Routines ****************************/
3235 /*******************************************************************************/
3236 /*
3237 Assumes
3238 x : m-vector
3239
3240 Results
3241 Computes the norm of x.
3242
3243 Returns
3244 Returns the norm of x upon success and -1.0 upon failure.
3245 */
3246 PRECISION Norm(TVector x)
3247 {
3248 int i;
3249 PRECISION result;
3250 if (!x)
3251 {
3252 dw_Error(NULL_ERR);
3253 return -1.0;
3254 }
3255 dw_ClearError();
3256 for (result=ElementV(x,0)*ElementV(x,0), i=DimV(x)-1; i > 0; i--)
3257 result+=ElementV(x,i)*ElementV(x,i);
3258 return sqrt(result);
3259 }
3260
3261 /*
3262 Assumes
3263 X : m x n matrix
3264
3265 Results
3266 Returns the Euclidean norm of x.
3267
3268 Notes
3269 The Euclidean norm is the square root of the sum of the squares of the
3270 elements of X
3271 */
3272 PRECISION MatrixNormEuclidean(TMatrix X)
3273 {
3274 int i;
3275 PRECISION result, *p;
3276 if (!X)
3277 {
3278 dw_Error(NULL_ERR);
3279 return -1.0;
3280 }
3281 dw_ClearError();
3282 p=pElementM(X);
3283 for (result=p[0]*p[0], i=RowM(X)*ColM(X)-1; i > 0; i--)
3284 result+=p[i]*p[i];
3285 return sqrt(result);
3286 }
3287
3288 /*
3289 Assumes
3290 X : m x n matrix
3291
3292 Results
3293 Returns the Euclidean norm of x.
3294
3295 Notes
3296 The matrix norm of X is the max of the norm of X*v over all vectors of unit
3297 length. It will be equal to the largest of the singular values of X.
3298 */
3299 PRECISION MatrixNorm(TMatrix X)
3300 {
3301 int err;
3302 PRECISION result, *d;
3303 if (!X)
3304 {
3305 dw_Error(NULL_ERR);
3306 return -1.0;
3307 }
3308 if (!(d=(PRECISION*)dw_malloc(((RowM(X) < ColM(X)) ? RowM(X) : ColM(X))*sizeof(PRECISION))))
3309 {
3310 dw_Error(MEM_ERR);
3311 return -1.0;
3312 }
3313 dw_ClearError();
3314 if ((err=bSVD((PRECISION*)NULL,d,(PRECISION*)NULL,pElementM(X),RowM(X),ColM(X),1,1,MajorForm(X),1)))
3315 {
3316 dw_free(d);
3317 dw_Error(err);
3318 return -1.0;
3319 }
3320 result=d[0];
3321 dw_free(d);
3322 return result;
3323 }
3324
3325 /*
3326 Assumes
3327 x : m-vector
3328 y : m-vector
3329
3330 Results
3331 returns dot product of x and y.
3332
3333 Notes
3334 On error returns 0.0. Call GetError() to determine the type of error.
3335 */
3336 PRECISION DotProduct(TVector x, TVector y)
3337 {
3338 PRECISION result;
3339 int i;
3340 if (!x || !y)
3341 {
3342 dw_Error(NULL_ERR);
3343 return 0.0;
3344 }
3345 if (DimV(x) != DimV(y))
3346 {
3347 dw_Error(SIZE_ERR);
3348 return 0.0;
3349 }
3350 dw_ClearError();
3351 for (result=ElementV(x,0)*ElementV(y,0), i=DimV(x)-1; i > 0; i--) result+=ElementV(x,i)*ElementV(y,i);
3352 return result;
3353 }
3354
3355 /*
3356 Assumes
3357 x : m-vector
3358 y : m-vector
3359 S : m x m matrix
3360
3361 Results
3362 returns x'*S*y
3363
3364 Notes
3365 In order for S to be a inner product, S must be positive definite
3366 and symmetric. A zero return could indicate a error condition.
3367 Call GetError() to determine if an error has occured.
3368 */
3369 PRECISION InnerProduct(TVector x, TVector y, TMatrix S)
3370 {
3371 if (!x || !y || !S)
3372 {
3373 dw_Error(NULL_ERR);
3374 return 0.0;
3375 }
3376 if ((DimV(x) != RowM(S)) || (DimV(y) != ColM(S)) || (DimV(x) != DimV(y)))
3377 {
3378 dw_Error(SIZE_ERR);
3379 return 0.0;
3380 }
3381 dw_ClearError();
3382 return bInnerProduct(pElementV(x),pElementV(y),pElementM(S),DimV(x));
3383 }
3384
3385 /*
3386 Assumes
3387 X : m x n matrix
3388 y : m-vector
3389 z : n-vector
3390
3391 Results
3392 Returns X = y * z' upon success and a null pointer on failure.
3393 */
3394 TMatrix OuterProduct(TMatrix X, TVector y, TVector z)
3395 {
3396 int i, j;
3397 if (!y || !z)
3398 {
3399 dw_Error(NULL_ERR);
3400 return (TMatrix)NULL;
3401 }
3402 if (!X)
3403 { if (!(X=CreateMatrix(DimV(y),DimV(z)))) return (TMatrix)NULL; }
3404 else
3405 if ((RowM(X) != DimV(y)) || (ColM(X) != DimV(z)))
3406 {
3407 dw_Error(SIZE_ERR);
3408 return (TMatrix)NULL;
3409 }
3410 for (i=DimV(y)-1; i >= 0; i--)
3411 for (j=DimV(z)-1; j >= 0; j--)
3412 ElementM(X,i,j)=ElementV(y,i)*ElementV(z,j);
3413 return X;
3414 }
3415
3416 /*
3417 Assumes
3418 X : m x m matrix
3419
3420 Results
3421 Returns the determinate of X upon success. Returns
3422 0 upon failure. Call GetError() to determine
3423 failure.
3424
3425 Notes
3426 Uses the LU decomposition to compute the determinate.
3427 */
3428 PRECISION Determinant_LU(TMatrix X)
3429 {
3430 PRECISION rtrn=0.0, *LU;
3431 int i, sgn, *p;
3432 if (!X)
3433 dw_Error(NULL_ERR);
3434 else
3435 if (RowM(X) != ColM(X))
3436 dw_Error(SIZE_ERR);
3437 else
3438 if (!(p=(int*)dw_malloc(RowM(X)*sizeof(int))))
3439 dw_Error(MEM_ERR);
3440 else
3441 {
3442 if (!(LU=(PRECISION*)dw_malloc(RowM(X)*RowM(X)*sizeof(PRECISION))))
3443 dw_Error(MEM_ERR);
3444 else
3445 {
3446 memcpy(LU,pElementM(X),RowM(X)*RowM(X)*sizeof(PRECISION));
3447 if (!bLU(p,LU,RowM(X),RowM(X),1))
3448 {
3449 for (sgn=1, i=RowM(X)-2; i >= 0; i--)
3450 if (p[i] != i) sgn=-sgn;
3451 for (rtrn=0.0, i=RowM(X)*RowM(X)-1; i >= 0; i-=RowM(X)+1)
3452 if (LU[i] < 0.0)
3453 {
3454 rtrn+=log(-LU[i]);
3455 sgn=-sgn;
3456 }
3457 else
3458 if (LU[i] > 0.0)
3459 rtrn+=log(LU[i]);
3460 else
3461 break;
3462 rtrn=(i >= 0) ? 0.0 : sgn*exp(rtrn);
3463 }
3464 dw_ClearError();
3465 dw_free(LU);
3466 }
3467 dw_free(p);
3468 }
3469 return rtrn;
3470 }
3471
3472 /*
3473 Assumes
3474 X : m x m matrix
3475
3476 Results
3477 Returns the determinate of X upon success. Returns
3478 0 upon failure. Call GetError() to determine
3479 failure.
3480
3481 Notes
3482 Uses the LU decomposition to compute the determinate.
3483 */
3484 PRECISION LogAbsDeterminant_LU(TMatrix X)
3485 {
3486 PRECISION rtrn=0.0, *LU;
3487 int i, *p;
3488 if (!X)
3489 dw_Error(NULL_ERR);
3490 else
3491 if (RowM(X) != ColM(X))
3492 dw_Error(SIZE_ERR);
3493 else
3494 if (!(p=(int*)dw_malloc(RowM(X)*sizeof(int))))
3495 dw_Error(MEM_ERR);
3496 else
3497 {
3498 if (!(LU=(PRECISION*)dw_malloc(RowM(X)*RowM(X)*sizeof(PRECISION))))
3499 dw_Error(MEM_ERR);
3500 else
3501 {
3502 memcpy(LU,pElementM(X),RowM(X)*RowM(X)*sizeof(PRECISION));
3503 if (!bLU(p,LU,RowM(X),RowM(X),1))
3504 {
3505 for (i=RowM(X)*RowM(X)-1; i >= 0; i-=RowM(X)+1)
3506 if (LU[i] < 0.0)
3507 rtrn+=log(-LU[i]);
3508 else
3509 if (LU[i] > 0.0)
3510 rtrn+=log(LU[i]);
3511 else
3512 {
3513 rtrn=MINUS_INFINITY;
3514 break;
3515 }
3516 dw_ClearError();
3517 }
3518 dw_free(LU);
3519 }
3520 dw_free(p);
3521 }
3522 return rtrn;
3523 }
3524
3525 /*
3526 Assumes
3527 X : m x m matrix
3528
3529 Results
3530 Returns the trace of X.
3531 */
3532 PRECISION Trace(TMatrix X)
3533 {
3534 PRECISION trace=0.0;
3535 int i;
3536 if (!X)
3537 dw_Error(NULL_ERR);
3538 else
3539 if (RowM(X) != ColM(X))
3540 dw_Error(SIZE_ERR);
3541 else
3542 for (trace=ElementM(X,0,0), i=RowM(X)-1; i > 0; i--) trace+=ElementM(X,i,i);
3543 return trace;
3544 }
3545
3546 /*
3547 Assumes
3548 X : m x n matrix
3549
3550 Results
3551 Returns the rank of X. Return -1 upon error. Use dw_GetError()
3552 to determine error type.
3553
3554 Notes
3555 Uses the singular value decomposition to compute the rank. A singular value
3556 is considered to be zero if it is smaller than the square root of machine
3557 epsilon times the largest singular value.
3558 */
3559 int Rank_SVD(TMatrix X)
3560 {
3561 PRECISION *d, small;
3562 int rank, q, err;
3563 if (!X)
3564 {
3565 dw_Error(NULL_ERR);
3566 return -1;
3567 }
3568 q=(ColM(X) < RowM(X)) ? ColM(X) : RowM(X);
3569 if (!(d=(PRECISION*)dw_malloc(q*sizeof(PRECISION))))
3570 {
3571 dw_Error(MEM_ERR);
3572 return -1;
3573 }
3574 if ((err=bSVD((PRECISION*)NULL,d,(PRECISION*)NULL,pElementM(X),RowM(X),ColM(X),COLUMN_MAJOR,ROW_MAJOR,MajorForm(X),1)))
3575 {
3576 dw_free(d);
3577 dw_Error(err);
3578 return -1;
3579 }
3580 for (small=d[0]*SQRT_MACHINE_EPSILON, rank=q-1; rank >= 0; rank--)
3581 if (d[rank] > small) break;
3582 dw_free(d);
3583 return rank+1;
3584 }
3585
3586 /*
3587 Assumes
3588 x : n-vector or null pointer
3589 Y : n x (n-1) matrix
3590
3591 Results
3592 The vector x is set to the cross product of the columns of Y.
3593
3594 Notes
3595 The cross product of the columns of Y is the vector such that
3596
3597 (1) Y'x = 0
3598 (2) det([Y x]) >= 0
3599 (3) norm(x) = volume of the parallelpiped spanned by the columns of Y
3600
3601 Uses the LU decomposition to compute the cross product.
3602 */
3603
3604 TVector CrossProduct_LU(TVector x, TMatrix Y)
3605 {
3606 int i, j, sgn;
3607 PRECISION s;
3608 TMatrix X;
3609 TPermutation P;
3610 TVector z=(TVector)NULL;
3611 if (!Y)
3612 {
3613 dw_Error(NULL_ERR);
3614 return (TVector)NULL;
3615 }
3616 if (RowM(Y) != ColM(Y)+1)
3617 {
3618 dw_Error(SIZE_ERR);
3619 return (TVector)NULL;
3620 }
3621 if (!x)
3622 {
3623 if (!(z=x=CreateVector(RowM(Y))))
3624 return (TVector)NULL;
3625 }
3626 else
3627 if (RowM(Y) != DimV(x))
3628 {
3629 dw_Error(SIZE_ERR);
3630 return (TVector)NULL;
3631 }
3632 if ((X=CreateMatrix(RowM(Y),ColM(Y))))
3633 {
3634 if ((P=CreatePermutation(RowM(Y))))
3635 {
3636 if (LU(P,X,Y))
3637 {
3638 ElementV(x,RowM(Y)-1)=1.0;
3639 for (i=RowM(Y)-2; i >= 0; i--)
3640 {
3641 ElementV(x,i)=0.0;
3642 for (j=RowM(Y)-1; j > i; j--)
3643 ElementV(x,i)-=ElementV(x,j)*ElementM(X,j,i);
3644 }
3645 ProductTransposeVP(x,x,P);
3646 for (sgn=1, i=RowM(X)-2; i >= 0; i--)
3647 if (ElementP(P,i) != i) sgn=-sgn;
3648 for (s=0.0, i=RowM(X)-2; i >= 0; i--)
3649 if (ElementM(X,i,i) < 0.0)
3650 {
3651 s+=log(-ElementM(X,i,i));
3652 sgn=-sgn;
3653 }
3654 else
3655 if (ElementM(X,i,i) > 0.0)
3656 s+=log(ElementM(X,i,i));
3657 else
3658 break;
3659 s=(i >= 0) ? 0.0 : sgn*exp(s);
3660 ProductSV(x,s,x);
3661 }
3662 else
3663 {
3664 FreePermutation(P);
3665 FreeMatrix(X);
3666 if (z) FreeVector(z);
3667 return (TVector)NULL;
3668 }
3669 FreePermutation(P);
3670 }
3671 FreeMatrix(X);
3672 }
3673 return x;
3674 }
3675
3676 /*
3677 Assumes
3678 Y : m x n matrix
3679
3680 Returns
3681 Upon success, the columns of the returned matrix form an orthonormal basis
3682 for the null space of Y. A null return either indicates the null space is
3683 {0} or an error condition. Call dw_GetError() to determine if a failure
3684 occured.
3685
3686 Notes
3687 Use the singular value decomposition to compute the null space.
3688
3689 If the largest singular value of Y is less than or equal to the square root
3690 of machine epsilon, then the matrix is assumed to be the zero matrix and
3691 the null space is all of n-dimensional Euclidean space. For this reason,
3692 care must be taken with the scale of Y.
3693
3694 A singular value is assumed to be zero if it is smaller than the square root
3695 of the minimum of m and n times machine epsilon.
3696 */
3697 TMatrix NullSpace(TMatrix X)
3698 {
3699 PRECISION *d, *v, small;
3700 int rank, q, err;
3701 TMatrix rtrn=(TMatrix)NULL;
3702 if (!X)
3703 {
3704 dw_Error(NULL_ERR);
3705 return (TMatrix)NULL;
3706 }
3707 q=(ColM(X) < RowM(X)) ? ColM(X) : RowM(X);
3708 d=(PRECISION*)dw_malloc(q*sizeof(PRECISION));
3709 v=(PRECISION*)dw_malloc(ColM(X)*ColM(X)*sizeof(PRECISION));
3710 if (!d || !v)
3711 dw_Error(MEM_ERR);
3712 else
3713 if ((err=bSVD((PRECISION*)NULL,d,v,pElementM(X),RowM(X),ColM(X),COLUMN_MAJOR,COLUMN_MAJOR,MajorForm(X),0)))
3714 dw_Error(err);
3715 else
3716 {
3717 dw_ClearError();
3718 for (small=d[0]*SQRT_MACHINE_EPSILON, rank=q-1; rank >= 0; rank--)
3719 if (d[rank] > small) break;
3720 if (++rank < ColM(X))
3721 {
3722 rtrn=CreateMatrix(ColM(X),ColM(X)-rank);
3723 if (MajorForm(rtrn))
3724 memcpy(pElementM(rtrn),v+(ColM(X)*rank),ColM(X)*(ColM(X)-rank)*sizeof(PRECISION));
3725 else
3726 bTranspose(pElementM(rtrn),v+(ColM(X)*rank),ColM(X),ColM(X)-rank,COLUMN_MAJOR);
3727 }
3728 }
3729 if (v) dw_free(v);
3730 if (d) dw_free(d);
3731 return rtrn;
3732 }
3733
3734 /*
3735 Assumes
3736 X : n x m matrix or null pointer
3737 Y : m x n matrix
3738
3739 Returns
3740 The generalized inverse upon success and null upon failure. If X is null,
3741 then it is created. The generalized inverse of Y is the unique matrix X
3742 such that
3743 (a) X*Y*X = X
3744 (b) Y*X*Y = Y
3745 (c) X*Y and Y*X are symmetric
3746
3747 Notes
3748 Use the singular value decomposition to compute the generalized inverse.
3749
3750 A singular value is assumed to be zero if it is smaller than the square root
3751 of machine epsilon times the largest singular value.
3752 */
3753 TMatrix GeneralizedInverse(TMatrix X, TMatrix Y)
3754 {
3755 PRECISION *d, *u, *v, small, scale;
3756 int err, q, i, j, k;
3757 TMatrix rtrn=(TMatrix)NULL;
3758 if (!Y)
3759 {
3760 dw_Error(NULL_ERR);
3761 return (TMatrix)NULL;
3762 }
3763 if (X && ((RowM(X) != ColM(Y)) || (ColM(X) != RowM(Y))))
3764 {
3765 dw_Error(SIZE_ERR);
3766 return (TMatrix)NULL;
3767 }
3768 q=(RowM(Y) < ColM(Y)) ? RowM(Y) : ColM(Y);
3769 d=(PRECISION*)dw_malloc(q*sizeof(PRECISION));
3770 u=(PRECISION*)dw_malloc(RowM(Y)*q*sizeof(PRECISION));
3771 v=(PRECISION*)dw_malloc(ColM(Y)*q*sizeof(PRECISION));
3772 if (!d || !u || !v)
3773 dw_Error(NULL_ERR);
3774 else
3775 if ((err=bSVD(u,d,v,pElementM(Y),RowM(Y),ColM(Y),COLUMN_MAJOR,ROW_MAJOR,MajorForm(Y),1)))
3776 dw_Error(err);
3777 else
3778 {
3779 small=d[0]*SQRT_MACHINE_EPSILON;
3780 for (k=ColM(Y)*q-1, i=q-1; i >= 0; k--, i--)
3781 for (scale=(d[i] < small) ? 0.0 : 1.0/d[i], j=k; j >= 0; j-=q)
3782 v[j]*=scale;
3783 rtrn=X ? X : CreateMatrix(ColM(Y),RowM(Y));
3784 bProductMM(pElementM(rtrn),v,u,RowM(rtrn),ColM(rtrn),q,MajorForm(rtrn),ROW_MAJOR,ROW_MAJOR);
3785 }
3786 if (v) dw_free(v);
3787 if (u) dw_free(u);
3788 if (d) dw_free(d);
3789 return rtrn;
3790 }
3791
3792 /*
3793 Assumes:
3794 X : m x m matrix
3795
3796 Return:
3797 X upon success and null upon failure.
3798 Results:
3799 X = 0.5*(X + X'), which forces X to symmetric.
3800 */
3801 TMatrix ForceSymmetric(TMatrix X)
3802 {
3803 int j, k, m;
3804 PRECISION *pX;
3805 if (!X || (RowM(X) != ColM(X))) return (TMatrix)NULL;
3806 for (m=RowM(X), pX=pElementM(X), j=m*m-2; j > 0; j+=k-1)
3807 for (k=j-m+1; k >= 0; j--, k-=m)
3808 pX[j]=pX[k]=0.5*(pX[j]+pX[k]);
3809 return X;
3810 }
3811 /*******************************************************************************/
3812 /*******************************************************************************/
3813 /*******************************************************************************/
3814
3815 /*******************************************************************************/
3816 /***************************** Kronecker Routines ******************************/
3817 /*******************************************************************************/
3818 /*
3819 Assumes
3820 x : mn vector or null pointer
3821 Y : m x n matrix
3822
3823 Results
3824 The vector x is filled by stacking the columns of Y.
3825
3826 Returns
3827 The vector x upon success and null upon failure. If x is null, it is
3828 created.
3829 */
3830 TVector Vec(TVector x, TMatrix Y)
3831 {
3832 if (!Y)
3833 {
3834 dw_Error(NULL_ERR);
3835 return (TVector)NULL;
3836 }
3837 if (!x)
3838 {
3839 if (!(x=CreateVector(RowM(Y)*ColM(Y))))
3840 return (TVector)NULL;
3841 }
3842 else
3843 if (RowM(Y)*ColM(Y) != DimV(x))
3844 {
3845 dw_Error(SIZE_ERR);
3846 return (TVector)NULL;
3847 }
3848 if (MajorForm(Y))
3849 memcpy(pElementV(x),pElementM(Y),RowM(Y)*ColM(Y)*sizeof(PRECISION));
3850 else
3851 bTranspose(pElementV(x),pElementM(Y),RowM(Y),ColM(Y),0);
3852 return x;
3853 }
3854
3855 /*
3856 Assumes
3857 X : m x n matrix or null pointer
3858 y : mn vector
3859
3860 Results
3861 The matrix is filled by reshaping the vector v in column order.
3862
3863 Returns
3864 The Matrix X upon success and null upon failure. If X is null, it is
3865 created.
3866 */
3867 TMatrix ReshapeV(TMatrix X, TVector y, int rows, int cols)
3868 {
3869 if (!y)
3870 {
3871 dw_Error(NULL_ERR);
3872 return (TMatrix)NULL;
3873 }
3874 if (DimV(y) != rows*cols)
3875 {
3876 dw_Error(SIZE_ERR);
3877 return (TMatrix)NULL;
3878 }
3879 if (!X)
3880 {
3881 if (!(X=CreateMatrix(rows,cols)))
3882 return (TMatrix)NULL;
3883 }
3884 else
3885 if ((RowM(X) != rows) || (ColM(X) != cols))
3886 {
3887 dw_Error(SIZE_ERR);
3888 return (TMatrix)NULL;
3889 }
3890 if (MajorForm(X))
3891 memcpy(pElementM(X),pElementV(y),rows*cols*sizeof(PRECISION));
3892 else
3893 bTranspose(pElementM(X),pElementV(y),rows,cols,1);
3894 return X;
3895 }
3896
3897 /*
3898 Assumes
3899 X : mr x ns vector or null pointer
3900 Y : m x n matrix
3901 Z : r x s matrix
3902
3903 Results
3904 The matrix X is is filled with the Kronecker product of Y and Z.
3905
3906 Returns
3907 The matrix X upon success and null upon failure. If X is null, it is
3908 created.
3909 */
3910 TMatrix KroneckerProduct(TMatrix X, TMatrix Y, TMatrix Z)
3911 {
3912 if (!Y || !Z)
3913 {
3914 dw_Error(NULL_ERR);
3915 return (TMatrix)NULL;
3916 }
3917 if (!X)
3918 {
3919 if (!(X=CreateMatrix(RowM(Y)*RowM(Z),ColM(Y)*ColM(Z))))
3920 return (TMatrix)NULL;
3921 }
3922 else
3923 if ((RowM(Y)*RowM(Z) != RowM(X)) || (ColM(Y)*ColM(Z) != ColM(X)))
3924 {
3925 dw_Error(SIZE_ERR);
3926 return (TMatrix)NULL;
3927 }
3928 if (X == Y)
3929 bMultiply(pElementM(X),pElementM(X),*pElementM(Z),RowM(X)*ColM(X));
3930 else
3931 if (X == Z)
3932 bMultiply(pElementM(X),pElementM(X),*pElementM(Y),RowM(X)*ColM(X));
3933 else
3934 bMatrixTensor(pElementM(X),pElementM(Y),pElementM(Z),RowM(Y),ColM(Y),RowM(Z),ColM(Z),MajorForm(X),MajorForm(Y),MajorForm(Z));
3935 return X;
3936 }
3937 /*******************************************************************************/
3938 /*******************************************************************************/
3939
3940 /*******************************************************************************/
3941 /****************************** Output Routines ********************************/
3942 /*******************************************************************************/
3943 /*
3944 Assumes
3945 x : m-vector
3946
3947 Returns
3948 One upon success and zero otherwise.
3949
3950 Results
3951 Prints x to f using format.
3952
3953 Notes
3954 If format is the NULL pointer, uses default formating. The elements of x
3955 are output as double independently of the setting of PRECISION. Currently,
3956 the function does not check for failure.
3957 */
3958 int dw_PrintVector(FILE *f, TVector x, const char *format)
3959 {
3960 int i, m;
3961 if (!x) return 0;
3962 m=DimV(x);
3963 if (!format) format="%7.3lf\t";
3964 for (i=0; i < m; i++) fprintf(f,format,(double)(ElementV(x,i)));
3965 fprintf(f,"\n");
3966 return 1;
3967 }
3968
3969 /*
3970 Assumes
3971 X : m x n matrix
3972
3973 Returns
3974 One upon success and zero otherwise.
3975
3976 Results
3977 Prints X to f using format.
3978
3979 Notes
3980 If format is the NULL pointer, uses default formating. The elements of X
3981 are output as double independently of the setting of PRECISION.
3982 */
3983 int dw_PrintMatrix(FILE *f, TMatrix X, const char *format)
3984 {
3985 int i, j, m, n;
3986 if (!X) return 0;
3987 m=RowM(X); n=ColM(X);
3988 if (!format) format="%7.3lf\t";
3989 for (i=0; i < m; i++)
3990 {
3991 for (j=0; j < n; j++) fprintf(f,format,(double)(ElementM(X,i,j)));
3992 fprintf(f,"\n");
3993 }
3994 return 1;
3995 }
3996
3997 /*
3998 Assumes
3999 x : m vector
4000 f : an pointer to an open file
4001
4002 Results
4003 Reads in vector x from f. Return 1 on success, 0 otherwise.
4004 */
4005 int dw_ReadVector(FILE *f, TVector x)
4006 {
4007 int i, m;
4008 PRECISION *px;
4009 if (!x) return 0;
4010 for (px=pElementV(x), i=0, m=DimV(x); i < m; i++)
4011 #if (PRECISION_SIZE == 8)
4012 if (fscanf(f," %lf ",px+i) != 1) return 0;
4013 #else
4014 if (fscanf(f," %f ",px+i) != 1) return 0;
4015 #endif
4016 return 1;
4017 }
4018
4019 /*
4020 Assumes
4021 X : m x n matrix
4022 f : an pointer to an open file
4023
4024 Results
4025 Reads in matrix X from f. Return 1 on success, 0 otherwise.
4026 */
4027 int dw_ReadMatrix(FILE *f, TMatrix X)
4028 {
4029 int i, j, m, n;
4030 if (!X) return 0;
4031 m=RowM(X); n=ColM(X);
4032 for (i=0; i < m; i++)
4033 for (j=0; j < n; j++)
4034 #if (PRECISION_SIZE == 8)
4035 if (fscanf(f," %lf ",&ElementM(X,i,j)) != 1) return 0;
4036 #else
4037 if (fscanf(f," %f ",&Element(X,i,j))) != 1) return 0;
4038 #endif
4039 return 1;
4040 }
4041
4042 /*
4043 Assumes
4044 f : pointer to open binary file
4045 x : m matrix
4046
4047 Results
4048 Outputs x in binary format.
4049
4050 Returns
4051 1 upon success, 0 upon failure
4052
4053 Notes
4054 Format
4055 int
4056 0x1004 - vector float
4057 0x2004 - matrix float
4058 0x1008 - vector double
4059 0x2008 - mattrix double
4060
4061 int rows if matrix, dimension if vector
4062 int columns if matrix, absent if vector
4063 data m binary floating point numbers
4064
4065 */
4066 int OutVectorFloat(FILE *f, TVector x)
4067 {
4068 float y;
4069 int i, format_code=0x1000+sizeof(float);
4070 if (!x) return 0;
4071 if (fwrite(&format_code,sizeof(int),1,f) != 1) return 0;
4072 if (fwrite(&DimV(x),sizeof(int),1,f) != 1) return 0;
4073 #if (PRECISION_SIZE == 8)
4074 for (i=DimV(x)-1; i >= 0; i--)
4075 {
4076 y=(float)ElementV(x,i);
4077 if (fwrite(&y,sizeof(float),1,f) != 1) return 0;
4078 }
4079 #else
4080 if (fwrite(pElementV(x),DimV(x)*sizeof(float),1,f) != 1) return 0;
4081 #endif
4082 return 1;
4083 }
4084
4085 /*
4086 Assumes
4087 f : pointer to open binary file
4088 x : m matrix
4089
4090 Results
4091 Outputs x in binary format. Returns 1 on success and 0 otherwise.
4092
4093 Notes
4094 Format
4095 int
4096 0x1004 - vector float
4097 0x2004 - matrix float
4098 0x1008 - vector double
4099 0x2008 - mattrix double
4100
4101 int rows if matrix, dimension if vector
4102 int columns if matrix, absent if vector
4103 data m binary floating point numbers
4104
4105 */
4106 int OutVectorDouble(FILE *f, TVector x)
4107 {
4108 double y;
4109 int i, format_code=0x1000+sizeof(double);
4110 if (!x) return 0;
4111 if (fwrite(&format_code,sizeof(int),1,f) == 1) return 0;
4112 if (fwrite(&DimV(x),sizeof(int),1,f) != 1) return 0;
4113 #if (PRECISION_SIZE == 8)
4114 for (i=DimV(x)-1; i >= 0; i--)
4115 {
4116 y=(double)ElementV(x,i);
4117 if (fwrite(&y,sizeof(double),1,f) != 1) return 0;
4118 }
4119 #else
4120 if (fwrite(pElementV(x),DimV(x)*sizeof(double),1,f) != 1) return 0;
4121 #endif
4122 return 1;
4123 }
4124
4125
4126 /*
4127 Assumes
4128 f : pointer to open binary file
4129 X : m x n matrix
4130
4131 Results
4132 Outputs X in binary format. Returns 1 on success and 0 otherwise.
4133
4134 Notes
4135 Format
4136 int
4137 0x1004 - vector float
4138 0x2004 - matrix float
4139 0x1008 - vector double
4140 0x2008 - mattrix double
4141
4142 int rows if matrix, dimension if vector
4143 int columns if matrix, absent if vector
4144 data m x n binary floating point numbers
4145 */
4146 int OutMatrixFloat(FILE *f, TMatrix X)
4147 {
4148 float y;
4149 int i, j, format_code=0x2000+sizeof(float);
4150 if (!X) return 0;
4151 if (fwrite(&format_code,sizeof(int),1,f) != 1) return 0;
4152 if (fwrite(&RowM(X),sizeof(int),2,f) != 1) return 0;
4153 if (fwrite(&ColM(X),sizeof(int),1,f) != 1) return 0;
4154 #if (PRECISION_SIZE == 8)
4155 for (j=ColM(X)-1; j >= 0; j--)
4156 for (i=RowM(X)-1; i >= 0; i--)
4157 {
4158 y=(float)ElementM(X,i,j);
4159 if (fwrite(&y,sizeof(float),1,f) != 1) return 0;
4160 }
4161 #else
4162 if (fwrite(pElementM(X),RowM(X)*ColM(X)*sizeof(float),1,f) != 1) return 0;
4163 #endif
4164 return 1;
4165 }
4166
4167 /*
4168 Assumes
4169 f : pointer to open binary file
4170 X : m x n matrix
4171
4172 Results
4173 Outputs X in binary format. Returns 1 on success and 0 otherwise.
4174
4175 Notes
4176 Format
4177 int
4178 0x1004 - vector float
4179 0x2004 - matrix float
4180 0x1008 - vector double
4181 0x2008 - mattrix double
4182
4183 int rows if matrix, dimension if vector
4184 int columns if matrix, absent if vector
4185 data m x n binary floating point numbers
4186 */
4187 int OutMatrixDouble(FILE *f, TMatrix X)
4188 {
4189 double y;
4190 int i, j, format_code=0x2000+sizeof(double);
4191 if (!X) return 0;
4192 if (fwrite(&format_code,sizeof(int),1,f) != 1) return 0;
4193 if (fwrite(&RowM(X),sizeof(int),2,f) != 1) return 0;
4194 if (fwrite(&ColM(X),sizeof(int),1,f) != 1) return 0;
4195 #if (PRECISION_SIZE == 8)
4196 if (fwrite(((int *)X)-2,sizeof(int),2,f) != 2) return 0;
4197 for (j=ColM(X)-1; j >= 0; j--)
4198 for (i=RowM(X)-1; i >= 0; i--)
4199 {
4200 y=ElementM(X,i,j);
4201 if (fwrite(&y,sizeof(double),1,f) != 1) return 0;
4202 }
4203 #else
4204 if (fwrite(pElementM(X),RowM(X)*ColM(X)*sizeof(double),1,f) != 1) return 0;
4205 #endif
4206 return 1;
4207 }
4208
4209
4210 /*
4211 Assumes
4212 f : pointer to open binary file
4213 x : m matrix or null pointer
4214
4215 Results
4216 Reads x from binary format. Returns 1 on success and 0 otherwise.
4217
4218 Notes
4219 Format
4220 int
4221 0x1004 - vector float
4222 0x2004 - matrix float
4223 0x1008 - vector double
4224 0x2008 - mattrix double
4225
4226 int number rows if matrix, dimension if vector
4227 int number columns if matrix, absent if vector
4228 data m binary floating point numbers
4229 */
4230 TVector InVector(FILE *f, TVector x)
4231 {
4232 int i, d[2], precision, del=0, position=ftell(f);
4233 void *y=(void*)NULL;
4234
4235 if (fread(d,sizeof(int),2,f) != 2) goto EXIT_ERROR;
4236
4237 switch(d[0])
4238 {
4239 case 0x1000+sizeof(float): precision=sizeof(float); break;
4240 case 0x1000+sizeof(double): precision=sizeof(double); break;
4241 default: goto EXIT_ERROR;
4242 }
4243
4244 i=d[1];
4245
4246 if (!x)
4247 {
4248 x=CreateVector(i);
4249 del=1;
4250 }
4251 else
4252 if (DimV(x) != i) goto EXIT_ERROR;
4253
4254 if (precision != sizeof(PRECISION))
4255 {
4256 if (!(y=dw_malloc(i*precision))) dw_Error(MEM_ERR);
4257 if (fread(y,i*precision,1,f) != 1) goto EXIT_ERROR;
4258 if (precision == sizeof(float))
4259 while (--i >= 0) ElementV(x,i)=((float*)y)[i];
4260 else
4261 while (--i >= 0) ElementV(x,i)=((double*)y)[i];
4262 dw_free(y);
4263 }
4264 else
4265 if (fread(pElementV(x),i*sizeof(PRECISION),1,f) != 1) goto EXIT_ERROR;
4266
4267 return x;
4268
4269 EXIT_ERROR:
4270 fseek(f,position,SEEK_SET);
4271 if (del) FreeVector(x);
4272 if (y) dw_free(y);
4273 return (TVector)NULL;
4274 }
4275
4276 /*
4277 Assumes
4278 f : pointer to open binary file
4279 X : m x n matrix or null pointer
4280
4281 Results
4282 Reads x from binary format. Returns 1 on success and 0 otherwise.
4283
4284 Notes
4285 Format
4286 int
4287 0x1004 - vector float
4288 0x2004 - matrix float
4289 0x1008 - vector double
4290 0x2008 - mattrix double
4291
4292 int number rows if matrix, dimension if vector
4293 int number columns if matrix, absent if vector
4294 data m x n binary floating point numbers
4295 */
4296 TMatrix InMatrix(FILE *f, TMatrix X)
4297 {
4298 int i, d[3], precision, del=0, position=ftell(f);
4299 void *Y=(void*)NULL;
4300
4301 if (fread(d,sizeof(int),3,f) != 3) goto EXIT_ERROR;
4302
4303 switch(d[0])
4304 {
4305 case 0x2000+sizeof(float): precision=sizeof(float); break;
4306 case 0x2000+sizeof(double): precision=sizeof(double); break;
4307 default: goto EXIT_ERROR;
4308 }
4309
4310 if (!X)
4311 {
4312 X=CreateMatrix(d[1],d[2]);
4313 del=1;
4314 }
4315 else
4316 if ((RowM(X) != d[1]) || (ColM(X) != d[2])) goto EXIT_ERROR;
4317
4318 i=d[1]*d[2];
4319
4320 if (precision != sizeof(PRECISION))
4321 {
4322 if (!(Y=dw_malloc(i*precision))) dw_Error(MEM_ERR);
4323 if (fread(Y,i*precision,1,f) != 1) goto EXIT_ERROR;
4324 if (precision == sizeof(float))
4325 while (--i >= 0) pElementM(X)[i]=((float*)Y)[i];
4326 else
4327 while (--i >= 0) pElementM(X)[i]=((double*)Y)[i];
4328 dw_free(Y);
4329 }
4330 else
4331 if (fread(pElementM(X),i*sizeof(PRECISION),1,f) != 1) goto EXIT_ERROR;
4332
4333 return X;
4334
4335 EXIT_ERROR:
4336 fseek(f,position,SEEK_SET);
4337 if (del) FreeMatrix(X);
4338 if (Y) dw_free(Y);
4339 return (TMatrix)NULL;
4340 }
4341 /*******************************************************************************/
4342 /*******************************************************************************/
4343
4344 /*******************************************************************************/
4345 /*************************** Matrix Decompositions *****************************/
4346 /*******************************************************************************/
4347 /*
4348 Assumes
4349 U : m x qu matrix where qu is either m or min(m,n)
4350 d : min(m,n)-vector
4351 V : n x qv matrix where qv is either n or min(m,n)
4352 A : m x n matrix
4353
4354 Returns
4355 1 upon success, and 0 on failure.
4356
4357 Results
4358 Finds U, V and d such that A = U * diag(d) * V'. The matrices U and V have
4359 orthonormal columns and the elemets of d are non-negative. Here, diag(d)
4360 denotes is the qu x qv diagonal matrix with the elements of d along the
4361 diagonal. The elements of d are non-negative and in descending order.
4362 */
4363 int SVD(TMatrix U, TVector d, TMatrix V, TMatrix A)
4364 {
4365 int err, compact=1;
4366 if (!d || !A)
4367 {
4368 dw_Error(NULL_ERR);
4369 return 0;
4370 }
4371 if ((DimV(d) != ((RowM(A) < ColM(A)) ? RowM(A) : ColM(A))))
4372 {
4373 dw_Error(SIZE_ERR);
4374 return 0;
4375 }
4376 if (U)
4377 {
4378 if (U == V)
4379 {
4380 dw_Error(ARG_ERR);
4381 return 0;
4382 }
4383 if (RowM(U) != RowM(A))
4384 {
4385 dw_Error(SIZE_ERR);
4386 return 0;
4387 }
4388 if (ColM(U) != DimV(d))
4389 {
4390 compact=0;
4391 if (ColM(U) != RowM(U))
4392 {
4393 dw_Error(SIZE_ERR);
4394 return 0;
4395 }
4396 }
4397 }
4398 if (V)
4399 {
4400 if (RowM(V) != ColM(A))
4401 {
4402 dw_Error(SIZE_ERR);
4403 return 0;
4404 }
4405 if (ColM(V) != DimV(d))
4406 {
4407 compact=0;
4408 if (ColM(V) != RowM(V))
4409 {
4410 dw_Error(SIZE_ERR);
4411 return 0;
4412 }
4413 }
4414 }
4415
4416 if ((err=bSVD(U ? pElementM(U) : (PRECISION*)NULL,pElementV(d),V ? pElementM(V) : (PRECISION*)NULL,
4417 pElementM(A),RowM(A),ColM(A),U ? MajorForm(U) : 1,V ? MajorForm(V) : 1,MajorForm(A),compact)))
4418 {
4419 dw_Error(err);
4420 return 0;
4421 }
4422 return 1;
4423 }
4424
4425 /*
4426 Assumes
4427 S : n x n matrix
4428 T : n x n matrix
4429 Q : n x n matrix or null pointer
4430 Z : n x n matrix or null pointer
4431 A : n x n matrix
4432 B : n x n matrix
4433 alpha_r : n vector or null pointer
4434 alpha_i : n vector or null pointer
4435 beta : n vector or null pointer
4436
4437 Returns
4438 1 upon success, and 0 on failure.
4439
4440 Results
4441 Finds orthogonal matrices Q and Z, an block upper triangular matrix S with
4442 1 x 1 or 2 x 2 blocks along the diagonal, and an upper triangular matrix T
4443 such that
4444
4445 A = Q*S*Z' and B = Q*T*Z'
4446
4447 the vectors alpha_r, alpha_i, and beta contain the generalized eigenvalues
4448 of A and B. alpha_r contains the real part of alpha and alpha_i contains
4449 the imginary part.
4450
4451 If Q, Z, alpha_r, alpha_i, or beta is null, then it is not returned.
4452
4453 Notes
4454 The generalized eigenvalue is (alpha_r + i*alpha_i)/beta, but because beta
4455 can be zero, alpha and beta are returned separately.
4456
4457 All the matrices and vectors should be distinct with the exception that A=S
4458 or B=T, but this is not checked.
4459 */
4460 int GeneralizedSchur_Real(TMatrix S, TMatrix T, TMatrix Q, TMatrix Z, TMatrix A, TMatrix B,
4461 TVector alpha_r, TVector alpha_i, TVector beta)
4462 {
4463 int n, err;
4464 if (!S || !T || !A || !B)
4465 {
4466 dw_Error(NULL_ERR);
4467 return 0;
4468 }
4469 n=RowM(A);
4470 if ((ColM(A) != n) || (RowM(B) != n) || (ColM(B) != n) || (RowM(S) != n) || (ColM(S) != n) || (RowM(T) != n) || (ColM(T) != n)
4471 || (Q && ((RowM(Q) != n) || (ColM(Q) != n))) || (Z && ((RowM(Z) != n) || (ColM(Z) != n)))
4472 || (alpha_r && (DimV(alpha_r) != n)) || (alpha_i && (DimV(alpha_i) != n)) || (beta && (DimV(beta) != n)))
4473 {
4474 dw_Error(SIZE_ERR);
4475 return 0;
4476 }
4477
4478 err=bGeneralizedSchur_real(Q ? pElementM(Q) : (PRECISION*)NULL,Z ? pElementM(Z) : (PRECISION*)NULL,pElementM(S),pElementM(T),
4479 pElementM(A),pElementM(B),n,Q ? MajorForm(Q) : 1,Z ? MajorForm(Z) : 1,MajorForm(S),MajorForm(T),
4480 MajorForm(A),MajorForm(B),alpha_r ? pElementV(alpha_r) : (PRECISION*)NULL,
4481 alpha_i ? pElementV(alpha_i) : (PRECISION*)NULL,beta ? pElementV(beta) : (PRECISION*)NULL);
4482
4483 if (err == NO_ERR)
4484 return 1;
4485 else
4486 {
4487 dw_Error(err);
4488 return 0;
4489 }
4490 }
4491
4492 /*
4493 Assumes
4494 SS : n x n matrix
4495 TT : n x n matrix
4496 QQ : n x n matrix or null
4497 ZZ : n x n matrix or null
4498 S : n x n matrix. S is block upper triangular with 1x1 or 2x2 blocks along the diagonal.
4499 T : n x n matrix. T is block upper triangular with positive diagonal.
4500 Q : n x n matrix or null. Q is orthogonal if it is not null.
4501 Z : n x n matrix or null. Z is orghogonal if it is not null.
4502 select : integer array of length n
4503 alpha_i : array of length n or null
4504 alpha_i : array of length n or null
4505 beta : array of length n or null
4506
4507 Returns
4508 NO_ERR : success
4509 MEM_ERR : out of memory
4510 BLAS_LAPACK_ERR : blas or lapack error
4511
4512 Results
4513 Finds orthogonal matrices QQ and ZZ, an block upper triangular matrix SS
4514 with 1 x 1 or 2 x 2 blocks along the diagonal, an upper triangular matrix TT
4515 such that
4516
4517 Q*S*Z' = QQ*SS*ZZ' and Q*T*Z' = QQ*TT*ZZ'
4518
4519 If either Q or QQ are null, then QQ is not computed and if either Z or ZZ is
4520 null, then ZZ is not computed. The generalized eigenvalues of S and T
4521 corresponding to the elements of select that are equal to one are
4522 transformed to the first block of SS and TT.
4523
4524 Notes
4525 Q, Z, S, and T should be the results of a call to GeneralizedSchur_Real(), SortGeneralizedSchur_Real(),
4526 or ReorderGeneralizedSchur_Real().
4527
4528 All the matrices and vectors should be distinct with the exception that
4529 S=SS, T=TT, Q=QQ, or Z=ZZ, but this is not checked.
4530 */
4531 int ReorderGeneralizedSchur_Real(TMatrix SS, TMatrix TT, TMatrix QQ, TMatrix ZZ, TMatrix S, TMatrix T, TMatrix Q,
4532 TMatrix Z, int *select, TVector alpha_r, TVector alpha_i, TVector beta)
4533 {
4534 int n, err;
4535 if (!SS || !TT || !S || !T || !select)
4536 {
4537 dw_Error(NULL_ERR);
4538 return 0;
4539 }
4540 n=RowM(SS);
4541 if ((ColM(SS) != n) || (RowM(TT) != n) || (ColM(TT) != n) || (RowM(S) != n) || (ColM(S) != n) || (RowM(T) != n) || (ColM(T) != n)
4542 || (QQ && ((RowM(QQ) != n) || (ColM(QQ) != n))) || (ZZ && ((RowM(ZZ) != n) || (ColM(ZZ) != n)))
4543 || (Q && ((RowM(Q) != n) || (ColM(Q) != n))) || (Z && ((RowM(Z) != n) || (ColM(Z) != n)))
4544 || (alpha_r && (DimV(alpha_r) != n)) || (alpha_i && (DimV(alpha_i) != n)) || (beta && (DimV(beta) != n)))
4545 {
4546 dw_Error(SIZE_ERR);
4547 return 0;
4548 }
4549 err=bReorderGeneralizedSchur_real(select,QQ ? pElementM(QQ) : (PRECISION*)NULL,
4550 ZZ ? pElementM(ZZ) : (PRECISION*)NULL,pElementM(SS),pElementM(TT),
4551 Q ? pElementM(Q) : (PRECISION*)NULL,Z ? pElementM(Z) : (PRECISION*)NULL,pElementM(S),pElementM(T),n,
4552 QQ ? MajorForm(QQ) : 1,ZZ ? MajorForm(ZZ) : 1,MajorForm(SS),MajorForm(TT),Q ? MajorForm(Q) : 1,
4553 Z ? MajorForm(Z) : 1,MajorForm(S),MajorForm(T),alpha_r ? pElementV(alpha_r) : (PRECISION*)NULL,
4554 alpha_i ? pElementV(alpha_i) : (PRECISION*)NULL,beta ? pElementV(beta) : (PRECISION*)NULL);
4555
4556 if (err == NO_ERR)
4557 return 1;
4558 else
4559 {
4560 dw_Error(err);
4561 return 0;
4562 }
4563 }
4564
4565 /*
4566 Assumes
4567 SS : n x n matrix
4568 TT : n x n matrix
4569 QQ : n x n matrix or null
4570 ZZ : n x n matrix or null
4571 S : n x n matrix. S is block upper triangular with 1x1 or 2x2 blocks
4572 along the diagonal.
4573 T : n x n matrix. T is block upper triangular with positive diagonal.
4574 Q : n x n matrix or null. Q is orthogonal if it is not null.
4575 Z : n x n matrix or null. Z is orghogonal if it is not null.
4576 alpha_r : array of length n
4577 alpha_i : array of length n
4578 beta : array of length n
4579
4580 Returns
4581 1 upon success and zero otherwise.
4582
4583 Results
4584 Finds orthogonal matrices QQ and ZZ, an block upper triangular matrix SS
4585 with 1 x 1 or 2 x 2 blocks along the diagonal, an upper triangular matrix TT
4586 such that
4587
4588 Q*S*Z' = QQ*SS*ZZ' and Q*T*Z' = QQ*TT*ZZ'
4589
4590 If either Q or QQ are null, then QQ is not computed and if either Z or ZZ is
4591 null, then ZZ is not computed. The generalized eigenvalues will be sorted
4592 in absolute value according to the value of descend.
4593
4594 Notes
4595 Q, Z, S, T, alpha_r, alpha_i, and beta should be the results of a call to
4596 GeneralizedSchur_Real() or ReorderGeneralizedSchur_Real(). All the matrices
4597 and vectors should be distinct except that S=SS, T=TT, Q=QQ, or Z=ZZ.
4598 */
4599 int SortGeneralizedSchur_Real(TMatrix SS, TMatrix TT, TMatrix QQ, TMatrix ZZ, TMatrix S, TMatrix T, TMatrix Q,
4600 TMatrix Z, TVector alpha_r, TVector alpha_i, TVector beta, int descend)
4601 {
4602 int n, err;
4603 if (!SS || !TT || !S || !T || !alpha_r || !alpha_i || !beta)
4604 {
4605 dw_Error(NULL_ERR);
4606 return 0;
4607 }
4608 n=RowM(SS);
4609 if ((ColM(SS) != n) || (RowM(TT) != n) || (ColM(TT) != n) || (RowM(S) != n) || (ColM(S) != n) || (RowM(T) != n) || (ColM(T) != n)
4610 || (QQ && ((RowM(QQ) != n) || (ColM(QQ) != n))) || (ZZ && ((RowM(ZZ) != n) || (ColM(ZZ) != n)))
4611 || (Q && ((RowM(Q) != n) || (ColM(Q) != n))) || (Z && ((RowM(Z) != n) || (ColM(Z) != n)))
4612 || (DimV(alpha_r) != n) || (DimV(alpha_i) != n) || (DimV(beta) != n))
4613 {
4614 dw_Error(SIZE_ERR);
4615 return 0;
4616 }
4617
4618 err=bSortGeneralizedSchur_real(QQ ? pElementM(QQ) : (PRECISION*)NULL,ZZ ? pElementM(ZZ) : (PRECISION*)NULL,pElementM(SS),pElementM(TT),
4619 Q ? pElementM(Q) : (PRECISION*)NULL,Z ? pElementM(Z) : (PRECISION*)NULL,pElementM(S),pElementM(T),n,
4620 QQ ? MajorForm(QQ) : 1,ZZ ? MajorForm(ZZ) : 1,MajorForm(SS),MajorForm(TT),Q ? MajorForm(Q) : 1,Z ? MajorForm(Z) : 1,
4621 MajorForm(S),MajorForm(T),pElementV(alpha_r),pElementV(alpha_i),pElementV(beta),descend);
4622
4623 if (err == NO_ERR)
4624 return 1;
4625 else
4626 {
4627 dw_Error(err);
4628 return 0;
4629 }
4630 }
4631
4632
4633 /*
4634 Assumes
4635 U : m x m matrix or null pointer
4636 X : m x m symmetric positive definite matrix
4637
4638 Results
4639 X = U' * U, where U is a upper triangular matrix with positive diagonal.
4640
4641 Returns
4642 The matrix U is returned upon success and a null pointer is return upon
4643 failure. It the matrix U is null, it is created.
4644
4645 Notes
4646 Failure usually indicates X is not positive definite. Only the upper
4647 half of X is used. The matrices U and X need not be distinct.
4648 */
4649 TMatrix CholeskyUT(TMatrix U, TMatrix X)
4650 {
4651 int err;
4652 if (!X)
4653 {
4654 dw_Error(NULL_ERR);
4655 return (TMatrix)NULL;
4656 }
4657 if (RowM(X) != ColM(X))
4658 {
4659 dw_Error(SIZE_ERR);
4660 return (TMatrix)NULL;
4661 }
4662 if (!U)
4663 {
4664 if (!(U=EquateMatrix((TMatrix)NULL,X)))
4665 return (TMatrix)NULL;
4666 if ((err=bCholesky(pElementM(U),RowM(U),1,MajorForm(U))))
4667 {
4668 FreeMatrix(U);
4669 dw_Error(err);
4670 return (TMatrix)NULL;
4671 }
4672 }
4673 else
4674 {
4675 if (U != X)
4676 {
4677 if ((RowM(X) != RowM(U)) || (RowM(X) != ColM(U)))
4678 {
4679 dw_Error(SIZE_ERR);
4680 return (TMatrix)NULL;
4681 }
4682 if (MajorForm(U) == MajorForm(X))
4683 memcpy(pElementM(U),pElementM(X),RowM(X)*RowM(X)*sizeof(PRECISION));
4684 else
4685 bTranspose(pElementM(U),pElementM(X),RowM(X),ColM(X),MajorForm(X));
4686 }
4687 if ((err=bCholesky(pElementM(U),RowM(X),1,MajorForm(U))))
4688 {
4689 dw_Error(err);
4690 return (TMatrix)NULL;
4691 }
4692 }
4693 return U;
4694 }
4695
4696 /*
4697 Assumes
4698 L : m x m matrix
4699 X : m x m symmetric positive definite matrix
4700
4701 Results
4702 X = L' * L, where L is a lower triangular matrix with positive diagonal.
4703
4704 Returns
4705 1 success
4706 0 failure, call GetError() to
4707
4708 Notes
4709 Failure usually indicates X is not positive definite. Only the upper
4710 half of X is used. The matrices L and X need not be distinct.
4711 */
4712 TMatrix CholeskyLT(TMatrix L, TMatrix X)
4713 {
4714 int err;
4715 if (!X)
4716 {
4717 dw_Error(NULL_ERR);
4718 return (TMatrix)NULL;
4719 }
4720 if (RowM(X) != ColM(X))
4721 {
4722 dw_Error(SIZE_ERR);
4723 return (TMatrix)NULL;
4724 }
4725 if (!L)
4726 {
4727 if (!(L=EquateMatrix((TMatrix)NULL,X)))
4728 return (TMatrix)NULL;
4729 if ((err=bCholesky(pElementM(L),RowM(X),0,MajorForm(L))))
4730 {
4731 FreeMatrix(L);
4732 dw_Error(err);
4733 return (TMatrix)NULL;
4734 }
4735 }
4736 else
4737 {
4738 if (L != X)
4739 {
4740 if ((RowM(X) != RowM(L)) || (RowM(X) != ColM(L)))
4741 {
4742 dw_Error(SIZE_ERR);
4743 return (TMatrix)NULL;
4744 }
4745 memcpy(pElementM(L),pElementM(X),RowM(X)*RowM(X)*sizeof(PRECISION));
4746 }
4747 if ((err=bCholesky(pElementM(L),RowM(X),0,MajorForm(L))))
4748 {
4749 dw_Error(err);
4750 return (TMatrix)NULL;
4751 }
4752 }
4753 return L;
4754 }
4755
4756 /*
4757 Assumes
4758 Q : m x q matrix or null pointer
4759 R : q x n matrix
4760 X : m x n matrix
4761
4762 Results
4763 Finds an orthogonal matrix Q and an upper triangular matrix R such that
4764
4765 X = Q * R
4766
4767 Returns
4768 1 - Success
4769 0 - Error, call GetError() to determine the type of error made.
4770
4771 Notes
4772 The integer q must be equal to either m or the minimum of m and n. The
4773 matrices R and X do not have to be distinct.
4774 */
4775 int QR(TMatrix Q, TMatrix R, TMatrix X)
4776 {
4777 int err;
4778 PRECISION *ptr;
4779 if (!R || !X)
4780 {
4781 dw_Error(NULL_ERR);
4782 return 0;
4783 }
4784 if (R != X)
4785 if (ColM(R) != ColM(X))
4786 {
4787 dw_Error(SIZE_ERR);
4788 return 0;
4789 }
4790 else
4791 if (RowM(R) == RowM(X))
4792 {
4793 EquateMatrix(R,X);
4794 ptr=pElementM(R);
4795 }
4796 else
4797 if ((RowM(R) == ColM(X)) && (ColM(X) < RowM(X)))
4798 if (!(ptr=(PRECISION*)dw_malloc(RowM(X)*ColM(X)*sizeof(PRECISION))))
4799 {
4800 dw_Error(MEM_ERR);
4801 return 0;
4802 }
4803 else
4804 memcpy(ptr,pElementM(X),RowM(X)*ColM(X)*sizeof(PRECISION));
4805 else
4806 {
4807 dw_Error(SIZE_ERR);
4808 return 0;
4809 }
4810 else
4811 ptr=pElementM(R);
4812 if (!Q)
4813 err=bQR((PRECISION*)NULL,pElementM(R),ptr,RowM(X),ColM(X),RowM(R),0,MajorForm(R),MajorForm(X));
4814 else
4815 if (Q == R)
4816 {
4817 dw_Error(ARG_ERR);
4818 return 0;
4819 }
4820 else
4821 if ((RowM(Q) != RowM(X)) || (ColM(Q) != RowM(R)))
4822 {
4823 dw_Error(SIZE_ERR);
4824 return 0;
4825 }
4826 else
4827 err=bQR(pElementM(Q),pElementM(R),ptr,RowM(X),ColM(X),RowM(R),MajorForm(Q),MajorForm(R),MajorForm(X));
4828 if (ptr != pElementM(R)) dw_free(ptr);
4829 if (!err) return 1;
4830 dw_Error(err);
4831 return 0;
4832 }
4833
4834 /*
4835 Assumes
4836 P : m x m permutation matrix
4837 X : m x n matrix
4838 A : m x n matrix invertible matrix
4839
4840 Results
4841 Computes the LU decomposition of A with partial pivoting. The
4842 decomposition is
4843
4844 A = P * L * U
4845
4846 where P is a permutation matrix, L is lower triangular with ones along
4847 the diagonal, and U is upper triangular. These matrices are stored as
4848 follows.
4849
4850 L is m x k, where k is the smaller of n and m, and is stored in the
4851 lower half of LU. The diagonal of L is not stored.
4852
4853 U is k x n, where k is the smaller of n and m, and is stored in the
4854 upper half of X, including the diagonal.
4855
4856 P is the integer representation of a permutation matrix. See the
4857 header file matrix.h for a description of its internal
4858 reqresentation.
4859
4860
4861 Returns
4862 1 : success
4863 0 : failure, call GetError() to determine the cause.
4864
4865 Notes
4866 The matrices X and A do not have to be distinct. Uses partial pivoting.
4867 */
4868 int LU(TPermutation P, TMatrix X, TMatrix A)
4869 {
4870 if (!P || !X || !A)
4871 {
4872 dw_Error(NULL_ERR);
4873 return 0;
4874 }
4875 if (DimP(P) != RowM(A))
4876 {
4877 dw_Error(SIZE_ERR);
4878 return 0;
4879 }
4880 if ((X != A) && !EquateMatrix(X,A)) return 0;
4881 bLU(pElementP(P),pElementM(X),RowM(X),ColM(X),MajorForm(X));
4882 UseP(P)=(RowM(X) < ColM(X)) ? RowM(X) : ColM(X);
4883 return 1;
4884 }
4885
4886 /*
4887 Assumes
4888 x : n vector or null pointer
4889 y : n vector
4890 LU : n x n matrix with non-zero diagonal
4891 P : Permutation matrix of dimension n
4892
4893 Results
4894 Solves P * L * U * x = y, where P is a permutation matrix, L is lower
4895 triangular with ones along the diagonal, and U is upper triangular.
4896
4897 Notes
4898 The vectors x and y do not have to be distinct. LU and P should be generated
4899 by the function LU().
4900
4901 The matrices L and U are stored as follows:
4902 U is stored in the upper half of LU, including the diagonal.
4903 L is stored in the lower half of LU. The diagonal of L is not stored.
4904 */
4905 TVector LU_SolveCol(TVector x, TVector y, TMatrix LU, TPermutation P)
4906 {
4907 TVector rtrn;
4908 int err;
4909 if (!y || !LU || !P)
4910 {
4911 dw_Error(NULL_ERR);
4912 return (TVector)NULL;
4913 }
4914 if ((DimV(y) != RowM(LU)) || (DimV(y) != ColM(LU)) || (DimV(y) != DimP(P)))
4915 {
4916 dw_Error(SIZE_ERR);
4917 return (TVector)NULL;
4918 }
4919 if ((rtrn=((x == y) ? x : EquateVector(x,y))))
4920 {
4921 bPermutationMultiply(pElementP(P),pElementV(rtrn),DimV(rtrn),1,UseP(P),1,1);
4922 bSolveUnitTriangular(pElementM(LU),pElementV(rtrn),DimV(rtrn),1,0,MajorForm(LU),1);
4923 if ((err=bSolveTriangular(pElementM(LU),pElementV(rtrn),DimV(rtrn),1,1,MajorForm(LU),1)))
4924 {
4925 if (rtrn != x) FreeVector(rtrn);
4926 rtrn=(TVector)NULL;
4927 dw_Error(err);
4928 }
4929 }
4930 return rtrn;
4931 }
4932
4933 /*
4934 Assumes
4935 x : n vector
4936 y : n vector
4937 LU : n x n matrix with non-zero diagonal
4938 P : integer array of length n
4939
4940 Results
4941 Solves x * P * L * U = y, where P is a permutation matrix, L is lower
4942 triangular with ones along the diagonal, and U is upper triangular.
4943
4944 Notes
4945 The vectors x and y do not have to be distinct.
4946
4947 The matrices P, L, and U are stored as follows.
4948 U is stored in the upper half of LU, including the diagonal.
4949 L is stored in the lower half of LU. The diagonal of L is not stored.
4950 */
4951 TVector LU_SolveRow(TVector x, TVector y, TMatrix LU, TPermutation P)
4952 {
4953 TVector rtrn;
4954 int err;
4955 if (!y || !LU || !P)
4956 {
4957 dw_Error(NULL_ERR);
4958 return (TVector)NULL;
4959 }
4960 if ((DimV(y) != RowM(LU)) || (DimV(y) != ColM(LU)) || (DimV(y) != DimP(P)))
4961 {
4962 dw_Error(SIZE_ERR);
4963 return (TVector)NULL;
4964 }
4965 if ((rtrn=((x == y) ? x : EquateVector(x,y))))
4966 {
4967 if ((err=bSolveTriangular(pElementM(LU),pElementV(rtrn),DimV(rtrn),1,0,1^MajorForm(LU),0)))
4968 {
4969 if (rtrn != x) FreeVector(rtrn);
4970 rtrn=(TVector)NULL;
4971 dw_Error(err);
4972 }
4973 else
4974 {
4975 bSolveUnitTriangular(pElementM(LU),pElementV(rtrn),DimV(rtrn),1,1,1^MajorForm(LU),0);
4976 bPermutationMultiply(pElementP(P),pElementV(rtrn),DimV(rtrn),1,UseP(P),0,0);
4977 }
4978 }
4979 return rtrn;
4980 }
4981
4982 /*
4983 Assumes
4984 Re : n vector
4985 Im : n vector
4986 X : n x n matrix
4987
4988 Results
4989 Re[i] + i*Im[i] is a eigenvalue of X.
4990
4991 Returns
4992 1 upon success and 0 otherwise.
4993 */
4994 int Eigenvalues(TVector Re, TVector Im, TMatrix X)
4995 {
4996 int n;
4997 if (!Re || !Im || !X)
4998 {
4999 dw_Error(NULL_ERR);
5000 return 0;
5001 }
5002 if (Re == Im)
5003 {
5004 dw_Error(ARG_ERR);
5005 return 0;
5006 }
5007 n=RowM(X);
5008 if ((ColM(X) != n) || (DimV(Re) != n) || (DimV(Im) != n))
5009 {
5010 dw_Error(SIZE_ERR);
5011 return 0;
5012 }
5013 return bEigenvalues(pElementV(Re),pElementV(Im),pElementM(X),n,MajorForm(X)) ? 0 : 1;
5014 }
5015
5016 /*
5017 Assumes
5018 ReVal : n vector
5019 ImVal : n vector
5020 ReVec : n x n matrix
5021 ImVec : n x n matrix
5022 X : n x n matrix
5023
5024 Results
5025 Computes the eigenvalues and eigenvectors of X. Note that
5026
5027 (ReVec + i*ImVec)*(DiagonalMatrix(ReVal) + i*DiagonalMatrix(ImVal)) = X*(ReVec + i*ImVec)
5028
5029 Returns
5030 1 upon success and 0 otherwise.
5031 */
5032 int Eigen(TVector ReVal, TVector ImVal, TMatrix ReVec, TMatrix ImVec, TMatrix X)
5033 {
5034 int n, rtrn;
5035 PRECISION *x;
5036 if (!ReVal || !ImVal || !ReVec || !ImVec || !X)
5037 {
5038 dw_Error(NULL_ERR);
5039 return 0;
5040 }
5041 if ((ReVal == ImVal) || (ReVec == ImVec))
5042 {
5043 dw_Error(ARG_ERR);
5044 return 0;
5045 }
5046 n=RowM(X);
5047 if ((ColM(X) != n) || (DimV(ReVal) != n) || (DimV(ImVal) != n) || (RowM(ReVec) != n)
5048 || (ColM(ReVec) != n) || (RowM(ImVec) != n) || (ColM(ImVec) != n))
5049 {
5050 dw_Error(SIZE_ERR);
5051 return 0;
5052 }
5053 if ((X == ReVec) || (X == ImVec))
5054 {
5055 if (!(x=(PRECISION*)dw_malloc(n*n*sizeof(PRECISION))))
5056 {
5057 dw_Error(MEM_ERR);
5058 return 0;
5059 }
5060 memcpy(x,pElementM(X),n*n*sizeof(PRECISION));
5061 }
5062 else
5063 x=pElementM(X);
5064
5065 rtrn=bEigen(pElementV(ReVal),pElementV(ImVal),pElementM(ReVec),pElementM(ImVec),x,n,MajorForm(X),MajorForm(ReVec),MajorForm(ImVec));
5066
5067 if (x != pElementM(X)) dw_free(x);
5068
5069 return rtrn ? 0 : 1;
5070 }
5071
5072 /*
5073 Assumes:
5074 X : matrix of the same size as Y or null pointer
5075 Y : symmetric and positive semi-definite matrix
5076
5077 Results:
5078 Computes symmetric matrix X such that X*X = Y.
5079
5080 Returns:
5081 X upon success and null upon failure. If X is null, it is allocated.
5082
5083 Notes:
5084 Uses the singular value decomposition to compute the square root. Does not
5085 check if Y is symmetric or positive semi-definite.
5086 */
5087 TMatrix MatrixSquareRoot(TMatrix X, TMatrix Y)
5088 {
5089 int i, j, n;
5090 TMatrix U, V;
5091 TVector d;
5092 PRECISION x;
5093 if (!Y)
5094 {
5095 dw_Error(NULL_ERR);
5096 return (TMatrix)NULL;
5097 }
5098 n=RowM(Y);
5099 if (n != ColM(Y))
5100 {
5101 dw_Error(SIZE_ERR);
5102 return (TMatrix)NULL;
5103 }
5104 if (X && ((RowM(X) != n)|| (ColM(X) != n)))
5105 {
5106 dw_Error(SIZE_ERR);
5107 return (TMatrix)NULL;
5108 }
5109 if (!SVD(U=CreateMatrix(n,n),d=CreateVector(n),V=CreateMatrix(n,n),Y))
5110 X=(TMatrix)NULL;
5111 else
5112 {
5113 // U=U*sqrt(d)
5114 for (j=n-1; j >= 0; j--)
5115 {
5116 x=sqrt(ElementV(d,j));
5117 for (i=n-1; i >= 0; i--)
5118 ElementM(U,i,j)*=x;
5119 }
5120 // X = U*sqrt(d)*V'
5121 X=ProductTransposeMM(X,U,V);
5122 }
5123 FreeMatrix(V);
5124 FreeVector(d);
5125 FreeMatrix(U);
5126 return X;
5127 }
5128
5129 /*******************************************************************************/
5130 /**************************** Permutation Matrices *****************************/
5131 /*******************************************************************************/
5132 /*
5133 Assumes
5134 m : positive integer
5135
5136 Results
5137 Creates TPermutation stucture of dimension m.
5138
5139 Returns
5140 A pointer to the TPermutation structure upon success and null upon failure.
5141 */
5142 TPermutation CreatePermutation(int m)
5143 {
5144 TPermutation X;
5145 if (m <= 0)
5146 {
5147 dw_Error(SIZE_ERR);
5148 return (TPermutation)NULL;
5149 }
5150 if ((X=(TPermutation)dw_malloc((m+2)*sizeof(int))))
5151 {
5152 X[0]=m;
5153 X[1]=0;
5154 }
5155 else
5156 dw_Error(MEM_ERR);
5157 return (TPermutation)(X+2);
5158 }
5159
5160 /*
5161 If X is not null, frees the TPermutation structure.
5162 */
5163 void FreePermutation(TPermutation X)
5164 {
5165 if (X) dw_free(X-2);
5166 }
5167
5168 /*
5169 Assumes
5170 X : m-permutation or null pointer.
5171 i : 0 <= i < m
5172 j : 0 <= j < m
5173 m : positive integer
5174
5175 Results
5176 X is initialized to be the permutation which is the transposition (i,j)
5177 */
5178 TPermutation TranspositionPermutation(TPermutation X, int i, int j, int m)
5179 {
5180 if ((m <= 0) || (i < 0) || (i >= m) || (j < 0) || (j >= m))
5181 {
5182 dw_Error(SIZE_ERR);
5183 return (TPermutation)NULL;
5184 }
5185 if (!X)
5186 {
5187 if (!(X=CreatePermutation(m)))
5188 return (TPermutation)NULL;
5189 }
5190 else
5191 if (DimP(X) != m)
5192 {
5193 dw_Error(SIZE_ERR);
5194 return (TPermutation)NULL;
5195 }
5196 if (j > i)
5197 {
5198 UseP(X)=i+1;
5199 ElementP(X,i)=j;
5200 for (i--; i >= 0; i--) ElementP(X,i)=i;
5201 }
5202 else
5203 if (i > j)
5204 {
5205 UseP(X)=j+1;
5206 ElementP(X,j)=i;
5207 for (j--; j >= 0; j--) ElementP(X,j)=j;
5208 }
5209 else
5210 UseP(X)=0;
5211 return X;
5212 }
5213
5214 /*
5215 Assumes
5216 X : m-permutation or null pointer.
5217 p : integer array of length m.
5218 m : postive integer
5219
5220 Results
5221 X is initialized to be the permutation which is the mapping i -> p[i]. If p
5222 does not represent a permutation, an error will not occur, but the results
5223 will not be correct.
5224 */
5225 TPermutation InitializePermutation(TPermutation X, int *p, int m)
5226 {
5227 int i, j;
5228 if (!p)
5229 {
5230 dw_Error(NULL_ERR);
5231 return (TPermutation)NULL;
5232 }
5233 if (!X)
5234 {
5235 if (!(X=CreatePermutation(m)))
5236 return (TPermutation)NULL;
5237 }
5238 else
5239 if (DimP(X) != m)
5240 {
5241 dw_Error(SIZE_ERR);
5242 return (TPermutation)NULL;
5243 }
5244 ElementP(X,0)=p[0];
5245 for (i=1; i < m-1; i++)
5246 for (ElementP(X,i)=p[i], j=0; j < i; j++)
5247 if (ElementP(X,i) < j)
5248 break;
5249 else
5250 if (ElementP(X,i) == j)
5251 ElementP(X,i)=ElementP(X,j);
5252 else
5253 if (ElementP(X,i) == ElementP(X,j))
5254 {
5255 ElementP(X,i)=j;
5256 break;
5257 }
5258 for (i=m-2; i >= 0; i--)
5259 if (ElementP(X,i) != i) break;
5260 UseP(X)=i+1;
5261 return X;
5262 }
5263
5264 /*
5265 Assumes:
5266 p : integer array of length m or null pointer
5267 X : m-permutation
5268
5269 Results:
5270 Allocates the array p if it was null. Fills p with the permutation which
5271 maps i to p[i].
5272 */
5273 int* ExtractPermutation(int *p, TPermutation X)
5274 {
5275 int i, j;
5276 if (!X)
5277 {
5278 dw_Error(NULL_ERR);
5279 return (int*)NULL;
5280 }
5281 if (!p && !(p=dw_malloc(DimP(X)*sizeof(int))))
5282 {
5283 dw_Error(MEM_ERR);
5284 return (int*)NULL;
5285 }
5286 for (i=DimP(X)-1; i >= 0; i--) p[i]=i;
5287 for (i=UseP(X)-1; i >= 0; i--)
5288 if (X[i] != i)
5289 {
5290 j=p[i];
5291 p[i]=p[X[i]];
5292 p[X[i]]=j;
5293 }
5294 return p;
5295 }
5296
5297 /*
5298 Assumes
5299 X : m-permutation or null pointer
5300 Y : m-permutation
5301
5302 Results
5303 X = Y. If X is null pointer, X is created.
5304
5305 Returns
5306 Returns X upon success and null on failure. Call GetError() to
5307 determine the cause of failure.
5308 */
5309 TPermutation EquatePermutation(TPermutation X, TPermutation Y)
5310 {
5311 if (!Y)
5312 {
5313 dw_Error(NULL_ERR);
5314 return (TPermutation)NULL;
5315 }
5316 if (X != Y)
5317 {
5318 if (!X)
5319 {
5320 if (!(X=CreatePermutation(DimP(Y))))
5321 return (TPermutation)NULL;
5322 }
5323 else
5324 if (DimP(X) != DimP(Y))
5325 {
5326 dw_Error(SIZE_ERR);
5327 return (TPermutation)NULL;
5328 }
5329 UseP(X)=UseP(Y);
5330 memcpy(pElementP(X),pElementP(Y),UseP(Y)*sizeof(int));
5331 }
5332 return X;
5333 }
5334
5335 /*
5336 Assumes
5337 X : m x m matrix pointer
5338 Y : m-permutation
5339
5340 Results
5341 The matrix X is the matrix representation of the permutation Y. If X is
5342 the null pointer, X is created.
5343
5344 Returns
5345 Returns X upon success and null on failure. Call GetError() to
5346 determine the cause of failure.
5347 */
5348 TMatrix PermutationMatrix(TMatrix X, TPermutation Y)
5349 {
5350 if (!Y)
5351 {
5352 dw_Error(NULL_ERR);
5353 return (TMatrix)NULL;
5354 }
5355 if (!X)
5356 {
5357 if (!(X=CreateMatrix(DimP(Y),DimP(Y))))
5358 {
5359 dw_Error(MEM_ERR);
5360 return (TMatrix)NULL;
5361 }
5362 }
5363 else
5364 if ((RowM(X) != DimP(Y)) || (ColM(X) != DimP(Y)))
5365 {
5366 dw_Error(SIZE_ERR);
5367 return (TMatrix)NULL;
5368 }
5369 bPermutation(pElementM(X),pElementP(Y),DimP(Y),UseP(Y),MajorForm(X));
5370 return X;
5371 }
5372
5373 TMatrix ProductPM(TMatrix X, TPermutation Y, TMatrix Z)
5374 {
5375 if (!Y || !Z)
5376 {
5377 dw_Error(NULL_ERR);
5378 return (TMatrix)NULL;
5379 }
5380 if (DimP(Y) != RowM(Z))
5381 {
5382 dw_Error(SIZE_ERR);
5383 return (TMatrix)NULL;
5384 }
5385 if ((X != Z) && !(X=EquateMatrix(X,Z))) return (TMatrix)NULL;
5386 bPermutationMultiply(pElementP(Y),pElementM(X),RowM(X),ColM(X),UseP(Y),0,MajorForm(X));
5387 return X;
5388 }
5389
5390 TMatrix ProductMP(TMatrix X, TMatrix Y, TPermutation Z)
5391 {
5392 if (!Y || !Z)
5393 {
5394 dw_Error(NULL_ERR);
5395 return (TMatrix)NULL;
5396 }
5397 if (ColM(Y) != DimP(Z))
5398 {
5399 dw_Error(SIZE_ERR);
5400 return (TMatrix)NULL;
5401 }
5402 if ((X != Y) && !(X=EquateMatrix(X,Y))) return (TMatrix)NULL;
5403 bPermutationMultiply(pElementP(Z),pElementM(X),ColM(X),RowM(X),UseP(Z),1,1^MajorForm(X));
5404 return X;
5405 }
5406
5407 TVector ProductPV(TVector x, TPermutation Y, TVector z)
5408 {
5409 if (!Y || !z)
5410 {
5411 dw_Error(NULL_ERR);
5412 return (TVector)NULL;
5413 }
5414 if (DimP(Y) != DimV(z))
5415 {
5416 dw_Error(SIZE_ERR);
5417 return (TVector)NULL;
5418 }
5419 if ((x != z) && !(x=EquateVector(x,z))) return (TVector)NULL;
5420 bPermutationMultiply(pElementP(Y),pElementV(x),DimV(x),1,UseP(Y),0,1);
5421 return x;
5422 }
5423
5424 TVector ProductVP(TVector x, TVector y, TPermutation Z)
5425 {
5426 if (!y || !Z)
5427 {
5428 dw_Error(NULL_ERR);
5429 return (TVector)NULL;
5430 }
5431 if (DimV(y) != DimP(Z))
5432 {
5433 dw_Error(SIZE_ERR);
5434 return (TVector)NULL;
5435 }
5436 if ((x != y) && !(x=EquateVector(x,y))) return (TVector)NULL;
5437 bPermutationMultiply(pElementP(Z),pElementV(x),DimV(x),1,UseP(Z),1,1);
5438 return x;
5439 }
5440
5441 TMatrix TransposeProductPM(TMatrix X, TPermutation Y, TMatrix Z)
5442 {
5443 if (!Y || !Z)
5444 {
5445 dw_Error(NULL_ERR);
5446 return (TMatrix)NULL;
5447 }
5448 if (DimP(Y) != RowM(Z))
5449 {
5450 dw_Error(SIZE_ERR);
5451 return (TMatrix)NULL;
5452 }
5453 if ((X != Z) && !(X=EquateMatrix(X,Z))) return (TMatrix)NULL;
5454 bPermutationMultiply(pElementP(Y),pElementM(X),RowM(X),ColM(X),UseP(Y),1,MajorForm(X));
5455 return X;
5456 }
5457
5458 TMatrix ProductTransposeMP(TMatrix X, TMatrix Y, TPermutation Z)
5459 {
5460 if (!Y || !Z)
5461 {
5462 dw_Error(NULL_ERR);
5463 return (TMatrix)NULL;
5464 }
5465 if (ColM(Y) != DimP(Z))
5466 {
5467 dw_Error(SIZE_ERR);
5468 return (TMatrix)NULL;
5469 }
5470 if ((X != Y) && !(X=EquateMatrix(X,Y))) return (TMatrix)NULL;
5471 bPermutationMultiply(pElementP(Z),pElementM(X),ColM(X),RowM(X),UseP(Z),0,1^MajorForm(X));
5472 return X;
5473 }
5474
5475
5476 void PrintPermutation(FILE *f, TPermutation X)
5477 {
5478 int i, j, k;
5479 for (i=0; i < DimP(X); i++)
5480 fprintf(f,"%3d ",i);
5481 fprintf(f,"\n");
5482 for (i=0; i < DimP(X); i++)
5483 {
5484 if (i < UseP(X))
5485 {
5486 k=ElementP(X,i);
5487 j=i-1;
5488 }
5489 else
5490 {
5491 k=i;
5492 j=UseP(X)-1;
5493 }
5494 for ( ; j >= 0; j--)
5495 if (k == ElementP(X,j)) k=j;
5496 fprintf(f,"%3d ",k);
5497 }
5498 fprintf(f,"\n");
5499 }
5500 /******************************************************************************/
5501 /******************************************************************************/
5502 /******************************************************************************/
5503