math icon indicating copy to clipboard operation
math copied to clipboard

Extend our Eigen plugin to support matrices of complex<var>

Open WardBrian opened this issue 3 years ago • 0 comments

Description

This follows on from the work described in #1197 and is necessary for #2740.

In order to allow the computation of derivatives of complex-matrix-valued functions, we need versions of the .var() and .adj() for Matrix<std::complex<var>, R, C>. These should both return Matrix<std::complex<double>, R, C>.

For a use case, see https://github.com/WardBrian/math/tree/fft-derivatives, my attempt at #2740

The reverse mode callback for something like FFT is:

  arena_t<V> arena_v = v;
  arena_t<V> res = fft(arena_v.val());

  reverse_pass_callback(
      [arena_v, res]() mutable { arena_v.adj() += inv_fft(res.adj()); });

However, this currently does not compile.

It is currently possible to do e.g., mat.real().adj(). This should be sufficient for assignment, but it is not sufficient for things like calling the function inv_fft, unless we had a function which combined two Matrix<double, R, C>s into a Matrix<complex<double>, R, C>, like a vectorized to_complex. I am not sure which would be more efficient.

I've discussed with @SteveBronder, and it seems like it may be easiest to do assignment piecewise like this, and have the complex-valued .val() and .adj() not be assignable. This should be sufficient.

I've tried this myself but could not really get anywhere. I've written a test which I'm hoping would pass: https://github.com/WardBrian/math/commit/8b12f4c40a8881a67a5be7204d850dd5313e65e0 and I'd appreciate help from @andrjohns if possible

Current Version:

v4.3.2

WardBrian avatar Jun 02 '22 14:06 WardBrian