stan-dev / stan-dev/math

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

Open
#2,747 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
C++
Stars
839
Forks
220
Avg merge
2d 4h
Merged PRs (30d)
14

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the Eigen plugin's existing .var() and .adj() implementations for matrix types, then inspect the test from commit 8b12f4c40a8881a67a5be7204d850dd5313e65e0. Confirm how complex matrix values and adjoints are assigned and run the relevant Eigen plugin tests. Done means complex Matrix<std::complex, R, C> supports both methods and the test passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend-api-design
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.