Skip to content

Commit 1df28a2

Browse files
committed
Add check in SolveH for compatible shapes
1 parent 2364dac commit 1df28a2

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

ndarray-linalg/src/solveh.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,25 +69,42 @@ pub trait SolveH<A: Scalar> {
6969
/// Solves a system of linear equations `A * x = b` with Hermitian (or real
7070
/// symmetric) matrix `A`, where `A` is `self`, `b` is the argument, and
7171
/// `x` is the successful result.
72+
///
73+
/// # Panics
74+
///
75+
/// Panics if the length of `b` is not the equal to the number of columns
76+
/// of `A`.
7277
fn solveh<S: Data<Elem = A>>(&self, b: &ArrayBase<S, Ix1>) -> Result<Array1<A>> {
7378
let mut b = replicate(b);
7479
self.solveh_inplace(&mut b)?;
7580
Ok(b)
7681
}
82+
7783
/// Solves a system of linear equations `A * x = b` with Hermitian (or real
7884
/// symmetric) matrix `A`, where `A` is `self`, `b` is the argument, and
7985
/// `x` is the successful result.
86+
///
87+
/// # Panics
88+
///
89+
/// Panics if the length of `b` is not the equal to the number of columns
90+
/// of `A`.
8091
fn solveh_into<S: DataMut<Elem = A>>(
8192
&self,
8293
mut b: ArrayBase<S, Ix1>,
8394
) -> Result<ArrayBase<S, Ix1>> {
8495
self.solveh_inplace(&mut b)?;
8596
Ok(b)
8697
}
98+
8799
/// Solves a system of linear equations `A * x = b` with Hermitian (or real
88100
/// symmetric) matrix `A`, where `A` is `self`, `b` is the argument, and
89101
/// `x` is the successful result. The value of `x` is also assigned to the
90102
/// argument.
103+
///
104+
/// # Panics
105+
///
106+
/// Panics if the length of `b` is not the equal to the number of columns
107+
/// of `A`.
91108
fn solveh_inplace<'a, S: DataMut<Elem = A>>(
92109
&self,
93110
b: &'a mut ArrayBase<S, Ix1>,
@@ -113,6 +130,11 @@ where
113130
where
114131
Sb: DataMut<Elem = A>,
115132
{
133+
assert_eq!(
134+
rhs.len(),
135+
self.a.len_of(Axis(1)),
136+
"The length of `rhs` must be compatible with the shape of the factored matrix.",
137+
);
116138
A::solveh(
117139
self.a.square_layout()?,
118140
UPLO::Upper,

0 commit comments

Comments
 (0)