1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | #ifdef HAVE_CONFIG_H1 |
29 | #include <config.h> |
30 | #endif |
31 | #include <cmath> |
32 | #include "GTools.hpp" |
33 | #include "GException.hpp" |
34 | #include "GVector.hpp" |
35 | #include "GMatrix.hpp" |
36 | #include "GMatrixSparse.hpp" |
37 | #include "GMatrixSymmetric.hpp" |
38 | |
39 | |
40 | #define G_CONSTRUCTOR"GMatrixSymmetric::GMatrixSymmetric(int&, int&)" "GMatrixSymmetric::GMatrixSymmetric(int&, int&)" |
41 | #define G_MATRIX"GMatrixSymmetric::GMatrixSymmetric(GMatrix&)" "GMatrixSymmetric::GMatrixSymmetric(GMatrix&)" |
42 | #define G_SPARSEMATRIX"GMatrixSymmetric::GMatrixSymmetric(GSparseMatrix&)" "GMatrixSymmetric::GMatrixSymmetric(GSparseMatrix&)" |
43 | #define G_OP_ADD"GMatrixSymmetric::operator+=(GMatrixSymmetric&)" "GMatrixSymmetric::operator+=(GMatrixSymmetric&)" |
44 | #define G_OP_SUB"GMatrixSymmetric::operator-=(GMatrixSymmetric&)" "GMatrixSymmetric::operator-=(GMatrixSymmetric&)" |
45 | #define G_OP_MUL_VEC"GMatrixSymmetric::operator*(GVector&)" "GMatrixSymmetric::operator*(GVector&)" |
46 | #define G_OP_MAT_MUL"GMatrixSymmetric::operator*=(GMatrixSymmetric&)" "GMatrixSymmetric::operator*=(GMatrixSymmetric&)" |
47 | #define G_AT"GMatrixSymmetric::at(int&, int&)" "GMatrixSymmetric::at(int&, int&)" |
48 | #define G_EXTRACT_ROW"GMatrixSymmetric::row(int&)" "GMatrixSymmetric::row(int&)" |
49 | #define G_SET_ROW"GMatrixSymmetric::row(int&, GVector&)" "GMatrixSymmetric::row(int&, GVector&)" |
50 | #define G_EXTRACT_COLUMN"GMatrixSymmetric::column(int&)" "GMatrixSymmetric::column(int&)" |
51 | #define G_SET_COLUMN"GMatrixSymmetric::column(int&, GVector&)" "GMatrixSymmetric::column(int&, GVector&)" |
52 | #define G_ADD_TO_ROW"GMatrixSymmetric::add_to_row(int&, GVector&)" "GMatrixSymmetric::add_to_row(int&, GVector&)" |
53 | #define G_ADD_TO_COLUMN"GMatrixSymmetric::add_to_column(int&, GVector&)" "GMatrixSymmetric::add_to_column(int&, GVector&)" |
54 | #define G_CHOL_DECOMP"GMatrixSymmetric::cholesky_decompose(int&)" "GMatrixSymmetric::cholesky_decompose(int&)" |
55 | #define G_CHOL_SOLVE"GMatrixSymmetric::cholesky_solver(GVector&, int&)" "GMatrixSymmetric::cholesky_solver(GVector&, int&)" |
56 | #define G_CHOL_INVERT"GMatrixSymmetric::cholesky_invert(int&)" "GMatrixSymmetric::cholesky_invert(int&)" |
57 | #define G_COPY_MEMBERS"GMatrixSymmetric::copy_members(GMatrixSymmetric&)" "GMatrixSymmetric::copy_members(GMatrixSymmetric&)" |
58 | #define G_ALLOC_MEMBERS"GMatrixSymmetric::alloc_members(int&, int&)" "GMatrixSymmetric::alloc_members(int&, int&)" |
59 | |
60 | |
61 | |
62 | |
63 | |
64 | |
65 | |
66 | |
67 | |
68 | |
69 | |
70 | |
71 | |
72 | |
73 | |
74 | |
75 | |
76 | GMatrixSymmetric::GMatrixSymmetric(void) : GMatrixBase() |
77 | { |
78 | |
79 | init_members(); |
80 | |
81 | |
82 | return; |
83 | } |
84 | |
85 | |
86 | |
87 | |
88 | |
89 | |
90 | |
91 | |
92 | |
93 | |
94 | GMatrixSymmetric::GMatrixSymmetric(const int& rows, const int& columns) : |
95 | GMatrixBase() |
96 | { |
97 | |
98 | if (rows > 0 && columns > 0) { |
99 | |
100 | |
101 | init_members(); |
102 | |
103 | |
104 | alloc_members(rows, columns); |
105 | |
106 | } |
107 | else { |
108 | throw GException::empty(G_CONSTRUCTOR"GMatrixSymmetric::GMatrixSymmetric(int&, int&)"); |
109 | } |
110 | |
111 | |
112 | return; |
113 | } |
114 | |
115 | |
116 | |
117 | |
118 | |
119 | |
120 | |
121 | |
122 | |
123 | |
124 | |
125 | |
126 | |
127 | GMatrixSymmetric::GMatrixSymmetric(const GMatrix& matrix) |
128 | { |
129 | |
130 | init_members(); |
131 | |
132 | |
133 | alloc_members(matrix.rows(), matrix.columns()); |
134 | |
135 | |
136 | for (int col = 0; col < m_cols; ++col) { |
137 | for (int row = col; row < m_rows; ++row) { |
138 | double value_ll = matrix(row,col); |
139 | double value_ur = matrix(col,row); |
140 | if (value_ll != value_ur) { |
141 | throw GException::matrix_not_symmetric(G_MATRIX"GMatrixSymmetric::GMatrixSymmetric(GMatrix&)", |
142 | matrix.rows(), |
143 | matrix.columns()); |
144 | } |
145 | (*this)(row, col) = matrix(row, col); |
146 | } |
147 | } |
148 | |
149 | |
150 | return; |
151 | } |
152 | |
153 | |
154 | |
155 | |
156 | |
157 | |
158 | |
159 | |
160 | |
161 | |
162 | |
163 | |
164 | |
165 | GMatrixSymmetric::GMatrixSymmetric(const GMatrixSparse& matrix) |
166 | { |
167 | |
168 | init_members(); |
169 | |
170 | |
171 | alloc_members(matrix.rows(), matrix.columns()); |
172 | |
173 | |
174 | for (int col = 0; col < m_cols; ++col) { |
175 | for (int row = col; row < m_rows; ++row) { |
176 | double value_ll = matrix(row,col); |
177 | double value_ur = matrix(col,row); |
178 | if (value_ll != value_ur) { |
179 | throw GException::matrix_not_symmetric(G_SPARSEMATRIX"GMatrixSymmetric::GMatrixSymmetric(GSparseMatrix&)", |
180 | matrix.rows(), |
181 | matrix.columns()); |
182 | } |
183 | (*this)(row, col) = matrix(row, col); |
184 | } |
185 | } |
186 | |
187 | |
188 | return; |
189 | } |
190 | |
191 | |
192 | |
193 | |
194 | |
195 | |
196 | |
197 | GMatrixSymmetric::GMatrixSymmetric(const GMatrixSymmetric& matrix) : |
198 | GMatrixBase(matrix) |
199 | { |
200 | |
201 | init_members(); |
| 3 | | Calling 'GMatrixSymmetric::init_members' | |
|
| 5 | | Returning from 'GMatrixSymmetric::init_members' | |
|
202 | |
203 | |
204 | copy_members(matrix); |
205 | |
206 | |
207 | return; |
208 | } |
209 | |
210 | |
211 | |
212 | |
213 | |
214 | GMatrixSymmetric::~GMatrixSymmetric(void) |
215 | { |
216 | |
217 | free_members(); |
218 | |
219 | |
220 | return; |
221 | } |
222 | |
223 | |
224 | |
225 | |
226 | |
227 | |
228 | |
229 | |
230 | |
231 | |
232 | |
233 | |
234 | |
235 | |
236 | |
237 | |
238 | GMatrixSymmetric& GMatrixSymmetric::operator=(const GMatrixSymmetric& matrix) |
239 | { |
240 | |
241 | if (this != &matrix) { |
242 | |
243 | |
244 | |
245 | |
246 | this->GMatrixBase::operator=(matrix); |
247 | |
248 | |
249 | free_members(); |
250 | |
251 | |
252 | init_members(); |
253 | |
254 | |
255 | copy_members(matrix); |
256 | |
257 | } |
258 | |
259 | |
260 | return *this; |
261 | } |
262 | |
263 | |
264 | |
265 | |
266 | |
267 | |
268 | |
269 | |
270 | |
271 | |
272 | GMatrixSymmetric& GMatrixSymmetric::operator=(const double& value) |
273 | { |
274 | |
275 | double* ptr = m_data; |
276 | for (int i = 0; i < m_elements; ++i) { |
277 | *ptr++ = value; |
278 | } |
279 | |
280 | |
281 | return *this; |
282 | } |
283 | |
284 | |
285 | |
286 | |
287 | |
288 | |
289 | |
290 | |
291 | |
292 | double& GMatrixSymmetric::operator()(const int& row, const int& column) |
293 | { |
294 | |
295 | int inx = (row >= column) ? m_colstart[column]+(row-column) |
296 | : m_colstart[row]+(column-row); |
297 | |
298 | |
299 | return m_data[inx]; |
300 | } |
301 | |
302 | |
303 | |
304 | |
305 | |
306 | |
307 | |
308 | |
309 | |
310 | const double& GMatrixSymmetric::operator()(const int& row, |
311 | const int& column) const |
312 | { |
313 | |
314 | int inx = (row >= column) ? m_colstart[column]+(row-column) |
315 | : m_colstart[row]+(column-row); |
316 | |
317 | |
318 | return m_data[inx]; |
319 | } |
320 | |
321 | |
322 | |
323 | |
324 | |
325 | |
326 | |
327 | |
328 | |
329 | |
330 | |
331 | |
332 | |
333 | |
334 | |
335 | GVector GMatrixSymmetric::operator*(const GVector& vector) const |
336 | { |
337 | |
338 | if (m_cols != vector.size()) { |
339 | throw GException::matrix_vector_mismatch(G_OP_MUL_VEC"GMatrixSymmetric::operator*(GVector&)", vector.size(), |
340 | m_rows, m_cols); |
341 | } |
342 | |
343 | |
344 | GVector result(m_rows); |
345 | for (int row = 0; row < m_rows; ++row) { |
346 | double sum = 0.0; |
347 | for (int col = 0; col < m_cols; ++col) { |
348 | sum += (*this)(row,col) * vector[col]; |
349 | } |
350 | result[row] = sum; |
351 | } |
352 | |
353 | |
354 | return result; |
355 | } |
356 | |
357 | |
358 | |
359 | |
360 | |
361 | |
362 | |
363 | |
364 | |
365 | |
366 | GMatrixSymmetric GMatrixSymmetric::operator-(void) const |
367 | { |
368 | |
369 | GMatrixSymmetric matrix = *this; |
370 | |
371 | |
372 | double* ptr = matrix.m_data; |
373 | for (int i = 0; i < matrix.m_elements; ++i, ++ptr) { |
374 | *ptr = -(*ptr); |
375 | } |
376 | |
377 | |
378 | return matrix; |
379 | } |
380 | |
381 | |
382 | |
383 | |
384 | |
385 | |
386 | |
387 | |
388 | |
389 | |
390 | |
391 | |
392 | |
393 | GMatrixSymmetric& GMatrixSymmetric::operator+=(const GMatrixSymmetric& matrix) |
394 | { |
395 | |
396 | if (m_rows != matrix.m_rows || m_cols != matrix.m_cols) { |
397 | throw GException::matrix_mismatch(G_OP_ADD"GMatrixSymmetric::operator+=(GMatrixSymmetric&)", |
398 | m_rows, m_cols, |
399 | matrix.m_rows, matrix.m_cols); |
400 | } |
401 | |
402 | |
403 | const double* src = matrix.m_data; |
404 | double* dst = m_data; |
405 | for (int i = 0; i < m_elements; ++i) { |
406 | *dst++ += *src++; |
407 | } |
408 | |
409 | |
410 | return *this; |
411 | } |
412 | |
413 | |
414 | |
415 | |
416 | |
417 | |
418 | |
419 | |
420 | |
421 | |
422 | |
423 | |
424 | |
425 | GMatrixSymmetric& GMatrixSymmetric::operator-=(const GMatrixSymmetric& matrix) |
426 | { |
427 | |
428 | if (m_rows != matrix.m_rows || m_cols != matrix.m_cols) { |
429 | throw GException::matrix_mismatch(G_OP_SUB"GMatrixSymmetric::operator-=(GMatrixSymmetric&)", |
430 | m_rows, m_cols, |
431 | matrix.m_rows, matrix.m_cols); |
432 | } |
433 | |
434 | |
435 | const double* src = matrix.m_data; |
436 | double* dst = m_data; |
437 | for (int i = 0; i < m_elements; ++i) { |
438 | *dst++ -= *src++; |
439 | } |
440 | |
441 | |
442 | return *this; |
443 | } |
444 | |
445 | |
446 | |
447 | |
448 | |
449 | |
450 | |
451 | |
452 | |
453 | |
454 | |
455 | void GMatrixSymmetric::clear(void) |
456 | { |
457 | |
458 | free_members(); |
459 | |
460 | |
461 | init_members(); |
462 | |
463 | |
464 | return; |
465 | } |
466 | |
467 | |
468 | |
469 | |
470 | |
471 | |
472 | |
473 | GMatrixSymmetric* GMatrixSymmetric::clone(void) const |
474 | { |
475 | |
476 | return new GMatrixSymmetric(*this); |
477 | } |
478 | |
479 | |
480 | |
481 | |
482 | |
483 | |
484 | |
485 | |
486 | |
487 | |
488 | |
489 | |
490 | double& GMatrixSymmetric::at(const int& row, const int& column) |
491 | { |
492 | |
493 | if (row < 0 || row >= m_rows || column < 0 || column >= m_cols) { |
494 | throw GException::out_of_range(G_AT"GMatrixSymmetric::at(int&, int&)", row, column, m_rows, m_cols); |
495 | } |
496 | |
497 | |
498 | int inx = (row >= column) ? m_colstart[column]+(row-column) |
499 | : m_colstart[row]+(column-row); |
500 | |
501 | |
502 | return m_data[inx]; |
503 | } |
504 | |
505 | |
506 | |
507 | |
508 | |
509 | |
510 | |
511 | |
512 | |
513 | |
514 | |
515 | |
516 | const double& GMatrixSymmetric::at(const int& row, const int& column) const |
517 | { |
518 | |
519 | if (row < 0 || row >= m_rows || column < 0 || column >= m_cols) { |
520 | throw GException::out_of_range(G_AT"GMatrixSymmetric::at(int&, int&)", row, column, m_rows, m_cols); |
521 | } |
522 | |
523 | |
524 | int inx = (row >= column) ? m_colstart[column]+(row-column) |
525 | : m_colstart[row]+(column-row); |
526 | |
527 | |
528 | return m_data[inx]; |
529 | } |
530 | |
531 | |
532 | |
533 | |
534 | |
535 | |
536 | |
537 | |
538 | |
539 | |
540 | |
541 | |
542 | GVector GMatrixSymmetric::row(const int& row) const |
543 | { |
544 | |
545 | #if defined(G_RANGE_CHECK1) |
546 | if (row < 0 || row >= m_rows) { |
547 | throw GException::out_of_range(G_EXTRACT_ROW"GMatrixSymmetric::row(int&)", row, 0, m_rows-1); |
548 | } |
549 | #endif |
550 | |
551 | |
552 | GVector result(m_cols); |
553 | |
554 | |
555 | for (int col = 0; col < m_cols; ++col) { |
556 | result[col] = (*this)(row,col); |
557 | } |
558 | |
559 | |
560 | return result; |
561 | } |
562 | |
563 | |
564 | |
565 | |
566 | |
567 | |
568 | |
569 | void GMatrixSymmetric::row(const int& row, const GVector& vector) |
570 | { |
571 | |
572 | #if defined(G_RANGE_CHECK1) |
573 | if (row < 0 || row >= m_rows) { |
574 | throw GException::out_of_range(G_SET_ROW"GMatrixSymmetric::row(int&, GVector&)", row, 0, m_rows-1); |
575 | } |
576 | #endif |
577 | |
578 | |
579 | return; |
580 | } |
581 | |
582 | |
583 | |
584 | |
585 | |
586 | |
587 | |
588 | |
589 | |
590 | |
591 | |
592 | |
593 | GVector GMatrixSymmetric::column(const int& column) const |
594 | { |
595 | |
596 | #if defined(G_RANGE_CHECK1) |
597 | if (column < 0 || column >= m_cols) { |
598 | throw GException::out_of_range(G_EXTRACT_COLUMN"GMatrixSymmetric::column(int&)", column, 0, m_cols-1); |
599 | } |
600 | #endif |
601 | |
602 | |
603 | GVector result(m_rows); |
604 | |
605 | |
606 | for (int row = 0; row < m_rows; ++row) { |
607 | result[row] = (*this)(row, column); |
608 | } |
609 | |
610 | |
611 | return result; |
612 | } |
613 | |
614 | |
615 | |
616 | |
617 | |
618 | |
619 | |
620 | |
621 | |
622 | |
623 | |
624 | |
625 | |
626 | |
627 | |
628 | |
629 | void GMatrixSymmetric::column(const int& column, const GVector& vector) |
630 | { |
631 | |
632 | #if defined(G_RANGE_CHECK1) |
633 | if (column < 0 || column >= m_cols) { |
634 | throw GException::out_of_range(G_SET_COLUMN"GMatrixSymmetric::column(int&, GVector&)", column, 0, m_cols-1); |
635 | } |
636 | #endif |
637 | |
638 | |
639 | |
640 | if (m_rows != vector.size()) { |
641 | throw GException::matrix_vector_mismatch(G_SET_COLUMN"GMatrixSymmetric::column(int&, GVector&)", vector.size(), |
642 | m_rows, m_cols); |
643 | } |
644 | |
645 | |
646 | for (int row = 0; row < m_rows; ++row) { |
647 | (*this)(row, column) = vector[row]; |
648 | } |
649 | |
650 | |
651 | return; |
652 | } |
653 | |
654 | |
655 | |
656 | |
657 | |
658 | |
659 | |
660 | void GMatrixSymmetric::add_to_row(const int& row, const GVector& vector) |
661 | { |
662 | |
663 | #if defined(G_RANGE_CHECK1) |
664 | if (row < 0 || row >= m_rows) { |
665 | throw GException::out_of_range(G_ADD_TO_ROW"GMatrixSymmetric::add_to_row(int&, GVector&)", row, 0, m_rows-1); |
666 | } |
667 | #endif |
668 | |
669 | |
670 | return; |
671 | } |
672 | |
673 | |
674 | |
675 | |
676 | |
677 | |
678 | |
679 | |
680 | |
681 | |
682 | |
683 | |
684 | |
685 | |
686 | |
687 | void GMatrixSymmetric::add_to_column(const int& column, const GVector& vector) |
688 | { |
689 | |
690 | #if defined(G_RANGE_CHECK1) |
691 | if (column < 0 || column >= m_cols) { |
692 | throw GException::out_of_range(G_ADD_TO_COLUMN"GMatrixSymmetric::add_to_column(int&, GVector&)", column, 0, m_cols-1); |
693 | } |
694 | #endif |
695 | |
696 | |
697 | |
698 | if (m_rows != vector.size()) { |
699 | throw GException::matrix_vector_mismatch(G_ADD_TO_COLUMN"GMatrixSymmetric::add_to_column(int&, GVector&)", vector.size(), |
700 | m_rows, m_cols); |
701 | } |
702 | |
703 | |
704 | for (int row = 0; row < m_rows; ++row) { |
705 | (*this)(row, column) += vector[row]; |
706 | } |
707 | |
708 | |
709 | return; |
710 | } |
711 | |
712 | |
713 | |
714 | |
715 | |
716 | |
717 | |
718 | |
719 | |
720 | |
721 | |
722 | |
723 | GMatrixSymmetric GMatrixSymmetric::invert(void) const |
724 | { |
725 | |
726 | GMatrixSymmetric matrix(m_cols, m_rows); |
727 | |
728 | |
729 | matrix.cholesky_invert(true); |
730 | |
731 | |
732 | return matrix; |
733 | } |
734 | |
735 | |
736 | |
737 | |
738 | |
739 | |
740 | |
741 | |
742 | |
743 | |
744 | |
745 | |
746 | |
747 | |
748 | |
749 | |
750 | |
751 | GVector GMatrixSymmetric::solve(const GVector& vector) const |
752 | { |
753 | |
754 | GMatrixSymmetric decomposition = cholesky_decompose(true); |
755 | |
756 | |
757 | GVector result = decomposition.cholesky_solver(vector); |
758 | |
759 | |
760 | return result; |
761 | } |
762 | |
763 | |
764 | |
765 | |
766 | |
767 | |
768 | |
769 | |
770 | |
771 | |
772 | GMatrixSymmetric GMatrixSymmetric::abs(void) const |
773 | { |
774 | |
775 | GMatrixSymmetric matrix(m_rows, m_cols); |
776 | |
777 | |
778 | double* src = m_data; |
779 | double* dst = matrix.m_data; |
780 | for (int i = 0; i < m_elements; ++i) { |
781 | *dst++ = std::abs(*src++); |
782 | } |
783 | |
784 | |
785 | return matrix; |
786 | } |
787 | |
788 | |
789 | |
790 | |
791 | |
792 | |
793 | |
794 | |
795 | |
796 | |
797 | double GMatrixSymmetric::fill(void) const |
798 | { |
799 | |
800 | int zero = 0; |
801 | for (int col = 0, i = 0; col < m_cols; ++col) { |
802 | if (m_data[i++] == 0.0) { |
803 | zero++; |
804 | } |
805 | for (int row = col+1; row < m_rows; ++row) { |
806 | if (m_data[i++] == 0.0) { |
807 | zero +=2; |
808 | } |
809 | } |
810 | } |
811 | |
812 | |
813 | return (1.0-double(zero)/double(m_elements)); |
814 | } |
815 | |
816 | |
817 | |
818 | |
819 | |
820 | |
821 | |
822 | double GMatrixSymmetric::sum(void) const |
823 | { |
824 | |
825 | double diag = 0.0; |
826 | double off_diag = 0.0; |
827 | |
828 | |
829 | for (int row = 0; row < m_rows; ++row) { |
830 | diag += m_data[m_colstart[row]]; |
831 | } |
832 | |
833 | |
834 | for (int row = 0; row < m_rows; ++row) { |
835 | for (int col = row+1; col < m_cols; ++col) { |
836 | off_diag += m_data[m_colstart[row]+(col-row)]; |
837 | } |
838 | } |
839 | |
840 | |
841 | double result = diag + 2.0 * off_diag; |
842 | |
843 | |
844 | return result; |
845 | } |
846 | |
847 | |
848 | |
849 | |
850 | |
851 | |
852 | |
853 | |
854 | |
855 | GMatrix GMatrixSymmetric::extract_lower_triangle(void) const |
856 | { |
857 | |
858 | GMatrix result(m_rows, m_cols); |
859 | |
860 | |
861 | for (int row = 0; row < m_rows; ++row) { |
862 | for (int col = 0; col <= row; ++col) { |
863 | result(row,col) = m_data[m_colstart[col]+(row-col)]; |
864 | } |
865 | } |
866 | |
867 | |
868 | return result; |
869 | } |
870 | |
871 | |
872 | |
873 | |
874 | |
875 | |
876 | |
877 | |
878 | |
879 | GMatrix GMatrixSymmetric::extract_upper_triangle(void) const |
880 | { |
881 | |
882 | GMatrix result(m_rows, m_cols); |
883 | |
884 | |
885 | for (int row = 0; row < m_rows; ++row) { |
886 | for (int col = row; col < m_cols; ++col) { |
887 | result(row,col) = m_data[m_colstart[row]+(col-row)]; |
888 | } |
889 | } |
890 | |
891 | |
892 | return result; |
893 | } |
894 | |
895 | |
896 | |
897 | |
898 | |
899 | |
900 | |
901 | |
902 | |
903 | |
904 | |
905 | |
906 | |
907 | |
908 | |
909 | |
910 | |
911 | |
912 | |
913 | |
914 | |
915 | |
916 | |
917 | GMatrixSymmetric GMatrixSymmetric::cholesky_decompose(const bool& compress) const |
918 | { |
919 | |
920 | GMatrixSymmetric matrix = *this; |
| 2 | | Calling copy constructor for 'GMatrixSymmetric' | |
|
| 6 | | Returning from copy constructor for 'GMatrixSymmetric' | |
|
921 | |
922 | |
923 | if (compress) { |
| |
924 | matrix.set_inx(); |
| 8 | | Calling 'GMatrixSymmetric::set_inx' | |
|
925 | } |
926 | |
927 | |
928 | int no_zeros = ((compress && (matrix.m_num_inx == matrix.m_rows)) || !compress); |
929 | |
930 | |
931 | if (no_zeros) { |
932 | |
933 | |
934 | double diag = 0.0; |
935 | for (int row = 0; row < matrix.m_rows; ++row) { |
936 | double* ptr = matrix.m_data + matrix.m_colstart[row]; |
937 | for (int col = row; col < matrix.m_cols; ++col, ++ptr) { |
938 | |
939 | double sum = *ptr; |
940 | for (int k = 0; k < row; ++k) { |
941 | int offset = matrix.m_colstart[k] - k; |
942 | sum -= matrix.m_data[offset+row] * matrix.m_data[offset+col]; |
943 | } |
944 | if (row == col) { |
945 | if (sum <= 0.0) { |
946 | throw GException::matrix_not_pos_definite(G_CHOL_DECOMP"GMatrixSymmetric::cholesky_decompose(int&)", row, sum); |
947 | } |
948 | *ptr = std::sqrt(sum); |
949 | diag = 1.0/(*ptr); |
950 | } |
951 | else { |
952 | *ptr = sum*diag; |
953 | } |
954 | } |
955 | } |
956 | } |
957 | |
958 | |
959 | else if (matrix.m_num_inx > 0) { |
960 | |
961 | |
962 | int row; |
963 | int col; |
964 | int k; |
965 | int* row_ptr; |
966 | int* col_ptr; |
967 | int* k_ptr; |
968 | |
969 | |
970 | double diag = 0.0; |
971 | for (row = 0, row_ptr = matrix.m_inx; row < matrix.m_num_inx; ++row, ++row_ptr) { |
972 | double* ptr_0 = matrix.m_data + matrix.m_colstart[*row_ptr] - *row_ptr; |
973 | for (col = row, col_ptr = matrix.m_inx + row; col < matrix.m_num_inx; ++col, ++col_ptr) { |
974 | double* ptr = ptr_0 + *col_ptr; |
975 | double sum = *ptr; |
976 | for (k = 0, k_ptr = matrix.m_inx; k < row; ++k, ++k_ptr) { |
977 | int offset = matrix.m_colstart[*k_ptr] - *k_ptr; |
978 | sum -= matrix.m_data[offset+*row_ptr] * |
979 | matrix.m_data[offset+*col_ptr]; |
980 | |
981 | } |
982 | if (*row_ptr == *col_ptr) { |
983 | if (sum <= 0.0) { |
984 | throw GException::matrix_not_pos_definite(G_CHOL_DECOMP"GMatrixSymmetric::cholesky_decompose(int&)", *row_ptr, sum); |
985 | } |
986 | *ptr = std::sqrt(sum); |
987 | diag = 1.0/(*ptr); |
988 | } |
989 | else { |
990 | *ptr = sum*diag; |
991 | } |
992 | } |
993 | } |
994 | } |
995 | |
996 | |
997 | else { |
998 | throw GException::matrix_zero(G_CHOL_DECOMP"GMatrixSymmetric::cholesky_decompose(int&)"); |
999 | } |
1000 | |
1001 | |
1002 | return matrix; |
1003 | } |
1004 | |
1005 | |
1006 | |
1007 | |
1008 | |
1009 | |
1010 | |
1011 | |
1012 | |
1013 | |
1014 | |
1015 | |
1016 | |
1017 | |
1018 | |
1019 | |
1020 | |
1021 | |
1022 | GVector GMatrixSymmetric::cholesky_solver(const GVector& vector, |
1023 | const bool& compress) const |
1024 | { |
1025 | |
1026 | if (m_rows != vector.size()) { |
1027 | throw GException::matrix_vector_mismatch(G_CHOL_SOLVE"GMatrixSymmetric::cholesky_solver(GVector&, int&)", vector.size(), |
1028 | m_rows, m_cols); |
1029 | } |
1030 | |
1031 | |
1032 | GVector x(m_rows); |
1033 | |
1034 | |
1035 | int no_zeros = ((compress && (m_num_inx == m_rows)) || !compress); |
1036 | |
1037 | |
1038 | if (no_zeros) { |
1039 | |
1040 | |
1041 | for (int row = 0; row < m_rows; ++row) { |
1042 | double sum = vector[row]; |
1043 | for (int k = 0; k < row; ++k) { |
1044 | sum -= m_data[m_colstart[k]+(row-k)] * x[k]; |
1045 | } |
1046 | x[row] = sum/m_data[m_colstart[row]]; |
1047 | } |
1048 | |
1049 | |
1050 | for (int row = m_rows-1; row >= 0; --row) { |
1051 | double sum = x[row]; |
1052 | double* ptr = m_data + m_colstart[row] + 1; |
1053 | for (int k = row+1; k < m_rows; ++k) { |
1054 | sum -= *ptr++ * x[k]; |
1055 | } |
1056 | x[row] = sum/m_data[m_colstart[row]]; |
1057 | } |
1058 | } |
1059 | |
1060 | |
1061 | else if (m_num_inx > 0) { |
1062 | |
1063 | |
1064 | int row; |
1065 | int k; |
1066 | int* row_ptr; |
1067 | int* k_ptr; |
1068 | |
1069 | |
1070 | for (row = 0, row_ptr = m_inx; row < m_num_inx; ++row, ++row_ptr) { |
1071 | double sum = vector[*row_ptr]; |
1072 | double* ptr = m_data + *row_ptr; |
1073 | for (k = 0, k_ptr = m_inx; k < row; ++k, ++k_ptr) { |
1074 | sum -= *(ptr + m_colstart[*k_ptr] - *k_ptr) * x[*k_ptr]; |
1075 | } |
1076 | x[*row_ptr] = sum/m_data[m_colstart[*row_ptr]]; |
1077 | } |
1078 | |
1079 | |
1080 | for (row = m_num_inx-1, row_ptr = m_inx+m_num_inx-1; row >= 0; --row, --row_ptr) { |
1081 | double sum = x[*row_ptr]; |
1082 | double* ptr_diag = m_data + m_colstart[*row_ptr]; |
1083 | double* ptr = ptr_diag - *row_ptr; |
1084 | for (k = row+1, k_ptr = m_inx+row+1; k < m_num_inx; ++k, ++k_ptr) { |
1085 | sum -= *(ptr + *k_ptr) * x[*k_ptr]; |
1086 | } |
1087 | x[*row_ptr] = sum/(*ptr_diag); |
1088 | } |
1089 | } |
1090 | |
1091 | |
1092 | else { |
1093 | throw GException::matrix_zero(G_CHOL_SOLVE"GMatrixSymmetric::cholesky_solver(GVector&, int&)"); |
1094 | } |
1095 | |
1096 | |
1097 | return x; |
1098 | } |
1099 | |
1100 | |
1101 | |
1102 | |
1103 | |
1104 | |
1105 | |
1106 | |
1107 | |
1108 | |
1109 | |
1110 | |
1111 | |
1112 | |
1113 | |
1114 | |
1115 | |
1116 | GMatrixSymmetric GMatrixSymmetric::cholesky_invert(const bool& compress) const |
1117 | { |
1118 | |
1119 | GMatrixSymmetric matrix = cholesky_decompose(compress); |
| 1 | Calling 'GMatrixSymmetric::cholesky_decompose' | |
|
1120 | |
1121 | |
1122 | int no_zeros = ((compress && (matrix.m_num_inx == matrix.m_rows)) || !compress); |
1123 | |
1124 | |
1125 | if (no_zeros) { |
1126 | |
1127 | |
1128 | for (int row = 0; row < matrix.m_rows; ++row) { |
1129 | |
1130 | |
1131 | double* ptr = matrix.m_data + matrix.m_colstart[row]; |
1132 | *ptr = 1.0/(*ptr); |
1133 | |
1134 | for (int col = row+1; col < matrix.m_cols; ++col) { |
1135 | |
1136 | |
1137 | double sum = 0.0; |
1138 | double* ptr1 = matrix.m_data + col - row; |
1139 | double* ptr2 = ptr; |
1140 | for (int k = row; k < col; ++k) { |
1141 | sum -= *(ptr1-- + matrix.m_colstart[k]) * *ptr2++; |
1142 | } |
1143 | |
1144 | |
1145 | *(ptr+col-row) = sum/matrix.m_data[matrix.m_colstart[col]]; |
1146 | } |
1147 | } |
1148 | |
1149 | |
1150 | for (int row = 0; row < matrix.m_rows; ++row) { |
1151 | double* ptr = matrix.m_data + matrix.m_colstart[row]; |
1152 | for (int col = row; col < matrix.m_cols; ++col) { |
1153 | |
1154 | double sum = 0.0; |
1155 | double* ptr1 = ptr + col - row; |
1156 | double* ptr2 = matrix.m_data + matrix.m_colstart[col]; |
1157 | for (int k = col; k < matrix.m_cols; ++k) { |
1158 | sum += *ptr1++ * *ptr2++; |
1159 | } |
1160 | |
1161 | *(ptr+col-row) = sum; |
1162 | } |
1163 | } |
1164 | } |
1165 | |
1166 | |
1167 | else if (matrix.m_num_inx > 0) { |
1168 | |
1169 | |
1170 | int row; |
1171 | int col; |
1172 | int k; |
1173 | int* row_ptr; |
1174 | int* col_ptr; |
1175 | int* k_ptr; |
1176 | |
1177 | |
1178 | for (row = 0, row_ptr = matrix.m_inx; |
1179 | row < matrix.m_num_inx; ++row, ++row_ptr) { |
1180 | |
1181 | |
1182 | double* ptr_diag = matrix.m_data + matrix.m_colstart[*row_ptr]; |
1183 | double* ptr_2 = ptr_diag - *row_ptr; |
1184 | *ptr_diag = 1.0/(*ptr_diag); |
1185 | |
1186 | for (col = row+1, col_ptr = matrix.m_inx+row+1; |
1187 | col < matrix.m_num_inx; ++col, ++col_ptr) { |
1188 | |
1189 | |
1190 | double sum = 0.0; |
1191 | double* ptr_1 = matrix.m_data + *col_ptr; |
1192 | for (k = row, k_ptr = matrix.m_inx+row; |
1193 | k < col; ++k, ++k_ptr) { |
1194 | sum -= *(ptr_1 + matrix.m_colstart[*k_ptr] - *k_ptr) * |
1195 | *(ptr_2 + *k_ptr); |
1196 | } |
1197 | |
1198 | |
1199 | *(ptr_2 + *col_ptr) = |
1200 | sum/matrix.m_data[matrix.m_colstart[*col_ptr]]; |
1201 | } |
1202 | } |
1203 | |
1204 | |
1205 | for (row = 0, row_ptr = matrix.m_inx; |
1206 | row < matrix.m_num_inx; ++row, ++row_ptr) { |
1207 | double* ptr_diag = matrix.m_data + matrix.m_colstart[*row_ptr]; |
1208 | double* ptr_1 = ptr_diag - *row_ptr; |
1209 | |
1210 | for (col = row, col_ptr = matrix.m_inx+row; |
1211 | col < matrix.m_num_inx; ++col, ++col_ptr) { |
1212 | |
1213 | |
1214 | double sum = 0.0; |
1215 | double* ptr_2 = matrix.m_data + matrix.m_colstart[*col_ptr] - *col_ptr; |
1216 | for (k = col, k_ptr = matrix.m_inx+col; |
1217 | k < matrix.m_num_inx; ++k, ++k_ptr) { |
1218 | sum += *(ptr_1 + *k_ptr) * *(ptr_2 + *k_ptr); |
1219 | } |
1220 | |
1221 | |
1222 | *(ptr_1 + *col_ptr) = sum; |
1223 | } |
1224 | } |
1225 | } |
1226 | |
1227 | |
1228 | else { |
1229 | throw GException::matrix_zero(G_CHOL_INVERT"GMatrixSymmetric::cholesky_invert(int&)"); |
1230 | } |
1231 | |
1232 | |
1233 | return matrix; |
1234 | } |
1235 | |
1236 | |
1237 | |
1238 | |
1239 | |
1240 | |
1241 | |
1242 | |
1243 | std::string GMatrixSymmetric::print(const GChatter& chatter) const |
1244 | { |
1245 | |
1246 | std::string result; |
1247 | |
1248 | |
1249 | result.append("=== GMatrixSymmetric ==="); |
1250 | |
1251 | |
1252 | if (chatter != SILENT) { |
1253 | |
1254 | |
1255 | result.append("\n"+gammalib::parformat("Number of rows")); |
1256 | result.append(gammalib::str(m_rows)); |
1257 | if (m_rowsel != NULL__null) { |
1258 | result.append(" (compressed "+gammalib::str(m_num_rowsel)+")"); |
1259 | } |
1260 | result.append("\n"+gammalib::parformat("Number of columns")); |
1261 | result.append(gammalib::str(m_cols)); |
1262 | if (m_colsel != NULL__null) { |
1263 | result.append(" (compressed "+gammalib::str(m_num_colsel)+")"); |
1264 | } |
1265 | result.append("\n"+gammalib::parformat("Number of elements")); |
1266 | result.append(gammalib::str(m_elements)); |
1267 | result.append("\n"+gammalib::parformat("Number of allocated cells")); |
1268 | result.append(gammalib::str(m_alloc)); |
1269 | |
1270 | |
1271 | result.append(print_elements(chatter)); |
1272 | result.append(print_row_compression(chatter)); |
1273 | result.append(print_col_compression(chatter)); |
1274 | |
1275 | } |
1276 | |
1277 | |
1278 | return result; |
1279 | } |
1280 | |
1281 | |
1282 | |
1283 | |
1284 | |
1285 | |
1286 | |
1287 | |
1288 | |
1289 | |
1290 | |
1291 | void GMatrixSymmetric::init_members(void) |
1292 | { |
1293 | |
1294 | m_num_inx = 0; |
1295 | m_inx = NULL__null; |
| 4 | | Null pointer value stored to 'matrix.m_inx' | |
|
1296 | |
1297 | |
1298 | return; |
1299 | } |
1300 | |
1301 | |
1302 | |
1303 | |
1304 | |
1305 | |
1306 | |
1307 | void GMatrixSymmetric::copy_members(const GMatrixSymmetric& matrix) |
1308 | { |
1309 | |
1310 | m_num_inx = matrix.m_num_inx; |
1311 | |
1312 | |
1313 | if (m_cols > 0) { |
1314 | m_inx = new int[m_cols]; |
1315 | for (int i = 0; i < m_cols; ++i) { |
1316 | m_inx[i] = matrix.m_inx[i]; |
1317 | } |
1318 | } |
1319 | |
1320 | |
1321 | return; |
1322 | } |
1323 | |
1324 | |
1325 | |
1326 | |
1327 | |
1328 | void GMatrixSymmetric::free_members(void) |
1329 | { |
1330 | |
1331 | if (m_inx != NULL__null) delete [] m_inx; |
1332 | |
1333 | |
1334 | return; |
1335 | } |
1336 | |
1337 | |
1338 | |
1339 | |
1340 | |
1341 | |
1342 | |
1343 | |
1344 | |
1345 | |
1346 | |
1347 | |
1348 | |
1349 | |
1350 | |
1351 | |
1352 | |
1353 | |
1354 | |
1355 | |
1356 | |
1357 | void GMatrixSymmetric::alloc_members(const int& rows, const int& columns) |
1358 | { |
1359 | |
1360 | int elements = rows*(rows+1)/2; |
1361 | |
1362 | |
1363 | if (rows != columns) { |
1364 | throw GException::matrix_not_symmetric(G_ALLOC_MEMBERS"GMatrixSymmetric::alloc_members(int&, int&)", rows, columns); |
1365 | } |
1366 | |
1367 | |
1368 | if (elements > 0) { |
1369 | |
1370 | |
1371 | if (m_data != NULL__null) delete [] m_data; |
1372 | if (m_colstart != NULL__null) delete [] m_colstart; |
1373 | if (m_inx != NULL__null) delete [] m_inx; |
1374 | |
1375 | |
1376 | m_data = new double[elements]; |
1377 | m_colstart = new int[columns+1]; |
1378 | m_inx = new int[columns]; |
1379 | |
1380 | |
1381 | m_rows = rows; |
1382 | m_cols = columns; |
1383 | m_elements = elements; |
1384 | m_alloc = elements; |
1385 | |
1386 | |
1387 | m_colstart[0] = 0; |
1388 | int offset = rows; |
1389 | for (int col = 1; col <= m_cols; ++col) { |
1390 | m_colstart[col] = m_colstart[col-1] + offset--; |
1391 | } |
1392 | |
1393 | |
1394 | for (int i = 0; i < m_elements; ++i) { |
1395 | m_data[i] = 0.0; |
1396 | } |
1397 | |
1398 | } |
1399 | |
1400 | |
1401 | return; |
1402 | } |
1403 | |
1404 | |
1405 | |
1406 | |
1407 | |
1408 | |
1409 | |
1410 | |
1411 | |
1412 | void GMatrixSymmetric::set_inx(void) |
1413 | { |
1414 | |
1415 | int row; |
1416 | int col; |
1417 | |
1418 | |
1419 | m_num_inx = 0; |
1420 | for (row = 0; row < m_rows; ++row) { |
| 9 | | Loop condition is true. Entering loop body | |
|
| 15 | | Loop condition is true. Entering loop body | |
|
1421 | |
1422 | |
1423 | |
1424 | if (m_data[m_colstart[row]] == 0.0) { |
| |
| |
1425 | for (col = 0; col < row; ++col) { |
| 11 | | Loop condition is false. Execution continues on line 1431 | |
|
| 17 | | Loop condition is true. Entering loop body | |
|
1426 | if (m_data[m_colstart[col]+(row-col)] != 0.0) { |
| |
1427 | break; |
| 19 | | Execution continues on line 1431 | |
|
1428 | } |
1429 | } |
1430 | |
1431 | if (col < row) { |
| |
| |
1432 | m_inx[m_num_inx++] = row; |
| 21 | | Array access (via field 'm_inx') results in a null pointer dereference |
|
1433 | } |
1434 | else { |
1435 | for (col = row+1; col < m_cols; ++col) { |
| 13 | | Loop condition is false. Execution continues on line 1441 | |
|
1436 | if (m_data[m_colstart[row]+(col-row)] != 0.0) { |
1437 | break; |
1438 | } |
1439 | } |
1440 | |
1441 | if (col < m_cols) { |
| |
1442 | m_inx[m_num_inx++] = row; |
1443 | } |
1444 | } |
1445 | } |
1446 | else { |
1447 | m_inx[m_num_inx++] = row; |
1448 | } |
1449 | } |
1450 | |
1451 | |
1452 | return; |
1453 | } |
1454 | |
1455 | |
1456 | |
1457 | |
1458 | |
1459 | |
1460 | |
1461 | |
1462 | |
1463 | |
1464 | |
1465 | |
1466 | |
1467 | |
1468 | |
1469 | |
1470 | |
1471 | |
1472 | |
1473 | |
1474 | |
1475 | |
1476 | |
1477 | GMatrix GMatrixSymmetric::operator*(const GMatrixSymmetric& matrix) const |
1478 | { |
1479 | |
1480 | if (m_cols != matrix.m_rows) { |
1481 | throw GException::matrix_mismatch(G_OP_MAT_MUL"GMatrixSymmetric::operator*=(GMatrixSymmetric&)", |
1482 | m_rows, m_cols, |
1483 | matrix.m_rows, matrix.m_cols); |
1484 | } |
1485 | |
1486 | |
1487 | GMatrix result(m_rows, matrix.m_cols); |
1488 | |
1489 | |
1490 | for (int row = 0; row < m_rows; ++row) { |
1491 | for (int col = 0; col < matrix.m_cols; ++col) { |
1492 | double sum = 0.0; |
1493 | for (int i = 0; i < m_cols; ++i) { |
1494 | sum += (*this)(row,i) * matrix(i,col); |
1495 | } |
1496 | result(row,col) = sum; |
1497 | } |
1498 | } |
1499 | |
1500 | |
1501 | return result; |
1502 | } |