Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- data/miniconda3/lib/python3.13/site-packages/scipy/_lib/__pycache__/__init__.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/_lib/__pycache__/_elementwise_iterative_method.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/_lib/__pycache__/_sparse.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/_lib/__pycache__/_tmpdirs.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/_lib/__pycache__/_util.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/_lib/__pycache__/deprecation.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/_lib/__pycache__/doccer.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/_lib/_uarray/LICENSE +29 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/_lib/_uarray/__init__.py +116 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/_lib/_uarray/_backend.py +707 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/fft/tests/__pycache__/mock_backend.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/fft/tests/__pycache__/test_backend.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/fft/tests/__pycache__/test_basic.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/fft/tests/__pycache__/test_fftlog.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/fft/tests/__pycache__/test_helper.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/fft/tests/__pycache__/test_multithreading.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/fft/tests/__pycache__/test_real_transforms.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/__init__.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_basic.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_decomp.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_decomp_cholesky.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_decomp_cossin.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_decomp_ldl.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_decomp_lu.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_decomp_polar.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_decomp_qr.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_decomp_qz.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_decomp_schur.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_decomp_svd.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_expm_frechet.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_matfuncs.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_matfuncs_inv_ssq.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_matfuncs_sqrtm.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_misc.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_procrustes.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_sketches.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_solvers.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_special_matrices.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_testutils.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/basic.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/blas.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/decomp.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/decomp_cholesky.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/decomp_lu.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/decomp_qr.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/decomp_schur.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/decomp_svd.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/interpolative.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/lapack.cpython-313.pyc +0 -0
- data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/matfuncs.cpython-313.pyc +0 -0
data/miniconda3/lib/python3.13/site-packages/scipy/_lib/__pycache__/__init__.cpython-313.pyc
ADDED
|
Binary file (556 Bytes). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/_lib/__pycache__/_elementwise_iterative_method.cpython-313.pyc
ADDED
|
Binary file (14.6 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/_lib/__pycache__/_sparse.cpython-313.pyc
ADDED
|
Binary file (1.26 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/_lib/__pycache__/_tmpdirs.cpython-313.pyc
ADDED
|
Binary file (3.05 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/_lib/__pycache__/_util.cpython-313.pyc
ADDED
|
Binary file (56 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/_lib/__pycache__/deprecation.cpython-313.pyc
ADDED
|
Binary file (11.1 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/_lib/__pycache__/doccer.cpython-313.pyc
ADDED
|
Binary file (12.1 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/_lib/_uarray/LICENSE
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
BSD 3-Clause License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2018, Quansight-Labs
|
| 4 |
+
All rights reserved.
|
| 5 |
+
|
| 6 |
+
Redistribution and use in source and binary forms, with or without
|
| 7 |
+
modification, are permitted provided that the following conditions are met:
|
| 8 |
+
|
| 9 |
+
* Redistributions of source code must retain the above copyright notice, this
|
| 10 |
+
list of conditions and the following disclaimer.
|
| 11 |
+
|
| 12 |
+
* Redistributions in binary form must reproduce the above copyright notice,
|
| 13 |
+
this list of conditions and the following disclaimer in the documentation
|
| 14 |
+
and/or other materials provided with the distribution.
|
| 15 |
+
|
| 16 |
+
* Neither the name of the copyright holder nor the names of its
|
| 17 |
+
contributors may be used to endorse or promote products derived from
|
| 18 |
+
this software without specific prior written permission.
|
| 19 |
+
|
| 20 |
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
| 21 |
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
| 22 |
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
| 23 |
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
| 24 |
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
| 25 |
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
| 26 |
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
| 27 |
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
| 28 |
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| 29 |
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/miniconda3/lib/python3.13/site-packages/scipy/_lib/_uarray/__init__.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
.. note:
|
| 3 |
+
If you are looking for overrides for NumPy-specific methods, see the
|
| 4 |
+
documentation for :obj:`unumpy`. This page explains how to write
|
| 5 |
+
back-ends and multimethods.
|
| 6 |
+
|
| 7 |
+
``uarray`` is built around a back-end protocol, and overridable multimethods.
|
| 8 |
+
It is necessary to define multimethods for back-ends to be able to override them.
|
| 9 |
+
See the documentation of :obj:`generate_multimethod` on how to write multimethods.
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
Let's start with the simplest:
|
| 14 |
+
|
| 15 |
+
``__ua_domain__`` defines the back-end *domain*. The domain consists of period-
|
| 16 |
+
separated string consisting of the modules you extend plus the submodule. For
|
| 17 |
+
example, if a submodule ``module2.submodule`` extends ``module1``
|
| 18 |
+
(i.e., it exposes dispatchables marked as types available in ``module1``),
|
| 19 |
+
then the domain string should be ``"module1.module2.submodule"``.
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
For the purpose of this demonstration, we'll be creating an object and setting
|
| 23 |
+
its attributes directly. However, note that you can use a module or your own type
|
| 24 |
+
as a backend as well.
|
| 25 |
+
|
| 26 |
+
>>> class Backend: pass
|
| 27 |
+
>>> be = Backend()
|
| 28 |
+
>>> be.__ua_domain__ = "ua_examples"
|
| 29 |
+
|
| 30 |
+
It might be useful at this point to sidetrack to the documentation of
|
| 31 |
+
:obj:`generate_multimethod` to find out how to generate a multimethod
|
| 32 |
+
overridable by :obj:`uarray`. Needless to say, writing a backend and
|
| 33 |
+
creating multimethods are mostly orthogonal activities, and knowing
|
| 34 |
+
one doesn't necessarily require knowledge of the other, although it
|
| 35 |
+
is certainly helpful. We expect core API designers/specifiers to write the
|
| 36 |
+
multimethods, and implementors to override them. But, as is often the case,
|
| 37 |
+
similar people write both.
|
| 38 |
+
|
| 39 |
+
Without further ado, here's an example multimethod:
|
| 40 |
+
|
| 41 |
+
>>> import uarray as ua
|
| 42 |
+
>>> from uarray import Dispatchable
|
| 43 |
+
>>> def override_me(a, b):
|
| 44 |
+
... return Dispatchable(a, int),
|
| 45 |
+
>>> def override_replacer(args, kwargs, dispatchables):
|
| 46 |
+
... return (dispatchables[0], args[1]), {}
|
| 47 |
+
>>> overridden_me = ua.generate_multimethod(
|
| 48 |
+
... override_me, override_replacer, "ua_examples"
|
| 49 |
+
... )
|
| 50 |
+
|
| 51 |
+
Next comes the part about overriding the multimethod. This requires
|
| 52 |
+
the ``__ua_function__`` protocol, and the ``__ua_convert__``
|
| 53 |
+
protocol. The ``__ua_function__`` protocol has the signature
|
| 54 |
+
``(method, args, kwargs)`` where ``method`` is the passed
|
| 55 |
+
multimethod, ``args``/``kwargs`` specify the arguments and ``dispatchables``
|
| 56 |
+
is the list of converted dispatchables passed in.
|
| 57 |
+
|
| 58 |
+
>>> def __ua_function__(method, args, kwargs):
|
| 59 |
+
... return method.__name__, args, kwargs
|
| 60 |
+
>>> be.__ua_function__ = __ua_function__
|
| 61 |
+
|
| 62 |
+
The other protocol of interest is the ``__ua_convert__`` protocol. It has the
|
| 63 |
+
signature ``(dispatchables, coerce)``. When ``coerce`` is ``False``, conversion
|
| 64 |
+
between the formats should ideally be an ``O(1)`` operation, but it means that
|
| 65 |
+
no memory copying should be involved, only views of the existing data.
|
| 66 |
+
|
| 67 |
+
>>> def __ua_convert__(dispatchables, coerce):
|
| 68 |
+
... for d in dispatchables:
|
| 69 |
+
... if d.type is int:
|
| 70 |
+
... if coerce and d.coercible:
|
| 71 |
+
... yield str(d.value)
|
| 72 |
+
... else:
|
| 73 |
+
... yield d.value
|
| 74 |
+
>>> be.__ua_convert__ = __ua_convert__
|
| 75 |
+
|
| 76 |
+
Now that we have defined the backend, the next thing to do is to call the multimethod.
|
| 77 |
+
|
| 78 |
+
>>> with ua.set_backend(be):
|
| 79 |
+
... overridden_me(1, "2")
|
| 80 |
+
('override_me', (1, '2'), {})
|
| 81 |
+
|
| 82 |
+
Note that the marked type has no effect on the actual type of the passed object.
|
| 83 |
+
We can also coerce the type of the input.
|
| 84 |
+
|
| 85 |
+
>>> with ua.set_backend(be, coerce=True):
|
| 86 |
+
... overridden_me(1, "2")
|
| 87 |
+
... overridden_me(1.0, "2")
|
| 88 |
+
('override_me', ('1', '2'), {})
|
| 89 |
+
('override_me', ('1.0', '2'), {})
|
| 90 |
+
|
| 91 |
+
Another feature is that if you remove ``__ua_convert__``, the arguments are not
|
| 92 |
+
converted at all and it's up to the backend to handle that.
|
| 93 |
+
|
| 94 |
+
>>> del be.__ua_convert__
|
| 95 |
+
>>> with ua.set_backend(be):
|
| 96 |
+
... overridden_me(1, "2")
|
| 97 |
+
('override_me', (1, '2'), {})
|
| 98 |
+
|
| 99 |
+
You also have the option to return ``NotImplemented``, in which case processing moves on
|
| 100 |
+
to the next back-end, which in this case, doesn't exist. The same applies to
|
| 101 |
+
``__ua_convert__``.
|
| 102 |
+
|
| 103 |
+
>>> be.__ua_function__ = lambda *a, **kw: NotImplemented
|
| 104 |
+
>>> with ua.set_backend(be):
|
| 105 |
+
... overridden_me(1, "2")
|
| 106 |
+
Traceback (most recent call last):
|
| 107 |
+
...
|
| 108 |
+
uarray.BackendNotImplementedError: ...
|
| 109 |
+
|
| 110 |
+
The last possibility is if we don't have ``__ua_convert__``, in which case the job is
|
| 111 |
+
left up to ``__ua_function__``, but putting things back into arrays after conversion
|
| 112 |
+
will not be possible.
|
| 113 |
+
"""
|
| 114 |
+
|
| 115 |
+
from ._backend import *
|
| 116 |
+
__version__ = '0.8.8.dev0+aa94c5a4.scipy'
|
data/miniconda3/lib/python3.13/site-packages/scipy/_lib/_uarray/_backend.py
ADDED
|
@@ -0,0 +1,707 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import typing
|
| 2 |
+
import types
|
| 3 |
+
import inspect
|
| 4 |
+
import functools
|
| 5 |
+
from . import _uarray
|
| 6 |
+
import copyreg
|
| 7 |
+
import pickle
|
| 8 |
+
import contextlib
|
| 9 |
+
import threading
|
| 10 |
+
|
| 11 |
+
from ._uarray import ( # type: ignore
|
| 12 |
+
BackendNotImplementedError,
|
| 13 |
+
_Function,
|
| 14 |
+
_SkipBackendContext,
|
| 15 |
+
_SetBackendContext,
|
| 16 |
+
_BackendState,
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
__all__ = [
|
| 20 |
+
"set_backend",
|
| 21 |
+
"set_global_backend",
|
| 22 |
+
"skip_backend",
|
| 23 |
+
"register_backend",
|
| 24 |
+
"determine_backend",
|
| 25 |
+
"determine_backend_multi",
|
| 26 |
+
"clear_backends",
|
| 27 |
+
"create_multimethod",
|
| 28 |
+
"generate_multimethod",
|
| 29 |
+
"_Function",
|
| 30 |
+
"BackendNotImplementedError",
|
| 31 |
+
"Dispatchable",
|
| 32 |
+
"wrap_single_convertor",
|
| 33 |
+
"wrap_single_convertor_instance",
|
| 34 |
+
"all_of_type",
|
| 35 |
+
"mark_as",
|
| 36 |
+
"set_state",
|
| 37 |
+
"get_state",
|
| 38 |
+
"reset_state",
|
| 39 |
+
"_BackendState",
|
| 40 |
+
"_SkipBackendContext",
|
| 41 |
+
"_SetBackendContext",
|
| 42 |
+
]
|
| 43 |
+
|
| 44 |
+
ArgumentExtractorType = typing.Callable[..., tuple["Dispatchable", ...]]
|
| 45 |
+
ArgumentReplacerType = typing.Callable[
|
| 46 |
+
[tuple, dict, tuple], tuple[tuple, dict]
|
| 47 |
+
]
|
| 48 |
+
|
| 49 |
+
def unpickle_function(mod_name, qname, self_):
|
| 50 |
+
import importlib
|
| 51 |
+
|
| 52 |
+
try:
|
| 53 |
+
module = importlib.import_module(mod_name)
|
| 54 |
+
qname = qname.split(".")
|
| 55 |
+
func = module
|
| 56 |
+
for q in qname:
|
| 57 |
+
func = getattr(func, q)
|
| 58 |
+
|
| 59 |
+
if self_ is not None:
|
| 60 |
+
func = types.MethodType(func, self_)
|
| 61 |
+
|
| 62 |
+
return func
|
| 63 |
+
except (ImportError, AttributeError) as e:
|
| 64 |
+
from pickle import UnpicklingError
|
| 65 |
+
|
| 66 |
+
raise UnpicklingError from e
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def pickle_function(func):
|
| 70 |
+
mod_name = getattr(func, "__module__", None)
|
| 71 |
+
qname = getattr(func, "__qualname__", None)
|
| 72 |
+
self_ = getattr(func, "__self__", None)
|
| 73 |
+
|
| 74 |
+
try:
|
| 75 |
+
test = unpickle_function(mod_name, qname, self_)
|
| 76 |
+
except pickle.UnpicklingError:
|
| 77 |
+
test = None
|
| 78 |
+
|
| 79 |
+
if test is not func:
|
| 80 |
+
raise pickle.PicklingError(
|
| 81 |
+
f"Can't pickle {func}: it's not the same object as {test}"
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
return unpickle_function, (mod_name, qname, self_)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def pickle_state(state):
|
| 88 |
+
return _uarray._BackendState._unpickle, state._pickle()
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def pickle_set_backend_context(ctx):
|
| 92 |
+
return _SetBackendContext, ctx._pickle()
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def pickle_skip_backend_context(ctx):
|
| 96 |
+
return _SkipBackendContext, ctx._pickle()
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
copyreg.pickle(_Function, pickle_function)
|
| 100 |
+
copyreg.pickle(_uarray._BackendState, pickle_state)
|
| 101 |
+
copyreg.pickle(_SetBackendContext, pickle_set_backend_context)
|
| 102 |
+
copyreg.pickle(_SkipBackendContext, pickle_skip_backend_context)
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def get_state():
|
| 106 |
+
"""
|
| 107 |
+
Returns an opaque object containing the current state of all the backends.
|
| 108 |
+
|
| 109 |
+
Can be used for synchronization between threads/processes.
|
| 110 |
+
|
| 111 |
+
See Also
|
| 112 |
+
--------
|
| 113 |
+
set_state
|
| 114 |
+
Sets the state returned by this function.
|
| 115 |
+
"""
|
| 116 |
+
return _uarray.get_state()
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
@contextlib.contextmanager
|
| 120 |
+
def reset_state():
|
| 121 |
+
"""
|
| 122 |
+
Returns a context manager that resets all state once exited.
|
| 123 |
+
|
| 124 |
+
See Also
|
| 125 |
+
--------
|
| 126 |
+
set_state
|
| 127 |
+
Context manager that sets the backend state.
|
| 128 |
+
get_state
|
| 129 |
+
Gets a state to be set by this context manager.
|
| 130 |
+
"""
|
| 131 |
+
with set_state(get_state()):
|
| 132 |
+
yield
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
@contextlib.contextmanager
|
| 136 |
+
def set_state(state):
|
| 137 |
+
"""
|
| 138 |
+
A context manager that sets the state of the backends to one returned by :obj:`get_state`.
|
| 139 |
+
|
| 140 |
+
See Also
|
| 141 |
+
--------
|
| 142 |
+
get_state
|
| 143 |
+
Gets a state to be set by this context manager.
|
| 144 |
+
""" # noqa: E501
|
| 145 |
+
old_state = get_state()
|
| 146 |
+
_uarray.set_state(state)
|
| 147 |
+
try:
|
| 148 |
+
yield
|
| 149 |
+
finally:
|
| 150 |
+
_uarray.set_state(old_state, True)
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
def create_multimethod(*args, **kwargs):
|
| 154 |
+
"""
|
| 155 |
+
Creates a decorator for generating multimethods.
|
| 156 |
+
|
| 157 |
+
This function creates a decorator that can be used with an argument
|
| 158 |
+
extractor in order to generate a multimethod. Other than for the
|
| 159 |
+
argument extractor, all arguments are passed on to
|
| 160 |
+
:obj:`generate_multimethod`.
|
| 161 |
+
|
| 162 |
+
See Also
|
| 163 |
+
--------
|
| 164 |
+
generate_multimethod
|
| 165 |
+
Generates a multimethod.
|
| 166 |
+
"""
|
| 167 |
+
|
| 168 |
+
def wrapper(a):
|
| 169 |
+
return generate_multimethod(a, *args, **kwargs)
|
| 170 |
+
|
| 171 |
+
return wrapper
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
def generate_multimethod(
|
| 175 |
+
argument_extractor: ArgumentExtractorType,
|
| 176 |
+
argument_replacer: ArgumentReplacerType,
|
| 177 |
+
domain: str,
|
| 178 |
+
default: typing.Callable | None = None,
|
| 179 |
+
):
|
| 180 |
+
"""
|
| 181 |
+
Generates a multimethod.
|
| 182 |
+
|
| 183 |
+
Parameters
|
| 184 |
+
----------
|
| 185 |
+
argument_extractor : ArgumentExtractorType
|
| 186 |
+
A callable which extracts the dispatchable arguments. Extracted arguments
|
| 187 |
+
should be marked by the :obj:`Dispatchable` class. It has the same signature
|
| 188 |
+
as the desired multimethod.
|
| 189 |
+
argument_replacer : ArgumentReplacerType
|
| 190 |
+
A callable with the signature (args, kwargs, dispatchables), which should also
|
| 191 |
+
return an (args, kwargs) pair with the dispatchables replaced inside the
|
| 192 |
+
args/kwargs.
|
| 193 |
+
domain : str
|
| 194 |
+
A string value indicating the domain of this multimethod.
|
| 195 |
+
default: Optional[Callable], optional
|
| 196 |
+
The default implementation of this multimethod, where ``None`` (the default)
|
| 197 |
+
specifies there is no default implementation.
|
| 198 |
+
|
| 199 |
+
Examples
|
| 200 |
+
--------
|
| 201 |
+
In this example, ``a`` is to be dispatched over, so we return it, while marking it
|
| 202 |
+
as an ``int``.
|
| 203 |
+
The trailing comma is needed because the args have to be returned as an iterable.
|
| 204 |
+
|
| 205 |
+
>>> def override_me(a, b):
|
| 206 |
+
... return Dispatchable(a, int),
|
| 207 |
+
|
| 208 |
+
Next, we define the argument replacer that replaces the dispatchables inside
|
| 209 |
+
args/kwargs with the supplied ones.
|
| 210 |
+
|
| 211 |
+
>>> def override_replacer(args, kwargs, dispatchables):
|
| 212 |
+
... return (dispatchables[0], args[1]), {}
|
| 213 |
+
|
| 214 |
+
Next, we define the multimethod.
|
| 215 |
+
|
| 216 |
+
>>> overridden_me = generate_multimethod(
|
| 217 |
+
... override_me, override_replacer, "ua_examples"
|
| 218 |
+
... )
|
| 219 |
+
|
| 220 |
+
Notice that there's no default implementation, unless you supply one.
|
| 221 |
+
|
| 222 |
+
>>> overridden_me(1, "a")
|
| 223 |
+
Traceback (most recent call last):
|
| 224 |
+
...
|
| 225 |
+
uarray.BackendNotImplementedError: ...
|
| 226 |
+
|
| 227 |
+
>>> overridden_me2 = generate_multimethod(
|
| 228 |
+
... override_me, override_replacer, "ua_examples", default=lambda x, y: (x, y)
|
| 229 |
+
... )
|
| 230 |
+
>>> overridden_me2(1, "a")
|
| 231 |
+
(1, 'a')
|
| 232 |
+
|
| 233 |
+
See Also
|
| 234 |
+
--------
|
| 235 |
+
uarray
|
| 236 |
+
See the module documentation for how to override the method by creating
|
| 237 |
+
backends.
|
| 238 |
+
"""
|
| 239 |
+
kw_defaults, arg_defaults, opts = get_defaults(argument_extractor)
|
| 240 |
+
ua_func = _Function(
|
| 241 |
+
argument_extractor,
|
| 242 |
+
argument_replacer,
|
| 243 |
+
domain,
|
| 244 |
+
arg_defaults,
|
| 245 |
+
kw_defaults,
|
| 246 |
+
default,
|
| 247 |
+
)
|
| 248 |
+
|
| 249 |
+
return functools.update_wrapper(ua_func, argument_extractor)
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
def set_backend(backend, coerce=False, only=False):
|
| 253 |
+
"""
|
| 254 |
+
A context manager that sets the preferred backend.
|
| 255 |
+
|
| 256 |
+
Parameters
|
| 257 |
+
----------
|
| 258 |
+
backend
|
| 259 |
+
The backend to set.
|
| 260 |
+
coerce
|
| 261 |
+
Whether or not to coerce to a specific backend's types. Implies ``only``.
|
| 262 |
+
only
|
| 263 |
+
Whether or not this should be the last backend to try.
|
| 264 |
+
|
| 265 |
+
See Also
|
| 266 |
+
--------
|
| 267 |
+
skip_backend: A context manager that allows skipping of backends.
|
| 268 |
+
set_global_backend: Set a single, global backend for a domain.
|
| 269 |
+
"""
|
| 270 |
+
tid = threading.get_native_id()
|
| 271 |
+
try:
|
| 272 |
+
return backend.__ua_cache__[tid, "set", coerce, only]
|
| 273 |
+
except AttributeError:
|
| 274 |
+
backend.__ua_cache__ = {}
|
| 275 |
+
except KeyError:
|
| 276 |
+
pass
|
| 277 |
+
|
| 278 |
+
ctx = _SetBackendContext(backend, coerce, only)
|
| 279 |
+
backend.__ua_cache__[tid, "set", coerce, only] = ctx
|
| 280 |
+
return ctx
|
| 281 |
+
|
| 282 |
+
|
| 283 |
+
def skip_backend(backend):
|
| 284 |
+
"""
|
| 285 |
+
A context manager that allows one to skip a given backend from processing
|
| 286 |
+
entirely. This allows one to use another backend's code in a library that
|
| 287 |
+
is also a consumer of the same backend.
|
| 288 |
+
|
| 289 |
+
Parameters
|
| 290 |
+
----------
|
| 291 |
+
backend
|
| 292 |
+
The backend to skip.
|
| 293 |
+
|
| 294 |
+
See Also
|
| 295 |
+
--------
|
| 296 |
+
set_backend: A context manager that allows setting of backends.
|
| 297 |
+
set_global_backend: Set a single, global backend for a domain.
|
| 298 |
+
"""
|
| 299 |
+
tid = threading.get_native_id()
|
| 300 |
+
try:
|
| 301 |
+
return backend.__ua_cache__[tid, "skip"]
|
| 302 |
+
except AttributeError:
|
| 303 |
+
backend.__ua_cache__ = {}
|
| 304 |
+
except KeyError:
|
| 305 |
+
pass
|
| 306 |
+
|
| 307 |
+
ctx = _SkipBackendContext(backend)
|
| 308 |
+
backend.__ua_cache__[tid, "skip"] = ctx
|
| 309 |
+
return ctx
|
| 310 |
+
|
| 311 |
+
|
| 312 |
+
def get_defaults(f):
|
| 313 |
+
sig = inspect.signature(f)
|
| 314 |
+
kw_defaults = {}
|
| 315 |
+
arg_defaults = []
|
| 316 |
+
opts = set()
|
| 317 |
+
for k, v in sig.parameters.items():
|
| 318 |
+
if v.default is not inspect.Parameter.empty:
|
| 319 |
+
kw_defaults[k] = v.default
|
| 320 |
+
if v.kind in (
|
| 321 |
+
inspect.Parameter.POSITIONAL_ONLY,
|
| 322 |
+
inspect.Parameter.POSITIONAL_OR_KEYWORD,
|
| 323 |
+
):
|
| 324 |
+
arg_defaults.append(v.default)
|
| 325 |
+
opts.add(k)
|
| 326 |
+
|
| 327 |
+
return kw_defaults, tuple(arg_defaults), opts
|
| 328 |
+
|
| 329 |
+
|
| 330 |
+
def set_global_backend(backend, coerce=False, only=False, *, try_last=False):
|
| 331 |
+
"""
|
| 332 |
+
This utility method replaces the default backend for permanent use. It
|
| 333 |
+
will be tried in the list of backends automatically, unless the
|
| 334 |
+
``only`` flag is set on a backend. This will be the first tried
|
| 335 |
+
backend outside the :obj:`set_backend` context manager.
|
| 336 |
+
|
| 337 |
+
Note that this method is not thread-safe.
|
| 338 |
+
|
| 339 |
+
.. warning::
|
| 340 |
+
We caution library authors against using this function in
|
| 341 |
+
their code. We do *not* support this use-case. This function
|
| 342 |
+
is meant to be used only by users themselves, or by a reference
|
| 343 |
+
implementation, if one exists.
|
| 344 |
+
|
| 345 |
+
Parameters
|
| 346 |
+
----------
|
| 347 |
+
backend
|
| 348 |
+
The backend to register.
|
| 349 |
+
coerce : bool
|
| 350 |
+
Whether to coerce input types when trying this backend.
|
| 351 |
+
only : bool
|
| 352 |
+
If ``True``, no more backends will be tried if this fails.
|
| 353 |
+
Implied by ``coerce=True``.
|
| 354 |
+
try_last : bool
|
| 355 |
+
If ``True``, the global backend is tried after registered backends.
|
| 356 |
+
|
| 357 |
+
See Also
|
| 358 |
+
--------
|
| 359 |
+
set_backend: A context manager that allows setting of backends.
|
| 360 |
+
skip_backend: A context manager that allows skipping of backends.
|
| 361 |
+
"""
|
| 362 |
+
_uarray.set_global_backend(backend, coerce, only, try_last)
|
| 363 |
+
|
| 364 |
+
|
| 365 |
+
def register_backend(backend):
|
| 366 |
+
"""
|
| 367 |
+
This utility method sets registers backend for permanent use. It
|
| 368 |
+
will be tried in the list of backends automatically, unless the
|
| 369 |
+
``only`` flag is set on a backend.
|
| 370 |
+
|
| 371 |
+
Note that this method is not thread-safe.
|
| 372 |
+
|
| 373 |
+
Parameters
|
| 374 |
+
----------
|
| 375 |
+
backend
|
| 376 |
+
The backend to register.
|
| 377 |
+
"""
|
| 378 |
+
_uarray.register_backend(backend)
|
| 379 |
+
|
| 380 |
+
|
| 381 |
+
def clear_backends(domain, registered=True, globals=False):
|
| 382 |
+
"""
|
| 383 |
+
This utility method clears registered backends.
|
| 384 |
+
|
| 385 |
+
.. warning::
|
| 386 |
+
We caution library authors against using this function in
|
| 387 |
+
their code. We do *not* support this use-case. This function
|
| 388 |
+
is meant to be used only by users themselves.
|
| 389 |
+
|
| 390 |
+
.. warning::
|
| 391 |
+
Do NOT use this method inside a multimethod call, or the
|
| 392 |
+
program is likely to crash.
|
| 393 |
+
|
| 394 |
+
Parameters
|
| 395 |
+
----------
|
| 396 |
+
domain : Optional[str]
|
| 397 |
+
The domain for which to de-register backends. ``None`` means
|
| 398 |
+
de-register for all domains.
|
| 399 |
+
registered : bool
|
| 400 |
+
Whether or not to clear registered backends. See :obj:`register_backend`.
|
| 401 |
+
globals : bool
|
| 402 |
+
Whether or not to clear global backends. See :obj:`set_global_backend`.
|
| 403 |
+
|
| 404 |
+
See Also
|
| 405 |
+
--------
|
| 406 |
+
register_backend : Register a backend globally.
|
| 407 |
+
set_global_backend : Set a global backend.
|
| 408 |
+
"""
|
| 409 |
+
_uarray.clear_backends(domain, registered, globals)
|
| 410 |
+
|
| 411 |
+
|
| 412 |
+
class Dispatchable:
|
| 413 |
+
"""
|
| 414 |
+
A utility class which marks an argument with a specific dispatch type.
|
| 415 |
+
|
| 416 |
+
|
| 417 |
+
Attributes
|
| 418 |
+
----------
|
| 419 |
+
value
|
| 420 |
+
The value of the Dispatchable.
|
| 421 |
+
|
| 422 |
+
type
|
| 423 |
+
The type of the Dispatchable.
|
| 424 |
+
|
| 425 |
+
Examples
|
| 426 |
+
--------
|
| 427 |
+
>>> x = Dispatchable(1, str)
|
| 428 |
+
>>> x
|
| 429 |
+
<Dispatchable: type=<class 'str'>, value=1>
|
| 430 |
+
|
| 431 |
+
See Also
|
| 432 |
+
--------
|
| 433 |
+
all_of_type
|
| 434 |
+
Marks all unmarked parameters of a function.
|
| 435 |
+
|
| 436 |
+
mark_as
|
| 437 |
+
Allows one to create a utility function to mark as a given type.
|
| 438 |
+
"""
|
| 439 |
+
|
| 440 |
+
def __init__(self, value, dispatch_type, coercible=True):
|
| 441 |
+
self.value = value
|
| 442 |
+
self.type = dispatch_type
|
| 443 |
+
self.coercible = coercible
|
| 444 |
+
|
| 445 |
+
def __getitem__(self, index):
|
| 446 |
+
return (self.type, self.value)[index]
|
| 447 |
+
|
| 448 |
+
def __str__(self):
|
| 449 |
+
return f"<{type(self).__name__}: type={self.type!r}, value={self.value!r}>"
|
| 450 |
+
|
| 451 |
+
__repr__ = __str__
|
| 452 |
+
|
| 453 |
+
|
| 454 |
+
def mark_as(dispatch_type):
|
| 455 |
+
"""
|
| 456 |
+
Creates a utility function to mark something as a specific type.
|
| 457 |
+
|
| 458 |
+
Examples
|
| 459 |
+
--------
|
| 460 |
+
>>> mark_int = mark_as(int)
|
| 461 |
+
>>> mark_int(1)
|
| 462 |
+
<Dispatchable: type=<class 'int'>, value=1>
|
| 463 |
+
"""
|
| 464 |
+
return functools.partial(Dispatchable, dispatch_type=dispatch_type)
|
| 465 |
+
|
| 466 |
+
|
| 467 |
+
def all_of_type(arg_type):
|
| 468 |
+
"""
|
| 469 |
+
Marks all unmarked arguments as a given type.
|
| 470 |
+
|
| 471 |
+
Examples
|
| 472 |
+
--------
|
| 473 |
+
>>> @all_of_type(str)
|
| 474 |
+
... def f(a, b):
|
| 475 |
+
... return a, Dispatchable(b, int)
|
| 476 |
+
>>> f('a', 1)
|
| 477 |
+
(<Dispatchable: type=<class 'str'>, value='a'>,
|
| 478 |
+
<Dispatchable: type=<class 'int'>, value=1>)
|
| 479 |
+
"""
|
| 480 |
+
|
| 481 |
+
def outer(func):
|
| 482 |
+
@functools.wraps(func)
|
| 483 |
+
def inner(*args, **kwargs):
|
| 484 |
+
extracted_args = func(*args, **kwargs)
|
| 485 |
+
return tuple(
|
| 486 |
+
Dispatchable(arg, arg_type)
|
| 487 |
+
if not isinstance(arg, Dispatchable)
|
| 488 |
+
else arg
|
| 489 |
+
for arg in extracted_args
|
| 490 |
+
)
|
| 491 |
+
|
| 492 |
+
return inner
|
| 493 |
+
|
| 494 |
+
return outer
|
| 495 |
+
|
| 496 |
+
|
| 497 |
+
def wrap_single_convertor(convert_single):
|
| 498 |
+
"""
|
| 499 |
+
Wraps a ``__ua_convert__`` defined for a single element to all elements.
|
| 500 |
+
If any of them return ``NotImplemented``, the operation is assumed to be
|
| 501 |
+
undefined.
|
| 502 |
+
|
| 503 |
+
Accepts a signature of (value, type, coerce).
|
| 504 |
+
"""
|
| 505 |
+
|
| 506 |
+
@functools.wraps(convert_single)
|
| 507 |
+
def __ua_convert__(dispatchables, coerce):
|
| 508 |
+
converted = []
|
| 509 |
+
for d in dispatchables:
|
| 510 |
+
c = convert_single(d.value, d.type, coerce and d.coercible)
|
| 511 |
+
|
| 512 |
+
if c is NotImplemented:
|
| 513 |
+
return NotImplemented
|
| 514 |
+
|
| 515 |
+
converted.append(c)
|
| 516 |
+
|
| 517 |
+
return converted
|
| 518 |
+
|
| 519 |
+
return __ua_convert__
|
| 520 |
+
|
| 521 |
+
|
| 522 |
+
def wrap_single_convertor_instance(convert_single):
|
| 523 |
+
"""
|
| 524 |
+
Wraps a ``__ua_convert__`` defined for a single element to all elements.
|
| 525 |
+
If any of them return ``NotImplemented``, the operation is assumed to be
|
| 526 |
+
undefined.
|
| 527 |
+
|
| 528 |
+
Accepts a signature of (value, type, coerce).
|
| 529 |
+
"""
|
| 530 |
+
|
| 531 |
+
@functools.wraps(convert_single)
|
| 532 |
+
def __ua_convert__(self, dispatchables, coerce):
|
| 533 |
+
converted = []
|
| 534 |
+
for d in dispatchables:
|
| 535 |
+
c = convert_single(self, d.value, d.type, coerce and d.coercible)
|
| 536 |
+
|
| 537 |
+
if c is NotImplemented:
|
| 538 |
+
return NotImplemented
|
| 539 |
+
|
| 540 |
+
converted.append(c)
|
| 541 |
+
|
| 542 |
+
return converted
|
| 543 |
+
|
| 544 |
+
return __ua_convert__
|
| 545 |
+
|
| 546 |
+
|
| 547 |
+
def determine_backend(value, dispatch_type, *, domain, only=True, coerce=False):
|
| 548 |
+
"""Set the backend to the first active backend that supports ``value``
|
| 549 |
+
|
| 550 |
+
This is useful for functions that call multimethods without any dispatchable
|
| 551 |
+
arguments. You can use :func:`determine_backend` to ensure the same backend
|
| 552 |
+
is used everywhere in a block of multimethod calls.
|
| 553 |
+
|
| 554 |
+
Parameters
|
| 555 |
+
----------
|
| 556 |
+
value
|
| 557 |
+
The value being tested
|
| 558 |
+
dispatch_type
|
| 559 |
+
The dispatch type associated with ``value``, aka
|
| 560 |
+
":ref:`marking <MarkingGlossary>`".
|
| 561 |
+
domain: string
|
| 562 |
+
The domain to query for backends and set.
|
| 563 |
+
coerce: bool
|
| 564 |
+
Whether or not to allow coercion to the backend's types. Implies ``only``.
|
| 565 |
+
only: bool
|
| 566 |
+
Whether or not this should be the last backend to try.
|
| 567 |
+
|
| 568 |
+
See Also
|
| 569 |
+
--------
|
| 570 |
+
set_backend: For when you know which backend to set
|
| 571 |
+
|
| 572 |
+
Notes
|
| 573 |
+
-----
|
| 574 |
+
|
| 575 |
+
Support is determined by the ``__ua_convert__`` protocol. Backends not
|
| 576 |
+
supporting the type must return ``NotImplemented`` from their
|
| 577 |
+
``__ua_convert__`` if they don't support input of that type.
|
| 578 |
+
|
| 579 |
+
Examples
|
| 580 |
+
--------
|
| 581 |
+
|
| 582 |
+
Suppose we have two backends ``BackendA`` and ``BackendB`` each supporting
|
| 583 |
+
different types, ``TypeA`` and ``TypeB``. Neither supporting the other type:
|
| 584 |
+
|
| 585 |
+
>>> with ua.set_backend(ex.BackendA):
|
| 586 |
+
... ex.call_multimethod(ex.TypeB(), ex.TypeB())
|
| 587 |
+
Traceback (most recent call last):
|
| 588 |
+
...
|
| 589 |
+
uarray.BackendNotImplementedError: ...
|
| 590 |
+
|
| 591 |
+
Now consider a multimethod that creates a new object of ``TypeA``, or
|
| 592 |
+
``TypeB`` depending on the active backend.
|
| 593 |
+
|
| 594 |
+
>>> with ua.set_backend(ex.BackendA), ua.set_backend(ex.BackendB):
|
| 595 |
+
... res = ex.creation_multimethod()
|
| 596 |
+
... ex.call_multimethod(res, ex.TypeA())
|
| 597 |
+
Traceback (most recent call last):
|
| 598 |
+
...
|
| 599 |
+
uarray.BackendNotImplementedError: ...
|
| 600 |
+
|
| 601 |
+
``res`` is an object of ``TypeB`` because ``BackendB`` is set in the
|
| 602 |
+
innermost with statement. So, ``call_multimethod`` fails since the types
|
| 603 |
+
don't match.
|
| 604 |
+
|
| 605 |
+
Instead, we need to first find a backend suitable for all of our objects.
|
| 606 |
+
|
| 607 |
+
>>> with ua.set_backend(ex.BackendA), ua.set_backend(ex.BackendB):
|
| 608 |
+
... x = ex.TypeA()
|
| 609 |
+
... with ua.determine_backend(x, "mark", domain="ua_examples"):
|
| 610 |
+
... res = ex.creation_multimethod()
|
| 611 |
+
... ex.call_multimethod(res, x)
|
| 612 |
+
TypeA
|
| 613 |
+
|
| 614 |
+
"""
|
| 615 |
+
dispatchables = (Dispatchable(value, dispatch_type, coerce),)
|
| 616 |
+
backend = _uarray.determine_backend(domain, dispatchables, coerce)
|
| 617 |
+
|
| 618 |
+
return set_backend(backend, coerce=coerce, only=only)
|
| 619 |
+
|
| 620 |
+
|
| 621 |
+
def determine_backend_multi(
|
| 622 |
+
dispatchables, *, domain, only=True, coerce=False, **kwargs
|
| 623 |
+
):
|
| 624 |
+
"""Set a backend supporting all ``dispatchables``
|
| 625 |
+
|
| 626 |
+
This is useful for functions that call multimethods without any dispatchable
|
| 627 |
+
arguments. You can use :func:`determine_backend_multi` to ensure the same
|
| 628 |
+
backend is used everywhere in a block of multimethod calls involving
|
| 629 |
+
multiple arrays.
|
| 630 |
+
|
| 631 |
+
Parameters
|
| 632 |
+
----------
|
| 633 |
+
dispatchables: Sequence[Union[uarray.Dispatchable, Any]]
|
| 634 |
+
The dispatchables that must be supported
|
| 635 |
+
domain: string
|
| 636 |
+
The domain to query for backends and set.
|
| 637 |
+
coerce: bool
|
| 638 |
+
Whether or not to allow coercion to the backend's types. Implies ``only``.
|
| 639 |
+
only: bool
|
| 640 |
+
Whether or not this should be the last backend to try.
|
| 641 |
+
dispatch_type: Optional[Any]
|
| 642 |
+
The default dispatch type associated with ``dispatchables``, aka
|
| 643 |
+
":ref:`marking <MarkingGlossary>`".
|
| 644 |
+
|
| 645 |
+
See Also
|
| 646 |
+
--------
|
| 647 |
+
determine_backend: For a single dispatch value
|
| 648 |
+
set_backend: For when you know which backend to set
|
| 649 |
+
|
| 650 |
+
Notes
|
| 651 |
+
-----
|
| 652 |
+
|
| 653 |
+
Support is determined by the ``__ua_convert__`` protocol. Backends not
|
| 654 |
+
supporting the type must return ``NotImplemented`` from their
|
| 655 |
+
``__ua_convert__`` if they don't support input of that type.
|
| 656 |
+
|
| 657 |
+
Examples
|
| 658 |
+
--------
|
| 659 |
+
|
| 660 |
+
:func:`determine_backend` allows the backend to be set from a single
|
| 661 |
+
object. :func:`determine_backend_multi` allows multiple objects to be
|
| 662 |
+
checked simultaneously for support in the backend. Suppose we have a
|
| 663 |
+
``BackendAB`` which supports ``TypeA`` and ``TypeB`` in the same call,
|
| 664 |
+
and a ``BackendBC`` that doesn't support ``TypeA``.
|
| 665 |
+
|
| 666 |
+
>>> with ua.set_backend(ex.BackendAB), ua.set_backend(ex.BackendBC):
|
| 667 |
+
... a, b = ex.TypeA(), ex.TypeB()
|
| 668 |
+
... with ua.determine_backend_multi(
|
| 669 |
+
... [ua.Dispatchable(a, "mark"), ua.Dispatchable(b, "mark")],
|
| 670 |
+
... domain="ua_examples"
|
| 671 |
+
... ):
|
| 672 |
+
... res = ex.creation_multimethod()
|
| 673 |
+
... ex.call_multimethod(res, a, b)
|
| 674 |
+
TypeA
|
| 675 |
+
|
| 676 |
+
This won't call ``BackendBC`` because it doesn't support ``TypeA``.
|
| 677 |
+
|
| 678 |
+
We can also use leave out the ``ua.Dispatchable`` if we specify the
|
| 679 |
+
default ``dispatch_type`` for the ``dispatchables`` argument.
|
| 680 |
+
|
| 681 |
+
>>> with ua.set_backend(ex.BackendAB), ua.set_backend(ex.BackendBC):
|
| 682 |
+
... a, b = ex.TypeA(), ex.TypeB()
|
| 683 |
+
... with ua.determine_backend_multi(
|
| 684 |
+
... [a, b], dispatch_type="mark", domain="ua_examples"
|
| 685 |
+
... ):
|
| 686 |
+
... res = ex.creation_multimethod()
|
| 687 |
+
... ex.call_multimethod(res, a, b)
|
| 688 |
+
TypeA
|
| 689 |
+
|
| 690 |
+
"""
|
| 691 |
+
if "dispatch_type" in kwargs:
|
| 692 |
+
disp_type = kwargs.pop("dispatch_type")
|
| 693 |
+
dispatchables = tuple(
|
| 694 |
+
d if isinstance(d, Dispatchable) else Dispatchable(d, disp_type)
|
| 695 |
+
for d in dispatchables
|
| 696 |
+
)
|
| 697 |
+
else:
|
| 698 |
+
dispatchables = tuple(dispatchables)
|
| 699 |
+
if not all(isinstance(d, Dispatchable) for d in dispatchables):
|
| 700 |
+
raise TypeError("dispatchables must be instances of uarray.Dispatchable")
|
| 701 |
+
|
| 702 |
+
if len(kwargs) != 0:
|
| 703 |
+
raise TypeError(f"Received unexpected keyword arguments: {kwargs}")
|
| 704 |
+
|
| 705 |
+
backend = _uarray.determine_backend(domain, dispatchables, coerce)
|
| 706 |
+
|
| 707 |
+
return set_backend(backend, coerce=coerce, only=only)
|
data/miniconda3/lib/python3.13/site-packages/scipy/fft/tests/__pycache__/mock_backend.cpython-313.pyc
ADDED
|
Binary file (6.16 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/fft/tests/__pycache__/test_backend.cpython-313.pyc
ADDED
|
Binary file (8.22 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/fft/tests/__pycache__/test_basic.cpython-313.pyc
ADDED
|
Binary file (39.8 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/fft/tests/__pycache__/test_fftlog.cpython-313.pyc
ADDED
|
Binary file (8.84 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/fft/tests/__pycache__/test_helper.cpython-313.pyc
ADDED
|
Binary file (24.7 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/fft/tests/__pycache__/test_multithreading.cpython-313.pyc
ADDED
|
Binary file (5.67 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/fft/tests/__pycache__/test_real_transforms.cpython-313.pyc
ADDED
|
Binary file (13.1 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/__init__.cpython-313.pyc
ADDED
|
Binary file (7.78 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_basic.cpython-313.pyc
ADDED
|
Binary file (90.6 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_decomp.cpython-313.pyc
ADDED
|
Binary file (60.4 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_decomp_cholesky.cpython-313.pyc
ADDED
|
Binary file (15.6 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_decomp_cossin.cpython-313.pyc
ADDED
|
Binary file (11.3 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_decomp_ldl.cpython-313.pyc
ADDED
|
Binary file (12.3 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_decomp_lu.cpython-313.pyc
ADDED
|
Binary file (14.6 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_decomp_polar.cpython-313.pyc
ADDED
|
Binary file (3.99 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_decomp_qr.cpython-313.pyc
ADDED
|
Binary file (16.4 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_decomp_qz.cpython-313.pyc
ADDED
|
Binary file (16.6 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_decomp_schur.cpython-313.pyc
ADDED
|
Binary file (14.9 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_decomp_svd.cpython-313.pyc
ADDED
|
Binary file (18.1 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_expm_frechet.cpython-313.pyc
ADDED
|
Binary file (18.6 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_matfuncs.cpython-313.pyc
ADDED
|
Binary file (35.8 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_matfuncs_inv_ssq.cpython-313.pyc
ADDED
|
Binary file (34.8 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_matfuncs_sqrtm.cpython-313.pyc
ADDED
|
Binary file (4.02 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_misc.cpython-313.pyc
ADDED
|
Binary file (6.47 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_procrustes.cpython-313.pyc
ADDED
|
Binary file (4.28 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_sketches.cpython-313.pyc
ADDED
|
Binary file (6.95 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_solvers.cpython-313.pyc
ADDED
|
Binary file (34.1 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_special_matrices.cpython-313.pyc
ADDED
|
Binary file (44.8 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/_testutils.cpython-313.pyc
ADDED
|
Binary file (3.54 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/basic.cpython-313.pyc
ADDED
|
Binary file (840 Bytes). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/blas.cpython-313.pyc
ADDED
|
Binary file (11.7 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/decomp.cpython-313.pyc
ADDED
|
Binary file (803 Bytes). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/decomp_cholesky.cpython-313.pyc
ADDED
|
Binary file (775 Bytes). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/decomp_lu.cpython-313.pyc
ADDED
|
Binary file (714 Bytes). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/decomp_qr.cpython-313.pyc
ADDED
|
Binary file (695 Bytes). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/decomp_schur.cpython-313.pyc
ADDED
|
Binary file (728 Bytes). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/decomp_svd.cpython-313.pyc
ADDED
|
Binary file (748 Bytes). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/interpolative.cpython-313.pyc
ADDED
|
Binary file (31 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/lapack.cpython-313.pyc
ADDED
|
Binary file (18.2 kB). View file
|
|
|
data/miniconda3/lib/python3.13/site-packages/scipy/linalg/__pycache__/matfuncs.cpython-313.pyc
ADDED
|
Binary file (825 Bytes). View file
|
|
|