| 509 | |
| 510 | |
| 511 | bool UdUinverse (RowMatrix& UD) |
| 512 | /* In-place (destructive) inversion of diagonal and unit upper triangular matrices in UD |
| 513 | * BE VERY CAREFUL THIS IS NOT THE INVERSE OF UD |
| 514 | * Inversion on d and U is separate: inv(U)*inv(d)*inv(U') = inv(U'dU) NOT EQUAL inv(UdU') |
| 515 | * Lower triangle of UD is ignored and unmodified |
| 516 | * Only diagonal part d can be singular (zero elements), inverse is computed of all elements other then singular |
| 517 | * Reference: A+G p.223 |
| 518 | * |
| 519 | * Output: |
| 520 | * UD: inv(U), inv(d) |
| 521 | * Return: |
| 522 | * singularity (of d), true iff d has a zero element |
| 523 | */ |
| 524 | { |
| 525 | std::size_t i,j,k; |
| 526 | const std::size_t n = UD.size1(); |
| 527 | assert (n == UD.size2()); |
| 528 | |
| 529 | // Invert U in place |
| 530 | if (n > 1) |
| 531 | { |
| 532 | i = n-2; |
| 533 | do { |
| 534 | RowMatrix::Row UDi(UD,i); |
| 535 | for (j = n-1; j > i; --j) |
| 536 | { |
| 537 | RowMatrix::value_type UDij = - UDi[j]; |
| 538 | for (k = i+1; k < j; ++k) |
| 539 | UDij -= UDi[k] * UD(k,j); |
| 540 | UDi[j] = UDij; |
| 541 | } |
| 542 | } while (i-- > 0); |
| 543 | } |
| 544 | |
| 545 | // Invert d in place |
| 546 | bool singular = false; |
| 547 | for (i = 0; i < n; ++i) |
| 548 | { |
| 549 | // Detect singular element |
| 550 | if (UD(i,i) != 0) |
| 551 | UD(i,i) = Float(1) / UD(i,i); |
| 552 | else |
| 553 | singular = true; |
| 554 | } |
| 555 | |
| 556 | return singular; |
| 557 | } |
| 558 | |
| 559 | |
| 560 | bool UTinverse (UTriMatrix& U) |
no outgoing calls
no test coverage detected