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
//! Module for testing various matrix properties, such as being
//! diagonal or unitary.
//!
//! Most tests have a regular and a `_tol` version, which takes a
//! specific tolerance. In the latter case, all floating-point
//! comparisons are done within the tolerance. In the former case, a
//! default tolerance is used (`T::tol()` for type `T`).

use impl_prelude::*;
use util::conj_t;
use num_traits::{Float};

/// Return true iff the matrix is square.
pub fn is_square_size(d: &Ix2) -> bool {
    d[0] == d[1]
}

/// Return the default tolerance used for most property testing.
pub fn default_tol<T, D>(mat: &ArrayBase<D, Ix2>)
                              -> T::RealPart
    where T: LinxalImplScalar,
          D: Data<Elem=T> {
    T::tol() * mat.iter().map(|x| x.mag())
        .fold(T::RealPart::neg_infinity(), |x, y| if x > y { x } else { y })

}

/// Returns true iff the matrix is diagonal.
pub fn is_diagonal<T: LinxalImplScalar, D: Data<Elem=T>>(mat: &ArrayBase<D, Ix2>) -> bool {
    is_diagonal_tol(mat, default_tol(mat))
}

/// Returns true iff the matrix is diagonal, within a given tolerance.
///
/// The input matrix need not be square to be diagonal.
pub fn is_diagonal_tol<T, D, F>(mat: &ArrayBase<D, Ix2>, tolerance: F)
                                -> bool
    where T: LinxalImplScalar,
          D: Data<Elem=T>,
          F: Into<T::RealPart> {
    let tol = tolerance.into();

    !mat.indexed_iter().any(|(i, x)| i.0 != i.1 && x.mag() > tol)
}

/// Returns true iff the matrix is an identity matrix.
pub fn is_identity<T: LinxalImplScalar, D: Data<Elem=T>>(mat: &ArrayBase<D, Ix2>) -> bool {
    is_identity_tol(mat, T::tol())
}

/// Returns true iff the matrix is an identity matrix, within a
/// given tolerance.
pub fn is_identity_tol<T: LinxalImplScalar, D: Data<Elem=T>>(mat: &ArrayBase<D, Ix2>, tol: T::RealPart) -> bool {
    if !is_square_size(&mat.raw_dim()) {
        return false;
    }
    !mat.indexed_iter().any(
        |(i, x)| (i.0 != i.1 && x.mag() > tol) || (i.0 == i.1 && (*x - T::one()).mag() > tol))
}

/// Return true iff the matrix if Hermitian (or symmetric in the real
/// case).
pub fn is_symmetric<T: LinxalImplScalar, D: Data<Elem=T>>(mat: &ArrayBase<D, Ix2>) -> bool {
    is_symmetric_tol(mat, default_tol(mat))
}

/// Return true iff the matrix if Hermitian (or symmetric in the real
/// case), within a given tolerance.
pub fn is_symmetric_tol<T: LinxalImplScalar, D: Data<Elem=T>>(mat: &ArrayBase<D, Ix2>, tol: T::RealPart) -> bool {
    let d = mat.dim();

    if !is_square_size(&mat.raw_dim()) {
        return false;
    }

    for i in 0..d.0 {
        for j in 0..i {
            if (mat[(i, j)] - mat[(j, i)].cj()).mag() > tol {
                return false;
            }
        }
    }

    true
}

/// Return true iff the matrix is unitary, within tolerance.
///
/// A unitary matrix U is a square matrix that satisfiies
///
/// $$U^H\ cdot U = U \cdot U^H = I$$
///
pub fn is_unitary_tol<T: LinxalImplScalar, D: Data<Elem=T>>(mat: &ArrayBase<D, Ix2>, tol: T::RealPart) -> bool {
    if !is_square_size(&mat.raw_dim()) {
        return false;
    }
    let prod = mat.dot(&conj_t(&mat));
    is_identity_tol(&prod, tol)
}

/// Return true iff the matrix is unitary
///
/// A unitary matrix U is a square matrix taht satisfiies
///
/// $$U^H\ cdot U = U \cdot U^H = I$$
///
pub fn is_unitary<T: LinxalImplScalar, D: Data<Elem=T>>(mat: &ArrayBase<D, Ix2>) -> bool {
    is_unitary_tol(mat, default_tol(mat))
}

/// Return true iff the matrix is triangular / trapezoidal, with the
/// side specified by `uplo`.
pub fn is_triangular_tol<T, D, F>(mat: &ArrayBase<D, Ix2>, uplo: Symmetric, tolerance: F)
                                  -> bool
    where T: LinxalImplScalar,
          D: Data<Elem=T>,
          F: Into<T::RealPart> {

    match uplo {
        Symmetric::Upper => get_lower_bandwidth_tol(mat, tolerance) == 0,
        Symmetric::Lower => get_upper_bandwidth_tol(mat, tolerance) == 0
    }
}

/// Return true iff the matrix is triangular / trapezoidal, with the
/// side specified by `uplo`.
///
/// Uses the defalut toleranace for comparisons to 0.
pub fn is_triangular<T, D>(mat: &ArrayBase<D, Ix2>, uplo: Symmetric)
                                  -> bool
    where T: LinxalImplScalar,
          D: Data<Elem=T> {

    is_triangular_tol(mat, uplo, default_tol(&mat))
}

/// Return the lower bandwidth of the matrix, within the specified
/// tolerance.
///
/// Zero-dimension matrices have 0 bandwidth.
pub fn get_lower_bandwidth_tol<T: LinxalImplScalar, D: Data<Elem=T>, F: Into<T::RealPart>>(mat: &ArrayBase<D, Ix2>, tol: F) -> usize {
    let dim = mat.dim();
    let r = dim.0 - 1;
    let t = tol.into();

    for b in 0..r {
        for i in 0..cmp::min(b+1, dim.1) {
            let a = mat[[r - b + i, i]];
            if a.mag() > t {
                return r - b;
            }
        }
    }
    0
}

/// Return the lower bandwidth of the matrix.
///
/// Uses a tolerance of `T::tol()` * `max |a_ij|`
pub fn get_lower_bandwidth<T: LinxalImplScalar, D: Data<Elem=T>>(mat: &ArrayBase<D, Ix2>) -> usize {
    get_lower_bandwidth_tol(mat, default_tol(mat))
}


/// Return the upper bandwidth of the matrix, within the specified
/// tolerance.
///
/// Zero-dimension matrices have 0 bandwidth.
pub fn get_upper_bandwidth_tol<T: LinxalImplScalar, D: Data<Elem=T>, F: Into<T::RealPart>>(mat: &ArrayBase<D, Ix2>, tol: F) -> usize {
    let dim = mat.dim();
    let c = dim.1 - 1;
    let t = tol.into();

    for b in 0..c {
        for i in 0..cmp::min(b+1, dim.0) {
            let a = mat[[i, c - b + i]];
            if a.mag() > t {
                return c - b;
            }
        }
    }
    0
}

/// Return the upper bandwidth of the matrix.
///
/// Uses a tolerance of `T::tol()` * `max |a_ij|`
pub fn get_upper_bandwidth<T: LinxalImplScalar, D: Data<Elem=T>>(mat: &ArrayBase<D, Ix2>) -> usize {
    get_upper_bandwidth_tol(mat, default_tol(mat))
}