Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] NumPy's asarray wrong casting when evoked from SciPy's integrate.solve_ivp #8453

Open
homocomputeris opened this issue Feb 21, 2018 · 6 comments
Labels
enhancement A new feature or improvement scipy.integrate

Comments

@homocomputeris
Copy link

homocomputeris commented Feb 21, 2018

As RK45 supports complex numbers, it should be able to solve a simple Schrödinger equation, right?

It seems that solve_ivp cannot automatically detect than the propagator's type is complex, which will result in a complex vector after the 1st iteration, and then the solver proceeds as if it were still real.

Reproducing code example:

import numpy as np
import scipy as sp

dim = 8
shp = (dim, dim)
# a complex Hermitian matrix
R = np.random.rand(dim, dim) + 1j*np.random.rand(dim, dim)
H = R + R.conj().T
# a real vector
psi = np.random.rand(dim)
# this makes integration possible:
# psi = np.asarray(psi, dtype=np.complex128)


def Hf(t, y):
    return -1j*H@y


sol = sp.integrate.solve_ivp(Hf, (0, 1), psi).y[:, -1]
exact = sp.linalg.expm(-1j*H)@psi

difference = sol - exact

Error message:

/usr/lib/python3.6/site-packages/numpy/core/numeric.py:492: ComplexWarning: Casting complex values to real discards the imaginary part
  return array(a, dtype, copy=False, order=order)

Scipy/Numpy/Python version information:

1.0.0 1.14.0 sys.version_info(major=3, minor=6, micro=4, releaselevel='final', serial=0)
@homocomputeris
Copy link
Author

homocomputeris commented Feb 21, 2018

Trace's interesting part:

  File "/home/nik/Documents/Code/GniPy/scipy_bug.py", line 36, in <module>
    sol = sp.integrate.solve_ivp(Hf, (0, 1), psi).y[:, -1]
  File "/usr/lib/python3.6/site-packages/scipy/integrate/_ivp/ivp.py", line 455, in solve_ivp
    solver = method(fun, t0, y0, tf, vectorized=vectorized, **options)
  File "/usr/lib/python3.6/site-packages/scipy/integrate/_ivp/rk.py", line 99, in __init__
    self.f = self.fun(self.t, self.y)
  File "/usr/lib/python3.6/site-packages/scipy/integrate/_ivp/base.py", line 139, in fun
    return self.fun_single(t, y)
  File "/usr/lib/python3.6/site-packages/scipy/integrate/_ivp/base.py", line 21, in fun_wrapped
    return np.asarray(fun(t, y), dtype=dtype)
  File "/usr/lib/python3.6/site-packages/numpy/core/numeric.py", line 492, in asarray
    return array(a, dtype, copy=False, order=order)
  File "/usr/lib/python3.6/warnings.py", line 99, in _showwarnmsg
    msg.file, msg.line)
  File "/home/nik/Documents/Code/GniPy/krypy_bug.py", line 18, in warn_with_traceback
    traceback.print_stack(file=log)
/usr/lib/python3.6/site-packages/numpy/core/numeric.py:492: ComplexWarning: Casting complex values to real discards the imaginary part
  return array(a, dtype, copy=False, order=order)

@homocomputeris homocomputeris changed the title NumPy's asarray wrong casting when evoked from SciPy's integrate.solve_ivp [Bug] NumPy's asarray wrong casting when evoked from SciPy's integrate.solve_ivp Feb 21, 2018
@pv
Copy link
Member

pv commented Feb 21, 2018 via email

@homocomputeris
Copy link
Author

Wouldn't it be better to use numpy.common_type(y0, H(0, y0)) or whatever? What are the problems it may cause?
I think automatic type definition can be expected. We write programs to offload tasks from the brain, not to create more.

@drhagen
Copy link
Contributor

drhagen commented Apr 11, 2018

The feature to support complex-valued solvers was proposed in the development repository, but never implemented.

@derdotte
Copy link

derdotte commented Nov 28, 2022

I have recently come across this problem aswell. This time with the dirac equation. This should definitely get a revisit and perhaps the current status quo should be reevaluated.

EDIT: 7 December:
A friend of mine told me just a few hours ago that he had a problem with wrong casting aswell while solving his system (this time it was a free basis expansion of the schrödinger equation for calculating hyperfinestructure transition probabilities). This really needs a reevaluation. Might need a better error message if the behavior isnt going to change, might need to be more prominant in the documentation. Eitherway this is definitely not how a python library should operate, after all a python library user should mostly not need to care about inputing the correct combination of types such that the expected operation of the function is achieved.

@j-bowhay j-bowhay added the defect A clear bug or issue that prevents SciPy from being installed or used as expected label Nov 28, 2022
@mdhaber
Copy link
Contributor

mdhaber commented Jan 23, 2023

Just thought I'd mention a relevant discussion on the mailing list.

@mdhaber mdhaber added enhancement A new feature or improvement and removed defect A clear bug or issue that prevents SciPy from being installed or used as expected labels Jan 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A new feature or improvement scipy.integrate
Projects
None yet
Development

No branches or pull requests

7 participants