ndarray-linalg icon indicating copy to clipboard operation
ndarray-linalg copied to clipboard

Adding an Adjoint trait for operators

Open cjblocker opened this issue 5 years ago • 1 comments

I am writing an optimization library for which I would like several of the routine's inputs to be generic LinearOperators. I was happy to find that this trait abstraction was already provided in ndarray-linalg, but some of my inputs additionally require that I can take an adjoint/hermitian transpose.

I've implemented this trait and its function on Array2, Diagonal, as well as my own LinearOperators (Identity, DFT), but as adjoints are a linear algebra concept, and linear operators in other languages often come with this functionality, I thought I'd see if there was any interest in a pull request.

Here is some example code. I could add into_adj() or adj_mut() if necessary.

pub trait Adjoint<'a>
{
    type Output;
    fn adj(&'a self) -> Self::Output;
}


impl<'a, A, S> Adjoint<'a> for ArrayBase<S, Ix2>
where
    A: 'a + Float,  // just the real case
    S: Data<Elem = A>,
{
    type Output = ArrayView<'a, A, Ix2>;

    fn adj(&'a self) ->  Self::Output {
        self.t()
    }
}

cjblocker avatar Jan 30 '20 00:01 cjblocker

How this issue currently being addressed? The above code doesn't seem to follow the case of complex numbers, but I think it can be easily extended by applying Scalar::conj() to each element. If this trait is not implemented yet, I will send PR, but I'd appreciate it if you could tell me in which mod I should implement it (including the case of creating a new mod file).

doraneko94 avatar Jul 16 '21 00:07 doraneko94