|
| 1 | +//! Least squares |
| 2 | +
|
| 3 | +use lapacke; |
| 4 | +use ndarray::{ErrorKind, ShapeError}; |
| 5 | +use num_traits::Zero; |
| 6 | + |
| 7 | +use crate::error::*; |
| 8 | +use crate::layout::MatrixLayout; |
| 9 | +use crate::types::*; |
| 10 | + |
| 11 | +use super::into_result; |
| 12 | + |
| 13 | +/// Result of LeastSquares |
| 14 | +pub struct LeastSquaresOutput<A: Scalar> { |
| 15 | + /// singular values |
| 16 | + pub singular_values: Vec<A::Real>, |
| 17 | + /// The rank of the input matrix A |
| 18 | + pub rank: i32, |
| 19 | +} |
| 20 | + |
| 21 | +/// Wraps `*gelsd` |
| 22 | +pub trait LeastSquaresSvdDivideConquer_: Scalar { |
| 23 | + unsafe fn least_squares( |
| 24 | + a_layout: MatrixLayout, |
| 25 | + a: &mut [Self], |
| 26 | + b: &mut [Self], |
| 27 | + ) -> Result<LeastSquaresOutput<Self>>; |
| 28 | + |
| 29 | + unsafe fn least_squares_nrhs( |
| 30 | + a_layout: MatrixLayout, |
| 31 | + a: &mut [Self], |
| 32 | + b_layout: MatrixLayout, |
| 33 | + b: &mut [Self], |
| 34 | + ) -> Result<LeastSquaresOutput<Self>>; |
| 35 | +} |
| 36 | + |
| 37 | +macro_rules! impl_least_squares { |
| 38 | + ($scalar:ty, $gelsd:path) => { |
| 39 | + impl LeastSquaresSvdDivideConquer_ for $scalar { |
| 40 | + unsafe fn least_squares( |
| 41 | + a_layout: MatrixLayout, |
| 42 | + a: &mut [Self], |
| 43 | + b: &mut [Self], |
| 44 | + ) -> Result<LeastSquaresOutput<Self>> { |
| 45 | + let (m, n) = a_layout.size(); |
| 46 | + if (m as usize) > b.len() || (n as usize) > b.len() { |
| 47 | + return Err(LinalgError::Shape(ShapeError::from_kind( |
| 48 | + ErrorKind::IncompatibleShape, |
| 49 | + ))); |
| 50 | + } |
| 51 | + let k = ::std::cmp::min(m, n); |
| 52 | + let nrhs = 1; |
| 53 | + let rcond: Self::Real = -1.; |
| 54 | + let mut singular_values: Vec<Self::Real> = vec![Self::Real::zero(); k as usize]; |
| 55 | + let mut rank: i32 = 0; |
| 56 | + |
| 57 | + let status = $gelsd( |
| 58 | + a_layout.lapacke_layout(), |
| 59 | + m, |
| 60 | + n, |
| 61 | + nrhs, |
| 62 | + a, |
| 63 | + a_layout.lda(), |
| 64 | + b, |
| 65 | + // this is the 'leading dimension of b', in the case where |
| 66 | + // b is a single vector, this is 1 |
| 67 | + nrhs, |
| 68 | + &mut singular_values, |
| 69 | + rcond, |
| 70 | + &mut rank, |
| 71 | + ); |
| 72 | + |
| 73 | + into_result( |
| 74 | + status, |
| 75 | + LeastSquaresOutput { |
| 76 | + singular_values, |
| 77 | + rank, |
| 78 | + }, |
| 79 | + ) |
| 80 | + } |
| 81 | + |
| 82 | + unsafe fn least_squares_nrhs( |
| 83 | + a_layout: MatrixLayout, |
| 84 | + a: &mut [Self], |
| 85 | + b_layout: MatrixLayout, |
| 86 | + b: &mut [Self], |
| 87 | + ) -> Result<LeastSquaresOutput<Self>> { |
| 88 | + let (m, n) = a_layout.size(); |
| 89 | + if (m as usize) > b.len() |
| 90 | + || (n as usize) > b.len() |
| 91 | + || a_layout.lapacke_layout() != b_layout.lapacke_layout() |
| 92 | + { |
| 93 | + return Err(LinalgError::Shape(ShapeError::from_kind( |
| 94 | + ErrorKind::IncompatibleShape, |
| 95 | + ))); |
| 96 | + } |
| 97 | + let k = ::std::cmp::min(m, n); |
| 98 | + let nrhs = b_layout.size().1; |
| 99 | + let rcond: Self::Real = -1.; |
| 100 | + let mut singular_values: Vec<Self::Real> = vec![Self::Real::zero(); k as usize]; |
| 101 | + let mut rank: i32 = 0; |
| 102 | + |
| 103 | + let status = $gelsd( |
| 104 | + a_layout.lapacke_layout(), |
| 105 | + m, |
| 106 | + n, |
| 107 | + nrhs, |
| 108 | + a, |
| 109 | + a_layout.lda(), |
| 110 | + b, |
| 111 | + b_layout.lda(), |
| 112 | + &mut singular_values, |
| 113 | + rcond, |
| 114 | + &mut rank, |
| 115 | + ); |
| 116 | + |
| 117 | + into_result( |
| 118 | + status, |
| 119 | + LeastSquaresOutput { |
| 120 | + singular_values, |
| 121 | + rank, |
| 122 | + }, |
| 123 | + ) |
| 124 | + } |
| 125 | + } |
| 126 | + }; |
| 127 | +} |
| 128 | + |
| 129 | +impl_least_squares!(f64, lapacke::dgelsd); |
| 130 | +impl_least_squares!(f32, lapacke::sgelsd); |
| 131 | +impl_least_squares!(c64, lapacke::zgelsd); |
| 132 | +impl_least_squares!(c32, lapacke::cgelsd); |
0 commit comments