pub trait MatrixErrorType {
type Error: std::error::Error;
}
impl<M: MatrixErrorType + ?Sized> MatrixErrorType for &M {
type Error = M::Error;
}
pub trait MatrixShape {
fn nrows(&self) -> usize;
fn ncols(&self) -> usize;
}
pub trait MatVec<V>: MatrixShape + MatrixErrorType {
fn matvec(&self, x: &V) -> Result<V, Self::Error>;
}
pub trait MatVecInto<X, Y = X>: MatrixShape + MatrixErrorType {
fn matvec_into(&self, x: &X, out: &mut Y) -> Result<(), Self::Error>;
}
pub trait MatVecScaledInto<X, Y, F>: MatrixShape + MatrixErrorType {
fn matvec_scaled_into(&self, alpha: F, x: &X, beta: F, out: &mut Y) -> Result<(), Self::Error>;
}
pub trait MatTransposeVec<V>: MatrixShape + MatrixErrorType {
fn mat_transpose_vec(&self, x: &V) -> Result<V, Self::Error>;
}
pub trait MatTransposeVecInto<X, Y = X>: MatrixShape + MatrixErrorType {
fn mat_transpose_vec_into(&self, x: &X, out: &mut Y) -> Result<(), Self::Error>;
}
pub trait MatTransposeVecScaledInto<X, Y, F>: MatrixShape + MatrixErrorType {
fn mat_transpose_vec_scaled_into(
&self,
alpha: F,
x: &X,
beta: F,
out: &mut Y,
) -> Result<(), Self::Error>;
}
pub(crate) fn scale_output<F: super::Scalar, Y: super::VectorViewMut<F> + ?Sized>(
beta: F,
out: &mut Y,
) {
if beta == F::zero() {
for i in 0..out.len() {
out.set(i, F::zero());
}
} else if beta != F::one() {
for i in 0..out.len() {
out.set(i, beta * out.get(i));
}
}
}
impl<M: MatrixShape + ?Sized> MatrixShape for &M {
fn nrows(&self) -> usize {
(**self).nrows()
}
fn ncols(&self) -> usize {
(**self).ncols()
}
}
impl<M, V> MatVec<V> for &M
where
M: MatVec<V> + ?Sized,
{
fn matvec(&self, x: &V) -> Result<V, Self::Error> {
(**self).matvec(x)
}
}
impl<M, X, Y> MatVecInto<X, Y> for &M
where
M: MatVecInto<X, Y> + ?Sized,
{
fn matvec_into(&self, x: &X, out: &mut Y) -> Result<(), Self::Error> {
(**self).matvec_into(x, out)
}
}
impl<M, X, Y, F> MatVecScaledInto<X, Y, F> for &M
where
M: MatVecScaledInto<X, Y, F> + ?Sized,
{
fn matvec_scaled_into(&self, alpha: F, x: &X, beta: F, out: &mut Y) -> Result<(), Self::Error> {
(**self).matvec_scaled_into(alpha, x, beta, out)
}
}
impl<M, V> MatTransposeVec<V> for &M
where
M: MatTransposeVec<V> + ?Sized,
{
fn mat_transpose_vec(&self, x: &V) -> Result<V, Self::Error> {
(**self).mat_transpose_vec(x)
}
}
impl<M, X, Y> MatTransposeVecInto<X, Y> for &M
where
M: MatTransposeVecInto<X, Y> + ?Sized,
{
fn mat_transpose_vec_into(&self, x: &X, out: &mut Y) -> Result<(), Self::Error> {
(**self).mat_transpose_vec_into(x, out)
}
}
impl<M, X, Y, F> MatTransposeVecScaledInto<X, Y, F> for &M
where
M: MatTransposeVecScaledInto<X, Y, F> + ?Sized,
{
fn mat_transpose_vec_scaled_into(
&self,
alpha: F,
x: &X,
beta: F,
out: &mut Y,
) -> Result<(), Self::Error> {
(**self).mat_transpose_vec_scaled_into(alpha, x, beta, out)
}
}