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
use matrix::{Matrix, BaseMatrix, BaseMatrixMut};
use matrix::{back_substitution};
use matrix::PermutationMatrix;
use vector::Vector;
use error::{Error, ErrorKind};
use std::any::Any;
use std::cmp;
use libnum::{Float, Zero, One};
use matrix::decomposition::Decomposition;
/// Result of unpacking an instance of
/// [PartialPivLu](struct.PartialPivLu.html).
#[derive(Debug, Clone)]
pub struct LUP<T> {
/// The lower triangular matrix in the decomposition.
pub l: Matrix<T>,
/// The upper triangular matrix in the decomposition.
pub u: Matrix<T>,
/// The permutation matrix in the decomposition.
pub p: PermutationMatrix<T>
}
/// LU decomposition with partial pivoting.
///
/// For any square matrix A, there exist a permutation matrix
/// `P`, a lower triangular matrix `L` and an upper triangular
/// matrix `U` such that
///
/// ```text
/// PA = LU.
/// ```
///
/// However, due to the way partial pivoting algorithms work,
/// LU decomposition with partial pivoting is in general
/// *only numerically stable for well-conditioned invertible matrices*.
///
/// That said, partial pivoting is sufficient in the vast majority
/// of practical applications, and it is also the fastest of the
/// pivoting schemes in existence.
///
///
/// # Applications
///
/// Given a matrix `x`, computing the LU(P) decomposition is simple:
///
/// ```
/// use rulinalg::matrix::decomposition::{PartialPivLu, LUP, Decomposition};
/// use rulinalg::matrix::Matrix;
///
/// let x = Matrix::<f64>::identity(4);
///
/// // The matrix is consumed and its memory
/// // re-purposed for the decomposition
/// let lu = PartialPivLu::decompose(x).expect("Matrix is invertible.");
///
/// // See below for applications
/// // ...
///
/// // The factors L, U and P can be obtained by unpacking the
/// // decomposition, for example by destructuring as seen here
/// let LUP { l, u, p } = lu.unpack();
///
/// ```
///
/// ## Solving linear systems
///
/// Arguably the most common use case of LU decomposition
/// is the computation of solutions to (multiple) linear systems
/// that share the same coefficient matrix.
///
/// ```
/// # #[macro_use] extern crate rulinalg;
/// # use rulinalg::matrix::decomposition::PartialPivLu;
/// # use rulinalg::matrix::Matrix;
/// # fn main() {
/// # let x = Matrix::identity(4);
/// # let lu = PartialPivLu::decompose(x).unwrap();
/// let b = vector![3.0, 4.0, 2.0, 1.0];
/// let y = lu.solve(b)
/// .expect("Matrix is invertible.");
/// assert_vector_eq!(y, vector![3.0, 4.0, 2.0, 1.0], comp = float);
///
/// // We can efficiently solve multiple such systems
/// let c = vector![0.0, 0.0, 0.0, 0.0];
/// let z = lu.solve(c).unwrap();
/// assert_vector_eq!(z, vector![0.0, 0.0, 0.0, 0.0], comp = float);
/// # }
/// ```
///
/// ## Computing the inverse of a matrix
///
/// The LU decomposition provides a convenient way to obtain
/// the inverse of the decomposed matrix. However, please keep
/// in mind that explicitly computing the inverse of a matrix
/// is *usually* a bad idea. In many cases, one might instead simply
/// solve multiple systems using `solve`.
///
/// For example, a common misconception is that when one needs
/// to solve multiple linear systems `Ax = b` for different `b`,
/// one should pre-compute the inverse of the matrix for efficiency.
/// In fact, this is practically never a good idea! A far more efficient
/// and accurate method is to perform the LU decomposition once, and
/// then solve each system as shown in the examples of the previous
/// subsection.
///
/// That said, there are definitely cases where an explicit inverse is
/// needed. In these cases, the inverse can easily be obtained
/// through the `inverse()` method.
///
/// # Computing the determinant of a matrix
///
/// Once the LU decomposition has been obtained, computing
/// the determinant of the decomposed matrix is a very cheap
/// operation.
///
/// ```
/// # #[macro_use] extern crate rulinalg;
/// # use rulinalg::matrix::decomposition::PartialPivLu;
/// # use rulinalg::matrix::Matrix;
/// # fn main() {
/// # let x = Matrix::<f64>::identity(4);
/// # let lu = PartialPivLu::decompose(x).unwrap();
/// assert_eq!(lu.det(), 1.0);
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct PartialPivLu<T> {
lu: Matrix<T>,
p: PermutationMatrix<T>
}
impl<T: Clone + One + Zero> Decomposition for PartialPivLu<T> {
type Factors = LUP<T>;
fn unpack(self) -> LUP<T> {
use internal_utils::nullify_lower_triangular_part;
let l = unit_lower_triangular_part(&self.lu);
let mut u = self.lu;
nullify_lower_triangular_part(&mut u);
LUP {
l: l,
u: u,
p: self.p
}
}
}
impl<T: 'static + Float> PartialPivLu<T> {
/// Performs the decomposition.
///
/// # Panics
///
/// The matrix must be square.
///
/// # Errors
///
/// An error will be returned if the matrix
/// is singular to working precision (badly conditioned).
pub fn decompose(matrix: Matrix<T>) -> Result<Self, Error> {
let n = matrix.cols;
assert!(matrix.rows == n, "Matrix must be square for LU decomposition.");
let mut lu = matrix;
let mut p = PermutationMatrix::identity(n);
for index in 0..n {
let mut curr_max_idx = index;
let mut curr_max = lu[[curr_max_idx, curr_max_idx]];
for i in (curr_max_idx+1)..n {
if lu[[i, index]].abs() > curr_max.abs() {
curr_max = lu[[i, index]];
curr_max_idx = i;
}
}
if curr_max.abs() < T::epsilon() {
return Err(Error::new(ErrorKind::DivByZero,
"The matrix is too ill-conditioned for
LU decomposition with partial pivoting."));
}
lu.swap_rows(index, curr_max_idx);
p.swap_rows(index, curr_max_idx);
gaussian_elimination(&mut lu, index);
}
Ok(PartialPivLu {
lu: lu,
p: p.inverse()
})
}
}
// TODO: Remove Any bound (cannot for the time being, since
// back substitution uses Any bound)
impl<T> PartialPivLu<T> where T: Any + Float {
/// Solves the linear system `Ax = b`.
///
/// Here, `A` is the decomposed matrix satisfying
/// `PA = LU`. Note that this method is particularly
/// well suited to solving multiple such linear systems
/// involving the same `A` but different `b`.
///
/// # Errors
///
/// If the matrix is very ill-conditioned, the function
/// might fail to obtain the solution to the system.
///
/// # Panics
///
/// The right-hand side vector `b` must have compatible size.
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate rulinalg;
/// # use rulinalg::matrix::decomposition::PartialPivLu;
/// # use rulinalg::matrix::Matrix;
/// # fn main() {
/// let x = Matrix::identity(4);
/// let lu = PartialPivLu::decompose(x).unwrap();
/// let b = vector![3.0, 4.0, 2.0, 1.0];
/// let y = lu.solve(b)
/// .expect("Matrix is invertible.");
/// assert_vector_eq!(y, vector![3.0, 4.0, 2.0, 1.0], comp = float);
/// # }
/// ```
pub fn solve(&self, b: Vector<T>) -> Result<Vector<T>, Error> {
assert!(b.size() == self.lu.rows(),
"Right-hand side vector must have compatible size.");
// Note that applying p here implicitly incurs a clone.
// TODO: Is it possible to avoid the clone somehow?
// To my knowledge, applying a permutation matrix
// in-place in O(n) time requires O(n) storage for bookkeeping.
// However, we might be able to get by with something like
// O(n log n) for the permutation as the forward/backward
// substitution algorithms are O(n^2), if this helps us
// avoid the memory overhead.
let b = lu_forward_substitution(&self.lu, &self.p * b);
back_substitution(&self.lu, b)
}
/// Computes the inverse of the matrix which this LUP decomposition
/// represents.
///
/// # Errors
/// The inversion might fail if the matrix is very ill-conditioned.
pub fn inverse(&self) -> Result<Matrix<T>, Error> {
let n = self.lu.rows();
let mut inv = Matrix::zeros(n, n);
let mut e = Vector::zeros(n);
// To compute the inverse of a matrix A, note that
// we can simply solve the system
// AX = I,
// where X is the inverse of A, and I is the identity
// matrix of appropriate dimension.
//
// Note that this is not optimal in terms of performance,
// and there is likely significant potential for improvement.
//
// A more performant technique is usually to compute the
// triangular inverse of each of the L and U triangular matrices,
// but this again requires efficient algorithms (blocked/recursive)
// to invert triangular matrices, which at this point
// we do not have available.
// Solve for each column of the inverse matrix
for i in 0 .. n {
e[i] = T::one();
let col = try!(self.solve(e));
for j in 0 .. n {
inv[[j, i]] = col[j];
}
e = col.apply(&|_| T::zero());
}
Ok(inv)
}
/// Computes the determinant of the decomposed matrix.
///
/// Note that the determinant of an empty matrix is considered
/// to be equal to 1.
pub fn det(&self) -> T {
// Recall that the determinant of a triangular matrix
// is the product of its diagonal entries. Also,
// the determinant of L is implicitly 1.
let u_det = self.lu.diag().fold(T::one(), |x, &y| x * y);
// Note that the determinant of P is equal to the
// determinant of P^T, so we don't have to invert it
let p_det = self.p.clone().det();
p_det * u_det
}
}
/// Result of unpacking an instance of
/// [FullPivLu](struct.FullPivLu.html).
///
/// PAQ = LU
#[derive(Debug, Clone)]
pub struct LUPQ<T> {
/// The lower triangular matrix in the decomposition.
pub l: Matrix<T>,
/// The upper triangular matrix in the decomposition.
pub u: Matrix<T>,
/// The row-exchange permutation matrix in the decomposition.
pub p: PermutationMatrix<T>,
/// The column-exchange permutation matrix in the decomposition.
pub q: PermutationMatrix<T>
}
/// LU decomposition with complete pivoting.
///
/// For any square matrix A, there exist two permutation matrices
/// `P` and `Q`, a lower triangular matrix `L` and an upper triangular
/// matrix `U` such that
///
/// ```text
/// PAQ = LU.
/// ```
///
/// Unlike the LU decomposition computed with partial pivoting, this
/// decomposition is stable for singular matrices. It is also a rank-
/// revealing decomposition.
///
/// See [PartialPivLu](decomposition/struct.PartialPivLu.html) for
/// applications of LU decompositions in general.
#[derive(Debug, Clone)]
pub struct FullPivLu<T> {
lu: Matrix<T>,
p: PermutationMatrix<T>,
q: PermutationMatrix<T>
}
impl<T: Clone + One + Zero> Decomposition for FullPivLu<T> {
type Factors = LUPQ<T>;
fn unpack(self) -> LUPQ<T> {
use internal_utils::nullify_lower_triangular_part;
let l = unit_lower_triangular_part(&self.lu);
let mut u = self.lu;
nullify_lower_triangular_part(&mut u);
LUPQ {
l: l,
u: u,
p: self.p,
q: self.q,
}
}
}
impl<T: 'static + Float> FullPivLu<T> {
fn select_pivot(mat: &Matrix<T>, index: usize) -> (usize, usize, T) {
let mut piv_row = index;
let mut piv_col = index;
let mut piv_val = mat[[index,index]];
for row in index..mat.rows() {
for col in index..mat.cols() {
let val = mat[[row,col]];
if val.abs() > piv_val.abs() {
piv_val = val;
piv_row = row;
piv_col = col;
}
}
}
(piv_row, piv_col, piv_val)
}
/// Performs the decomposition.
pub fn decompose(matrix: Matrix<T>) -> Result<Self, Error> {
assert!(
matrix.rows() == matrix.cols(),
"Matrix must be square for LU decomposition.");
let mut lu = matrix;
let nrows = lu.rows();
let ncols = lu.cols();
let diag_size = cmp::min(nrows, ncols);
let mut p = PermutationMatrix::identity(nrows);
let mut q = PermutationMatrix::identity(ncols);
for index in 0..diag_size {
// Select the current pivot. This is the largest value in
// the bottom right corner of the matrix, starting at
// (index, index).
let (piv_row, piv_col, piv_val) = FullPivLu::select_pivot(&lu, index);
if piv_val.abs() == T::zero() {
break;
}
lu.swap_rows(index, piv_row);
lu.swap_cols(index, piv_col);
p.swap_rows(index, piv_row);
// This is a little misleading, but even though
// we're calling swap_rows here, since q is applied on the
// right to A (i.e. P * A * Q), the result is a column swap of A.
q.swap_rows(index, piv_col);
// We've swapped the pivot row and column so that the pivot
// ends up in the (index, index) position, so apply gaussian
// elimination to the bottom-right corner.
gaussian_elimination(&mut lu, index);
}
Ok(FullPivLu {
lu: lu,
p: p.inverse(),
q: q.inverse()
})
}
}
// TODO: Remove Any bound (cannot for the time being, since
// back substitution uses Any bound)
impl<T> FullPivLu<T> where T: Any + Float {
/// Solves the linear system `Ax = b`.
///
/// Here, `A` is the decomposed matrix satisfying
/// `PAQ = LU`. Note that this method is particularly
/// well suited to solving multiple such linear systems
/// involving the same `A` but different `b`.
///
/// # Errors
///
/// If the matrix is very ill-conditioned, the function
/// might fail to obtain the solution to the system.
///
/// # Panics
///
/// The right-hand side vector `b` must have compatible size.
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate rulinalg;
/// # use rulinalg::matrix::decomposition::FullPivLu;
/// # use rulinalg::matrix::Matrix;
/// # fn main() {
/// let x = Matrix::identity(4);
/// let lu = FullPivLu::decompose(x).unwrap();
/// let b = vector![3.0, 4.0, 2.0, 1.0];
/// let y = lu.solve(b)
/// .expect("Matrix is invertible.");
/// assert_vector_eq!(y, vector![3.0, 4.0, 2.0, 1.0], comp = float);
/// # }
/// ```
pub fn solve(&self, b: Vector<T>) -> Result<Vector<T>, Error> {
assert!(b.size() == self.lu.rows(),
"Right-hand side vector must have compatible size.");
let b = lu_forward_substitution(&self.lu, &self.p * b);
back_substitution(&self.lu, b).map(|x| &self.q * x)
}
/// Computes the inverse of the matrix which this LUP decomposition
/// represents.
///
/// # Errors
/// The inversion might fail if the matrix is very ill-conditioned.
/// The inversion fails if the matrix is not invertible.
pub fn inverse(&self) -> Result<Matrix<T>, Error> {
let n = self.lu.rows();
let mut inv = Matrix::zeros(n, n);
let mut e = Vector::zeros(n);
if !self.is_invertible() {
return Err(
Error::new(
ErrorKind::DivByZero,
"Non-invertible matrix found while attempting inversion"));
}
for i in 0 .. n {
e[i] = T::one();
let col = try!(self.solve(e));
for j in 0 .. n {
inv[[j, i]] = col[j];
}
e = col.apply(&|_| T::zero());
}
Ok(inv)
}
/// Computes the determinant of the decomposed matrix.
///
/// Empty matrices are considered to have a determinant of 1.0.
///
/// # Panics
/// If the underlying matrix is non-square.
pub fn det(&self) -> T {
// Recall that the determinant of a triangular matrix
// is the product of its diagonal entries. Also,
// the determinant of L is implicitly 1.
let u_det = self.lu.diag().fold(T::one(), |x, &y| x * y);
// Note that the determinants of P and Q are equal to the
// determinant of P^T and Q^T, so we don't have to invert them
let p_det = self.p.clone().det();
let q_det = self.q.clone().det();
p_det * u_det * q_det
}
/// Computes the rank of the decomposed matrix.
/// # Examples
///
/// ```
/// # #[macro_use] extern crate rulinalg;
/// # use rulinalg::matrix::decomposition::FullPivLu;
/// # use rulinalg::matrix::Matrix;
/// # fn main() {
/// let x = matrix![1.0, 2.0, 3.0;
/// 4.0, 5.0, 6.0;
/// 5.0, 7.0, 9.0];
/// let lu = FullPivLu::decompose(x).unwrap();
/// assert_eq!(lu.rank(), 2);
/// # }
/// ```
pub fn rank(&self) -> usize {
let eps = self.epsilon();
let mut rank = 0;
for d in self.lu.diag() {
if d.abs() > eps {
rank = rank + 1;
} else {
break;
}
}
rank
}
/// Returns whether the matrix is invertible.
///
/// Empty matrices are considered to be invertible for
/// the sake of this function.
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate rulinalg;
/// # use rulinalg::matrix::decomposition::FullPivLu;
/// # use rulinalg::matrix::Matrix;
/// # fn main() {
/// let x = Matrix::<f64>::identity(4);
/// let lu = FullPivLu::decompose(x).unwrap();
/// assert!(lu.is_invertible());
///
/// let y = matrix![1.0, 2.0, 3.0;
/// 4.0, 5.0, 6.0;
/// 5.0, 7.0, 9.0];
/// let lu = FullPivLu::decompose(y).unwrap();
/// assert!(!lu.is_invertible());
/// # }
/// ```
pub fn is_invertible(&self) -> bool {
let diag_size = cmp::min(self.lu.rows(), self.lu.cols());
if diag_size > 0 {
let diag_last = diag_size - 1;
let last =
unsafe { self.lu.get_unchecked([diag_last, diag_last]) };
last.abs() > self.epsilon()
} else {
true
}
}
fn epsilon(&self) -> T {
self.lu.get([0, 0]).unwrap_or(&T::one()).abs() * T::epsilon()
}
}
/// Performs Gaussian elimination in the lower-right hand corner starting at
/// (index, index).
fn gaussian_elimination<T: Float>(lu: &mut Matrix<T>, index: usize) {
let piv_val = lu[[index, index]];
for i in (index+1)..lu.rows() {
let mult = lu[[i, index]] / piv_val;
lu[[i, index]] = mult;
for j in (index+1)..lu.cols() {
lu[[i, j]] = lu[[i,j]] - mult*lu[[index, j]];
}
}
}
/// Performs forward substitution using the LU matrix
/// for which L has an implicit unit diagonal. That is,
/// the strictly lower triangular part of LU corresponds
/// to the strictly lower triangular part of L.
///
/// This is equivalent to solving the system Lx = b.
fn lu_forward_substitution<T: Float>(lu: &Matrix<T>, b: Vector<T>) -> Vector<T> {
assert!(lu.rows() == lu.cols(), "LU matrix must be square.");
assert!(b.size() == lu.rows(), "LU matrix and RHS vector must be compatible.");
let mut x = b;
for (i, row) in lu.row_iter().enumerate().skip(1) {
// Note that at time of writing we need raw_slice here for
// auto-vectorization to kick in
let adjustment = row.raw_slice()
.iter()
.take(i)
.cloned()
.zip(x.iter().cloned())
.fold(T::zero(), |sum, (l, x)| sum + l * x);
x[i] = x[i] - adjustment;
}
x
}
fn unit_lower_triangular_part<T, M>(matrix: &M) -> Matrix<T>
where T: Zero + One + Clone, M: BaseMatrix<T> {
let m = matrix.rows();
let mut data = Vec::<T>::with_capacity(m * m);
for (i, row) in matrix.row_iter().enumerate() {
for element in row.iter().take(i).cloned() {
data.push(element);
}
data.push(T::one());
for _ in (i + 1) .. m {
data.push(T::zero());
}
}
Matrix::new(m, m, data)
}
impl<T> Matrix<T> where T: Any + Float
{
/// Computes L, U, and P for LUP decomposition.
///
/// Returns L,U, and P respectively.
///
/// This function is deprecated.
/// Please see [PartialPivLu](decomposition/struct.PartialPivLu.html)
/// for a replacement.
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate rulinalg; fn main() {
/// use rulinalg::matrix::Matrix;
///
/// let a = matrix![1.0, 2.0, 0.0;
/// 0.0, 3.0, 4.0;
/// 5.0, 1.0, 2.0];
///
/// let (l, u, p) = a.lup_decomp().expect("This matrix should decompose!");
/// # }
/// ```
///
/// # Panics
///
/// - Matrix is not square.
///
/// # Failures
///
/// - Matrix cannot be LUP decomposed.
#[deprecated]
pub fn lup_decomp(self) -> Result<(Matrix<T>, Matrix<T>, Matrix<T>), Error> {
let n = self.cols;
assert!(self.rows == n, "Matrix must be square for LUP decomposition.");
let mut l = Matrix::<T>::zeros(n, n);
let mut u = self;
let mut p = Matrix::<T>::identity(n);
for index in 0..n {
let mut curr_max_idx = index;
let mut curr_max = u[[curr_max_idx, curr_max_idx]];
for i in (curr_max_idx+1)..n {
if u[[i, index]].abs() > curr_max.abs() {
curr_max = u[[i, index]];
curr_max_idx = i;
}
}
if curr_max.abs() < T::epsilon() {
return Err(Error::new(ErrorKind::DivByZero,
"Singular matrix found in LUP decomposition. \
A value in the diagonal of U == 0.0."));
}
if curr_max_idx != index {
l.swap_rows(index, curr_max_idx);
u.swap_rows(index, curr_max_idx);
p.swap_rows(index, curr_max_idx);
}
l[[index, index]] = T::one();
for i in (index+1)..n {
let mult = u[[i, index]]/curr_max;
l[[i, index]] = mult;
u[[i, index]] = T::zero();
for j in (index+1)..n {
u[[i, j]] = u[[i,j]] - mult*u[[index, j]];
}
}
}
Ok((l, u, p))
}
}
#[cfg(test)]
mod tests {
use matrix::{Matrix, PermutationMatrix};
use testsupport::{is_lower_triangular, is_upper_triangular};
use super::{PartialPivLu, LUP, FullPivLu, LUPQ};
use matrix::decomposition::Decomposition;
#[allow(deprecated)]
#[test]
#[should_panic]
fn test_non_square_lup_decomp() {
let a: Matrix<f64> = Matrix::ones(2, 3);
let _ = a.lup_decomp();
}
#[allow(deprecated)]
#[test]
fn test_lup_decomp() {
use error::ErrorKind;
let a: Matrix<f64> = matrix![
1., 2., 3., 4.;
0., 0., 0., 0.;
0., 0., 0., 0.;
0., 0., 0., 0.
];
match a.lup_decomp() {
Err(e) => assert!(*e.kind() == ErrorKind::DivByZero),
Ok(_) => panic!()
}
}
#[test]
fn partial_piv_lu_decompose_arbitrary() {
// Since the LUP decomposition is not in general unique,
// we can not test against factors directly, but
// instead we must rely on the fact that the
// matrices P, L and U together construct the
// original matrix
let x = matrix![ -3.0, 0.0, 4.0, 1.0;
-12.0, 5.0, 17.0, 1.0;
15.0, 0.0, -18.0, -5.0;
6.0, 20.0, -10.0, -15.0 ];
let LUP { l, u, p } = PartialPivLu::decompose(x.clone())
.unwrap()
.unpack();
let y = p.inverse() * &l * &u;
assert_matrix_eq!(x, y, comp = float);
assert!(is_lower_triangular(&l));
assert!(is_upper_triangular(&u));
}
#[test]
pub fn partial_piv_lu_inverse_identity() {
let lu = PartialPivLu::<f64> {
lu: Matrix::identity(3),
p: PermutationMatrix::identity(3)
};
let inv = lu.inverse().expect("Matrix is invertible.");
assert_matrix_eq!(inv, Matrix::identity(3), comp = float);
}
#[test]
pub fn partial_piv_lu_inverse_arbitrary_invertible_matrix() {
let x = matrix![5.0, 0.0, 0.0, 1.0;
2.0, 2.0, 2.0, 1.0;
4.0, 5.0, 5.0, 5.0;
1.0, 6.0, 4.0, 5.0];
let inv = matrix![1.85185185185185203e-01, 1.85185185185185175e-01, -7.40740740740740561e-02, -1.02798428206033007e-17;
1.66666666666666630e-01, 6.66666666666666519e-01, -6.66666666666666519e-01, 4.99999999999999833e-01;
-3.88888888888888840e-01, 1.11111111111111174e-01, 5.55555555555555358e-01, -4.99999999999999833e-01;
7.40740740740740838e-02, -9.25925925925925819e-01, 3.70370370370370294e-01, 5.13992141030165006e-17];
let lu = PartialPivLu::decompose(x).unwrap();
assert_matrix_eq!(lu.inverse().unwrap(), inv, comp = float);
}
#[test]
pub fn partial_piv_lu_det_identity() {
let lu = PartialPivLu::<f64> {
lu: Matrix::identity(3),
p: PermutationMatrix::identity(3)
};
assert_eq!(lu.det(), 1.0);
}
#[test]
pub fn partial_piv_lu_det_arbitrary_invertible_matrix() {
let x = matrix![ 5.0, 0.0, 0.0, 1.0;
0.0, 2.0, 2.0, 1.0;
15.0, 4.0, 7.0, 10.0;
5.0, 2.0, 17.0, 32.0];
let lu = PartialPivLu::decompose(x).unwrap();
let expected_det = 149.99999999999997;
assert_scalar_eq!(lu.det(), expected_det, comp = float);
}
#[test]
pub fn partial_piv_lu_solve_arbitrary_matrix() {
let x = matrix![ 5.0, 0.0, 0.0, 1.0;
2.0, 2.0, 2.0, 1.0;
4.0, 5.0, 5.0, 5.0;
1.0, 6.0, 4.0, 5.0 ];
let b = vector![9.0, 16.0, 49.0, 45.0];
let expected = vector![1.0, 2.0, 3.0, 4.0];
let lu = PartialPivLu::decompose(x).unwrap();
let y = lu.solve(b).unwrap();
// Need to up the tolerance to take into account
// numerical error. Ideally there'd be a more systematic
// way to test this.
assert_vector_eq!(y, expected, comp = ulp, tol = 100);
}
#[test]
pub fn lu_forward_substitution() {
use super::lu_forward_substitution;
{
let lu: Matrix<f64> = matrix![];
let b = vector![];
let x = lu_forward_substitution(&lu, b);
assert!(x.size() == 0);
}
{
let lu = matrix![3.0];
let b = vector![1.0];
let x = lu_forward_substitution(&lu, b);
assert_eq!(x, vector![1.0]);
}
{
let lu = matrix![3.0, 2.0;
2.0, 2.0];
let b = vector![1.0, 2.0];
let x = lu_forward_substitution(&lu, b);
assert_eq!(x, vector![1.0, 0.0]);
}
}
#[test]
fn full_piv_lu_decompose_arbitrary() {
// Since the LUP decomposition is not in general unique,
// we can not test against factors directly, but
// instead we must rely on the fact that the
// matrices P, L and U together construct the
// original matrix
let x = matrix![ -3.0, 0.0, 4.0, 1.0;
-12.0, 5.0, 17.0, 1.0;
15.0, 0.0, -18.0, -5.0;
6.0, 20.0, -10.0, -15.0 ];
let LUPQ { l, u, p, q } = FullPivLu::decompose(x.clone())
.unwrap()
.unpack();
let y = p.inverse() * &l * &u * q.inverse();
assert_matrix_eq!(x, y, comp = float);
assert!(is_lower_triangular(&l));
assert!(is_upper_triangular(&u));
}
#[test]
fn full_piv_lu_decompose_singular() {
let x = matrix![ -3.0, 0.0, 4.0, 1.0;
-12.0, 5.0, 17.0, 1.0;
15.0, 0.0, -18.0, -5.0;
-6.0, 0.0, 8.0, 2.0 ];
let lu = FullPivLu::decompose(x.clone()).unwrap();
assert_eq!(lu.rank(), 3);
let LUPQ { l, u, p, q } = lu.unpack();
let y = p.inverse() * &l * &u * q.inverse();
assert_matrix_eq!(x, y, comp = float);
assert!(is_lower_triangular(&l));
assert!(is_upper_triangular(&u));
}
#[test]
#[should_panic]
fn full_piv_lu_decompose_rectangular() {
let x = matrix![ -3.0, 0.0, 4.0;
-12.0, 5.0, 17.0;
15.0, 0.0, -18.0;
-6.0, 0.0, 20.0];
FullPivLu::decompose(x.clone()).unwrap();
}
#[test]
pub fn full_piv_lu_solve_arbitrary_matrix() {
let x = matrix![ 5.0, 0.0, 0.0, 1.0;
2.0, 2.0, 2.0, 1.0;
4.0, 5.0, 5.0, 5.0;
1.0, 6.0, 4.0, 5.0 ];
let b = vector![9.0, 16.0, 49.0, 45.0];
let expected = vector![1.0, 2.0, 3.0, 4.0];
let lu = FullPivLu::decompose(x).unwrap();
let y = lu.solve(b).unwrap();
// Need to up the tolerance to take into account
// numerical error. Ideally there'd be a more systematic
// way to test this.
assert_vector_eq!(y, expected, comp = ulp, tol = 100);
}
#[test]
pub fn full_piv_lu_inverse_arbitrary_invertible_matrix() {
let x = matrix![5.0, 0.0, 0.0, 1.0;
2.0, 2.0, 2.0, 1.0;
4.0, 5.0, 5.0, 5.0;
1.0, 6.0, 4.0, 5.0];
let inv = matrix![1.85185185185185203e-01, 1.85185185185185175e-01, -7.40740740740740561e-02, -1.02798428206033007e-17;
1.66666666666666630e-01, 6.66666666666666519e-01, -6.66666666666666519e-01, 4.99999999999999833e-01;
-3.88888888888888840e-01, 1.11111111111111174e-01, 5.55555555555555358e-01, -4.99999999999999833e-01;
7.40740740740740838e-02, -9.25925925925925819e-01, 3.70370370370370294e-01, 5.13992141030165006e-17];
let lu = FullPivLu::decompose(x).unwrap();
assert_matrix_eq!(lu.inverse().unwrap(), inv, comp = float);
}
#[test]
pub fn full_piv_lu_inverse_noninvertible() {
let x = matrix![5.0, 0.0, 1.0;
4.0, 5.0, 5.0;
9.0, 5.0, 6.0];
let lu = FullPivLu::decompose(x).unwrap();
assert!(lu.inverse().is_err());
}
#[test]
pub fn full_piv_lu_empty_matrix() {
use matrix::base::BaseMatrix;
let x = Matrix::from_fn(0, 0, |_, _| 0.0);
assert_eq!(x.rows(), 0);
assert_eq!(x.cols(), 0);
let lu = FullPivLu::decompose(x).unwrap();
assert!(lu.is_invertible());
assert_eq!(lu.rank(), 0);
assert_eq!(lu.det(), 1.0);
let inverse = lu.inverse().unwrap();
assert_eq!(inverse.rows(), 0);
assert_eq!(inverse.cols(), 0);
let LUPQ { l, u, p, q } = lu.unpack();
assert_eq!(l.rows(), 0);
assert_eq!(l.cols(), 0);
assert_eq!(u.rows(), 0);
assert_eq!(u.cols(), 0);
assert_eq!(p.size(), 0);
assert_eq!(q.size(), 0);
}
}