question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Solving with complex initialization

See original GitHub issue

When trying to run a basic example:

from diffrax import diffeqsolve, ODETerm, Dopri5
import jax.numpy as jnp
from jax.lax import cond


Array.set_default_backend('jax')
def f(t, y, args):
    return -y

term = ODETerm(f)
solver = Dopri5()
y0 = jnp.array([2., 3.], dtype=complex)
solution = diffeqsolve(term, solver, t0=0, t1=1, dt0=0.1, y0=y0)

I receive this warning: ComplexWarning: Casting complex values to real discards the imaginary part, which goes away if I change the type of y0 to float.

Moreover, when running something else with a complex y0, there was an error TypeError: nextafter does not accept dtype complex64 at position 0. Accepted dtypes at position 0 are subtypes of floating.

Does diffrax not support complex y0s? If so is there any plan to enable this? Although we can convert the complex elements to 2d arrays, it would make the process a lot smoother if we could just use complex elements.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Phionxcommented, Nov 7, 2022

What’s the status of supporting complex dtypes in diffrax? I see that PR #112 was closed before merging.


BTW (in case it helps anyone looking at this thread), as a temporary workaround, I used the following isomorphism [1] between complex-valued and real matrices/vectors:

Screenshot 2022-11-07 at 8 28 03 AM

That said, it seems that this is less optimal than supporting complex dtypes directly.

[1] N. Leung, M. Abdelhafez, J. Koch, and D. Schuster, Speedup for quantum optimal control from automatic differentiation based on graphics processing units, Phys. Rev. A 95, 042318 (2017).

1reaction
patrick-kidgercommented, Jun 7, 2022

I haven’t looked into this so I don’t know how many places might need changing. If you’re happy to dig into this I’d still be happy to accept a PR on this. (Including tests, to be sure that this doesn’t revert in the future.) My expectation is that it’s probably just a few lines that need tweaking.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Initialize const members using complex function in C++ class
One way to solve setting const memebers with complex behavioour is to initialize them using a functoin. In your case if I understand ......
Read more >
Complex initialization for a const variable - Herb Sutter
I have seen in a lot of places code like int i; if(someConditionIstrue) { Do some operations and calculate the value of i;...
Read more >
C++ Program to initialize and print a complex number
In the first example, we use the constructor to assign values to the real and the imaginary part of the complex number as...
Read more >
Initialization of complex types (C11) - IBM
When the C11 complex initialization feature is enabled, you can initialize C99 complex types with a value of the form x + yi...
Read more >
Program to Add Two Complex Numbers - GeeksforGeeks
Here the values of real and imaginary numbers are passed while calling the parameterized constructor and, with the help of a default(empty) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found