mod scalar;
pub use scalar::{ComplexLapackScalar, LapackScalar, RealLapackScalar};
use crate::enums::{Layout, Side, Transpose, UpLo};
use crate::error::{Error, Result};
use crate::matrix::{MatrixMut, MatrixRef};
use crate::util::dim_to_mkl_int;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Job {
None,
Compute,
All,
Some,
Overwrite,
}
impl Job {
#[inline]
#[must_use]
pub const fn as_char(self) -> u8 {
match self {
Self::None => b'N',
Self::Compute => b'V',
Self::All => b'A',
Self::Some => b'S',
Self::Overwrite => b'O',
}
}
}
#[inline]
fn check_info(info: i32) -> Result<()> {
if info == 0 {
Ok(())
} else if info < 0 {
Err(Error::LapackIllegalArgument { argument: -info })
} else {
Err(Error::LapackComputationFailure { info })
}
}
#[inline]
fn ensure_square<T>(a: &MatrixMut<'_, T>) -> Result<usize> {
if a.rows() != a.cols() {
return Err(Error::InvalidArgument("matrix must be square"));
}
Ok(a.rows())
}
#[inline]
fn ensure_square_ref<T>(a: &MatrixRef<'_, T>) -> Result<usize> {
if a.rows() != a.cols() {
return Err(Error::InvalidArgument("matrix must be square"));
}
Ok(a.rows())
}
pub fn gesv<T: LapackScalar>(
a: &mut MatrixMut<'_, T>,
ipiv: &mut [i32],
b: &mut MatrixMut<'_, T>,
) -> Result<()> {
let n = ensure_square(a)?;
let layout = ensure_layout(&[a.layout(), b.layout()])?;
if b.rows() != n {
return Err(Error::InvalidArgument(
"B must have the same number of rows as A",
));
}
if ipiv.len() < n {
return Err(Error::InvalidArgument("ipiv must have at least n entries"));
}
let info = unsafe {
T::lapacke_gesv(
layout.as_lapack(),
dim_to_mkl_int(n)?,
dim_to_mkl_int(b.cols())?,
a.as_mut_ptr(),
dim_to_mkl_int(a.leading_dim())?,
ipiv.as_mut_ptr(),
b.as_mut_ptr(),
dim_to_mkl_int(b.leading_dim())?,
)
};
check_info(info)
}
pub fn getrf<T: LapackScalar>(a: &mut MatrixMut<'_, T>, ipiv: &mut [i32]) -> Result<()> {
let need = a.rows().min(a.cols());
if ipiv.len() < need {
return Err(Error::InvalidArgument(
"ipiv must have at least min(m, n) entries",
));
}
let info = unsafe {
T::lapacke_getrf(
a.layout().as_lapack(),
dim_to_mkl_int(a.rows())?,
dim_to_mkl_int(a.cols())?,
a.as_mut_ptr(),
dim_to_mkl_int(a.leading_dim())?,
ipiv.as_mut_ptr(),
)
};
check_info(info)
}
pub fn getrs<T: LapackScalar>(
trans: Transpose,
a: &MatrixRef<'_, T>,
ipiv: &[i32],
b: &mut MatrixMut<'_, T>,
) -> Result<()> {
let n = ensure_square_ref(a)?;
let layout = ensure_layout(&[a.layout(), b.layout()])?;
if b.rows() != n {
return Err(Error::InvalidArgument(
"B must have the same number of rows as A",
));
}
if ipiv.len() < n {
return Err(Error::InvalidArgument("ipiv must have at least n entries"));
}
let info = unsafe {
T::lapacke_getrs(
layout.as_lapack(),
trans.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
dim_to_mkl_int(b.cols())?,
a.as_ptr(),
dim_to_mkl_int(a.leading_dim())?,
ipiv.as_ptr(),
b.as_mut_ptr(),
dim_to_mkl_int(b.leading_dim())?,
)
};
check_info(info)
}
pub fn getri<T: LapackScalar>(a: &mut MatrixMut<'_, T>, ipiv: &[i32]) -> Result<()> {
let n = ensure_square(a)?;
if ipiv.len() < n {
return Err(Error::InvalidArgument("ipiv must have at least n entries"));
}
let info = unsafe {
T::lapacke_getri(
a.layout().as_lapack(),
dim_to_mkl_int(n)?,
a.as_mut_ptr(),
dim_to_mkl_int(a.leading_dim())?,
ipiv.as_ptr(),
)
};
check_info(info)
}
pub fn posv<T: LapackScalar>(
uplo: UpLo,
a: &mut MatrixMut<'_, T>,
b: &mut MatrixMut<'_, T>,
) -> Result<()> {
let n = ensure_square(a)?;
let layout = ensure_layout(&[a.layout(), b.layout()])?;
if b.rows() != n {
return Err(Error::InvalidArgument(
"B must have the same number of rows as A",
));
}
let info = unsafe {
T::lapacke_posv(
layout.as_lapack(),
uplo.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
dim_to_mkl_int(b.cols())?,
a.as_mut_ptr(),
dim_to_mkl_int(a.leading_dim())?,
b.as_mut_ptr(),
dim_to_mkl_int(b.leading_dim())?,
)
};
check_info(info)
}
pub fn potrf<T: LapackScalar>(uplo: UpLo, a: &mut MatrixMut<'_, T>) -> Result<()> {
let n = ensure_square(a)?;
let info = unsafe {
T::lapacke_potrf(
a.layout().as_lapack(),
uplo.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
a.as_mut_ptr(),
dim_to_mkl_int(a.leading_dim())?,
)
};
check_info(info)
}
pub fn potrs<T: LapackScalar>(
uplo: UpLo,
a: &MatrixRef<'_, T>,
b: &mut MatrixMut<'_, T>,
) -> Result<()> {
let n = ensure_square_ref(a)?;
let layout = ensure_layout(&[a.layout(), b.layout()])?;
if b.rows() != n {
return Err(Error::InvalidArgument(
"B must have the same number of rows as A",
));
}
let info = unsafe {
T::lapacke_potrs(
layout.as_lapack(),
uplo.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
dim_to_mkl_int(b.cols())?,
a.as_ptr(),
dim_to_mkl_int(a.leading_dim())?,
b.as_mut_ptr(),
dim_to_mkl_int(b.leading_dim())?,
)
};
check_info(info)
}
pub fn sysv<T: RealLapackScalar>(
uplo: UpLo,
a: &mut MatrixMut<'_, T>,
ipiv: &mut [i32],
b: &mut MatrixMut<'_, T>,
) -> Result<()> {
let n = ensure_square(a)?;
let layout = ensure_layout(&[a.layout(), b.layout()])?;
if b.rows() != n {
return Err(Error::InvalidArgument(
"B must have the same number of rows as A",
));
}
if ipiv.len() < n {
return Err(Error::InvalidArgument("ipiv must have at least n entries"));
}
let info = unsafe {
T::lapacke_sysv(
layout.as_lapack(),
uplo.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
dim_to_mkl_int(b.cols())?,
a.as_mut_ptr(),
dim_to_mkl_int(a.leading_dim())?,
ipiv.as_mut_ptr(),
b.as_mut_ptr(),
dim_to_mkl_int(b.leading_dim())?,
)
};
check_info(info)
}
pub fn hesv<T: ComplexLapackScalar>(
uplo: UpLo,
a: &mut MatrixMut<'_, T>,
ipiv: &mut [i32],
b: &mut MatrixMut<'_, T>,
) -> Result<()> {
let n = ensure_square(a)?;
let layout = ensure_layout(&[a.layout(), b.layout()])?;
if b.rows() != n {
return Err(Error::InvalidArgument(
"B must have the same number of rows as A",
));
}
if ipiv.len() < n {
return Err(Error::InvalidArgument("ipiv must have at least n entries"));
}
let info = unsafe {
T::lapacke_hesv(
layout.as_lapack(),
uplo.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
dim_to_mkl_int(b.cols())?,
a.as_mut_ptr(),
dim_to_mkl_int(a.leading_dim())?,
ipiv.as_mut_ptr(),
b.as_mut_ptr(),
dim_to_mkl_int(b.leading_dim())?,
)
};
check_info(info)
}
pub fn geqrf<T: LapackScalar>(a: &mut MatrixMut<'_, T>, tau: &mut [T]) -> Result<()> {
let need = a.rows().min(a.cols());
if tau.len() < need {
return Err(Error::InvalidArgument(
"tau must have at least min(m, n) entries",
));
}
let info = unsafe {
T::lapacke_geqrf(
a.layout().as_lapack(),
dim_to_mkl_int(a.rows())?,
dim_to_mkl_int(a.cols())?,
a.as_mut_ptr(),
dim_to_mkl_int(a.leading_dim())?,
tau.as_mut_ptr(),
)
};
check_info(info)
}
pub fn gelqf<T: LapackScalar>(a: &mut MatrixMut<'_, T>, tau: &mut [T]) -> Result<()> {
let need = a.rows().min(a.cols());
if tau.len() < need {
return Err(Error::InvalidArgument(
"tau must have at least min(m, n) entries",
));
}
let info = unsafe {
T::lapacke_gelqf(
a.layout().as_lapack(),
dim_to_mkl_int(a.rows())?,
dim_to_mkl_int(a.cols())?,
a.as_mut_ptr(),
dim_to_mkl_int(a.leading_dim())?,
tau.as_mut_ptr(),
)
};
check_info(info)
}
pub fn orgqr<T: LapackScalar>(a: &mut MatrixMut<'_, T>, tau: &[T], k: usize) -> Result<()> {
if tau.len() < k {
return Err(Error::InvalidArgument("tau must have at least k entries"));
}
let info = unsafe {
T::lapacke_orgqr(
a.layout().as_lapack(),
dim_to_mkl_int(a.rows())?,
dim_to_mkl_int(a.cols())?,
dim_to_mkl_int(k)?,
a.as_mut_ptr(),
dim_to_mkl_int(a.leading_dim())?,
tau.as_ptr(),
)
};
check_info(info)
}
pub fn gels<T: LapackScalar>(
trans: Transpose,
a: &mut MatrixMut<'_, T>,
b: &mut MatrixMut<'_, T>,
) -> Result<()> {
let layout = ensure_layout(&[a.layout(), b.layout()])?;
let info = unsafe {
T::lapacke_gels(
layout.as_lapack(),
trans.as_char() as core::ffi::c_char,
dim_to_mkl_int(a.rows())?,
dim_to_mkl_int(a.cols())?,
dim_to_mkl_int(b.cols())?,
a.as_mut_ptr(),
dim_to_mkl_int(a.leading_dim())?,
b.as_mut_ptr(),
dim_to_mkl_int(b.leading_dim())?,
)
};
check_info(info)
}
pub fn gelsd<T: LapackScalar>(
a: &mut MatrixMut<'_, T>,
b: &mut MatrixMut<'_, T>,
s: &mut [T::Real],
rcond: T::Real,
) -> Result<i32> {
let layout = ensure_layout(&[a.layout(), b.layout()])?;
let need = a.rows().min(a.cols());
if s.len() < need {
return Err(Error::InvalidArgument(
"s must have at least min(m, n) entries",
));
}
let mut rank: i32 = 0;
let info = unsafe {
T::lapacke_gelsd(
layout.as_lapack(),
dim_to_mkl_int(a.rows())?,
dim_to_mkl_int(a.cols())?,
dim_to_mkl_int(b.cols())?,
a.as_mut_ptr(),
dim_to_mkl_int(a.leading_dim())?,
b.as_mut_ptr(),
dim_to_mkl_int(b.leading_dim())?,
s.as_mut_ptr(),
rcond,
&mut rank,
)
};
check_info(info)?;
Ok(rank)
}
pub fn syev<T: RealLapackScalar>(
jobz: Job,
uplo: UpLo,
a: &mut MatrixMut<'_, T>,
w: &mut [T],
) -> Result<()> {
let n = ensure_square(a)?;
if w.len() < n {
return Err(Error::InvalidArgument("w must have at least n entries"));
}
let info = unsafe {
T::lapacke_syev(
a.layout().as_lapack(),
jobz.as_char() as core::ffi::c_char,
uplo.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
a.as_mut_ptr(),
dim_to_mkl_int(a.leading_dim())?,
w.as_mut_ptr(),
)
};
check_info(info)
}
pub fn heev<T: ComplexLapackScalar>(
jobz: Job,
uplo: UpLo,
a: &mut MatrixMut<'_, T>,
w: &mut [T::Real],
) -> Result<()> {
let n = ensure_square(a)?;
if w.len() < n {
return Err(Error::InvalidArgument("w must have at least n entries"));
}
let info = unsafe {
T::lapacke_heev(
a.layout().as_lapack(),
jobz.as_char() as core::ffi::c_char,
uplo.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
a.as_mut_ptr(),
dim_to_mkl_int(a.leading_dim())?,
w.as_mut_ptr(),
)
};
check_info(info)
}
pub fn geev_real<T: RealLapackScalar>(
jobvl: Job,
jobvr: Job,
a: &mut MatrixMut<'_, T>,
wr: &mut [T],
wi: &mut [T],
vl: Option<&mut MatrixMut<'_, T>>,
vr: Option<&mut MatrixMut<'_, T>>,
) -> Result<()> {
let n = ensure_square(a)?;
if wr.len() < n || wi.len() < n {
return Err(Error::InvalidArgument(
"wr and wi must have at least n entries",
));
}
let lapack_layout = a.layout().as_lapack();
let lda = dim_to_mkl_int(a.leading_dim())?;
let (vl_ptr, ldvl) = match vl {
Some(m) => (m.as_mut_ptr(), dim_to_mkl_int(m.leading_dim())?),
None => (core::ptr::null_mut(), dim_to_mkl_int(n.max(1))?),
};
let (vr_ptr, ldvr) = match vr {
Some(m) => (m.as_mut_ptr(), dim_to_mkl_int(m.leading_dim())?),
None => (core::ptr::null_mut(), dim_to_mkl_int(n.max(1))?),
};
let info = unsafe {
T::lapacke_geev(
lapack_layout,
jobvl.as_char() as core::ffi::c_char,
jobvr.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
a.as_mut_ptr(),
lda,
wr.as_mut_ptr(),
wi.as_mut_ptr(),
vl_ptr,
ldvl,
vr_ptr,
ldvr,
)
};
check_info(info)
}
pub fn geev_complex<T: ComplexLapackScalar>(
jobvl: Job,
jobvr: Job,
a: &mut MatrixMut<'_, T>,
w: &mut [T],
vl: Option<&mut MatrixMut<'_, T>>,
vr: Option<&mut MatrixMut<'_, T>>,
) -> Result<()> {
let n = ensure_square(a)?;
if w.len() < n {
return Err(Error::InvalidArgument("w must have at least n entries"));
}
let lapack_layout = a.layout().as_lapack();
let lda = dim_to_mkl_int(a.leading_dim())?;
let (vl_ptr, ldvl) = match vl {
Some(m) => (m.as_mut_ptr(), dim_to_mkl_int(m.leading_dim())?),
None => (core::ptr::null_mut(), dim_to_mkl_int(n.max(1))?),
};
let (vr_ptr, ldvr) = match vr {
Some(m) => (m.as_mut_ptr(), dim_to_mkl_int(m.leading_dim())?),
None => (core::ptr::null_mut(), dim_to_mkl_int(n.max(1))?),
};
let info = unsafe {
T::lapacke_geev_complex(
lapack_layout,
jobvl.as_char() as core::ffi::c_char,
jobvr.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
a.as_mut_ptr(),
lda,
w.as_mut_ptr(),
vl_ptr,
ldvl,
vr_ptr,
ldvr,
)
};
check_info(info)
}
pub fn gesdd<T: LapackScalar>(
jobz: Job,
a: &mut MatrixMut<'_, T>,
s: &mut [T::Real],
u: Option<&mut MatrixMut<'_, T>>,
vt: Option<&mut MatrixMut<'_, T>>,
) -> Result<()> {
let m = a.rows();
let n = a.cols();
if s.len() < m.min(n) {
return Err(Error::InvalidArgument(
"s must have at least min(m, n) entries",
));
}
let lapack_layout = a.layout().as_lapack();
let lda = dim_to_mkl_int(a.leading_dim())?;
let (u_ptr, ldu) = match u {
Some(m) => (m.as_mut_ptr(), dim_to_mkl_int(m.leading_dim())?),
None => (core::ptr::null_mut(), dim_to_mkl_int(m.max(1))?),
};
let (vt_ptr, ldvt) = match vt {
Some(m) => (m.as_mut_ptr(), dim_to_mkl_int(m.leading_dim())?),
None => (core::ptr::null_mut(), dim_to_mkl_int(n.max(1))?),
};
let info = unsafe {
T::lapacke_gesdd(
lapack_layout,
jobz.as_char() as core::ffi::c_char,
dim_to_mkl_int(m)?,
dim_to_mkl_int(n)?,
a.as_mut_ptr(),
lda,
s.as_mut_ptr(),
u_ptr,
ldu,
vt_ptr,
ldvt,
)
};
check_info(info)
}
pub fn gesvd<T: LapackScalar>(
jobu: Job,
jobvt: Job,
a: &mut MatrixMut<'_, T>,
s: &mut [T::Real],
u: Option<&mut MatrixMut<'_, T>>,
vt: Option<&mut MatrixMut<'_, T>>,
superb: &mut [T::Real],
) -> Result<()> {
let m = a.rows();
let n = a.cols();
if s.len() < m.min(n) {
return Err(Error::InvalidArgument(
"s must have at least min(m, n) entries",
));
}
if superb.len() < m.min(n).saturating_sub(1) {
return Err(Error::InvalidArgument(
"superb must have at least min(m, n) - 1 entries",
));
}
let lapack_layout = a.layout().as_lapack();
let lda = dim_to_mkl_int(a.leading_dim())?;
let (u_ptr, ldu) = match u {
Some(m) => (m.as_mut_ptr(), dim_to_mkl_int(m.leading_dim())?),
None => (core::ptr::null_mut(), dim_to_mkl_int(m.max(1))?),
};
let (vt_ptr, ldvt) = match vt {
Some(m) => (m.as_mut_ptr(), dim_to_mkl_int(m.leading_dim())?),
None => (core::ptr::null_mut(), dim_to_mkl_int(n.max(1))?),
};
let info = unsafe {
T::lapacke_gesvd(
lapack_layout,
jobu.as_char() as core::ffi::c_char,
jobvt.as_char() as core::ffi::c_char,
dim_to_mkl_int(m)?,
dim_to_mkl_int(n)?,
a.as_mut_ptr(),
lda,
s.as_mut_ptr(),
u_ptr,
ldu,
vt_ptr,
ldvt,
superb.as_mut_ptr(),
)
};
check_info(info)
}
#[inline]
fn ensure_layout(layouts: &[Layout]) -> Result<Layout> {
let first = layouts[0];
if layouts.iter().all(|l| *l == first) {
Ok(first)
} else {
Err(Error::InvalidArgument(
"all matrices in a LAPACK call must share the same Layout",
))
}
}
#[allow(
clippy::too_many_arguments,
reason = "stable public surface: published since v0.1.0 with 10 arguments; changing the signature breaks consumers"
)]
pub fn gbsv<T: LapackScalar>(
layout: Layout,
n: usize,
kl: usize,
ku: usize,
nrhs: usize,
ab: &mut [T],
ldab: usize,
ipiv: &mut [i32],
b: &mut [T],
ldb: usize,
) -> Result<()> {
if ipiv.len() < n {
return Err(Error::InvalidArgument("ipiv must have at least n entries"));
}
let info = unsafe {
T::lapacke_gbsv(
layout.as_lapack(),
dim_to_mkl_int(n)?,
dim_to_mkl_int(kl)?,
dim_to_mkl_int(ku)?,
dim_to_mkl_int(nrhs)?,
ab.as_mut_ptr(),
dim_to_mkl_int(ldab)?,
ipiv.as_mut_ptr(),
b.as_mut_ptr(),
dim_to_mkl_int(ldb)?,
)
};
check_info(info)
}
#[allow(
clippy::too_many_arguments,
reason = "stable public surface: published since v0.1.0 with 8 arguments; changing the signature breaks consumers"
)]
pub fn gbtrf<T: LapackScalar>(
layout: Layout,
m: usize,
n: usize,
kl: usize,
ku: usize,
ab: &mut [T],
ldab: usize,
ipiv: &mut [i32],
) -> Result<()> {
if ipiv.len() < m.min(n) {
return Err(Error::InvalidArgument(
"ipiv must have at least min(m, n) entries",
));
}
let info = unsafe {
T::lapacke_gbtrf(
layout.as_lapack(),
dim_to_mkl_int(m)?,
dim_to_mkl_int(n)?,
dim_to_mkl_int(kl)?,
dim_to_mkl_int(ku)?,
ab.as_mut_ptr(),
dim_to_mkl_int(ldab)?,
ipiv.as_mut_ptr(),
)
};
check_info(info)
}
#[allow(
clippy::too_many_arguments,
reason = "stable public surface: published since v0.1.0 with 11 arguments; changing the signature breaks consumers"
)]
pub fn gbtrs<T: LapackScalar>(
layout: Layout,
trans: Transpose,
n: usize,
kl: usize,
ku: usize,
nrhs: usize,
ab: &[T],
ldab: usize,
ipiv: &[i32],
b: &mut [T],
ldb: usize,
) -> Result<()> {
if ipiv.len() < n {
return Err(Error::InvalidArgument("ipiv must have at least n entries"));
}
let info = unsafe {
T::lapacke_gbtrs(
layout.as_lapack(),
trans.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
dim_to_mkl_int(kl)?,
dim_to_mkl_int(ku)?,
dim_to_mkl_int(nrhs)?,
ab.as_ptr(),
dim_to_mkl_int(ldab)?,
ipiv.as_ptr(),
b.as_mut_ptr(),
dim_to_mkl_int(ldb)?,
)
};
check_info(info)
}
#[allow(
clippy::too_many_arguments,
reason = "stable public surface: published since v0.1.0 with 8 arguments; changing the signature breaks consumers"
)]
pub fn gtsv<T: LapackScalar>(
layout: Layout,
n: usize,
nrhs: usize,
dl: &mut [T],
d: &mut [T],
du: &mut [T],
b: &mut [T],
ldb: usize,
) -> Result<()> {
if dl.len() < n.saturating_sub(1) || d.len() < n || du.len() < n.saturating_sub(1) {
return Err(Error::InvalidArgument(
"tridiagonal arrays too short: need |dl|, |du| ≥ n-1, |d| ≥ n",
));
}
let info = unsafe {
T::lapacke_gtsv(
layout.as_lapack(),
dim_to_mkl_int(n)?,
dim_to_mkl_int(nrhs)?,
dl.as_mut_ptr(),
d.as_mut_ptr(),
du.as_mut_ptr(),
b.as_mut_ptr(),
dim_to_mkl_int(ldb)?,
)
};
check_info(info)
}
pub fn gttrf<T: LapackScalar>(
n: usize,
dl: &mut [T],
d: &mut [T],
du: &mut [T],
du2: &mut [T],
ipiv: &mut [i32],
) -> Result<()> {
if du2.len() < n.saturating_sub(2) || ipiv.len() < n {
return Err(Error::InvalidArgument(
"du2 must have ≥ n-2 entries and ipiv ≥ n entries",
));
}
let info = unsafe {
T::lapacke_gttrf(
dim_to_mkl_int(n)?,
dl.as_mut_ptr(),
d.as_mut_ptr(),
du.as_mut_ptr(),
du2.as_mut_ptr(),
ipiv.as_mut_ptr(),
)
};
check_info(info)
}
#[allow(
clippy::too_many_arguments,
reason = "stable public surface: published since v0.1.0 with 11 arguments; changing the signature breaks consumers"
)]
pub fn gttrs<T: LapackScalar>(
layout: Layout,
trans: Transpose,
n: usize,
nrhs: usize,
dl: &[T],
d: &[T],
du: &[T],
du2: &[T],
ipiv: &[i32],
b: &mut [T],
ldb: usize,
) -> Result<()> {
let info = unsafe {
T::lapacke_gttrs(
layout.as_lapack(),
trans.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
dim_to_mkl_int(nrhs)?,
dl.as_ptr(),
d.as_ptr(),
du.as_ptr(),
du2.as_ptr(),
ipiv.as_ptr(),
b.as_mut_ptr(),
dim_to_mkl_int(ldb)?,
)
};
check_info(info)
}
#[allow(
clippy::too_many_arguments,
reason = "stable public surface: published since v0.1.0 with 9 arguments; changing the signature breaks consumers"
)]
pub fn pbsv<T: LapackScalar>(
layout: Layout,
uplo: UpLo,
n: usize,
kd: usize,
nrhs: usize,
ab: &mut [T],
ldab: usize,
b: &mut [T],
ldb: usize,
) -> Result<()> {
let info = unsafe {
T::lapacke_pbsv(
layout.as_lapack(),
uplo.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
dim_to_mkl_int(kd)?,
dim_to_mkl_int(nrhs)?,
ab.as_mut_ptr(),
dim_to_mkl_int(ldab)?,
b.as_mut_ptr(),
dim_to_mkl_int(ldb)?,
)
};
check_info(info)
}
pub fn pbtrf<T: LapackScalar>(
layout: Layout,
uplo: UpLo,
n: usize,
kd: usize,
ab: &mut [T],
ldab: usize,
) -> Result<()> {
let info = unsafe {
T::lapacke_pbtrf(
layout.as_lapack(),
uplo.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
dim_to_mkl_int(kd)?,
ab.as_mut_ptr(),
dim_to_mkl_int(ldab)?,
)
};
check_info(info)
}
#[allow(
clippy::too_many_arguments,
reason = "stable public surface: published since v0.1.0 with 9 arguments; changing the signature breaks consumers"
)]
pub fn pbtrs<T: LapackScalar>(
layout: Layout,
uplo: UpLo,
n: usize,
kd: usize,
nrhs: usize,
ab: &[T],
ldab: usize,
b: &mut [T],
ldb: usize,
) -> Result<()> {
let info = unsafe {
T::lapacke_pbtrs(
layout.as_lapack(),
uplo.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
dim_to_mkl_int(kd)?,
dim_to_mkl_int(nrhs)?,
ab.as_ptr(),
dim_to_mkl_int(ldab)?,
b.as_mut_ptr(),
dim_to_mkl_int(ldb)?,
)
};
check_info(info)
}
pub fn ptsv<T: LapackScalar>(
layout: Layout,
n: usize,
nrhs: usize,
d: &mut [T::Real],
e: &mut [T],
b: &mut [T],
ldb: usize,
) -> Result<()> {
if d.len() < n || e.len() < n.saturating_sub(1) {
return Err(Error::InvalidArgument(
"PD tridiagonal arrays too short: need |d| ≥ n, |e| ≥ n-1",
));
}
let info = unsafe {
T::lapacke_ptsv(
layout.as_lapack(),
dim_to_mkl_int(n)?,
dim_to_mkl_int(nrhs)?,
d.as_mut_ptr(),
e.as_mut_ptr(),
b.as_mut_ptr(),
dim_to_mkl_int(ldb)?,
)
};
check_info(info)
}
pub fn pttrf<T: LapackScalar>(n: usize, d: &mut [T::Real], e: &mut [T]) -> Result<()> {
if d.len() < n || e.len() < n.saturating_sub(1) {
return Err(Error::InvalidArgument(
"PD tridiagonal arrays too short: need |d| ≥ n, |e| ≥ n-1",
));
}
let info = unsafe { T::lapacke_pttrf(dim_to_mkl_int(n)?, d.as_mut_ptr(), e.as_mut_ptr()) };
check_info(info)
}
pub fn pttrs_real<T: RealLapackScalar>(
layout: Layout,
n: usize,
nrhs: usize,
d: &[T],
e: &[T],
b: &mut [T],
ldb: usize,
) -> Result<()> {
let info = unsafe {
T::lapacke_pttrs(
layout.as_lapack(),
dim_to_mkl_int(n)?,
dim_to_mkl_int(nrhs)?,
d.as_ptr(),
e.as_ptr(),
b.as_mut_ptr(),
dim_to_mkl_int(ldb)?,
)
};
check_info(info)
}
#[allow(
clippy::too_many_arguments,
reason = "stable public surface: published since v0.1.0 with 8 arguments; changing the signature breaks consumers"
)]
pub fn pttrs_complex<T: ComplexLapackScalar>(
layout: Layout,
uplo: UpLo,
n: usize,
nrhs: usize,
d: &[T::Real],
e: &[T],
b: &mut [T],
ldb: usize,
) -> Result<()> {
let info = unsafe {
T::lapacke_pttrs_complex(
layout.as_lapack(),
uplo.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
dim_to_mkl_int(nrhs)?,
d.as_ptr(),
e.as_ptr(),
b.as_mut_ptr(),
dim_to_mkl_int(ldb)?,
)
};
check_info(info)
}
#[allow(
clippy::too_many_arguments,
reason = "stable public surface: published since v0.1.0 with 8 arguments; changing the signature breaks consumers"
)]
pub fn spsv<T: LapackScalar>(
layout: Layout,
uplo: UpLo,
n: usize,
nrhs: usize,
ap: &mut [T],
ipiv: &mut [i32],
b: &mut [T],
ldb: usize,
) -> Result<()> {
if ap.len() < n * (n + 1) / 2 {
return Err(Error::InvalidArgument(
"ap must have at least n*(n+1)/2 entries",
));
}
if ipiv.len() < n {
return Err(Error::InvalidArgument("ipiv must have at least n entries"));
}
let info = unsafe {
T::lapacke_spsv(
layout.as_lapack(),
uplo.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
dim_to_mkl_int(nrhs)?,
ap.as_mut_ptr(),
ipiv.as_mut_ptr(),
b.as_mut_ptr(),
dim_to_mkl_int(ldb)?,
)
};
check_info(info)
}
pub fn sptrf<T: LapackScalar>(
layout: Layout,
uplo: UpLo,
n: usize,
ap: &mut [T],
ipiv: &mut [i32],
) -> Result<()> {
if ap.len() < n * (n + 1) / 2 {
return Err(Error::InvalidArgument(
"ap must have at least n*(n+1)/2 entries",
));
}
if ipiv.len() < n {
return Err(Error::InvalidArgument("ipiv must have at least n entries"));
}
let info = unsafe {
T::lapacke_sptrf(
layout.as_lapack(),
uplo.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
ap.as_mut_ptr(),
ipiv.as_mut_ptr(),
)
};
check_info(info)
}
#[allow(
clippy::too_many_arguments,
reason = "stable public surface: published since v0.1.0 with 8 arguments; changing the signature breaks consumers"
)]
pub fn sptrs<T: LapackScalar>(
layout: Layout,
uplo: UpLo,
n: usize,
nrhs: usize,
ap: &[T],
ipiv: &[i32],
b: &mut [T],
ldb: usize,
) -> Result<()> {
let info = unsafe {
T::lapacke_sptrs(
layout.as_lapack(),
uplo.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
dim_to_mkl_int(nrhs)?,
ap.as_ptr(),
ipiv.as_ptr(),
b.as_mut_ptr(),
dim_to_mkl_int(ldb)?,
)
};
check_info(info)
}
pub fn ppsv<T: LapackScalar>(
layout: Layout,
uplo: UpLo,
n: usize,
nrhs: usize,
ap: &mut [T],
b: &mut [T],
ldb: usize,
) -> Result<()> {
if ap.len() < n * (n + 1) / 2 {
return Err(Error::InvalidArgument(
"ap must have at least n*(n+1)/2 entries",
));
}
let info = unsafe {
T::lapacke_ppsv(
layout.as_lapack(),
uplo.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
dim_to_mkl_int(nrhs)?,
ap.as_mut_ptr(),
b.as_mut_ptr(),
dim_to_mkl_int(ldb)?,
)
};
check_info(info)
}
pub fn pptrf<T: LapackScalar>(layout: Layout, uplo: UpLo, n: usize, ap: &mut [T]) -> Result<()> {
if ap.len() < n * (n + 1) / 2 {
return Err(Error::InvalidArgument(
"ap must have at least n*(n+1)/2 entries",
));
}
let info = unsafe {
T::lapacke_pptrf(
layout.as_lapack(),
uplo.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
ap.as_mut_ptr(),
)
};
check_info(info)
}
pub fn pptrs<T: LapackScalar>(
layout: Layout,
uplo: UpLo,
n: usize,
nrhs: usize,
ap: &[T],
b: &mut [T],
ldb: usize,
) -> Result<()> {
let info = unsafe {
T::lapacke_pptrs(
layout.as_lapack(),
uplo.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
dim_to_mkl_int(nrhs)?,
ap.as_ptr(),
b.as_mut_ptr(),
dim_to_mkl_int(ldb)?,
)
};
check_info(info)
}
#[allow(
clippy::too_many_arguments,
reason = "stable public surface: published since v0.1.0 with 8 arguments; changing the signature breaks consumers"
)]
pub fn hpsv<T: ComplexLapackScalar>(
layout: Layout,
uplo: UpLo,
n: usize,
nrhs: usize,
ap: &mut [T],
ipiv: &mut [i32],
b: &mut [T],
ldb: usize,
) -> Result<()> {
if ap.len() < n * (n + 1) / 2 {
return Err(Error::InvalidArgument(
"ap must have at least n*(n+1)/2 entries",
));
}
if ipiv.len() < n {
return Err(Error::InvalidArgument("ipiv must have at least n entries"));
}
let info = unsafe {
T::lapacke_hpsv(
layout.as_lapack(),
uplo.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
dim_to_mkl_int(nrhs)?,
ap.as_mut_ptr(),
ipiv.as_mut_ptr(),
b.as_mut_ptr(),
dim_to_mkl_int(ldb)?,
)
};
check_info(info)
}
pub fn hptrf<T: ComplexLapackScalar>(
layout: Layout,
uplo: UpLo,
n: usize,
ap: &mut [T],
ipiv: &mut [i32],
) -> Result<()> {
if ap.len() < n * (n + 1) / 2 {
return Err(Error::InvalidArgument(
"ap must have at least n*(n+1)/2 entries",
));
}
if ipiv.len() < n {
return Err(Error::InvalidArgument("ipiv must have at least n entries"));
}
let info = unsafe {
T::lapacke_hptrf(
layout.as_lapack(),
uplo.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
ap.as_mut_ptr(),
ipiv.as_mut_ptr(),
)
};
check_info(info)
}
#[allow(
clippy::too_many_arguments,
reason = "stable public surface: published since v0.1.0 with 8 arguments; changing the signature breaks consumers"
)]
pub fn hptrs<T: ComplexLapackScalar>(
layout: Layout,
uplo: UpLo,
n: usize,
nrhs: usize,
ap: &[T],
ipiv: &[i32],
b: &mut [T],
ldb: usize,
) -> Result<()> {
let info = unsafe {
T::lapacke_hptrs(
layout.as_lapack(),
uplo.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
dim_to_mkl_int(nrhs)?,
ap.as_ptr(),
ipiv.as_ptr(),
b.as_mut_ptr(),
dim_to_mkl_int(ldb)?,
)
};
check_info(info)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum GeneralizedEigenType {
AxLambdaBx,
AbxLambdaX,
BaxLambdaX,
}
impl GeneralizedEigenType {
#[inline]
fn as_int(self) -> core::ffi::c_int {
match self {
Self::AxLambdaBx => 1,
Self::AbxLambdaX => 2,
Self::BaxLambdaX => 3,
}
}
}
pub fn sygv<T: RealLapackScalar>(
itype: GeneralizedEigenType,
jobz: Job,
uplo: UpLo,
a: &mut MatrixMut<'_, T>,
b: &mut MatrixMut<'_, T>,
w: &mut [T],
) -> Result<()> {
let n = ensure_square(a)?;
if b.rows() != n || b.cols() != n {
return Err(Error::InvalidArgument("B must have the same shape as A"));
}
let layout = ensure_layout(&[a.layout(), b.layout()])?;
if w.len() < n {
return Err(Error::InvalidArgument("w must have at least n entries"));
}
let info = unsafe {
T::lapacke_sygv(
layout.as_lapack(),
itype.as_int(),
jobz.as_char() as core::ffi::c_char,
uplo.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
a.as_mut_ptr(),
dim_to_mkl_int(a.leading_dim())?,
b.as_mut_ptr(),
dim_to_mkl_int(b.leading_dim())?,
w.as_mut_ptr(),
)
};
check_info(info)
}
pub fn hegv<T: ComplexLapackScalar>(
itype: GeneralizedEigenType,
jobz: Job,
uplo: UpLo,
a: &mut MatrixMut<'_, T>,
b: &mut MatrixMut<'_, T>,
w: &mut [T::Real],
) -> Result<()> {
let n = ensure_square(a)?;
if b.rows() != n || b.cols() != n {
return Err(Error::InvalidArgument("B must have the same shape as A"));
}
let layout = ensure_layout(&[a.layout(), b.layout()])?;
if w.len() < n {
return Err(Error::InvalidArgument("w must have at least n entries"));
}
let info = unsafe {
T::lapacke_hegv(
layout.as_lapack(),
itype.as_int(),
jobz.as_char() as core::ffi::c_char,
uplo.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
a.as_mut_ptr(),
dim_to_mkl_int(a.leading_dim())?,
b.as_mut_ptr(),
dim_to_mkl_int(b.leading_dim())?,
w.as_mut_ptr(),
)
};
check_info(info)
}
#[allow(
clippy::too_many_arguments,
reason = "stable public surface: published since v0.1.0 with 9 arguments; changing the signature breaks consumers"
)]
pub fn ggev_real<T: RealLapackScalar>(
jobvl: Job,
jobvr: Job,
a: &mut MatrixMut<'_, T>,
b: &mut MatrixMut<'_, T>,
alphar: &mut [T],
alphai: &mut [T],
beta: &mut [T],
vl: Option<&mut MatrixMut<'_, T>>,
vr: Option<&mut MatrixMut<'_, T>>,
) -> Result<()> {
let n = ensure_square(a)?;
if b.rows() != n || b.cols() != n {
return Err(Error::InvalidArgument("B must have the same shape as A"));
}
let layout = ensure_layout(&[a.layout(), b.layout()])?;
if alphar.len() < n || alphai.len() < n || beta.len() < n {
return Err(Error::InvalidArgument(
"alphar / alphai / beta must each have at least n entries",
));
}
let lda = dim_to_mkl_int(a.leading_dim())?;
let ldb = dim_to_mkl_int(b.leading_dim())?;
let (vl_ptr, ldvl) = match vl {
Some(m) => (m.as_mut_ptr(), dim_to_mkl_int(m.leading_dim())?),
None => (core::ptr::null_mut(), dim_to_mkl_int(n.max(1))?),
};
let (vr_ptr, ldvr) = match vr {
Some(m) => (m.as_mut_ptr(), dim_to_mkl_int(m.leading_dim())?),
None => (core::ptr::null_mut(), dim_to_mkl_int(n.max(1))?),
};
let info = unsafe {
T::lapacke_ggev(
layout.as_lapack(),
jobvl.as_char() as core::ffi::c_char,
jobvr.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
a.as_mut_ptr(),
lda,
b.as_mut_ptr(),
ldb,
alphar.as_mut_ptr(),
alphai.as_mut_ptr(),
beta.as_mut_ptr(),
vl_ptr,
ldvl,
vr_ptr,
ldvr,
)
};
check_info(info)
}
#[derive(Debug, Clone, Copy)]
pub enum EigenRange<R> {
All,
Values {
vl: R,
vu: R,
},
Indices {
il: i32,
iu: i32,
},
}
impl<R> EigenRange<R> {
fn as_char(&self) -> u8 {
match self {
Self::All => b'A',
Self::Values { .. } => b'V',
Self::Indices { .. } => b'I',
}
}
}
pub fn syevd<T: RealLapackScalar>(
jobz: Job,
uplo: UpLo,
a: &mut MatrixMut<'_, T>,
w: &mut [T],
) -> Result<()> {
let n = ensure_square(a)?;
if w.len() < n {
return Err(Error::InvalidArgument("w must have at least n entries"));
}
let info = unsafe {
T::lapacke_syevd(
a.layout().as_lapack(),
jobz.as_char() as core::ffi::c_char,
uplo.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
a.as_mut_ptr(),
dim_to_mkl_int(a.leading_dim())?,
w.as_mut_ptr(),
)
};
check_info(info)
}
pub fn heevd<T: ComplexLapackScalar>(
jobz: Job,
uplo: UpLo,
a: &mut MatrixMut<'_, T>,
w: &mut [T::Real],
) -> Result<()> {
let n = ensure_square(a)?;
if w.len() < n {
return Err(Error::InvalidArgument("w must have at least n entries"));
}
let info = unsafe {
T::lapacke_heevd(
a.layout().as_lapack(),
jobz.as_char() as core::ffi::c_char,
uplo.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
a.as_mut_ptr(),
dim_to_mkl_int(a.leading_dim())?,
w.as_mut_ptr(),
)
};
check_info(info)
}
#[derive(Debug, Clone)]
pub struct RrrEigResult<T, R> {
pub m: usize,
pub eigenvalues: Vec<R>,
pub eigenvectors: Vec<T>,
pub isuppz: Vec<i32>,
}
pub fn syevr<T: RealLapackScalar + Default>(
jobz: Job,
range: EigenRange<T>,
uplo: UpLo,
a: &mut MatrixMut<'_, T>,
abstol: T,
) -> Result<RrrEigResult<T, T>> {
let n = ensure_square(a)?;
let n_i = dim_to_mkl_int(n)?;
let lda = dim_to_mkl_int(a.leading_dim())?;
let layout = a.layout();
let range_char = range.as_char() as core::ffi::c_char;
let (vl, vu, il, iu) = match range {
EigenRange::All => (T::default(), T::default(), 1_i32, n.max(1) as i32),
EigenRange::Values { vl, vu } => (vl, vu, 1_i32, n.max(1) as i32),
EigenRange::Indices { il, iu } => (T::default(), T::default(), il, iu),
};
let il = dim_to_mkl_int(il.max(1) as usize)?;
let iu = dim_to_mkl_int(iu.max(1) as usize)?;
let want_z = matches!(jobz, Job::Compute);
let mut m_out: i32 = 0;
let mut w: Vec<T> = vec![T::default(); n.max(1)];
let mut z: Vec<T> = if want_z {
vec![T::default(); n.max(1) * n.max(1)]
} else {
Vec::new()
};
let mut isuppz: Vec<i32> = if want_z {
vec![0_i32; 2 * n.max(1)]
} else {
Vec::new()
};
let info = unsafe {
T::lapacke_syevr(
layout.as_lapack(),
jobz.as_char() as core::ffi::c_char,
range_char,
uplo.as_char() as core::ffi::c_char,
n_i,
a.as_mut_ptr(),
lda,
vl,
vu,
il,
iu,
abstol,
&mut m_out,
w.as_mut_ptr(),
if want_z {
z.as_mut_ptr()
} else {
core::ptr::null_mut()
},
dim_to_mkl_int(n.max(1))?,
if want_z {
isuppz.as_mut_ptr()
} else {
core::ptr::null_mut()
},
)
};
check_info(info)?;
let m = m_out.max(0) as usize;
w.truncate(m);
if want_z {
z.truncate(n.max(1) * m);
isuppz.truncate(2 * m);
}
Ok(RrrEigResult {
m,
eigenvalues: w,
eigenvectors: z,
isuppz,
})
}
pub fn heevr<T: ComplexLapackScalar + Default>(
jobz: Job,
range: EigenRange<T::Real>,
uplo: UpLo,
a: &mut MatrixMut<'_, T>,
abstol: T::Real,
) -> Result<RrrEigResult<T, T::Real>>
where
T::Real: Default,
{
let n = ensure_square(a)?;
let n_i = dim_to_mkl_int(n)?;
let lda = dim_to_mkl_int(a.leading_dim())?;
let layout = a.layout();
let range_char = range.as_char() as core::ffi::c_char;
let (vl, vu, il, iu) = match range {
EigenRange::All => (
T::Real::default(),
T::Real::default(),
1_i32,
n.max(1) as i32,
),
EigenRange::Values { vl, vu } => (vl, vu, 1_i32, n.max(1) as i32),
EigenRange::Indices { il, iu } => (T::Real::default(), T::Real::default(), il, iu),
};
let il = dim_to_mkl_int(il.max(1) as usize)?;
let iu = dim_to_mkl_int(iu.max(1) as usize)?;
let want_z = matches!(jobz, Job::Compute);
let mut m_out: i32 = 0;
let mut w: Vec<T::Real> = vec![T::Real::default(); n.max(1)];
let mut z: Vec<T> = if want_z {
vec![T::default(); n.max(1) * n.max(1)]
} else {
Vec::new()
};
let mut isuppz: Vec<i32> = if want_z {
vec![0_i32; 2 * n.max(1)]
} else {
Vec::new()
};
let info = unsafe {
T::lapacke_heevr(
layout.as_lapack(),
jobz.as_char() as core::ffi::c_char,
range_char,
uplo.as_char() as core::ffi::c_char,
n_i,
a.as_mut_ptr(),
lda,
vl,
vu,
il,
iu,
abstol,
&mut m_out,
w.as_mut_ptr(),
if want_z {
z.as_mut_ptr()
} else {
core::ptr::null_mut()
},
dim_to_mkl_int(n.max(1))?,
if want_z {
isuppz.as_mut_ptr()
} else {
core::ptr::null_mut()
},
)
};
check_info(info)?;
let m = m_out.max(0) as usize;
w.truncate(m);
if want_z {
z.truncate(n.max(1) * m);
isuppz.truncate(2 * m);
}
Ok(RrrEigResult {
m,
eigenvalues: w,
eigenvectors: z,
isuppz,
})
}
#[allow(
clippy::too_many_arguments,
reason = "stable public surface: published since v0.1.0 with 8 arguments; changing the signature breaks consumers"
)]
pub fn ggev_complex<T: ComplexLapackScalar>(
jobvl: Job,
jobvr: Job,
a: &mut MatrixMut<'_, T>,
b: &mut MatrixMut<'_, T>,
alpha: &mut [T],
beta: &mut [T],
vl: Option<&mut MatrixMut<'_, T>>,
vr: Option<&mut MatrixMut<'_, T>>,
) -> Result<()> {
let n = ensure_square(a)?;
if b.rows() != n || b.cols() != n {
return Err(Error::InvalidArgument("B must have the same shape as A"));
}
let layout = ensure_layout(&[a.layout(), b.layout()])?;
if alpha.len() < n || beta.len() < n {
return Err(Error::InvalidArgument(
"alpha / beta must each have at least n entries",
));
}
let lda = dim_to_mkl_int(a.leading_dim())?;
let ldb = dim_to_mkl_int(b.leading_dim())?;
let (vl_ptr, ldvl) = match vl {
Some(m) => (m.as_mut_ptr(), dim_to_mkl_int(m.leading_dim())?),
None => (core::ptr::null_mut(), dim_to_mkl_int(n.max(1))?),
};
let (vr_ptr, ldvr) = match vr {
Some(m) => (m.as_mut_ptr(), dim_to_mkl_int(m.leading_dim())?),
None => (core::ptr::null_mut(), dim_to_mkl_int(n.max(1))?),
};
let info = unsafe {
T::lapacke_ggev_complex(
layout.as_lapack(),
jobvl.as_char() as core::ffi::c_char,
jobvr.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
a.as_mut_ptr(),
lda,
b.as_mut_ptr(),
ldb,
alpha.as_mut_ptr(),
beta.as_mut_ptr(),
vl_ptr,
ldvl,
vr_ptr,
ldvr,
)
};
check_info(info)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum LacpyPart {
Upper,
Lower,
Full,
}
impl LacpyPart {
#[inline]
fn as_char(self) -> core::ffi::c_char {
match self {
Self::Upper => b'U' as _,
Self::Lower => b'L' as _,
Self::Full => b'A' as _,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MatrixNorm {
Max,
One,
Infinity,
Frobenius,
}
impl MatrixNorm {
#[inline]
fn as_char(self) -> core::ffi::c_char {
match self {
Self::Max => b'M' as _,
Self::One => b'1' as _,
Self::Infinity => b'I' as _,
Self::Frobenius => b'F' as _,
}
}
}
pub fn lacpy<T: LapackScalar>(
part: LacpyPart,
a: &MatrixRef<'_, T>,
b: &mut MatrixMut<'_, T>,
) -> Result<()> {
if a.rows() != b.rows() || a.cols() != b.cols() {
return Err(Error::InvalidArgument(
"source and destination must have the same shape",
));
}
let layout = ensure_layout(&[a.layout(), b.layout()])?;
let m = dim_to_mkl_int(a.rows())?;
let n = dim_to_mkl_int(a.cols())?;
let lda = dim_to_mkl_int(a.leading_dim())?;
let ldb = dim_to_mkl_int(b.leading_dim())?;
let info = unsafe {
T::lapacke_lacpy(
layout.as_lapack(),
part.as_char(),
m,
n,
a.as_ptr(),
lda,
b.as_mut_ptr(),
ldb,
)
};
check_info(info)
}
pub fn lange<T: LapackScalar>(norm: MatrixNorm, a: &MatrixRef<'_, T>) -> Result<T::Real> {
let m = dim_to_mkl_int(a.rows())?;
let n = dim_to_mkl_int(a.cols())?;
let lda = dim_to_mkl_int(a.leading_dim())?;
let value = unsafe {
T::lapacke_lange(
a.layout().as_lapack(),
norm.as_char(),
m,
n,
a.as_ptr(),
lda,
)
};
Ok(value)
}
pub fn gecon<T: LapackScalar>(
norm: MatrixNorm,
a: &MatrixRef<'_, T>,
anorm: T::Real,
) -> Result<T::Real>
where
T::Real: Default,
{
if a.rows() != a.cols() {
return Err(Error::InvalidArgument("gecon requires a square matrix"));
}
let n_i = dim_to_mkl_int(a.rows())?;
let lda = dim_to_mkl_int(a.leading_dim())?;
let mut rcond: T::Real = T::Real::default();
let info = unsafe {
T::lapacke_gecon(
a.layout().as_lapack(),
norm.as_char(),
n_i,
a.as_ptr(),
lda,
anorm,
&mut rcond,
)
};
check_info(info)?;
Ok(rcond)
}
pub fn laswp<T: LapackScalar>(
a: &mut MatrixMut<'_, T>,
k1: i32,
k2: i32,
ipiv: &[i32],
incx: i32,
) -> Result<()> {
let n = dim_to_mkl_int(a.cols())?;
let lda = dim_to_mkl_int(a.leading_dim())?;
if k1 < 1 || k2 < k1 || (k2 as usize) > ipiv.len() {
return Err(Error::InvalidArgument(
"k1, k2 must satisfy 1 ≤ k1 ≤ k2 ≤ ipiv.len()",
));
}
let info = unsafe {
T::lapacke_laswp(
a.layout().as_lapack(),
n,
a.as_mut_ptr(),
lda,
k1,
k2,
ipiv.as_ptr(),
incx,
)
};
check_info(info)
}
#[derive(Debug, Clone, Copy)]
pub struct HouseholderReflector<T> {
pub alpha: T,
pub tau: T,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct IterRefineOutcome {
pub iter: i32,
}
pub fn iter_refine_gesv_f64(
a: &mut MatrixMut<'_, f64>,
ipiv: &mut [i32],
b: &mut MatrixMut<'_, f64>,
x: &mut MatrixMut<'_, f64>,
) -> Result<IterRefineOutcome> {
let n = ensure_square(a)?;
let layout = ensure_layout(&[a.layout(), b.layout(), x.layout()])?;
if b.rows() != n {
return Err(Error::InvalidArgument("B must have n rows"));
}
if x.rows() != n || x.cols() != b.cols() {
return Err(Error::InvalidArgument("X must have the same shape as B"));
}
if ipiv.len() < n {
return Err(Error::InvalidArgument("ipiv must have at least n entries"));
}
let mut iter: core::ffi::c_int = 0;
let info = unsafe {
onemkl_sys::LAPACKE_dsgesv(
layout.as_lapack(),
dim_to_mkl_int(n)?,
dim_to_mkl_int(b.cols())?,
a.as_mut_ptr(),
dim_to_mkl_int(a.leading_dim())?,
ipiv.as_mut_ptr(),
b.as_mut_ptr(),
dim_to_mkl_int(b.leading_dim())?,
x.as_mut_ptr(),
dim_to_mkl_int(x.leading_dim())?,
&mut iter,
)
};
check_info(info)?;
Ok(IterRefineOutcome { iter })
}
pub fn iter_refine_posv_f64(
uplo: UpLo,
a: &mut MatrixMut<'_, f64>,
b: &mut MatrixMut<'_, f64>,
x: &mut MatrixMut<'_, f64>,
) -> Result<IterRefineOutcome> {
let n = ensure_square(a)?;
let layout = ensure_layout(&[a.layout(), b.layout(), x.layout()])?;
if b.rows() != n {
return Err(Error::InvalidArgument("B must have n rows"));
}
if x.rows() != n || x.cols() != b.cols() {
return Err(Error::InvalidArgument("X must have the same shape as B"));
}
let mut iter: core::ffi::c_int = 0;
let info = unsafe {
onemkl_sys::LAPACKE_dsposv(
layout.as_lapack(),
uplo.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
dim_to_mkl_int(b.cols())?,
a.as_mut_ptr(),
dim_to_mkl_int(a.leading_dim())?,
b.as_mut_ptr(),
dim_to_mkl_int(b.leading_dim())?,
x.as_mut_ptr(),
dim_to_mkl_int(x.leading_dim())?,
&mut iter,
)
};
check_info(info)?;
Ok(IterRefineOutcome { iter })
}
pub fn iter_refine_gesv_c64(
a: &mut MatrixMut<'_, num_complex::Complex64>,
ipiv: &mut [i32],
b: &mut MatrixMut<'_, num_complex::Complex64>,
x: &mut MatrixMut<'_, num_complex::Complex64>,
) -> Result<IterRefineOutcome> {
let n = ensure_square(a)?;
let layout = ensure_layout(&[a.layout(), b.layout(), x.layout()])?;
if b.rows() != n {
return Err(Error::InvalidArgument("B must have n rows"));
}
if x.rows() != n || x.cols() != b.cols() {
return Err(Error::InvalidArgument("X must have the same shape as B"));
}
if ipiv.len() < n {
return Err(Error::InvalidArgument("ipiv must have at least n entries"));
}
let mut iter: core::ffi::c_int = 0;
let info = unsafe {
onemkl_sys::LAPACKE_zcgesv(
layout.as_lapack(),
dim_to_mkl_int(n)?,
dim_to_mkl_int(b.cols())?,
a.as_mut_ptr().cast::<onemkl_sys::MKL_Complex16>(),
dim_to_mkl_int(a.leading_dim())?,
ipiv.as_mut_ptr(),
b.as_mut_ptr().cast::<onemkl_sys::MKL_Complex16>(),
dim_to_mkl_int(b.leading_dim())?,
x.as_mut_ptr().cast::<onemkl_sys::MKL_Complex16>(),
dim_to_mkl_int(x.leading_dim())?,
&mut iter,
)
};
check_info(info)?;
Ok(IterRefineOutcome { iter })
}
pub fn iter_refine_posv_c64(
uplo: UpLo,
a: &mut MatrixMut<'_, num_complex::Complex64>,
b: &mut MatrixMut<'_, num_complex::Complex64>,
x: &mut MatrixMut<'_, num_complex::Complex64>,
) -> Result<IterRefineOutcome> {
let n = ensure_square(a)?;
let layout = ensure_layout(&[a.layout(), b.layout(), x.layout()])?;
if b.rows() != n {
return Err(Error::InvalidArgument("B must have n rows"));
}
if x.rows() != n || x.cols() != b.cols() {
return Err(Error::InvalidArgument("X must have the same shape as B"));
}
let mut iter: core::ffi::c_int = 0;
let info = unsafe {
onemkl_sys::LAPACKE_zcposv(
layout.as_lapack(),
uplo.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
dim_to_mkl_int(b.cols())?,
a.as_mut_ptr().cast::<onemkl_sys::MKL_Complex16>(),
dim_to_mkl_int(a.leading_dim())?,
b.as_mut_ptr().cast::<onemkl_sys::MKL_Complex16>(),
dim_to_mkl_int(b.leading_dim())?,
x.as_mut_ptr().cast::<onemkl_sys::MKL_Complex16>(),
dim_to_mkl_int(x.leading_dim())?,
&mut iter,
)
};
check_info(info)?;
Ok(IterRefineOutcome { iter })
}
pub fn larfg<T: LapackScalar + Default>(
n: usize,
alpha: &mut T,
x: &mut [T],
incx: i32,
) -> Result<HouseholderReflector<T>> {
if n == 0 {
return Err(Error::InvalidArgument("n must be ≥ 1"));
}
if x.len() < n.saturating_sub(1) {
return Err(Error::InvalidArgument("x is too short for n - 1 elements"));
}
let n_i = dim_to_mkl_int(n)?;
let mut tau: T = T::default();
let info = unsafe { T::lapacke_larfg(n_i, alpha, x.as_mut_ptr(), incx, &mut tau) };
check_info(info)?;
Ok(HouseholderReflector { alpha: *alpha, tau })
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ReflectorDirection {
Forward,
Backward,
}
impl ReflectorDirection {
fn as_char(self) -> core::ffi::c_char {
(match self {
Self::Forward => b'F',
Self::Backward => b'B',
}) as core::ffi::c_char
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ReflectorStorage {
Columnwise,
Rowwise,
}
impl ReflectorStorage {
fn as_char(self) -> core::ffi::c_char {
(match self {
Self::Columnwise => b'C',
Self::Rowwise => b'R',
}) as core::ffi::c_char
}
}
pub fn larft<T: LapackScalar>(
direct: ReflectorDirection,
storev: ReflectorStorage,
v: &MatrixRef<'_, T>,
tau: &[T],
t: &mut MatrixMut<'_, T>,
) -> Result<()> {
let k = t.rows();
if t.cols() != k {
return Err(Error::InvalidArgument("T must be square (k x k)"));
}
if tau.len() < k {
return Err(Error::InvalidArgument("tau must have at least k entries"));
}
let n = match storev {
ReflectorStorage::Columnwise => {
if v.cols() != k {
return Err(Error::InvalidArgument("columnwise V must be n x k"));
}
v.rows()
}
ReflectorStorage::Rowwise => {
if v.rows() != k {
return Err(Error::InvalidArgument("rowwise V must be k x n"));
}
v.cols()
}
};
let layout = ensure_layout(&[v.layout(), t.layout()])?;
let info = unsafe {
T::lapacke_larft(
layout.as_lapack(),
direct.as_char(),
storev.as_char(),
dim_to_mkl_int(n)?,
dim_to_mkl_int(k)?,
v.as_ptr(),
dim_to_mkl_int(v.leading_dim())?,
tau.as_ptr(),
t.as_mut_ptr(),
dim_to_mkl_int(t.leading_dim())?,
)
};
check_info(info)
}
pub fn larfb<T: LapackScalar>(
side: Side,
trans: Transpose,
direct: ReflectorDirection,
storev: ReflectorStorage,
v: &MatrixRef<'_, T>,
t: &MatrixRef<'_, T>,
c: &mut MatrixMut<'_, T>,
) -> Result<()> {
let k = t.rows();
if t.cols() != k {
return Err(Error::InvalidArgument("T must be square (k x k)"));
}
let (order, vk) = match (storev, side) {
(ReflectorStorage::Columnwise, Side::Left) => (c.rows(), (v.rows(), v.cols())),
(ReflectorStorage::Columnwise, Side::Right) => (c.cols(), (v.rows(), v.cols())),
(ReflectorStorage::Rowwise, Side::Left) => (c.rows(), (v.cols(), v.rows())),
(ReflectorStorage::Rowwise, Side::Right) => (c.cols(), (v.cols(), v.rows())),
};
if vk != (order, k) {
return Err(Error::InvalidArgument(
"V must hold k reflectors of the order of the side C is multiplied on",
));
}
let layout = ensure_layout(&[v.layout(), t.layout(), c.layout()])?;
let side_c = match side {
Side::Left => b'L',
Side::Right => b'R',
} as core::ffi::c_char;
let info = unsafe {
T::lapacke_larfb(
layout.as_lapack(),
side_c,
trans.as_char() as core::ffi::c_char,
direct.as_char(),
storev.as_char(),
dim_to_mkl_int(c.rows())?,
dim_to_mkl_int(c.cols())?,
dim_to_mkl_int(k)?,
v.as_ptr(),
dim_to_mkl_int(v.leading_dim())?,
t.as_ptr(),
dim_to_mkl_int(t.leading_dim())?,
c.as_mut_ptr(),
dim_to_mkl_int(c.leading_dim())?,
)
};
check_info(info)
}
#[allow(missing_docs)]
pub trait ComplexAuxScalar: Copy + 'static {
type Real: Copy + 'static;
unsafe fn lapacke_lacgv(n: core::ffi::c_int, x: *mut Self, incx: core::ffi::c_int) -> i32;
#[allow(
clippy::too_many_arguments,
reason = "mirrors the oneMKL C signature LAPACKE_?lacrm argument for argument"
)]
unsafe fn lapacke_lacrm(
layout: core::ffi::c_int,
m: core::ffi::c_int,
n: core::ffi::c_int,
a: *const Self,
lda: core::ffi::c_int,
b: *const Self::Real,
ldb: core::ffi::c_int,
c: *mut Self,
ldc: core::ffi::c_int,
) -> i32;
}
macro_rules! impl_complex_aux {
($c:ty, $r:ty, $ct:ty, $lacgv:ident, $lacrm:ident) => {
impl ComplexAuxScalar for $c {
type Real = $r;
unsafe fn lapacke_lacgv(
n: core::ffi::c_int,
x: *mut Self,
incx: core::ffi::c_int,
) -> i32 {
unsafe { onemkl_sys::$lacgv(n, x.cast::<$ct>(), incx) }
}
unsafe fn lapacke_lacrm(
layout: core::ffi::c_int,
m: core::ffi::c_int,
n: core::ffi::c_int,
a: *const Self,
lda: core::ffi::c_int,
b: *const $r,
ldb: core::ffi::c_int,
c: *mut Self,
ldc: core::ffi::c_int,
) -> i32 {
unsafe {
onemkl_sys::$lacrm(
layout,
m,
n,
a.cast::<$ct>(),
lda,
b,
ldb,
c.cast::<$ct>(),
ldc,
)
}
}
}
};
}
impl_complex_aux!(
num_complex::Complex32,
f32,
onemkl_sys::MKL_Complex8,
LAPACKE_clacgv,
LAPACKE_clacrm
);
impl_complex_aux!(
num_complex::Complex64,
f64,
onemkl_sys::MKL_Complex16,
LAPACKE_zlacgv,
LAPACKE_zlacrm
);
pub fn lacgv<T: ComplexAuxScalar>(n: usize, x: &mut [T], incx: i32) -> Result<()> {
if incx == 0 {
return Err(Error::InvalidArgument("incx must be non-zero"));
}
if n > 0 && x.len() < 1 + (n - 1) * incx.unsigned_abs() as usize {
return Err(Error::InvalidArgument(
"x is too short for n elements at stride incx",
));
}
let info = unsafe { T::lapacke_lacgv(dim_to_mkl_int(n)?, x.as_mut_ptr(), incx) };
check_info(info)
}
pub fn lacrm<T: ComplexAuxScalar>(
a: &MatrixRef<'_, T>,
b: &MatrixRef<'_, T::Real>,
c: &mut MatrixMut<'_, T>,
) -> Result<()> {
let (m, n) = (a.rows(), a.cols());
if b.rows() != n || b.cols() != n {
return Err(Error::InvalidArgument("B must be n x n for an m x n A"));
}
if c.rows() != m || c.cols() != n {
return Err(Error::InvalidArgument("C must have the shape of A"));
}
let layout = ensure_layout(&[a.layout(), b.layout(), c.layout()])?;
let info = unsafe {
T::lapacke_lacrm(
layout.as_lapack(),
dim_to_mkl_int(m)?,
dim_to_mkl_int(n)?,
a.as_ptr(),
dim_to_mkl_int(a.leading_dim())?,
b.as_ptr(),
dim_to_mkl_int(b.leading_dim())?,
c.as_mut_ptr(),
dim_to_mkl_int(c.leading_dim())?,
)
};
check_info(info)
}
#[allow(missing_docs)]
pub trait SymConvScalar: Copy + 'static {
unsafe fn lapacke_sytrf(
layout: core::ffi::c_int,
uplo: core::ffi::c_char,
n: core::ffi::c_int,
a: *mut Self,
lda: core::ffi::c_int,
ipiv: *mut core::ffi::c_int,
) -> i32;
#[allow(
clippy::too_many_arguments,
reason = "mirrors the oneMKL C signature LAPACKE_?syconv argument for argument"
)]
unsafe fn lapacke_syconv(
layout: core::ffi::c_int,
uplo: core::ffi::c_char,
way: core::ffi::c_char,
n: core::ffi::c_int,
a: *mut Self,
lda: core::ffi::c_int,
ipiv: *const core::ffi::c_int,
e: *mut Self,
) -> i32;
}
macro_rules! impl_symconv {
($t:ty, $ct:ty, $sytrf:ident, $syconv:ident) => {
impl SymConvScalar for $t {
unsafe fn lapacke_sytrf(
layout: core::ffi::c_int,
uplo: core::ffi::c_char,
n: core::ffi::c_int,
a: *mut Self,
lda: core::ffi::c_int,
ipiv: *mut core::ffi::c_int,
) -> i32 {
unsafe { onemkl_sys::$sytrf(layout, uplo, n, a.cast::<$ct>(), lda, ipiv) }
}
unsafe fn lapacke_syconv(
layout: core::ffi::c_int,
uplo: core::ffi::c_char,
way: core::ffi::c_char,
n: core::ffi::c_int,
a: *mut Self,
lda: core::ffi::c_int,
ipiv: *const core::ffi::c_int,
e: *mut Self,
) -> i32 {
unsafe {
onemkl_sys::$syconv(
layout,
uplo,
way,
n,
a.cast::<$ct>(),
lda,
ipiv,
e.cast::<$ct>(),
)
}
}
}
};
}
impl_symconv!(f32, f32, LAPACKE_ssytrf, LAPACKE_ssyconv);
impl_symconv!(f64, f64, LAPACKE_dsytrf, LAPACKE_dsyconv);
impl_symconv!(
num_complex::Complex32,
onemkl_sys::MKL_Complex8,
LAPACKE_csytrf,
LAPACKE_csyconv
);
impl_symconv!(
num_complex::Complex64,
onemkl_sys::MKL_Complex16,
LAPACKE_zsytrf,
LAPACKE_zsyconv
);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SyconvWay {
Convert,
Revert,
}
pub fn sytrf<T: SymConvScalar>(
uplo: UpLo,
a: &mut MatrixMut<'_, T>,
ipiv: &mut [i32],
) -> Result<()> {
let n = a.rows();
if a.cols() != n {
return Err(Error::InvalidArgument("A must be square"));
}
if ipiv.len() < n {
return Err(Error::InvalidArgument("ipiv must have at least n entries"));
}
let info = unsafe {
T::lapacke_sytrf(
a.layout().as_lapack(),
uplo.as_char() as core::ffi::c_char,
dim_to_mkl_int(n)?,
a.as_mut_ptr(),
dim_to_mkl_int(a.leading_dim())?,
ipiv.as_mut_ptr(),
)
};
check_info(info)
}
pub fn syconv<T: SymConvScalar>(
uplo: UpLo,
way: SyconvWay,
a: &mut MatrixMut<'_, T>,
ipiv: &[i32],
e: &mut [T],
) -> Result<()> {
let n = a.rows();
if a.cols() != n {
return Err(Error::InvalidArgument("A must be square"));
}
if ipiv.len() < n {
return Err(Error::InvalidArgument("ipiv must have at least n entries"));
}
if e.len() < n {
return Err(Error::InvalidArgument("e must have at least n entries"));
}
let way = match way {
SyconvWay::Convert => b'C',
SyconvWay::Revert => b'R',
};
let info = unsafe {
T::lapacke_syconv(
a.layout().as_lapack(),
uplo.as_char() as core::ffi::c_char,
way as core::ffi::c_char,
dim_to_mkl_int(n)?,
a.as_mut_ptr(),
dim_to_mkl_int(a.leading_dim())?,
ipiv.as_ptr(),
e.as_mut_ptr(),
)
};
check_info(info)
}