/usr/include/root/Math/CholeskyDecomp.h is in libroot-math-smatrix-dev 5.34.30-0ubuntu8.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 | // @(#)root/smatrix:$Id$
// Author: M. Schiller 2009
#ifndef ROOT_Math_CholeskyDecomp
#define ROOT_Math_CholeskyDecomp
/** @file
* header file containing the templated implementation of matrix inversion
* routines for use with ROOT's SMatrix classes (symmetric positive
* definite case)
*
* @author Manuel Schiller
* @date Aug 29 2008
* initial release inside LHCb
* @date May 7 2009
* factored code to provide a nice Cholesky decomposition class, along
* with separate methods for solving a single linear system and to
* obtain the inverse matrix from the decomposition
* @date July 15th 2013
* provide a version of that class which works if the dimension of the
* problem is only known at run time
* @date September 30th 2013
* provide routines to access the result of the decomposition L and its
* inverse
*/
#include <cmath>
#include <algorithm>
namespace ROOT {
namespace Math {
/// helpers for CholeskyDecomp
namespace CholeskyDecompHelpers {
// forward decls
template<class F, class M> struct _decomposerGenDim;
template<class F, unsigned N, class M> struct _decomposer;
template<class F, class M> struct _inverterGenDim;
template<class F, unsigned N, class M> struct _inverter;
template<class F, class V> struct _solverGenDim;
template<class F, unsigned N, class V> struct _solver;
template<typename G> class PackedArrayAdapter;
}
/// class to compute the Cholesky decomposition of a matrix
/** class to compute the Cholesky decomposition of a symmetric
* positive definite matrix
*
* provides routines to check if the decomposition succeeded (i.e. if
* matrix is positive definite and non-singular), to solve a linear
* system for the given matrix and to obtain its inverse
*
* the actual functionality is implemented in templated helper
* classes which have specializations for dimensions N = 1 to 6
* to achieve a gain in speed for common matrix sizes
*
* usage example:
* @code
* // let m be a symmetric positive definite SMatrix (use type float
* // for internal computations, matrix size is 4x4)
* CholeskyDecomp<float, 4> decomp(m);
* // check if the decomposition succeeded
* if (!decomp) {
* std::cerr << "decomposition failed!" << std::endl;
* } else {
* // let rhs be a vector; we seek a vector x such that m * x = rhs
* decomp.Solve(rhs);
* // rhs now contains the solution we are looking for
*
* // obtain the inverse of m, put it into m itself
* decomp.Invert(m);
* }
* @endcode
*/
template<class F, unsigned N> class CholeskyDecomp
{
private:
/// lower triangular matrix L
/** lower triangular matrix L, packed storage, with diagonal
* elements pre-inverted */
F fL[N * (N + 1) / 2];
/// flag indicating a successful decomposition
bool fOk;
public:
/// perform a Cholesky decomposition
/** perfrom a Cholesky decomposition of a symmetric positive
* definite matrix m
*
* this is the constructor to uses with an SMatrix (and objects
* that behave like an SMatrix in terms of using
* operator()(int i, int j) for access to elements)
*/
template<class M> CholeskyDecomp(const M& m) :
fL( ), fOk(false)
{
using CholeskyDecompHelpers::_decomposer;
fOk = _decomposer<F, N, M>()(fL, m);
}
/// perform a Cholesky decomposition
/** perfrom a Cholesky decomposition of a symmetric positive
* definite matrix m
*
* this is the constructor to use in special applications where
* plain arrays are used
*
* NOTE: the matrix is given in packed representation, matrix
* element m(i,j) (j <= i) is supposed to be in array element
* (i * (i + 1)) / 2 + j
*/
template<typename G> CholeskyDecomp(G* m) :
fL(), fOk(false)
{
using CholeskyDecompHelpers::_decomposer;
using CholeskyDecompHelpers::PackedArrayAdapter;
fOk = _decomposer<F, N, PackedArrayAdapter<G> >()(
fL, PackedArrayAdapter<G>(m));
}
/// returns true if decomposition was successful
/** @returns true if decomposition was successful */
bool ok() const { return fOk; }
/// returns true if decomposition was successful
/** @returns true if decomposition was successful */
operator bool() const { return fOk; }
/** @brief solves a linear system for the given right hand side
*
* Note that you can use both SVector classes and plain arrays for
* rhs. (Make sure that the sizes match!). It will work with any vector
* implementing the operator [i]
*
* @returns if the decomposition was successful
*/
template<class V> bool Solve(V& rhs) const
{
using CholeskyDecompHelpers::_solver;
if (fOk) _solver<F,N,V>()(rhs, fL); return fOk;
}
/** @brief place the inverse into m
*
* This is the method to use with an SMatrix.
*
* @returns if the decomposition was successful
*/
template<class M> bool Invert(M& m) const
{
using CholeskyDecompHelpers::_inverter;
if (fOk) _inverter<F,N,M>()(m, fL); return fOk;
}
/** @brief place the inverse into m
*
* This is the method to use with a plain array.
*
* @returns if the decomposition was successful
*
* NOTE: the matrix is given in packed representation, matrix
* element m(i,j) (j <= i) is supposed to be in array element
* (i * (i + 1)) / 2 + j
*/
template<typename G> bool Invert(G* m) const
{
using CholeskyDecompHelpers::_inverter;
using CholeskyDecompHelpers::PackedArrayAdapter;
if (fOk) {
PackedArrayAdapter<G> adapted(m);
_inverter<F,N,PackedArrayAdapter<G> >()(adapted, fL);
}
return fOk;
}
/** @brief obtain the decomposed matrix L
*
* This is the method to use with a plain array.
*
* @returns if the decomposition was successful
*/
template<class M> bool getL(M& m) const
{
if (!fOk) return false;
for (unsigned i = 0; i < N; ++i) {
// zero upper half of matrix
for (unsigned j = i + 1; j < N; ++j)
m(i, j) = F(0);
// copy the rest
for (unsigned j = 0; j <= i; ++j)
m(i, j) = fL[i * (i + 1) / 2 + j];
// adjust the diagonal - we save 1/L(i, i) in that position, so
// convert to what caller expects
m(i, i) = F(1) / m(i, i);
}
return true;
}
/** @brief obtain the decomposed matrix L
*
* @returns if the decomposition was successful
*
* NOTE: the matrix is given in packed representation, matrix
* element m(i,j) (j <= i) is supposed to be in array element
* (i * (i + 1)) / 2 + j
*/
template<typename G> bool getL(G* m) const
{
if (!fOk) return false;
// copy L
for (unsigned i = 0; i < (N * (N + 1)) / 2; ++i)
m[i] = fL[i];
// adjust diagonal - we save 1/L(i, i) in that position, so convert to
// what caller expects
for (unsigned i = 0; i < N; ++i)
m[(i * (i + 1)) / 2 + i] = F(1) / fL[(i * (i + 1)) / 2 + i];
return true;
}
/** @brief obtain the inverse of the decomposed matrix L
*
* This is the method to use with a plain array.
*
* @returns if the decomposition was successful
*/
template<class M> bool getLi(M& m) const
{
if (!fOk) return false;
for (unsigned i = 0; i < N; ++i) {
// zero lower half of matrix
for (unsigned j = i + 1; j < N; ++j)
m(j, i) = F(0);
// copy the rest
for (unsigned j = 0; j <= i; ++j)
m(j, i) = fL[i * (i + 1) / 2 + j];
}
// invert the off-diagonal part of what we just copied
for (unsigned i = 1; i < N; ++i) {
for (unsigned j = 0; j < i; ++j) {
typename M::value_type tmp = F(0);
for (unsigned k = i; k-- > j;)
tmp -= m(k, i) * m(j, k);
m(j, i) = tmp * m(i, i);
}
}
return true;
}
/** @brief obtain the inverse of the decomposed matrix L
*
* @returns if the decomposition was successful
*
* NOTE: the matrix is given in packed representation, matrix
* element m(j,i) (j <= i) is supposed to be in array element
* (i * (i + 1)) / 2 + j
*/
template<typename G> bool getLi(G* m) const
{
if (!fOk) return false;
// copy L
for (unsigned i = 0; i < (N * (N + 1)) / 2; ++i)
m[i] = fL[i];
// invert the off-diagonal part of what we just copied
G* base1 = &m[1];
for (unsigned i = 1; i < N; base1 += ++i) {
for (unsigned j = 0; j < i; ++j) {
G tmp = F(0);
const G *base2 = &m[(i * (i - 1)) / 2];
for (unsigned k = i; k-- > j; base2 -= k)
tmp -= base1[k] * base2[j];
base1[j] = tmp * base1[i];
}
}
return true;
}
};
/// class to compute the Cholesky decomposition of a matrix
/** class to compute the Cholesky decomposition of a symmetric
* positive definite matrix when the dimensionality of the problem is not known
* at compile time
*
* provides routines to check if the decomposition succeeded (i.e. if
* matrix is positive definite and non-singular), to solve a linear
* system for the given matrix and to obtain its inverse
*
* the actual functionality is implemented in templated helper
* classes which have specializations for dimensions N = 1 to 6
* to achieve a gain in speed for common matrix sizes
*
* usage example:
* @code
* // let m be a symmetric positive definite SMatrix (use type float
* // for internal computations, matrix size is 4x4)
* CholeskyDecompGenDim<float> decomp(4, m);
* // check if the decomposition succeeded
* if (!decomp) {
* std::cerr << "decomposition failed!" << std::endl;
* } else {
* // let rhs be a vector; we seek a vector x such that m * x = rhs
* decomp.Solve(rhs);
* // rhs now contains the solution we are looking for
*
* // obtain the inverse of m, put it into m itself
* decomp.Invert(m);
* }
* @endcode
*/
template<class F> class CholeskyDecompGenDim
{
private:
/** @brief dimensionality
* dimensionality of the problem */
unsigned fN;
/// lower triangular matrix L
/** lower triangular matrix L, packed storage, with diagonal
* elements pre-inverted */
F *fL;
/// flag indicating a successful decomposition
bool fOk;
public:
/// perform a Cholesky decomposition
/** perfrom a Cholesky decomposition of a symmetric positive
* definite matrix m
*
* this is the constructor to uses with an SMatrix (and objects
* that behave like an SMatrix in terms of using
* operator()(int i, int j) for access to elements)
*/
template<class M> CholeskyDecompGenDim(unsigned N, const M& m) :
fN(N), fL(new F[(fN * (fN + 1)) / 2]), fOk(false)
{
using CholeskyDecompHelpers::_decomposerGenDim;
fOk = _decomposerGenDim<F, M>()(fL, m, fN);
}
/// perform a Cholesky decomposition
/** perfrom a Cholesky decomposition of a symmetric positive
* definite matrix m
*
* this is the constructor to use in special applications where
* plain arrays are used
*
* NOTE: the matrix is given in packed representation, matrix
* element m(i,j) (j <= i) is supposed to be in array element
* (i * (i + 1)) / 2 + j
*/
template<typename G> CholeskyDecompGenDim(unsigned N, G* m) :
fN(N), fL(new F[(fN * (fN + 1)) / 2]), fOk(false)
{
using CholeskyDecompHelpers::_decomposerGenDim;
using CholeskyDecompHelpers::PackedArrayAdapter;
fOk = _decomposerGenDim<F, PackedArrayAdapter<G> >()(
fL, PackedArrayAdapter<G>(m), fN);
}
/// destructor
~CholeskyDecompGenDim() { delete[] fL; }
/// returns true if decomposition was successful
/** @returns true if decomposition was successful */
bool ok() const { return fOk; }
/// returns true if decomposition was successful
/** @returns true if decomposition was successful */
operator bool() const { return fOk; }
/** @brief solves a linear system for the given right hand side
*
* Note that you can use both SVector classes and plain arrays for
* rhs. (Make sure that the sizes match!). It will work with any vector
* implementing the operator [i]
*
* @returns if the decomposition was successful
*/
template<class V> bool Solve(V& rhs) const
{
using CholeskyDecompHelpers::_solverGenDim;
if (fOk) _solverGenDim<F,V>()(rhs, fL, fN); return fOk;
}
/** @brief place the inverse into m
*
* This is the method to use with an SMatrix.
*
* @returns if the decomposition was successful
*/
template<class M> bool Invert(M& m) const
{
using CholeskyDecompHelpers::_inverterGenDim;
if (fOk) _inverterGenDim<F,M>()(m, fL, fN); return fOk;
}
/** @brief place the inverse into m
*
* This is the method to use with a plain array.
*
* @returns if the decomposition was successful
*
* NOTE: the matrix is given in packed representation, matrix
* element m(i,j) (j <= i) is supposed to be in array element
* (i * (i + 1)) / 2 + j
*/
template<typename G> bool Invert(G* m) const
{
using CholeskyDecompHelpers::_inverterGenDim;
using CholeskyDecompHelpers::PackedArrayAdapter;
if (fOk) {
PackedArrayAdapter<G> adapted(m);
_inverterGenDim<F,PackedArrayAdapter<G> >()(adapted, fL, fN);
}
return fOk;
}
/** @brief obtain the decomposed matrix L
*
* This is the method to use with a plain array.
*
* @returns if the decomposition was successful
*/
template<class M> bool getL(M& m) const
{
if (!fOk) return false;
for (unsigned i = 0; i < fN; ++i) {
// zero upper half of matrix
for (unsigned j = i + 1; j < fN; ++j)
m(i, j) = F(0);
// copy the rest
for (unsigned j = 0; j <= i; ++j)
m(i, j) = fL[i * (i + 1) / 2 + j];
// adjust the diagonal - we save 1/L(i, i) in that position, so
// convert to what caller expects
m(i, i) = F(1) / m(i, i);
}
return true;
}
/** @brief obtain the decomposed matrix L
*
* @returns if the decomposition was successful
*
* NOTE: the matrix is given in packed representation, matrix
* element m(i,j) (j <= i) is supposed to be in array element
* (i * (i + 1)) / 2 + j
*/
template<typename G> bool getL(G* m) const
{
if (!fOk) return false;
// copy L
for (unsigned i = 0; i < (fN * (fN + 1)) / 2; ++i)
m[i] = fL[i];
// adjust diagonal - we save 1/L(i, i) in that position, so convert to
// what caller expects
for (unsigned i = 0; i < fN; ++i)
m[(i * (i + 1)) / 2 + i] = F(1) / fL[(i * (i + 1)) / 2 + i];
return true;
}
/** @brief obtain the inverse of the decomposed matrix L
*
* This is the method to use with a plain array.
*
* @returns if the decomposition was successful
*/
template<class M> bool getLi(M& m) const
{
if (!fOk) return false;
for (unsigned i = 0; i < fN; ++i) {
// zero lower half of matrix
for (unsigned j = i + 1; j < fN; ++j)
m(j, i) = F(0);
// copy the rest
for (unsigned j = 0; j <= i; ++j)
m(j, i) = fL[i * (i + 1) / 2 + j];
}
// invert the off-diagonal part of what we just copied
for (unsigned i = 1; i < fN; ++i) {
for (unsigned j = 0; j < i; ++j) {
typename M::value_type tmp = F(0);
for (unsigned k = i; k-- > j;)
tmp -= m(k, i) * m(j, k);
m(j, i) = tmp * m(i, i);
}
}
return true;
}
/** @brief obtain the inverse of the decomposed matrix L
*
* @returns if the decomposition was successful
*
* NOTE: the matrix is given in packed representation, matrix
* element m(j,i) (j <= i) is supposed to be in array element
* (i * (i + 1)) / 2 + j
*/
template<typename G> bool getLi(G* m) const
{
if (!fOk) return false;
// copy L
for (unsigned i = 0; i < (fN * (fN + 1)) / 2; ++i)
m[i] = fL[i];
// invert the off-diagonal part of what we just copied
G* base1 = &m[1];
for (unsigned i = 1; i < fN; base1 += ++i) {
for (unsigned j = 0; j < i; ++j) {
G tmp = F(0);
const G *base2 = &m[(i * (i - 1)) / 2];
for (unsigned k = i; k-- > j; base2 -= k)
tmp -= base1[k] * base2[j];
base1[j] = tmp * base1[i];
}
}
return true;
}
};
namespace CholeskyDecompHelpers {
/// adapter for packed arrays (to SMatrix indexing conventions)
template<typename G> class PackedArrayAdapter
{
private:
G* fArr; ///< pointer to first array element
public:
/// constructor
PackedArrayAdapter(G* arr) : fArr(arr) {}
/// read access to elements (make sure that j <= i)
const G operator()(unsigned i, unsigned j) const
{ return fArr[((i * (i + 1)) / 2) + j]; }
/// write access to elements (make sure that j <= i)
G& operator()(unsigned i, unsigned j)
{ return fArr[((i * (i + 1)) / 2) + j]; }
};
/// struct to do a Cholesky decomposition (general dimensionality)
template<class F, class M> struct _decomposerGenDim
{
/// method to do the decomposition
/** @returns if the decomposition was successful */
bool operator()(F* dst, const M& src, unsigned N) const
{
// perform Cholesky decomposition of matrix: M = L L^T
// only thing that can go wrong: trying to take square
// root of negative number or zero (matrix is
// ill-conditioned or singular in these cases)
// element L(i,j) is at array position (i * (i+1)) / 2 + j
// quirk: we may need to invert L later anyway, so we can
// invert elements on diagonale straight away (we only
// ever need their reciprocals!)
// cache starting address of rows of L for speed reasons
F *base1 = &dst[0];
for (unsigned i = 0; i < N; base1 += ++i) {
F tmpdiag = F(0.0); // for element on diagonale
// calculate off-diagonal elements
F *base2 = &dst[0];
for (unsigned j = 0; j < i; base2 += ++j) {
F tmp = src(i, j);
for (unsigned k = j; k--; )
tmp -= base1[k] * base2[k];
base1[j] = tmp *= base2[j];
// keep track of contribution to element on diagonale
tmpdiag += tmp * tmp;
}
// keep truncation error small
tmpdiag = src(i, i) - tmpdiag;
// check if positive definite
if (tmpdiag <= F(0.0)) return false;
else base1[i] = std::sqrt(F(1.0) / tmpdiag);
}
return true;
}
};
/// struct to do a Cholesky decomposition
template<class F, unsigned N, class M> struct _decomposer
{
/// method to do the decomposition
/** @returns if the decomposition was successful */
bool operator()(F* dst, const M& src) const
{ return _decomposerGenDim<F, M>()(dst, src, N); }
};
/// struct to obtain the inverse from a Cholesky decomposition (general dimensionality)
template<class F, class M> struct _inverterGenDim
{
/// method to do the inversion
void operator()(M& dst, const F* src, unsigned N) const
{
// make working copy
F * l = new F[N * (N + 1) / 2];
std::copy(src, src + ((N * (N + 1)) / 2), l);
// ok, next step: invert off-diagonal part of matrix
F* base1 = &l[1];
for (unsigned i = 1; i < N; base1 += ++i) {
for (unsigned j = 0; j < i; ++j) {
F tmp = F(0.0);
const F *base2 = &l[(i * (i - 1)) / 2];
for (unsigned k = i; k-- > j; base2 -= k)
tmp -= base1[k] * base2[j];
base1[j] = tmp * base1[i];
}
}
// Li = L^(-1) formed, now calculate M^(-1) = Li^T Li
for (unsigned i = N; i--; ) {
for (unsigned j = i + 1; j--; ) {
F tmp = F(0.0);
base1 = &l[(N * (N - 1)) / 2];
for (unsigned k = N; k-- > i; base1 -= k)
tmp += base1[i] * base1[j];
dst(i, j) = tmp;
}
}
delete [] l;
}
};
/// struct to obtain the inverse from a Cholesky decomposition
template<class F, unsigned N, class M> struct _inverter
{
/// method to do the inversion
void operator()(M& dst, const F* src) const
{ return _inverterGenDim<F, M>()(dst, src, N); }
};
/// struct to solve a linear system using its Cholesky decomposition (generalised dimensionality)
template<class F, class V> struct _solverGenDim
{
/// method to solve the linear system
void operator()(V& rhs, const F* l, unsigned N) const
{
// solve Ly = rhs
for (unsigned k = 0; k < N; ++k) {
const unsigned base = (k * (k + 1)) / 2;
F sum = F(0.0);
for (unsigned i = k; i--; )
sum += rhs[i] * l[base + i];
// elements on diagonale are pre-inverted!
rhs[k] = (rhs[k] - sum) * l[base + k];
}
// solve L^Tx = y
for (unsigned k = N; k--; ) {
F sum = F(0.0);
for (unsigned i = N; --i > k; )
sum += rhs[i] * l[(i * (i + 1)) / 2 + k];
// elements on diagonale are pre-inverted!
rhs[k] = (rhs[k] - sum) * l[(k * (k + 1)) / 2 + k];
}
}
};
/// struct to solve a linear system using its Cholesky decomposition
template<class F, unsigned N, class V> struct _solver
{
/// method to solve the linear system
void operator()(V& rhs, const F* l) const
{ _solverGenDim<F, V>()(rhs, l, N); }
};
/// struct to do a Cholesky decomposition (specialized, N = 6)
template<class F, class M> struct _decomposer<F, 6, M>
{
/// method to do the decomposition
bool operator()(F* dst, const M& src) const
{
if (src(0,0) <= F(0.0)) return false;
dst[0] = std::sqrt(F(1.0) / src(0,0));
dst[1] = src(1,0) * dst[0];
dst[2] = src(1,1) - dst[1] * dst[1];
if (dst[2] <= F(0.0)) return false;
else dst[2] = std::sqrt(F(1.0) / dst[2]);
dst[3] = src(2,0) * dst[0];
dst[4] = (src(2,1) - dst[1] * dst[3]) * dst[2];
dst[5] = src(2,2) - (dst[3] * dst[3] + dst[4] * dst[4]);
if (dst[5] <= F(0.0)) return false;
else dst[5] = std::sqrt(F(1.0) / dst[5]);
dst[6] = src(3,0) * dst[0];
dst[7] = (src(3,1) - dst[1] * dst[6]) * dst[2];
dst[8] = (src(3,2) - dst[3] * dst[6] - dst[4] * dst[7]) * dst[5];
dst[9] = src(3,3) - (dst[6] * dst[6] + dst[7] * dst[7] + dst[8] * dst[8]);
if (dst[9] <= F(0.0)) return false;
else dst[9] = std::sqrt(F(1.0) / dst[9]);
dst[10] = src(4,0) * dst[0];
dst[11] = (src(4,1) - dst[1] * dst[10]) * dst[2];
dst[12] = (src(4,2) - dst[3] * dst[10] - dst[4] * dst[11]) * dst[5];
dst[13] = (src(4,3) - dst[6] * dst[10] - dst[7] * dst[11] - dst[8] * dst[12]) * dst[9];
dst[14] = src(4,4) - (dst[10]*dst[10]+dst[11]*dst[11]+dst[12]*dst[12]+dst[13]*dst[13]);
if (dst[14] <= F(0.0)) return false;
else dst[14] = std::sqrt(F(1.0) / dst[14]);
dst[15] = src(5,0) * dst[0];
dst[16] = (src(5,1) - dst[1] * dst[15]) * dst[2];
dst[17] = (src(5,2) - dst[3] * dst[15] - dst[4] * dst[16]) * dst[5];
dst[18] = (src(5,3) - dst[6] * dst[15] - dst[7] * dst[16] - dst[8] * dst[17]) * dst[9];
dst[19] = (src(5,4) - dst[10] * dst[15] - dst[11] * dst[16] - dst[12] * dst[17] - dst[13] * dst[18]) * dst[14];
dst[20] = src(5,5) - (dst[15]*dst[15]+dst[16]*dst[16]+dst[17]*dst[17]+dst[18]*dst[18]+dst[19]*dst[19]);
if (dst[20] <= F(0.0)) return false;
else dst[20] = std::sqrt(F(1.0) / dst[20]);
return true;
}
};
/// struct to do a Cholesky decomposition (specialized, N = 5)
template<class F, class M> struct _decomposer<F, 5, M>
{
/// method to do the decomposition
bool operator()(F* dst, const M& src) const
{
if (src(0,0) <= F(0.0)) return false;
dst[0] = std::sqrt(F(1.0) / src(0,0));
dst[1] = src(1,0) * dst[0];
dst[2] = src(1,1) - dst[1] * dst[1];
if (dst[2] <= F(0.0)) return false;
else dst[2] = std::sqrt(F(1.0) / dst[2]);
dst[3] = src(2,0) * dst[0];
dst[4] = (src(2,1) - dst[1] * dst[3]) * dst[2];
dst[5] = src(2,2) - (dst[3] * dst[3] + dst[4] * dst[4]);
if (dst[5] <= F(0.0)) return false;
else dst[5] = std::sqrt(F(1.0) / dst[5]);
dst[6] = src(3,0) * dst[0];
dst[7] = (src(3,1) - dst[1] * dst[6]) * dst[2];
dst[8] = (src(3,2) - dst[3] * dst[6] - dst[4] * dst[7]) * dst[5];
dst[9] = src(3,3) - (dst[6] * dst[6] + dst[7] * dst[7] + dst[8] * dst[8]);
if (dst[9] <= F(0.0)) return false;
else dst[9] = std::sqrt(F(1.0) / dst[9]);
dst[10] = src(4,0) * dst[0];
dst[11] = (src(4,1) - dst[1] * dst[10]) * dst[2];
dst[12] = (src(4,2) - dst[3] * dst[10] - dst[4] * dst[11]) * dst[5];
dst[13] = (src(4,3) - dst[6] * dst[10] - dst[7] * dst[11] - dst[8] * dst[12]) * dst[9];
dst[14] = src(4,4) - (dst[10]*dst[10]+dst[11]*dst[11]+dst[12]*dst[12]+dst[13]*dst[13]);
if (dst[14] <= F(0.0)) return false;
else dst[14] = std::sqrt(F(1.0) / dst[14]);
return true;
}
};
/// struct to do a Cholesky decomposition (specialized, N = 4)
template<class F, class M> struct _decomposer<F, 4, M>
{
/// method to do the decomposition
bool operator()(F* dst, const M& src) const
{
if (src(0,0) <= F(0.0)) return false;
dst[0] = std::sqrt(F(1.0) / src(0,0));
dst[1] = src(1,0) * dst[0];
dst[2] = src(1,1) - dst[1] * dst[1];
if (dst[2] <= F(0.0)) return false;
else dst[2] = std::sqrt(F(1.0) / dst[2]);
dst[3] = src(2,0) * dst[0];
dst[4] = (src(2,1) - dst[1] * dst[3]) * dst[2];
dst[5] = src(2,2) - (dst[3] * dst[3] + dst[4] * dst[4]);
if (dst[5] <= F(0.0)) return false;
else dst[5] = std::sqrt(F(1.0) / dst[5]);
dst[6] = src(3,0) * dst[0];
dst[7] = (src(3,1) - dst[1] * dst[6]) * dst[2];
dst[8] = (src(3,2) - dst[3] * dst[6] - dst[4] * dst[7]) * dst[5];
dst[9] = src(3,3) - (dst[6] * dst[6] + dst[7] * dst[7] + dst[8] * dst[8]);
if (dst[9] <= F(0.0)) return false;
else dst[9] = std::sqrt(F(1.0) / dst[9]);
return true;
}
};
/// struct to do a Cholesky decomposition (specialized, N = 3)
template<class F, class M> struct _decomposer<F, 3, M>
{
/// method to do the decomposition
bool operator()(F* dst, const M& src) const
{
if (src(0,0) <= F(0.0)) return false;
dst[0] = std::sqrt(F(1.0) / src(0,0));
dst[1] = src(1,0) * dst[0];
dst[2] = src(1,1) - dst[1] * dst[1];
if (dst[2] <= F(0.0)) return false;
else dst[2] = std::sqrt(F(1.0) / dst[2]);
dst[3] = src(2,0) * dst[0];
dst[4] = (src(2,1) - dst[1] * dst[3]) * dst[2];
dst[5] = src(2,2) - (dst[3] * dst[3] + dst[4] * dst[4]);
if (dst[5] <= F(0.0)) return false;
else dst[5] = std::sqrt(F(1.0) / dst[5]);
return true;
}
};
/// struct to do a Cholesky decomposition (specialized, N = 2)
template<class F, class M> struct _decomposer<F, 2, M>
{
/// method to do the decomposition
bool operator()(F* dst, const M& src) const
{
if (src(0,0) <= F(0.0)) return false;
dst[0] = std::sqrt(F(1.0) / src(0,0));
dst[1] = src(1,0) * dst[0];
dst[2] = src(1,1) - dst[1] * dst[1];
if (dst[2] <= F(0.0)) return false;
else dst[2] = std::sqrt(F(1.0) / dst[2]);
return true;
}
};
/// struct to do a Cholesky decomposition (specialized, N = 1)
template<class F, class M> struct _decomposer<F, 1, M>
{
/// method to do the decomposition
bool operator()(F* dst, const M& src) const
{
if (src(0,0) <= F(0.0)) return false;
dst[0] = std::sqrt(F(1.0) / src(0,0));
return true;
}
};
/// struct to do a Cholesky decomposition (specialized, N = 0)
template<class F, class M> struct _decomposer<F, 0, M>
{
private:
_decomposer() { };
bool operator()(F* dst, const M& src) const;
};
/// struct to obtain the inverse from a Cholesky decomposition (N = 6)
template<class F, class M> struct _inverter<F,6,M>
{
/// method to do the inversion
inline void operator()(M& dst, const F* src) const
{
const F li21 = -src[1] * src[0] * src[2];
const F li32 = -src[4] * src[2] * src[5];
const F li31 = (src[1] * src[4] * src[2] - src[3]) * src[0] * src[5];
const F li43 = -src[8] * src[9] * src[5];
const F li42 = (src[4] * src[8] * src[5] - src[7]) * src[2] * src[9];
const F li41 = (-src[1] * src[4] * src[8] * src[2] * src[5] +
src[1] * src[7] * src[2] + src[3] * src[8] * src[5] - src[6]) * src[0] * src[9];
const F li54 = -src[13] * src[14] * src[9];
const F li53 = (src[13] * src[8] * src[9] - src[12]) * src[5] * src[14];
const F li52 = (-src[4] * src[8] * src[13] * src[5] * src[9] +
src[4] * src[12] * src[5] + src[7] * src[13] * src[9] - src[11]) * src[2] * src[14];
const F li51 = (src[1]*src[4]*src[8]*src[13]*src[2]*src[5]*src[9] -
src[13]*src[8]*src[3]*src[9]*src[5] - src[12]*src[4]*src[1]*src[2]*src[5] - src[13]*src[7]*src[1]*src[9]*src[2] +
src[11]*src[1]*src[2] + src[12]*src[3]*src[5] + src[13]*src[6]*src[9] -src[10]) * src[0] * src[14];
const F li65 = -src[19] * src[20] * src[14];
const F li64 = (src[19] * src[13] * src[14] - src[18]) * src[9] * src[20];
const F li63 = (-src[8] * src[13] * src[19] * src[9] * src[14] +
src[8] * src[18] * src[9] + src[12] * src[19] * src[14] - src[17]) * src[5] * src[20];
const F li62 = (src[4]*src[8]*src[13]*src[19]*src[5]*src[9]*src[14] -
src[18]*src[8]*src[4]*src[9]*src[5] - src[19]*src[12]*src[4]*src[14]*src[5] -src[19]*src[13]*src[7]*src[14]*src[9] +
src[17]*src[4]*src[5] + src[18]*src[7]*src[9] + src[19]*src[11]*src[14] - src[16]) * src[2] * src[20];
const F li61 = (-src[19]*src[13]*src[8]*src[4]*src[1]*src[2]*src[5]*src[9]*src[14] +
src[18]*src[8]*src[4]*src[1]*src[2]*src[5]*src[9] + src[19]*src[12]*src[4]*src[1]*src[2]*src[5]*src[14] +
src[19]*src[13]*src[7]*src[1]*src[2]*src[9]*src[14] + src[19]*src[13]*src[8]*src[3]*src[5]*src[9]*src[14] -
src[17]*src[4]*src[1]*src[2]*src[5] - src[18]*src[7]*src[1]*src[2]*src[9] - src[19]*src[11]*src[1]*src[2]*src[14] -
src[18]*src[8]*src[3]*src[5]*src[9] - src[19]*src[12]*src[3]*src[5]*src[14] - src[19]*src[13]*src[6]*src[9]*src[14] +
src[16]*src[1]*src[2] + src[17]*src[3]*src[5] + src[18]*src[6]*src[9] + src[19]*src[10]*src[14] - src[15]) *
src[0] * src[20];
dst(0,0) = li61*li61 + li51*li51 + li41*li41 + li31*li31 + li21*li21 + src[0]*src[0];
dst(1,0) = li61*li62 + li51*li52 + li41*li42 + li31*li32 + li21*src[2];
dst(1,1) = li62*li62 + li52*li52 + li42*li42 + li32*li32 + src[2]*src[2];
dst(2,0) = li61*li63 + li51*li53 + li41*li43 + li31*src[5];
dst(2,1) = li62*li63 + li52*li53 + li42*li43 + li32*src[5];
dst(2,2) = li63*li63 + li53*li53 + li43*li43 + src[5]*src[5];
dst(3,0) = li61*li64 + li51*li54 + li41*src[9];
dst(3,1) = li62*li64 + li52*li54 + li42*src[9];
dst(3,2) = li63*li64 + li53*li54 + li43*src[9];
dst(3,3) = li64*li64 + li54*li54 + src[9]*src[9];
dst(4,0) = li61*li65 + li51*src[14];
dst(4,1) = li62*li65 + li52*src[14];
dst(4,2) = li63*li65 + li53*src[14];
dst(4,3) = li64*li65 + li54*src[14];
dst(4,4) = li65*li65 + src[14]*src[14];
dst(5,0) = li61*src[20];
dst(5,1) = li62*src[20];
dst(5,2) = li63*src[20];
dst(5,3) = li64*src[20];
dst(5,4) = li65*src[20];
dst(5,5) = src[20]*src[20];
}
};
/// struct to obtain the inverse from a Cholesky decomposition (N = 5)
template<class F, class M> struct _inverter<F,5,M>
{
/// method to do the inversion
inline void operator()(M& dst, const F* src) const
{
const F li21 = -src[1] * src[0] * src[2];
const F li32 = -src[4] * src[2] * src[5];
const F li31 = (src[1] * src[4] * src[2] - src[3]) * src[0] * src[5];
const F li43 = -src[8] * src[9] * src[5];
const F li42 = (src[4] * src[8] * src[5] - src[7]) * src[2] * src[9];
const F li41 = (-src[1] * src[4] * src[8] * src[2] * src[5] +
src[1] * src[7] * src[2] + src[3] * src[8] * src[5] - src[6]) * src[0] * src[9];
const F li54 = -src[13] * src[14] * src[9];
const F li53 = (src[13] * src[8] * src[9] - src[12]) * src[5] * src[14];
const F li52 = (-src[4] * src[8] * src[13] * src[5] * src[9] +
src[4] * src[12] * src[5] + src[7] * src[13] * src[9] - src[11]) * src[2] * src[14];
const F li51 = (src[1]*src[4]*src[8]*src[13]*src[2]*src[5]*src[9] -
src[13]*src[8]*src[3]*src[9]*src[5] - src[12]*src[4]*src[1]*src[2]*src[5] - src[13]*src[7]*src[1]*src[9]*src[2] +
src[11]*src[1]*src[2] + src[12]*src[3]*src[5] + src[13]*src[6]*src[9] -src[10]) * src[0] * src[14];
dst(0,0) = li51*li51 + li41*li41 + li31*li31 + li21*li21 + src[0]*src[0];
dst(1,0) = li51*li52 + li41*li42 + li31*li32 + li21*src[2];
dst(1,1) = li52*li52 + li42*li42 + li32*li32 + src[2]*src[2];
dst(2,0) = li51*li53 + li41*li43 + li31*src[5];
dst(2,1) = li52*li53 + li42*li43 + li32*src[5];
dst(2,2) = li53*li53 + li43*li43 + src[5]*src[5];
dst(3,0) = li51*li54 + li41*src[9];
dst(3,1) = li52*li54 + li42*src[9];
dst(3,2) = li53*li54 + li43*src[9];
dst(3,3) = li54*li54 + src[9]*src[9];
dst(4,0) = li51*src[14];
dst(4,1) = li52*src[14];
dst(4,2) = li53*src[14];
dst(4,3) = li54*src[14];
dst(4,4) = src[14]*src[14];
}
};
/// struct to obtain the inverse from a Cholesky decomposition (N = 4)
template<class F, class M> struct _inverter<F,4,M>
{
/// method to do the inversion
inline void operator()(M& dst, const F* src) const
{
const F li21 = -src[1] * src[0] * src[2];
const F li32 = -src[4] * src[2] * src[5];
const F li31 = (src[1] * src[4] * src[2] - src[3]) * src[0] * src[5];
const F li43 = -src[8] * src[9] * src[5];
const F li42 = (src[4] * src[8] * src[5] - src[7]) * src[2] * src[9];
const F li41 = (-src[1] * src[4] * src[8] * src[2] * src[5] +
src[1] * src[7] * src[2] + src[3] * src[8] * src[5] - src[6]) * src[0] * src[9];
dst(0,0) = li41*li41 + li31*li31 + li21*li21 + src[0]*src[0];
dst(1,0) = li41*li42 + li31*li32 + li21*src[2];
dst(1,1) = li42*li42 + li32*li32 + src[2]*src[2];
dst(2,0) = li41*li43 + li31*src[5];
dst(2,1) = li42*li43 + li32*src[5];
dst(2,2) = li43*li43 + src[5]*src[5];
dst(3,0) = li41*src[9];
dst(3,1) = li42*src[9];
dst(3,2) = li43*src[9];
dst(3,3) = src[9]*src[9];
}
};
/// struct to obtain the inverse from a Cholesky decomposition (N = 3)
template<class F, class M> struct _inverter<F,3,M>
{
/// method to do the inversion
inline void operator()(M& dst, const F* src) const
{
const F li21 = -src[1] * src[0] * src[2];
const F li32 = -src[4] * src[2] * src[5];
const F li31 = (src[1] * src[4] * src[2] - src[3]) * src[0] * src[5];
dst(0,0) = li31*li31 + li21*li21 + src[0]*src[0];
dst(1,0) = li31*li32 + li21*src[2];
dst(1,1) = li32*li32 + src[2]*src[2];
dst(2,0) = li31*src[5];
dst(2,1) = li32*src[5];
dst(2,2) = src[5]*src[5];
}
};
/// struct to obtain the inverse from a Cholesky decomposition (N = 2)
template<class F, class M> struct _inverter<F,2,M>
{
/// method to do the inversion
inline void operator()(M& dst, const F* src) const
{
const F li21 = -src[1] * src[0] * src[2];
dst(0,0) = li21*li21 + src[0]*src[0];
dst(1,0) = li21*src[2];
dst(1,1) = src[2]*src[2];
}
};
/// struct to obtain the inverse from a Cholesky decomposition (N = 1)
template<class F, class M> struct _inverter<F,1,M>
{
/// method to do the inversion
inline void operator()(M& dst, const F* src) const
{
dst(0,0) = src[0]*src[0];
}
};
/// struct to obtain the inverse from a Cholesky decomposition (N = 0)
template<class F, class M> struct _inverter<F,0,M>
{
private:
_inverter();
void operator()(M& dst, const F* src) const;
};
/// struct to solve a linear system using its Cholesky decomposition (N=6)
template<class F, class V> struct _solver<F,6,V>
{
/// method to solve the linear system
void operator()(V& rhs, const F* l) const
{
// solve Ly = rhs
const F y0 = rhs[0] * l[0];
const F y1 = (rhs[1]-l[1]*y0)*l[2];
const F y2 = (rhs[2]-(l[3]*y0+l[4]*y1))*l[5];
const F y3 = (rhs[3]-(l[6]*y0+l[7]*y1+l[8]*y2))*l[9];
const F y4 = (rhs[4]-(l[10]*y0+l[11]*y1+l[12]*y2+l[13]*y3))*l[14];
const F y5 = (rhs[5]-(l[15]*y0+l[16]*y1+l[17]*y2+l[18]*y3+l[19]*y4))*l[20];
// solve L^Tx = y, and put x into rhs
rhs[5] = y5 * l[20];
rhs[4] = (y4-l[19]*rhs[5])*l[14];
rhs[3] = (y3-(l[18]*rhs[5]+l[13]*rhs[4]))*l[9];
rhs[2] = (y2-(l[17]*rhs[5]+l[12]*rhs[4]+l[8]*rhs[3]))*l[5];
rhs[1] = (y1-(l[16]*rhs[5]+l[11]*rhs[4]+l[7]*rhs[3]+l[4]*rhs[2]))*l[2];
rhs[0] = (y0-(l[15]*rhs[5]+l[10]*rhs[4]+l[6]*rhs[3]+l[3]*rhs[2]+l[1]*rhs[1]))*l[0];
}
};
/// struct to solve a linear system using its Cholesky decomposition (N=5)
template<class F, class V> struct _solver<F,5,V>
{
/// method to solve the linear system
void operator()(V& rhs, const F* l) const
{
// solve Ly = rhs
const F y0 = rhs[0] * l[0];
const F y1 = (rhs[1]-l[1]*y0)*l[2];
const F y2 = (rhs[2]-(l[3]*y0+l[4]*y1))*l[5];
const F y3 = (rhs[3]-(l[6]*y0+l[7]*y1+l[8]*y2))*l[9];
const F y4 = (rhs[4]-(l[10]*y0+l[11]*y1+l[12]*y2+l[13]*y3))*l[14];
// solve L^Tx = y, and put x into rhs
rhs[4] = (y4)*l[14];
rhs[3] = (y3-(l[13]*rhs[4]))*l[9];
rhs[2] = (y2-(l[12]*rhs[4]+l[8]*rhs[3]))*l[5];
rhs[1] = (y1-(l[11]*rhs[4]+l[7]*rhs[3]+l[4]*rhs[2]))*l[2];
rhs[0] = (y0-(l[10]*rhs[4]+l[6]*rhs[3]+l[3]*rhs[2]+l[1]*rhs[1]))*l[0];
}
};
/// struct to solve a linear system using its Cholesky decomposition (N=4)
template<class F, class V> struct _solver<F,4,V>
{
/// method to solve the linear system
void operator()(V& rhs, const F* l) const
{
// solve Ly = rhs
const F y0 = rhs[0] * l[0];
const F y1 = (rhs[1]-l[1]*y0)*l[2];
const F y2 = (rhs[2]-(l[3]*y0+l[4]*y1))*l[5];
const F y3 = (rhs[3]-(l[6]*y0+l[7]*y1+l[8]*y2))*l[9];
// solve L^Tx = y, and put x into rhs
rhs[3] = (y3)*l[9];
rhs[2] = (y2-(l[8]*rhs[3]))*l[5];
rhs[1] = (y1-(l[7]*rhs[3]+l[4]*rhs[2]))*l[2];
rhs[0] = (y0-(l[6]*rhs[3]+l[3]*rhs[2]+l[1]*rhs[1]))*l[0];
}
};
/// struct to solve a linear system using its Cholesky decomposition (N=3)
template<class F, class V> struct _solver<F,3,V>
{
/// method to solve the linear system
void operator()(V& rhs, const F* l) const
{
// solve Ly = rhs
const F y0 = rhs[0] * l[0];
const F y1 = (rhs[1]-l[1]*y0)*l[2];
const F y2 = (rhs[2]-(l[3]*y0+l[4]*y1))*l[5];
// solve L^Tx = y, and put x into rhs
rhs[2] = (y2)*l[5];
rhs[1] = (y1-(l[4]*rhs[2]))*l[2];
rhs[0] = (y0-(l[3]*rhs[2]+l[1]*rhs[1]))*l[0];
}
};
/// struct to solve a linear system using its Cholesky decomposition (N=2)
template<class F, class V> struct _solver<F,2,V>
{
/// method to solve the linear system
void operator()(V& rhs, const F* l) const
{
// solve Ly = rhs
const F y0 = rhs[0] * l[0];
const F y1 = (rhs[1]-l[1]*y0)*l[2];
// solve L^Tx = y, and put x into rhs
rhs[1] = (y1)*l[2];
rhs[0] = (y0-(l[1]*rhs[1]))*l[0];
}
};
/// struct to solve a linear system using its Cholesky decomposition (N=1)
template<class F, class V> struct _solver<F,1,V>
{
/// method to solve the linear system
void operator()(V& rhs, const F* l) const
{
// solve LL^T x = rhs, put y into rhs
rhs[0] *= l[0] * l[0];
}
};
/// struct to solve a linear system using its Cholesky decomposition (N=0)
template<class F, class V> struct _solver<F,0,V>
{
private:
_solver();
void operator()(V& rhs, const F* l) const;
};
}
} // namespace Math
} // namespace ROOT
#endif // ROOT_Math_CHOLESKYDECOMP
|