dual.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. """
  2. .. deprecated:: 1.20
  3. *This module is deprecated. Instead of importing functions from*
  4. ``numpy.dual``, *the functions should be imported directly from NumPy
  5. or SciPy*.
  6. Aliases for functions which may be accelerated by SciPy.
  7. SciPy_ can be built to use accelerated or otherwise improved libraries
  8. for FFTs, linear algebra, and special functions. This module allows
  9. developers to transparently support these accelerated functions when
  10. SciPy is available but still support users who have only installed
  11. NumPy.
  12. .. _SciPy : https://www.scipy.org
  13. """
  14. import warnings
  15. warnings.warn('The module numpy.dual is deprecated. Instead of using dual, '
  16. 'use the functions directly from numpy or scipy.',
  17. category=DeprecationWarning,
  18. stacklevel=2)
  19. # This module should be used for functions both in numpy and scipy if
  20. # you want to use the numpy version if available but the scipy version
  21. # otherwise.
  22. # Usage --- from numpy.dual import fft, inv
  23. __all__ = ['fft', 'ifft', 'fftn', 'ifftn', 'fft2', 'ifft2',
  24. 'norm', 'inv', 'svd', 'solve', 'det', 'eig', 'eigvals',
  25. 'eigh', 'eigvalsh', 'lstsq', 'pinv', 'cholesky', 'i0']
  26. import numpy.linalg as linpkg
  27. import numpy.fft as fftpkg
  28. from numpy.lib import i0
  29. import sys
  30. fft = fftpkg.fft
  31. ifft = fftpkg.ifft
  32. fftn = fftpkg.fftn
  33. ifftn = fftpkg.ifftn
  34. fft2 = fftpkg.fft2
  35. ifft2 = fftpkg.ifft2
  36. norm = linpkg.norm
  37. inv = linpkg.inv
  38. svd = linpkg.svd
  39. solve = linpkg.solve
  40. det = linpkg.det
  41. eig = linpkg.eig
  42. eigvals = linpkg.eigvals
  43. eigh = linpkg.eigh
  44. eigvalsh = linpkg.eigvalsh
  45. lstsq = linpkg.lstsq
  46. pinv = linpkg.pinv
  47. cholesky = linpkg.cholesky
  48. _restore_dict = {}
  49. def register_func(name, func):
  50. if name not in __all__:
  51. raise ValueError("{} not a dual function.".format(name))
  52. f = sys._getframe(0).f_globals
  53. _restore_dict[name] = f[name]
  54. f[name] = func
  55. def restore_func(name):
  56. if name not in __all__:
  57. raise ValueError("{} not a dual function.".format(name))
  58. try:
  59. val = _restore_dict[name]
  60. except KeyError:
  61. return
  62. else:
  63. sys._getframe(0).f_globals[name] = val
  64. def restore_all():
  65. for name in _restore_dict.keys():
  66. restore_func(name)