parent
474364b550
commit
8d6ab03a45
@ -0,0 +1,128 @@
|
||||
import sys
|
||||
import os
|
||||
import re
|
||||
import importlib
|
||||
import warnings
|
||||
|
||||
|
||||
is_pypy = '__pypy__' in sys.builtin_module_names
|
||||
|
||||
|
||||
warnings.filterwarnings('ignore',
|
||||
r'.+ distutils\b.+ deprecated',
|
||||
DeprecationWarning)
|
||||
|
||||
|
||||
def warn_distutils_present():
|
||||
if 'distutils' not in sys.modules:
|
||||
return
|
||||
if is_pypy and sys.version_info < (3, 7):
|
||||
# PyPy for 3.6 unconditionally imports distutils, so bypass the warning
|
||||
# https://foss.heptapod.net/pypy/pypy/-/blob/be829135bc0d758997b3566062999ee8b23872b4/lib-python/3/site.py#L250
|
||||
return
|
||||
warnings.warn(
|
||||
"Distutils was imported before Setuptools, but importing Setuptools "
|
||||
"also replaces the `distutils` module in `sys.modules`. This may lead "
|
||||
"to undesirable behaviors or errors. To avoid these issues, avoid "
|
||||
"using distutils directly, ensure that setuptools is installed in the "
|
||||
"traditional way (e.g. not an editable install), and/or make sure "
|
||||
"that setuptools is always imported before distutils.")
|
||||
|
||||
|
||||
def clear_distutils():
|
||||
if 'distutils' not in sys.modules:
|
||||
return
|
||||
warnings.warn("Setuptools is replacing distutils.")
|
||||
mods = [name for name in sys.modules if re.match(r'distutils\b', name)]
|
||||
for name in mods:
|
||||
del sys.modules[name]
|
||||
|
||||
|
||||
def enabled():
|
||||
"""
|
||||
Allow selection of distutils by environment variable.
|
||||
"""
|
||||
which = os.environ.get('SETUPTOOLS_USE_DISTUTILS', 'stdlib')
|
||||
return which == 'local'
|
||||
|
||||
|
||||
def ensure_local_distutils():
|
||||
clear_distutils()
|
||||
distutils = importlib.import_module('setuptools._distutils')
|
||||
distutils.__name__ = 'distutils'
|
||||
sys.modules['distutils'] = distutils
|
||||
|
||||
# sanity check that submodules load as expected
|
||||
core = importlib.import_module('distutils.core')
|
||||
assert '_distutils' in core.__file__, core.__file__
|
||||
|
||||
|
||||
def do_override():
|
||||
"""
|
||||
Ensure that the local copy of distutils is preferred over stdlib.
|
||||
|
||||
See https://github.com/pypa/setuptools/issues/417#issuecomment-392298401
|
||||
for more motivation.
|
||||
"""
|
||||
if enabled():
|
||||
warn_distutils_present()
|
||||
ensure_local_distutils()
|
||||
|
||||
|
||||
class DistutilsMetaFinder:
|
||||
def find_spec(self, fullname, path, target=None):
|
||||
if path is not None:
|
||||
return
|
||||
|
||||
method_name = 'spec_for_{fullname}'.format(**locals())
|
||||
method = getattr(self, method_name, lambda: None)
|
||||
return method()
|
||||
|
||||
def spec_for_distutils(self):
|
||||
import importlib.abc
|
||||
import importlib.util
|
||||
|
||||
class DistutilsLoader(importlib.abc.Loader):
|
||||
|
||||
def create_module(self, spec):
|
||||
return importlib.import_module('setuptools._distutils')
|
||||
|
||||
def exec_module(self, module):
|
||||
pass
|
||||
|
||||
return importlib.util.spec_from_loader('distutils', DistutilsLoader())
|
||||
|
||||
def spec_for_pip(self):
|
||||
"""
|
||||
Ensure stdlib distutils when running under pip.
|
||||
See pypa/pip#8761 for rationale.
|
||||
"""
|
||||
if self.pip_imported_during_build():
|
||||
return
|
||||
clear_distutils()
|
||||
self.spec_for_distutils = lambda: None
|
||||
|
||||
@staticmethod
|
||||
def pip_imported_during_build():
|
||||
"""
|
||||
Detect if pip is being imported in a build script. Ref #2355.
|
||||
"""
|
||||
import traceback
|
||||
return any(
|
||||
frame.f_globals['__file__'].endswith('setup.py')
|
||||
for frame, line in traceback.walk_stack(None)
|
||||
)
|
||||
|
||||
|
||||
DISTUTILS_FINDER = DistutilsMetaFinder()
|
||||
|
||||
|
||||
def add_shim():
|
||||
sys.meta_path.insert(0, DISTUTILS_FINDER)
|
||||
|
||||
|
||||
def remove_shim():
|
||||
try:
|
||||
sys.meta_path.remove(DISTUTILS_FINDER)
|
||||
except ValueError:
|
||||
pass
|
||||
@ -0,0 +1 @@
|
||||
__import__('_distutils_hack').do_override()
|
||||
@ -0,0 +1 @@
|
||||
pip
|
||||
@ -0,0 +1,295 @@
|
||||
Metadata-Version: 2.3
|
||||
Name: annotated-types
|
||||
Version: 0.7.0
|
||||
Summary: Reusable constraint types to use with typing.Annotated
|
||||
Project-URL: Homepage, https://github.com/annotated-types/annotated-types
|
||||
Project-URL: Source, https://github.com/annotated-types/annotated-types
|
||||
Project-URL: Changelog, https://github.com/annotated-types/annotated-types/releases
|
||||
Author-email: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com>, Samuel Colvin <s@muelcolvin.com>, Zac Hatfield-Dodds <zac@zhd.dev>
|
||||
License-File: LICENSE
|
||||
Classifier: Development Status :: 4 - Beta
|
||||
Classifier: Environment :: Console
|
||||
Classifier: Environment :: MacOS X
|
||||
Classifier: Intended Audience :: Developers
|
||||
Classifier: Intended Audience :: Information Technology
|
||||
Classifier: License :: OSI Approved :: MIT License
|
||||
Classifier: Operating System :: POSIX :: Linux
|
||||
Classifier: Operating System :: Unix
|
||||
Classifier: Programming Language :: Python :: 3 :: Only
|
||||
Classifier: Programming Language :: Python :: 3.8
|
||||
Classifier: Programming Language :: Python :: 3.9
|
||||
Classifier: Programming Language :: Python :: 3.10
|
||||
Classifier: Programming Language :: Python :: 3.11
|
||||
Classifier: Programming Language :: Python :: 3.12
|
||||
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
||||
Classifier: Typing :: Typed
|
||||
Requires-Python: >=3.8
|
||||
Requires-Dist: typing-extensions>=4.0.0; python_version < '3.9'
|
||||
Description-Content-Type: text/markdown
|
||||
|
||||
# annotated-types
|
||||
|
||||
[](https://github.com/annotated-types/annotated-types/actions?query=event%3Apush+branch%3Amain+workflow%3ACI)
|
||||
[](https://pypi.python.org/pypi/annotated-types)
|
||||
[](https://github.com/annotated-types/annotated-types)
|
||||
[](https://github.com/annotated-types/annotated-types/blob/main/LICENSE)
|
||||
|
||||
[PEP-593](https://peps.python.org/pep-0593/) added `typing.Annotated` as a way of
|
||||
adding context-specific metadata to existing types, and specifies that
|
||||
`Annotated[T, x]` _should_ be treated as `T` by any tool or library without special
|
||||
logic for `x`.
|
||||
|
||||
This package provides metadata objects which can be used to represent common
|
||||
constraints such as upper and lower bounds on scalar values and collection sizes,
|
||||
a `Predicate` marker for runtime checks, and
|
||||
descriptions of how we intend these metadata to be interpreted. In some cases,
|
||||
we also note alternative representations which do not require this package.
|
||||
|
||||
## Install
|
||||
|
||||
```bash
|
||||
pip install annotated-types
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
```python
|
||||
from typing import Annotated
|
||||
from annotated_types import Gt, Len, Predicate
|
||||
|
||||
class MyClass:
|
||||
age: Annotated[int, Gt(18)] # Valid: 19, 20, ...
|
||||
# Invalid: 17, 18, "19", 19.0, ...
|
||||
factors: list[Annotated[int, Predicate(is_prime)]] # Valid: 2, 3, 5, 7, 11, ...
|
||||
# Invalid: 4, 8, -2, 5.0, "prime", ...
|
||||
|
||||
my_list: Annotated[list[int], Len(0, 10)] # Valid: [], [10, 20, 30, 40, 50]
|
||||
# Invalid: (1, 2), ["abc"], [0] * 20
|
||||
```
|
||||
|
||||
## Documentation
|
||||
|
||||
_While `annotated-types` avoids runtime checks for performance, users should not
|
||||
construct invalid combinations such as `MultipleOf("non-numeric")` or `Annotated[int, Len(3)]`.
|
||||
Downstream implementors may choose to raise an error, emit a warning, silently ignore
|
||||
a metadata item, etc., if the metadata objects described below are used with an
|
||||
incompatible type - or for any other reason!_
|
||||
|
||||
### Gt, Ge, Lt, Le
|
||||
|
||||
Express inclusive and/or exclusive bounds on orderable values - which may be numbers,
|
||||
dates, times, strings, sets, etc. Note that the boundary value need not be of the
|
||||
same type that was annotated, so long as they can be compared: `Annotated[int, Gt(1.5)]`
|
||||
is fine, for example, and implies that the value is an integer x such that `x > 1.5`.
|
||||
|
||||
We suggest that implementors may also interpret `functools.partial(operator.le, 1.5)`
|
||||
as being equivalent to `Gt(1.5)`, for users who wish to avoid a runtime dependency on
|
||||
the `annotated-types` package.
|
||||
|
||||
To be explicit, these types have the following meanings:
|
||||
|
||||
* `Gt(x)` - value must be "Greater Than" `x` - equivalent to exclusive minimum
|
||||
* `Ge(x)` - value must be "Greater than or Equal" to `x` - equivalent to inclusive minimum
|
||||
* `Lt(x)` - value must be "Less Than" `x` - equivalent to exclusive maximum
|
||||
* `Le(x)` - value must be "Less than or Equal" to `x` - equivalent to inclusive maximum
|
||||
|
||||
### Interval
|
||||
|
||||
`Interval(gt, ge, lt, le)` allows you to specify an upper and lower bound with a single
|
||||
metadata object. `None` attributes should be ignored, and non-`None` attributes
|
||||
treated as per the single bounds above.
|
||||
|
||||
### MultipleOf
|
||||
|
||||
`MultipleOf(multiple_of=x)` might be interpreted in two ways:
|
||||
|
||||
1. Python semantics, implying `value % multiple_of == 0`, or
|
||||
2. [JSONschema semantics](https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.2.1),
|
||||
where `int(value / multiple_of) == value / multiple_of`.
|
||||
|
||||
We encourage users to be aware of these two common interpretations and their
|
||||
distinct behaviours, especially since very large or non-integer numbers make
|
||||
it easy to cause silent data corruption due to floating-point imprecision.
|
||||
|
||||
We encourage libraries to carefully document which interpretation they implement.
|
||||
|
||||
### MinLen, MaxLen, Len
|
||||
|
||||
`Len()` implies that `min_length <= len(value) <= max_length` - lower and upper bounds are inclusive.
|
||||
|
||||
As well as `Len()` which can optionally include upper and lower bounds, we also
|
||||
provide `MinLen(x)` and `MaxLen(y)` which are equivalent to `Len(min_length=x)`
|
||||
and `Len(max_length=y)` respectively.
|
||||
|
||||
`Len`, `MinLen`, and `MaxLen` may be used with any type which supports `len(value)`.
|
||||
|
||||
Examples of usage:
|
||||
|
||||
* `Annotated[list, MaxLen(10)]` (or `Annotated[list, Len(max_length=10))`) - list must have a length of 10 or less
|
||||
* `Annotated[str, MaxLen(10)]` - string must have a length of 10 or less
|
||||
* `Annotated[list, MinLen(3))` (or `Annotated[list, Len(min_length=3))`) - list must have a length of 3 or more
|
||||
* `Annotated[list, Len(4, 6)]` - list must have a length of 4, 5, or 6
|
||||
* `Annotated[list, Len(8, 8)]` - list must have a length of exactly 8
|
||||
|
||||
#### Changed in v0.4.0
|
||||
|
||||
* `min_inclusive` has been renamed to `min_length`, no change in meaning
|
||||
* `max_exclusive` has been renamed to `max_length`, upper bound is now **inclusive** instead of **exclusive**
|
||||
* The recommendation that slices are interpreted as `Len` has been removed due to ambiguity and different semantic
|
||||
meaning of the upper bound in slices vs. `Len`
|
||||
|
||||
See [issue #23](https://github.com/annotated-types/annotated-types/issues/23) for discussion.
|
||||
|
||||
### Timezone
|
||||
|
||||
`Timezone` can be used with a `datetime` or a `time` to express which timezones
|
||||
are allowed. `Annotated[datetime, Timezone(None)]` must be a naive datetime.
|
||||
`Timezone[...]` ([literal ellipsis](https://docs.python.org/3/library/constants.html#Ellipsis))
|
||||
expresses that any timezone-aware datetime is allowed. You may also pass a specific
|
||||
timezone string or [`tzinfo`](https://docs.python.org/3/library/datetime.html#tzinfo-objects)
|
||||
object such as `Timezone(timezone.utc)` or `Timezone("Africa/Abidjan")` to express that you only
|
||||
allow a specific timezone, though we note that this is often a symptom of fragile design.
|
||||
|
||||
#### Changed in v0.x.x
|
||||
|
||||
* `Timezone` accepts [`tzinfo`](https://docs.python.org/3/library/datetime.html#tzinfo-objects) objects instead of
|
||||
`timezone`, extending compatibility to [`zoneinfo`](https://docs.python.org/3/library/zoneinfo.html) and third party libraries.
|
||||
|
||||
### Unit
|
||||
|
||||
`Unit(unit: str)` expresses that the annotated numeric value is the magnitude of
|
||||
a quantity with the specified unit. For example, `Annotated[float, Unit("m/s")]`
|
||||
would be a float representing a velocity in meters per second.
|
||||
|
||||
Please note that `annotated_types` itself makes no attempt to parse or validate
|
||||
the unit string in any way. That is left entirely to downstream libraries,
|
||||
such as [`pint`](https://pint.readthedocs.io) or
|
||||
[`astropy.units`](https://docs.astropy.org/en/stable/units/).
|
||||
|
||||
An example of how a library might use this metadata:
|
||||
|
||||
```python
|
||||
from annotated_types import Unit
|
||||
from typing import Annotated, TypeVar, Callable, Any, get_origin, get_args
|
||||
|
||||
# given a type annotated with a unit:
|
||||
Meters = Annotated[float, Unit("m")]
|
||||
|
||||
|
||||
# you can cast the annotation to a specific unit type with any
|
||||
# callable that accepts a string and returns the desired type
|
||||
T = TypeVar("T")
|
||||
def cast_unit(tp: Any, unit_cls: Callable[[str], T]) -> T | None:
|
||||
if get_origin(tp) is Annotated:
|
||||
for arg in get_args(tp):
|
||||
if isinstance(arg, Unit):
|
||||
return unit_cls(arg.unit)
|
||||
return None
|
||||
|
||||
|
||||
# using `pint`
|
||||
import pint
|
||||
pint_unit = cast_unit(Meters, pint.Unit)
|
||||
|
||||
|
||||
# using `astropy.units`
|
||||
import astropy.units as u
|
||||
astropy_unit = cast_unit(Meters, u.Unit)
|
||||
```
|
||||
|
||||
### Predicate
|
||||
|
||||
`Predicate(func: Callable)` expresses that `func(value)` is truthy for valid values.
|
||||
Users should prefer the statically inspectable metadata above, but if you need
|
||||
the full power and flexibility of arbitrary runtime predicates... here it is.
|
||||
|
||||
For some common constraints, we provide generic types:
|
||||
|
||||
* `IsLower = Annotated[T, Predicate(str.islower)]`
|
||||
* `IsUpper = Annotated[T, Predicate(str.isupper)]`
|
||||
* `IsDigit = Annotated[T, Predicate(str.isdigit)]`
|
||||
* `IsFinite = Annotated[T, Predicate(math.isfinite)]`
|
||||
* `IsNotFinite = Annotated[T, Predicate(Not(math.isfinite))]`
|
||||
* `IsNan = Annotated[T, Predicate(math.isnan)]`
|
||||
* `IsNotNan = Annotated[T, Predicate(Not(math.isnan))]`
|
||||
* `IsInfinite = Annotated[T, Predicate(math.isinf)]`
|
||||
* `IsNotInfinite = Annotated[T, Predicate(Not(math.isinf))]`
|
||||
|
||||
so that you can write e.g. `x: IsFinite[float] = 2.0` instead of the longer
|
||||
(but exactly equivalent) `x: Annotated[float, Predicate(math.isfinite)] = 2.0`.
|
||||
|
||||
Some libraries might have special logic to handle known or understandable predicates,
|
||||
for example by checking for `str.isdigit` and using its presence to both call custom
|
||||
logic to enforce digit-only strings, and customise some generated external schema.
|
||||
Users are therefore encouraged to avoid indirection like `lambda s: s.lower()`, in
|
||||
favor of introspectable methods such as `str.lower` or `re.compile("pattern").search`.
|
||||
|
||||
To enable basic negation of commonly used predicates like `math.isnan` without introducing introspection that makes it impossible for implementers to introspect the predicate we provide a `Not` wrapper that simply negates the predicate in an introspectable manner. Several of the predicates listed above are created in this manner.
|
||||
|
||||
We do not specify what behaviour should be expected for predicates that raise
|
||||
an exception. For example `Annotated[int, Predicate(str.isdigit)]` might silently
|
||||
skip invalid constraints, or statically raise an error; or it might try calling it
|
||||
and then propagate or discard the resulting
|
||||
`TypeError: descriptor 'isdigit' for 'str' objects doesn't apply to a 'int' object`
|
||||
exception. We encourage libraries to document the behaviour they choose.
|
||||
|
||||
### Doc
|
||||
|
||||
`doc()` can be used to add documentation information in `Annotated`, for function and method parameters, variables, class attributes, return types, and any place where `Annotated` can be used.
|
||||
|
||||
It expects a value that can be statically analyzed, as the main use case is for static analysis, editors, documentation generators, and similar tools.
|
||||
|
||||
It returns a `DocInfo` class with a single attribute `documentation` containing the value passed to `doc()`.
|
||||
|
||||
This is the early adopter's alternative form of the [`typing-doc` proposal](https://github.com/tiangolo/fastapi/blob/typing-doc/typing_doc.md).
|
||||
|
||||
### Integrating downstream types with `GroupedMetadata`
|
||||
|
||||
Implementers may choose to provide a convenience wrapper that groups multiple pieces of metadata.
|
||||
This can help reduce verbosity and cognitive overhead for users.
|
||||
For example, an implementer like Pydantic might provide a `Field` or `Meta` type that accepts keyword arguments and transforms these into low-level metadata:
|
||||
|
||||
```python
|
||||
from dataclasses import dataclass
|
||||
from typing import Iterator
|
||||
from annotated_types import GroupedMetadata, Ge
|
||||
|
||||
@dataclass
|
||||
class Field(GroupedMetadata):
|
||||
ge: int | None = None
|
||||
description: str | None = None
|
||||
|
||||
def __iter__(self) -> Iterator[object]:
|
||||
# Iterating over a GroupedMetadata object should yield annotated-types
|
||||
# constraint metadata objects which describe it as fully as possible,
|
||||
# and may include other unknown objects too.
|
||||
if self.ge is not None:
|
||||
yield Ge(self.ge)
|
||||
if self.description is not None:
|
||||
yield Description(self.description)
|
||||
```
|
||||
|
||||
Libraries consuming annotated-types constraints should check for `GroupedMetadata` and unpack it by iterating over the object and treating the results as if they had been "unpacked" in the `Annotated` type. The same logic should be applied to the [PEP 646 `Unpack` type](https://peps.python.org/pep-0646/), so that `Annotated[T, Field(...)]`, `Annotated[T, Unpack[Field(...)]]` and `Annotated[T, *Field(...)]` are all treated consistently.
|
||||
|
||||
Libraries consuming annotated-types should also ignore any metadata they do not recongize that came from unpacking a `GroupedMetadata`, just like they ignore unrecognized metadata in `Annotated` itself.
|
||||
|
||||
Our own `annotated_types.Interval` class is a `GroupedMetadata` which unpacks itself into `Gt`, `Lt`, etc., so this is not an abstract concern. Similarly, `annotated_types.Len` is a `GroupedMetadata` which unpacks itself into `MinLen` (optionally) and `MaxLen`.
|
||||
|
||||
### Consuming metadata
|
||||
|
||||
We intend to not be prescriptive as to _how_ the metadata and constraints are used, but as an example of how one might parse constraints from types annotations see our [implementation in `test_main.py`](https://github.com/annotated-types/annotated-types/blob/f59cf6d1b5255a0fe359b93896759a180bec30ae/tests/test_main.py#L94-L103).
|
||||
|
||||
It is up to the implementer to determine how this metadata is used.
|
||||
You could use the metadata for runtime type checking, for generating schemas or to generate example data, amongst other use cases.
|
||||
|
||||
## Design & History
|
||||
|
||||
This package was designed at the PyCon 2022 sprints by the maintainers of Pydantic
|
||||
and Hypothesis, with the goal of making it as easy as possible for end-users to
|
||||
provide more informative annotations for use by runtime libraries.
|
||||
|
||||
It is deliberately minimal, and following PEP-593 allows considerable downstream
|
||||
discretion in what (if anything!) they choose to support. Nonetheless, we expect
|
||||
that staying simple and covering _only_ the most common use-cases will give users
|
||||
and maintainers the best experience we can. If you'd like more constraints for your
|
||||
types - follow our lead, by defining them and documenting them downstream!
|
||||
@ -0,0 +1,10 @@
|
||||
annotated_types-0.7.0.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
|
||||
annotated_types-0.7.0.dist-info/METADATA,sha256=7ltqxksJJ0wCYFGBNIQCWTlWQGeAH0hRFdnK3CB895E,15046
|
||||
annotated_types-0.7.0.dist-info/RECORD,,
|
||||
annotated_types-0.7.0.dist-info/WHEEL,sha256=zEMcRr9Kr03x1ozGwg5v9NQBKn3kndp6LSoSlVg-jhU,87
|
||||
annotated_types-0.7.0.dist-info/licenses/LICENSE,sha256=_hBJiEsaDZNCkB6I4H8ykl0ksxIdmXK2poBfuYJLCV0,1083
|
||||
annotated_types/__init__.py,sha256=RynLsRKUEGI0KimXydlD1fZEfEzWwDo0Uon3zOKhG1Q,13819
|
||||
annotated_types/__pycache__/__init__.cpython-39.pyc,,
|
||||
annotated_types/__pycache__/test_cases.cpython-39.pyc,,
|
||||
annotated_types/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
||||
annotated_types/test_cases.py,sha256=zHFX6EpcMbGJ8FzBYDbO56bPwx_DYIVSKbZM-4B3_lg,6421
|
||||
@ -0,0 +1,4 @@
|
||||
Wheel-Version: 1.0
|
||||
Generator: hatchling 1.24.2
|
||||
Root-Is-Purelib: true
|
||||
Tag: py3-none-any
|
||||
@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2022 the contributors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@ -0,0 +1,432 @@
|
||||
import math
|
||||
import sys
|
||||
import types
|
||||
from dataclasses import dataclass
|
||||
from datetime import tzinfo
|
||||
from typing import TYPE_CHECKING, Any, Callable, Iterator, Optional, SupportsFloat, SupportsIndex, TypeVar, Union
|
||||
|
||||
if sys.version_info < (3, 8):
|
||||
from typing_extensions import Protocol, runtime_checkable
|
||||
else:
|
||||
from typing import Protocol, runtime_checkable
|
||||
|
||||
if sys.version_info < (3, 9):
|
||||
from typing_extensions import Annotated, Literal
|
||||
else:
|
||||
from typing import Annotated, Literal
|
||||
|
||||
if sys.version_info < (3, 10):
|
||||
EllipsisType = type(Ellipsis)
|
||||
KW_ONLY = {}
|
||||
SLOTS = {}
|
||||
else:
|
||||
from types import EllipsisType
|
||||
|
||||
KW_ONLY = {"kw_only": True}
|
||||
SLOTS = {"slots": True}
|
||||
|
||||
|
||||
__all__ = (
|
||||
'BaseMetadata',
|
||||
'GroupedMetadata',
|
||||
'Gt',
|
||||
'Ge',
|
||||
'Lt',
|
||||
'Le',
|
||||
'Interval',
|
||||
'MultipleOf',
|
||||
'MinLen',
|
||||
'MaxLen',
|
||||
'Len',
|
||||
'Timezone',
|
||||
'Predicate',
|
||||
'LowerCase',
|
||||
'UpperCase',
|
||||
'IsDigits',
|
||||
'IsFinite',
|
||||
'IsNotFinite',
|
||||
'IsNan',
|
||||
'IsNotNan',
|
||||
'IsInfinite',
|
||||
'IsNotInfinite',
|
||||
'doc',
|
||||
'DocInfo',
|
||||
'__version__',
|
||||
)
|
||||
|
||||
__version__ = '0.7.0'
|
||||
|
||||
|
||||
T = TypeVar('T')
|
||||
|
||||
|
||||
# arguments that start with __ are considered
|
||||
# positional only
|
||||
# see https://peps.python.org/pep-0484/#positional-only-arguments
|
||||
|
||||
|
||||
class SupportsGt(Protocol):
|
||||
def __gt__(self: T, __other: T) -> bool:
|
||||
...
|
||||
|
||||
|
||||
class SupportsGe(Protocol):
|
||||
def __ge__(self: T, __other: T) -> bool:
|
||||
...
|
||||
|
||||
|
||||
class SupportsLt(Protocol):
|
||||
def __lt__(self: T, __other: T) -> bool:
|
||||
...
|
||||
|
||||
|
||||
class SupportsLe(Protocol):
|
||||
def __le__(self: T, __other: T) -> bool:
|
||||
...
|
||||
|
||||
|
||||
class SupportsMod(Protocol):
|
||||
def __mod__(self: T, __other: T) -> T:
|
||||
...
|
||||
|
||||
|
||||
class SupportsDiv(Protocol):
|
||||
def __div__(self: T, __other: T) -> T:
|
||||
...
|
||||
|
||||
|
||||
class BaseMetadata:
|
||||
"""Base class for all metadata.
|
||||
|
||||
This exists mainly so that implementers
|
||||
can do `isinstance(..., BaseMetadata)` while traversing field annotations.
|
||||
"""
|
||||
|
||||
__slots__ = ()
|
||||
|
||||
|
||||
@dataclass(frozen=True, **SLOTS)
|
||||
class Gt(BaseMetadata):
|
||||
"""Gt(gt=x) implies that the value must be greater than x.
|
||||
|
||||
It can be used with any type that supports the ``>`` operator,
|
||||
including numbers, dates and times, strings, sets, and so on.
|
||||
"""
|
||||
|
||||
gt: SupportsGt
|
||||
|
||||
|
||||
@dataclass(frozen=True, **SLOTS)
|
||||
class Ge(BaseMetadata):
|
||||
"""Ge(ge=x) implies that the value must be greater than or equal to x.
|
||||
|
||||
It can be used with any type that supports the ``>=`` operator,
|
||||
including numbers, dates and times, strings, sets, and so on.
|
||||
"""
|
||||
|
||||
ge: SupportsGe
|
||||
|
||||
|
||||
@dataclass(frozen=True, **SLOTS)
|
||||
class Lt(BaseMetadata):
|
||||
"""Lt(lt=x) implies that the value must be less than x.
|
||||
|
||||
It can be used with any type that supports the ``<`` operator,
|
||||
including numbers, dates and times, strings, sets, and so on.
|
||||
"""
|
||||
|
||||
lt: SupportsLt
|
||||
|
||||
|
||||
@dataclass(frozen=True, **SLOTS)
|
||||
class Le(BaseMetadata):
|
||||
"""Le(le=x) implies that the value must be less than or equal to x.
|
||||
|
||||
It can be used with any type that supports the ``<=`` operator,
|
||||
including numbers, dates and times, strings, sets, and so on.
|
||||
"""
|
||||
|
||||
le: SupportsLe
|
||||
|
||||
|
||||
@runtime_checkable
|
||||
class GroupedMetadata(Protocol):
|
||||
"""A grouping of multiple objects, like typing.Unpack.
|
||||
|
||||
`GroupedMetadata` on its own is not metadata and has no meaning.
|
||||
All of the constraints and metadata should be fully expressable
|
||||
in terms of the `BaseMetadata`'s returned by `GroupedMetadata.__iter__()`.
|
||||
|
||||
Concrete implementations should override `GroupedMetadata.__iter__()`
|
||||
to add their own metadata.
|
||||
For example:
|
||||
|
||||
>>> @dataclass
|
||||
>>> class Field(GroupedMetadata):
|
||||
>>> gt: float | None = None
|
||||
>>> description: str | None = None
|
||||
...
|
||||
>>> def __iter__(self) -> Iterable[object]:
|
||||
>>> if self.gt is not None:
|
||||
>>> yield Gt(self.gt)
|
||||
>>> if self.description is not None:
|
||||
>>> yield Description(self.gt)
|
||||
|
||||
Also see the implementation of `Interval` below for an example.
|
||||
|
||||
Parsers should recognize this and unpack it so that it can be used
|
||||
both with and without unpacking:
|
||||
|
||||
- `Annotated[int, Field(...)]` (parser must unpack Field)
|
||||
- `Annotated[int, *Field(...)]` (PEP-646)
|
||||
""" # noqa: trailing-whitespace
|
||||
|
||||
@property
|
||||
def __is_annotated_types_grouped_metadata__(self) -> Literal[True]:
|
||||
return True
|
||||
|
||||
def __iter__(self) -> Iterator[object]:
|
||||
...
|
||||
|
||||
if not TYPE_CHECKING:
|
||||
__slots__ = () # allow subclasses to use slots
|
||||
|
||||
def __init_subclass__(cls, *args: Any, **kwargs: Any) -> None:
|
||||
# Basic ABC like functionality without the complexity of an ABC
|
||||
super().__init_subclass__(*args, **kwargs)
|
||||
if cls.__iter__ is GroupedMetadata.__iter__:
|
||||
raise TypeError("Can't subclass GroupedMetadata without implementing __iter__")
|
||||
|
||||
def __iter__(self) -> Iterator[object]: # noqa: F811
|
||||
raise NotImplementedError # more helpful than "None has no attribute..." type errors
|
||||
|
||||
|
||||
@dataclass(frozen=True, **KW_ONLY, **SLOTS)
|
||||
class Interval(GroupedMetadata):
|
||||
"""Interval can express inclusive or exclusive bounds with a single object.
|
||||
|
||||
It accepts keyword arguments ``gt``, ``ge``, ``lt``, and/or ``le``, which
|
||||
are interpreted the same way as the single-bound constraints.
|
||||
"""
|
||||
|
||||
gt: Union[SupportsGt, None] = None
|
||||
ge: Union[SupportsGe, None] = None
|
||||
lt: Union[SupportsLt, None] = None
|
||||
le: Union[SupportsLe, None] = None
|
||||
|
||||
def __iter__(self) -> Iterator[BaseMetadata]:
|
||||
"""Unpack an Interval into zero or more single-bounds."""
|
||||
if self.gt is not None:
|
||||
yield Gt(self.gt)
|
||||
if self.ge is not None:
|
||||
yield Ge(self.ge)
|
||||
if self.lt is not None:
|
||||
yield Lt(self.lt)
|
||||
if self.le is not None:
|
||||
yield Le(self.le)
|
||||
|
||||
|
||||
@dataclass(frozen=True, **SLOTS)
|
||||
class MultipleOf(BaseMetadata):
|
||||
"""MultipleOf(multiple_of=x) might be interpreted in two ways:
|
||||
|
||||
1. Python semantics, implying ``value % multiple_of == 0``, or
|
||||
2. JSONschema semantics, where ``int(value / multiple_of) == value / multiple_of``
|
||||
|
||||
We encourage users to be aware of these two common interpretations,
|
||||
and libraries to carefully document which they implement.
|
||||
"""
|
||||
|
||||
multiple_of: Union[SupportsDiv, SupportsMod]
|
||||
|
||||
|
||||
@dataclass(frozen=True, **SLOTS)
|
||||
class MinLen(BaseMetadata):
|
||||
"""
|
||||
MinLen() implies minimum inclusive length,
|
||||
e.g. ``len(value) >= min_length``.
|
||||
"""
|
||||
|
||||
min_length: Annotated[int, Ge(0)]
|
||||
|
||||
|
||||
@dataclass(frozen=True, **SLOTS)
|
||||
class MaxLen(BaseMetadata):
|
||||
"""
|
||||
MaxLen() implies maximum inclusive length,
|
||||
e.g. ``len(value) <= max_length``.
|
||||
"""
|
||||
|
||||
max_length: Annotated[int, Ge(0)]
|
||||
|
||||
|
||||
@dataclass(frozen=True, **SLOTS)
|
||||
class Len(GroupedMetadata):
|
||||
"""
|
||||
Len() implies that ``min_length <= len(value) <= max_length``.
|
||||
|
||||
Upper bound may be omitted or ``None`` to indicate no upper length bound.
|
||||
"""
|
||||
|
||||
min_length: Annotated[int, Ge(0)] = 0
|
||||
max_length: Optional[Annotated[int, Ge(0)]] = None
|
||||
|
||||
def __iter__(self) -> Iterator[BaseMetadata]:
|
||||
"""Unpack a Len into zone or more single-bounds."""
|
||||
if self.min_length > 0:
|
||||
yield MinLen(self.min_length)
|
||||
if self.max_length is not None:
|
||||
yield MaxLen(self.max_length)
|
||||
|
||||
|
||||
@dataclass(frozen=True, **SLOTS)
|
||||
class Timezone(BaseMetadata):
|
||||
"""Timezone(tz=...) requires a datetime to be aware (or ``tz=None``, naive).
|
||||
|
||||
``Annotated[datetime, Timezone(None)]`` must be a naive datetime.
|
||||
``Timezone[...]`` (the ellipsis literal) expresses that the datetime must be
|
||||
tz-aware but any timezone is allowed.
|
||||
|
||||
You may also pass a specific timezone string or tzinfo object such as
|
||||
``Timezone(timezone.utc)`` or ``Timezone("Africa/Abidjan")`` to express that
|
||||
you only allow a specific timezone, though we note that this is often
|
||||
a symptom of poor design.
|
||||
"""
|
||||
|
||||
tz: Union[str, tzinfo, EllipsisType, None]
|
||||
|
||||
|
||||
@dataclass(frozen=True, **SLOTS)
|
||||
class Unit(BaseMetadata):
|
||||
"""Indicates that the value is a physical quantity with the specified unit.
|
||||
|
||||
It is intended for usage with numeric types, where the value represents the
|
||||
magnitude of the quantity. For example, ``distance: Annotated[float, Unit('m')]``
|
||||
or ``speed: Annotated[float, Unit('m/s')]``.
|
||||
|
||||
Interpretation of the unit string is left to the discretion of the consumer.
|
||||
It is suggested to follow conventions established by python libraries that work
|
||||
with physical quantities, such as
|
||||
|
||||
- ``pint`` : <https://pint.readthedocs.io/en/stable/>
|
||||
- ``astropy.units``: <https://docs.astropy.org/en/stable/units/>
|
||||
|
||||
For indicating a quantity with a certain dimensionality but without a specific unit
|
||||
it is recommended to use square brackets, e.g. `Annotated[float, Unit('[time]')]`.
|
||||
Note, however, ``annotated_types`` itself makes no use of the unit string.
|
||||
"""
|
||||
|
||||
unit: str
|
||||
|
||||
|
||||
@dataclass(frozen=True, **SLOTS)
|
||||
class Predicate(BaseMetadata):
|
||||
"""``Predicate(func: Callable)`` implies `func(value)` is truthy for valid values.
|
||||
|
||||
Users should prefer statically inspectable metadata, but if you need the full
|
||||
power and flexibility of arbitrary runtime predicates... here it is.
|
||||
|
||||
We provide a few predefined predicates for common string constraints:
|
||||
``IsLower = Predicate(str.islower)``, ``IsUpper = Predicate(str.isupper)``, and
|
||||
``IsDigits = Predicate(str.isdigit)``. Users are encouraged to use methods which
|
||||
can be given special handling, and avoid indirection like ``lambda s: s.lower()``.
|
||||
|
||||
Some libraries might have special logic to handle certain predicates, e.g. by
|
||||
checking for `str.isdigit` and using its presence to both call custom logic to
|
||||
enforce digit-only strings, and customise some generated external schema.
|
||||
|
||||
We do not specify what behaviour should be expected for predicates that raise
|
||||
an exception. For example `Annotated[int, Predicate(str.isdigit)]` might silently
|
||||
skip invalid constraints, or statically raise an error; or it might try calling it
|
||||
and then propagate or discard the resulting exception.
|
||||
"""
|
||||
|
||||
func: Callable[[Any], bool]
|
||||
|
||||
def __repr__(self) -> str:
|
||||
if getattr(self.func, "__name__", "<lambda>") == "<lambda>":
|
||||
return f"{self.__class__.__name__}({self.func!r})"
|
||||
if isinstance(self.func, (types.MethodType, types.BuiltinMethodType)) and (
|
||||
namespace := getattr(self.func.__self__, "__name__", None)
|
||||
):
|
||||
return f"{self.__class__.__name__}({namespace}.{self.func.__name__})"
|
||||
if isinstance(self.func, type(str.isascii)): # method descriptor
|
||||
return f"{self.__class__.__name__}({self.func.__qualname__})"
|
||||
return f"{self.__class__.__name__}({self.func.__name__})"
|
||||
|
||||
|
||||
@dataclass
|
||||
class Not:
|
||||
func: Callable[[Any], bool]
|
||||
|
||||
def __call__(self, __v: Any) -> bool:
|
||||
return not self.func(__v)
|
||||
|
||||
|
||||
_StrType = TypeVar("_StrType", bound=str)
|
||||
|
||||
LowerCase = Annotated[_StrType, Predicate(str.islower)]
|
||||
"""
|
||||
Return True if the string is a lowercase string, False otherwise.
|
||||
|
||||
A string is lowercase if all cased characters in the string are lowercase and there is at least one cased character in the string.
|
||||
""" # noqa: E501
|
||||
UpperCase = Annotated[_StrType, Predicate(str.isupper)]
|
||||
"""
|
||||
Return True if the string is an uppercase string, False otherwise.
|
||||
|
||||
A string is uppercase if all cased characters in the string are uppercase and there is at least one cased character in the string.
|
||||
""" # noqa: E501
|
||||
IsDigit = Annotated[_StrType, Predicate(str.isdigit)]
|
||||
IsDigits = IsDigit # type: ignore # plural for backwards compatibility, see #63
|
||||
"""
|
||||
Return True if the string is a digit string, False otherwise.
|
||||
|
||||
A string is a digit string if all characters in the string are digits and there is at least one character in the string.
|
||||
""" # noqa: E501
|
||||
IsAscii = Annotated[_StrType, Predicate(str.isascii)]
|
||||
"""
|
||||
Return True if all characters in the string are ASCII, False otherwise.
|
||||
|
||||
ASCII characters have code points in the range U+0000-U+007F. Empty string is ASCII too.
|
||||
"""
|
||||
|
||||
_NumericType = TypeVar('_NumericType', bound=Union[SupportsFloat, SupportsIndex])
|
||||
IsFinite = Annotated[_NumericType, Predicate(math.isfinite)]
|
||||
"""Return True if x is neither an infinity nor a NaN, and False otherwise."""
|
||||
IsNotFinite = Annotated[_NumericType, Predicate(Not(math.isfinite))]
|
||||
"""Return True if x is one of infinity or NaN, and False otherwise"""
|
||||
IsNan = Annotated[_NumericType, Predicate(math.isnan)]
|
||||
"""Return True if x is a NaN (not a number), and False otherwise."""
|
||||
IsNotNan = Annotated[_NumericType, Predicate(Not(math.isnan))]
|
||||
"""Return True if x is anything but NaN (not a number), and False otherwise."""
|
||||
IsInfinite = Annotated[_NumericType, Predicate(math.isinf)]
|
||||
"""Return True if x is a positive or negative infinity, and False otherwise."""
|
||||
IsNotInfinite = Annotated[_NumericType, Predicate(Not(math.isinf))]
|
||||
"""Return True if x is neither a positive or negative infinity, and False otherwise."""
|
||||
|
||||
try:
|
||||
from typing_extensions import DocInfo, doc # type: ignore [attr-defined]
|
||||
except ImportError:
|
||||
|
||||
@dataclass(frozen=True, **SLOTS)
|
||||
class DocInfo: # type: ignore [no-redef]
|
||||
""" "
|
||||
The return value of doc(), mainly to be used by tools that want to extract the
|
||||
Annotated documentation at runtime.
|
||||
"""
|
||||
|
||||
documentation: str
|
||||
"""The documentation string passed to doc()."""
|
||||
|
||||
def doc(
|
||||
documentation: str,
|
||||
) -> DocInfo:
|
||||
"""
|
||||
Add documentation to a type annotation inside of Annotated.
|
||||
|
||||
For example:
|
||||
|
||||
>>> def hi(name: Annotated[int, doc("The name of the user")]) -> None: ...
|
||||
"""
|
||||
return DocInfo(documentation)
|
||||
@ -0,0 +1,151 @@
|
||||
import math
|
||||
import sys
|
||||
from datetime import date, datetime, timedelta, timezone
|
||||
from decimal import Decimal
|
||||
from typing import Any, Dict, Iterable, Iterator, List, NamedTuple, Set, Tuple
|
||||
|
||||
if sys.version_info < (3, 9):
|
||||
from typing_extensions import Annotated
|
||||
else:
|
||||
from typing import Annotated
|
||||
|
||||
import annotated_types as at
|
||||
|
||||
|
||||
class Case(NamedTuple):
|
||||
"""
|
||||
A test case for `annotated_types`.
|
||||
"""
|
||||
|
||||
annotation: Any
|
||||
valid_cases: Iterable[Any]
|
||||
invalid_cases: Iterable[Any]
|
||||
|
||||
|
||||
def cases() -> Iterable[Case]:
|
||||
# Gt, Ge, Lt, Le
|
||||
yield Case(Annotated[int, at.Gt(4)], (5, 6, 1000), (4, 0, -1))
|
||||
yield Case(Annotated[float, at.Gt(0.5)], (0.6, 0.7, 0.8, 0.9), (0.5, 0.0, -0.1))
|
||||
yield Case(
|
||||
Annotated[datetime, at.Gt(datetime(2000, 1, 1))],
|
||||
[datetime(2000, 1, 2), datetime(2000, 1, 3)],
|
||||
[datetime(2000, 1, 1), datetime(1999, 12, 31)],
|
||||
)
|
||||
yield Case(
|
||||
Annotated[datetime, at.Gt(date(2000, 1, 1))],
|
||||
[date(2000, 1, 2), date(2000, 1, 3)],
|
||||
[date(2000, 1, 1), date(1999, 12, 31)],
|
||||
)
|
||||
yield Case(
|
||||
Annotated[datetime, at.Gt(Decimal('1.123'))],
|
||||
[Decimal('1.1231'), Decimal('123')],
|
||||
[Decimal('1.123'), Decimal('0')],
|
||||
)
|
||||
|
||||
yield Case(Annotated[int, at.Ge(4)], (4, 5, 6, 1000, 4), (0, -1))
|
||||
yield Case(Annotated[float, at.Ge(0.5)], (0.5, 0.6, 0.7, 0.8, 0.9), (0.4, 0.0, -0.1))
|
||||
yield Case(
|
||||
Annotated[datetime, at.Ge(datetime(2000, 1, 1))],
|
||||
[datetime(2000, 1, 2), datetime(2000, 1, 3)],
|
||||
[datetime(1998, 1, 1), datetime(1999, 12, 31)],
|
||||
)
|
||||
|
||||
yield Case(Annotated[int, at.Lt(4)], (0, -1), (4, 5, 6, 1000, 4))
|
||||
yield Case(Annotated[float, at.Lt(0.5)], (0.4, 0.0, -0.1), (0.5, 0.6, 0.7, 0.8, 0.9))
|
||||
yield Case(
|
||||
Annotated[datetime, at.Lt(datetime(2000, 1, 1))],
|
||||
[datetime(1999, 12, 31), datetime(1999, 12, 31)],
|
||||
[datetime(2000, 1, 2), datetime(2000, 1, 3)],
|
||||
)
|
||||
|
||||
yield Case(Annotated[int, at.Le(4)], (4, 0, -1), (5, 6, 1000))
|
||||
yield Case(Annotated[float, at.Le(0.5)], (0.5, 0.0, -0.1), (0.6, 0.7, 0.8, 0.9))
|
||||
yield Case(
|
||||
Annotated[datetime, at.Le(datetime(2000, 1, 1))],
|
||||
[datetime(2000, 1, 1), datetime(1999, 12, 31)],
|
||||
[datetime(2000, 1, 2), datetime(2000, 1, 3)],
|
||||
)
|
||||
|
||||
# Interval
|
||||
yield Case(Annotated[int, at.Interval(gt=4)], (5, 6, 1000), (4, 0, -1))
|
||||
yield Case(Annotated[int, at.Interval(gt=4, lt=10)], (5, 6), (4, 10, 1000, 0, -1))
|
||||
yield Case(Annotated[float, at.Interval(ge=0.5, le=1)], (0.5, 0.9, 1), (0.49, 1.1))
|
||||
yield Case(
|
||||
Annotated[datetime, at.Interval(gt=datetime(2000, 1, 1), le=datetime(2000, 1, 3))],
|
||||
[datetime(2000, 1, 2), datetime(2000, 1, 3)],
|
||||
[datetime(2000, 1, 1), datetime(2000, 1, 4)],
|
||||
)
|
||||
|
||||
yield Case(Annotated[int, at.MultipleOf(multiple_of=3)], (0, 3, 9), (1, 2, 4))
|
||||
yield Case(Annotated[float, at.MultipleOf(multiple_of=0.5)], (0, 0.5, 1, 1.5), (0.4, 1.1))
|
||||
|
||||
# lengths
|
||||
|
||||
yield Case(Annotated[str, at.MinLen(3)], ('123', '1234', 'x' * 10), ('', '1', '12'))
|
||||
yield Case(Annotated[str, at.Len(3)], ('123', '1234', 'x' * 10), ('', '1', '12'))
|
||||
yield Case(Annotated[List[int], at.MinLen(3)], ([1, 2, 3], [1, 2, 3, 4], [1] * 10), ([], [1], [1, 2]))
|
||||
yield Case(Annotated[List[int], at.Len(3)], ([1, 2, 3], [1, 2, 3, 4], [1] * 10), ([], [1], [1, 2]))
|
||||
|
||||
yield Case(Annotated[str, at.MaxLen(4)], ('', '1234'), ('12345', 'x' * 10))
|
||||
yield Case(Annotated[str, at.Len(0, 4)], ('', '1234'), ('12345', 'x' * 10))
|
||||
yield Case(Annotated[List[str], at.MaxLen(4)], ([], ['a', 'bcdef'], ['a', 'b', 'c']), (['a'] * 5, ['b'] * 10))
|
||||
yield Case(Annotated[List[str], at.Len(0, 4)], ([], ['a', 'bcdef'], ['a', 'b', 'c']), (['a'] * 5, ['b'] * 10))
|
||||
|
||||
yield Case(Annotated[str, at.Len(3, 5)], ('123', '12345'), ('', '1', '12', '123456', 'x' * 10))
|
||||
yield Case(Annotated[str, at.Len(3, 3)], ('123',), ('12', '1234'))
|
||||
|
||||
yield Case(Annotated[Dict[int, int], at.Len(2, 3)], [{1: 1, 2: 2}], [{}, {1: 1}, {1: 1, 2: 2, 3: 3, 4: 4}])
|
||||
yield Case(Annotated[Set[int], at.Len(2, 3)], ({1, 2}, {1, 2, 3}), (set(), {1}, {1, 2, 3, 4}))
|
||||
yield Case(Annotated[Tuple[int, ...], at.Len(2, 3)], ((1, 2), (1, 2, 3)), ((), (1,), (1, 2, 3, 4)))
|
||||
|
||||
# Timezone
|
||||
|
||||
yield Case(
|
||||
Annotated[datetime, at.Timezone(None)], [datetime(2000, 1, 1)], [datetime(2000, 1, 1, tzinfo=timezone.utc)]
|
||||
)
|
||||
yield Case(
|
||||
Annotated[datetime, at.Timezone(...)], [datetime(2000, 1, 1, tzinfo=timezone.utc)], [datetime(2000, 1, 1)]
|
||||
)
|
||||
yield Case(
|
||||
Annotated[datetime, at.Timezone(timezone.utc)],
|
||||
[datetime(2000, 1, 1, tzinfo=timezone.utc)],
|
||||
[datetime(2000, 1, 1), datetime(2000, 1, 1, tzinfo=timezone(timedelta(hours=6)))],
|
||||
)
|
||||
yield Case(
|
||||
Annotated[datetime, at.Timezone('Europe/London')],
|
||||
[datetime(2000, 1, 1, tzinfo=timezone(timedelta(0), name='Europe/London'))],
|
||||
[datetime(2000, 1, 1), datetime(2000, 1, 1, tzinfo=timezone(timedelta(hours=6)))],
|
||||
)
|
||||
|
||||
# Quantity
|
||||
|
||||
yield Case(Annotated[float, at.Unit(unit='m')], (5, 4.2), ('5m', '4.2m'))
|
||||
|
||||
# predicate types
|
||||
|
||||
yield Case(at.LowerCase[str], ['abc', 'foobar'], ['', 'A', 'Boom'])
|
||||
yield Case(at.UpperCase[str], ['ABC', 'DEFO'], ['', 'a', 'abc', 'AbC'])
|
||||
yield Case(at.IsDigit[str], ['123'], ['', 'ab', 'a1b2'])
|
||||
yield Case(at.IsAscii[str], ['123', 'foo bar'], ['£100', '😊', 'whatever 👀'])
|
||||
|
||||
yield Case(Annotated[int, at.Predicate(lambda x: x % 2 == 0)], [0, 2, 4], [1, 3, 5])
|
||||
|
||||
yield Case(at.IsFinite[float], [1.23], [math.nan, math.inf, -math.inf])
|
||||
yield Case(at.IsNotFinite[float], [math.nan, math.inf], [1.23])
|
||||
yield Case(at.IsNan[float], [math.nan], [1.23, math.inf])
|
||||
yield Case(at.IsNotNan[float], [1.23, math.inf], [math.nan])
|
||||
yield Case(at.IsInfinite[float], [math.inf], [math.nan, 1.23])
|
||||
yield Case(at.IsNotInfinite[float], [math.nan, 1.23], [math.inf])
|
||||
|
||||
# check stacked predicates
|
||||
yield Case(at.IsInfinite[Annotated[float, at.Predicate(lambda x: x > 0)]], [math.inf], [-math.inf, 1.23, math.nan])
|
||||
|
||||
# doc
|
||||
yield Case(Annotated[int, at.doc("A number")], [1, 2], [])
|
||||
|
||||
# custom GroupedMetadata
|
||||
class MyCustomGroupedMetadata(at.GroupedMetadata):
|
||||
def __iter__(self) -> Iterator[at.Predicate]:
|
||||
yield at.Predicate(lambda x: float(x).is_integer())
|
||||
|
||||
yield Case(Annotated[float, MyCustomGroupedMetadata()], [0, 2.0], [0.01, 1.5])
|
||||
@ -0,0 +1 @@
|
||||
pip
|
||||
@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
@ -0,0 +1,43 @@
|
||||
Metadata-Version: 2.1
|
||||
Name: astral
|
||||
Version: 3.2
|
||||
Summary: Calculations for the position of the sun and moon.
|
||||
Home-page: https://github.com/sffjunkie/astral
|
||||
License: Apache-2.0
|
||||
Keywords: sun,moon,sunrise,sunset,dawn,dusk
|
||||
Author: Simon Kennedy
|
||||
Author-email: sffjunkie+code@gmail.com
|
||||
Requires-Python: >=3.7,<4.0
|
||||
Classifier: Intended Audience :: Developers
|
||||
Classifier: License :: OSI Approved :: Apache Software License
|
||||
Classifier: Programming Language :: Python
|
||||
Classifier: Programming Language :: Python :: 3
|
||||
Classifier: Programming Language :: Python :: 3.7
|
||||
Classifier: Programming Language :: Python :: 3.8
|
||||
Classifier: Programming Language :: Python :: 3.9
|
||||
Classifier: Programming Language :: Python :: 3.10
|
||||
Classifier: Programming Language :: Python :: 3.10
|
||||
Classifier: Programming Language :: Python :: 3.7
|
||||
Classifier: Programming Language :: Python :: 3.8
|
||||
Classifier: Programming Language :: Python :: 3.9
|
||||
Requires-Dist: backports.zoneinfo; python_version < "3.9"
|
||||
Requires-Dist: tzdata; sys_platform == "win32"
|
||||
Project-URL: Documentation, https://sffjunkie.github.io/astral
|
||||
Project-URL: Repository, https://github.com/sffjunkie/astral
|
||||
Description-Content-Type: text/markdown
|
||||
|
||||
# Astral
|
||||
|
||||
This is 'astral' a Python module which calculates
|
||||
|
||||
- Times for various positions of the sun: dawn, sunrise, solar noon,
|
||||
sunset, dusk, solar elevation, solar azimuth and rahukaalam.
|
||||
- Moon rise, set, azimuth and zenith.
|
||||
- The phase of the moon.
|
||||
|
||||
For documentation see https://sffjunkie.github.io/astral/
|
||||
|
||||
## Package Status
|
||||
|
||||
 
|
||||
|
||||
@ -0,0 +1,25 @@
|
||||
astral-3.2.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
|
||||
astral-3.2.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
||||
astral-3.2.dist-info/METADATA,sha256=2M9fzf_SZamwVd7JIi_-mumvN8VkopI6LbyEG2T2wXg,1673
|
||||
astral-3.2.dist-info/RECORD,,
|
||||
astral-3.2.dist-info/REQUESTED,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
||||
astral-3.2.dist-info/WHEEL,sha256=gSF7fibx4crkLz_A-IKR6kcuq0jJ64KNCkG8_bcaEao,88
|
||||
astral/__init__.py,sha256=jb2kEoCTVcvOMuknIurdIG4NqqF60tIVRb2cEamJ70c,9063
|
||||
astral/__main__.py,sha256=wZZQTOK-8Pm0wh7duX7Z75kVX_Unglf2DhfiEv93VWs,1826
|
||||
astral/__pycache__/__init__.cpython-39.pyc,,
|
||||
astral/__pycache__/__main__.cpython-39.pyc,,
|
||||
astral/__pycache__/geocoder.cpython-39.pyc,,
|
||||
astral/__pycache__/julian.cpython-39.pyc,,
|
||||
astral/__pycache__/location.cpython-39.pyc,,
|
||||
astral/__pycache__/moon.cpython-39.pyc,,
|
||||
astral/__pycache__/sidereal.cpython-39.pyc,,
|
||||
astral/__pycache__/sun.cpython-39.pyc,,
|
||||
astral/__pycache__/table4.cpython-39.pyc,,
|
||||
astral/geocoder.py,sha256=Fsq8ReUgnXYvZM7_Xlc4byUqdT-hiMtt8I36ak1jslU,26132
|
||||
astral/julian.py,sha256=5xR3a9pHPH4JkNDphLWomaTQLEhvx0pXHGtXUadFphs,3280
|
||||
astral/location.py,sha256=udxsm9Mvac5VWOmRUM4wVFk4EHCh8BoMI_6JxcJRvKU,31289
|
||||
astral/moon.py,sha256=z30LPCquIOAWoj0yuQTXorHQ7HQOXdUkc-wMrxhswZo,18310
|
||||
astral/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
||||
astral/sidereal.py,sha256=fcWjlDgoP3S2Y4kIvfBbz4DqiGktfEE8KVKYmdmdClI,748
|
||||
astral/sun.py,sha256=fQbt7TesUwYrcKLHZCSBMtv0T5espQle3u_AXg0wLiM,37691
|
||||
astral/table4.py,sha256=pQRqn7UlECFIDVQFkscu85oWCmnAvpBbvLyM8JEea-8,15715
|
||||
@ -0,0 +1,4 @@
|
||||
Wheel-Version: 1.0
|
||||
Generator: poetry-core 1.2.0
|
||||
Root-Is-Purelib: true
|
||||
Tag: py3-none-any
|
||||
@ -0,0 +1,73 @@
|
||||
import argparse
|
||||
import datetime
|
||||
import json
|
||||
from typing import Any, Dict
|
||||
|
||||
from astral import LocationInfo, Observer, sun
|
||||
|
||||
try:
|
||||
import zoneinfo
|
||||
except ImportError:
|
||||
from backports import zoneinfo
|
||||
|
||||
options = argparse.ArgumentParser()
|
||||
options.add_argument(
|
||||
"-n",
|
||||
"--name",
|
||||
dest="name",
|
||||
default="Somewhere",
|
||||
help="Location name (free-form text)",
|
||||
)
|
||||
options.add_argument(
|
||||
"-r", "--region", dest="region", default="On Earth", help="Region (free-form text)"
|
||||
)
|
||||
options.add_argument(
|
||||
"-d", "--date", dest="date", help="Date to compute times for (yyyy-mm-dd)"
|
||||
)
|
||||
options.add_argument("-t", "--tzname", help="Timezone name")
|
||||
options.add_argument("latitude", type=float, help="Location latitude (float)")
|
||||
options.add_argument("longitude", type=float, help="Location longitude (float)")
|
||||
options.add_argument(
|
||||
"elevation", nargs="?", type=float, default=0.0, help="Elevation in metres (float)"
|
||||
)
|
||||
args = options.parse_args()
|
||||
|
||||
loc = LocationInfo(
|
||||
args.name,
|
||||
args.region,
|
||||
args.tzname,
|
||||
args.latitude,
|
||||
args.longitude,
|
||||
)
|
||||
|
||||
obs = Observer(args.latitude, args.longitude, args.elevation)
|
||||
|
||||
kwargs: Dict[str, Any] = {}
|
||||
kwargs["observer"] = obs
|
||||
|
||||
if args.date is not None:
|
||||
try:
|
||||
kwargs["date"] = datetime.datetime.strptime(args.date, "%Y-%m-%d").date()
|
||||
except: # noqa: E722
|
||||
kwargs["date"] = datetime.date.today()
|
||||
|
||||
sun_as_str = {}
|
||||
format_str = "%Y-%m-%dT%H:%M:%S"
|
||||
if args.tzname is None:
|
||||
tzinfo = datetime.timezone.utc
|
||||
format_str += "Z"
|
||||
else:
|
||||
tzinfo = zoneinfo.ZoneInfo(loc.timezone)
|
||||
format_str += "%z"
|
||||
|
||||
kwargs["tzinfo"] = tzinfo
|
||||
|
||||
s = sun.sun(**kwargs)
|
||||
|
||||
for key, value in s.items():
|
||||
sun_as_str[key] = s[key].strftime(format_str)
|
||||
|
||||
sun_as_str["timezone"] = tzinfo.tzname
|
||||
sun_as_str["location"] = f"{loc.name}, {loc.region}"
|
||||
|
||||
print(json.dumps(sun_as_str))
|
||||
@ -0,0 +1,629 @@
|
||||
"""Astral geocoder is a database of locations stored within the package.
|
||||
|
||||
To get the :class:`~astral.LocationInfo` for a location use the
|
||||
:func:`~astral.geocoder.lookup` function e.g. ::
|
||||
|
||||
from astral.geocoder import lookup, database
|
||||
l = lookup("London", database())
|
||||
|
||||
All locations stored in the database can be accessed using the `all_locations`
|
||||
generator ::
|
||||
|
||||
from astral.geocoder import all_locations
|
||||
for location in all_locations:
|
||||
print(location)
|
||||
"""
|
||||
|
||||
from typing import Dict, Generator, List, Optional, Tuple, Union
|
||||
|
||||
from astral import LocationInfo, dms_to_float
|
||||
|
||||
__all__ = ["lookup", "database", "add_locations", "all_locations"]
|
||||
|
||||
|
||||
# region Location Info
|
||||
# name,region,timezone,latitude,longitude,elevation
|
||||
_LOCATION_INFO = """Abu Dhabi,UAE,Asia/Dubai,24°28'N,54°22'E
|
||||
Abu Dhabi,United Arab Emirates,Asia/Dubai,24°28'N,54°22'E
|
||||
Abuja,Nigeria,Africa/Lagos,09°05'N,07°32'E
|
||||
Accra,Ghana,Africa/Accra,05°35'N,00°06'W
|
||||
Addis Ababa,Ethiopia,Africa/Addis_Ababa,09°02'N,38°42'E
|
||||
Adelaide,Australia,Australia/Adelaide,34°56'S,138°36'E
|
||||
Al Jubail,Saudi Arabia,Asia/Riyadh,25°24'N,49°39'W
|
||||
Algiers,Algeria,Africa/Algiers,36°42'N,03°08'E
|
||||
Amman,Jordan,Asia/Amman,31°57'N,35°52'E
|
||||
Amsterdam,Netherlands,Europe/Amsterdam,52°23'N,04°54'E
|
||||
Andorra la Vella,Andorra,Europe/Andorra,42°31'N,01°32'E
|
||||
Ankara,Turkey,Europe/Istanbul,39°57'N,32°54'E
|
||||
Antananarivo,Madagascar,Indian/Antananarivo,18°55'S,47°31'E
|
||||
Apia,Samoa,Pacific/Apia,13°50'S,171°50'W
|
||||
Ashgabat,Turkmenistan,Asia/Ashgabat,38°00'N,57°50'E
|
||||
Asmara,Eritrea,Africa/Asmara,15°19'N,38°55'E
|
||||
Astana,Kazakhstan,Asia/Qyzylorda,51°10'N,71°30'E
|
||||
Asuncion,Paraguay,America/Asuncion,25°10'S,57°30'W
|
||||
Athens,Greece,Europe/Athens,37°58'N,23°46'E
|
||||
Avarua,Cook Islands,Etc/GMT-10,21°12'N,159°46'W
|
||||
Baghdad,Iraq,Asia/Baghdad,33°20'N,44°30'E
|
||||
Baku,Azerbaijan,Asia/Baku,40°29'N,49°56'E
|
||||
Bamako,Mali,Africa/Bamako,12°34'N,07°55'W
|
||||
Bandar Seri Begawan,Brunei Darussalam,Asia/Brunei,04°52'N,115°00'E
|
||||
Bangkok,Thailand,Asia/Bangkok,13°45'N,100°35'E
|
||||
Bangui,Central African Republic,Africa/Bangui,04°23'N,18°35'E
|
||||
Banjul,Gambia,Africa/Banjul,13°28'N,16°40'W
|
||||
Basse-Terre,Guadeloupe,America/Guadeloupe,16°00'N,61°44'W
|
||||
Basseterre,Saint Kitts and Nevis,America/St_Kitts,17°17'N,62°43'W
|
||||
Beijing,China,Asia/Harbin,39°55'N,116°20'E
|
||||
Beirut,Lebanon,Asia/Beirut,33°53'N,35°31'E
|
||||
Belfast,Northern Ireland,Europe/Belfast,54°36'N,5°56'W
|
||||
Belgrade,Yugoslavia,Europe/Belgrade,44°50'N,20°37'E
|
||||
Belmopan,Belize,America/Belize,17°18'N,88°30'W
|
||||
Berlin,Germany,Europe/Berlin,52°30'N,13°25'E
|
||||
Bern,Switzerland,Europe/Zurich,46°57'N,07°28'E
|
||||
Bishkek,Kyrgyzstan,Asia/Bishkek,42°54'N,74°46'E
|
||||
Bissau,Guinea-Bissau,Africa/Bissau,11°45'N,15°45'W
|
||||
Bloemfontein,South Africa,Africa/Johannesburg,29°12'S,26°07'E
|
||||
Bogota,Colombia,America/Bogota,04°34'N,74°00'W
|
||||
Brasilia,Brazil,Brazil/East,15°47'S,47°55'W
|
||||
Bratislava,Slovakia,Europe/Bratislava,48°10'N,17°07'E
|
||||
Brazzaville,Congo,Africa/Brazzaville,04°09'S,15°12'E
|
||||
Bridgetown,Barbados,America/Barbados,13°05'N,59°30'W
|
||||
Brisbane,Australia,Australia/Brisbane,27°30'S,153°01'E
|
||||
Brussels,Belgium,Europe/Brussels,50°51'N,04°21'E
|
||||
Bucharest,Romania,Europe/Bucharest,44°27'N,26°10'E
|
||||
Bucuresti,Romania,Europe/Bucharest,44°27'N,26°10'E
|
||||
Budapest,Hungary,Europe/Budapest,47°29'N,19°05'E
|
||||
Buenos Aires,Argentina,America/Buenos_Aires,34°62'S,58°44'W
|
||||
Bujumbura,Burundi,Africa/Bujumbura,03°16'S,29°18'E
|
||||
Cairo,Egypt,Africa/Cairo,30°01'N,31°14'E
|
||||
Canberra,Australia,Australia/Canberra,35°15'S,149°08'E
|
||||
Cape Town,South Africa,Africa/Johannesburg,33°55'S,18°22'E
|
||||
Caracas,Venezuela,America/Caracas,10°30'N,66°55'W
|
||||
Castries,Saint Lucia,America/St_Lucia,14°02'N,60°58'W
|
||||
Cayenne,French Guiana,America/Cayenne,05°05'N,52°18'W
|
||||
Charlotte Amalie,United States of Virgin Islands,America/Virgin,18°21'N,64°56'W
|
||||
Chisinau,Moldova,Europe/Chisinau,47°02'N,28°50'E
|
||||
Conakry,Guinea,Africa/Conakry,09°29'N,13°49'W
|
||||
Copenhagen,Denmark,Europe/Copenhagen,55°41'N,12°34'E
|
||||
Cotonou,Benin,Africa/Porto-Novo,06°23'N,02°42'E
|
||||
Dakar,Senegal,Africa/Dakar,14°34'N,17°29'W
|
||||
Damascus,Syrian Arab Republic,Asia/Damascus,33°30'N,36°18'E
|
||||
Dammam,Saudi Arabia,Asia/Riyadh,26°30'N,50°12'E
|
||||
Darwin,Australia,Australia/Darwin,12°26'S,130°50'E
|
||||
Dhaka,Bangladesh,Asia/Dhaka,23°43'N,90°26'E
|
||||
Dili,East Timor,Asia/Dili,08°29'S,125°34'E
|
||||
Djibouti,Djibouti,Africa/Djibouti,11°08'N,42°20'E
|
||||
Dodoma,United Republic of Tanzania,Africa/Dar_es_Salaam,06°08'S,35°45'E
|
||||
Doha,Qatar,Asia/Qatar,25°15'N,51°35'E
|
||||
Douglas,Isle Of Man,Europe/London,54°9'N,4°29'W
|
||||
Dublin,Ireland,Europe/Dublin,53°21'N,06°15'W
|
||||
Dushanbe,Tajikistan,Asia/Dushanbe,38°33'N,68°48'E
|
||||
El Aaiun,Morocco,UTC,27°9'N,13°12'W
|
||||
Fort-de-France,Martinique,America/Martinique,14°36'N,61°02'W
|
||||
Freetown,Sierra Leone,Africa/Freetown,08°30'N,13°17'W
|
||||
Funafuti,Tuvalu,Pacific/Funafuti,08°31'S,179°13'E
|
||||
Gaborone,Botswana,Africa/Gaborone,24°45'S,25°57'E
|
||||
George Town,Cayman Islands,America/Cayman,19°20'N,81°24'W
|
||||
Georgetown,Guyana,America/Guyana,06°50'N,58°12'W
|
||||
Gibraltar,Gibraltar,Europe/Gibraltar,36°9'N,5°21'W
|
||||
Guatemala,Guatemala,America/Guatemala,14°40'N,90°22'W
|
||||
Hanoi,Viet Nam,Asia/Saigon,21°05'N,105°55'E
|
||||
Harare,Zimbabwe,Africa/Harare,17°43'S,31°02'E
|
||||
Havana,Cuba,America/Havana,23°08'N,82°22'W
|
||||
Helsinki,Finland,Europe/Helsinki,60°15'N,25°03'E
|
||||
Hobart,Tasmania,Australia/Hobart,42°53'S,147°19'E
|
||||
Hong Kong,China,Asia/Hong_Kong,22°16'N,114°09'E
|
||||
Honiara,Solomon Islands,Pacific/Guadalcanal,09°27'S,159°57'E
|
||||
Islamabad,Pakistan,Asia/Karachi,33°40'N,73°10'E
|
||||
Jakarta,Indonesia,Asia/Jakarta,06°09'S,106°49'E
|
||||
Jerusalem,Israel,Asia/Jerusalem,31°47'N,35°12'E
|
||||
Juba,South Sudan,Africa/Juba,4°51'N,31°36'E
|
||||
Jubail,Saudi Arabia,Asia/Riyadh,27°02'N,49°39'E
|
||||
Kabul,Afghanistan,Asia/Kabul,34°28'N,69°11'E
|
||||
Kampala,Uganda,Africa/Kampala,00°20'N,32°30'E
|
||||
Kathmandu,Nepal,Asia/Kathmandu,27°45'N,85°20'E
|
||||
Khartoum,Sudan,Africa/Khartoum,15°31'N,32°35'E
|
||||
Kiev,Ukraine,Europe/Kiev,50°30'N,30°28'E
|
||||
Kigali,Rwanda,Africa/Kigali,01°59'S,30°04'E
|
||||
Kingston,Jamaica,America/Jamaica,18°00'N,76°50'W
|
||||
Kingston,Norfolk Island,Pacific/Norfolk,45°20'S,168°43'E
|
||||
Kingstown,Saint Vincent and the Grenadines,America/St_Vincent,13°10'N,61°10'W
|
||||
Kinshasa,Democratic Republic of the Congo,Africa/Kinshasa,04°20'S,15°15'E
|
||||
Koror,Palau,Pacific/Palau,07°20'N,134°28'E
|
||||
Kuala Lumpur,Malaysia,Asia/Kuala_Lumpur,03°09'N,101°41'E
|
||||
Kuwait,Kuwait,Asia/Kuwait,29°30'N,48°00'E
|
||||
La Paz,Bolivia,America/La_Paz,16°20'S,68°10'W
|
||||
Libreville,Gabon,Africa/Libreville,00°25'N,09°26'E
|
||||
Lilongwe,Malawi,Africa/Blantyre,14°00'S,33°48'E
|
||||
Lima,Peru,America/Lima,12°00'S,77°00'W
|
||||
Lisbon,Portugal,Europe/Lisbon,38°42'N,09°10'W
|
||||
Ljubljana,Slovenia,Europe/Ljubljana,46°04'N,14°33'E
|
||||
Lome,Togo,Africa/Lome,06°09'N,01°20'E
|
||||
London,England,Europe/London,51°28'24"N,00°00'3"W
|
||||
Luanda,Angola,Africa/Luanda,08°50'S,13°15'E
|
||||
Lusaka,Zambia,Africa/Lusaka,15°28'S,28°16'E
|
||||
Luxembourg,Luxembourg,Europe/Luxembourg,49°37'N,06°09'E
|
||||
Macau,Macao,Asia/Macau,22°12'N,113°33'E
|
||||
Madinah,Saudi Arabia,Asia/Riyadh,24°28'N,39°36'E
|
||||
Madrid,Spain,Europe/Madrid,40°25'N,03°45'W
|
||||
Majuro,Marshall Islands,Pacific/Majuro,7°4'N,171°16'E
|
||||
Makkah,Saudi Arabia,Asia/Riyadh,21°26'N,39°49'E
|
||||
Malabo,Equatorial Guinea,Africa/Malabo,03°45'N,08°50'E
|
||||
Male,Maldives,Indian/Maldives,04°00'N,73°28'E
|
||||
Mamoudzou,Mayotte,Indian/Mayotte,12°48'S,45°14'E
|
||||
Managua,Nicaragua,America/Managua,12°06'N,86°20'W
|
||||
Manama,Bahrain,Asia/Bahrain,26°10'N,50°30'E
|
||||
Manila,Philippines,Asia/Manila,14°40'N,121°03'E
|
||||
Maputo,Mozambique,Africa/Maputo,25°58'S,32°32'E
|
||||
Maseru,Lesotho,Africa/Maseru,29°18'S,27°30'E
|
||||
Masqat,Oman,Asia/Muscat,23°37'N,58°36'E
|
||||
Mbabane,Swaziland,Africa/Mbabane,26°18'S,31°06'E
|
||||
Mecca,Saudi Arabia,Asia/Riyadh,21°26'N,39°49'E
|
||||
Medina,Saudi Arabia,Asia/Riyadh,24°28'N,39°36'E
|
||||
Melbourne,Australia,Australia/Melbourne,37°48'S,144°57'E
|
||||
Mexico,Mexico,America/Mexico_City,19°20'N,99°10'W
|
||||
Minsk,Belarus,Europe/Minsk,53°52'N,27°30'E
|
||||
Mogadishu,Somalia,Africa/Mogadishu,02°02'N,45°25'E
|
||||
Monaco,Priciplality Of Monaco,Europe/Monaco,43°43'N,7°25'E
|
||||
Monrovia,Liberia,Africa/Monrovia,06°18'N,10°47'W
|
||||
Montevideo,Uruguay,America/Montevideo,34°50'S,56°11'W
|
||||
Moroni,Comoros,Indian/Comoro,11°40'S,43°16'E
|
||||
Moscow,Russian Federation,Europe/Moscow,55°45'N,37°35'E
|
||||
Moskva,Russian Federation,Europe/Moscow,55°45'N,37°35'E
|
||||
Mumbai,India,Asia/Kolkata,18°58'N,72°49'E
|
||||
Muscat,Oman,Asia/Muscat,23°37'N,58°32'E
|
||||
N'Djamena,Chad,Africa/Ndjamena,12°10'N,14°59'E
|
||||
Nairobi,Kenya,Africa/Nairobi,01°17'S,36°48'E
|
||||
Nassau,Bahamas,America/Nassau,25°05'N,77°20'W
|
||||
Naypyidaw,Myanmar,Asia/Rangoon,19°45'N,96°6'E
|
||||
New Delhi,India,Asia/Kolkata,28°37'N,77°13'E
|
||||
Ngerulmud,Palau,Pacific/Palau,7°30'N,134°37'E
|
||||
Niamey,Niger,Africa/Niamey,13°27'N,02°06'E
|
||||
Nicosia,Cyprus,Asia/Nicosia,35°10'N,33°25'E
|
||||
Nouakchott,Mauritania,Africa/Nouakchott,20°10'S,57°30'E
|
||||
Noumea,New Caledonia,Pacific/Noumea,22°17'S,166°30'E
|
||||
Nuku'alofa,Tonga,Pacific/Tongatapu,21°10'S,174°00'W
|
||||
Nuuk,Greenland,America/Godthab,64°10'N,51°35'W
|
||||
Oranjestad,Aruba,America/Aruba,12°32'N,70°02'W
|
||||
Oslo,Norway,Europe/Oslo,59°55'N,10°45'E
|
||||
Ottawa,Canada,US/Eastern,45°27'N,75°42'W
|
||||
Ouagadougou,Burkina Faso,Africa/Ouagadougou,12°15'N,01°30'W
|
||||
P'yongyang,Democratic People's Republic of Korea,Asia/Pyongyang,39°09'N,125°30'E
|
||||
Pago Pago,American Samoa,Pacific/Pago_Pago,14°16'S,170°43'W
|
||||
Palikir,Micronesia,Pacific/Ponape,06°55'N,158°09'E
|
||||
Panama,Panama,America/Panama,09°00'N,79°25'W
|
||||
Papeete,French Polynesia,Pacific/Tahiti,17°32'S,149°34'W
|
||||
Paramaribo,Suriname,America/Paramaribo,05°50'N,55°10'W
|
||||
Paris,France,Europe/Paris,48°50'N,02°20'E
|
||||
Perth,Australia,Australia/Perth,31°56'S,115°50'E
|
||||
Phnom Penh,Cambodia,Asia/Phnom_Penh,11°33'N,104°55'E
|
||||
Podgorica,Montenegro,Europe/Podgorica,42°28'N,19°16'E
|
||||
Port Louis,Mauritius,Indian/Mauritius,20°9'S,57°30'E
|
||||
Port Moresby,Papua New Guinea,Pacific/Port_Moresby,09°24'S,147°08'E
|
||||
Port-Vila,Vanuatu,Pacific/Efate,17°45'S,168°18'E
|
||||
Port-au-Prince,Haiti,America/Port-au-Prince,18°40'N,72°20'W
|
||||
Port of Spain,Trinidad and Tobago,America/Port_of_Spain,10°40'N,61°31'W
|
||||
Porto-Novo,Benin,Africa/Porto-Novo,06°23'N,02°42'E
|
||||
Prague,Czech Republic,Europe/Prague,50°05'N,14°22'E
|
||||
Praia,Cape Verde,Atlantic/Cape_Verde,15°02'N,23°34'W
|
||||
Pretoria,South Africa,Africa/Johannesburg,25°44'S,28°12'E
|
||||
Pristina,Albania,Europe/Tirane,42°40'N,21°10'E
|
||||
Quito,Ecuador,America/Guayaquil,00°15'S,78°35'W
|
||||
Rabat,Morocco,Africa/Casablanca,34°1'N,6°50'W
|
||||
Reykjavik,Iceland,Atlantic/Reykjavik,64°10'N,21°57'W
|
||||
Riga,Latvia,Europe/Riga,56°53'N,24°08'E
|
||||
Riyadh,Saudi Arabia,Asia/Riyadh,24°41'N,46°42'E
|
||||
Road Town,British Virgin Islands,America/Virgin,18°27'N,64°37'W
|
||||
Rome,Italy,Europe/Rome,41°54'N,12°29'E
|
||||
Roseau,Dominica,America/Dominica,15°20'N,61°24'W
|
||||
Saint Helier,Jersey,Etc/GMT,49°11'N,2°6'W
|
||||
Saint Pierre,Saint Pierre and Miquelon,America/Miquelon,46°46'N,56°12'W
|
||||
Saipan,Northern Mariana Islands,Pacific/Saipan,15°12'N,145°45'E
|
||||
Sana,Yemen,Asia/Aden,15°20'N,44°12'W
|
||||
Sana'a,Yemen,Asia/Aden,15°20'N,44°12'W
|
||||
San Jose,Costa Rica,America/Costa_Rica,09°55'N,84°02'W
|
||||
San Juan,Puerto Rico,America/Puerto_Rico,18°28'N,66°07'W
|
||||
San Marino,San Marino,Europe/San_Marino,43°55'N,12°30'E
|
||||
San Salvador,El Salvador,America/El_Salvador,13°40'N,89°10'W
|
||||
Santiago,Chile,America/Santiago,33°24'S,70°40'W
|
||||
Santo Domingo,Dominica Republic,America/Santo_Domingo,18°30'N,69°59'W
|
||||
Sao Tome,Sao Tome and Principe,Africa/Sao_Tome,00°10'N,06°39'E
|
||||
Sarajevo,Bosnia and Herzegovina,Europe/Sarajevo,43°52'N,18°26'E
|
||||
Seoul,Republic of Korea,Asia/Seoul,37°31'N,126°58'E
|
||||
Singapore,Republic of Singapore,Asia/Singapore,1°18'N,103°48'E
|
||||
Skopje,The Former Yugoslav Republic of Macedonia,Europe/Skopje,42°01'N,21°26'E
|
||||
Sofia,Bulgaria,Europe/Sofia,42°45'N,23°20'E
|
||||
Sri Jayawardenapura Kotte,Sri Lanka,Asia/Colombo,6°54'N,79°53'E
|
||||
St. George's,Grenada,America/Grenada,32°22'N,64°40'W
|
||||
St. John's,Antigua and Barbuda,America/Antigua,17°7'N,61°51'W
|
||||
St. Peter Port,Guernsey,Europe/Guernsey,49°26'N,02°33'W
|
||||
Stanley,Falkland Islands,Atlantic/Stanley,51°40'S,59°51'W
|
||||
Stockholm,Sweden,Europe/Stockholm,59°20'N,18°05'E
|
||||
Sucre,Bolivia,America/La_Paz,16°20'S,68°10'W
|
||||
Suva,Fiji,Pacific/Fiji,18°06'S,178°30'E
|
||||
Sydney,Australia,Australia/Sydney,33°53'S,151°13'E
|
||||
Taipei,Republic of China (Taiwan),Asia/Taipei,25°02'N,121°38'E
|
||||
T'bilisi,Georgia,Asia/Tbilisi,41°43'N,44°50'E
|
||||
Tbilisi,Georgia,Asia/Tbilisi,41°43'N,44°50'E
|
||||
Tallinn,Estonia,Europe/Tallinn,59°22'N,24°48'E
|
||||
Tarawa,Kiribati,Pacific/Tarawa,01°30'N,173°00'E
|
||||
Tashkent,Uzbekistan,Asia/Tashkent,41°20'N,69°10'E
|
||||
Tegucigalpa,Honduras,America/Tegucigalpa,14°05'N,87°14'W
|
||||
Tehran,Iran,Asia/Tehran,35°44'N,51°30'E
|
||||
Thimphu,Bhutan,Asia/Thimphu,27°31'N,89°45'E
|
||||
Tirana,Albania,Europe/Tirane,41°18'N,19°49'E
|
||||
Tirane,Albania,Europe/Tirane,41°18'N,19°49'E
|
||||
Torshavn,Faroe Islands,Atlantic/Faroe,62°05'N,06°56'W
|
||||
Tokyo,Japan,Asia/Tokyo,35°41'N,139°41'E
|
||||
Tripoli,Libyan Arab Jamahiriya,Africa/Tripoli,32°49'N,13°07'E
|
||||
Tunis,Tunisia,Africa/Tunis,36°50'N,10°11'E
|
||||
Ulan Bator,Mongolia,Asia/Ulaanbaatar,47°55'N,106°55'E
|
||||
Ulaanbaatar,Mongolia,Asia/Ulaanbaatar,47°55'N,106°55'E
|
||||
Vaduz,Liechtenstein,Europe/Vaduz,47°08'N,09°31'E
|
||||
Valletta,Malta,Europe/Malta,35°54'N,14°31'E
|
||||
Vienna,Austria,Europe/Vienna,48°12'N,16°22'E
|
||||
Vientiane,Lao People's Democratic Republic,Asia/Vientiane,17°58'N,102°36'E
|
||||
Vilnius,Lithuania,Europe/Vilnius,54°38'N,25°19'E
|
||||
W. Indies,Antigua and Barbuda,America/Antigua,17°20'N,61°48'W
|
||||
Warsaw,Poland,Europe/Warsaw,52°13'N,21°00'E
|
||||
Washington DC,USA,US/Eastern,39°91'N,77°02'W
|
||||
Wellington,New Zealand,Pacific/Auckland,41°19'S,174°46'E
|
||||
Willemstad,Netherlands Antilles,America/Curacao,12°05'N,69°00'W
|
||||
Windhoek,Namibia,Africa/Windhoek,22°35'S,17°04'E
|
||||
Yamoussoukro,Cote d'Ivoire,Africa/Abidjan,06°49'N,05°17'W
|
||||
Yangon,Myanmar,Asia/Rangoon,16°45'N,96°20'E
|
||||
Yaounde,Cameroon,Africa/Douala,03°50'N,11°35'E
|
||||
Yaren,Nauru,Pacific/Nauru,0°32'S,166°55'E
|
||||
Yerevan,Armenia,Asia/Yerevan,40°10'N,44°31'E
|
||||
Zagreb,Croatia,Europe/Zagreb,45°50'N,15°58'E
|
||||
Zurich,Switzerland,Europe/Zurich,47°22'N,08°33'E
|
||||
|
||||
# UK Cities
|
||||
Aberdeen,Scotland,Europe/London,57°08'N,02°06'W
|
||||
Birmingham,England,Europe/London,52°30'N,01°50'W
|
||||
Bolton,England,Europe/London,53°35'N,02°15'W
|
||||
Bradford,England,Europe/London,53°47'N,01°45'W
|
||||
Bristol,England,Europe/London,51°28'N,02°35'W
|
||||
Cardiff,Wales,Europe/London,51°29'N,03°13'W
|
||||
Crawley,England,Europe/London,51°8'N,00°10'W
|
||||
Edinburgh,Scotland,Europe/London,55°57'N,03°13'W
|
||||
Glasgow,Scotland,Europe/London,55°50'N,04°15'W
|
||||
Greenwich,England,Europe/London,51°28'N,00°00'W
|
||||
Leeds,England,Europe/London,53°48'N,01°35'W
|
||||
Leicester,England,Europe/London,52°38'N,01°08'W
|
||||
Liverpool,England,Europe/London,53°25'N,03°00'W
|
||||
Manchester,England,Europe/London,53°30'N,02°15'W
|
||||
Newcastle Upon Tyne,England,Europe/London,54°59'N,01°36'W
|
||||
Newcastle,England,Europe/London,54°59'N,01°36'W
|
||||
Norwich,England,Europe/London,52°38'N,01°18'E
|
||||
Oxford,England,Europe/London,51°45'N,01°15'W
|
||||
Plymouth,England,Europe/London,50°25'N,04°15'W
|
||||
Portsmouth,England,Europe/London,50°48'N,01°05'W
|
||||
Reading,England,Europe/London,51°27'N,0°58'W
|
||||
Sheffield,England,Europe/London,53°23'N,01°28'W
|
||||
Southampton,England,Europe/London,50°55'N,01°25'W
|
||||
Swansea,England,Europe/London,51°37'N,03°57'W
|
||||
Swindon,England,Europe/London,51°34'N,01°47'W
|
||||
Wolverhampton,England,Europe/London,52°35'N,2°08'W
|
||||
Barrow-In-Furness,England,Europe/London,54°06'N,3°13'W
|
||||
|
||||
# US State Capitals
|
||||
Montgomery,USA,US/Central,32°21'N,86°16'W
|
||||
Juneau,USA,US/Alaska,58°23'N,134°11'W
|
||||
Phoenix,USA,America/Phoenix,33°26'N,112°04'W
|
||||
Little Rock,USA,US/Central,34°44'N,92°19'W
|
||||
Sacramento,USA,US/Pacific,38°33'N,121°28'W
|
||||
Denver,USA,US/Mountain,39°44'N,104°59'W
|
||||
Hartford,USA,US/Eastern,41°45'N,72°41'W
|
||||
Dover,USA,US/Eastern,39°09'N,75°31'W
|
||||
Tallahassee,USA,US/Eastern,30°27'N,84°16'W
|
||||
Atlanta,USA,US/Eastern,33°45'N,84°23'W
|
||||
Honolulu,USA,US/Hawaii,21°18'N,157°49'W
|
||||
Boise,USA,US/Mountain,43°36'N,116°12'W
|
||||
Springfield,USA,US/Central,39°47'N,89°39'W
|
||||
Indianapolis,USA,US/Eastern,39°46'N,86°9'W
|
||||
Des Moines,USA,US/Central,41°35'N,93°37'W
|
||||
Topeka,USA,US/Central,39°03'N,95°41'W
|
||||
Frankfort,USA,US/Eastern,38°11'N,84°51'W
|
||||
Baton Rouge,USA,US/Central,30°27'N,91°8'W
|
||||
Augusta,USA,US/Eastern,44°18'N,69°46'W
|
||||
Annapolis,USA,US/Eastern,38°58'N,76°30'W
|
||||
Boston,USA,US/Eastern,42°21'N,71°03'W
|
||||
Lansing,USA,US/Eastern,42°44'N,84°32'W
|
||||
Saint Paul,USA,US/Central,44°56'N,93°05'W
|
||||
Jackson,USA,US/Central,32°17'N,90°11'W
|
||||
Jefferson City,USA,US/Central,38°34'N,92°10'W
|
||||
Helena,USA,US/Mountain,46°35'N,112°1'W
|
||||
Lincoln,USA,US/Central,40°48'N,96°40'W
|
||||
Carson City,USA,US/Pacific,39°9'N,119°45'W
|
||||
Concord,USA,US/Eastern,43°12'N,71°32'W
|
||||
Trenton,USA,US/Eastern,40°13'N,74°45'W
|
||||
Santa Fe,USA,US/Mountain,35°40'N,105°57'W
|
||||
Albany,USA,US/Eastern,42°39'N,73°46'W
|
||||
Raleigh,USA,US/Eastern,35°49'N,78°38'W
|
||||
Bismarck,USA,US/Central,46°48'N,100°46'W
|
||||
Columbus,USA,US/Eastern,39°59'N,82°59'W
|
||||
Oklahoma City,USA,US/Central,35°28'N,97°32'W
|
||||
Salem,USA,US/Pacific,44°55'N,123°1'W
|
||||
Harrisburg,USA,US/Eastern,40°16'N,76°52'W
|
||||
Providence,USA,US/Eastern,41°49'N,71°25'W
|
||||
Columbia,USA,US/Eastern,34°00'N,81°02'W
|
||||
Pierre,USA,US/Central,44°22'N,100°20'W
|
||||
Nashville,USA,US/Central,36°10'N,86°47'W
|
||||
Austin,USA,US/Central,30°16'N,97°45'W
|
||||
Salt Lake City,USA,US/Mountain,40°45'N,111°53'W
|
||||
Montpelier,USA,US/Eastern,44°15'N,72°34'W
|
||||
Richmond,USA,US/Eastern,37°32'N,77°25'W
|
||||
Olympia,USA,US/Pacific,47°2'N,122°53'W
|
||||
Charleston,USA,US/Eastern,38°20'N,81°38'W
|
||||
Madison,USA,US/Central,43°4'N,89°24'W
|
||||
Cheyenne,USA,US/Mountain,41°8'N,104°48'W
|
||||
|
||||
# Major US Cities
|
||||
Birmingham,USA,US/Central,33°39'N,86°48'W
|
||||
Anchorage,USA,US/Alaska,61°13'N,149°53'W
|
||||
Los Angeles,USA,US/Pacific,34°03'N,118°15'W
|
||||
San Francisco,USA,US/Pacific,37°46'N,122°25'W
|
||||
Bridgeport,USA,US/Eastern,41°11'N,73°11'W
|
||||
Wilmington,USA,US/Eastern,39°44'N,75°32'W
|
||||
Jacksonville,USA,US/Eastern,30°19'N,81°39'W
|
||||
Miami,USA,US/Eastern,26°8'N,80°12'W
|
||||
Chicago,USA,US/Central,41°50'N,87°41'W
|
||||
Wichita,USA,US/Central,37°41'N,97°20'W
|
||||
Louisville,USA,US/Eastern,38°15'N,85°45'W
|
||||
New Orleans,USA,US/Central,29°57'N,90°4'W
|
||||
Portland,USA,US/Eastern,43°39'N,70°16'W
|
||||
Baltimore,USA,US/Eastern,39°17'N,76°37'W
|
||||
Detroit,USA,US/Eastern,42°19'N,83°2'W
|
||||
Minneapolis,USA,US/Central,44°58'N,93°15'W
|
||||
Kansas City,USA,US/Central,39°06'N,94°35'W
|
||||
Billings,USA,US/Mountain,45°47'N,108°32'W
|
||||
Omaha,USA,US/Central,41°15'N,96°0'W
|
||||
Las Vegas,USA,US/Pacific,36°10'N,115°08'W
|
||||
Manchester,USA,US/Eastern,42°59'N,71°27'W
|
||||
Newark,USA,US/Eastern,40°44'N,74°11'W
|
||||
Albuquerque,USA,US/Mountain,35°06'N,106°36'W
|
||||
New York,USA,US/Eastern,40°43'N,74°0'W
|
||||
Charlotte,USA,US/Eastern,35°13'N,80°50'W
|
||||
Fargo,USA,US/Central,46°52'N,96°47'W
|
||||
Cleveland,USA,US/Eastern,41°28'N,81°40'W
|
||||
Philadelphia,USA,US/Eastern,39°57'N,75°10'W
|
||||
Sioux Falls,USA,US/Central,43°32'N,96°43'W
|
||||
Memphis,USA,US/Central,35°07'N,89°58'W
|
||||
Houston,USA,US/Central,29°45'N,95°22'W
|
||||
Dallas,USA,US/Central,32°47'N,96°48'W
|
||||
Burlington,USA,US/Eastern,44°28'N,73°9'W
|
||||
Virginia Beach,USA,US/Eastern,36°50'N,76°05'W
|
||||
Seattle,USA,US/Pacific,47°36'N,122°19'W
|
||||
Milwaukee,USA,US/Central,43°03'N,87°57'W
|
||||
San Diego,USA,US/Pacific,32°42'N,117°09'W
|
||||
Orlando,USA,US/Eastern,28°32'N,81°22'W
|
||||
Buffalo,USA,US/Eastern,42°54'N,78°50'W
|
||||
Toledo,USA,US/Eastern,41°39'N,83°34'W
|
||||
|
||||
# Canadian cities
|
||||
Vancouver,Canada,America/Vancouver,49°15'N,123°6'W
|
||||
Calgary,Canada,America/Edmonton,51°2'N,114°3'W
|
||||
Edmonton,Canada,America/Edmonton,53°32'N,113°29'W
|
||||
Saskatoon,Canada,America/Regina,52°8'N,106°40'W
|
||||
Regina,Canada,America/Regina,50°27'N,104°36'W
|
||||
Winnipeg,Canada,America/Winnipeg,49°53'N,97°8'W
|
||||
Toronto,Canada,America/Toronto,43°39'N,79°22'W
|
||||
Montreal,Canada,America/Montreal,45°30'N,73°33'W
|
||||
Quebec,Canada,America/Toronto,46°48'N,71°14'W
|
||||
Fredericton,Canada,America/Halifax,45°57'N,66°38'W
|
||||
Halifax,Canada,America/Halifax,44°38'N,63°34'W
|
||||
Charlottetown,Canada,America/Halifax,46°14'N,63°7'W
|
||||
St. John's,Canada,America/Halifax,47°33'N,52°42'W
|
||||
Whitehorse,Canada,America/Whitehorse,60°43'N,135°3'W
|
||||
Yellowknife,Canada,America/Yellowknife,62°27'N,114°22'W
|
||||
Iqaluit,Canada,America/Iqaluit,63°44'N,68°31'W
|
||||
"""
|
||||
# endregion
|
||||
|
||||
GroupName = str
|
||||
LocationName = str
|
||||
GroupInfo = Dict[LocationName, List[LocationInfo]]
|
||||
LocationDatabase = Dict[GroupName, GroupInfo]
|
||||
|
||||
|
||||
def database() -> LocationDatabase:
|
||||
"""Returns a database populated with the inital set of locations stored
|
||||
in this module
|
||||
"""
|
||||
db: LocationDatabase = {}
|
||||
_add_locations_from_str(_LOCATION_INFO, db)
|
||||
return db
|
||||
|
||||
|
||||
def _sanitize_key(key: str) -> str:
|
||||
"""Sanitize the location or group key to look up
|
||||
|
||||
Args:
|
||||
key: The key to sanitize
|
||||
"""
|
||||
return str(key).lower().replace(" ", "_")
|
||||
|
||||
|
||||
def _get_group(name: str, db: LocationDatabase) -> Optional[GroupInfo]:
|
||||
return db.get(name, None)
|
||||
|
||||
|
||||
def _add_location_to_db(location: LocationInfo, db: LocationDatabase) -> None:
|
||||
"""Add a single location to a database"""
|
||||
key = _sanitize_key(location.timezone_group)
|
||||
group = _get_group(key, db)
|
||||
if not group:
|
||||
group = {}
|
||||
db[key] = group
|
||||
|
||||
location_key = _sanitize_key(location.name)
|
||||
if location_key not in group:
|
||||
group[location_key] = [location]
|
||||
else:
|
||||
group[location_key].append(location)
|
||||
|
||||
|
||||
def _locationinfo_from_str(info: str) -> LocationInfo:
|
||||
idxable = info.split(",")
|
||||
return LocationInfo(
|
||||
name=idxable[0],
|
||||
region=idxable[1],
|
||||
timezone=idxable[2],
|
||||
latitude=dms_to_float(idxable[3], 90.0),
|
||||
longitude=dms_to_float(idxable[4], 180.0),
|
||||
)
|
||||
|
||||
|
||||
def _locationinfo_from_indexable(
|
||||
idxable: Union[Tuple[str, ...], List[str]]
|
||||
) -> LocationInfo:
|
||||
return LocationInfo(
|
||||
name=idxable[0],
|
||||
region=idxable[1],
|
||||
timezone=idxable[2],
|
||||
latitude=dms_to_float(idxable[3], 90.0),
|
||||
longitude=dms_to_float(idxable[4], 180.0),
|
||||
)
|
||||
|
||||
|
||||
def _add_locations_from_str(location_string: str, db: LocationDatabase) -> None:
|
||||
"""Add locations from a string."""
|
||||
|
||||
for line in location_string.split("\n"):
|
||||
line = line.strip()
|
||||
if line != "" and line[0] != "#":
|
||||
location = _locationinfo_from_str(line)
|
||||
_add_location_to_db(location, db)
|
||||
|
||||
|
||||
def _add_locations_from_list(
|
||||
location_list: Union[List[str], List[List[str]], List[Tuple[str, ...]]],
|
||||
db: LocationDatabase,
|
||||
) -> None:
|
||||
"""Add locations from a list of either strings or lists of strings
|
||||
or tuples of strings.
|
||||
"""
|
||||
for info in location_list:
|
||||
if isinstance(info, str):
|
||||
_add_locations_from_str(info, db)
|
||||
else:
|
||||
location = _locationinfo_from_indexable(info)
|
||||
_add_location_to_db(location, db)
|
||||
|
||||
|
||||
def add_locations(
|
||||
locations: Union[str, List[str], List[List[str]], List[Tuple[str, ...]]],
|
||||
db: LocationDatabase,
|
||||
) -> None:
|
||||
"""Add locations to the database.
|
||||
|
||||
Locations can be added by passing either a string with one line per location or
|
||||
by passing a list containing strings, lists or tuples (lists and tuples are
|
||||
passed directly to the LocationInfo constructor)."""
|
||||
if isinstance(locations, str):
|
||||
_add_locations_from_str(locations, db)
|
||||
else:
|
||||
_add_locations_from_list(locations, db)
|
||||
|
||||
|
||||
def group(region: str, db: LocationDatabase) -> GroupInfo:
|
||||
"""Access to each timezone group. For example London is in timezone
|
||||
group Europe.
|
||||
|
||||
Lookups are case insensitive
|
||||
|
||||
Args:
|
||||
region: the name to look up
|
||||
|
||||
Raises:
|
||||
KeyError: if the location is not found
|
||||
"""
|
||||
key = _sanitize_key(region)
|
||||
for name, value in db.items():
|
||||
if name == key:
|
||||
return value
|
||||
|
||||
raise KeyError(f"Unrecognised Group - {region}")
|
||||
|
||||
|
||||
def lookup_in_group(
|
||||
location: str, group: Dict[str, List[LocationInfo]]
|
||||
) -> LocationInfo:
|
||||
"""Looks up the location within a group dictionary
|
||||
|
||||
You can supply an optional region name by adding a comma
|
||||
followed by the region name. Where multiple locations have the
|
||||
same name you may need to supply the region name otherwise
|
||||
the first result will be returned which may not be the one
|
||||
you're looking for::
|
||||
|
||||
location = group['Abu Dhabi,United Arab Emirates']
|
||||
|
||||
Lookups are case insensitive.
|
||||
|
||||
Args:
|
||||
location: The location to look up
|
||||
group: The location group to look in
|
||||
|
||||
Raises:
|
||||
KeyError: if the location is not found
|
||||
"""
|
||||
key = _sanitize_key(location)
|
||||
|
||||
try:
|
||||
lookup_name, lookup_region = key.split(",", 1)
|
||||
except ValueError:
|
||||
lookup_name = key
|
||||
lookup_region = ""
|
||||
|
||||
lookup_name = lookup_name.strip("\"'")
|
||||
lookup_region = lookup_region.strip("\"'")
|
||||
|
||||
for (location_name, location_list) in group.items():
|
||||
if location_name == lookup_name:
|
||||
if lookup_region == "":
|
||||
return location_list[0]
|
||||
|
||||
for loc in location_list:
|
||||
if _sanitize_key(loc.region) == lookup_region:
|
||||
return loc
|
||||
|
||||
raise KeyError(f"Unrecognised location name - {key}")
|
||||
|
||||
|
||||
def lookup(name: str, db: LocationDatabase) -> Union[GroupInfo, LocationInfo]:
|
||||
"""Look up a name in a database.
|
||||
|
||||
If a group with the name specified is a group name then that will
|
||||
be returned. If no group is found a location with the name will be
|
||||
looked up.
|
||||
|
||||
Args:
|
||||
name: The group/location name to look up
|
||||
db: The location database to look in
|
||||
|
||||
Raises:
|
||||
KeyError: if the name is not found
|
||||
"""
|
||||
|
||||
key = _sanitize_key(name)
|
||||
for group_key, group in db.items():
|
||||
if group_key == key:
|
||||
return group
|
||||
|
||||
try:
|
||||
return lookup_in_group(name, group)
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
raise KeyError(f"Unrecognised name - {name}")
|
||||
|
||||
|
||||
def all_locations(db: LocationDatabase) -> Generator[LocationInfo, None, None]:
|
||||
"""A generator that returns all the :class:`~astral.LocationInfo`\\s
|
||||
contained in the database
|
||||
"""
|
||||
for group_info in db.values():
|
||||
for location_list in group_info.values():
|
||||
for location in location_list:
|
||||
yield location
|
||||
@ -0,0 +1,140 @@
|
||||
import datetime
|
||||
from enum import Enum
|
||||
from typing import Union
|
||||
|
||||
|
||||
class Calendar(Enum):
|
||||
GREGORIAN = 1
|
||||
JULIAN = 2
|
||||
|
||||
|
||||
def day_fraction_to_time(fraction: float) -> datetime.time:
|
||||
s = fraction * (24 * 60 * 60)
|
||||
h = int(s / (60 * 60))
|
||||
s -= h * 60 * 60
|
||||
m = int(s / 60)
|
||||
s -= m * 60
|
||||
s = int(s)
|
||||
return datetime.time(h, m, s)
|
||||
|
||||
|
||||
def julianday(
|
||||
at: Union[datetime.datetime, datetime.date], calendar: Calendar = Calendar.GREGORIAN
|
||||
) -> float:
|
||||
"""Calculate the Julian Day (number) for the specified date/time
|
||||
|
||||
julian day numbers for dates are calculated for the start of the day
|
||||
"""
|
||||
|
||||
def _time_to_seconds(t: datetime.time) -> int:
|
||||
return int(t.hour * 3600 + t.minute * 60 + t.second)
|
||||
|
||||
year = at.year
|
||||
month = at.month
|
||||
day = at.day
|
||||
day_fraction = 0
|
||||
if isinstance(at, datetime.datetime):
|
||||
t = _time_to_seconds(at.time())
|
||||
day_fraction = t / (24 * 60 * 60)
|
||||
else:
|
||||
day_fraction = 0
|
||||
|
||||
if month <= 2:
|
||||
year -= 1
|
||||
month += 12
|
||||
|
||||
a = int(year / 100)
|
||||
if calendar == Calendar.GREGORIAN:
|
||||
b = 2 - a + int(a / 4)
|
||||
else:
|
||||
b = 0
|
||||
jd = (
|
||||
int(365.25 * (year + 4716))
|
||||
+ int(30.6001 * (month + 1))
|
||||
+ day
|
||||
+ day_fraction
|
||||
+ b
|
||||
- 1524.5
|
||||
)
|
||||
|
||||
return jd
|
||||
|
||||
|
||||
def julianday_modified(at: datetime.datetime) -> float:
|
||||
"""Calculate the Modified Julian Date number"""
|
||||
|
||||
year = at.year
|
||||
month = at.month
|
||||
day = at.day
|
||||
|
||||
a = 10000 * year + 100 * month + day
|
||||
|
||||
if year < 0:
|
||||
year += 1
|
||||
|
||||
if month <= 2:
|
||||
month += 12
|
||||
year -= 1
|
||||
|
||||
if a <= 15821004.1:
|
||||
b = -2 + (year + 4716) / 4 - 1179
|
||||
else:
|
||||
b = (year / 400) - (year / 100) + (year / 4)
|
||||
|
||||
a = 365 * year - 679004
|
||||
mjd = a + b + int(30.6001 * (month + 1)) + day + at.hour / 24
|
||||
return mjd
|
||||
|
||||
|
||||
def julianday_to_datetime(jd: float) -> datetime.datetime:
|
||||
"""Convert a Julian Day number to a datetime"""
|
||||
jd += 0.5
|
||||
z = int(jd)
|
||||
f = jd - z
|
||||
if z < 2299161:
|
||||
a = z
|
||||
else:
|
||||
alpha = int((z - 1867216.25) / 36524.25)
|
||||
a = z + 1 + alpha + int(alpha / 4.0)
|
||||
|
||||
b = a + 1524
|
||||
c = int((b - 122.1) / 365.25)
|
||||
d = int(365.25 * c)
|
||||
e = int((b - d) / 30.6001)
|
||||
|
||||
d = b - d - int(30.6001 * e) + f
|
||||
day = int(d)
|
||||
t = d - day
|
||||
total_seconds = t * (24 * 60 * 60)
|
||||
hour = int(total_seconds / 3600)
|
||||
total_seconds -= hour * 3600
|
||||
minute = int(total_seconds / 60)
|
||||
total_seconds -= minute * 60
|
||||
seconds = int(total_seconds)
|
||||
|
||||
if e < 14:
|
||||
month = e - 1
|
||||
else:
|
||||
month = e - 13
|
||||
|
||||
if month > 2:
|
||||
year = c - 4716
|
||||
else:
|
||||
year = c - 4715
|
||||
|
||||
return datetime.datetime(year, month, day, hour, minute, seconds)
|
||||
|
||||
|
||||
def julianday_to_juliancentury(julianday: float) -> float:
|
||||
"""Convert a Julian Day number to a Julian Century"""
|
||||
return (julianday - 2451545.0) / 36525.0
|
||||
|
||||
|
||||
def juliancentury_to_julianday(juliancentury: float) -> float:
|
||||
"""Convert a Julian Century number to a Julian Day"""
|
||||
return (juliancentury * 36525.0) + 2451545.0
|
||||
|
||||
|
||||
def julianday_2000(at: Union[datetime.datetime, datetime.date]) -> float:
|
||||
"""Calculate the numer of Julian Days since Jan 1.5, 2000"""
|
||||
return julianday(at) - 2451545.0
|
||||
@ -0,0 +1,878 @@
|
||||
import dataclasses
|
||||
import datetime
|
||||
|
||||
try:
|
||||
import zoneinfo
|
||||
except ImportError:
|
||||
from backports import zoneinfo
|
||||
|
||||
from typing import Any, Dict, Optional, Tuple, Union
|
||||
|
||||
import astral.moon
|
||||
import astral.sun
|
||||
from astral import (
|
||||
Depression,
|
||||
Elevation,
|
||||
LocationInfo,
|
||||
Observer,
|
||||
SunDirection,
|
||||
dms_to_float,
|
||||
today,
|
||||
)
|
||||
|
||||
|
||||
class Location:
|
||||
"""Provides access to information for single location."""
|
||||
|
||||
def __init__(self, info: Optional[LocationInfo] = None):
|
||||
"""Initializes the Location with a LocationInfo object.
|
||||
|
||||
The tuple should contain items in the following order
|
||||
|
||||
================ =============
|
||||
Field Default
|
||||
================ =============
|
||||
name Greenwich
|
||||
region England
|
||||
time zone name Europe/London
|
||||
latitude 51.4733
|
||||
longitude -0.0008333
|
||||
================ =============
|
||||
|
||||
See the :attr:`timezone` property for a method of obtaining time zone
|
||||
names
|
||||
"""
|
||||
|
||||
self._location_info: LocationInfo
|
||||
self._solar_depression: float = Depression.CIVIL.value
|
||||
|
||||
if not info:
|
||||
self._location_info = LocationInfo(
|
||||
"Greenwich", "England", "Europe/London", 51.4733, -0.0008333
|
||||
)
|
||||
else:
|
||||
self._location_info = info
|
||||
|
||||
def __eq__(self, other: object) -> bool:
|
||||
if type(other) is Location:
|
||||
return self._location_info == other._location_info # type: ignore
|
||||
return NotImplemented
|
||||
|
||||
def __repr__(self) -> str:
|
||||
if self.region:
|
||||
_repr = "%s/%s" % (self.name, self.region)
|
||||
else:
|
||||
_repr = self.name
|
||||
return (
|
||||
f"{_repr}, tz={self.timezone}, "
|
||||
f"lat={self.latitude:0.02f}, "
|
||||
f"lon={self.longitude:0.02f}"
|
||||
)
|
||||
|
||||
@property
|
||||
def info(self) -> LocationInfo:
|
||||
return LocationInfo(
|
||||
self.name,
|
||||
self.region,
|
||||
self.timezone,
|
||||
self.latitude,
|
||||
self.longitude,
|
||||
)
|
||||
|
||||
@property
|
||||
def observer(self) -> Observer:
|
||||
return Observer(self.latitude, self.longitude, 0.0)
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self._location_info.name
|
||||
|
||||
@name.setter
|
||||
def name(self, name: str) -> None:
|
||||
self._location_info = dataclasses.replace(self._location_info, name=name)
|
||||
|
||||
@property
|
||||
def region(self) -> str:
|
||||
return self._location_info.region
|
||||
|
||||
@region.setter
|
||||
def region(self, region: str) -> None:
|
||||
self._location_info = dataclasses.replace(self._location_info, region=region)
|
||||
|
||||
@property
|
||||
def latitude(self) -> float:
|
||||
"""The location's latitude
|
||||
|
||||
``latitude`` can be set either as a string or as a number
|
||||
|
||||
For strings they must be of the form
|
||||
|
||||
degrees°minutes'[N|S] e.g. 51°31'N
|
||||
|
||||
For numbers, positive numbers signify latitudes to the North.
|
||||
"""
|
||||
|
||||
return self._location_info.latitude
|
||||
|
||||
@latitude.setter
|
||||
def latitude(self, latitude: Union[float, str]) -> None:
|
||||
self._location_info = dataclasses.replace(
|
||||
self._location_info, latitude=dms_to_float(latitude, 90.0)
|
||||
)
|
||||
|
||||
@property
|
||||
def longitude(self) -> float:
|
||||
"""The location's longitude.
|
||||
|
||||
``longitude`` can be set either as a string or as a number
|
||||
|
||||
For strings they must be of the form
|
||||
|
||||
degrees°minutes'[E|W] e.g. 51°31'W
|
||||
|
||||
For numbers, positive numbers signify longitudes to the East.
|
||||
"""
|
||||
|
||||
return self._location_info.longitude
|
||||
|
||||
@longitude.setter
|
||||
def longitude(self, longitude: Union[float, str]) -> None:
|
||||
self._location_info = dataclasses.replace(
|
||||
self._location_info, longitude=dms_to_float(longitude, 180.0)
|
||||
)
|
||||
|
||||
@property
|
||||
def timezone(self) -> str:
|
||||
"""The name of the time zone for the location.
|
||||
|
||||
A list of time zone names can be obtained from the zoneinfo module.
|
||||
For example.
|
||||
|
||||
>>> import zoneinfo
|
||||
>>> assert "CET" in zoneinfo.available_timezones()
|
||||
"""
|
||||
|
||||
return self._location_info.timezone
|
||||
|
||||
@timezone.setter
|
||||
def timezone(self, name: str) -> None:
|
||||
if name not in zoneinfo.available_timezones(): # type: ignore
|
||||
raise ValueError("Timezone '%s' not recognized" % name)
|
||||
|
||||
self._location_info = dataclasses.replace(self._location_info, timezone=name)
|
||||
|
||||
@property
|
||||
def tzinfo(self) -> zoneinfo.ZoneInfo: # type: ignore
|
||||
"""Time zone information."""
|
||||
|
||||
try:
|
||||
tz = zoneinfo.ZoneInfo(self._location_info.timezone) # type: ignore
|
||||
return tz # type: ignore
|
||||
except zoneinfo.ZoneInfoNotFoundError as exc: # type: ignore
|
||||
raise ValueError(
|
||||
"Unknown timezone '%s'" % self._location_info.timezone
|
||||
) from exc
|
||||
|
||||
tz = tzinfo
|
||||
|
||||
@property
|
||||
def solar_depression(self) -> float:
|
||||
"""The number of degrees the sun must be below the horizon for the
|
||||
dawn/dusk calculation.
|
||||
|
||||
Can either be set as a number of degrees below the horizon or as
|
||||
one of the following strings
|
||||
|
||||
============= =======
|
||||
String Degrees
|
||||
============= =======
|
||||
civil 6.0
|
||||
nautical 12.0
|
||||
astronomical 18.0
|
||||
============= =======
|
||||
"""
|
||||
|
||||
return self._solar_depression
|
||||
|
||||
@solar_depression.setter
|
||||
def solar_depression(self, depression: Union[float, str, Depression]) -> None:
|
||||
if isinstance(depression, str):
|
||||
try:
|
||||
self._solar_depression = {
|
||||
"civil": 6.0,
|
||||
"nautical": 12.0,
|
||||
"astronomical": 18.0,
|
||||
}[depression]
|
||||
except KeyError:
|
||||
raise KeyError(
|
||||
(
|
||||
"solar_depression must be either a number "
|
||||
"or one of 'civil', 'nautical' or "
|
||||
"'astronomical'"
|
||||
)
|
||||
)
|
||||
elif isinstance(depression, Depression):
|
||||
self._solar_depression = depression.value
|
||||
else:
|
||||
self._solar_depression = float(depression)
|
||||
|
||||
def today(self, local: bool = True) -> datetime.date:
|
||||
if local:
|
||||
return today(self.tzinfo)
|
||||
else:
|
||||
return today()
|
||||
|
||||
def sun(
|
||||
self,
|
||||
date: Optional[datetime.date] = None,
|
||||
local: bool = True,
|
||||
observer_elevation: Elevation = 0.0,
|
||||
) -> Dict[str, Any]:
|
||||
"""Returns dawn, sunrise, noon, sunset and dusk as a dictionary.
|
||||
|
||||
:param date: The date for which to calculate the times.
|
||||
If no date is specified then the current date will be used.
|
||||
|
||||
:param local: True = Time to be returned in location's time zone;
|
||||
False = Time to be returned in UTC.
|
||||
If not specified then the time will be returned in local time
|
||||
|
||||
:param observer_elevation: Elevation of the observer in metres above
|
||||
the location.
|
||||
|
||||
:returns: Dictionary with keys ``dawn``, ``sunrise``, ``noon``,
|
||||
``sunset`` and ``dusk`` whose values are the results of the
|
||||
corresponding methods.
|
||||
"""
|
||||
|
||||
if local and self.timezone is None:
|
||||
raise ValueError("Local time requested but Location has no timezone set.")
|
||||
|
||||
if date is None:
|
||||
date = self.today(local)
|
||||
|
||||
observer = Observer(self.latitude, self.longitude, observer_elevation)
|
||||
|
||||
if local:
|
||||
return astral.sun.sun(observer, date, self.solar_depression, self.tzinfo)
|
||||
else:
|
||||
return astral.sun.sun(observer, date, self.solar_depression)
|
||||
|
||||
def dawn(
|
||||
self,
|
||||
date: Optional[datetime.date] = None,
|
||||
local: bool = True,
|
||||
observer_elevation: Elevation = 0.0,
|
||||
) -> datetime.datetime:
|
||||
"""Calculates the time in the morning when the sun is a certain number
|
||||
of degrees below the horizon. By default this is 6 degrees but can be
|
||||
changed by setting the :attr:`Astral.solar_depression` property.
|
||||
|
||||
:param date: The date for which to calculate the dawn time.
|
||||
If no date is specified then the current date will be used.
|
||||
|
||||
:param local: True = Time to be returned in location's time zone;
|
||||
False = Time to be returned in UTC.
|
||||
If not specified then the time will be returned in local time
|
||||
|
||||
:param observer_elevation: Elevation of the observer in metres above
|
||||
the location.
|
||||
|
||||
:returns: The date and time at which dawn occurs.
|
||||
"""
|
||||
|
||||
if local and self.timezone is None:
|
||||
raise ValueError("Local time requested but Location has no timezone set.")
|
||||
|
||||
if date is None:
|
||||
date = self.today(local)
|
||||
|
||||
observer = Observer(self.latitude, self.longitude, observer_elevation)
|
||||
|
||||
if local:
|
||||
return astral.sun.dawn(observer, date, self.solar_depression, self.tzinfo)
|
||||
else:
|
||||
return astral.sun.dawn(observer, date, self.solar_depression)
|
||||
|
||||
def sunrise(
|
||||
self,
|
||||
date: Optional[datetime.date] = None,
|
||||
local: bool = True,
|
||||
observer_elevation: Elevation = 0.0,
|
||||
) -> datetime.datetime:
|
||||
"""Return sunrise time.
|
||||
|
||||
Calculates the time in the morning when the sun is a 0.833 degrees
|
||||
below the horizon. This is to account for refraction.
|
||||
|
||||
:param date: The date for which to calculate the sunrise time.
|
||||
If no date is specified then the current date will be used.
|
||||
|
||||
:param local: True = Time to be returned in location's time zone;
|
||||
False = Time to be returned in UTC.
|
||||
If not specified then the time will be returned in local time
|
||||
|
||||
:param observer_elevation: Elevation of the observer in metres above
|
||||
the location.
|
||||
|
||||
:returns: The date and time at which sunrise occurs.
|
||||
"""
|
||||
|
||||
if local and self.timezone is None:
|
||||
raise ValueError("Local time requested but Location has no timezone set.")
|
||||
|
||||
if date is None:
|
||||
date = self.today(local)
|
||||
|
||||
observer = Observer(self.latitude, self.longitude, observer_elevation)
|
||||
|
||||
if local:
|
||||
return astral.sun.sunrise(observer, date, self.tzinfo)
|
||||
else:
|
||||
return astral.sun.sunrise(observer, date)
|
||||
|
||||
def noon(
|
||||
self, date: Optional[datetime.date] = None, local: bool = True
|
||||
) -> datetime.datetime:
|
||||
"""Calculates the solar noon (the time when the sun is at its highest
|
||||
point.)
|
||||
|
||||
:param date: The date for which to calculate the noon time.
|
||||
If no date is specified then the current date will be used.
|
||||
|
||||
:param local: True = Time to be returned in location's time zone;
|
||||
False = Time to be returned in UTC.
|
||||
If not specified then the time will be returned in local time
|
||||
|
||||
:returns: The date and time at which the solar noon occurs.
|
||||
"""
|
||||
|
||||
if local and self.timezone is None:
|
||||
raise ValueError("Local time requested but Location has no timezone set.")
|
||||
|
||||
if date is None:
|
||||
date = self.today(local)
|
||||
|
||||
observer = Observer(self.latitude, self.longitude)
|
||||
if local:
|
||||
return astral.sun.noon(observer, date, self.tzinfo)
|
||||
else:
|
||||
return astral.sun.noon(observer, date)
|
||||
|
||||
def sunset(
|
||||
self,
|
||||
date: Optional[datetime.date] = None,
|
||||
local: bool = True,
|
||||
observer_elevation: Elevation = 0.0,
|
||||
) -> datetime.datetime:
|
||||
"""Calculates sunset time (the time in the evening when the sun is a
|
||||
0.833 degrees below the horizon. This is to account for refraction.)
|
||||
|
||||
:param date: The date for which to calculate the sunset time.
|
||||
If no date is specified then the current date will be used.
|
||||
|
||||
:param local: True = Time to be returned in location's time zone;
|
||||
False = Time to be returned in UTC.
|
||||
If not specified then the time will be returned in local time
|
||||
|
||||
:param observer_elevation: Elevation of the observer in metres above
|
||||
the location.
|
||||
|
||||
:returns: The date and time at which sunset occurs.
|
||||
"""
|
||||
|
||||
if local and self.timezone is None:
|
||||
raise ValueError("Local time requested but Location has no timezone set.")
|
||||
|
||||
if date is None:
|
||||
date = self.today(local)
|
||||
|
||||
observer = Observer(self.latitude, self.longitude, observer_elevation)
|
||||
|
||||
if local:
|
||||
return astral.sun.sunset(observer, date, self.tzinfo)
|
||||
else:
|
||||
return astral.sun.sunset(observer, date)
|
||||
|
||||
def dusk(
|
||||
self,
|
||||
date: Optional[datetime.date] = None,
|
||||
local: bool = True,
|
||||
observer_elevation: Elevation = 0.0,
|
||||
) -> datetime.datetime:
|
||||
"""Calculates the dusk time (the time in the evening when the sun is a
|
||||
certain number of degrees below the horizon. By default this is 6
|
||||
degrees but can be changed by setting the
|
||||
:attr:`solar_depression` property.)
|
||||
|
||||
:param date: The date for which to calculate the dusk time.
|
||||
If no date is specified then the current date will be used.
|
||||
|
||||
:param local: True = Time to be returned in location's time zone;
|
||||
False = Time to be returned in UTC.
|
||||
If not specified then the time will be returned in local time
|
||||
|
||||
:param observer_elevation: Elevation of the observer in metres above
|
||||
the location.
|
||||
|
||||
:returns: The date and time at which dusk occurs.
|
||||
"""
|
||||
|
||||
if local and self.timezone is None:
|
||||
raise ValueError("Local time requested but Location has no timezone set.")
|
||||
|
||||
if date is None:
|
||||
date = self.today(local)
|
||||
|
||||
observer = Observer(self.latitude, self.longitude, observer_elevation)
|
||||
|
||||
if local:
|
||||
return astral.sun.dusk(observer, date, self.solar_depression, self.tzinfo)
|
||||
else:
|
||||
return astral.sun.dusk(observer, date, self.solar_depression)
|
||||
|
||||
def midnight(
|
||||
self, date: Optional[datetime.date] = None, local: bool = True
|
||||
) -> datetime.datetime:
|
||||
"""Calculates the solar midnight (the time when the sun is at its lowest
|
||||
point.)
|
||||
|
||||
:param date: The date for which to calculate the midnight time.
|
||||
If no date is specified then the current date will be used.
|
||||
|
||||
:param local: True = Time to be returned in location's time zone;
|
||||
False = Time to be returned in UTC.
|
||||
If not specified then the time will be returned in local time
|
||||
|
||||
:returns: The date and time at which the solar midnight occurs.
|
||||
"""
|
||||
|
||||
if local and self.timezone is None:
|
||||
raise ValueError("Local time requested but Location has no timezone set.")
|
||||
|
||||
if date is None:
|
||||
date = self.today(local)
|
||||
|
||||
observer = Observer(self.latitude, self.longitude)
|
||||
|
||||
if local:
|
||||
return astral.sun.midnight(observer, date, self.tzinfo)
|
||||
else:
|
||||
return astral.sun.midnight(observer, date)
|
||||
|
||||
def daylight(
|
||||
self,
|
||||
date: Optional[datetime.date] = None,
|
||||
local: bool = True,
|
||||
observer_elevation: Elevation = 0.0,
|
||||
) -> Tuple[datetime.datetime, datetime.datetime]:
|
||||
"""Calculates the daylight time (the time between sunrise and sunset)
|
||||
|
||||
:param date: The date for which to calculate daylight.
|
||||
If no date is specified then the current date will be used.
|
||||
|
||||
:param local: True = Time to be returned in location's time zone;
|
||||
False = Time to be returned in UTC.
|
||||
If not specified then the time will be returned in local time
|
||||
|
||||
:param observer_elevation: Elevation of the observer in metres above
|
||||
the location.
|
||||
|
||||
:returns: A tuple containing the start and end times
|
||||
"""
|
||||
|
||||
if local and self.timezone is None:
|
||||
raise ValueError("Local time requested but Location has no timezone set.")
|
||||
|
||||
if date is None:
|
||||
date = self.today(local)
|
||||
|
||||
observer = Observer(self.latitude, self.longitude, observer_elevation)
|
||||
|
||||
if local:
|
||||
return astral.sun.daylight(observer, date, self.tzinfo)
|
||||
else:
|
||||
return astral.sun.daylight(observer, date)
|
||||
|
||||
def night(
|
||||
self,
|
||||
date: Optional[datetime.date] = None,
|
||||
local: bool = True,
|
||||
observer_elevation: Elevation = 0.0,
|
||||
) -> Tuple[datetime.datetime, datetime.datetime]:
|
||||
"""Calculates the night time (the time between astronomical dusk and
|
||||
astronomical dawn of the next day)
|
||||
|
||||
:param date: The date for which to calculate the start of the night time.
|
||||
If no date is specified then the current date will be used.
|
||||
|
||||
:param local: True = Time to be returned in location's time zone;
|
||||
False = Time to be returned in UTC.
|
||||
If not specified then the time will be returned in local time
|
||||
|
||||
:param observer_elevation: Elevation of the observer in metres above
|
||||
the location.
|
||||
|
||||
:returns: A tuple containing the start and end times
|
||||
"""
|
||||
|
||||
if local and self.timezone is None:
|
||||
raise ValueError("Local time requested but Location has no timezone set.")
|
||||
|
||||
if date is None:
|
||||
date = self.today(local)
|
||||
|
||||
observer = Observer(self.latitude, self.longitude, observer_elevation)
|
||||
|
||||
if local:
|
||||
return astral.sun.night(observer, date, self.tzinfo)
|
||||
else:
|
||||
return astral.sun.night(observer, date)
|
||||
|
||||
def twilight(
|
||||
self,
|
||||
date: Optional[datetime.date] = None,
|
||||
direction: SunDirection = SunDirection.RISING,
|
||||
local: bool = True,
|
||||
observer_elevation: Elevation = 0.0,
|
||||
):
|
||||
"""Returns the start and end times of Twilight in the UTC timezone when
|
||||
the sun is traversing in the specified direction.
|
||||
|
||||
This method defines twilight as being between the time
|
||||
when the sun is at -6 degrees and sunrise/sunset.
|
||||
|
||||
:param direction: Determines whether the time is for the sun rising or setting.
|
||||
Use ``astral.SUN_RISING`` or ``astral.SunDirection.SETTING``.
|
||||
|
||||
:param date: The date for which to calculate the times.
|
||||
|
||||
:param local: True = Time to be returned in location's time zone;
|
||||
False = Time to be returned in UTC.
|
||||
If not specified then the time will be returned in local time
|
||||
|
||||
:param observer_elevation: Elevation of the observer in metres above
|
||||
the location.
|
||||
|
||||
:return: A tuple of the UTC date and time at which twilight starts and ends.
|
||||
"""
|
||||
|
||||
if local and self.timezone is None:
|
||||
raise ValueError("Local time requested but Location has no timezone set.")
|
||||
|
||||
if date is None:
|
||||
date = self.today(local)
|
||||
|
||||
observer = Observer(self.latitude, self.longitude, observer_elevation)
|
||||
|
||||
if local:
|
||||
return astral.sun.twilight(observer, date, direction, self.tzinfo)
|
||||
else:
|
||||
return astral.sun.twilight(observer, date, direction)
|
||||
|
||||
def moonrise(
|
||||
self,
|
||||
date: Optional[datetime.date] = None,
|
||||
local: bool = True,
|
||||
) -> Optional[datetime.datetime]:
|
||||
"""Calculates the time when the moon rises.
|
||||
|
||||
:param date: The date for which to calculate the moonrise time.
|
||||
If no date is specified then the current date will be used.
|
||||
|
||||
:param local: True = Time to be returned in location's time zone;
|
||||
False = Time to be returned in UTC.
|
||||
If not specified then the time will be returned in local time
|
||||
|
||||
:returns: The date and time at which moonrise occurs.
|
||||
"""
|
||||
|
||||
if local and self.timezone is None:
|
||||
raise ValueError("Local time requested but Location has no timezone set.")
|
||||
|
||||
if date is None:
|
||||
date = self.today(local)
|
||||
|
||||
observer = Observer(self.latitude, self.longitude, 0)
|
||||
|
||||
if local:
|
||||
return astral.moon.moonrise(observer, date, self.tzinfo)
|
||||
else:
|
||||
return astral.moon.moonrise(observer, date)
|
||||
|
||||
def moonset(
|
||||
self,
|
||||
date: Optional[datetime.date] = None,
|
||||
local: bool = True,
|
||||
) -> Optional[datetime.datetime]:
|
||||
"""Calculates the time when the moon sets.
|
||||
|
||||
:param date: The date for which to calculate the moonset time.
|
||||
If no date is specified then the current date will be used.
|
||||
|
||||
:param local: True = Time to be returned in location's time zone;
|
||||
False = Time to be returned in UTC.
|
||||
If not specified then the time will be returned in local time
|
||||
|
||||
:returns: The date and time at which moonset occurs.
|
||||
"""
|
||||
|
||||
if local and self.timezone is None:
|
||||
raise ValueError("Local time requested but Location has no timezone set.")
|
||||
|
||||
if date is None:
|
||||
date = self.today(local)
|
||||
|
||||
observer = Observer(self.latitude, self.longitude, 0)
|
||||
|
||||
if local:
|
||||
return astral.moon.moonset(observer, date, self.tzinfo)
|
||||
else:
|
||||
return astral.moon.moonset(observer, date)
|
||||
|
||||
def time_at_elevation(
|
||||
self,
|
||||
elevation: float,
|
||||
date: Optional[datetime.date] = None,
|
||||
direction: SunDirection = SunDirection.RISING,
|
||||
local: bool = True,
|
||||
) -> datetime.datetime:
|
||||
"""Calculate the time when the sun is at the specified elevation.
|
||||
|
||||
Note:
|
||||
This method uses positive elevations for those above the horizon.
|
||||
|
||||
Elevations greater than 90 degrees are converted to a setting sun
|
||||
i.e. an elevation of 110 will calculate a setting sun at 70 degrees.
|
||||
|
||||
:param elevation: Elevation in degrees above the horizon to calculate for.
|
||||
|
||||
:param date: The date for which to calculate the elevation time.
|
||||
If no date is specified then the current date will be used.
|
||||
|
||||
:param direction: Determines whether the time is for the sun rising or setting.
|
||||
Use ``SunDirection.RISING`` or ``SunDirection.SETTING``.
|
||||
Default is rising.
|
||||
|
||||
:param local: True = Time to be returned in location's time zone;
|
||||
False = Time to be returned in UTC.
|
||||
If not specified then the time will be returned in local time
|
||||
|
||||
:returns: The date and time at which dusk occurs.
|
||||
"""
|
||||
|
||||
if local and self.timezone is None:
|
||||
raise ValueError("Local time requested but Location has no timezone set.")
|
||||
|
||||
if date is None:
|
||||
date = self.today(local)
|
||||
|
||||
if elevation > 90.0:
|
||||
elevation = 180.0 - elevation
|
||||
direction = SunDirection.SETTING
|
||||
|
||||
observer = Observer(self.latitude, self.longitude, 0.0)
|
||||
|
||||
if local:
|
||||
return astral.sun.time_at_elevation(
|
||||
observer, elevation, date, direction, self.tzinfo
|
||||
)
|
||||
else:
|
||||
return astral.sun.time_at_elevation(observer, elevation, date, direction)
|
||||
|
||||
def rahukaalam(
|
||||
self,
|
||||
date: Optional[datetime.date] = None,
|
||||
local: bool = True,
|
||||
observer_elevation: Elevation = 0.0,
|
||||
) -> Tuple[datetime.datetime, datetime.datetime]:
|
||||
"""Calculates the period of rahukaalam.
|
||||
|
||||
:param date: The date for which to calculate the rahukaalam period.
|
||||
A value of ``None`` uses the current date.
|
||||
|
||||
:param local: True = Time to be returned in location's time zone;
|
||||
False = Time to be returned in UTC.
|
||||
|
||||
:param observer_elevation: Elevation of the observer in metres above
|
||||
the location.
|
||||
|
||||
:return: Tuple containing the start and end times for Rahukaalam.
|
||||
"""
|
||||
|
||||
if local and self.timezone is None:
|
||||
raise ValueError("Local time requested but Location has no timezone set.")
|
||||
|
||||
if date is None:
|
||||
date = self.today(local)
|
||||
|
||||
observer = Observer(self.latitude, self.longitude, observer_elevation)
|
||||
|
||||
if local:
|
||||
return astral.sun.rahukaalam(observer, date, tzinfo=self.tzinfo)
|
||||
else:
|
||||
return astral.sun.rahukaalam(observer, date)
|
||||
|
||||
def golden_hour(
|
||||
self,
|
||||
direction: SunDirection = SunDirection.RISING,
|
||||
date: Optional[datetime.date] = None,
|
||||
local: bool = True,
|
||||
observer_elevation: Elevation = 0.0,
|
||||
) -> Tuple[datetime.datetime, datetime.datetime]:
|
||||
"""Returns the start and end times of the Golden Hour when the sun is traversing
|
||||
in the specified direction.
|
||||
|
||||
This method uses the definition from PhotoPills i.e. the
|
||||
golden hour is when the sun is between 4 degrees below the horizon
|
||||
and 6 degrees above.
|
||||
|
||||
:param direction: Determines whether the time is for the sun rising or setting.
|
||||
Use ``SunDirection.RISING`` or ``SunDirection.SETTING``.
|
||||
Default is rising.
|
||||
|
||||
:param date: The date for which to calculate the times.
|
||||
|
||||
:param local: True = Times to be returned in location's time zone;
|
||||
False = Times to be returned in UTC.
|
||||
If not specified then the time will be returned in local time
|
||||
|
||||
:param observer_elevation: Elevation of the observer in metres above
|
||||
the location.
|
||||
|
||||
:return: A tuple of the date and time at which the Golden Hour starts and ends.
|
||||
"""
|
||||
|
||||
if local and self.timezone is None:
|
||||
raise ValueError("Local time requested but Location has no timezone set.")
|
||||
|
||||
if date is None:
|
||||
date = self.today(local)
|
||||
|
||||
observer = Observer(self.latitude, self.longitude, observer_elevation)
|
||||
|
||||
if local:
|
||||
return astral.sun.golden_hour(observer, date, direction, self.tzinfo)
|
||||
else:
|
||||
return astral.sun.golden_hour(observer, date, direction)
|
||||
|
||||
def blue_hour(
|
||||
self,
|
||||
direction: SunDirection = SunDirection.RISING,
|
||||
date: Optional[datetime.date] = None,
|
||||
local: bool = True,
|
||||
observer_elevation: Elevation = 0.0,
|
||||
) -> Tuple[datetime.datetime, datetime.datetime]:
|
||||
"""Returns the start and end times of the Blue Hour when the sun is traversing
|
||||
in the specified direction.
|
||||
|
||||
This method uses the definition from PhotoPills i.e. the
|
||||
blue hour is when the sun is between 6 and 4 degrees below the horizon.
|
||||
|
||||
:param direction: Determines whether the time is for the sun rising or setting.
|
||||
Use ``SunDirection.RISING`` or ``SunDirection.SETTING``.
|
||||
Default is rising.
|
||||
|
||||
:param date: The date for which to calculate the times.
|
||||
If no date is specified then the current date will be used.
|
||||
|
||||
:param local: True = Times to be returned in location's time zone;
|
||||
False = Times to be returned in UTC.
|
||||
If not specified then the time will be returned in local time
|
||||
|
||||
:param observer_elevation: Elevation of the observer in metres above
|
||||
the location.
|
||||
|
||||
:return: A tuple of the date and time at which the Blue Hour starts and ends.
|
||||
"""
|
||||
|
||||
if local and self.timezone is None:
|
||||
raise ValueError("Local time requested but Location has no timezone set.")
|
||||
|
||||
if date is None:
|
||||
date = self.today(local)
|
||||
|
||||
observer = Observer(self.latitude, self.longitude, observer_elevation)
|
||||
|
||||
if local:
|
||||
return astral.sun.blue_hour(observer, date, direction, self.tzinfo)
|
||||
else:
|
||||
return astral.sun.blue_hour(observer, date, direction)
|
||||
|
||||
def solar_azimuth(
|
||||
self,
|
||||
dateandtime: Optional[datetime.datetime] = None,
|
||||
observer_elevation: Elevation = 0.0,
|
||||
) -> float:
|
||||
"""Calculates the solar azimuth angle for a specific date/time.
|
||||
|
||||
:param dateandtime: The date and time for which to calculate the angle.
|
||||
:returns: The azimuth angle in degrees clockwise from North.
|
||||
"""
|
||||
|
||||
if dateandtime is None:
|
||||
dateandtime = astral.sun.now(self.tzinfo)
|
||||
elif not dateandtime.tzinfo:
|
||||
dateandtime = dateandtime.replace(tzinfo=self.tzinfo)
|
||||
|
||||
observer = Observer(self.latitude, self.longitude, observer_elevation)
|
||||
|
||||
dateandtime = dateandtime.astimezone(datetime.timezone.utc) # type: ignore
|
||||
return astral.sun.azimuth(observer, dateandtime)
|
||||
|
||||
def solar_elevation(
|
||||
self,
|
||||
dateandtime: Optional[datetime.datetime] = None,
|
||||
observer_elevation: Elevation = 0.0,
|
||||
) -> float:
|
||||
"""Calculates the solar elevation angle for a specific time.
|
||||
|
||||
:param dateandtime: The date and time for which to calculate the angle.
|
||||
|
||||
:returns: The elevation angle in degrees above the horizon.
|
||||
"""
|
||||
|
||||
if dateandtime is None:
|
||||
dateandtime = astral.sun.now(self.tzinfo)
|
||||
elif not dateandtime.tzinfo:
|
||||
dateandtime = dateandtime.replace(tzinfo=self.tzinfo)
|
||||
|
||||
observer = Observer(self.latitude, self.longitude, observer_elevation)
|
||||
|
||||
dateandtime = dateandtime.astimezone(datetime.timezone.utc) # type: ignore
|
||||
return astral.sun.elevation(observer, dateandtime)
|
||||
|
||||
def solar_zenith(
|
||||
self,
|
||||
dateandtime: Optional[datetime.datetime] = None,
|
||||
observer_elevation: Elevation = 0.0,
|
||||
) -> float:
|
||||
"""Calculates the solar zenith angle for a specific time.
|
||||
|
||||
:param dateandtime: The date and time for which to calculate the angle.
|
||||
:returns: The zenith angle in degrees from vertical.
|
||||
"""
|
||||
|
||||
return 90.0 - self.solar_elevation(dateandtime, observer_elevation)
|
||||
|
||||
def moon_phase(self, date: Optional[datetime.date] = None, local: bool = True):
|
||||
"""Calculates the moon phase for a specific date.
|
||||
|
||||
:param date: The date to calculate the phase for. If ommitted the
|
||||
current date is used.
|
||||
|
||||
:returns:
|
||||
A number designating the phase
|
||||
|
||||
============ ==============
|
||||
0 .. 6.99 New moon
|
||||
7 .. 13.99 First quarter
|
||||
14 .. 20.99 Full moon
|
||||
21 .. 27.99 Last quarter
|
||||
============ ==============
|
||||
"""
|
||||
|
||||
if date is None:
|
||||
date = self.today(local)
|
||||
|
||||
return astral.moon.phase(date)
|
||||
@ -0,0 +1,601 @@
|
||||
"""Moon phase, rise and set times
|
||||
|
||||
Right ascension, declination and distance of moon calcaulation
|
||||
from
|
||||
|
||||
LOW-PRECISION FORMULAE FOR PLANETARY POSITIONS
|
||||
http://articles.adsabs.harvard.edu/pdf/1979ApJS...41..391V
|
||||
"""
|
||||
|
||||
import datetime
|
||||
from dataclasses import dataclass, field, replace
|
||||
from math import asin, atan2, cos, degrees, fabs, pi, radians, sin, sqrt
|
||||
from typing import Callable, List, Optional, Union
|
||||
|
||||
try:
|
||||
import zoneinfo
|
||||
except ImportError:
|
||||
from backports import zoneinfo
|
||||
|
||||
from astral import AstralBodyPosition, Observer, now, today
|
||||
from astral.julian import julianday, julianday_2000
|
||||
from astral.sidereal import lmst
|
||||
from astral.table4 import Table4Row, table4_u, table4_v, table4_w
|
||||
|
||||
__all__ = ["moonrise", "moonset", "phase"]
|
||||
|
||||
# Using 1896 arc seconds as moon's apparent diameter
|
||||
MOON_APPARENT_RADIUS = 1896.0 / (60.0 * 60.0)
|
||||
|
||||
Degrees = float
|
||||
Radians = float
|
||||
Revolutions = float
|
||||
ArgumentFunc = Optional[Callable[[float], float]]
|
||||
|
||||
|
||||
@dataclass
|
||||
class NoTransit:
|
||||
parallax: float = field(default_factory=float)
|
||||
|
||||
|
||||
@dataclass
|
||||
class TransitEvent:
|
||||
event: str
|
||||
when: datetime.time = field(default_factory=datetime.time)
|
||||
azimuth: float = field(default_factory=float)
|
||||
distance: float = field(default_factory=float)
|
||||
|
||||
|
||||
def interpolate(f0: float, f1: float, f2: float, p: float) -> float:
|
||||
"""3-point interpolation"""
|
||||
a = f1 - f0
|
||||
b = f2 - f1 - a
|
||||
f = f0 + p * (2 * a + b * (2 * p - 1))
|
||||
return f
|
||||
|
||||
|
||||
def sgn(value1: Union[float, datetime.timedelta]) -> int:
|
||||
"""Test whether value1 and value2 have the same sign"""
|
||||
if isinstance(value1, datetime.timedelta):
|
||||
value1 = value1.total_seconds()
|
||||
|
||||
if value1 < 0:
|
||||
return -1
|
||||
elif value1 > 0:
|
||||
return 1
|
||||
else:
|
||||
return 0
|
||||
|
||||
|
||||
def moon_mean_longitude(jd2000: float) -> Revolutions:
|
||||
_mean_longitude = 0.606434 + 0.03660110129 * jd2000
|
||||
_mean_longitude = _mean_longitude - int(_mean_longitude)
|
||||
return _mean_longitude
|
||||
|
||||
|
||||
def moon_mean_anomoly(jd2000: float) -> Revolutions:
|
||||
_mean_anomoly = 0.374897 + 0.03629164709 * jd2000
|
||||
_mean_anomoly = _mean_anomoly - int(_mean_anomoly)
|
||||
return _mean_anomoly
|
||||
|
||||
|
||||
def moon_argument_of_latitude(jd2000: float) -> Revolutions:
|
||||
_argument_of_latitude = 0.259091 + 0.03674819520 * jd2000
|
||||
_argument_of_latitude = _argument_of_latitude - int(_argument_of_latitude)
|
||||
return _argument_of_latitude
|
||||
|
||||
|
||||
def moon_mean_elongation_from_sun(jd2000: float) -> Revolutions:
|
||||
_mean_elongation_from_sun = 0.827362 + 0.03386319198 * jd2000
|
||||
_mean_elongation_from_sun = _mean_elongation_from_sun - int(
|
||||
_mean_elongation_from_sun
|
||||
)
|
||||
return _mean_elongation_from_sun
|
||||
|
||||
|
||||
def longitude_lunar_ascending_node(jd2000: float) -> Revolutions:
|
||||
_longitude_lunar_ascending_node = moon_mean_longitude(
|
||||
jd2000
|
||||
) - moon_argument_of_latitude(jd2000)
|
||||
return _longitude_lunar_ascending_node
|
||||
|
||||
|
||||
def sun_mean_longitude(jd2000: float) -> Revolutions:
|
||||
_sun_mean_longitude = 0.779072 + 0.00273790931 * jd2000
|
||||
_sun_mean_longitude = _sun_mean_longitude - int(_sun_mean_longitude)
|
||||
return _sun_mean_longitude
|
||||
|
||||
|
||||
def sun_mean_anomoly(jd2000: float) -> Revolutions:
|
||||
_sun_mean_anomoly = 0.993126 + 0.00273777850 * jd2000
|
||||
_sun_mean_anomoly = _sun_mean_anomoly - int(_sun_mean_anomoly)
|
||||
return _sun_mean_anomoly
|
||||
|
||||
|
||||
def venus_mean_longitude(jd2000: float) -> Revolutions:
|
||||
_venus_mean_longitude = 0.505498 + 0.00445046867 * jd2000
|
||||
_venus_mean_longitude = _venus_mean_longitude - int(_venus_mean_longitude)
|
||||
return _venus_mean_longitude
|
||||
|
||||
|
||||
def moon_position(jd2000: float) -> AstralBodyPosition:
|
||||
"""Calculate right ascension, declination and geocentric distance for the moon"""
|
||||
|
||||
argument_values: List[Union[float, None]] = [
|
||||
moon_mean_longitude(jd2000), # 1 = Lm
|
||||
moon_mean_anomoly(jd2000), # 2 = Gm
|
||||
moon_argument_of_latitude(jd2000), # 3 = Fm
|
||||
moon_mean_elongation_from_sun(jd2000), # 4 = D
|
||||
longitude_lunar_ascending_node(jd2000), # 5 = Om
|
||||
None, # 6
|
||||
sun_mean_longitude(jd2000), # 7 = Ls
|
||||
sun_mean_anomoly(jd2000), # 8 = Gs
|
||||
None, # 9
|
||||
None, # 10
|
||||
None, # 11
|
||||
venus_mean_longitude(jd2000), # 12 = L2
|
||||
]
|
||||
|
||||
T = jd2000 / 36525 + 1
|
||||
|
||||
def _calc_value(table: List[Table4Row]) -> float:
|
||||
result = 0.0
|
||||
for row in table:
|
||||
revolutions: float = 0.0
|
||||
for arg_number, multiplier in row.argument_multiplers.items():
|
||||
if multiplier != 0:
|
||||
arg_value = argument_values[arg_number - 1]
|
||||
if arg_value:
|
||||
value = arg_value * multiplier
|
||||
revolutions += value
|
||||
else:
|
||||
raise ValueError
|
||||
|
||||
t_multipler = T if row.t else 1
|
||||
result += row.coefficient * t_multipler * row.sincos(revolutions * 2 * pi)
|
||||
|
||||
return result
|
||||
|
||||
v = _calc_value(table4_v)
|
||||
u = _calc_value(table4_u)
|
||||
w = _calc_value(table4_w)
|
||||
|
||||
s = w / sqrt(u - v * v)
|
||||
right_ascension = asin(s) + (argument_values[0] or 0) * 2 * pi # In radians
|
||||
|
||||
s = v / sqrt(u)
|
||||
declination = asin(s) # In radians
|
||||
|
||||
distance = 60.40974 * sqrt(u) # In Earth radii (≈6378km)
|
||||
|
||||
return AstralBodyPosition(right_ascension, declination, distance)
|
||||
|
||||
|
||||
def moon_transit_event(
|
||||
hour: float,
|
||||
lmst: Degrees,
|
||||
latitude: Degrees,
|
||||
distance: float,
|
||||
window: List[AstralBodyPosition],
|
||||
) -> Union[TransitEvent, NoTransit]:
|
||||
"""Check if the moon transits the horizon within the window.
|
||||
|
||||
Args:
|
||||
hour: Hour of the day
|
||||
lmst: Local mean sidereal time in degrees
|
||||
latitude: Observer latitude
|
||||
distance: Distance to the moon
|
||||
window: Sliding window of moon positions that covers a part of the day
|
||||
"""
|
||||
mst = radians(lmst)
|
||||
hour_angle = [0.0, 0.0, 0.0]
|
||||
|
||||
k1 = radians(15 * 1.0027379097096138907193594760917)
|
||||
|
||||
if window[2].right_ascension < window[0].right_ascension:
|
||||
window[2].right_ascension = window[2].right_ascension + 2 * pi
|
||||
|
||||
hour_angle[0] = mst - window[0].right_ascension + (hour * k1)
|
||||
hour_angle[2] = mst - window[2].right_ascension + (hour * k1) + k1
|
||||
hour_angle[1] = (hour_angle[2] + hour_angle[0]) / 2
|
||||
|
||||
window[1].declination = (window[2].declination + window[0].declination) / 2
|
||||
|
||||
sl = sin(radians(latitude))
|
||||
cl = cos(radians(latitude))
|
||||
|
||||
# moon apparent radius + parallax correction
|
||||
z = cos(radians(90 + MOON_APPARENT_RADIUS - (41.685 / distance)))
|
||||
|
||||
if hour == 0:
|
||||
window[0].distance = (
|
||||
sl * sin(window[0].declination)
|
||||
+ cl * cos(window[0].declination) * cos(hour_angle[0])
|
||||
- z
|
||||
)
|
||||
|
||||
window[2].distance = (
|
||||
sl * sin(window[2].declination)
|
||||
+ cl * cos(window[2].declination) * cos(hour_angle[2])
|
||||
- z
|
||||
)
|
||||
|
||||
if sgn(window[0].distance) == sgn(window[2].distance):
|
||||
return NoTransit(window[2].distance)
|
||||
|
||||
window[1].distance = (
|
||||
sl * sin(window[1].declination)
|
||||
+ cl * cos(window[1].declination) * cos(hour_angle[1])
|
||||
- z
|
||||
)
|
||||
|
||||
a = 2 * window[2].distance - 4 * window[1].distance + 2 * window[0].distance
|
||||
b = 4 * window[1].distance - 3 * window[0].distance - window[2].distance
|
||||
discriminant = b * b - 4 * a * window[0].distance
|
||||
|
||||
if discriminant < 0:
|
||||
return NoTransit(window[2].distance)
|
||||
|
||||
discriminant = sqrt(discriminant)
|
||||
e = (-b + discriminant) / (2 * a)
|
||||
if e > 1 or e < 0:
|
||||
e = (-b - discriminant) / (2 * a)
|
||||
|
||||
time = hour + e + 1 / 120
|
||||
|
||||
h = int(time)
|
||||
m = int((time - h) * 60)
|
||||
|
||||
sd = sin(window[1].declination)
|
||||
cd = cos(window[1].declination)
|
||||
|
||||
hour_angle_crossing = hour_angle[0] + e * (hour_angle[2] - hour_angle[0])
|
||||
sh = sin(hour_angle_crossing)
|
||||
ch = cos(hour_angle_crossing)
|
||||
|
||||
x = cl * sd - sl * cd * ch
|
||||
y = -cd * sh
|
||||
|
||||
az = degrees(atan2(y, x))
|
||||
if az < 0:
|
||||
az += 360
|
||||
if az > 360:
|
||||
az -= 360
|
||||
|
||||
event_time = datetime.time(h, m, 0)
|
||||
if window[0].distance < 0 and window[2].distance > 0:
|
||||
return TransitEvent("rise", event_time, az, window[2].distance)
|
||||
|
||||
if window[0].distance > 0 and window[2].distance < 0:
|
||||
return TransitEvent("set", event_time, az, window[2].distance)
|
||||
|
||||
return NoTransit(window[2].distance)
|
||||
|
||||
|
||||
def riseset(
|
||||
on: datetime.date,
|
||||
observer: Observer,
|
||||
):
|
||||
"""Calculate rise and set times"""
|
||||
jd2000 = julianday_2000(on)
|
||||
t0 = lmst(
|
||||
on,
|
||||
observer.longitude,
|
||||
)
|
||||
|
||||
m: List[AstralBodyPosition] = []
|
||||
for interval in range(3):
|
||||
pos = moon_position(jd2000 + (interval * 0.5))
|
||||
m.append(pos)
|
||||
|
||||
for interval in range(1, 3):
|
||||
if m[interval].right_ascension <= m[interval - 1].right_ascension:
|
||||
m[interval].right_ascension = m[interval].right_ascension + 2 * pi
|
||||
|
||||
moon_position_window: List[AstralBodyPosition] = [
|
||||
replace(m[0]), # copy m[0]
|
||||
AstralBodyPosition(),
|
||||
AstralBodyPosition(),
|
||||
]
|
||||
|
||||
rise_time = None
|
||||
set_time = None
|
||||
|
||||
# events = []
|
||||
for hour in range(24):
|
||||
ph = (hour + 1) / 24
|
||||
moon_position_window[2].right_ascension = interpolate(
|
||||
m[0].right_ascension,
|
||||
m[1].right_ascension,
|
||||
m[2].right_ascension,
|
||||
ph,
|
||||
)
|
||||
moon_position_window[2].declination = interpolate(
|
||||
m[0].declination,
|
||||
m[1].declination,
|
||||
m[2].declination,
|
||||
ph,
|
||||
)
|
||||
|
||||
transit_info = moon_transit_event(
|
||||
hour, t0, observer.latitude, m[1].distance, moon_position_window
|
||||
)
|
||||
if isinstance(transit_info, NoTransit):
|
||||
moon_position_window[2].distance = transit_info.parallax
|
||||
else:
|
||||
query_time = datetime.datetime(
|
||||
on.year, on.month, on.day, hour, 0, 0, tzinfo=datetime.timezone.utc
|
||||
)
|
||||
|
||||
if transit_info.event == "rise":
|
||||
event_time = transit_info.when
|
||||
event = datetime.datetime(
|
||||
on.year,
|
||||
on.month,
|
||||
on.day,
|
||||
event_time.hour,
|
||||
event_time.minute,
|
||||
0,
|
||||
tzinfo=datetime.timezone.utc,
|
||||
)
|
||||
if rise_time is None:
|
||||
rise_time = event
|
||||
else:
|
||||
rq_diff = (rise_time - query_time).total_seconds()
|
||||
eq_diff = (event - query_time).total_seconds()
|
||||
if set_time is not None:
|
||||
sq_diff = (set_time - query_time).total_seconds()
|
||||
else:
|
||||
sq_diff = 0
|
||||
|
||||
update_rise_time = sgn(rq_diff) == sgn(eq_diff) and fabs(
|
||||
rq_diff
|
||||
) > fabs(eq_diff)
|
||||
update_rise_time |= sgn(rq_diff) != sgn(eq_diff) and (
|
||||
set_time is not None and sgn(rq_diff) == sgn(sq_diff)
|
||||
)
|
||||
|
||||
if update_rise_time:
|
||||
rise_time = event
|
||||
elif transit_info.event == "set":
|
||||
event_time = transit_info.when
|
||||
event = datetime.datetime(
|
||||
on.year,
|
||||
on.month,
|
||||
on.day,
|
||||
event_time.hour,
|
||||
event_time.minute,
|
||||
0,
|
||||
tzinfo=datetime.timezone.utc,
|
||||
)
|
||||
if set_time is None:
|
||||
set_time = event
|
||||
else:
|
||||
sq_diff = (set_time - query_time).total_seconds()
|
||||
eq_diff = (event - query_time).total_seconds()
|
||||
if rise_time is not None:
|
||||
rq_diff = (rise_time - query_time).total_seconds()
|
||||
else:
|
||||
rq_diff = 0
|
||||
|
||||
update_set_time = sgn(sq_diff) == sgn(eq_diff) and fabs(
|
||||
sq_diff
|
||||
) > fabs(eq_diff)
|
||||
update_set_time |= sgn(sq_diff) != sgn(eq_diff) and (
|
||||
rise_time is not None and sgn(rq_diff) == sgn(sq_diff)
|
||||
)
|
||||
|
||||
if update_set_time:
|
||||
set_time = event
|
||||
|
||||
moon_position_window[0].right_ascension = moon_position_window[
|
||||
2
|
||||
].right_ascension
|
||||
moon_position_window[0].declination = moon_position_window[2].declination
|
||||
moon_position_window[0].distance = moon_position_window[2].distance
|
||||
|
||||
return rise_time, set_time
|
||||
|
||||
|
||||
def moonrise(
|
||||
observer: Observer,
|
||||
date: Optional[datetime.date] = None,
|
||||
tzinfo: Union[str, datetime.tzinfo] = datetime.timezone.utc,
|
||||
) -> Optional[datetime.datetime]:
|
||||
"""Calculate the moon rise time
|
||||
|
||||
Args:
|
||||
observer: Observer to calculate moonrise for
|
||||
date: Date to calculate for. Default is today's date in the
|
||||
timezone `tzinfo`.
|
||||
tzinfo: Timezone to return times in. Default is UTC.
|
||||
|
||||
Returns:
|
||||
Date and time at which moonrise occurs.
|
||||
"""
|
||||
if isinstance(tzinfo, str):
|
||||
tzinfo = zoneinfo.ZoneInfo(tzinfo) # type: ignore
|
||||
|
||||
if date is None:
|
||||
date = today(tzinfo) # type: ignore
|
||||
elif isinstance(date, datetime.datetime):
|
||||
date = date.date()
|
||||
|
||||
info = riseset(date, observer)
|
||||
if info[0]:
|
||||
rise = info[0].astimezone(tzinfo) # type: ignore
|
||||
rd = rise.date()
|
||||
if rd != date:
|
||||
if rd > date:
|
||||
delta = datetime.timedelta(days=-1)
|
||||
else:
|
||||
delta = datetime.timedelta(days=1)
|
||||
new_date = date + delta
|
||||
info = riseset(new_date, observer)
|
||||
if info[0]:
|
||||
rise = info[0].astimezone(tzinfo) # type: ignore
|
||||
rd = rise.date()
|
||||
if rd != date:
|
||||
rise = None
|
||||
return rise
|
||||
else:
|
||||
raise ValueError("Moon never rises on this date, at this location")
|
||||
|
||||
|
||||
def moonset(
|
||||
observer: Observer,
|
||||
date: Optional[datetime.date] = None,
|
||||
tzinfo: Union[str, datetime.tzinfo] = datetime.timezone.utc,
|
||||
) -> Optional[datetime.datetime]:
|
||||
"""Calculate the moon set time
|
||||
|
||||
Args:
|
||||
observer: Observer to calculate moonset for
|
||||
date: Date to calculate for. Default is today's date in the
|
||||
timezone `tzinfo`.
|
||||
tzinfo: Timezone to return times in. Default is UTC.
|
||||
|
||||
Returns:
|
||||
Date and time at which moonset occurs.
|
||||
"""
|
||||
if isinstance(tzinfo, str):
|
||||
tzinfo = zoneinfo.ZoneInfo(tzinfo) # type: ignore
|
||||
|
||||
if date is None:
|
||||
date = today(tzinfo) # type: ignore
|
||||
elif isinstance(date, datetime.datetime):
|
||||
date = date.date()
|
||||
|
||||
info = riseset(date, observer)
|
||||
if info[1]:
|
||||
set = info[1].astimezone(tzinfo) # type: ignore
|
||||
sd = set.date()
|
||||
if sd != date:
|
||||
if sd > date:
|
||||
delta = datetime.timedelta(days=-1)
|
||||
else:
|
||||
delta = datetime.timedelta(days=1)
|
||||
new_date = date + delta
|
||||
info = riseset(new_date, observer)
|
||||
if info[1]:
|
||||
set = info[1].astimezone(tzinfo) # type: ignore
|
||||
sd = set.date()
|
||||
if sd != date:
|
||||
set = None
|
||||
return set
|
||||
else:
|
||||
raise ValueError("Moon never sets on this date, at this location")
|
||||
|
||||
|
||||
def azimuth(
|
||||
observer: Observer,
|
||||
at: Optional[datetime.datetime] = None,
|
||||
) -> Degrees:
|
||||
if at is None:
|
||||
at = now()
|
||||
|
||||
jd2000 = julianday_2000(at)
|
||||
position = moon_position(jd2000)
|
||||
lst0: Radians = radians(lmst(at, observer.longitude))
|
||||
hourangle: Radians = lst0 - position.right_ascension
|
||||
|
||||
sh = sin(hourangle)
|
||||
ch = cos(hourangle)
|
||||
sd = sin(position.declination)
|
||||
cd = cos(position.declination)
|
||||
sl = sin(radians(observer.latitude))
|
||||
cl = cos(radians(observer.latitude))
|
||||
|
||||
x = -ch * cd * sl + sd * cl
|
||||
y = -sh * cd
|
||||
azimuth = degrees(atan2(y, x)) % 360
|
||||
return azimuth
|
||||
|
||||
|
||||
def elevation(
|
||||
observer: Observer,
|
||||
at: Optional[datetime.datetime] = None,
|
||||
):
|
||||
if at is None:
|
||||
at = now()
|
||||
|
||||
jd2000 = julianday_2000(at)
|
||||
position = moon_position(jd2000)
|
||||
lst0: Radians = radians(lmst(at, observer.longitude))
|
||||
hourangle: Radians = lst0 - position.right_ascension
|
||||
|
||||
sh = sin(hourangle)
|
||||
ch = cos(hourangle)
|
||||
sd = sin(position.declination)
|
||||
cd = cos(position.declination)
|
||||
sl = sin(radians(observer.latitude))
|
||||
cl = cos(radians(observer.latitude))
|
||||
|
||||
x = -ch * cd * sl + sd * cl
|
||||
y = -sh * cd
|
||||
|
||||
z = ch * cd * cl + sd * sl
|
||||
r = sqrt(x * x + y * y)
|
||||
elevation = degrees(atan2(z, r))
|
||||
|
||||
return elevation
|
||||
|
||||
|
||||
def zenith(
|
||||
observer: Observer,
|
||||
at: Optional[datetime.datetime] = None,
|
||||
):
|
||||
return 90 - elevation(observer, at)
|
||||
|
||||
|
||||
def _phase_asfloat(date: datetime.date) -> float:
|
||||
jd = julianday(date)
|
||||
dt = pow((jd - 2382148), 2) / (41048480 * 86400)
|
||||
t = (jd + dt - 2451545.0) / 36525
|
||||
t2 = pow(t, 2)
|
||||
t3 = pow(t, 3)
|
||||
|
||||
d = 297.85 + (445267.1115 * t) - (0.0016300 * t2) + (t3 / 545868)
|
||||
d = radians(d % 360.0)
|
||||
|
||||
m = 357.53 + (35999.0503 * t)
|
||||
m = radians(m % 360.0)
|
||||
|
||||
m1 = 134.96 + (477198.8676 * t) + (0.0089970 * t2) + (t3 / 69699)
|
||||
m1 = radians(m1 % 360.0)
|
||||
|
||||
elong = degrees(d) + 6.29 * sin(m1)
|
||||
elong -= 2.10 * sin(m)
|
||||
elong += 1.27 * sin(2 * d - m1)
|
||||
elong += 0.66 * sin(2 * d)
|
||||
elong = elong % 360.0
|
||||
elong = int(elong)
|
||||
moon = ((elong + 6.43) / 360) * 28
|
||||
return moon
|
||||
|
||||
|
||||
def phase(date: Optional[datetime.date] = None) -> float:
|
||||
"""Calculates the phase of the moon on the specified date.
|
||||
|
||||
Args:
|
||||
date: The date to calculate the phase for. Dates are always in the UTC timezone.
|
||||
If not specified then today's date is used.
|
||||
|
||||
Returns:
|
||||
A number designating the phase.
|
||||
|
||||
============ ==============
|
||||
0 .. 6.99 New moon
|
||||
7 .. 13.99 First quarter
|
||||
14 .. 20.99 Full moon
|
||||
21 .. 27.99 Last quarter
|
||||
============ ==============
|
||||
"""
|
||||
|
||||
if date is None:
|
||||
date = today()
|
||||
|
||||
moon = _phase_asfloat(date)
|
||||
if moon >= 28.0:
|
||||
moon -= 28.0
|
||||
return moon
|
||||
@ -0,0 +1,35 @@
|
||||
import datetime
|
||||
from typing import Union
|
||||
|
||||
from astral.julian import julianday_2000
|
||||
|
||||
Degrees = float
|
||||
|
||||
|
||||
def gmst(at: Union[datetime.datetime, datetime.date]) -> Degrees:
|
||||
"""Calculate Greenwich Mean Sidereal Time in degrees"""
|
||||
jd2000 = julianday_2000(at)
|
||||
|
||||
t0 = jd2000 / 36525
|
||||
value = (
|
||||
280.46061837
|
||||
+ 360.98564736629 * jd2000
|
||||
+ 0.000387933 * pow(t0, 2)
|
||||
+ pow(t0, 3) / 38710000
|
||||
)
|
||||
return value % 360
|
||||
|
||||
|
||||
def lmst(
|
||||
at: Union[datetime.datetime, datetime.date],
|
||||
longitude: Degrees,
|
||||
) -> Degrees:
|
||||
"""Local Mean Sidereal Time for longitude in degrees
|
||||
|
||||
Args:
|
||||
jd2000: Julian day
|
||||
longitude: Longitude in degrees
|
||||
"""
|
||||
mst = gmst(at)
|
||||
mst += longitude
|
||||
return mst
|
||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,216 @@
|
||||
from math import cos, sin
|
||||
from typing import Callable, Dict, List, NamedTuple
|
||||
|
||||
|
||||
class Table4Row(NamedTuple):
|
||||
coefficient: float
|
||||
t: bool
|
||||
sincos: Callable[[float], float]
|
||||
argument_multiplers: Dict[int, int]
|
||||
|
||||
|
||||
Gm = 2 # Moon mean anomoly
|
||||
Fm = 3 # Moon argument of latitude
|
||||
D = 4 # Moon mean elongation from sun
|
||||
Om = 5 # Longitude of the lunar ascending node
|
||||
Ls = 7 # Sun mean longitude
|
||||
Gs = 8 # Sun mean anomoly
|
||||
L2 = 12 # Venus mean longitude
|
||||
|
||||
table4_v: List[Table4Row] = [
|
||||
Table4Row(0.39558, False, sin, {Gm: 0, Fm: 1, D: 0, Om: 1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.08200, False, sin, {Gm: 0, Fm: 1, D: 0, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.03257, False, sin, {Gm: 1, Fm: -1, D: 0, Om: -1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.01092, False, sin, {Gm: 1, Fm: 1, D: 0, Om: 1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00666, False, sin, {Gm: 1, Fm: -1, D: 0, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00644, False, sin, {Gm: 1, Fm: 1, D: -2, Om: 1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00331, False, sin, {Gm: 0, Fm: 1, D: -2, Om: 1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00304, False, sin, {Gm: 0, Fm: 1, D: -2, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(
|
||||
-0.00240, False, sin, {Gm: 1, Fm: -1, D: -2, Om: -1, Ls: 0, Gs: 0, L2: 0}
|
||||
),
|
||||
Table4Row(0.00226, False, sin, {Gm: 1, Fm: 1, D: 0, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00108, False, sin, {Gm: 1, Fm: 1, D: -2, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00079, False, sin, {Gm: 0, Fm: 1, D: 0, Om: -1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00078, False, sin, {Gm: 0, Fm: 1, D: 2, Om: 1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00066, False, sin, {Gm: 0, Fm: 1, D: 0, Om: 1, Ls: 0, Gs: -1, L2: 0}),
|
||||
Table4Row(-0.00062, False, sin, {Gm: 0, Fm: 1, D: 0, Om: 1, Ls: 0, Gs: 1, L2: 0}),
|
||||
Table4Row(-0.00050, False, sin, {Gm: 1, Fm: -1, D: -2, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00045, False, sin, {Gm: 2, Fm: 1, D: 0, Om: 1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00031, False, sin, {Gm: 2, Fm: 1, D: -2, Om: 1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00027, False, sin, {Gm: 1, Fm: 1, D: -2, Om: 1, Ls: 0, Gs: 1, L2: 0}),
|
||||
Table4Row(-0.00024, False, sin, {Gm: 0, Fm: 1, D: -2, Om: 1, Ls: 0, Gs: 1, L2: 0}),
|
||||
Table4Row(-0.00021, True, sin, {Gm: 0, Fm: 1, D: 0, Om: 1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00018, False, sin, {Gm: 0, Fm: 1, D: -1, Om: 1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00016, False, sin, {Gm: 0, Fm: 1, D: 2, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00016, False, sin, {Gm: 1, Fm: -1, D: 0, Om: -1, Ls: 0, Gs: -1, L2: 0}),
|
||||
Table4Row(-0.00016, False, sin, {Gm: 2, Fm: -1, D: 0, Om: -1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00015, False, sin, {Gm: 0, Fm: 1, D: -2, Om: 0, Ls: 0, Gs: 1, L2: 0}),
|
||||
Table4Row(
|
||||
-0.00012, False, sin, {Gm: 1, Fm: -1, D: -2, Om: -1, Ls: 0, Gs: 1, L2: 0}
|
||||
),
|
||||
Table4Row(-0.00011, False, sin, {Gm: 1, Fm: -1, D: 0, Om: -1, Ls: 0, Gs: 1, L2: 0}),
|
||||
Table4Row(0.00009, False, sin, {Gm: 1, Fm: 1, D: 0, Om: 1, Ls: 0, Gs: -1, L2: 0}),
|
||||
Table4Row(0.00009, False, sin, {Gm: 2, Fm: 1, D: 0, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00008, False, sin, {Gm: 2, Fm: -1, D: 0, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00008, False, sin, {Gm: 1, Fm: 1, D: 2, Om: 1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00008, False, sin, {Gm: 0, Fm: 3, D: -2, Om: 1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00007, False, sin, {Gm: 1, Fm: -1, D: 2, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(
|
||||
-0.00007, False, sin, {Gm: 2, Fm: -1, D: -2, Om: -1, Ls: 0, Gs: 0, L2: 0}
|
||||
),
|
||||
Table4Row(-0.00007, False, sin, {Gm: 1, Fm: 1, D: 0, Om: 1, Ls: 0, Gs: 1, L2: 0}),
|
||||
Table4Row(-0.00006, False, sin, {Gm: 0, Fm: 1, D: 1, Om: 1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00006, False, sin, {Gm: 0, Fm: 1, D: -2, Om: 0, Ls: 0, Gs: -1, L2: 0}),
|
||||
Table4Row(0.00006, False, sin, {Gm: 1, Fm: -1, D: 0, Om: 1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00006, False, sin, {Gm: 0, Fm: 1, D: 2, Om: 1, Ls: 0, Gs: -1, L2: 0}),
|
||||
Table4Row(-0.00005, False, sin, {Gm: 1, Fm: 1, D: -2, Om: 0, Ls: 0, Gs: 1, L2: 0}),
|
||||
Table4Row(-0.00004, False, sin, {Gm: 2, Fm: 1, D: -2, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00004, False, sin, {Gm: 1, Fm: -3, D: 0, Om: -1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00004, False, sin, {Gm: 1, Fm: -1, D: 0, Om: 0, Ls: 0, Gs: -1, L2: 0}),
|
||||
Table4Row(-0.00003, False, sin, {Gm: 1, Fm: -1, D: 0, Om: 0, Ls: 0, Gs: 1, L2: 0}),
|
||||
Table4Row(0.00003, False, sin, {Gm: 0, Fm: 1, D: -1, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00003, False, sin, {Gm: 0, Fm: 1, D: -2, Om: 1, Ls: 0, Gs: -1, L2: 0}),
|
||||
Table4Row(-0.00003, False, sin, {Gm: 0, Fm: 1, D: -2, Om: -1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00003, False, sin, {Gm: 1, Fm: 1, D: -2, Om: 1, Ls: 0, Gs: -1, L2: 0}),
|
||||
Table4Row(0.00003, False, sin, {Gm: 0, Fm: 1, D: 0, Om: 0, Ls: 0, Gs: -1, L2: 0}),
|
||||
Table4Row(-0.00003, False, sin, {Gm: 0, Fm: 1, D: -1, Om: 1, Ls: 0, Gs: -1, L2: 0}),
|
||||
Table4Row(-0.00002, False, sin, {Gm: 1, Fm: -1, D: -2, Om: 0, Ls: 0, Gs: 1, L2: 0}),
|
||||
Table4Row(-0.00002, False, sin, {Gm: 0, Fm: 1, D: 0, Om: 0, Ls: 0, Gs: 1, L2: 0}),
|
||||
Table4Row(0.00002, False, sin, {Gm: 1, Fm: 1, D: -1, Om: 1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00002, False, sin, {Gm: 1, Fm: 1, D: 0, Om: -1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00002, False, sin, {Gm: 3, Fm: 1, D: 0, Om: 1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(
|
||||
-0.00002, False, sin, {Gm: 2, Fm: -1, D: -4, Om: -1, Ls: 0, Gs: 0, L2: 0}
|
||||
),
|
||||
Table4Row(
|
||||
0.00002, False, sin, {Gm: 1, Fm: -1, D: -2, Om: -1, Ls: 0, Gs: -1, L2: 0}
|
||||
),
|
||||
Table4Row(-0.00002, True, sin, {Gm: 1, Fm: -1, D: 0, Om: -1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(
|
||||
-0.00002, False, sin, {Gm: 1, Fm: -1, D: -4, Om: -1, Ls: 0, Gs: 0, L2: 0}
|
||||
),
|
||||
Table4Row(-0.00002, False, sin, {Gm: 1, Fm: 1, D: -4, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00002, False, sin, {Gm: 2, Fm: -1, D: -2, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00002, False, sin, {Gm: 1, Fm: 1, D: 2, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00002, False, sin, {Gm: 1, Fm: 1, D: 0, Om: 0, Ls: 0, Gs: -1, L2: 0}),
|
||||
]
|
||||
|
||||
table4_u: List[Table4Row] = [
|
||||
Table4Row(1, False, cos, {Gm: 0, Fm: 0, D: 0, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.10828, False, cos, {Gm: 1, Fm: 0, D: 0, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.01880, False, cos, {Gm: 1, Fm: 0, D: -2, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.01479, False, cos, {Gm: 0, Fm: 0, D: 2, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00181, False, cos, {Gm: 2, Fm: 0, D: -2, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00147, False, cos, {Gm: 2, Fm: 0, D: 0, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00105, False, cos, {Gm: 0, Fm: 0, D: 2, Om: 0, Ls: 0, Gs: -1, L2: 0}),
|
||||
Table4Row(-0.00075, False, cos, {Gm: 1, Fm: 0, D: -2, Om: 0, Ls: 0, Gs: 1, L2: 0}),
|
||||
Table4Row(-0.00067, False, cos, {Gm: 1, Fm: 0, D: 0, Om: 0, Ls: 0, Gs: -1, L2: 0}),
|
||||
Table4Row(0.00057, False, cos, {Gm: 0, Fm: 0, D: 1, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00055, False, cos, {Gm: 1, Fm: 0, D: 0, Om: 0, Ls: 0, Gs: 1, L2: 0}),
|
||||
Table4Row(-0.00046, False, cos, {Gm: 1, Fm: 0, D: 2, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00041, False, cos, {Gm: 1, Fm: -2, D: 0, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00024, False, cos, {Gm: 0, Fm: 0, D: 0, Om: 0, Ls: 0, Gs: 1, L2: 0}),
|
||||
Table4Row(0.00017, False, cos, {Gm: 0, Fm: 0, D: 2, Om: 0, Ls: 0, Gs: 1, L2: 0}),
|
||||
Table4Row(0.00013, False, cos, {Gm: 1, Fm: 0, D: -2, Om: 0, Ls: 0, Gs: -1, L2: 0}),
|
||||
Table4Row(-0.00010, False, cos, {Gm: 1, Fm: 0, D: -4, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00009, False, cos, {Gm: 0, Fm: 0, D: 1, Om: 0, Ls: 0, Gs: 1, L2: 0}),
|
||||
Table4Row(0.00007, False, cos, {Gm: 2, Fm: 0, D: -2, Om: 0, Ls: 0, Gs: 1, L2: 0}),
|
||||
Table4Row(0.00006, False, cos, {Gm: 3, Fm: 0, D: -2, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00006, False, cos, {Gm: 0, Fm: 2, D: -2, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00005, False, cos, {Gm: 0, Fm: 0, D: 2, Om: 0, Ls: 0, Gs: -2, L2: 0}),
|
||||
Table4Row(-0.00005, False, cos, {Gm: 2, Fm: 0, D: -4, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00005, False, cos, {Gm: 1, Fm: 2, D: -2, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00005, False, cos, {Gm: 1, Fm: 0, D: -1, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00004, False, cos, {Gm: 1, Fm: 0, D: 2, Om: 0, Ls: 0, Gs: -1, L2: 0}),
|
||||
Table4Row(-0.00004, False, cos, {Gm: 3, Fm: 0, D: 0, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00003, False, cos, {Gm: 1, Fm: 0, D: -4, Om: 0, Ls: 0, Gs: 1, L2: 0}),
|
||||
Table4Row(-0.00003, False, cos, {Gm: 2, Fm: -2, D: 0, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00003, False, cos, {Gm: 0, Fm: 2, D: 0, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
]
|
||||
|
||||
|
||||
table4_w: List[Table4Row] = [
|
||||
Table4Row(0.10478, False, sin, {Gm: 1, Fm: 0, D: 0, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.04105, False, sin, {Gm: 0, Fm: 2, D: 0, Om: 2, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.02130, False, sin, {Gm: 1, Fm: 0, D: -2, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.01779, False, sin, {Gm: 0, Fm: 2, D: 0, Om: 1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.01774, False, sin, {Gm: 0, Fm: 0, D: 0, Om: 1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00987, False, sin, {Gm: 0, Fm: 0, D: 2, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00338, False, sin, {Gm: 1, Fm: -2, D: 0, Om: -2, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00309, False, sin, {Gm: 0, Fm: 0, D: 0, Om: 0, Ls: 0, Gs: 1, L2: 0}),
|
||||
Table4Row(-0.00190, False, sin, {Gm: 0, Fm: 2, D: 0, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00144, False, sin, {Gm: 1, Fm: 0, D: 0, Om: 1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00144, False, sin, {Gm: 1, Fm: -2, D: 0, Om: -1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00113, False, sin, {Gm: 1, Fm: 2, D: 0, Om: 2, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00094, False, sin, {Gm: 1, Fm: 0, D: -2, Om: 0, Ls: 0, Gs: 1, L2: 0}),
|
||||
Table4Row(-0.00092, False, sin, {Gm: 2, Fm: 0, D: -2, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00071, False, sin, {Gm: 0, Fm: 0, D: 2, Om: 0, Ls: 0, Gs: -1, L2: 0}),
|
||||
Table4Row(0.00070, False, sin, {Gm: 2, Fm: 0, D: 0, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00067, False, sin, {Gm: 1, Fm: 2, D: -2, Om: 2, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00066, False, sin, {Gm: 0, Fm: 2, D: -2, Om: 1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00066, False, sin, {Gm: 0, Fm: 0, D: 2, Om: 1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00061, False, sin, {Gm: 1, Fm: 0, D: 0, Om: 0, Ls: 0, Gs: -1, L2: 0}),
|
||||
Table4Row(-0.00058, False, sin, {Gm: 0, Fm: 0, D: 1, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00049, False, sin, {Gm: 1, Fm: 2, D: 0, Om: 1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00049, False, sin, {Gm: 1, Fm: 0, D: 0, Om: -1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00042, False, sin, {Gm: 1, Fm: 0, D: 0, Om: 0, Ls: 0, Gs: 1, L2: 0}),
|
||||
Table4Row(0.00034, False, sin, {Gm: 0, Fm: 2, D: -2, Om: 2, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00026, False, sin, {Gm: 0, Fm: 2, D: -2, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00025, False, sin, {Gm: 1, Fm: -2, D: -2, Om: -2, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00024, False, sin, {Gm: 1, Fm: -2, D: 0, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00023, False, sin, {Gm: 1, Fm: 2, D: -2, Om: 1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00023, False, sin, {Gm: 1, Fm: 0, D: -2, Om: -1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00019, False, sin, {Gm: 1, Fm: 0, D: 2, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00012, False, sin, {Gm: 1, Fm: 0, D: -2, Om: 0, Ls: 0, Gs: -1, L2: 0}),
|
||||
Table4Row(0.00011, False, sin, {Gm: 1, Fm: 0, D: -2, Om: 1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00011, False, sin, {Gm: 1, Fm: -2, D: -2, Om: -1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00010, False, sin, {Gm: 0, Fm: 0, D: 2, Om: 0, Ls: 0, Gs: 1, L2: 0}),
|
||||
Table4Row(0.00009, False, sin, {Gm: 1, Fm: 0, D: -1, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00008, False, sin, {Gm: 0, Fm: 0, D: 1, Om: 0, Ls: 0, Gs: 1, L2: 0}),
|
||||
Table4Row(-0.00008, False, sin, {Gm: 0, Fm: 2, D: 2, Om: 2, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00008, False, sin, {Gm: 0, Fm: 0, D: 0, Om: 2, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00007, False, sin, {Gm: 0, Fm: 2, D: 0, Om: 2, Ls: 0, Gs: -1, L2: 0}),
|
||||
Table4Row(0.00006, False, sin, {Gm: 0, Fm: 2, D: 0, Om: 2, Ls: 0, Gs: 1, L2: 0}),
|
||||
Table4Row(-0.00005, False, sin, {Gm: 1, Fm: 2, D: 0, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00005, False, sin, {Gm: 3, Fm: 0, D: 0, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(
|
||||
-0.00005, False, sin, {Gm: 1, Fm: 0, D: 0, Om: 0, Ls: 16, Gs: 0, L2: -18}
|
||||
),
|
||||
Table4Row(-0.00005, False, sin, {Gm: 2, Fm: 2, D: 0, Om: 2, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00004, True, sin, {Gm: 0, Fm: 2, D: 0, Om: 2, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00004, False, cos, {Gm: 1, Fm: 0, D: 0, Om: 0, Ls: 16, Gs: 0, L2: -18}),
|
||||
Table4Row(-0.00004, False, sin, {Gm: 1, Fm: -2, D: 2, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00004, False, sin, {Gm: 1, Fm: 0, D: -4, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00004, False, sin, {Gm: 3, Fm: 0, D: -2, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00004, False, sin, {Gm: 0, Fm: 2, D: 2, Om: 1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00004, False, sin, {Gm: 0, Fm: 0, D: 2, Om: -1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00003, False, sin, {Gm: 0, Fm: 0, D: 0, Om: 0, Ls: 0, Gs: 2, L2: 0}),
|
||||
Table4Row(-0.00003, False, sin, {Gm: 1, Fm: 0, D: -2, Om: 0, Ls: 0, Gs: 2, L2: 0}),
|
||||
Table4Row(0.00003, False, sin, {Gm: 0, Fm: 2, D: -2, Om: 1, Ls: 0, Gs: 1, L2: 0}),
|
||||
Table4Row(-0.00003, False, sin, {Gm: 0, Fm: 0, D: 2, Om: 1, Ls: 0, Gs: -1, L2: 0}),
|
||||
Table4Row(0.00003, False, sin, {Gm: 2, Fm: 2, D: -2, Om: 2, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00003, False, sin, {Gm: 0, Fm: 0, D: 2, Om: 0, Ls: 0, Gs: -2, L2: 0}),
|
||||
Table4Row(-0.00003, False, sin, {Gm: 2, Fm: 0, D: -2, Om: 0, Ls: 0, Gs: 1, L2: 0}),
|
||||
Table4Row(0.00003, False, sin, {Gm: 1, Fm: 2, D: -2, Om: 2, Ls: 0, Gs: 1, L2: 0}),
|
||||
Table4Row(-0.00003, False, sin, {Gm: 2, Fm: 0, D: -4, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00002, False, sin, {Gm: 0, Fm: 2, D: -2, Om: 2, Ls: 0, Gs: 1, L2: 0}),
|
||||
Table4Row(-0.00002, False, sin, {Gm: 2, Fm: 2, D: 0, Om: 1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00002, False, sin, {Gm: 2, Fm: 0, D: 0, Om: -1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00002, True, cos, {Gm: 1, Fm: 0, D: 0, Om: 0, Ls: 16, Gs: 0, L2: -18}),
|
||||
Table4Row(0.00002, False, sin, {Gm: 0, Fm: 0, D: 4, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00002, False, sin, {Gm: 0, Fm: 2, D: -1, Om: 2, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00002, False, sin, {Gm: 1, Fm: 2, D: -2, Om: 0, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00002, False, sin, {Gm: 2, Fm: 0, D: 0, Om: 1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00002, False, sin, {Gm: 2, Fm: -2, D: 0, Om: -1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(0.00002, False, sin, {Gm: 1, Fm: 0, D: 2, Om: 0, Ls: 0, Gs: -1, L2: 0}),
|
||||
Table4Row(0.00002, False, sin, {Gm: 2, Fm: 0, D: 0, Om: 0, Ls: 0, Gs: -1, L2: 0}),
|
||||
Table4Row(-0.00002, False, sin, {Gm: 1, Fm: 0, D: -4, Om: 0, Ls: 0, Gs: 1, L2: 0}),
|
||||
Table4Row(0.00002, True, sin, {Gm: 1, Fm: 0, D: 0, Om: 0, Ls: 16, Gs: 0, L2: -18}),
|
||||
Table4Row(
|
||||
-0.00002, False, sin, {Gm: 1, Fm: -2, D: 0, Om: -2, Ls: 0, Gs: -1, L2: 0}
|
||||
),
|
||||
Table4Row(0.00002, False, sin, {Gm: 2, Fm: -2, D: 0, Om: -2, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00002, False, sin, {Gm: 1, Fm: 0, D: 2, Om: 1, Ls: 0, Gs: 0, L2: 0}),
|
||||
Table4Row(-0.00002, False, sin, {Gm: 1, Fm: -2, D: 2, Om: -1, Ls: 0, Gs: 0, L2: 0}),
|
||||
]
|
||||
@ -0,0 +1 @@
|
||||
import os; var = 'SETUPTOOLS_USE_DISTUTILS'; enabled = os.environ.get(var, 'stdlib') == 'local'; enabled and __import__('_distutils_hack').add_shim();
|
||||
@ -0,0 +1,2 @@
|
||||
Version: 1.8.0
|
||||
Arguments: ['C:\\Users\\runneradmin\\AppData\\Local\\Temp\\cibw-run-vclkdodh\\cp39-win_amd64\\build\\venv\\Scripts\\delvewheel', 'repair', '--add-path', 'D:/a/numpy/numpy/.openblas/lib', '-w', 'C:\\Users\\runneradmin\\AppData\\Local\\Temp\\cibw-run-vclkdodh\\cp39-win_amd64\\repaired_wheel', 'C:\\Users\\runneradmin\\AppData\\Local\\Temp\\cibw-run-vclkdodh\\cp39-win_amd64\\built_wheel\\numpy-2.0.2-cp39-cp39-win_amd64.whl']
|
||||
@ -0,0 +1 @@
|
||||
pip
|
||||
@ -0,0 +1,945 @@
|
||||
Copyright (c) 2005-2024, NumPy Developers.
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following
|
||||
disclaimer in the documentation and/or other materials provided
|
||||
with the distribution.
|
||||
|
||||
* Neither the name of the NumPy Developers nor the names of any
|
||||
contributors may be used to endorse or promote products derived
|
||||
from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
----
|
||||
|
||||
The NumPy repository and source distributions bundle several libraries that are
|
||||
compatibly licensed. We list these here.
|
||||
|
||||
Name: lapack-lite
|
||||
Files: numpy/linalg/lapack_lite/*
|
||||
License: BSD-3-Clause
|
||||
For details, see numpy/linalg/lapack_lite/LICENSE.txt
|
||||
|
||||
Name: dragon4
|
||||
Files: numpy/_core/src/multiarray/dragon4.c
|
||||
License: MIT
|
||||
For license text, see numpy/_core/src/multiarray/dragon4.c
|
||||
|
||||
Name: libdivide
|
||||
Files: numpy/_core/include/numpy/libdivide/*
|
||||
License: Zlib
|
||||
For license text, see numpy/_core/include/numpy/libdivide/LICENSE.txt
|
||||
|
||||
|
||||
Note that the following files are vendored in the repository and sdist but not
|
||||
installed in built numpy packages:
|
||||
|
||||
Name: Meson
|
||||
Files: vendored-meson/meson/*
|
||||
License: Apache 2.0
|
||||
For license text, see vendored-meson/meson/COPYING
|
||||
|
||||
Name: spin
|
||||
Files: .spin/cmds.py
|
||||
License: BSD-3
|
||||
For license text, see .spin/LICENSE
|
||||
|
||||
----
|
||||
|
||||
This binary distribution of NumPy also bundles the following software:
|
||||
|
||||
|
||||
Name: OpenBLAS
|
||||
Files: numpy.libs\libscipy_openblas*.dll
|
||||
Description: bundled as a dynamically linked library
|
||||
Availability: https://github.com/OpenMathLib/OpenBLAS/
|
||||
License: BSD-3-Clause
|
||||
Copyright (c) 2011-2014, The OpenBLAS Project
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
3. Neither the name of the OpenBLAS project nor the names of
|
||||
its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written
|
||||
permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
|
||||
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
|
||||
Name: LAPACK
|
||||
Files: numpy.libs\libscipy_openblas*.dll
|
||||
Description: bundled in OpenBLAS
|
||||
Availability: https://github.com/OpenMathLib/OpenBLAS/
|
||||
License: BSD-3-Clause-Attribution
|
||||
Copyright (c) 1992-2013 The University of Tennessee and The University
|
||||
of Tennessee Research Foundation. All rights
|
||||
reserved.
|
||||
Copyright (c) 2000-2013 The University of California Berkeley. All
|
||||
rights reserved.
|
||||
Copyright (c) 2006-2013 The University of Colorado Denver. All rights
|
||||
reserved.
|
||||
|
||||
$COPYRIGHT$
|
||||
|
||||
Additional copyrights may follow
|
||||
|
||||
$HEADER$
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
- Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
- Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer listed
|
||||
in this license in the documentation and/or other materials
|
||||
provided with the distribution.
|
||||
|
||||
- Neither the name of the copyright holders nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
The copyright holders provide no reassurances that the source code
|
||||
provided does not infringe any patent, copyright, or any other
|
||||
intellectual property rights of third parties. The copyright holders
|
||||
disclaim any liability to any recipient for claims brought against
|
||||
recipient by any third party for infringement of that parties
|
||||
intellectual property rights.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
|
||||
Name: GCC runtime library
|
||||
Files: numpy.libs\libscipy_openblas*.dll
|
||||
Description: statically linked to files compiled with gcc
|
||||
Availability: https://gcc.gnu.org/git/?p=gcc.git;a=tree;f=libgfortran
|
||||
License: GPL-3.0-with-GCC-exception
|
||||
Copyright (C) 2002-2017 Free Software Foundation, Inc.
|
||||
|
||||
Libgfortran is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 3, or (at your option)
|
||||
any later version.
|
||||
|
||||
Libgfortran is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
Under Section 7 of GPL version 3, you are granted additional
|
||||
permissions described in the GCC Runtime Library Exception, version
|
||||
3.1, as published by the Free Software Foundation.
|
||||
|
||||
You should have received a copy of the GNU General Public License and
|
||||
a copy of the GCC Runtime Library Exception along with this program;
|
||||
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
||||
<http://www.gnu.org/licenses/>.
|
||||
|
||||
----
|
||||
|
||||
Full text of license texts referred to above follows (that they are
|
||||
listed below does not necessarily imply the conditions apply to the
|
||||
present binary release):
|
||||
|
||||
----
|
||||
|
||||
GCC RUNTIME LIBRARY EXCEPTION
|
||||
|
||||
Version 3.1, 31 March 2009
|
||||
|
||||
Copyright (C) 2009 Free Software Foundation, Inc. <http://fsf.org/>
|
||||
|
||||
Everyone is permitted to copy and distribute verbatim copies of this
|
||||
license document, but changing it is not allowed.
|
||||
|
||||
This GCC Runtime Library Exception ("Exception") is an additional
|
||||
permission under section 7 of the GNU General Public License, version
|
||||
3 ("GPLv3"). It applies to a given file (the "Runtime Library") that
|
||||
bears a notice placed by the copyright holder of the file stating that
|
||||
the file is governed by GPLv3 along with this Exception.
|
||||
|
||||
When you use GCC to compile a program, GCC may combine portions of
|
||||
certain GCC header files and runtime libraries with the compiled
|
||||
program. The purpose of this Exception is to allow compilation of
|
||||
non-GPL (including proprietary) programs to use, in this way, the
|
||||
header files and runtime libraries covered by this Exception.
|
||||
|
||||
0. Definitions.
|
||||
|
||||
A file is an "Independent Module" if it either requires the Runtime
|
||||
Library for execution after a Compilation Process, or makes use of an
|
||||
interface provided by the Runtime Library, but is not otherwise based
|
||||
on the Runtime Library.
|
||||
|
||||
"GCC" means a version of the GNU Compiler Collection, with or without
|
||||
modifications, governed by version 3 (or a specified later version) of
|
||||
the GNU General Public License (GPL) with the option of using any
|
||||
subsequent versions published by the FSF.
|
||||
|
||||
"GPL-compatible Software" is software whose conditions of propagation,
|
||||
modification and use would permit combination with GCC in accord with
|
||||
the license of GCC.
|
||||
|
||||
"Target Code" refers to output from any compiler for a real or virtual
|
||||
target processor architecture, in executable form or suitable for
|
||||
input to an assembler, loader, linker and/or execution
|
||||
phase. Notwithstanding that, Target Code does not include data in any
|
||||
format that is used as a compiler intermediate representation, or used
|
||||
for producing a compiler intermediate representation.
|
||||
|
||||
The "Compilation Process" transforms code entirely represented in
|
||||
non-intermediate languages designed for human-written code, and/or in
|
||||
Java Virtual Machine byte code, into Target Code. Thus, for example,
|
||||
use of source code generators and preprocessors need not be considered
|
||||
part of the Compilation Process, since the Compilation Process can be
|
||||
understood as starting with the output of the generators or
|
||||
preprocessors.
|
||||
|
||||
A Compilation Process is "Eligible" if it is done using GCC, alone or
|
||||
with other GPL-compatible software, or if it is done without using any
|
||||
work based on GCC. For example, using non-GPL-compatible Software to
|
||||
optimize any GCC intermediate representations would not qualify as an
|
||||
Eligible Compilation Process.
|
||||
|
||||
1. Grant of Additional Permission.
|
||||
|
||||
You have permission to propagate a work of Target Code formed by
|
||||
combining the Runtime Library with Independent Modules, even if such
|
||||
propagation would otherwise violate the terms of GPLv3, provided that
|
||||
all Target Code was generated by Eligible Compilation Processes. You
|
||||
may then convey such a combination under terms of your choice,
|
||||
consistent with the licensing of the Independent Modules.
|
||||
|
||||
2. No Weakening of GCC Copyleft.
|
||||
|
||||
The availability of this Exception does not imply any general
|
||||
presumption that third-party software is unaffected by the copyleft
|
||||
requirements of the license of GCC.
|
||||
|
||||
----
|
||||
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
the GNU General Public License is intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users. We, the Free Software Foundation, use the
|
||||
GNU General Public License for most of our software; it applies also to
|
||||
any other work released this way by its authors. You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to prevent others from denying you
|
||||
these rights or asking you to surrender the rights. Therefore, you have
|
||||
certain responsibilities if you distribute copies of the software, or if
|
||||
you modify it: responsibilities to respect the freedom of others.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must pass on to the recipients the same
|
||||
freedoms that you received. You must make sure that they, too, receive
|
||||
or can get the source code. And you must show them these terms so they
|
||||
know their rights.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
that there is no warranty for this free software. For both users' and
|
||||
authors' sake, the GPL requires that modified versions be marked as
|
||||
changed, so that their problems will not be attributed erroneously to
|
||||
authors of previous versions.
|
||||
|
||||
Some devices are designed to deny users access to install or run
|
||||
modified versions of the software inside them, although the manufacturer
|
||||
can do so. This is fundamentally incompatible with the aim of
|
||||
protecting users' freedom to change the software. The systematic
|
||||
pattern of such abuse occurs in the area of products for individuals to
|
||||
use, which is precisely where it is most unacceptable. Therefore, we
|
||||
have designed this version of the GPL to prohibit the practice for those
|
||||
products. If such problems arise substantially in other domains, we
|
||||
stand ready to extend this provision to those domains in future versions
|
||||
of the GPL, as needed to protect the freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
States should not allow patents to restrict development and use of
|
||||
software on general-purpose computers, but in those that do, we wish to
|
||||
avoid the special danger that patents applied to a free program could
|
||||
make it effectively proprietary. To prevent this, the GPL assures that
|
||||
patents cannot be used to render the program non-free.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Use with the GNU Affero General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU Affero General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the special requirements of the GNU Affero General Public License,
|
||||
section 13, concerning interaction through a network will apply to the
|
||||
combination as such.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program does terminal interaction, make it output a short
|
||||
notice like this when it starts in an interactive mode:
|
||||
|
||||
<program> Copyright (C) <year> <name of author>
|
||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, your program's commands
|
||||
might be different; for a GUI interface, you would use an "about box".
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU GPL, see
|
||||
<http://www.gnu.org/licenses/>.
|
||||
|
||||
The GNU General Public License does not permit incorporating your program
|
||||
into proprietary programs. If your program is a subroutine library, you
|
||||
may consider it more useful to permit linking proprietary applications with
|
||||
the library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License. But first, please read
|
||||
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,4 @@
|
||||
Wheel-Version: 1.0
|
||||
Generator: meson
|
||||
Root-Is-Purelib: false
|
||||
Tag: cp39-cp39-win_amd64
|
||||
@ -0,0 +1,10 @@
|
||||
[array_api]
|
||||
numpy = numpy
|
||||
|
||||
[pyinstaller40]
|
||||
hook-dirs = numpy:_pyinstaller_hooks_dir
|
||||
|
||||
[console_scripts]
|
||||
f2py = numpy.f2py.f2py2e:main
|
||||
numpy-config = numpy._configtool:main
|
||||
|
||||
@ -0,0 +1,2 @@
|
||||
libscipy_openblas64_-caad452230ae4ddb57899b8b3a33c55c.dll
|
||||
msvcp140-23ebcc0b37c8e3d074511f362feac48b.dll
|
||||
Binary file not shown.
Binary file not shown.
@ -0,0 +1,162 @@
|
||||
# This file is generated by numpy's build process
|
||||
# It contains system_info results at the time of building this package.
|
||||
from enum import Enum
|
||||
from numpy._core._multiarray_umath import (
|
||||
__cpu_features__,
|
||||
__cpu_baseline__,
|
||||
__cpu_dispatch__,
|
||||
)
|
||||
|
||||
__all__ = ["show"]
|
||||
_built_with_meson = True
|
||||
|
||||
|
||||
class DisplayModes(Enum):
|
||||
stdout = "stdout"
|
||||
dicts = "dicts"
|
||||
|
||||
|
||||
def _cleanup(d):
|
||||
"""
|
||||
Removes empty values in a `dict` recursively
|
||||
This ensures we remove values that Meson could not provide to CONFIG
|
||||
"""
|
||||
if isinstance(d, dict):
|
||||
return {k: _cleanup(v) for k, v in d.items() if v and _cleanup(v)}
|
||||
else:
|
||||
return d
|
||||
|
||||
|
||||
CONFIG = _cleanup(
|
||||
{
|
||||
"Compilers": {
|
||||
"c": {
|
||||
"name": "msvc",
|
||||
"linker": r"link",
|
||||
"version": "19.29.30154",
|
||||
"commands": r"cl",
|
||||
"args": r"",
|
||||
"linker args": r"",
|
||||
},
|
||||
"cython": {
|
||||
"name": "cython",
|
||||
"linker": r"cython",
|
||||
"version": "3.0.11",
|
||||
"commands": r"cython",
|
||||
"args": r"",
|
||||
"linker args": r"",
|
||||
},
|
||||
"c++": {
|
||||
"name": "msvc",
|
||||
"linker": r"link",
|
||||
"version": "19.29.30154",
|
||||
"commands": r"cl",
|
||||
"args": r"",
|
||||
"linker args": r"",
|
||||
},
|
||||
},
|
||||
"Machine Information": {
|
||||
"host": {
|
||||
"cpu": "x86_64",
|
||||
"family": "x86_64",
|
||||
"endian": "little",
|
||||
"system": "windows",
|
||||
},
|
||||
"build": {
|
||||
"cpu": "x86_64",
|
||||
"family": "x86_64",
|
||||
"endian": "little",
|
||||
"system": "windows",
|
||||
},
|
||||
"cross-compiled": bool("False".lower().replace("false", "")),
|
||||
},
|
||||
"Build Dependencies": {
|
||||
"blas": {
|
||||
"name": "scipy-openblas",
|
||||
"found": bool("True".lower().replace("false", "")),
|
||||
"version": "0.3.27",
|
||||
"detection method": "pkgconfig",
|
||||
"include directory": r"C:/Users/runneradmin/AppData/Local/Temp/cibw-run-vclkdodh/cp39-win_amd64/build/venv/Lib/site-packages/scipy_openblas64/include",
|
||||
"lib directory": r"C:/Users/runneradmin/AppData/Local/Temp/cibw-run-vclkdodh/cp39-win_amd64/build/venv/Lib/site-packages/scipy_openblas64/lib",
|
||||
"openblas configuration": r"OpenBLAS 0.3.27 USE64BITINT DYNAMIC_ARCH NO_AFFINITY Zen MAX_THREADS=24",
|
||||
"pc file directory": r"D:/a/numpy/numpy/.openblas",
|
||||
},
|
||||
"lapack": {
|
||||
"name": "scipy-openblas",
|
||||
"found": bool("True".lower().replace("false", "")),
|
||||
"version": "0.3.27",
|
||||
"detection method": "pkgconfig",
|
||||
"include directory": r"C:/Users/runneradmin/AppData/Local/Temp/cibw-run-vclkdodh/cp39-win_amd64/build/venv/Lib/site-packages/scipy_openblas64/include",
|
||||
"lib directory": r"C:/Users/runneradmin/AppData/Local/Temp/cibw-run-vclkdodh/cp39-win_amd64/build/venv/Lib/site-packages/scipy_openblas64/lib",
|
||||
"openblas configuration": r"OpenBLAS 0.3.27 USE64BITINT DYNAMIC_ARCH NO_AFFINITY Zen MAX_THREADS=24",
|
||||
"pc file directory": r"D:/a/numpy/numpy/.openblas",
|
||||
},
|
||||
},
|
||||
"Python Information": {
|
||||
"path": r"C:\Users\runneradmin\AppData\Local\Temp\build-env-le8kd45u\Scripts\python.exe",
|
||||
"version": "3.9",
|
||||
},
|
||||
"SIMD Extensions": {
|
||||
"baseline": __cpu_baseline__,
|
||||
"found": [
|
||||
feature for feature in __cpu_dispatch__ if __cpu_features__[feature]
|
||||
],
|
||||
"not found": [
|
||||
feature for feature in __cpu_dispatch__ if not __cpu_features__[feature]
|
||||
],
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def _check_pyyaml():
|
||||
import yaml
|
||||
|
||||
return yaml
|
||||
|
||||
|
||||
def show(mode=DisplayModes.stdout.value):
|
||||
"""
|
||||
Show libraries and system information on which NumPy was built
|
||||
and is being used
|
||||
|
||||
Parameters
|
||||
----------
|
||||
mode : {`'stdout'`, `'dicts'`}, optional.
|
||||
Indicates how to display the config information.
|
||||
`'stdout'` prints to console, `'dicts'` returns a dictionary
|
||||
of the configuration.
|
||||
|
||||
Returns
|
||||
-------
|
||||
out : {`dict`, `None`}
|
||||
If mode is `'dicts'`, a dict is returned, else None
|
||||
|
||||
See Also
|
||||
--------
|
||||
get_include : Returns the directory containing NumPy C
|
||||
header files.
|
||||
|
||||
Notes
|
||||
-----
|
||||
1. The `'stdout'` mode will give more readable
|
||||
output if ``pyyaml`` is installed
|
||||
|
||||
"""
|
||||
if mode == DisplayModes.stdout.value:
|
||||
try: # Non-standard library, check import
|
||||
yaml = _check_pyyaml()
|
||||
|
||||
print(yaml.dump(CONFIG))
|
||||
except ModuleNotFoundError:
|
||||
import warnings
|
||||
import json
|
||||
|
||||
warnings.warn("Install `pyyaml` for better output", stacklevel=1)
|
||||
print(json.dumps(CONFIG, indent=2))
|
||||
elif mode == DisplayModes.dicts.value:
|
||||
return CONFIG
|
||||
else:
|
||||
raise AttributeError(
|
||||
f"Invalid `mode`, use one of: {', '.join([e.value for e in DisplayModes])}"
|
||||
)
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,568 @@
|
||||
"""
|
||||
NumPy
|
||||
=====
|
||||
|
||||
Provides
|
||||
1. An array object of arbitrary homogeneous items
|
||||
2. Fast mathematical operations over arrays
|
||||
3. Linear Algebra, Fourier Transforms, Random Number Generation
|
||||
|
||||
How to use the documentation
|
||||
----------------------------
|
||||
Documentation is available in two forms: docstrings provided
|
||||
with the code, and a loose standing reference guide, available from
|
||||
`the NumPy homepage <https://numpy.org>`_.
|
||||
|
||||
We recommend exploring the docstrings using
|
||||
`IPython <https://ipython.org>`_, an advanced Python shell with
|
||||
TAB-completion and introspection capabilities. See below for further
|
||||
instructions.
|
||||
|
||||
The docstring examples assume that `numpy` has been imported as ``np``::
|
||||
|
||||
>>> import numpy as np
|
||||
|
||||
Code snippets are indicated by three greater-than signs::
|
||||
|
||||
>>> x = 42
|
||||
>>> x = x + 1
|
||||
|
||||
Use the built-in ``help`` function to view a function's docstring::
|
||||
|
||||
>>> help(np.sort)
|
||||
... # doctest: +SKIP
|
||||
|
||||
For some objects, ``np.info(obj)`` may provide additional help. This is
|
||||
particularly true if you see the line "Help on ufunc object:" at the top
|
||||
of the help() page. Ufuncs are implemented in C, not Python, for speed.
|
||||
The native Python help() does not know how to view their help, but our
|
||||
np.info() function does.
|
||||
|
||||
Available subpackages
|
||||
---------------------
|
||||
lib
|
||||
Basic functions used by several sub-packages.
|
||||
random
|
||||
Core Random Tools
|
||||
linalg
|
||||
Core Linear Algebra Tools
|
||||
fft
|
||||
Core FFT routines
|
||||
polynomial
|
||||
Polynomial tools
|
||||
testing
|
||||
NumPy testing tools
|
||||
distutils
|
||||
Enhancements to distutils with support for
|
||||
Fortran compilers support and more (for Python <= 3.11)
|
||||
|
||||
Utilities
|
||||
---------
|
||||
test
|
||||
Run numpy unittests
|
||||
show_config
|
||||
Show numpy build configuration
|
||||
__version__
|
||||
NumPy version string
|
||||
|
||||
Viewing documentation using IPython
|
||||
-----------------------------------
|
||||
|
||||
Start IPython and import `numpy` usually under the alias ``np``: `import
|
||||
numpy as np`. Then, directly past or use the ``%cpaste`` magic to paste
|
||||
examples into the shell. To see which functions are available in `numpy`,
|
||||
type ``np.<TAB>`` (where ``<TAB>`` refers to the TAB key), or use
|
||||
``np.*cos*?<ENTER>`` (where ``<ENTER>`` refers to the ENTER key) to narrow
|
||||
down the list. To view the docstring for a function, use
|
||||
``np.cos?<ENTER>`` (to view the docstring) and ``np.cos??<ENTER>`` (to view
|
||||
the source code).
|
||||
|
||||
Copies vs. in-place operation
|
||||
-----------------------------
|
||||
Most of the functions in `numpy` return a copy of the array argument
|
||||
(e.g., `np.sort`). In-place versions of these functions are often
|
||||
available as array methods, i.e. ``x = np.array([1,2,3]); x.sort()``.
|
||||
Exceptions to this rule are documented.
|
||||
|
||||
"""
|
||||
|
||||
|
||||
# start delvewheel patch
|
||||
def _delvewheel_patch_1_8_0():
|
||||
import ctypes
|
||||
import os
|
||||
import platform
|
||||
import sys
|
||||
libs_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, 'numpy.libs'))
|
||||
is_conda_cpython = platform.python_implementation() == 'CPython' and (hasattr(ctypes.pythonapi, 'Anaconda_GetVersion') or 'packaged by conda-forge' in sys.version)
|
||||
if sys.version_info[:2] >= (3, 8) and not is_conda_cpython or sys.version_info[:2] >= (3, 10):
|
||||
if os.path.isdir(libs_dir):
|
||||
os.add_dll_directory(libs_dir)
|
||||
else:
|
||||
load_order_filepath = os.path.join(libs_dir, '.load-order-numpy-2.0.2')
|
||||
if os.path.isfile(load_order_filepath):
|
||||
with open(os.path.join(libs_dir, '.load-order-numpy-2.0.2')) as file:
|
||||
load_order = file.read().split()
|
||||
kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
|
||||
kernel32.LoadLibraryExW.restype = ctypes.c_void_p
|
||||
for lib in load_order:
|
||||
lib_path = os.path.join(os.path.join(libs_dir, lib))
|
||||
if os.path.isfile(lib_path) and not kernel32.LoadLibraryExW(ctypes.c_wchar_p(lib_path), None, 8):
|
||||
raise OSError('Error loading {}; {}'.format(lib, ctypes.FormatError(ctypes.get_last_error())))
|
||||
|
||||
|
||||
_delvewheel_patch_1_8_0()
|
||||
del _delvewheel_patch_1_8_0
|
||||
# end delvewheel patch
|
||||
|
||||
import os
|
||||
import sys
|
||||
import warnings
|
||||
|
||||
from ._globals import _NoValue, _CopyMode
|
||||
from ._expired_attrs_2_0 import __expired_attributes__
|
||||
|
||||
|
||||
# If a version with git hash was stored, use that instead
|
||||
from . import version
|
||||
from .version import __version__
|
||||
|
||||
# We first need to detect if we're being called as part of the numpy setup
|
||||
# procedure itself in a reliable manner.
|
||||
try:
|
||||
__NUMPY_SETUP__
|
||||
except NameError:
|
||||
__NUMPY_SETUP__ = False
|
||||
|
||||
if __NUMPY_SETUP__:
|
||||
sys.stderr.write('Running from numpy source directory.\n')
|
||||
else:
|
||||
# Allow distributors to run custom init code before importing numpy._core
|
||||
from . import _distributor_init
|
||||
|
||||
try:
|
||||
from numpy.__config__ import show as show_config
|
||||
except ImportError as e:
|
||||
msg = """Error importing numpy: you should not try to import numpy from
|
||||
its source directory; please exit the numpy source tree, and relaunch
|
||||
your python interpreter from there."""
|
||||
raise ImportError(msg) from e
|
||||
|
||||
from . import _core
|
||||
from ._core import (
|
||||
False_, ScalarType, True_, _get_promotion_state, _no_nep50_warning,
|
||||
_set_promotion_state, abs, absolute, acos, acosh, add, all, allclose,
|
||||
amax, amin, any, arange, arccos, arccosh, arcsin, arcsinh,
|
||||
arctan, arctan2, arctanh, argmax, argmin, argpartition, argsort,
|
||||
argwhere, around, array, array2string, array_equal, array_equiv,
|
||||
array_repr, array_str, asanyarray, asarray, ascontiguousarray,
|
||||
asfortranarray, asin, asinh, atan, atanh, atan2, astype, atleast_1d,
|
||||
atleast_2d, atleast_3d, base_repr, binary_repr, bitwise_and,
|
||||
bitwise_count, bitwise_invert, bitwise_left_shift, bitwise_not,
|
||||
bitwise_or, bitwise_right_shift, bitwise_xor, block, bool, bool_,
|
||||
broadcast, busday_count, busday_offset, busdaycalendar, byte, bytes_,
|
||||
can_cast, cbrt, cdouble, ceil, character, choose, clip, clongdouble,
|
||||
complex128, complex64, complexfloating, compress, concat, concatenate,
|
||||
conj, conjugate, convolve, copysign, copyto, correlate, cos, cosh,
|
||||
count_nonzero, cross, csingle, cumprod, cumsum,
|
||||
datetime64, datetime_as_string, datetime_data, deg2rad, degrees,
|
||||
diagonal, divide, divmod, dot, double, dtype, e, einsum, einsum_path,
|
||||
empty, empty_like, equal, errstate, euler_gamma, exp, exp2, expm1,
|
||||
fabs, finfo, flatiter, flatnonzero, flexible, float16, float32,
|
||||
float64, float_power, floating, floor, floor_divide, fmax, fmin, fmod,
|
||||
format_float_positional, format_float_scientific, frexp, from_dlpack,
|
||||
frombuffer, fromfile, fromfunction, fromiter, frompyfunc, fromstring,
|
||||
full, full_like, gcd, generic, geomspace, get_printoptions,
|
||||
getbufsize, geterr, geterrcall, greater, greater_equal, half,
|
||||
heaviside, hstack, hypot, identity, iinfo, iinfo, indices, inexact,
|
||||
inf, inner, int16, int32, int64, int8, int_, intc, integer, intp,
|
||||
invert, is_busday, isclose, isdtype, isfinite, isfortran, isinf,
|
||||
isnan, isnat, isscalar, issubdtype, lcm, ldexp, left_shift, less,
|
||||
less_equal, lexsort, linspace, little_endian, log, log10, log1p, log2,
|
||||
logaddexp, logaddexp2, logical_and, logical_not, logical_or,
|
||||
logical_xor, logspace, long, longdouble, longlong, matmul,
|
||||
matrix_transpose, max, maximum, may_share_memory, mean, memmap, min,
|
||||
min_scalar_type, minimum, mod, modf, moveaxis, multiply, nan, ndarray,
|
||||
ndim, nditer, negative, nested_iters, newaxis, nextafter, nonzero,
|
||||
not_equal, number, object_, ones, ones_like, outer, partition,
|
||||
permute_dims, pi, positive, pow, power, printoptions, prod,
|
||||
promote_types, ptp, put, putmask, rad2deg, radians, ravel, recarray,
|
||||
reciprocal, record, remainder, repeat, require, reshape, resize,
|
||||
result_type, right_shift, rint, roll, rollaxis, round, sctypeDict,
|
||||
searchsorted, set_printoptions, setbufsize, seterr, seterrcall, shape,
|
||||
shares_memory, short, sign, signbit, signedinteger, sin, single, sinh,
|
||||
size, sort, spacing, sqrt, square, squeeze, stack, std,
|
||||
str_, subtract, sum, swapaxes, take, tan, tanh, tensordot,
|
||||
timedelta64, trace, transpose, true_divide, trunc, typecodes, ubyte,
|
||||
ufunc, uint, uint16, uint32, uint64, uint8, uintc, uintp, ulong,
|
||||
ulonglong, unsignedinteger, ushort, var, vdot, vecdot, void, vstack,
|
||||
where, zeros, zeros_like
|
||||
)
|
||||
|
||||
# NOTE: It's still under discussion whether these aliases
|
||||
# should be removed.
|
||||
for ta in ["float96", "float128", "complex192", "complex256"]:
|
||||
try:
|
||||
globals()[ta] = getattr(_core, ta)
|
||||
except AttributeError:
|
||||
pass
|
||||
del ta
|
||||
|
||||
from . import lib
|
||||
from .lib import scimath as emath
|
||||
from .lib._histograms_impl import (
|
||||
histogram, histogram_bin_edges, histogramdd
|
||||
)
|
||||
from .lib._nanfunctions_impl import (
|
||||
nanargmax, nanargmin, nancumprod, nancumsum, nanmax, nanmean,
|
||||
nanmedian, nanmin, nanpercentile, nanprod, nanquantile, nanstd,
|
||||
nansum, nanvar
|
||||
)
|
||||
from .lib._function_base_impl import (
|
||||
select, piecewise, trim_zeros, copy, iterable, percentile, diff,
|
||||
gradient, angle, unwrap, sort_complex, flip, rot90, extract, place,
|
||||
vectorize, asarray_chkfinite, average, bincount, digitize, cov,
|
||||
corrcoef, median, sinc, hamming, hanning, bartlett, blackman,
|
||||
kaiser, trapezoid, trapz, i0, meshgrid, delete, insert, append,
|
||||
interp, quantile
|
||||
)
|
||||
from .lib._twodim_base_impl import (
|
||||
diag, diagflat, eye, fliplr, flipud, tri, triu, tril, vander,
|
||||
histogram2d, mask_indices, tril_indices, tril_indices_from,
|
||||
triu_indices, triu_indices_from
|
||||
)
|
||||
from .lib._shape_base_impl import (
|
||||
apply_over_axes, apply_along_axis, array_split, column_stack, dsplit,
|
||||
dstack, expand_dims, hsplit, kron, put_along_axis, row_stack, split,
|
||||
take_along_axis, tile, vsplit
|
||||
)
|
||||
from .lib._type_check_impl import (
|
||||
iscomplexobj, isrealobj, imag, iscomplex, isreal, nan_to_num, real,
|
||||
real_if_close, typename, mintypecode, common_type
|
||||
)
|
||||
from .lib._arraysetops_impl import (
|
||||
ediff1d, in1d, intersect1d, isin, setdiff1d, setxor1d, union1d,
|
||||
unique, unique_all, unique_counts, unique_inverse, unique_values
|
||||
)
|
||||
from .lib._ufunclike_impl import fix, isneginf, isposinf
|
||||
from .lib._arraypad_impl import pad
|
||||
from .lib._utils_impl import (
|
||||
show_runtime, get_include, info
|
||||
)
|
||||
from .lib._stride_tricks_impl import (
|
||||
broadcast_arrays, broadcast_shapes, broadcast_to
|
||||
)
|
||||
from .lib._polynomial_impl import (
|
||||
poly, polyint, polyder, polyadd, polysub, polymul, polydiv, polyval,
|
||||
polyfit, poly1d, roots
|
||||
)
|
||||
from .lib._npyio_impl import (
|
||||
savetxt, loadtxt, genfromtxt, load, save, savez, packbits,
|
||||
savez_compressed, unpackbits, fromregex
|
||||
)
|
||||
from .lib._index_tricks_impl import (
|
||||
diag_indices_from, diag_indices, fill_diagonal, ndindex, ndenumerate,
|
||||
ix_, c_, r_, s_, ogrid, mgrid, unravel_index, ravel_multi_index,
|
||||
index_exp
|
||||
)
|
||||
from . import matrixlib as _mat
|
||||
from .matrixlib import (
|
||||
asmatrix, bmat, matrix
|
||||
)
|
||||
|
||||
# public submodules are imported lazily, therefore are accessible from
|
||||
# __getattr__. Note that `distutils` (deprecated) and `array_api`
|
||||
# (experimental label) are not added here, because `from numpy import *`
|
||||
# must not raise any warnings - that's too disruptive.
|
||||
__numpy_submodules__ = {
|
||||
"linalg", "fft", "dtypes", "random", "polynomial", "ma",
|
||||
"exceptions", "lib", "ctypeslib", "testing", "typing",
|
||||
"f2py", "test", "rec", "char", "core", "strings",
|
||||
}
|
||||
|
||||
# We build warning messages for former attributes
|
||||
_msg = (
|
||||
"module 'numpy' has no attribute '{n}'.\n"
|
||||
"`np.{n}` was a deprecated alias for the builtin `{n}`. "
|
||||
"To avoid this error in existing code, use `{n}` by itself. "
|
||||
"Doing this will not modify any behavior and is safe. {extended_msg}\n"
|
||||
"The aliases was originally deprecated in NumPy 1.20; for more "
|
||||
"details and guidance see the original release note at:\n"
|
||||
" https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations")
|
||||
|
||||
_specific_msg = (
|
||||
"If you specifically wanted the numpy scalar type, use `np.{}` here.")
|
||||
|
||||
_int_extended_msg = (
|
||||
"When replacing `np.{}`, you may wish to use e.g. `np.int64` "
|
||||
"or `np.int32` to specify the precision. If you wish to review "
|
||||
"your current use, check the release note link for "
|
||||
"additional information.")
|
||||
|
||||
_type_info = [
|
||||
("object", ""), # The NumPy scalar only exists by name.
|
||||
("float", _specific_msg.format("float64")),
|
||||
("complex", _specific_msg.format("complex128")),
|
||||
("str", _specific_msg.format("str_")),
|
||||
("int", _int_extended_msg.format("int"))]
|
||||
|
||||
__former_attrs__ = {
|
||||
n: _msg.format(n=n, extended_msg=extended_msg)
|
||||
for n, extended_msg in _type_info
|
||||
}
|
||||
|
||||
|
||||
# Some of these could be defined right away, but most were aliases to
|
||||
# the Python objects and only removed in NumPy 1.24. Defining them should
|
||||
# probably wait for NumPy 1.26 or 2.0.
|
||||
# When defined, these should possibly not be added to `__all__` to avoid
|
||||
# import with `from numpy import *`.
|
||||
__future_scalars__ = {"str", "bytes", "object"}
|
||||
|
||||
__array_api_version__ = "2022.12"
|
||||
|
||||
# now that numpy core module is imported, can initialize limits
|
||||
_core.getlimits._register_known_types()
|
||||
|
||||
__all__ = list(
|
||||
__numpy_submodules__ |
|
||||
set(_core.__all__) |
|
||||
set(_mat.__all__) |
|
||||
set(lib._histograms_impl.__all__) |
|
||||
set(lib._nanfunctions_impl.__all__) |
|
||||
set(lib._function_base_impl.__all__) |
|
||||
set(lib._twodim_base_impl.__all__) |
|
||||
set(lib._shape_base_impl.__all__) |
|
||||
set(lib._type_check_impl.__all__) |
|
||||
set(lib._arraysetops_impl.__all__) |
|
||||
set(lib._ufunclike_impl.__all__) |
|
||||
set(lib._arraypad_impl.__all__) |
|
||||
set(lib._utils_impl.__all__) |
|
||||
set(lib._stride_tricks_impl.__all__) |
|
||||
set(lib._polynomial_impl.__all__) |
|
||||
set(lib._npyio_impl.__all__) |
|
||||
set(lib._index_tricks_impl.__all__) |
|
||||
{"emath", "show_config", "__version__"}
|
||||
)
|
||||
|
||||
# Filter out Cython harmless warnings
|
||||
warnings.filterwarnings("ignore", message="numpy.dtype size changed")
|
||||
warnings.filterwarnings("ignore", message="numpy.ufunc size changed")
|
||||
warnings.filterwarnings("ignore", message="numpy.ndarray size changed")
|
||||
|
||||
def __getattr__(attr):
|
||||
# Warn for expired attributes
|
||||
import warnings
|
||||
|
||||
if attr == "linalg":
|
||||
import numpy.linalg as linalg
|
||||
return linalg
|
||||
elif attr == "fft":
|
||||
import numpy.fft as fft
|
||||
return fft
|
||||
elif attr == "dtypes":
|
||||
import numpy.dtypes as dtypes
|
||||
return dtypes
|
||||
elif attr == "random":
|
||||
import numpy.random as random
|
||||
return random
|
||||
elif attr == "polynomial":
|
||||
import numpy.polynomial as polynomial
|
||||
return polynomial
|
||||
elif attr == "ma":
|
||||
import numpy.ma as ma
|
||||
return ma
|
||||
elif attr == "ctypeslib":
|
||||
import numpy.ctypeslib as ctypeslib
|
||||
return ctypeslib
|
||||
elif attr == "exceptions":
|
||||
import numpy.exceptions as exceptions
|
||||
return exceptions
|
||||
elif attr == "testing":
|
||||
import numpy.testing as testing
|
||||
return testing
|
||||
elif attr == "matlib":
|
||||
import numpy.matlib as matlib
|
||||
return matlib
|
||||
elif attr == "f2py":
|
||||
import numpy.f2py as f2py
|
||||
return f2py
|
||||
elif attr == "typing":
|
||||
import numpy.typing as typing
|
||||
return typing
|
||||
elif attr == "rec":
|
||||
import numpy.rec as rec
|
||||
return rec
|
||||
elif attr == "char":
|
||||
import numpy.char as char
|
||||
return char
|
||||
elif attr == "array_api":
|
||||
raise AttributeError("`numpy.array_api` is not available from "
|
||||
"numpy 2.0 onwards")
|
||||
elif attr == "core":
|
||||
import numpy.core as core
|
||||
return core
|
||||
elif attr == "strings":
|
||||
import numpy.strings as strings
|
||||
return strings
|
||||
elif attr == "distutils":
|
||||
if 'distutils' in __numpy_submodules__:
|
||||
import numpy.distutils as distutils
|
||||
return distutils
|
||||
else:
|
||||
raise AttributeError("`numpy.distutils` is not available from "
|
||||
"Python 3.12 onwards")
|
||||
|
||||
if attr in __future_scalars__:
|
||||
# And future warnings for those that will change, but also give
|
||||
# the AttributeError
|
||||
warnings.warn(
|
||||
f"In the future `np.{attr}` will be defined as the "
|
||||
"corresponding NumPy scalar.", FutureWarning, stacklevel=2)
|
||||
|
||||
if attr in __former_attrs__:
|
||||
raise AttributeError(__former_attrs__[attr])
|
||||
|
||||
if attr in __expired_attributes__:
|
||||
raise AttributeError(
|
||||
f"`np.{attr}` was removed in the NumPy 2.0 release. "
|
||||
f"{__expired_attributes__[attr]}"
|
||||
)
|
||||
|
||||
if attr == "chararray":
|
||||
warnings.warn(
|
||||
"`np.chararray` is deprecated and will be removed from "
|
||||
"the main namespace in the future. Use an array with a string "
|
||||
"or bytes dtype instead.", DeprecationWarning, stacklevel=2)
|
||||
import numpy.char as char
|
||||
return char.chararray
|
||||
|
||||
raise AttributeError("module {!r} has no attribute "
|
||||
"{!r}".format(__name__, attr))
|
||||
|
||||
def __dir__():
|
||||
public_symbols = (
|
||||
globals().keys() | __numpy_submodules__
|
||||
)
|
||||
public_symbols -= {
|
||||
"matrixlib", "matlib", "tests", "conftest", "version",
|
||||
"compat", "distutils", "array_api"
|
||||
}
|
||||
return list(public_symbols)
|
||||
|
||||
# Pytest testing
|
||||
from numpy._pytesttester import PytestTester
|
||||
test = PytestTester(__name__)
|
||||
del PytestTester
|
||||
|
||||
def _sanity_check():
|
||||
"""
|
||||
Quick sanity checks for common bugs caused by environment.
|
||||
There are some cases e.g. with wrong BLAS ABI that cause wrong
|
||||
results under specific runtime conditions that are not necessarily
|
||||
achieved during test suite runs, and it is useful to catch those early.
|
||||
|
||||
See https://github.com/numpy/numpy/issues/8577 and other
|
||||
similar bug reports.
|
||||
|
||||
"""
|
||||
try:
|
||||
x = ones(2, dtype=float32)
|
||||
if not abs(x.dot(x) - float32(2.0)) < 1e-5:
|
||||
raise AssertionError()
|
||||
except AssertionError:
|
||||
msg = ("The current Numpy installation ({!r}) fails to "
|
||||
"pass simple sanity checks. This can be caused for example "
|
||||
"by incorrect BLAS library being linked in, or by mixing "
|
||||
"package managers (pip, conda, apt, ...). Search closed "
|
||||
"numpy issues for similar problems.")
|
||||
raise RuntimeError(msg.format(__file__)) from None
|
||||
|
||||
_sanity_check()
|
||||
del _sanity_check
|
||||
|
||||
def _mac_os_check():
|
||||
"""
|
||||
Quick Sanity check for Mac OS look for accelerate build bugs.
|
||||
Testing numpy polyfit calls init_dgelsd(LAPACK)
|
||||
"""
|
||||
try:
|
||||
c = array([3., 2., 1.])
|
||||
x = linspace(0, 2, 5)
|
||||
y = polyval(c, x)
|
||||
_ = polyfit(x, y, 2, cov=True)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
if sys.platform == "darwin":
|
||||
from . import exceptions
|
||||
with warnings.catch_warnings(record=True) as w:
|
||||
_mac_os_check()
|
||||
# Throw runtime error, if the test failed Check for warning and error_message
|
||||
if len(w) > 0:
|
||||
for _wn in w:
|
||||
if _wn.category is exceptions.RankWarning:
|
||||
# Ignore other warnings, they may not be relevant (see gh-25433).
|
||||
error_message = f"{_wn.category.__name__}: {str(_wn.message)}"
|
||||
msg = (
|
||||
"Polyfit sanity test emitted a warning, most likely due "
|
||||
"to using a buggy Accelerate backend."
|
||||
"\nIf you compiled yourself, more information is available at:"
|
||||
"\nhttps://numpy.org/devdocs/building/index.html"
|
||||
"\nOtherwise report this to the vendor "
|
||||
"that provided NumPy.\n\n{}\n".format(error_message))
|
||||
raise RuntimeError(msg)
|
||||
del _wn
|
||||
del w
|
||||
del _mac_os_check
|
||||
|
||||
def hugepage_setup():
|
||||
"""
|
||||
We usually use madvise hugepages support, but on some old kernels it
|
||||
is slow and thus better avoided. Specifically kernel version 4.6
|
||||
had a bug fix which probably fixed this:
|
||||
https://github.com/torvalds/linux/commit/7cf91a98e607c2f935dbcc177d70011e95b8faff
|
||||
"""
|
||||
use_hugepage = os.environ.get("NUMPY_MADVISE_HUGEPAGE", None)
|
||||
if sys.platform == "linux" and use_hugepage is None:
|
||||
# If there is an issue with parsing the kernel version,
|
||||
# set use_hugepage to 0. Usage of LooseVersion will handle
|
||||
# the kernel version parsing better, but avoided since it
|
||||
# will increase the import time.
|
||||
# See: #16679 for related discussion.
|
||||
try:
|
||||
use_hugepage = 1
|
||||
kernel_version = os.uname().release.split(".")[:2]
|
||||
kernel_version = tuple(int(v) for v in kernel_version)
|
||||
if kernel_version < (4, 6):
|
||||
use_hugepage = 0
|
||||
except ValueError:
|
||||
use_hugepage = 0
|
||||
elif use_hugepage is None:
|
||||
# This is not Linux, so it should not matter, just enable anyway
|
||||
use_hugepage = 1
|
||||
else:
|
||||
use_hugepage = int(use_hugepage)
|
||||
return use_hugepage
|
||||
|
||||
# Note that this will currently only make a difference on Linux
|
||||
_core.multiarray._set_madvise_hugepage(hugepage_setup())
|
||||
del hugepage_setup
|
||||
|
||||
# Give a warning if NumPy is reloaded or imported on a sub-interpreter
|
||||
# We do this from python, since the C-module may not be reloaded and
|
||||
# it is tidier organized.
|
||||
_core.multiarray._multiarray_umath._reload_guard()
|
||||
|
||||
# TODO: Remove the environment variable entirely now that it is "weak"
|
||||
_core._set_promotion_state(
|
||||
os.environ.get("NPY_PROMOTION_STATE", "weak"))
|
||||
|
||||
# Tell PyInstaller where to find hook-numpy.py
|
||||
def _pyinstaller_hooks_dir():
|
||||
from pathlib import Path
|
||||
return [str(Path(__file__).with_name("_pyinstaller").resolve())]
|
||||
|
||||
|
||||
# Remove symbols imported for internal use
|
||||
del os, sys, warnings
|
||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,39 @@
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
from .version import __version__
|
||||
from .lib._utils_impl import get_include
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
"--version",
|
||||
action="version",
|
||||
version=__version__,
|
||||
help="Print the version and exit.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--cflags",
|
||||
action="store_true",
|
||||
help="Compile flag needed when using the NumPy headers.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--pkgconfigdir",
|
||||
action="store_true",
|
||||
help=("Print the pkgconfig directory in which `numpy.pc` is stored "
|
||||
"(useful for setting $PKG_CONFIG_PATH)."),
|
||||
)
|
||||
args = parser.parse_args()
|
||||
if not sys.argv[1:]:
|
||||
parser.print_help()
|
||||
if args.cflags:
|
||||
print("-I" + get_include())
|
||||
if args.pkgconfigdir:
|
||||
_path = Path(get_include()) / '..' / 'lib' / 'pkgconfig'
|
||||
print(_path.resolve())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@ -0,0 +1,180 @@
|
||||
"""
|
||||
Contains the core of NumPy: ndarray, ufuncs, dtypes, etc.
|
||||
|
||||
Please note that this module is private. All functions and objects
|
||||
are available in the main ``numpy`` namespace - use that instead.
|
||||
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from numpy.version import version as __version__
|
||||
|
||||
|
||||
# disables OpenBLAS affinity setting of the main thread that limits
|
||||
# python threads or processes to one core
|
||||
env_added = []
|
||||
for envkey in ['OPENBLAS_MAIN_FREE', 'GOTOBLAS_MAIN_FREE']:
|
||||
if envkey not in os.environ:
|
||||
os.environ[envkey] = '1'
|
||||
env_added.append(envkey)
|
||||
|
||||
try:
|
||||
from . import multiarray
|
||||
except ImportError as exc:
|
||||
import sys
|
||||
msg = """
|
||||
|
||||
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
|
||||
|
||||
Importing the numpy C-extensions failed. This error can happen for
|
||||
many reasons, often due to issues with your setup or how NumPy was
|
||||
installed.
|
||||
|
||||
We have compiled some common reasons and troubleshooting tips at:
|
||||
|
||||
https://numpy.org/devdocs/user/troubleshooting-importerror.html
|
||||
|
||||
Please note and check the following:
|
||||
|
||||
* The Python version is: Python%d.%d from "%s"
|
||||
* The NumPy version is: "%s"
|
||||
|
||||
and make sure that they are the versions you expect.
|
||||
Please carefully study the documentation linked above for further help.
|
||||
|
||||
Original error was: %s
|
||||
""" % (sys.version_info[0], sys.version_info[1], sys.executable,
|
||||
__version__, exc)
|
||||
raise ImportError(msg)
|
||||
finally:
|
||||
for envkey in env_added:
|
||||
del os.environ[envkey]
|
||||
del envkey
|
||||
del env_added
|
||||
del os
|
||||
|
||||
from . import umath
|
||||
|
||||
# Check that multiarray,umath are pure python modules wrapping
|
||||
# _multiarray_umath and not either of the old c-extension modules
|
||||
if not (hasattr(multiarray, '_multiarray_umath') and
|
||||
hasattr(umath, '_multiarray_umath')):
|
||||
import sys
|
||||
path = sys.modules['numpy'].__path__
|
||||
msg = ("Something is wrong with the numpy installation. "
|
||||
"While importing we detected an older version of "
|
||||
"numpy in {}. One method of fixing this is to repeatedly uninstall "
|
||||
"numpy until none is found, then reinstall this version.")
|
||||
raise ImportError(msg.format(path))
|
||||
|
||||
from . import numerictypes as nt
|
||||
from .numerictypes import sctypes, sctypeDict
|
||||
multiarray.set_typeDict(nt.sctypeDict)
|
||||
from . import numeric
|
||||
from .numeric import *
|
||||
from . import fromnumeric
|
||||
from .fromnumeric import *
|
||||
from .records import record, recarray
|
||||
# Note: module name memmap is overwritten by a class with same name
|
||||
from .memmap import *
|
||||
from . import function_base
|
||||
from .function_base import *
|
||||
from . import _machar
|
||||
from . import getlimits
|
||||
from .getlimits import *
|
||||
from . import shape_base
|
||||
from .shape_base import *
|
||||
from . import einsumfunc
|
||||
from .einsumfunc import *
|
||||
del nt
|
||||
|
||||
from .numeric import absolute as abs
|
||||
|
||||
# do this after everything else, to minimize the chance of this misleadingly
|
||||
# appearing in an import-time traceback
|
||||
from . import _add_newdocs
|
||||
from . import _add_newdocs_scalars
|
||||
# add these for module-freeze analysis (like PyInstaller)
|
||||
from . import _dtype_ctypes
|
||||
from . import _internal
|
||||
from . import _dtype
|
||||
from . import _methods
|
||||
|
||||
acos = numeric.arccos
|
||||
acosh = numeric.arccosh
|
||||
asin = numeric.arcsin
|
||||
asinh = numeric.arcsinh
|
||||
atan = numeric.arctan
|
||||
atanh = numeric.arctanh
|
||||
atan2 = numeric.arctan2
|
||||
concat = numeric.concatenate
|
||||
bitwise_left_shift = numeric.left_shift
|
||||
bitwise_invert = numeric.invert
|
||||
bitwise_right_shift = numeric.right_shift
|
||||
permute_dims = numeric.transpose
|
||||
pow = numeric.power
|
||||
|
||||
__all__ = [
|
||||
"abs", "acos", "acosh", "asin", "asinh", "atan", "atanh", "atan2",
|
||||
"bitwise_invert", "bitwise_left_shift", "bitwise_right_shift", "concat",
|
||||
"pow", "permute_dims", "memmap", "sctypeDict", "record", "recarray"
|
||||
]
|
||||
__all__ += numeric.__all__
|
||||
__all__ += function_base.__all__
|
||||
__all__ += getlimits.__all__
|
||||
__all__ += shape_base.__all__
|
||||
__all__ += einsumfunc.__all__
|
||||
|
||||
|
||||
def _ufunc_reduce(func):
|
||||
# Report the `__name__`. pickle will try to find the module. Note that
|
||||
# pickle supports for this `__name__` to be a `__qualname__`. It may
|
||||
# make sense to add a `__qualname__` to ufuncs, to allow this more
|
||||
# explicitly (Numba has ufuncs as attributes).
|
||||
# See also: https://github.com/dask/distributed/issues/3450
|
||||
return func.__name__
|
||||
|
||||
|
||||
def _DType_reconstruct(scalar_type):
|
||||
# This is a work-around to pickle type(np.dtype(np.float64)), etc.
|
||||
# and it should eventually be replaced with a better solution, e.g. when
|
||||
# DTypes become HeapTypes.
|
||||
return type(dtype(scalar_type))
|
||||
|
||||
|
||||
def _DType_reduce(DType):
|
||||
# As types/classes, most DTypes can simply be pickled by their name:
|
||||
if not DType._legacy or DType.__module__ == "numpy.dtypes":
|
||||
return DType.__name__
|
||||
|
||||
# However, user defined legacy dtypes (like rational) do not end up in
|
||||
# `numpy.dtypes` as module and do not have a public class at all.
|
||||
# For these, we pickle them by reconstructing them from the scalar type:
|
||||
scalar_type = DType.type
|
||||
return _DType_reconstruct, (scalar_type,)
|
||||
|
||||
|
||||
def __getattr__(name):
|
||||
# Deprecated 2022-11-22, NumPy 1.25.
|
||||
if name == "MachAr":
|
||||
import warnings
|
||||
warnings.warn(
|
||||
"The `np._core.MachAr` is considered private API (NumPy 1.24)",
|
||||
DeprecationWarning, stacklevel=2,
|
||||
)
|
||||
return _machar.MachAr
|
||||
raise AttributeError(f"Module {__name__!r} has no attribute {name!r}")
|
||||
|
||||
|
||||
import copyreg
|
||||
|
||||
copyreg.pickle(ufunc, _ufunc_reduce)
|
||||
copyreg.pickle(type(dtype), _DType_reduce, _DType_reconstruct)
|
||||
|
||||
# Unclutter namespace (must keep _*_reconstruct for unpickling)
|
||||
del copyreg, _ufunc_reduce, _DType_reduce
|
||||
|
||||
from numpy._pytesttester import PytestTester
|
||||
test = PytestTester(__name__)
|
||||
del PytestTester
|
||||
@ -0,0 +1,2 @@
|
||||
# NOTE: The `np._core` namespace is deliberately kept empty due to it
|
||||
# being private
|
||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,388 @@
|
||||
"""
|
||||
This file is separate from ``_add_newdocs.py`` so that it can be mocked out by
|
||||
our sphinx ``conf.py`` during doc builds, where we want to avoid showing
|
||||
platform-dependent information.
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
from numpy._core import dtype
|
||||
from numpy._core import numerictypes as _numerictypes
|
||||
from numpy._core.function_base import add_newdoc
|
||||
|
||||
##############################################################################
|
||||
#
|
||||
# Documentation for concrete scalar classes
|
||||
#
|
||||
##############################################################################
|
||||
|
||||
def numeric_type_aliases(aliases):
|
||||
def type_aliases_gen():
|
||||
for alias, doc in aliases:
|
||||
try:
|
||||
alias_type = getattr(_numerictypes, alias)
|
||||
except AttributeError:
|
||||
# The set of aliases that actually exist varies between platforms
|
||||
pass
|
||||
else:
|
||||
yield (alias_type, alias, doc)
|
||||
return list(type_aliases_gen())
|
||||
|
||||
|
||||
possible_aliases = numeric_type_aliases([
|
||||
('int8', '8-bit signed integer (``-128`` to ``127``)'),
|
||||
('int16', '16-bit signed integer (``-32_768`` to ``32_767``)'),
|
||||
('int32', '32-bit signed integer (``-2_147_483_648`` to ``2_147_483_647``)'),
|
||||
('int64', '64-bit signed integer (``-9_223_372_036_854_775_808`` to ``9_223_372_036_854_775_807``)'),
|
||||
('intp', 'Signed integer large enough to fit pointer, compatible with C ``intptr_t``'),
|
||||
('uint8', '8-bit unsigned integer (``0`` to ``255``)'),
|
||||
('uint16', '16-bit unsigned integer (``0`` to ``65_535``)'),
|
||||
('uint32', '32-bit unsigned integer (``0`` to ``4_294_967_295``)'),
|
||||
('uint64', '64-bit unsigned integer (``0`` to ``18_446_744_073_709_551_615``)'),
|
||||
('uintp', 'Unsigned integer large enough to fit pointer, compatible with C ``uintptr_t``'),
|
||||
('float16', '16-bit-precision floating-point number type: sign bit, 5 bits exponent, 10 bits mantissa'),
|
||||
('float32', '32-bit-precision floating-point number type: sign bit, 8 bits exponent, 23 bits mantissa'),
|
||||
('float64', '64-bit precision floating-point number type: sign bit, 11 bits exponent, 52 bits mantissa'),
|
||||
('float96', '96-bit extended-precision floating-point number type'),
|
||||
('float128', '128-bit extended-precision floating-point number type'),
|
||||
('complex64', 'Complex number type composed of 2 32-bit-precision floating-point numbers'),
|
||||
('complex128', 'Complex number type composed of 2 64-bit-precision floating-point numbers'),
|
||||
('complex192', 'Complex number type composed of 2 96-bit extended-precision floating-point numbers'),
|
||||
('complex256', 'Complex number type composed of 2 128-bit extended-precision floating-point numbers'),
|
||||
])
|
||||
|
||||
|
||||
def _get_platform_and_machine():
|
||||
try:
|
||||
system, _, _, _, machine = os.uname()
|
||||
except AttributeError:
|
||||
system = sys.platform
|
||||
if system == 'win32':
|
||||
machine = os.environ.get('PROCESSOR_ARCHITEW6432', '') \
|
||||
or os.environ.get('PROCESSOR_ARCHITECTURE', '')
|
||||
else:
|
||||
machine = 'unknown'
|
||||
return system, machine
|
||||
|
||||
|
||||
_system, _machine = _get_platform_and_machine()
|
||||
_doc_alias_string = f":Alias on this platform ({_system} {_machine}):"
|
||||
|
||||
|
||||
def add_newdoc_for_scalar_type(obj, fixed_aliases, doc):
|
||||
# note: `:field: value` is rST syntax which renders as field lists.
|
||||
o = getattr(_numerictypes, obj)
|
||||
|
||||
character_code = dtype(o).char
|
||||
canonical_name_doc = "" if obj == o.__name__ else \
|
||||
f":Canonical name: `numpy.{obj}`\n "
|
||||
if fixed_aliases:
|
||||
alias_doc = ''.join(f":Alias: `numpy.{alias}`\n "
|
||||
for alias in fixed_aliases)
|
||||
else:
|
||||
alias_doc = ''
|
||||
alias_doc += ''.join(f"{_doc_alias_string} `numpy.{alias}`: {doc}.\n "
|
||||
for (alias_type, alias, doc) in possible_aliases if alias_type is o)
|
||||
|
||||
docstring = f"""
|
||||
{doc.strip()}
|
||||
|
||||
:Character code: ``'{character_code}'``
|
||||
{canonical_name_doc}{alias_doc}
|
||||
"""
|
||||
|
||||
add_newdoc('numpy._core.numerictypes', obj, docstring)
|
||||
|
||||
|
||||
_bool_docstring = (
|
||||
"""
|
||||
Boolean type (True or False), stored as a byte.
|
||||
|
||||
.. warning::
|
||||
|
||||
The :class:`bool` type is not a subclass of the :class:`int_` type
|
||||
(the :class:`bool` is not even a number type). This is different
|
||||
than Python's default implementation of :class:`bool` as a
|
||||
sub-class of :class:`int`.
|
||||
"""
|
||||
)
|
||||
|
||||
add_newdoc_for_scalar_type('bool', [], _bool_docstring)
|
||||
|
||||
add_newdoc_for_scalar_type('bool_', [], _bool_docstring)
|
||||
|
||||
add_newdoc_for_scalar_type('byte', [],
|
||||
"""
|
||||
Signed integer type, compatible with C ``char``.
|
||||
""")
|
||||
|
||||
add_newdoc_for_scalar_type('short', [],
|
||||
"""
|
||||
Signed integer type, compatible with C ``short``.
|
||||
""")
|
||||
|
||||
add_newdoc_for_scalar_type('intc', [],
|
||||
"""
|
||||
Signed integer type, compatible with C ``int``.
|
||||
""")
|
||||
|
||||
# TODO: These docs probably need an if to highlight the default rather than
|
||||
# the C-types (and be correct).
|
||||
add_newdoc_for_scalar_type('int_', [],
|
||||
"""
|
||||
Default signed integer type, 64bit on 64bit systems and 32bit on 32bit
|
||||
systems.
|
||||
""")
|
||||
|
||||
add_newdoc_for_scalar_type('longlong', [],
|
||||
"""
|
||||
Signed integer type, compatible with C ``long long``.
|
||||
""")
|
||||
|
||||
add_newdoc_for_scalar_type('ubyte', [],
|
||||
"""
|
||||
Unsigned integer type, compatible with C ``unsigned char``.
|
||||
""")
|
||||
|
||||
add_newdoc_for_scalar_type('ushort', [],
|
||||
"""
|
||||
Unsigned integer type, compatible with C ``unsigned short``.
|
||||
""")
|
||||
|
||||
add_newdoc_for_scalar_type('uintc', [],
|
||||
"""
|
||||
Unsigned integer type, compatible with C ``unsigned int``.
|
||||
""")
|
||||
|
||||
add_newdoc_for_scalar_type('uint', [],
|
||||
"""
|
||||
Unsigned signed integer type, 64bit on 64bit systems and 32bit on 32bit
|
||||
systems.
|
||||
""")
|
||||
|
||||
add_newdoc_for_scalar_type('ulonglong', [],
|
||||
"""
|
||||
Signed integer type, compatible with C ``unsigned long long``.
|
||||
""")
|
||||
|
||||
add_newdoc_for_scalar_type('half', [],
|
||||
"""
|
||||
Half-precision floating-point number type.
|
||||
""")
|
||||
|
||||
add_newdoc_for_scalar_type('single', [],
|
||||
"""
|
||||
Single-precision floating-point number type, compatible with C ``float``.
|
||||
""")
|
||||
|
||||
add_newdoc_for_scalar_type('double', [],
|
||||
"""
|
||||
Double-precision floating-point number type, compatible with Python
|
||||
:class:`float` and C ``double``.
|
||||
""")
|
||||
|
||||
add_newdoc_for_scalar_type('longdouble', [],
|
||||
"""
|
||||
Extended-precision floating-point number type, compatible with C
|
||||
``long double`` but not necessarily with IEEE 754 quadruple-precision.
|
||||
""")
|
||||
|
||||
add_newdoc_for_scalar_type('csingle', [],
|
||||
"""
|
||||
Complex number type composed of two single-precision floating-point
|
||||
numbers.
|
||||
""")
|
||||
|
||||
add_newdoc_for_scalar_type('cdouble', [],
|
||||
"""
|
||||
Complex number type composed of two double-precision floating-point
|
||||
numbers, compatible with Python :class:`complex`.
|
||||
""")
|
||||
|
||||
add_newdoc_for_scalar_type('clongdouble', [],
|
||||
"""
|
||||
Complex number type composed of two extended-precision floating-point
|
||||
numbers.
|
||||
""")
|
||||
|
||||
add_newdoc_for_scalar_type('object_', [],
|
||||
"""
|
||||
Any Python object.
|
||||
""")
|
||||
|
||||
add_newdoc_for_scalar_type('str_', [],
|
||||
r"""
|
||||
A unicode string.
|
||||
|
||||
This type strips trailing null codepoints.
|
||||
|
||||
>>> s = np.str_("abc\x00")
|
||||
>>> s
|
||||
'abc'
|
||||
|
||||
Unlike the builtin :class:`str`, this supports the
|
||||
:ref:`python:bufferobjects`, exposing its contents as UCS4:
|
||||
|
||||
>>> m = memoryview(np.str_("abc"))
|
||||
>>> m.format
|
||||
'3w'
|
||||
>>> m.tobytes()
|
||||
b'a\x00\x00\x00b\x00\x00\x00c\x00\x00\x00'
|
||||
""")
|
||||
|
||||
add_newdoc_for_scalar_type('bytes_', [],
|
||||
r"""
|
||||
A byte string.
|
||||
|
||||
When used in arrays, this type strips trailing null bytes.
|
||||
""")
|
||||
|
||||
add_newdoc_for_scalar_type('void', [],
|
||||
r"""
|
||||
np.void(length_or_data, /, dtype=None)
|
||||
|
||||
Create a new structured or unstructured void scalar.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
length_or_data : int, array-like, bytes-like, object
|
||||
One of multiple meanings (see notes). The length or
|
||||
bytes data of an unstructured void. Or alternatively,
|
||||
the data to be stored in the new scalar when `dtype`
|
||||
is provided.
|
||||
This can be an array-like, in which case an array may
|
||||
be returned.
|
||||
dtype : dtype, optional
|
||||
If provided the dtype of the new scalar. This dtype must
|
||||
be "void" dtype (i.e. a structured or unstructured void,
|
||||
see also :ref:`defining-structured-types`).
|
||||
|
||||
.. versionadded:: 1.24
|
||||
|
||||
Notes
|
||||
-----
|
||||
For historical reasons and because void scalars can represent both
|
||||
arbitrary byte data and structured dtypes, the void constructor
|
||||
has three calling conventions:
|
||||
|
||||
1. ``np.void(5)`` creates a ``dtype="V5"`` scalar filled with five
|
||||
``\0`` bytes. The 5 can be a Python or NumPy integer.
|
||||
2. ``np.void(b"bytes-like")`` creates a void scalar from the byte string.
|
||||
The dtype itemsize will match the byte string length, here ``"V10"``.
|
||||
3. When a ``dtype=`` is passed the call is roughly the same as an
|
||||
array creation. However, a void scalar rather than array is returned.
|
||||
|
||||
Please see the examples which show all three different conventions.
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> np.void(5)
|
||||
np.void(b'\x00\x00\x00\x00\x00')
|
||||
>>> np.void(b'abcd')
|
||||
np.void(b'\x61\x62\x63\x64')
|
||||
>>> np.void((3.2, b'eggs'), dtype="d,S5")
|
||||
np.void((3.2, b'eggs'), dtype=[('f0', '<f8'), ('f1', 'S5')])
|
||||
>>> np.void(3, dtype=[('x', np.int8), ('y', np.int8)])
|
||||
np.void((3, 3), dtype=[('x', 'i1'), ('y', 'i1')])
|
||||
|
||||
""")
|
||||
|
||||
add_newdoc_for_scalar_type('datetime64', [],
|
||||
"""
|
||||
If created from a 64-bit integer, it represents an offset from
|
||||
``1970-01-01T00:00:00``.
|
||||
If created from string, the string can be in ISO 8601 date
|
||||
or datetime format.
|
||||
|
||||
When parsing a string to create a datetime object, if the string contains
|
||||
a trailing timezone (A 'Z' or a timezone offset), the timezone will be
|
||||
dropped and a User Warning is given.
|
||||
|
||||
Datetime64 objects should be considered to be UTC and therefore have an
|
||||
offset of +0000.
|
||||
|
||||
>>> np.datetime64(10, 'Y')
|
||||
numpy.datetime64('1980')
|
||||
>>> np.datetime64('1980', 'Y')
|
||||
numpy.datetime64('1980')
|
||||
>>> np.datetime64(10, 'D')
|
||||
numpy.datetime64('1970-01-11')
|
||||
|
||||
See :ref:`arrays.datetime` for more information.
|
||||
""")
|
||||
|
||||
add_newdoc_for_scalar_type('timedelta64', [],
|
||||
"""
|
||||
A timedelta stored as a 64-bit integer.
|
||||
|
||||
See :ref:`arrays.datetime` for more information.
|
||||
""")
|
||||
|
||||
add_newdoc('numpy._core.numerictypes', "integer", ('is_integer',
|
||||
"""
|
||||
integer.is_integer() -> bool
|
||||
|
||||
Return ``True`` if the number is finite with integral value.
|
||||
|
||||
.. versionadded:: 1.22
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> np.int64(-2).is_integer()
|
||||
True
|
||||
>>> np.uint32(5).is_integer()
|
||||
True
|
||||
"""))
|
||||
|
||||
# TODO: work out how to put this on the base class, np.floating
|
||||
for float_name in ('half', 'single', 'double', 'longdouble'):
|
||||
add_newdoc('numpy._core.numerictypes', float_name, ('as_integer_ratio',
|
||||
"""
|
||||
{ftype}.as_integer_ratio() -> (int, int)
|
||||
|
||||
Return a pair of integers, whose ratio is exactly equal to the original
|
||||
floating point number, and with a positive denominator.
|
||||
Raise `OverflowError` on infinities and a `ValueError` on NaNs.
|
||||
|
||||
>>> np.{ftype}(10.0).as_integer_ratio()
|
||||
(10, 1)
|
||||
>>> np.{ftype}(0.0).as_integer_ratio()
|
||||
(0, 1)
|
||||
>>> np.{ftype}(-.25).as_integer_ratio()
|
||||
(-1, 4)
|
||||
""".format(ftype=float_name)))
|
||||
|
||||
add_newdoc('numpy._core.numerictypes', float_name, ('is_integer',
|
||||
f"""
|
||||
{float_name}.is_integer() -> bool
|
||||
|
||||
Return ``True`` if the floating point number is finite with integral
|
||||
value, and ``False`` otherwise.
|
||||
|
||||
.. versionadded:: 1.22
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> np.{float_name}(-2.0).is_integer()
|
||||
True
|
||||
>>> np.{float_name}(3.2).is_integer()
|
||||
False
|
||||
"""))
|
||||
|
||||
for int_name in ('int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32',
|
||||
'int64', 'uint64', 'int64', 'uint64', 'int64', 'uint64'):
|
||||
# Add negative examples for signed cases by checking typecode
|
||||
add_newdoc('numpy._core.numerictypes', int_name, ('bit_count',
|
||||
f"""
|
||||
{int_name}.bit_count() -> int
|
||||
|
||||
Computes the number of 1-bits in the absolute value of the input.
|
||||
Analogous to the builtin `int.bit_count` or ``popcount`` in C++.
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> np.{int_name}(127).bit_count()
|
||||
7""" +
|
||||
(f"""
|
||||
>>> np.{int_name}(-127).bit_count()
|
||||
7
|
||||
""" if dtype(int_name).char.islower() else "")))
|
||||
@ -0,0 +1,134 @@
|
||||
"""
|
||||
Functions in the ``as*array`` family that promote array-likes into arrays.
|
||||
|
||||
`require` fits this category despite its name not matching this pattern.
|
||||
"""
|
||||
from .overrides import (
|
||||
array_function_dispatch,
|
||||
set_array_function_like_doc,
|
||||
set_module,
|
||||
)
|
||||
from .multiarray import array, asanyarray
|
||||
|
||||
|
||||
__all__ = ["require"]
|
||||
|
||||
|
||||
POSSIBLE_FLAGS = {
|
||||
'C': 'C', 'C_CONTIGUOUS': 'C', 'CONTIGUOUS': 'C',
|
||||
'F': 'F', 'F_CONTIGUOUS': 'F', 'FORTRAN': 'F',
|
||||
'A': 'A', 'ALIGNED': 'A',
|
||||
'W': 'W', 'WRITEABLE': 'W',
|
||||
'O': 'O', 'OWNDATA': 'O',
|
||||
'E': 'E', 'ENSUREARRAY': 'E'
|
||||
}
|
||||
|
||||
|
||||
@set_array_function_like_doc
|
||||
@set_module('numpy')
|
||||
def require(a, dtype=None, requirements=None, *, like=None):
|
||||
"""
|
||||
Return an ndarray of the provided type that satisfies requirements.
|
||||
|
||||
This function is useful to be sure that an array with the correct flags
|
||||
is returned for passing to compiled code (perhaps through ctypes).
|
||||
|
||||
Parameters
|
||||
----------
|
||||
a : array_like
|
||||
The object to be converted to a type-and-requirement-satisfying array.
|
||||
dtype : data-type
|
||||
The required data-type. If None preserve the current dtype. If your
|
||||
application requires the data to be in native byteorder, include
|
||||
a byteorder specification as a part of the dtype specification.
|
||||
requirements : str or sequence of str
|
||||
The requirements list can be any of the following
|
||||
|
||||
* 'F_CONTIGUOUS' ('F') - ensure a Fortran-contiguous array
|
||||
* 'C_CONTIGUOUS' ('C') - ensure a C-contiguous array
|
||||
* 'ALIGNED' ('A') - ensure a data-type aligned array
|
||||
* 'WRITEABLE' ('W') - ensure a writable array
|
||||
* 'OWNDATA' ('O') - ensure an array that owns its own data
|
||||
* 'ENSUREARRAY', ('E') - ensure a base array, instead of a subclass
|
||||
${ARRAY_FUNCTION_LIKE}
|
||||
|
||||
.. versionadded:: 1.20.0
|
||||
|
||||
Returns
|
||||
-------
|
||||
out : ndarray
|
||||
Array with specified requirements and type if given.
|
||||
|
||||
See Also
|
||||
--------
|
||||
asarray : Convert input to an ndarray.
|
||||
asanyarray : Convert to an ndarray, but pass through ndarray subclasses.
|
||||
ascontiguousarray : Convert input to a contiguous array.
|
||||
asfortranarray : Convert input to an ndarray with column-major
|
||||
memory order.
|
||||
ndarray.flags : Information about the memory layout of the array.
|
||||
|
||||
Notes
|
||||
-----
|
||||
The returned array will be guaranteed to have the listed requirements
|
||||
by making a copy if needed.
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> x = np.arange(6).reshape(2,3)
|
||||
>>> x.flags
|
||||
C_CONTIGUOUS : True
|
||||
F_CONTIGUOUS : False
|
||||
OWNDATA : False
|
||||
WRITEABLE : True
|
||||
ALIGNED : True
|
||||
WRITEBACKIFCOPY : False
|
||||
|
||||
>>> y = np.require(x, dtype=np.float32, requirements=['A', 'O', 'W', 'F'])
|
||||
>>> y.flags
|
||||
C_CONTIGUOUS : False
|
||||
F_CONTIGUOUS : True
|
||||
OWNDATA : True
|
||||
WRITEABLE : True
|
||||
ALIGNED : True
|
||||
WRITEBACKIFCOPY : False
|
||||
|
||||
"""
|
||||
if like is not None:
|
||||
return _require_with_like(
|
||||
like,
|
||||
a,
|
||||
dtype=dtype,
|
||||
requirements=requirements,
|
||||
)
|
||||
|
||||
if not requirements:
|
||||
return asanyarray(a, dtype=dtype)
|
||||
|
||||
requirements = {POSSIBLE_FLAGS[x.upper()] for x in requirements}
|
||||
|
||||
if 'E' in requirements:
|
||||
requirements.remove('E')
|
||||
subok = False
|
||||
else:
|
||||
subok = True
|
||||
|
||||
order = 'A'
|
||||
if requirements >= {'C', 'F'}:
|
||||
raise ValueError('Cannot specify both "C" and "F" order')
|
||||
elif 'F' in requirements:
|
||||
order = 'F'
|
||||
requirements.remove('F')
|
||||
elif 'C' in requirements:
|
||||
order = 'C'
|
||||
requirements.remove('C')
|
||||
|
||||
arr = array(a, dtype=dtype, order=order, copy=None, subok=subok)
|
||||
|
||||
for prop in requirements:
|
||||
if not arr.flags[prop]:
|
||||
return arr.copy(order)
|
||||
return arr
|
||||
|
||||
|
||||
_require_with_like = array_function_dispatch()(require)
|
||||
@ -0,0 +1,41 @@
|
||||
from collections.abc import Iterable
|
||||
from typing import Any, TypeVar, overload, Literal
|
||||
|
||||
from numpy._typing import NDArray, DTypeLike, _SupportsArrayFunc
|
||||
|
||||
_ArrayType = TypeVar("_ArrayType", bound=NDArray[Any])
|
||||
|
||||
_Requirements = Literal[
|
||||
"C", "C_CONTIGUOUS", "CONTIGUOUS",
|
||||
"F", "F_CONTIGUOUS", "FORTRAN",
|
||||
"A", "ALIGNED",
|
||||
"W", "WRITEABLE",
|
||||
"O", "OWNDATA"
|
||||
]
|
||||
_E = Literal["E", "ENSUREARRAY"]
|
||||
_RequirementsWithE = _Requirements | _E
|
||||
|
||||
@overload
|
||||
def require(
|
||||
a: _ArrayType,
|
||||
dtype: None = ...,
|
||||
requirements: None | _Requirements | Iterable[_Requirements] = ...,
|
||||
*,
|
||||
like: _SupportsArrayFunc = ...
|
||||
) -> _ArrayType: ...
|
||||
@overload
|
||||
def require(
|
||||
a: object,
|
||||
dtype: DTypeLike = ...,
|
||||
requirements: _E | Iterable[_RequirementsWithE] = ...,
|
||||
*,
|
||||
like: _SupportsArrayFunc = ...
|
||||
) -> NDArray[Any]: ...
|
||||
@overload
|
||||
def require(
|
||||
a: object,
|
||||
dtype: DTypeLike = ...,
|
||||
requirements: None | _Requirements | Iterable[_Requirements] = ...,
|
||||
*,
|
||||
like: _SupportsArrayFunc = ...
|
||||
) -> NDArray[Any]: ...
|
||||
@ -0,0 +1,376 @@
|
||||
"""
|
||||
A place for code to be called from the implementation of np.dtype
|
||||
|
||||
String handling is much easier to do correctly in python.
|
||||
"""
|
||||
import numpy as np
|
||||
|
||||
|
||||
_kind_to_stem = {
|
||||
'u': 'uint',
|
||||
'i': 'int',
|
||||
'c': 'complex',
|
||||
'f': 'float',
|
||||
'b': 'bool',
|
||||
'V': 'void',
|
||||
'O': 'object',
|
||||
'M': 'datetime',
|
||||
'm': 'timedelta',
|
||||
'S': 'bytes',
|
||||
'U': 'str',
|
||||
}
|
||||
|
||||
|
||||
def _kind_name(dtype):
|
||||
try:
|
||||
return _kind_to_stem[dtype.kind]
|
||||
except KeyError as e:
|
||||
raise RuntimeError(
|
||||
"internal dtype error, unknown kind {!r}"
|
||||
.format(dtype.kind)
|
||||
) from None
|
||||
|
||||
|
||||
def __str__(dtype):
|
||||
if dtype.fields is not None:
|
||||
return _struct_str(dtype, include_align=True)
|
||||
elif dtype.subdtype:
|
||||
return _subarray_str(dtype)
|
||||
elif issubclass(dtype.type, np.flexible) or not dtype.isnative:
|
||||
return dtype.str
|
||||
else:
|
||||
return dtype.name
|
||||
|
||||
|
||||
def __repr__(dtype):
|
||||
arg_str = _construction_repr(dtype, include_align=False)
|
||||
if dtype.isalignedstruct:
|
||||
arg_str = arg_str + ", align=True"
|
||||
return "dtype({})".format(arg_str)
|
||||
|
||||
|
||||
def _unpack_field(dtype, offset, title=None):
|
||||
"""
|
||||
Helper function to normalize the items in dtype.fields.
|
||||
|
||||
Call as:
|
||||
|
||||
dtype, offset, title = _unpack_field(*dtype.fields[name])
|
||||
"""
|
||||
return dtype, offset, title
|
||||
|
||||
|
||||
def _isunsized(dtype):
|
||||
# PyDataType_ISUNSIZED
|
||||
return dtype.itemsize == 0
|
||||
|
||||
|
||||
def _construction_repr(dtype, include_align=False, short=False):
|
||||
"""
|
||||
Creates a string repr of the dtype, excluding the 'dtype()' part
|
||||
surrounding the object. This object may be a string, a list, or
|
||||
a dict depending on the nature of the dtype. This
|
||||
is the object passed as the first parameter to the dtype
|
||||
constructor, and if no additional constructor parameters are
|
||||
given, will reproduce the exact memory layout.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
short : bool
|
||||
If true, this creates a shorter repr using 'kind' and 'itemsize',
|
||||
instead of the longer type name.
|
||||
|
||||
include_align : bool
|
||||
If true, this includes the 'align=True' parameter
|
||||
inside the struct dtype construction dict when needed. Use this flag
|
||||
if you want a proper repr string without the 'dtype()' part around it.
|
||||
|
||||
If false, this does not preserve the
|
||||
'align=True' parameter or sticky NPY_ALIGNED_STRUCT flag for
|
||||
struct arrays like the regular repr does, because the 'align'
|
||||
flag is not part of first dtype constructor parameter. This
|
||||
mode is intended for a full 'repr', where the 'align=True' is
|
||||
provided as the second parameter.
|
||||
"""
|
||||
if dtype.fields is not None:
|
||||
return _struct_str(dtype, include_align=include_align)
|
||||
elif dtype.subdtype:
|
||||
return _subarray_str(dtype)
|
||||
else:
|
||||
return _scalar_str(dtype, short=short)
|
||||
|
||||
|
||||
def _scalar_str(dtype, short):
|
||||
byteorder = _byte_order_str(dtype)
|
||||
|
||||
if dtype.type == np.bool:
|
||||
if short:
|
||||
return "'?'"
|
||||
else:
|
||||
return "'bool'"
|
||||
|
||||
elif dtype.type == np.object_:
|
||||
# The object reference may be different sizes on different
|
||||
# platforms, so it should never include the itemsize here.
|
||||
return "'O'"
|
||||
|
||||
elif dtype.type == np.bytes_:
|
||||
if _isunsized(dtype):
|
||||
return "'S'"
|
||||
else:
|
||||
return "'S%d'" % dtype.itemsize
|
||||
|
||||
elif dtype.type == np.str_:
|
||||
if _isunsized(dtype):
|
||||
return "'%sU'" % byteorder
|
||||
else:
|
||||
return "'%sU%d'" % (byteorder, dtype.itemsize / 4)
|
||||
|
||||
elif dtype.type == str:
|
||||
return "'T'"
|
||||
|
||||
elif not type(dtype)._legacy:
|
||||
return f"'{byteorder}{type(dtype).__name__}{dtype.itemsize * 8}'"
|
||||
|
||||
# unlike the other types, subclasses of void are preserved - but
|
||||
# historically the repr does not actually reveal the subclass
|
||||
elif issubclass(dtype.type, np.void):
|
||||
if _isunsized(dtype):
|
||||
return "'V'"
|
||||
else:
|
||||
return "'V%d'" % dtype.itemsize
|
||||
|
||||
elif dtype.type == np.datetime64:
|
||||
return "'%sM8%s'" % (byteorder, _datetime_metadata_str(dtype))
|
||||
|
||||
elif dtype.type == np.timedelta64:
|
||||
return "'%sm8%s'" % (byteorder, _datetime_metadata_str(dtype))
|
||||
|
||||
elif np.issubdtype(dtype, np.number):
|
||||
# Short repr with endianness, like '<f8'
|
||||
if short or dtype.byteorder not in ('=', '|'):
|
||||
return "'%s%c%d'" % (byteorder, dtype.kind, dtype.itemsize)
|
||||
|
||||
# Longer repr, like 'float64'
|
||||
else:
|
||||
return "'%s%d'" % (_kind_name(dtype), 8*dtype.itemsize)
|
||||
|
||||
elif dtype.isbuiltin == 2:
|
||||
return dtype.type.__name__
|
||||
|
||||
else:
|
||||
raise RuntimeError(
|
||||
"Internal error: NumPy dtype unrecognized type number")
|
||||
|
||||
|
||||
def _byte_order_str(dtype):
|
||||
""" Normalize byteorder to '<' or '>' """
|
||||
# hack to obtain the native and swapped byte order characters
|
||||
swapped = np.dtype(int).newbyteorder('S')
|
||||
native = swapped.newbyteorder('S')
|
||||
|
||||
byteorder = dtype.byteorder
|
||||
if byteorder == '=':
|
||||
return native.byteorder
|
||||
if byteorder == 'S':
|
||||
# TODO: this path can never be reached
|
||||
return swapped.byteorder
|
||||
elif byteorder == '|':
|
||||
return ''
|
||||
else:
|
||||
return byteorder
|
||||
|
||||
|
||||
def _datetime_metadata_str(dtype):
|
||||
# TODO: this duplicates the C metastr_to_unicode functionality
|
||||
unit, count = np.datetime_data(dtype)
|
||||
if unit == 'generic':
|
||||
return ''
|
||||
elif count == 1:
|
||||
return '[{}]'.format(unit)
|
||||
else:
|
||||
return '[{}{}]'.format(count, unit)
|
||||
|
||||
|
||||
def _struct_dict_str(dtype, includealignedflag):
|
||||
# unpack the fields dictionary into ls
|
||||
names = dtype.names
|
||||
fld_dtypes = []
|
||||
offsets = []
|
||||
titles = []
|
||||
for name in names:
|
||||
fld_dtype, offset, title = _unpack_field(*dtype.fields[name])
|
||||
fld_dtypes.append(fld_dtype)
|
||||
offsets.append(offset)
|
||||
titles.append(title)
|
||||
|
||||
# Build up a string to make the dictionary
|
||||
|
||||
if np._core.arrayprint._get_legacy_print_mode() <= 121:
|
||||
colon = ":"
|
||||
fieldsep = ","
|
||||
else:
|
||||
colon = ": "
|
||||
fieldsep = ", "
|
||||
|
||||
# First, the names
|
||||
ret = "{'names'%s[" % colon
|
||||
ret += fieldsep.join(repr(name) for name in names)
|
||||
|
||||
# Second, the formats
|
||||
ret += "], 'formats'%s[" % colon
|
||||
ret += fieldsep.join(
|
||||
_construction_repr(fld_dtype, short=True) for fld_dtype in fld_dtypes)
|
||||
|
||||
# Third, the offsets
|
||||
ret += "], 'offsets'%s[" % colon
|
||||
ret += fieldsep.join("%d" % offset for offset in offsets)
|
||||
|
||||
# Fourth, the titles
|
||||
if any(title is not None for title in titles):
|
||||
ret += "], 'titles'%s[" % colon
|
||||
ret += fieldsep.join(repr(title) for title in titles)
|
||||
|
||||
# Fifth, the itemsize
|
||||
ret += "], 'itemsize'%s%d" % (colon, dtype.itemsize)
|
||||
|
||||
if (includealignedflag and dtype.isalignedstruct):
|
||||
# Finally, the aligned flag
|
||||
ret += ", 'aligned'%sTrue}" % colon
|
||||
else:
|
||||
ret += "}"
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
def _aligned_offset(offset, alignment):
|
||||
# round up offset:
|
||||
return - (-offset // alignment) * alignment
|
||||
|
||||
|
||||
def _is_packed(dtype):
|
||||
"""
|
||||
Checks whether the structured data type in 'dtype'
|
||||
has a simple layout, where all the fields are in order,
|
||||
and follow each other with no alignment padding.
|
||||
|
||||
When this returns true, the dtype can be reconstructed
|
||||
from a list of the field names and dtypes with no additional
|
||||
dtype parameters.
|
||||
|
||||
Duplicates the C `is_dtype_struct_simple_unaligned_layout` function.
|
||||
"""
|
||||
align = dtype.isalignedstruct
|
||||
max_alignment = 1
|
||||
total_offset = 0
|
||||
for name in dtype.names:
|
||||
fld_dtype, fld_offset, title = _unpack_field(*dtype.fields[name])
|
||||
|
||||
if align:
|
||||
total_offset = _aligned_offset(total_offset, fld_dtype.alignment)
|
||||
max_alignment = max(max_alignment, fld_dtype.alignment)
|
||||
|
||||
if fld_offset != total_offset:
|
||||
return False
|
||||
total_offset += fld_dtype.itemsize
|
||||
|
||||
if align:
|
||||
total_offset = _aligned_offset(total_offset, max_alignment)
|
||||
|
||||
if total_offset != dtype.itemsize:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def _struct_list_str(dtype):
|
||||
items = []
|
||||
for name in dtype.names:
|
||||
fld_dtype, fld_offset, title = _unpack_field(*dtype.fields[name])
|
||||
|
||||
item = "("
|
||||
if title is not None:
|
||||
item += "({!r}, {!r}), ".format(title, name)
|
||||
else:
|
||||
item += "{!r}, ".format(name)
|
||||
# Special case subarray handling here
|
||||
if fld_dtype.subdtype is not None:
|
||||
base, shape = fld_dtype.subdtype
|
||||
item += "{}, {}".format(
|
||||
_construction_repr(base, short=True),
|
||||
shape
|
||||
)
|
||||
else:
|
||||
item += _construction_repr(fld_dtype, short=True)
|
||||
|
||||
item += ")"
|
||||
items.append(item)
|
||||
|
||||
return "[" + ", ".join(items) + "]"
|
||||
|
||||
|
||||
def _struct_str(dtype, include_align):
|
||||
# The list str representation can't include the 'align=' flag,
|
||||
# so if it is requested and the struct has the aligned flag set,
|
||||
# we must use the dict str instead.
|
||||
if not (include_align and dtype.isalignedstruct) and _is_packed(dtype):
|
||||
sub = _struct_list_str(dtype)
|
||||
|
||||
else:
|
||||
sub = _struct_dict_str(dtype, include_align)
|
||||
|
||||
# If the data type isn't the default, void, show it
|
||||
if dtype.type != np.void:
|
||||
return "({t.__module__}.{t.__name__}, {f})".format(t=dtype.type, f=sub)
|
||||
else:
|
||||
return sub
|
||||
|
||||
|
||||
def _subarray_str(dtype):
|
||||
base, shape = dtype.subdtype
|
||||
return "({}, {})".format(
|
||||
_construction_repr(base, short=True),
|
||||
shape
|
||||
)
|
||||
|
||||
|
||||
def _name_includes_bit_suffix(dtype):
|
||||
if dtype.type == np.object_:
|
||||
# pointer size varies by system, best to omit it
|
||||
return False
|
||||
elif dtype.type == np.bool:
|
||||
# implied
|
||||
return False
|
||||
elif dtype.type is None:
|
||||
return True
|
||||
elif np.issubdtype(dtype, np.flexible) and _isunsized(dtype):
|
||||
# unspecified
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
|
||||
def _name_get(dtype):
|
||||
# provides dtype.name.__get__, documented as returning a "bit name"
|
||||
|
||||
if dtype.isbuiltin == 2:
|
||||
# user dtypes don't promise to do anything special
|
||||
return dtype.type.__name__
|
||||
|
||||
if not type(dtype)._legacy:
|
||||
name = type(dtype).__name__
|
||||
|
||||
elif issubclass(dtype.type, np.void):
|
||||
# historically, void subclasses preserve their name, eg `record64`
|
||||
name = dtype.type.__name__
|
||||
else:
|
||||
name = _kind_name(dtype)
|
||||
|
||||
# append bit counts
|
||||
if _name_includes_bit_suffix(dtype):
|
||||
name += "{}".format(dtype.itemsize * 8)
|
||||
|
||||
# append metadata to datetimes
|
||||
if dtype.type in (np.datetime64, np.timedelta64):
|
||||
name += _datetime_metadata_str(dtype)
|
||||
|
||||
return name
|
||||
@ -0,0 +1,120 @@
|
||||
"""
|
||||
Conversion from ctypes to dtype.
|
||||
|
||||
In an ideal world, we could achieve this through the PEP3118 buffer protocol,
|
||||
something like::
|
||||
|
||||
def dtype_from_ctypes_type(t):
|
||||
# needed to ensure that the shape of `t` is within memoryview.format
|
||||
class DummyStruct(ctypes.Structure):
|
||||
_fields_ = [('a', t)]
|
||||
|
||||
# empty to avoid memory allocation
|
||||
ctype_0 = (DummyStruct * 0)()
|
||||
mv = memoryview(ctype_0)
|
||||
|
||||
# convert the struct, and slice back out the field
|
||||
return _dtype_from_pep3118(mv.format)['a']
|
||||
|
||||
Unfortunately, this fails because:
|
||||
|
||||
* ctypes cannot handle length-0 arrays with PEP3118 (bpo-32782)
|
||||
* PEP3118 cannot represent unions, but both numpy and ctypes can
|
||||
* ctypes cannot handle big-endian structs with PEP3118 (bpo-32780)
|
||||
"""
|
||||
|
||||
# We delay-import ctypes for distributions that do not include it.
|
||||
# While this module is not used unless the user passes in ctypes
|
||||
# members, it is eagerly imported from numpy/_core/__init__.py.
|
||||
import numpy as np
|
||||
|
||||
|
||||
def _from_ctypes_array(t):
|
||||
return np.dtype((dtype_from_ctypes_type(t._type_), (t._length_,)))
|
||||
|
||||
|
||||
def _from_ctypes_structure(t):
|
||||
for item in t._fields_:
|
||||
if len(item) > 2:
|
||||
raise TypeError(
|
||||
"ctypes bitfields have no dtype equivalent")
|
||||
|
||||
if hasattr(t, "_pack_"):
|
||||
import ctypes
|
||||
formats = []
|
||||
offsets = []
|
||||
names = []
|
||||
current_offset = 0
|
||||
for fname, ftyp in t._fields_:
|
||||
names.append(fname)
|
||||
formats.append(dtype_from_ctypes_type(ftyp))
|
||||
# Each type has a default offset, this is platform dependent
|
||||
# for some types.
|
||||
effective_pack = min(t._pack_, ctypes.alignment(ftyp))
|
||||
current_offset = (
|
||||
(current_offset + effective_pack - 1) // effective_pack
|
||||
) * effective_pack
|
||||
offsets.append(current_offset)
|
||||
current_offset += ctypes.sizeof(ftyp)
|
||||
|
||||
return np.dtype(dict(
|
||||
formats=formats,
|
||||
offsets=offsets,
|
||||
names=names,
|
||||
itemsize=ctypes.sizeof(t)))
|
||||
else:
|
||||
fields = []
|
||||
for fname, ftyp in t._fields_:
|
||||
fields.append((fname, dtype_from_ctypes_type(ftyp)))
|
||||
|
||||
# by default, ctypes structs are aligned
|
||||
return np.dtype(fields, align=True)
|
||||
|
||||
|
||||
def _from_ctypes_scalar(t):
|
||||
"""
|
||||
Return the dtype type with endianness included if it's the case
|
||||
"""
|
||||
if getattr(t, '__ctype_be__', None) is t:
|
||||
return np.dtype('>' + t._type_)
|
||||
elif getattr(t, '__ctype_le__', None) is t:
|
||||
return np.dtype('<' + t._type_)
|
||||
else:
|
||||
return np.dtype(t._type_)
|
||||
|
||||
|
||||
def _from_ctypes_union(t):
|
||||
import ctypes
|
||||
formats = []
|
||||
offsets = []
|
||||
names = []
|
||||
for fname, ftyp in t._fields_:
|
||||
names.append(fname)
|
||||
formats.append(dtype_from_ctypes_type(ftyp))
|
||||
offsets.append(0) # Union fields are offset to 0
|
||||
|
||||
return np.dtype(dict(
|
||||
formats=formats,
|
||||
offsets=offsets,
|
||||
names=names,
|
||||
itemsize=ctypes.sizeof(t)))
|
||||
|
||||
|
||||
def dtype_from_ctypes_type(t):
|
||||
"""
|
||||
Construct a dtype object from a ctypes type
|
||||
"""
|
||||
import _ctypes
|
||||
if issubclass(t, _ctypes.Array):
|
||||
return _from_ctypes_array(t)
|
||||
elif issubclass(t, _ctypes._Pointer):
|
||||
raise TypeError("ctypes pointers have no dtype equivalent")
|
||||
elif issubclass(t, _ctypes.Structure):
|
||||
return _from_ctypes_structure(t)
|
||||
elif issubclass(t, _ctypes.Union):
|
||||
return _from_ctypes_union(t)
|
||||
elif isinstance(getattr(t, '_type_', None), str):
|
||||
return _from_ctypes_scalar(t)
|
||||
else:
|
||||
raise NotImplementedError(
|
||||
"Unknown ctypes type {}".format(t.__name__))
|
||||
@ -0,0 +1,172 @@
|
||||
"""
|
||||
Various richly-typed exceptions, that also help us deal with string formatting
|
||||
in python where it's easier.
|
||||
|
||||
By putting the formatting in `__str__`, we also avoid paying the cost for
|
||||
users who silence the exceptions.
|
||||
"""
|
||||
from .._utils import set_module
|
||||
|
||||
def _unpack_tuple(tup):
|
||||
if len(tup) == 1:
|
||||
return tup[0]
|
||||
else:
|
||||
return tup
|
||||
|
||||
|
||||
def _display_as_base(cls):
|
||||
"""
|
||||
A decorator that makes an exception class look like its base.
|
||||
|
||||
We use this to hide subclasses that are implementation details - the user
|
||||
should catch the base type, which is what the traceback will show them.
|
||||
|
||||
Classes decorated with this decorator are subject to removal without a
|
||||
deprecation warning.
|
||||
"""
|
||||
assert issubclass(cls, Exception)
|
||||
cls.__name__ = cls.__base__.__name__
|
||||
return cls
|
||||
|
||||
|
||||
class UFuncTypeError(TypeError):
|
||||
""" Base class for all ufunc exceptions """
|
||||
def __init__(self, ufunc):
|
||||
self.ufunc = ufunc
|
||||
|
||||
|
||||
@_display_as_base
|
||||
class _UFuncNoLoopError(UFuncTypeError):
|
||||
""" Thrown when a ufunc loop cannot be found """
|
||||
def __init__(self, ufunc, dtypes):
|
||||
super().__init__(ufunc)
|
||||
self.dtypes = tuple(dtypes)
|
||||
|
||||
def __str__(self):
|
||||
return (
|
||||
"ufunc {!r} did not contain a loop with signature matching types "
|
||||
"{!r} -> {!r}"
|
||||
).format(
|
||||
self.ufunc.__name__,
|
||||
_unpack_tuple(self.dtypes[:self.ufunc.nin]),
|
||||
_unpack_tuple(self.dtypes[self.ufunc.nin:])
|
||||
)
|
||||
|
||||
|
||||
@_display_as_base
|
||||
class _UFuncBinaryResolutionError(_UFuncNoLoopError):
|
||||
""" Thrown when a binary resolution fails """
|
||||
def __init__(self, ufunc, dtypes):
|
||||
super().__init__(ufunc, dtypes)
|
||||
assert len(self.dtypes) == 2
|
||||
|
||||
def __str__(self):
|
||||
return (
|
||||
"ufunc {!r} cannot use operands with types {!r} and {!r}"
|
||||
).format(
|
||||
self.ufunc.__name__, *self.dtypes
|
||||
)
|
||||
|
||||
|
||||
@_display_as_base
|
||||
class _UFuncCastingError(UFuncTypeError):
|
||||
def __init__(self, ufunc, casting, from_, to):
|
||||
super().__init__(ufunc)
|
||||
self.casting = casting
|
||||
self.from_ = from_
|
||||
self.to = to
|
||||
|
||||
|
||||
@_display_as_base
|
||||
class _UFuncInputCastingError(_UFuncCastingError):
|
||||
""" Thrown when a ufunc input cannot be casted """
|
||||
def __init__(self, ufunc, casting, from_, to, i):
|
||||
super().__init__(ufunc, casting, from_, to)
|
||||
self.in_i = i
|
||||
|
||||
def __str__(self):
|
||||
# only show the number if more than one input exists
|
||||
i_str = "{} ".format(self.in_i) if self.ufunc.nin != 1 else ""
|
||||
return (
|
||||
"Cannot cast ufunc {!r} input {}from {!r} to {!r} with casting "
|
||||
"rule {!r}"
|
||||
).format(
|
||||
self.ufunc.__name__, i_str, self.from_, self.to, self.casting
|
||||
)
|
||||
|
||||
|
||||
@_display_as_base
|
||||
class _UFuncOutputCastingError(_UFuncCastingError):
|
||||
""" Thrown when a ufunc output cannot be casted """
|
||||
def __init__(self, ufunc, casting, from_, to, i):
|
||||
super().__init__(ufunc, casting, from_, to)
|
||||
self.out_i = i
|
||||
|
||||
def __str__(self):
|
||||
# only show the number if more than one output exists
|
||||
i_str = "{} ".format(self.out_i) if self.ufunc.nout != 1 else ""
|
||||
return (
|
||||
"Cannot cast ufunc {!r} output {}from {!r} to {!r} with casting "
|
||||
"rule {!r}"
|
||||
).format(
|
||||
self.ufunc.__name__, i_str, self.from_, self.to, self.casting
|
||||
)
|
||||
|
||||
|
||||
@_display_as_base
|
||||
class _ArrayMemoryError(MemoryError):
|
||||
""" Thrown when an array cannot be allocated"""
|
||||
def __init__(self, shape, dtype):
|
||||
self.shape = shape
|
||||
self.dtype = dtype
|
||||
|
||||
@property
|
||||
def _total_size(self):
|
||||
num_bytes = self.dtype.itemsize
|
||||
for dim in self.shape:
|
||||
num_bytes *= dim
|
||||
return num_bytes
|
||||
|
||||
@staticmethod
|
||||
def _size_to_string(num_bytes):
|
||||
""" Convert a number of bytes into a binary size string """
|
||||
|
||||
# https://en.wikipedia.org/wiki/Binary_prefix
|
||||
LOG2_STEP = 10
|
||||
STEP = 1024
|
||||
units = ['bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB']
|
||||
|
||||
unit_i = max(num_bytes.bit_length() - 1, 1) // LOG2_STEP
|
||||
unit_val = 1 << (unit_i * LOG2_STEP)
|
||||
n_units = num_bytes / unit_val
|
||||
del unit_val
|
||||
|
||||
# ensure we pick a unit that is correct after rounding
|
||||
if round(n_units) == STEP:
|
||||
unit_i += 1
|
||||
n_units /= STEP
|
||||
|
||||
# deal with sizes so large that we don't have units for them
|
||||
if unit_i >= len(units):
|
||||
new_unit_i = len(units) - 1
|
||||
n_units *= 1 << ((unit_i - new_unit_i) * LOG2_STEP)
|
||||
unit_i = new_unit_i
|
||||
|
||||
unit_name = units[unit_i]
|
||||
# format with a sensible number of digits
|
||||
if unit_i == 0:
|
||||
# no decimal point on bytes
|
||||
return '{:.0f} {}'.format(n_units, unit_name)
|
||||
elif round(n_units) < 1000:
|
||||
# 3 significant figures, if none are dropped to the left of the .
|
||||
return '{:#.3g} {}'.format(n_units, unit_name)
|
||||
else:
|
||||
# just give all the digits otherwise
|
||||
return '{:#.0f} {}'.format(n_units, unit_name)
|
||||
|
||||
def __str__(self):
|
||||
size_str = self._size_to_string(self._total_size)
|
||||
return (
|
||||
"Unable to allocate {} for an array with shape {} and data type {}"
|
||||
.format(size_str, self.shape, self.dtype)
|
||||
)
|
||||
@ -0,0 +1,959 @@
|
||||
"""
|
||||
A place for internal code
|
||||
|
||||
Some things are more easily handled Python.
|
||||
|
||||
"""
|
||||
import ast
|
||||
import re
|
||||
import sys
|
||||
import warnings
|
||||
|
||||
from ..exceptions import DTypePromotionError
|
||||
from .multiarray import dtype, array, ndarray, promote_types, StringDType
|
||||
from numpy import _NoValue
|
||||
try:
|
||||
import ctypes
|
||||
except ImportError:
|
||||
ctypes = None
|
||||
|
||||
IS_PYPY = sys.implementation.name == 'pypy'
|
||||
|
||||
if sys.byteorder == 'little':
|
||||
_nbo = '<'
|
||||
else:
|
||||
_nbo = '>'
|
||||
|
||||
def _makenames_list(adict, align):
|
||||
allfields = []
|
||||
|
||||
for fname, obj in adict.items():
|
||||
n = len(obj)
|
||||
if not isinstance(obj, tuple) or n not in (2, 3):
|
||||
raise ValueError("entry not a 2- or 3- tuple")
|
||||
if n > 2 and obj[2] == fname:
|
||||
continue
|
||||
num = int(obj[1])
|
||||
if num < 0:
|
||||
raise ValueError("invalid offset.")
|
||||
format = dtype(obj[0], align=align)
|
||||
if n > 2:
|
||||
title = obj[2]
|
||||
else:
|
||||
title = None
|
||||
allfields.append((fname, format, num, title))
|
||||
# sort by offsets
|
||||
allfields.sort(key=lambda x: x[2])
|
||||
names = [x[0] for x in allfields]
|
||||
formats = [x[1] for x in allfields]
|
||||
offsets = [x[2] for x in allfields]
|
||||
titles = [x[3] for x in allfields]
|
||||
|
||||
return names, formats, offsets, titles
|
||||
|
||||
# Called in PyArray_DescrConverter function when
|
||||
# a dictionary without "names" and "formats"
|
||||
# fields is used as a data-type descriptor.
|
||||
def _usefields(adict, align):
|
||||
try:
|
||||
names = adict[-1]
|
||||
except KeyError:
|
||||
names = None
|
||||
if names is None:
|
||||
names, formats, offsets, titles = _makenames_list(adict, align)
|
||||
else:
|
||||
formats = []
|
||||
offsets = []
|
||||
titles = []
|
||||
for name in names:
|
||||
res = adict[name]
|
||||
formats.append(res[0])
|
||||
offsets.append(res[1])
|
||||
if len(res) > 2:
|
||||
titles.append(res[2])
|
||||
else:
|
||||
titles.append(None)
|
||||
|
||||
return dtype({"names": names,
|
||||
"formats": formats,
|
||||
"offsets": offsets,
|
||||
"titles": titles}, align)
|
||||
|
||||
|
||||
# construct an array_protocol descriptor list
|
||||
# from the fields attribute of a descriptor
|
||||
# This calls itself recursively but should eventually hit
|
||||
# a descriptor that has no fields and then return
|
||||
# a simple typestring
|
||||
|
||||
def _array_descr(descriptor):
|
||||
fields = descriptor.fields
|
||||
if fields is None:
|
||||
subdtype = descriptor.subdtype
|
||||
if subdtype is None:
|
||||
if descriptor.metadata is None:
|
||||
return descriptor.str
|
||||
else:
|
||||
new = descriptor.metadata.copy()
|
||||
if new:
|
||||
return (descriptor.str, new)
|
||||
else:
|
||||
return descriptor.str
|
||||
else:
|
||||
return (_array_descr(subdtype[0]), subdtype[1])
|
||||
|
||||
names = descriptor.names
|
||||
ordered_fields = [fields[x] + (x,) for x in names]
|
||||
result = []
|
||||
offset = 0
|
||||
for field in ordered_fields:
|
||||
if field[1] > offset:
|
||||
num = field[1] - offset
|
||||
result.append(('', f'|V{num}'))
|
||||
offset += num
|
||||
elif field[1] < offset:
|
||||
raise ValueError(
|
||||
"dtype.descr is not defined for types with overlapping or "
|
||||
"out-of-order fields")
|
||||
if len(field) > 3:
|
||||
name = (field[2], field[3])
|
||||
else:
|
||||
name = field[2]
|
||||
if field[0].subdtype:
|
||||
tup = (name, _array_descr(field[0].subdtype[0]),
|
||||
field[0].subdtype[1])
|
||||
else:
|
||||
tup = (name, _array_descr(field[0]))
|
||||
offset += field[0].itemsize
|
||||
result.append(tup)
|
||||
|
||||
if descriptor.itemsize > offset:
|
||||
num = descriptor.itemsize - offset
|
||||
result.append(('', f'|V{num}'))
|
||||
|
||||
return result
|
||||
|
||||
|
||||
# format_re was originally from numarray by J. Todd Miller
|
||||
|
||||
format_re = re.compile(r'(?P<order1>[<>|=]?)'
|
||||
r'(?P<repeats> *[(]?[ ,0-9]*[)]? *)'
|
||||
r'(?P<order2>[<>|=]?)'
|
||||
r'(?P<dtype>[A-Za-z0-9.?]*(?:\[[a-zA-Z0-9,.]+\])?)')
|
||||
sep_re = re.compile(r'\s*,\s*')
|
||||
space_re = re.compile(r'\s+$')
|
||||
|
||||
# astr is a string (perhaps comma separated)
|
||||
|
||||
_convorder = {'=': _nbo}
|
||||
|
||||
def _commastring(astr):
|
||||
startindex = 0
|
||||
result = []
|
||||
islist = False
|
||||
while startindex < len(astr):
|
||||
mo = format_re.match(astr, pos=startindex)
|
||||
try:
|
||||
(order1, repeats, order2, dtype) = mo.groups()
|
||||
except (TypeError, AttributeError):
|
||||
raise ValueError(
|
||||
f'format number {len(result)+1} of "{astr}" is not recognized'
|
||||
) from None
|
||||
startindex = mo.end()
|
||||
# Separator or ending padding
|
||||
if startindex < len(astr):
|
||||
if space_re.match(astr, pos=startindex):
|
||||
startindex = len(astr)
|
||||
else:
|
||||
mo = sep_re.match(astr, pos=startindex)
|
||||
if not mo:
|
||||
raise ValueError(
|
||||
'format number %d of "%s" is not recognized' %
|
||||
(len(result)+1, astr))
|
||||
startindex = mo.end()
|
||||
islist = True
|
||||
|
||||
if order2 == '':
|
||||
order = order1
|
||||
elif order1 == '':
|
||||
order = order2
|
||||
else:
|
||||
order1 = _convorder.get(order1, order1)
|
||||
order2 = _convorder.get(order2, order2)
|
||||
if (order1 != order2):
|
||||
raise ValueError(
|
||||
'inconsistent byte-order specification %s and %s' %
|
||||
(order1, order2))
|
||||
order = order1
|
||||
|
||||
if order in ('|', '=', _nbo):
|
||||
order = ''
|
||||
dtype = order + dtype
|
||||
if repeats == '':
|
||||
newitem = dtype
|
||||
else:
|
||||
if (repeats[0] == "(" and repeats[-1] == ")"
|
||||
and repeats[1:-1].strip() != ""
|
||||
and "," not in repeats):
|
||||
warnings.warn(
|
||||
'Passing in a parenthesized single number for repeats '
|
||||
'is deprecated; pass either a single number or indicate '
|
||||
'a tuple with a comma, like "(2,)".', DeprecationWarning,
|
||||
stacklevel=2)
|
||||
newitem = (dtype, ast.literal_eval(repeats))
|
||||
|
||||
result.append(newitem)
|
||||
|
||||
return result if islist else result[0]
|
||||
|
||||
class dummy_ctype:
|
||||
|
||||
def __init__(self, cls):
|
||||
self._cls = cls
|
||||
|
||||
def __mul__(self, other):
|
||||
return self
|
||||
|
||||
def __call__(self, *other):
|
||||
return self._cls(other)
|
||||
|
||||
def __eq__(self, other):
|
||||
return self._cls == other._cls
|
||||
|
||||
def __ne__(self, other):
|
||||
return self._cls != other._cls
|
||||
|
||||
def _getintp_ctype():
|
||||
val = _getintp_ctype.cache
|
||||
if val is not None:
|
||||
return val
|
||||
if ctypes is None:
|
||||
import numpy as np
|
||||
val = dummy_ctype(np.intp)
|
||||
else:
|
||||
char = dtype('n').char
|
||||
if char == 'i':
|
||||
val = ctypes.c_int
|
||||
elif char == 'l':
|
||||
val = ctypes.c_long
|
||||
elif char == 'q':
|
||||
val = ctypes.c_longlong
|
||||
else:
|
||||
val = ctypes.c_long
|
||||
_getintp_ctype.cache = val
|
||||
return val
|
||||
|
||||
|
||||
_getintp_ctype.cache = None
|
||||
|
||||
# Used for .ctypes attribute of ndarray
|
||||
|
||||
class _missing_ctypes:
|
||||
def cast(self, num, obj):
|
||||
return num.value
|
||||
|
||||
class c_void_p:
|
||||
def __init__(self, ptr):
|
||||
self.value = ptr
|
||||
|
||||
|
||||
class _ctypes:
|
||||
def __init__(self, array, ptr=None):
|
||||
self._arr = array
|
||||
|
||||
if ctypes:
|
||||
self._ctypes = ctypes
|
||||
self._data = self._ctypes.c_void_p(ptr)
|
||||
else:
|
||||
# fake a pointer-like object that holds onto the reference
|
||||
self._ctypes = _missing_ctypes()
|
||||
self._data = self._ctypes.c_void_p(ptr)
|
||||
self._data._objects = array
|
||||
|
||||
if self._arr.ndim == 0:
|
||||
self._zerod = True
|
||||
else:
|
||||
self._zerod = False
|
||||
|
||||
def data_as(self, obj):
|
||||
"""
|
||||
Return the data pointer cast to a particular c-types object.
|
||||
For example, calling ``self._as_parameter_`` is equivalent to
|
||||
``self.data_as(ctypes.c_void_p)``. Perhaps you want to use
|
||||
the data as a pointer to a ctypes array of floating-point data:
|
||||
``self.data_as(ctypes.POINTER(ctypes.c_double))``.
|
||||
|
||||
The returned pointer will keep a reference to the array.
|
||||
"""
|
||||
# _ctypes.cast function causes a circular reference of self._data in
|
||||
# self._data._objects. Attributes of self._data cannot be released
|
||||
# until gc.collect is called. Make a copy of the pointer first then
|
||||
# let it hold the array reference. This is a workaround to circumvent
|
||||
# the CPython bug https://bugs.python.org/issue12836.
|
||||
ptr = self._ctypes.cast(self._data, obj)
|
||||
ptr._arr = self._arr
|
||||
return ptr
|
||||
|
||||
def shape_as(self, obj):
|
||||
"""
|
||||
Return the shape tuple as an array of some other c-types
|
||||
type. For example: ``self.shape_as(ctypes.c_short)``.
|
||||
"""
|
||||
if self._zerod:
|
||||
return None
|
||||
return (obj*self._arr.ndim)(*self._arr.shape)
|
||||
|
||||
def strides_as(self, obj):
|
||||
"""
|
||||
Return the strides tuple as an array of some other
|
||||
c-types type. For example: ``self.strides_as(ctypes.c_longlong)``.
|
||||
"""
|
||||
if self._zerod:
|
||||
return None
|
||||
return (obj*self._arr.ndim)(*self._arr.strides)
|
||||
|
||||
@property
|
||||
def data(self):
|
||||
"""
|
||||
A pointer to the memory area of the array as a Python integer.
|
||||
This memory area may contain data that is not aligned, or not in
|
||||
correct byte-order. The memory area may not even be writeable.
|
||||
The array flags and data-type of this array should be respected
|
||||
when passing this attribute to arbitrary C-code to avoid trouble
|
||||
that can include Python crashing. User Beware! The value of this
|
||||
attribute is exactly the same as:
|
||||
``self._array_interface_['data'][0]``.
|
||||
|
||||
Note that unlike ``data_as``, a reference won't be kept to the array:
|
||||
code like ``ctypes.c_void_p((a + b).ctypes.data)`` will result in a
|
||||
pointer to a deallocated array, and should be spelt
|
||||
``(a + b).ctypes.data_as(ctypes.c_void_p)``
|
||||
"""
|
||||
return self._data.value
|
||||
|
||||
@property
|
||||
def shape(self):
|
||||
"""
|
||||
(c_intp*self.ndim): A ctypes array of length self.ndim where
|
||||
the basetype is the C-integer corresponding to ``dtype('p')`` on this
|
||||
platform (see `~numpy.ctypeslib.c_intp`). This base-type could be
|
||||
`ctypes.c_int`, `ctypes.c_long`, or `ctypes.c_longlong` depending on
|
||||
the platform. The ctypes array contains the shape of
|
||||
the underlying array.
|
||||
"""
|
||||
return self.shape_as(_getintp_ctype())
|
||||
|
||||
@property
|
||||
def strides(self):
|
||||
"""
|
||||
(c_intp*self.ndim): A ctypes array of length self.ndim where
|
||||
the basetype is the same as for the shape attribute. This ctypes
|
||||
array contains the strides information from the underlying array.
|
||||
This strides information is important for showing how many bytes
|
||||
must be jumped to get to the next element in the array.
|
||||
"""
|
||||
return self.strides_as(_getintp_ctype())
|
||||
|
||||
@property
|
||||
def _as_parameter_(self):
|
||||
"""
|
||||
Overrides the ctypes semi-magic method
|
||||
|
||||
Enables `c_func(some_array.ctypes)`
|
||||
"""
|
||||
return self.data_as(ctypes.c_void_p)
|
||||
|
||||
# Numpy 1.21.0, 2021-05-18
|
||||
|
||||
def get_data(self):
|
||||
"""Deprecated getter for the `_ctypes.data` property.
|
||||
|
||||
.. deprecated:: 1.21
|
||||
"""
|
||||
warnings.warn('"get_data" is deprecated. Use "data" instead',
|
||||
DeprecationWarning, stacklevel=2)
|
||||
return self.data
|
||||
|
||||
def get_shape(self):
|
||||
"""Deprecated getter for the `_ctypes.shape` property.
|
||||
|
||||
.. deprecated:: 1.21
|
||||
"""
|
||||
warnings.warn('"get_shape" is deprecated. Use "shape" instead',
|
||||
DeprecationWarning, stacklevel=2)
|
||||
return self.shape
|
||||
|
||||
def get_strides(self):
|
||||
"""Deprecated getter for the `_ctypes.strides` property.
|
||||
|
||||
.. deprecated:: 1.21
|
||||
"""
|
||||
warnings.warn('"get_strides" is deprecated. Use "strides" instead',
|
||||
DeprecationWarning, stacklevel=2)
|
||||
return self.strides
|
||||
|
||||
def get_as_parameter(self):
|
||||
"""Deprecated getter for the `_ctypes._as_parameter_` property.
|
||||
|
||||
.. deprecated:: 1.21
|
||||
"""
|
||||
warnings.warn(
|
||||
'"get_as_parameter" is deprecated. Use "_as_parameter_" instead',
|
||||
DeprecationWarning, stacklevel=2,
|
||||
)
|
||||
return self._as_parameter_
|
||||
|
||||
|
||||
def _newnames(datatype, order):
|
||||
"""
|
||||
Given a datatype and an order object, return a new names tuple, with the
|
||||
order indicated
|
||||
"""
|
||||
oldnames = datatype.names
|
||||
nameslist = list(oldnames)
|
||||
if isinstance(order, str):
|
||||
order = [order]
|
||||
seen = set()
|
||||
if isinstance(order, (list, tuple)):
|
||||
for name in order:
|
||||
try:
|
||||
nameslist.remove(name)
|
||||
except ValueError:
|
||||
if name in seen:
|
||||
raise ValueError(f"duplicate field name: {name}") from None
|
||||
else:
|
||||
raise ValueError(f"unknown field name: {name}") from None
|
||||
seen.add(name)
|
||||
return tuple(list(order) + nameslist)
|
||||
raise ValueError(f"unsupported order value: {order}")
|
||||
|
||||
def _copy_fields(ary):
|
||||
"""Return copy of structured array with padding between fields removed.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
ary : ndarray
|
||||
Structured array from which to remove padding bytes
|
||||
|
||||
Returns
|
||||
-------
|
||||
ary_copy : ndarray
|
||||
Copy of ary with padding bytes removed
|
||||
"""
|
||||
dt = ary.dtype
|
||||
copy_dtype = {'names': dt.names,
|
||||
'formats': [dt.fields[name][0] for name in dt.names]}
|
||||
return array(ary, dtype=copy_dtype, copy=True)
|
||||
|
||||
def _promote_fields(dt1, dt2):
|
||||
""" Perform type promotion for two structured dtypes.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
dt1 : structured dtype
|
||||
First dtype.
|
||||
dt2 : structured dtype
|
||||
Second dtype.
|
||||
|
||||
Returns
|
||||
-------
|
||||
out : dtype
|
||||
The promoted dtype
|
||||
|
||||
Notes
|
||||
-----
|
||||
If one of the inputs is aligned, the result will be. The titles of
|
||||
both descriptors must match (point to the same field).
|
||||
"""
|
||||
# Both must be structured and have the same names in the same order
|
||||
if (dt1.names is None or dt2.names is None) or dt1.names != dt2.names:
|
||||
raise DTypePromotionError(
|
||||
f"field names `{dt1.names}` and `{dt2.names}` mismatch.")
|
||||
|
||||
# if both are identical, we can (maybe!) just return the same dtype.
|
||||
identical = dt1 is dt2
|
||||
new_fields = []
|
||||
for name in dt1.names:
|
||||
field1 = dt1.fields[name]
|
||||
field2 = dt2.fields[name]
|
||||
new_descr = promote_types(field1[0], field2[0])
|
||||
identical = identical and new_descr is field1[0]
|
||||
|
||||
# Check that the titles match (if given):
|
||||
if field1[2:] != field2[2:]:
|
||||
raise DTypePromotionError(
|
||||
f"field titles of field '{name}' mismatch")
|
||||
if len(field1) == 2:
|
||||
new_fields.append((name, new_descr))
|
||||
else:
|
||||
new_fields.append(((field1[2], name), new_descr))
|
||||
|
||||
res = dtype(new_fields, align=dt1.isalignedstruct or dt2.isalignedstruct)
|
||||
|
||||
# Might as well preserve identity (and metadata) if the dtype is identical
|
||||
# and the itemsize, offsets are also unmodified. This could probably be
|
||||
# sped up, but also probably just be removed entirely.
|
||||
if identical and res.itemsize == dt1.itemsize:
|
||||
for name in dt1.names:
|
||||
if dt1.fields[name][1] != res.fields[name][1]:
|
||||
return res # the dtype changed.
|
||||
return dt1
|
||||
|
||||
return res
|
||||
|
||||
|
||||
def _getfield_is_safe(oldtype, newtype, offset):
|
||||
""" Checks safety of getfield for object arrays.
|
||||
|
||||
As in _view_is_safe, we need to check that memory containing objects is not
|
||||
reinterpreted as a non-object datatype and vice versa.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
oldtype : data-type
|
||||
Data type of the original ndarray.
|
||||
newtype : data-type
|
||||
Data type of the field being accessed by ndarray.getfield
|
||||
offset : int
|
||||
Offset of the field being accessed by ndarray.getfield
|
||||
|
||||
Raises
|
||||
------
|
||||
TypeError
|
||||
If the field access is invalid
|
||||
|
||||
"""
|
||||
if newtype.hasobject or oldtype.hasobject:
|
||||
if offset == 0 and newtype == oldtype:
|
||||
return
|
||||
if oldtype.names is not None:
|
||||
for name in oldtype.names:
|
||||
if (oldtype.fields[name][1] == offset and
|
||||
oldtype.fields[name][0] == newtype):
|
||||
return
|
||||
raise TypeError("Cannot get/set field of an object array")
|
||||
return
|
||||
|
||||
def _view_is_safe(oldtype, newtype):
|
||||
""" Checks safety of a view involving object arrays, for example when
|
||||
doing::
|
||||
|
||||
np.zeros(10, dtype=oldtype).view(newtype)
|
||||
|
||||
Parameters
|
||||
----------
|
||||
oldtype : data-type
|
||||
Data type of original ndarray
|
||||
newtype : data-type
|
||||
Data type of the view
|
||||
|
||||
Raises
|
||||
------
|
||||
TypeError
|
||||
If the new type is incompatible with the old type.
|
||||
|
||||
"""
|
||||
|
||||
# if the types are equivalent, there is no problem.
|
||||
# for example: dtype((np.record, 'i4,i4')) == dtype((np.void, 'i4,i4'))
|
||||
if oldtype == newtype:
|
||||
return
|
||||
|
||||
if newtype.hasobject or oldtype.hasobject:
|
||||
raise TypeError("Cannot change data-type for array of references.")
|
||||
return
|
||||
|
||||
|
||||
# Given a string containing a PEP 3118 format specifier,
|
||||
# construct a NumPy dtype
|
||||
|
||||
_pep3118_native_map = {
|
||||
'?': '?',
|
||||
'c': 'S1',
|
||||
'b': 'b',
|
||||
'B': 'B',
|
||||
'h': 'h',
|
||||
'H': 'H',
|
||||
'i': 'i',
|
||||
'I': 'I',
|
||||
'l': 'l',
|
||||
'L': 'L',
|
||||
'q': 'q',
|
||||
'Q': 'Q',
|
||||
'e': 'e',
|
||||
'f': 'f',
|
||||
'd': 'd',
|
||||
'g': 'g',
|
||||
'Zf': 'F',
|
||||
'Zd': 'D',
|
||||
'Zg': 'G',
|
||||
's': 'S',
|
||||
'w': 'U',
|
||||
'O': 'O',
|
||||
'x': 'V', # padding
|
||||
}
|
||||
_pep3118_native_typechars = ''.join(_pep3118_native_map.keys())
|
||||
|
||||
_pep3118_standard_map = {
|
||||
'?': '?',
|
||||
'c': 'S1',
|
||||
'b': 'b',
|
||||
'B': 'B',
|
||||
'h': 'i2',
|
||||
'H': 'u2',
|
||||
'i': 'i4',
|
||||
'I': 'u4',
|
||||
'l': 'i4',
|
||||
'L': 'u4',
|
||||
'q': 'i8',
|
||||
'Q': 'u8',
|
||||
'e': 'f2',
|
||||
'f': 'f',
|
||||
'd': 'd',
|
||||
'Zf': 'F',
|
||||
'Zd': 'D',
|
||||
's': 'S',
|
||||
'w': 'U',
|
||||
'O': 'O',
|
||||
'x': 'V', # padding
|
||||
}
|
||||
_pep3118_standard_typechars = ''.join(_pep3118_standard_map.keys())
|
||||
|
||||
_pep3118_unsupported_map = {
|
||||
'u': 'UCS-2 strings',
|
||||
'&': 'pointers',
|
||||
't': 'bitfields',
|
||||
'X': 'function pointers',
|
||||
}
|
||||
|
||||
class _Stream:
|
||||
def __init__(self, s):
|
||||
self.s = s
|
||||
self.byteorder = '@'
|
||||
|
||||
def advance(self, n):
|
||||
res = self.s[:n]
|
||||
self.s = self.s[n:]
|
||||
return res
|
||||
|
||||
def consume(self, c):
|
||||
if self.s[:len(c)] == c:
|
||||
self.advance(len(c))
|
||||
return True
|
||||
return False
|
||||
|
||||
def consume_until(self, c):
|
||||
if callable(c):
|
||||
i = 0
|
||||
while i < len(self.s) and not c(self.s[i]):
|
||||
i = i + 1
|
||||
return self.advance(i)
|
||||
else:
|
||||
i = self.s.index(c)
|
||||
res = self.advance(i)
|
||||
self.advance(len(c))
|
||||
return res
|
||||
|
||||
@property
|
||||
def next(self):
|
||||
return self.s[0]
|
||||
|
||||
def __bool__(self):
|
||||
return bool(self.s)
|
||||
|
||||
|
||||
def _dtype_from_pep3118(spec):
|
||||
stream = _Stream(spec)
|
||||
dtype, align = __dtype_from_pep3118(stream, is_subdtype=False)
|
||||
return dtype
|
||||
|
||||
def __dtype_from_pep3118(stream, is_subdtype):
|
||||
field_spec = dict(
|
||||
names=[],
|
||||
formats=[],
|
||||
offsets=[],
|
||||
itemsize=0
|
||||
)
|
||||
offset = 0
|
||||
common_alignment = 1
|
||||
is_padding = False
|
||||
|
||||
# Parse spec
|
||||
while stream:
|
||||
value = None
|
||||
|
||||
# End of structure, bail out to upper level
|
||||
if stream.consume('}'):
|
||||
break
|
||||
|
||||
# Sub-arrays (1)
|
||||
shape = None
|
||||
if stream.consume('('):
|
||||
shape = stream.consume_until(')')
|
||||
shape = tuple(map(int, shape.split(',')))
|
||||
|
||||
# Byte order
|
||||
if stream.next in ('@', '=', '<', '>', '^', '!'):
|
||||
byteorder = stream.advance(1)
|
||||
if byteorder == '!':
|
||||
byteorder = '>'
|
||||
stream.byteorder = byteorder
|
||||
|
||||
# Byte order characters also control native vs. standard type sizes
|
||||
if stream.byteorder in ('@', '^'):
|
||||
type_map = _pep3118_native_map
|
||||
type_map_chars = _pep3118_native_typechars
|
||||
else:
|
||||
type_map = _pep3118_standard_map
|
||||
type_map_chars = _pep3118_standard_typechars
|
||||
|
||||
# Item sizes
|
||||
itemsize_str = stream.consume_until(lambda c: not c.isdigit())
|
||||
if itemsize_str:
|
||||
itemsize = int(itemsize_str)
|
||||
else:
|
||||
itemsize = 1
|
||||
|
||||
# Data types
|
||||
is_padding = False
|
||||
|
||||
if stream.consume('T{'):
|
||||
value, align = __dtype_from_pep3118(
|
||||
stream, is_subdtype=True)
|
||||
elif stream.next in type_map_chars:
|
||||
if stream.next == 'Z':
|
||||
typechar = stream.advance(2)
|
||||
else:
|
||||
typechar = stream.advance(1)
|
||||
|
||||
is_padding = (typechar == 'x')
|
||||
dtypechar = type_map[typechar]
|
||||
if dtypechar in 'USV':
|
||||
dtypechar += '%d' % itemsize
|
||||
itemsize = 1
|
||||
numpy_byteorder = {'@': '=', '^': '='}.get(
|
||||
stream.byteorder, stream.byteorder)
|
||||
value = dtype(numpy_byteorder + dtypechar)
|
||||
align = value.alignment
|
||||
elif stream.next in _pep3118_unsupported_map:
|
||||
desc = _pep3118_unsupported_map[stream.next]
|
||||
raise NotImplementedError(
|
||||
"Unrepresentable PEP 3118 data type {!r} ({})"
|
||||
.format(stream.next, desc))
|
||||
else:
|
||||
raise ValueError(
|
||||
"Unknown PEP 3118 data type specifier %r" % stream.s
|
||||
)
|
||||
|
||||
#
|
||||
# Native alignment may require padding
|
||||
#
|
||||
# Here we assume that the presence of a '@' character implicitly
|
||||
# implies that the start of the array is *already* aligned.
|
||||
#
|
||||
extra_offset = 0
|
||||
if stream.byteorder == '@':
|
||||
start_padding = (-offset) % align
|
||||
intra_padding = (-value.itemsize) % align
|
||||
|
||||
offset += start_padding
|
||||
|
||||
if intra_padding != 0:
|
||||
if itemsize > 1 or (shape is not None and _prod(shape) > 1):
|
||||
# Inject internal padding to the end of the sub-item
|
||||
value = _add_trailing_padding(value, intra_padding)
|
||||
else:
|
||||
# We can postpone the injection of internal padding,
|
||||
# as the item appears at most once
|
||||
extra_offset += intra_padding
|
||||
|
||||
# Update common alignment
|
||||
common_alignment = _lcm(align, common_alignment)
|
||||
|
||||
# Convert itemsize to sub-array
|
||||
if itemsize != 1:
|
||||
value = dtype((value, (itemsize,)))
|
||||
|
||||
# Sub-arrays (2)
|
||||
if shape is not None:
|
||||
value = dtype((value, shape))
|
||||
|
||||
# Field name
|
||||
if stream.consume(':'):
|
||||
name = stream.consume_until(':')
|
||||
else:
|
||||
name = None
|
||||
|
||||
if not (is_padding and name is None):
|
||||
if name is not None and name in field_spec['names']:
|
||||
raise RuntimeError(
|
||||
f"Duplicate field name '{name}' in PEP3118 format"
|
||||
)
|
||||
field_spec['names'].append(name)
|
||||
field_spec['formats'].append(value)
|
||||
field_spec['offsets'].append(offset)
|
||||
|
||||
offset += value.itemsize
|
||||
offset += extra_offset
|
||||
|
||||
field_spec['itemsize'] = offset
|
||||
|
||||
# extra final padding for aligned types
|
||||
if stream.byteorder == '@':
|
||||
field_spec['itemsize'] += (-offset) % common_alignment
|
||||
|
||||
# Check if this was a simple 1-item type, and unwrap it
|
||||
if (field_spec['names'] == [None]
|
||||
and field_spec['offsets'][0] == 0
|
||||
and field_spec['itemsize'] == field_spec['formats'][0].itemsize
|
||||
and not is_subdtype):
|
||||
ret = field_spec['formats'][0]
|
||||
else:
|
||||
_fix_names(field_spec)
|
||||
ret = dtype(field_spec)
|
||||
|
||||
# Finished
|
||||
return ret, common_alignment
|
||||
|
||||
def _fix_names(field_spec):
|
||||
""" Replace names which are None with the next unused f%d name """
|
||||
names = field_spec['names']
|
||||
for i, name in enumerate(names):
|
||||
if name is not None:
|
||||
continue
|
||||
|
||||
j = 0
|
||||
while True:
|
||||
name = f'f{j}'
|
||||
if name not in names:
|
||||
break
|
||||
j = j + 1
|
||||
names[i] = name
|
||||
|
||||
def _add_trailing_padding(value, padding):
|
||||
"""Inject the specified number of padding bytes at the end of a dtype"""
|
||||
if value.fields is None:
|
||||
field_spec = dict(
|
||||
names=['f0'],
|
||||
formats=[value],
|
||||
offsets=[0],
|
||||
itemsize=value.itemsize
|
||||
)
|
||||
else:
|
||||
fields = value.fields
|
||||
names = value.names
|
||||
field_spec = dict(
|
||||
names=names,
|
||||
formats=[fields[name][0] for name in names],
|
||||
offsets=[fields[name][1] for name in names],
|
||||
itemsize=value.itemsize
|
||||
)
|
||||
|
||||
field_spec['itemsize'] += padding
|
||||
return dtype(field_spec)
|
||||
|
||||
def _prod(a):
|
||||
p = 1
|
||||
for x in a:
|
||||
p *= x
|
||||
return p
|
||||
|
||||
def _gcd(a, b):
|
||||
"""Calculate the greatest common divisor of a and b"""
|
||||
while b:
|
||||
a, b = b, a % b
|
||||
return a
|
||||
|
||||
def _lcm(a, b):
|
||||
return a // _gcd(a, b) * b
|
||||
|
||||
def array_ufunc_errmsg_formatter(dummy, ufunc, method, *inputs, **kwargs):
|
||||
""" Format the error message for when __array_ufunc__ gives up. """
|
||||
args_string = ', '.join(['{!r}'.format(arg) for arg in inputs] +
|
||||
['{}={!r}'.format(k, v)
|
||||
for k, v in kwargs.items()])
|
||||
args = inputs + kwargs.get('out', ())
|
||||
types_string = ', '.join(repr(type(arg).__name__) for arg in args)
|
||||
return ('operand type(s) all returned NotImplemented from '
|
||||
'__array_ufunc__({!r}, {!r}, {}): {}'
|
||||
.format(ufunc, method, args_string, types_string))
|
||||
|
||||
|
||||
def array_function_errmsg_formatter(public_api, types):
|
||||
""" Format the error message for when __array_ufunc__ gives up. """
|
||||
func_name = '{}.{}'.format(public_api.__module__, public_api.__name__)
|
||||
return ("no implementation found for '{}' on types that implement "
|
||||
'__array_function__: {}'.format(func_name, list(types)))
|
||||
|
||||
|
||||
def _ufunc_doc_signature_formatter(ufunc):
|
||||
"""
|
||||
Builds a signature string which resembles PEP 457
|
||||
|
||||
This is used to construct the first line of the docstring
|
||||
"""
|
||||
|
||||
# input arguments are simple
|
||||
if ufunc.nin == 1:
|
||||
in_args = 'x'
|
||||
else:
|
||||
in_args = ', '.join(f'x{i+1}' for i in range(ufunc.nin))
|
||||
|
||||
# output arguments are both keyword or positional
|
||||
if ufunc.nout == 0:
|
||||
out_args = ', /, out=()'
|
||||
elif ufunc.nout == 1:
|
||||
out_args = ', /, out=None'
|
||||
else:
|
||||
out_args = '[, {positional}], / [, out={default}]'.format(
|
||||
positional=', '.join(
|
||||
'out{}'.format(i+1) for i in range(ufunc.nout)),
|
||||
default=repr((None,)*ufunc.nout)
|
||||
)
|
||||
|
||||
# keyword only args depend on whether this is a gufunc
|
||||
kwargs = (
|
||||
", casting='same_kind'"
|
||||
", order='K'"
|
||||
", dtype=None"
|
||||
", subok=True"
|
||||
)
|
||||
|
||||
# NOTE: gufuncs may or may not support the `axis` parameter
|
||||
if ufunc.signature is None:
|
||||
kwargs = f", where=True{kwargs}[, signature]"
|
||||
else:
|
||||
kwargs += "[, signature, axes, axis]"
|
||||
|
||||
# join all the parts together
|
||||
return '{name}({in_args}{out_args}, *{kwargs})'.format(
|
||||
name=ufunc.__name__,
|
||||
in_args=in_args,
|
||||
out_args=out_args,
|
||||
kwargs=kwargs
|
||||
)
|
||||
|
||||
|
||||
def npy_ctypes_check(cls):
|
||||
# determine if a class comes from ctypes, in order to work around
|
||||
# a bug in the buffer protocol for those objects, bpo-10746
|
||||
try:
|
||||
# ctypes class are new-style, so have an __mro__. This probably fails
|
||||
# for ctypes classes with multiple inheritance.
|
||||
if IS_PYPY:
|
||||
# (..., _ctypes.basics._CData, Bufferable, object)
|
||||
ctype_base = cls.__mro__[-3]
|
||||
else:
|
||||
# # (..., _ctypes._CData, object)
|
||||
ctype_base = cls.__mro__[-2]
|
||||
# right now, they're part of the _ctypes module
|
||||
return '_ctypes' in ctype_base.__module__
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
# used to handle the _NoValue default argument for na_object
|
||||
# in the C implementation of the __reduce__ method for stringdtype
|
||||
def _convert_to_stringdtype_kwargs(coerce, na_object=_NoValue):
|
||||
if na_object is _NoValue:
|
||||
return StringDType(coerce=coerce)
|
||||
return StringDType(coerce=coerce, na_object=na_object)
|
||||
@ -0,0 +1,30 @@
|
||||
from typing import Any, TypeVar, overload, Generic
|
||||
import ctypes as ct
|
||||
|
||||
from numpy.typing import NDArray
|
||||
from numpy.ctypeslib import c_intp
|
||||
|
||||
_CastT = TypeVar("_CastT", bound=ct._CanCastTo) # Copied from `ctypes.cast`
|
||||
_CT = TypeVar("_CT", bound=ct._CData)
|
||||
_PT = TypeVar("_PT", bound=int)
|
||||
|
||||
# TODO: Let the likes of `shape_as` and `strides_as` return `None`
|
||||
# for 0D arrays once we've got shape-support
|
||||
|
||||
class _ctypes(Generic[_PT]):
|
||||
@overload
|
||||
def __new__(cls, array: NDArray[Any], ptr: None = ...) -> _ctypes[None]: ...
|
||||
@overload
|
||||
def __new__(cls, array: NDArray[Any], ptr: _PT) -> _ctypes[_PT]: ...
|
||||
@property
|
||||
def data(self) -> _PT: ...
|
||||
@property
|
||||
def shape(self) -> ct.Array[c_intp]: ...
|
||||
@property
|
||||
def strides(self) -> ct.Array[c_intp]: ...
|
||||
@property
|
||||
def _as_parameter_(self) -> ct.c_void_p: ...
|
||||
|
||||
def data_as(self, obj: type[_CastT]) -> _CastT: ...
|
||||
def shape_as(self, obj: type[_CT]) -> ct.Array[_CT]: ...
|
||||
def strides_as(self, obj: type[_CT]) -> ct.Array[_CT]: ...
|
||||
@ -0,0 +1,356 @@
|
||||
"""
|
||||
Machine arithmetic - determine the parameters of the
|
||||
floating-point arithmetic system
|
||||
|
||||
Author: Pearu Peterson, September 2003
|
||||
|
||||
"""
|
||||
__all__ = ['MachAr']
|
||||
|
||||
from .fromnumeric import any
|
||||
from ._ufunc_config import errstate
|
||||
from .._utils import set_module
|
||||
|
||||
# Need to speed this up...especially for longdouble
|
||||
|
||||
# Deprecated 2021-10-20, NumPy 1.22
|
||||
class MachAr:
|
||||
"""
|
||||
Diagnosing machine parameters.
|
||||
|
||||
Attributes
|
||||
----------
|
||||
ibeta : int
|
||||
Radix in which numbers are represented.
|
||||
it : int
|
||||
Number of base-`ibeta` digits in the floating point mantissa M.
|
||||
machep : int
|
||||
Exponent of the smallest (most negative) power of `ibeta` that,
|
||||
added to 1.0, gives something different from 1.0
|
||||
eps : float
|
||||
Floating-point number ``beta**machep`` (floating point precision)
|
||||
negep : int
|
||||
Exponent of the smallest power of `ibeta` that, subtracted
|
||||
from 1.0, gives something different from 1.0.
|
||||
epsneg : float
|
||||
Floating-point number ``beta**negep``.
|
||||
iexp : int
|
||||
Number of bits in the exponent (including its sign and bias).
|
||||
minexp : int
|
||||
Smallest (most negative) power of `ibeta` consistent with there
|
||||
being no leading zeros in the mantissa.
|
||||
xmin : float
|
||||
Floating-point number ``beta**minexp`` (the smallest [in
|
||||
magnitude] positive floating point number with full precision).
|
||||
maxexp : int
|
||||
Smallest (positive) power of `ibeta` that causes overflow.
|
||||
xmax : float
|
||||
``(1-epsneg) * beta**maxexp`` (the largest [in magnitude]
|
||||
usable floating value).
|
||||
irnd : int
|
||||
In ``range(6)``, information on what kind of rounding is done
|
||||
in addition, and on how underflow is handled.
|
||||
ngrd : int
|
||||
Number of 'guard digits' used when truncating the product
|
||||
of two mantissas to fit the representation.
|
||||
epsilon : float
|
||||
Same as `eps`.
|
||||
tiny : float
|
||||
An alias for `smallest_normal`, kept for backwards compatibility.
|
||||
huge : float
|
||||
Same as `xmax`.
|
||||
precision : float
|
||||
``- int(-log10(eps))``
|
||||
resolution : float
|
||||
``- 10**(-precision)``
|
||||
smallest_normal : float
|
||||
The smallest positive floating point number with 1 as leading bit in
|
||||
the mantissa following IEEE-754. Same as `xmin`.
|
||||
smallest_subnormal : float
|
||||
The smallest positive floating point number with 0 as leading bit in
|
||||
the mantissa following IEEE-754.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
float_conv : function, optional
|
||||
Function that converts an integer or integer array to a float
|
||||
or float array. Default is `float`.
|
||||
int_conv : function, optional
|
||||
Function that converts a float or float array to an integer or
|
||||
integer array. Default is `int`.
|
||||
float_to_float : function, optional
|
||||
Function that converts a float array to float. Default is `float`.
|
||||
Note that this does not seem to do anything useful in the current
|
||||
implementation.
|
||||
float_to_str : function, optional
|
||||
Function that converts a single float to a string. Default is
|
||||
``lambda v:'%24.16e' %v``.
|
||||
title : str, optional
|
||||
Title that is printed in the string representation of `MachAr`.
|
||||
|
||||
See Also
|
||||
--------
|
||||
finfo : Machine limits for floating point types.
|
||||
iinfo : Machine limits for integer types.
|
||||
|
||||
References
|
||||
----------
|
||||
.. [1] Press, Teukolsky, Vetterling and Flannery,
|
||||
"Numerical Recipes in C++," 2nd ed,
|
||||
Cambridge University Press, 2002, p. 31.
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, float_conv=float,int_conv=int,
|
||||
float_to_float=float,
|
||||
float_to_str=lambda v:'%24.16e' % v,
|
||||
title='Python floating point number'):
|
||||
"""
|
||||
|
||||
float_conv - convert integer to float (array)
|
||||
int_conv - convert float (array) to integer
|
||||
float_to_float - convert float array to float
|
||||
float_to_str - convert array float to str
|
||||
title - description of used floating point numbers
|
||||
|
||||
"""
|
||||
# We ignore all errors here because we are purposely triggering
|
||||
# underflow to detect the properties of the runninng arch.
|
||||
with errstate(under='ignore'):
|
||||
self._do_init(float_conv, int_conv, float_to_float, float_to_str, title)
|
||||
|
||||
def _do_init(self, float_conv, int_conv, float_to_float, float_to_str, title):
|
||||
max_iterN = 10000
|
||||
msg = "Did not converge after %d tries with %s"
|
||||
one = float_conv(1)
|
||||
two = one + one
|
||||
zero = one - one
|
||||
|
||||
# Do we really need to do this? Aren't they 2 and 2.0?
|
||||
# Determine ibeta and beta
|
||||
a = one
|
||||
for _ in range(max_iterN):
|
||||
a = a + a
|
||||
temp = a + one
|
||||
temp1 = temp - a
|
||||
if any(temp1 - one != zero):
|
||||
break
|
||||
else:
|
||||
raise RuntimeError(msg % (_, one.dtype))
|
||||
b = one
|
||||
for _ in range(max_iterN):
|
||||
b = b + b
|
||||
temp = a + b
|
||||
itemp = int_conv(temp-a)
|
||||
if any(itemp != 0):
|
||||
break
|
||||
else:
|
||||
raise RuntimeError(msg % (_, one.dtype))
|
||||
ibeta = itemp
|
||||
beta = float_conv(ibeta)
|
||||
|
||||
# Determine it and irnd
|
||||
it = -1
|
||||
b = one
|
||||
for _ in range(max_iterN):
|
||||
it = it + 1
|
||||
b = b * beta
|
||||
temp = b + one
|
||||
temp1 = temp - b
|
||||
if any(temp1 - one != zero):
|
||||
break
|
||||
else:
|
||||
raise RuntimeError(msg % (_, one.dtype))
|
||||
|
||||
betah = beta / two
|
||||
a = one
|
||||
for _ in range(max_iterN):
|
||||
a = a + a
|
||||
temp = a + one
|
||||
temp1 = temp - a
|
||||
if any(temp1 - one != zero):
|
||||
break
|
||||
else:
|
||||
raise RuntimeError(msg % (_, one.dtype))
|
||||
temp = a + betah
|
||||
irnd = 0
|
||||
if any(temp-a != zero):
|
||||
irnd = 1
|
||||
tempa = a + beta
|
||||
temp = tempa + betah
|
||||
if irnd == 0 and any(temp-tempa != zero):
|
||||
irnd = 2
|
||||
|
||||
# Determine negep and epsneg
|
||||
negep = it + 3
|
||||
betain = one / beta
|
||||
a = one
|
||||
for i in range(negep):
|
||||
a = a * betain
|
||||
b = a
|
||||
for _ in range(max_iterN):
|
||||
temp = one - a
|
||||
if any(temp-one != zero):
|
||||
break
|
||||
a = a * beta
|
||||
negep = negep - 1
|
||||
# Prevent infinite loop on PPC with gcc 4.0:
|
||||
if negep < 0:
|
||||
raise RuntimeError("could not determine machine tolerance "
|
||||
"for 'negep', locals() -> %s" % (locals()))
|
||||
else:
|
||||
raise RuntimeError(msg % (_, one.dtype))
|
||||
negep = -negep
|
||||
epsneg = a
|
||||
|
||||
# Determine machep and eps
|
||||
machep = - it - 3
|
||||
a = b
|
||||
|
||||
for _ in range(max_iterN):
|
||||
temp = one + a
|
||||
if any(temp-one != zero):
|
||||
break
|
||||
a = a * beta
|
||||
machep = machep + 1
|
||||
else:
|
||||
raise RuntimeError(msg % (_, one.dtype))
|
||||
eps = a
|
||||
|
||||
# Determine ngrd
|
||||
ngrd = 0
|
||||
temp = one + eps
|
||||
if irnd == 0 and any(temp*one - one != zero):
|
||||
ngrd = 1
|
||||
|
||||
# Determine iexp
|
||||
i = 0
|
||||
k = 1
|
||||
z = betain
|
||||
t = one + eps
|
||||
nxres = 0
|
||||
for _ in range(max_iterN):
|
||||
y = z
|
||||
z = y*y
|
||||
a = z*one # Check here for underflow
|
||||
temp = z*t
|
||||
if any(a+a == zero) or any(abs(z) >= y):
|
||||
break
|
||||
temp1 = temp * betain
|
||||
if any(temp1*beta == z):
|
||||
break
|
||||
i = i + 1
|
||||
k = k + k
|
||||
else:
|
||||
raise RuntimeError(msg % (_, one.dtype))
|
||||
if ibeta != 10:
|
||||
iexp = i + 1
|
||||
mx = k + k
|
||||
else:
|
||||
iexp = 2
|
||||
iz = ibeta
|
||||
while k >= iz:
|
||||
iz = iz * ibeta
|
||||
iexp = iexp + 1
|
||||
mx = iz + iz - 1
|
||||
|
||||
# Determine minexp and xmin
|
||||
for _ in range(max_iterN):
|
||||
xmin = y
|
||||
y = y * betain
|
||||
a = y * one
|
||||
temp = y * t
|
||||
if any((a + a) != zero) and any(abs(y) < xmin):
|
||||
k = k + 1
|
||||
temp1 = temp * betain
|
||||
if any(temp1*beta == y) and any(temp != y):
|
||||
nxres = 3
|
||||
xmin = y
|
||||
break
|
||||
else:
|
||||
break
|
||||
else:
|
||||
raise RuntimeError(msg % (_, one.dtype))
|
||||
minexp = -k
|
||||
|
||||
# Determine maxexp, xmax
|
||||
if mx <= k + k - 3 and ibeta != 10:
|
||||
mx = mx + mx
|
||||
iexp = iexp + 1
|
||||
maxexp = mx + minexp
|
||||
irnd = irnd + nxres
|
||||
if irnd >= 2:
|
||||
maxexp = maxexp - 2
|
||||
i = maxexp + minexp
|
||||
if ibeta == 2 and not i:
|
||||
maxexp = maxexp - 1
|
||||
if i > 20:
|
||||
maxexp = maxexp - 1
|
||||
if any(a != y):
|
||||
maxexp = maxexp - 2
|
||||
xmax = one - epsneg
|
||||
if any(xmax*one != xmax):
|
||||
xmax = one - beta*epsneg
|
||||
xmax = xmax / (xmin*beta*beta*beta)
|
||||
i = maxexp + minexp + 3
|
||||
for j in range(i):
|
||||
if ibeta == 2:
|
||||
xmax = xmax + xmax
|
||||
else:
|
||||
xmax = xmax * beta
|
||||
|
||||
smallest_subnormal = abs(xmin / beta ** (it))
|
||||
|
||||
self.ibeta = ibeta
|
||||
self.it = it
|
||||
self.negep = negep
|
||||
self.epsneg = float_to_float(epsneg)
|
||||
self._str_epsneg = float_to_str(epsneg)
|
||||
self.machep = machep
|
||||
self.eps = float_to_float(eps)
|
||||
self._str_eps = float_to_str(eps)
|
||||
self.ngrd = ngrd
|
||||
self.iexp = iexp
|
||||
self.minexp = minexp
|
||||
self.xmin = float_to_float(xmin)
|
||||
self._str_xmin = float_to_str(xmin)
|
||||
self.maxexp = maxexp
|
||||
self.xmax = float_to_float(xmax)
|
||||
self._str_xmax = float_to_str(xmax)
|
||||
self.irnd = irnd
|
||||
|
||||
self.title = title
|
||||
# Commonly used parameters
|
||||
self.epsilon = self.eps
|
||||
self.tiny = self.xmin
|
||||
self.huge = self.xmax
|
||||
self.smallest_normal = self.xmin
|
||||
self._str_smallest_normal = float_to_str(self.xmin)
|
||||
self.smallest_subnormal = float_to_float(smallest_subnormal)
|
||||
self._str_smallest_subnormal = float_to_str(smallest_subnormal)
|
||||
|
||||
import math
|
||||
self.precision = int(-math.log10(float_to_float(self.eps)))
|
||||
ten = two + two + two + two + two
|
||||
resolution = ten ** (-self.precision)
|
||||
self.resolution = float_to_float(resolution)
|
||||
self._str_resolution = float_to_str(resolution)
|
||||
|
||||
def __str__(self):
|
||||
fmt = (
|
||||
'Machine parameters for %(title)s\n'
|
||||
'---------------------------------------------------------------------\n'
|
||||
'ibeta=%(ibeta)s it=%(it)s iexp=%(iexp)s ngrd=%(ngrd)s irnd=%(irnd)s\n'
|
||||
'machep=%(machep)s eps=%(_str_eps)s (beta**machep == epsilon)\n'
|
||||
'negep =%(negep)s epsneg=%(_str_epsneg)s (beta**epsneg)\n'
|
||||
'minexp=%(minexp)s xmin=%(_str_xmin)s (beta**minexp == tiny)\n'
|
||||
'maxexp=%(maxexp)s xmax=%(_str_xmax)s ((1-epsneg)*beta**maxexp == huge)\n'
|
||||
'smallest_normal=%(smallest_normal)s '
|
||||
'smallest_subnormal=%(smallest_subnormal)s\n'
|
||||
'---------------------------------------------------------------------\n'
|
||||
)
|
||||
return fmt % self.__dict__
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(MachAr())
|
||||
@ -0,0 +1,251 @@
|
||||
"""
|
||||
Array methods which are called by both the C-code for the method
|
||||
and the Python code for the NumPy-namespace function
|
||||
|
||||
"""
|
||||
import os
|
||||
import pickle
|
||||
import warnings
|
||||
from contextlib import nullcontext
|
||||
|
||||
from numpy._core import multiarray as mu
|
||||
from numpy._core import umath as um
|
||||
from numpy._core.multiarray import asanyarray
|
||||
from numpy._core import numerictypes as nt
|
||||
from numpy._core import _exceptions
|
||||
from numpy._core._ufunc_config import _no_nep50_warning
|
||||
from numpy._globals import _NoValue
|
||||
|
||||
# save those O(100) nanoseconds!
|
||||
bool_dt = mu.dtype("bool")
|
||||
umr_maximum = um.maximum.reduce
|
||||
umr_minimum = um.minimum.reduce
|
||||
umr_sum = um.add.reduce
|
||||
umr_prod = um.multiply.reduce
|
||||
umr_bitwise_count = um.bitwise_count
|
||||
umr_any = um.logical_or.reduce
|
||||
umr_all = um.logical_and.reduce
|
||||
|
||||
# Complex types to -> (2,)float view for fast-path computation in _var()
|
||||
_complex_to_float = {
|
||||
nt.dtype(nt.csingle) : nt.dtype(nt.single),
|
||||
nt.dtype(nt.cdouble) : nt.dtype(nt.double),
|
||||
}
|
||||
# Special case for windows: ensure double takes precedence
|
||||
if nt.dtype(nt.longdouble) != nt.dtype(nt.double):
|
||||
_complex_to_float.update({
|
||||
nt.dtype(nt.clongdouble) : nt.dtype(nt.longdouble),
|
||||
})
|
||||
|
||||
# avoid keyword arguments to speed up parsing, saves about 15%-20% for very
|
||||
# small reductions
|
||||
def _amax(a, axis=None, out=None, keepdims=False,
|
||||
initial=_NoValue, where=True):
|
||||
return umr_maximum(a, axis, None, out, keepdims, initial, where)
|
||||
|
||||
def _amin(a, axis=None, out=None, keepdims=False,
|
||||
initial=_NoValue, where=True):
|
||||
return umr_minimum(a, axis, None, out, keepdims, initial, where)
|
||||
|
||||
def _sum(a, axis=None, dtype=None, out=None, keepdims=False,
|
||||
initial=_NoValue, where=True):
|
||||
return umr_sum(a, axis, dtype, out, keepdims, initial, where)
|
||||
|
||||
def _prod(a, axis=None, dtype=None, out=None, keepdims=False,
|
||||
initial=_NoValue, where=True):
|
||||
return umr_prod(a, axis, dtype, out, keepdims, initial, where)
|
||||
|
||||
def _any(a, axis=None, dtype=None, out=None, keepdims=False, *, where=True):
|
||||
# By default, return a boolean for any and all
|
||||
if dtype is None:
|
||||
dtype = bool_dt
|
||||
# Parsing keyword arguments is currently fairly slow, so avoid it for now
|
||||
if where is True:
|
||||
return umr_any(a, axis, dtype, out, keepdims)
|
||||
return umr_any(a, axis, dtype, out, keepdims, where=where)
|
||||
|
||||
def _all(a, axis=None, dtype=None, out=None, keepdims=False, *, where=True):
|
||||
# By default, return a boolean for any and all
|
||||
if dtype is None:
|
||||
dtype = bool_dt
|
||||
# Parsing keyword arguments is currently fairly slow, so avoid it for now
|
||||
if where is True:
|
||||
return umr_all(a, axis, dtype, out, keepdims)
|
||||
return umr_all(a, axis, dtype, out, keepdims, where=where)
|
||||
|
||||
def _count_reduce_items(arr, axis, keepdims=False, where=True):
|
||||
# fast-path for the default case
|
||||
if where is True:
|
||||
# no boolean mask given, calculate items according to axis
|
||||
if axis is None:
|
||||
axis = tuple(range(arr.ndim))
|
||||
elif not isinstance(axis, tuple):
|
||||
axis = (axis,)
|
||||
items = 1
|
||||
for ax in axis:
|
||||
items *= arr.shape[mu.normalize_axis_index(ax, arr.ndim)]
|
||||
items = nt.intp(items)
|
||||
else:
|
||||
# TODO: Optimize case when `where` is broadcast along a non-reduction
|
||||
# axis and full sum is more excessive than needed.
|
||||
|
||||
# guarded to protect circular imports
|
||||
from numpy.lib._stride_tricks_impl import broadcast_to
|
||||
# count True values in (potentially broadcasted) boolean mask
|
||||
items = umr_sum(broadcast_to(where, arr.shape), axis, nt.intp, None,
|
||||
keepdims)
|
||||
return items
|
||||
|
||||
def _clip(a, min=None, max=None, out=None, **kwargs):
|
||||
if min is None and max is None:
|
||||
raise ValueError("One of max or min must be given")
|
||||
|
||||
if min is None:
|
||||
return um.minimum(a, max, out=out, **kwargs)
|
||||
elif max is None:
|
||||
return um.maximum(a, min, out=out, **kwargs)
|
||||
else:
|
||||
return um.clip(a, min, max, out=out, **kwargs)
|
||||
|
||||
def _mean(a, axis=None, dtype=None, out=None, keepdims=False, *, where=True):
|
||||
arr = asanyarray(a)
|
||||
|
||||
is_float16_result = False
|
||||
|
||||
rcount = _count_reduce_items(arr, axis, keepdims=keepdims, where=where)
|
||||
if rcount == 0 if where is True else umr_any(rcount == 0, axis=None):
|
||||
warnings.warn("Mean of empty slice.", RuntimeWarning, stacklevel=2)
|
||||
|
||||
# Cast bool, unsigned int, and int to float64 by default
|
||||
if dtype is None:
|
||||
if issubclass(arr.dtype.type, (nt.integer, nt.bool)):
|
||||
dtype = mu.dtype('f8')
|
||||
elif issubclass(arr.dtype.type, nt.float16):
|
||||
dtype = mu.dtype('f4')
|
||||
is_float16_result = True
|
||||
|
||||
ret = umr_sum(arr, axis, dtype, out, keepdims, where=where)
|
||||
if isinstance(ret, mu.ndarray):
|
||||
with _no_nep50_warning():
|
||||
ret = um.true_divide(
|
||||
ret, rcount, out=ret, casting='unsafe', subok=False)
|
||||
if is_float16_result and out is None:
|
||||
ret = arr.dtype.type(ret)
|
||||
elif hasattr(ret, 'dtype'):
|
||||
if is_float16_result:
|
||||
ret = arr.dtype.type(ret / rcount)
|
||||
else:
|
||||
ret = ret.dtype.type(ret / rcount)
|
||||
else:
|
||||
ret = ret / rcount
|
||||
|
||||
return ret
|
||||
|
||||
def _var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False, *,
|
||||
where=True, mean=None):
|
||||
arr = asanyarray(a)
|
||||
|
||||
rcount = _count_reduce_items(arr, axis, keepdims=keepdims, where=where)
|
||||
# Make this warning show up on top.
|
||||
if ddof >= rcount if where is True else umr_any(ddof >= rcount, axis=None):
|
||||
warnings.warn("Degrees of freedom <= 0 for slice", RuntimeWarning,
|
||||
stacklevel=2)
|
||||
|
||||
# Cast bool, unsigned int, and int to float64 by default
|
||||
if dtype is None and issubclass(arr.dtype.type, (nt.integer, nt.bool)):
|
||||
dtype = mu.dtype('f8')
|
||||
|
||||
if mean is not None:
|
||||
arrmean = mean
|
||||
else:
|
||||
# Compute the mean.
|
||||
# Note that if dtype is not of inexact type then arraymean will
|
||||
# not be either.
|
||||
arrmean = umr_sum(arr, axis, dtype, keepdims=True, where=where)
|
||||
# The shape of rcount has to match arrmean to not change the shape of
|
||||
# out in broadcasting. Otherwise, it cannot be stored back to arrmean.
|
||||
if rcount.ndim == 0:
|
||||
# fast-path for default case when where is True
|
||||
div = rcount
|
||||
else:
|
||||
# matching rcount to arrmean when where is specified as array
|
||||
div = rcount.reshape(arrmean.shape)
|
||||
if isinstance(arrmean, mu.ndarray):
|
||||
with _no_nep50_warning():
|
||||
arrmean = um.true_divide(arrmean, div, out=arrmean,
|
||||
casting='unsafe', subok=False)
|
||||
elif hasattr(arrmean, "dtype"):
|
||||
arrmean = arrmean.dtype.type(arrmean / rcount)
|
||||
else:
|
||||
arrmean = arrmean / rcount
|
||||
|
||||
# Compute sum of squared deviations from mean
|
||||
# Note that x may not be inexact and that we need it to be an array,
|
||||
# not a scalar.
|
||||
x = asanyarray(arr - arrmean)
|
||||
|
||||
if issubclass(arr.dtype.type, (nt.floating, nt.integer)):
|
||||
x = um.multiply(x, x, out=x)
|
||||
# Fast-paths for built-in complex types
|
||||
elif x.dtype in _complex_to_float:
|
||||
xv = x.view(dtype=(_complex_to_float[x.dtype], (2,)))
|
||||
um.multiply(xv, xv, out=xv)
|
||||
x = um.add(xv[..., 0], xv[..., 1], out=x.real).real
|
||||
# Most general case; includes handling object arrays containing imaginary
|
||||
# numbers and complex types with non-native byteorder
|
||||
else:
|
||||
x = um.multiply(x, um.conjugate(x), out=x).real
|
||||
|
||||
ret = umr_sum(x, axis, dtype, out, keepdims=keepdims, where=where)
|
||||
|
||||
# Compute degrees of freedom and make sure it is not negative.
|
||||
rcount = um.maximum(rcount - ddof, 0)
|
||||
|
||||
# divide by degrees of freedom
|
||||
if isinstance(ret, mu.ndarray):
|
||||
with _no_nep50_warning():
|
||||
ret = um.true_divide(
|
||||
ret, rcount, out=ret, casting='unsafe', subok=False)
|
||||
elif hasattr(ret, 'dtype'):
|
||||
ret = ret.dtype.type(ret / rcount)
|
||||
else:
|
||||
ret = ret / rcount
|
||||
|
||||
return ret
|
||||
|
||||
def _std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False, *,
|
||||
where=True, mean=None):
|
||||
ret = _var(a, axis=axis, dtype=dtype, out=out, ddof=ddof,
|
||||
keepdims=keepdims, where=where, mean=mean)
|
||||
|
||||
if isinstance(ret, mu.ndarray):
|
||||
ret = um.sqrt(ret, out=ret)
|
||||
elif hasattr(ret, 'dtype'):
|
||||
ret = ret.dtype.type(um.sqrt(ret))
|
||||
else:
|
||||
ret = um.sqrt(ret)
|
||||
|
||||
return ret
|
||||
|
||||
def _ptp(a, axis=None, out=None, keepdims=False):
|
||||
return um.subtract(
|
||||
umr_maximum(a, axis, None, out, keepdims),
|
||||
umr_minimum(a, axis, None, None, keepdims),
|
||||
out
|
||||
)
|
||||
|
||||
def _dump(self, file, protocol=2):
|
||||
if hasattr(file, 'write'):
|
||||
ctx = nullcontext(file)
|
||||
else:
|
||||
ctx = open(os.fspath(file), "wb")
|
||||
with ctx as f:
|
||||
pickle.dump(self, f, protocol=protocol)
|
||||
|
||||
def _dumps(self, protocol=2):
|
||||
return pickle.dumps(self, protocol=protocol)
|
||||
|
||||
def _bitwise_count(a, out=None, *, where=True, casting='same_kind',
|
||||
order='K', dtype=None, subok=True):
|
||||
return umr_bitwise_count(a, out, where=where, casting=casting,
|
||||
order=order, dtype=dtype, subok=subok)
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,100 @@
|
||||
"""
|
||||
String-handling utilities to avoid locale-dependence.
|
||||
|
||||
Used primarily to generate type name aliases.
|
||||
"""
|
||||
# "import string" is costly to import!
|
||||
# Construct the translation tables directly
|
||||
# "A" = chr(65), "a" = chr(97)
|
||||
_all_chars = tuple(map(chr, range(256)))
|
||||
_ascii_upper = _all_chars[65:65+26]
|
||||
_ascii_lower = _all_chars[97:97+26]
|
||||
LOWER_TABLE = _all_chars[:65] + _ascii_lower + _all_chars[65+26:]
|
||||
UPPER_TABLE = _all_chars[:97] + _ascii_upper + _all_chars[97+26:]
|
||||
|
||||
|
||||
def english_lower(s):
|
||||
""" Apply English case rules to convert ASCII strings to all lower case.
|
||||
|
||||
This is an internal utility function to replace calls to str.lower() such
|
||||
that we can avoid changing behavior with changing locales. In particular,
|
||||
Turkish has distinct dotted and dotless variants of the Latin letter "I" in
|
||||
both lowercase and uppercase. Thus, "I".lower() != "i" in a "tr" locale.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
s : str
|
||||
|
||||
Returns
|
||||
-------
|
||||
lowered : str
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> from numpy._core.numerictypes import english_lower
|
||||
>>> english_lower('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_')
|
||||
'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789_'
|
||||
>>> english_lower('')
|
||||
''
|
||||
"""
|
||||
lowered = s.translate(LOWER_TABLE)
|
||||
return lowered
|
||||
|
||||
|
||||
def english_upper(s):
|
||||
""" Apply English case rules to convert ASCII strings to all upper case.
|
||||
|
||||
This is an internal utility function to replace calls to str.upper() such
|
||||
that we can avoid changing behavior with changing locales. In particular,
|
||||
Turkish has distinct dotted and dotless variants of the Latin letter "I" in
|
||||
both lowercase and uppercase. Thus, "i".upper() != "I" in a "tr" locale.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
s : str
|
||||
|
||||
Returns
|
||||
-------
|
||||
uppered : str
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> from numpy._core.numerictypes import english_upper
|
||||
>>> english_upper('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_')
|
||||
'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'
|
||||
>>> english_upper('')
|
||||
''
|
||||
"""
|
||||
uppered = s.translate(UPPER_TABLE)
|
||||
return uppered
|
||||
|
||||
|
||||
def english_capitalize(s):
|
||||
""" Apply English case rules to convert the first character of an ASCII
|
||||
string to upper case.
|
||||
|
||||
This is an internal utility function to replace calls to str.capitalize()
|
||||
such that we can avoid changing behavior with changing locales.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
s : str
|
||||
|
||||
Returns
|
||||
-------
|
||||
capitalized : str
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> from numpy._core.numerictypes import english_capitalize
|
||||
>>> english_capitalize('int8')
|
||||
'Int8'
|
||||
>>> english_capitalize('Int8')
|
||||
'Int8'
|
||||
>>> english_capitalize('')
|
||||
''
|
||||
"""
|
||||
if s:
|
||||
return english_upper(s[0]) + s[1:]
|
||||
else:
|
||||
return s
|
||||
Binary file not shown.
@ -0,0 +1,119 @@
|
||||
"""
|
||||
Due to compatibility, numpy has a very large number of different naming
|
||||
conventions for the scalar types (those subclassing from `numpy.generic`).
|
||||
This file produces a convoluted set of dictionaries mapping names to types,
|
||||
and sometimes other mappings too.
|
||||
|
||||
.. data:: allTypes
|
||||
A dictionary of names to types that will be exposed as attributes through
|
||||
``np._core.numerictypes.*``
|
||||
|
||||
.. data:: sctypeDict
|
||||
Similar to `allTypes`, but maps a broader set of aliases to their types.
|
||||
|
||||
.. data:: sctypes
|
||||
A dictionary keyed by a "type group" string, providing a list of types
|
||||
under that group.
|
||||
|
||||
"""
|
||||
|
||||
import numpy._core.multiarray as ma
|
||||
from numpy._core.multiarray import typeinfo, dtype
|
||||
|
||||
######################################
|
||||
# Building `sctypeDict` and `allTypes`
|
||||
######################################
|
||||
|
||||
sctypeDict = {}
|
||||
allTypes = {}
|
||||
c_names_dict = {}
|
||||
|
||||
_abstract_type_names = {
|
||||
"generic", "integer", "inexact", "floating", "number",
|
||||
"flexible", "character", "complexfloating", "unsignedinteger",
|
||||
"signedinteger"
|
||||
}
|
||||
|
||||
for _abstract_type_name in _abstract_type_names:
|
||||
allTypes[_abstract_type_name] = getattr(ma, _abstract_type_name)
|
||||
|
||||
for k, v in typeinfo.items():
|
||||
if k.startswith("NPY_") and v not in c_names_dict:
|
||||
c_names_dict[k[4:]] = v
|
||||
else:
|
||||
concrete_type = v.type
|
||||
allTypes[k] = concrete_type
|
||||
sctypeDict[k] = concrete_type
|
||||
|
||||
_aliases = {
|
||||
"double": "float64",
|
||||
"cdouble": "complex128",
|
||||
"single": "float32",
|
||||
"csingle": "complex64",
|
||||
"half": "float16",
|
||||
"bool_": "bool",
|
||||
# Default integer:
|
||||
"int_": "intp",
|
||||
"uint": "uintp",
|
||||
}
|
||||
|
||||
for k, v in _aliases.items():
|
||||
sctypeDict[k] = allTypes[v]
|
||||
allTypes[k] = allTypes[v]
|
||||
|
||||
# extra aliases are added only to `sctypeDict`
|
||||
# to support dtype name access, such as`np.dtype("float")`
|
||||
_extra_aliases = {
|
||||
"float": "float64",
|
||||
"complex": "complex128",
|
||||
"object": "object_",
|
||||
"bytes": "bytes_",
|
||||
"a": "bytes_",
|
||||
"int": "int_",
|
||||
"str": "str_",
|
||||
"unicode": "str_",
|
||||
}
|
||||
|
||||
for k, v in _extra_aliases.items():
|
||||
sctypeDict[k] = allTypes[v]
|
||||
|
||||
# include extended precision sized aliases
|
||||
for is_complex, full_name in [(False, "longdouble"), (True, "clongdouble")]:
|
||||
longdouble_type: type = allTypes[full_name]
|
||||
|
||||
bits: int = dtype(longdouble_type).itemsize * 8
|
||||
base_name: str = "complex" if is_complex else "float"
|
||||
extended_prec_name: str = f"{base_name}{bits}"
|
||||
if extended_prec_name not in allTypes:
|
||||
sctypeDict[extended_prec_name] = longdouble_type
|
||||
allTypes[extended_prec_name] = longdouble_type
|
||||
|
||||
|
||||
####################
|
||||
# Building `sctypes`
|
||||
####################
|
||||
|
||||
sctypes = {"int": set(), "uint": set(), "float": set(),
|
||||
"complex": set(), "others": set()}
|
||||
|
||||
for type_info in typeinfo.values():
|
||||
if type_info.kind in ["M", "m"]: # exclude timedelta and datetime
|
||||
continue
|
||||
|
||||
concrete_type = type_info.type
|
||||
|
||||
# find proper group for each concrete type
|
||||
for type_group, abstract_type in [
|
||||
("int", ma.signedinteger), ("uint", ma.unsignedinteger),
|
||||
("float", ma.floating), ("complex", ma.complexfloating),
|
||||
("others", ma.generic)
|
||||
]:
|
||||
if issubclass(concrete_type, abstract_type):
|
||||
sctypes[type_group].add(concrete_type)
|
||||
break
|
||||
|
||||
# sort sctype groups by bitsize
|
||||
for sctype_key in sctypes.keys():
|
||||
sctype_list = list(sctypes[sctype_key])
|
||||
sctype_list.sort(key=lambda x: dtype(x).itemsize)
|
||||
sctypes[sctype_key] = sctype_list
|
||||
@ -0,0 +1,3 @@
|
||||
from numpy import generic
|
||||
|
||||
sctypeDict: dict[int | str, type[generic]]
|
||||
@ -0,0 +1,472 @@
|
||||
"""
|
||||
Functions for changing global ufunc configuration
|
||||
|
||||
This provides helpers which wrap `_get_extobj_dict` and `_make_extobj`, and
|
||||
`_extobj_contextvar` from umath.
|
||||
"""
|
||||
import collections.abc
|
||||
import contextlib
|
||||
import contextvars
|
||||
import functools
|
||||
|
||||
from .._utils import set_module
|
||||
from .umath import _make_extobj, _get_extobj_dict, _extobj_contextvar
|
||||
|
||||
__all__ = [
|
||||
"seterr", "geterr", "setbufsize", "getbufsize", "seterrcall", "geterrcall",
|
||||
"errstate", '_no_nep50_warning'
|
||||
]
|
||||
|
||||
|
||||
@set_module('numpy')
|
||||
def seterr(all=None, divide=None, over=None, under=None, invalid=None):
|
||||
"""
|
||||
Set how floating-point errors are handled.
|
||||
|
||||
Note that operations on integer scalar types (such as `int16`) are
|
||||
handled like floating point, and are affected by these settings.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
all : {'ignore', 'warn', 'raise', 'call', 'print', 'log'}, optional
|
||||
Set treatment for all types of floating-point errors at once:
|
||||
|
||||
- ignore: Take no action when the exception occurs.
|
||||
- warn: Print a :exc:`RuntimeWarning` (via the Python `warnings`
|
||||
module).
|
||||
- raise: Raise a :exc:`FloatingPointError`.
|
||||
- call: Call a function specified using the `seterrcall` function.
|
||||
- print: Print a warning directly to ``stdout``.
|
||||
- log: Record error in a Log object specified by `seterrcall`.
|
||||
|
||||
The default is not to change the current behavior.
|
||||
divide : {'ignore', 'warn', 'raise', 'call', 'print', 'log'}, optional
|
||||
Treatment for division by zero.
|
||||
over : {'ignore', 'warn', 'raise', 'call', 'print', 'log'}, optional
|
||||
Treatment for floating-point overflow.
|
||||
under : {'ignore', 'warn', 'raise', 'call', 'print', 'log'}, optional
|
||||
Treatment for floating-point underflow.
|
||||
invalid : {'ignore', 'warn', 'raise', 'call', 'print', 'log'}, optional
|
||||
Treatment for invalid floating-point operation.
|
||||
|
||||
Returns
|
||||
-------
|
||||
old_settings : dict
|
||||
Dictionary containing the old settings.
|
||||
|
||||
See also
|
||||
--------
|
||||
seterrcall : Set a callback function for the 'call' mode.
|
||||
geterr, geterrcall, errstate
|
||||
|
||||
Notes
|
||||
-----
|
||||
The floating-point exceptions are defined in the IEEE 754 standard [1]_:
|
||||
|
||||
- Division by zero: infinite result obtained from finite numbers.
|
||||
- Overflow: result too large to be expressed.
|
||||
- Underflow: result so close to zero that some precision
|
||||
was lost.
|
||||
- Invalid operation: result is not an expressible number, typically
|
||||
indicates that a NaN was produced.
|
||||
|
||||
.. [1] https://en.wikipedia.org/wiki/IEEE_754
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> orig_settings = np.seterr(all='ignore') # seterr to known value
|
||||
>>> np.int16(32000) * np.int16(3)
|
||||
30464
|
||||
>>> np.seterr(over='raise')
|
||||
{'divide': 'ignore', 'over': 'ignore', 'under': 'ignore', 'invalid': 'ignore'}
|
||||
>>> old_settings = np.seterr(all='warn', over='raise')
|
||||
>>> np.int16(32000) * np.int16(3)
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
FloatingPointError: overflow encountered in scalar multiply
|
||||
|
||||
>>> old_settings = np.seterr(all='print')
|
||||
>>> np.geterr()
|
||||
{'divide': 'print', 'over': 'print', 'under': 'print', 'invalid': 'print'}
|
||||
>>> np.int16(32000) * np.int16(3)
|
||||
30464
|
||||
>>> np.seterr(**orig_settings) # restore original
|
||||
{'divide': 'print', 'over': 'print', 'under': 'print', 'invalid': 'print'}
|
||||
|
||||
"""
|
||||
|
||||
old = _get_extobj_dict()
|
||||
# The errstate doesn't include call and bufsize, so pop them:
|
||||
old.pop("call", None)
|
||||
old.pop("bufsize", None)
|
||||
|
||||
extobj = _make_extobj(
|
||||
all=all, divide=divide, over=over, under=under, invalid=invalid)
|
||||
_extobj_contextvar.set(extobj)
|
||||
return old
|
||||
|
||||
|
||||
@set_module('numpy')
|
||||
def geterr():
|
||||
"""
|
||||
Get the current way of handling floating-point errors.
|
||||
|
||||
Returns
|
||||
-------
|
||||
res : dict
|
||||
A dictionary with keys "divide", "over", "under", and "invalid",
|
||||
whose values are from the strings "ignore", "print", "log", "warn",
|
||||
"raise", and "call". The keys represent possible floating-point
|
||||
exceptions, and the values define how these exceptions are handled.
|
||||
|
||||
See Also
|
||||
--------
|
||||
geterrcall, seterr, seterrcall
|
||||
|
||||
Notes
|
||||
-----
|
||||
For complete documentation of the types of floating-point exceptions and
|
||||
treatment options, see `seterr`.
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> np.geterr()
|
||||
{'divide': 'warn', 'over': 'warn', 'under': 'ignore', 'invalid': 'warn'}
|
||||
>>> np.arange(3.) / np.arange(3.) # doctest: +SKIP
|
||||
array([nan, 1., 1.])
|
||||
RuntimeWarning: invalid value encountered in divide
|
||||
|
||||
>>> oldsettings = np.seterr(all='warn', invalid='raise')
|
||||
>>> np.geterr()
|
||||
{'divide': 'warn', 'over': 'warn', 'under': 'warn', 'invalid': 'raise'}
|
||||
>>> np.arange(3.) / np.arange(3.)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
FloatingPointError: invalid value encountered in divide
|
||||
>>> oldsettings = np.seterr(**oldsettings) # restore original
|
||||
|
||||
"""
|
||||
res = _get_extobj_dict()
|
||||
# The "geterr" doesn't include call and bufsize,:
|
||||
res.pop("call", None)
|
||||
res.pop("bufsize", None)
|
||||
return res
|
||||
|
||||
|
||||
@set_module('numpy')
|
||||
def setbufsize(size):
|
||||
"""
|
||||
Set the size of the buffer used in ufuncs.
|
||||
|
||||
.. versionchanged:: 2.0
|
||||
The scope of setting the buffer is tied to the `numpy.errstate`
|
||||
context. Exiting a ``with errstate():`` will also restore the bufsize.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
size : int
|
||||
Size of buffer.
|
||||
|
||||
"""
|
||||
old = _get_extobj_dict()["bufsize"]
|
||||
extobj = _make_extobj(bufsize=size)
|
||||
_extobj_contextvar.set(extobj)
|
||||
return old
|
||||
|
||||
|
||||
@set_module('numpy')
|
||||
def getbufsize():
|
||||
"""
|
||||
Return the size of the buffer used in ufuncs.
|
||||
|
||||
Returns
|
||||
-------
|
||||
getbufsize : int
|
||||
Size of ufunc buffer in bytes.
|
||||
|
||||
"""
|
||||
return _get_extobj_dict()["bufsize"]
|
||||
|
||||
|
||||
@set_module('numpy')
|
||||
def seterrcall(func):
|
||||
"""
|
||||
Set the floating-point error callback function or log object.
|
||||
|
||||
There are two ways to capture floating-point error messages. The first
|
||||
is to set the error-handler to 'call', using `seterr`. Then, set
|
||||
the function to call using this function.
|
||||
|
||||
The second is to set the error-handler to 'log', using `seterr`.
|
||||
Floating-point errors then trigger a call to the 'write' method of
|
||||
the provided object.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
func : callable f(err, flag) or object with write method
|
||||
Function to call upon floating-point errors ('call'-mode) or
|
||||
object whose 'write' method is used to log such message ('log'-mode).
|
||||
|
||||
The call function takes two arguments. The first is a string describing
|
||||
the type of error (such as "divide by zero", "overflow", "underflow",
|
||||
or "invalid value"), and the second is the status flag. The flag is a
|
||||
byte, whose four least-significant bits indicate the type of error, one
|
||||
of "divide", "over", "under", "invalid"::
|
||||
|
||||
[0 0 0 0 divide over under invalid]
|
||||
|
||||
In other words, ``flags = divide + 2*over + 4*under + 8*invalid``.
|
||||
|
||||
If an object is provided, its write method should take one argument,
|
||||
a string.
|
||||
|
||||
Returns
|
||||
-------
|
||||
h : callable, log instance or None
|
||||
The old error handler.
|
||||
|
||||
See Also
|
||||
--------
|
||||
seterr, geterr, geterrcall
|
||||
|
||||
Examples
|
||||
--------
|
||||
Callback upon error:
|
||||
|
||||
>>> def err_handler(type, flag):
|
||||
... print("Floating point error (%s), with flag %s" % (type, flag))
|
||||
...
|
||||
|
||||
>>> orig_handler = np.seterrcall(err_handler)
|
||||
>>> orig_err = np.seterr(all='call')
|
||||
|
||||
>>> np.array([1, 2, 3]) / 0.0
|
||||
Floating point error (divide by zero), with flag 1
|
||||
array([inf, inf, inf])
|
||||
|
||||
>>> np.seterrcall(orig_handler)
|
||||
<function err_handler at 0x...>
|
||||
>>> np.seterr(**orig_err)
|
||||
{'divide': 'call', 'over': 'call', 'under': 'call', 'invalid': 'call'}
|
||||
|
||||
Log error message:
|
||||
|
||||
>>> class Log:
|
||||
... def write(self, msg):
|
||||
... print("LOG: %s" % msg)
|
||||
...
|
||||
|
||||
>>> log = Log()
|
||||
>>> saved_handler = np.seterrcall(log)
|
||||
>>> save_err = np.seterr(all='log')
|
||||
|
||||
>>> np.array([1, 2, 3]) / 0.0
|
||||
LOG: Warning: divide by zero encountered in divide
|
||||
array([inf, inf, inf])
|
||||
|
||||
>>> np.seterrcall(orig_handler)
|
||||
<numpy.Log object at 0x...>
|
||||
>>> np.seterr(**orig_err)
|
||||
{'divide': 'log', 'over': 'log', 'under': 'log', 'invalid': 'log'}
|
||||
|
||||
"""
|
||||
old = _get_extobj_dict()["call"]
|
||||
extobj = _make_extobj(call=func)
|
||||
_extobj_contextvar.set(extobj)
|
||||
return old
|
||||
|
||||
|
||||
@set_module('numpy')
|
||||
def geterrcall():
|
||||
"""
|
||||
Return the current callback function used on floating-point errors.
|
||||
|
||||
When the error handling for a floating-point error (one of "divide",
|
||||
"over", "under", or "invalid") is set to 'call' or 'log', the function
|
||||
that is called or the log instance that is written to is returned by
|
||||
`geterrcall`. This function or log instance has been set with
|
||||
`seterrcall`.
|
||||
|
||||
Returns
|
||||
-------
|
||||
errobj : callable, log instance or None
|
||||
The current error handler. If no handler was set through `seterrcall`,
|
||||
``None`` is returned.
|
||||
|
||||
See Also
|
||||
--------
|
||||
seterrcall, seterr, geterr
|
||||
|
||||
Notes
|
||||
-----
|
||||
For complete documentation of the types of floating-point exceptions and
|
||||
treatment options, see `seterr`.
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> np.geterrcall() # we did not yet set a handler, returns None
|
||||
|
||||
>>> orig_settings = np.seterr(all='call')
|
||||
>>> def err_handler(type, flag):
|
||||
... print("Floating point error (%s), with flag %s" % (type, flag))
|
||||
>>> old_handler = np.seterrcall(err_handler)
|
||||
>>> np.array([1, 2, 3]) / 0.0
|
||||
Floating point error (divide by zero), with flag 1
|
||||
array([inf, inf, inf])
|
||||
|
||||
>>> cur_handler = np.geterrcall()
|
||||
>>> cur_handler is err_handler
|
||||
True
|
||||
>>> old_settings = np.seterr(**orig_settings) # restore original
|
||||
>>> old_handler = np.seterrcall(None) # restore original
|
||||
|
||||
"""
|
||||
return _get_extobj_dict()["call"]
|
||||
|
||||
|
||||
class _unspecified:
|
||||
pass
|
||||
|
||||
|
||||
_Unspecified = _unspecified()
|
||||
|
||||
|
||||
@set_module('numpy')
|
||||
class errstate:
|
||||
"""
|
||||
errstate(**kwargs)
|
||||
|
||||
Context manager for floating-point error handling.
|
||||
|
||||
Using an instance of `errstate` as a context manager allows statements in
|
||||
that context to execute with a known error handling behavior. Upon entering
|
||||
the context the error handling is set with `seterr` and `seterrcall`, and
|
||||
upon exiting it is reset to what it was before.
|
||||
|
||||
.. versionchanged:: 1.17.0
|
||||
`errstate` is also usable as a function decorator, saving
|
||||
a level of indentation if an entire function is wrapped.
|
||||
|
||||
.. versionchanged:: 2.0
|
||||
`errstate` is now fully thread and asyncio safe, but may not be
|
||||
entered more than once.
|
||||
It is not safe to decorate async functions using ``errstate``.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
kwargs : {divide, over, under, invalid}
|
||||
Keyword arguments. The valid keywords are the possible floating-point
|
||||
exceptions. Each keyword should have a string value that defines the
|
||||
treatment for the particular error. Possible values are
|
||||
{'ignore', 'warn', 'raise', 'call', 'print', 'log'}.
|
||||
|
||||
See Also
|
||||
--------
|
||||
seterr, geterr, seterrcall, geterrcall
|
||||
|
||||
Notes
|
||||
-----
|
||||
For complete documentation of the types of floating-point exceptions and
|
||||
treatment options, see `seterr`.
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> olderr = np.seterr(all='ignore') # Set error handling to known state.
|
||||
|
||||
>>> np.arange(3) / 0.
|
||||
array([nan, inf, inf])
|
||||
>>> with np.errstate(divide='ignore'):
|
||||
... np.arange(3) / 0.
|
||||
array([nan, inf, inf])
|
||||
|
||||
>>> np.sqrt(-1)
|
||||
np.float64(nan)
|
||||
>>> with np.errstate(invalid='raise'):
|
||||
... np.sqrt(-1)
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 2, in <module>
|
||||
FloatingPointError: invalid value encountered in sqrt
|
||||
|
||||
Outside the context the error handling behavior has not changed:
|
||||
|
||||
>>> np.geterr()
|
||||
{'divide': 'ignore', 'over': 'ignore', 'under': 'ignore', 'invalid': 'ignore'}
|
||||
>>> olderr = np.seterr(**olderr) # restore original state
|
||||
|
||||
"""
|
||||
__slots__ = (
|
||||
"_call", "_all", "_divide", "_over", "_under", "_invalid", "_token")
|
||||
|
||||
def __init__(self, *, call=_Unspecified,
|
||||
all=None, divide=None, over=None, under=None, invalid=None):
|
||||
self._token = None
|
||||
self._call = call
|
||||
self._all = all
|
||||
self._divide = divide
|
||||
self._over = over
|
||||
self._under = under
|
||||
self._invalid = invalid
|
||||
|
||||
def __enter__(self):
|
||||
# Note that __call__ duplicates much of this logic
|
||||
if self._token is not None:
|
||||
raise TypeError("Cannot enter `np.errstate` twice.")
|
||||
if self._call is _Unspecified:
|
||||
extobj = _make_extobj(
|
||||
all=self._all, divide=self._divide, over=self._over,
|
||||
under=self._under, invalid=self._invalid)
|
||||
else:
|
||||
extobj = _make_extobj(
|
||||
call=self._call,
|
||||
all=self._all, divide=self._divide, over=self._over,
|
||||
under=self._under, invalid=self._invalid)
|
||||
|
||||
self._token = _extobj_contextvar.set(extobj)
|
||||
|
||||
def __exit__(self, *exc_info):
|
||||
_extobj_contextvar.reset(self._token)
|
||||
|
||||
def __call__(self, func):
|
||||
# We need to customize `__call__` compared to `ContextDecorator`
|
||||
# because we must store the token per-thread so cannot store it on
|
||||
# the instance (we could create a new instance for this).
|
||||
# This duplicates the code from `__enter__`.
|
||||
@functools.wraps(func)
|
||||
def inner(*args, **kwargs):
|
||||
if self._call is _Unspecified:
|
||||
extobj = _make_extobj(
|
||||
all=self._all, divide=self._divide, over=self._over,
|
||||
under=self._under, invalid=self._invalid)
|
||||
else:
|
||||
extobj = _make_extobj(
|
||||
call=self._call,
|
||||
all=self._all, divide=self._divide, over=self._over,
|
||||
under=self._under, invalid=self._invalid)
|
||||
|
||||
_token = _extobj_contextvar.set(extobj)
|
||||
try:
|
||||
# Call the original, decorated, function:
|
||||
return func(*args, **kwargs)
|
||||
finally:
|
||||
_extobj_contextvar.reset(_token)
|
||||
|
||||
return inner
|
||||
|
||||
|
||||
NO_NEP50_WARNING = contextvars.ContextVar("_no_nep50_warning", default=False)
|
||||
|
||||
@set_module('numpy')
|
||||
@contextlib.contextmanager
|
||||
def _no_nep50_warning():
|
||||
"""
|
||||
Context manager to disable NEP 50 warnings. This context manager is
|
||||
only relevant if the NEP 50 warnings are enabled globally (which is not
|
||||
thread/context safe).
|
||||
|
||||
This warning context manager itself is fully safe, however.
|
||||
"""
|
||||
token = NO_NEP50_WARNING.set(True)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
NO_NEP50_WARNING.reset(token)
|
||||
@ -0,0 +1,37 @@
|
||||
from collections.abc import Callable
|
||||
from typing import Any, Literal, TypedDict
|
||||
|
||||
from numpy import _SupportsWrite
|
||||
|
||||
_ErrKind = Literal["ignore", "warn", "raise", "call", "print", "log"]
|
||||
_ErrFunc = Callable[[str, int], Any]
|
||||
|
||||
class _ErrDict(TypedDict):
|
||||
divide: _ErrKind
|
||||
over: _ErrKind
|
||||
under: _ErrKind
|
||||
invalid: _ErrKind
|
||||
|
||||
class _ErrDictOptional(TypedDict, total=False):
|
||||
all: None | _ErrKind
|
||||
divide: None | _ErrKind
|
||||
over: None | _ErrKind
|
||||
under: None | _ErrKind
|
||||
invalid: None | _ErrKind
|
||||
|
||||
def seterr(
|
||||
all: None | _ErrKind = ...,
|
||||
divide: None | _ErrKind = ...,
|
||||
over: None | _ErrKind = ...,
|
||||
under: None | _ErrKind = ...,
|
||||
invalid: None | _ErrKind = ...,
|
||||
) -> _ErrDict: ...
|
||||
def geterr() -> _ErrDict: ...
|
||||
def setbufsize(size: int) -> int: ...
|
||||
def getbufsize() -> int: ...
|
||||
def seterrcall(
|
||||
func: None | _ErrFunc | _SupportsWrite[str]
|
||||
) -> None | _ErrFunc | _SupportsWrite[str]: ...
|
||||
def geterrcall() -> None | _ErrFunc | _SupportsWrite[str]: ...
|
||||
|
||||
# See `numpy/__init__.pyi` for the `errstate` class and `no_nep5_warnings`
|
||||
Binary file not shown.
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,134 @@
|
||||
from collections.abc import Callable
|
||||
from typing import Any, Literal, TypedDict, SupportsIndex
|
||||
|
||||
# Using a private class is by no means ideal, but it is simply a consequence
|
||||
# of a `contextlib.context` returning an instance of aforementioned class
|
||||
from contextlib import _GeneratorContextManager
|
||||
|
||||
import numpy as np
|
||||
from numpy import (
|
||||
integer,
|
||||
timedelta64,
|
||||
datetime64,
|
||||
floating,
|
||||
complexfloating,
|
||||
void,
|
||||
longdouble,
|
||||
clongdouble,
|
||||
)
|
||||
from numpy._typing import NDArray, _CharLike_co, _FloatLike_co
|
||||
|
||||
_FloatMode = Literal["fixed", "unique", "maxprec", "maxprec_equal"]
|
||||
|
||||
class _FormatDict(TypedDict, total=False):
|
||||
bool: Callable[[np.bool], str]
|
||||
int: Callable[[integer[Any]], str]
|
||||
timedelta: Callable[[timedelta64], str]
|
||||
datetime: Callable[[datetime64], str]
|
||||
float: Callable[[floating[Any]], str]
|
||||
longfloat: Callable[[longdouble], str]
|
||||
complexfloat: Callable[[complexfloating[Any, Any]], str]
|
||||
longcomplexfloat: Callable[[clongdouble], str]
|
||||
void: Callable[[void], str]
|
||||
numpystr: Callable[[_CharLike_co], str]
|
||||
object: Callable[[object], str]
|
||||
all: Callable[[object], str]
|
||||
int_kind: Callable[[integer[Any]], str]
|
||||
float_kind: Callable[[floating[Any]], str]
|
||||
complex_kind: Callable[[complexfloating[Any, Any]], str]
|
||||
str_kind: Callable[[_CharLike_co], str]
|
||||
|
||||
class _FormatOptions(TypedDict):
|
||||
precision: int
|
||||
threshold: int
|
||||
edgeitems: int
|
||||
linewidth: int
|
||||
suppress: bool
|
||||
nanstr: str
|
||||
infstr: str
|
||||
formatter: None | _FormatDict
|
||||
sign: Literal["-", "+", " "]
|
||||
floatmode: _FloatMode
|
||||
legacy: Literal[False, "1.13", "1.21"]
|
||||
|
||||
def set_printoptions(
|
||||
precision: None | SupportsIndex = ...,
|
||||
threshold: None | int = ...,
|
||||
edgeitems: None | int = ...,
|
||||
linewidth: None | int = ...,
|
||||
suppress: None | bool = ...,
|
||||
nanstr: None | str = ...,
|
||||
infstr: None | str = ...,
|
||||
formatter: None | _FormatDict = ...,
|
||||
sign: Literal[None, "-", "+", " "] = ...,
|
||||
floatmode: None | _FloatMode = ...,
|
||||
*,
|
||||
legacy: Literal[None, False, "1.13", "1.21"] = ...
|
||||
) -> None: ...
|
||||
def get_printoptions() -> _FormatOptions: ...
|
||||
def array2string(
|
||||
a: NDArray[Any],
|
||||
max_line_width: None | int = ...,
|
||||
precision: None | SupportsIndex = ...,
|
||||
suppress_small: None | bool = ...,
|
||||
separator: str = ...,
|
||||
prefix: str = ...,
|
||||
# NOTE: With the `style` argument being deprecated,
|
||||
# all arguments between `formatter` and `suffix` are de facto
|
||||
# keyworld-only arguments
|
||||
*,
|
||||
formatter: None | _FormatDict = ...,
|
||||
threshold: None | int = ...,
|
||||
edgeitems: None | int = ...,
|
||||
sign: Literal[None, "-", "+", " "] = ...,
|
||||
floatmode: None | _FloatMode = ...,
|
||||
suffix: str = ...,
|
||||
legacy: Literal[None, False, "1.13", "1.21"] = ...,
|
||||
) -> str: ...
|
||||
def format_float_scientific(
|
||||
x: _FloatLike_co,
|
||||
precision: None | int = ...,
|
||||
unique: bool = ...,
|
||||
trim: Literal["k", ".", "0", "-"] = ...,
|
||||
sign: bool = ...,
|
||||
pad_left: None | int = ...,
|
||||
exp_digits: None | int = ...,
|
||||
min_digits: None | int = ...,
|
||||
) -> str: ...
|
||||
def format_float_positional(
|
||||
x: _FloatLike_co,
|
||||
precision: None | int = ...,
|
||||
unique: bool = ...,
|
||||
fractional: bool = ...,
|
||||
trim: Literal["k", ".", "0", "-"] = ...,
|
||||
sign: bool = ...,
|
||||
pad_left: None | int = ...,
|
||||
pad_right: None | int = ...,
|
||||
min_digits: None | int = ...,
|
||||
) -> str: ...
|
||||
def array_repr(
|
||||
arr: NDArray[Any],
|
||||
max_line_width: None | int = ...,
|
||||
precision: None | SupportsIndex = ...,
|
||||
suppress_small: None | bool = ...,
|
||||
) -> str: ...
|
||||
def array_str(
|
||||
a: NDArray[Any],
|
||||
max_line_width: None | int = ...,
|
||||
precision: None | SupportsIndex = ...,
|
||||
suppress_small: None | bool = ...,
|
||||
) -> str: ...
|
||||
def printoptions(
|
||||
precision: None | SupportsIndex = ...,
|
||||
threshold: None | int = ...,
|
||||
edgeitems: None | int = ...,
|
||||
linewidth: None | int = ...,
|
||||
suppress: None | bool = ...,
|
||||
nanstr: None | str = ...,
|
||||
infstr: None | str = ...,
|
||||
formatter: None | _FormatDict = ...,
|
||||
sign: Literal[None, "-", "+", " "] = ...,
|
||||
floatmode: None | _FloatMode = ...,
|
||||
*,
|
||||
legacy: Literal[None, False, "1.13", "1.21"] = ...
|
||||
) -> _GeneratorContextManager[_FormatOptions]: ...
|
||||
@ -0,0 +1,13 @@
|
||||
"""Simple script to compute the api hash of the current API.
|
||||
|
||||
The API has is defined by numpy_api_order and ufunc_api_order.
|
||||
|
||||
"""
|
||||
from os.path import dirname
|
||||
|
||||
from code_generators.genapi import fullapi_hash
|
||||
from code_generators.numpy_api import full_api
|
||||
|
||||
if __name__ == '__main__':
|
||||
curdir = dirname(__file__)
|
||||
print(fullapi_hash(full_api))
|
||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,843 @@
|
||||
from typing import (
|
||||
Literal as L,
|
||||
overload,
|
||||
TypeVar,
|
||||
Any,
|
||||
SupportsIndex,
|
||||
SupportsInt,
|
||||
)
|
||||
|
||||
import numpy as np
|
||||
from numpy import (
|
||||
ndarray,
|
||||
dtype,
|
||||
str_,
|
||||
bytes_,
|
||||
int_,
|
||||
object_,
|
||||
_OrderKACF,
|
||||
_ShapeType,
|
||||
_CharDType,
|
||||
_SupportsBuffer,
|
||||
)
|
||||
|
||||
from numpy._typing import (
|
||||
NDArray,
|
||||
_ShapeLike,
|
||||
_ArrayLikeStr_co as U_co,
|
||||
_ArrayLikeBytes_co as S_co,
|
||||
_ArrayLikeInt_co as i_co,
|
||||
_ArrayLikeBool_co as b_co,
|
||||
)
|
||||
|
||||
from numpy._core.multiarray import compare_chararrays as compare_chararrays
|
||||
|
||||
_SCT = TypeVar("_SCT", str_, bytes_)
|
||||
_CharArray = chararray[Any, dtype[_SCT]]
|
||||
|
||||
class chararray(ndarray[_ShapeType, _CharDType]):
|
||||
@overload
|
||||
def __new__(
|
||||
subtype,
|
||||
shape: _ShapeLike,
|
||||
itemsize: SupportsIndex | SupportsInt = ...,
|
||||
unicode: L[False] = ...,
|
||||
buffer: _SupportsBuffer = ...,
|
||||
offset: SupportsIndex = ...,
|
||||
strides: _ShapeLike = ...,
|
||||
order: _OrderKACF = ...,
|
||||
) -> chararray[Any, dtype[bytes_]]: ...
|
||||
@overload
|
||||
def __new__(
|
||||
subtype,
|
||||
shape: _ShapeLike,
|
||||
itemsize: SupportsIndex | SupportsInt = ...,
|
||||
unicode: L[True] = ...,
|
||||
buffer: _SupportsBuffer = ...,
|
||||
offset: SupportsIndex = ...,
|
||||
strides: _ShapeLike = ...,
|
||||
order: _OrderKACF = ...,
|
||||
) -> chararray[Any, dtype[str_]]: ...
|
||||
|
||||
def __array_finalize__(self, obj: object) -> None: ...
|
||||
def __mul__(self, other: i_co) -> chararray[Any, _CharDType]: ...
|
||||
def __rmul__(self, other: i_co) -> chararray[Any, _CharDType]: ...
|
||||
def __mod__(self, i: Any) -> chararray[Any, _CharDType]: ...
|
||||
|
||||
@overload
|
||||
def __eq__(
|
||||
self: _CharArray[str_],
|
||||
other: U_co,
|
||||
) -> NDArray[np.bool]: ...
|
||||
@overload
|
||||
def __eq__(
|
||||
self: _CharArray[bytes_],
|
||||
other: S_co,
|
||||
) -> NDArray[np.bool]: ...
|
||||
|
||||
@overload
|
||||
def __ne__(
|
||||
self: _CharArray[str_],
|
||||
other: U_co,
|
||||
) -> NDArray[np.bool]: ...
|
||||
@overload
|
||||
def __ne__(
|
||||
self: _CharArray[bytes_],
|
||||
other: S_co,
|
||||
) -> NDArray[np.bool]: ...
|
||||
|
||||
@overload
|
||||
def __ge__(
|
||||
self: _CharArray[str_],
|
||||
other: U_co,
|
||||
) -> NDArray[np.bool]: ...
|
||||
@overload
|
||||
def __ge__(
|
||||
self: _CharArray[bytes_],
|
||||
other: S_co,
|
||||
) -> NDArray[np.bool]: ...
|
||||
|
||||
@overload
|
||||
def __le__(
|
||||
self: _CharArray[str_],
|
||||
other: U_co,
|
||||
) -> NDArray[np.bool]: ...
|
||||
@overload
|
||||
def __le__(
|
||||
self: _CharArray[bytes_],
|
||||
other: S_co,
|
||||
) -> NDArray[np.bool]: ...
|
||||
|
||||
@overload
|
||||
def __gt__(
|
||||
self: _CharArray[str_],
|
||||
other: U_co,
|
||||
) -> NDArray[np.bool]: ...
|
||||
@overload
|
||||
def __gt__(
|
||||
self: _CharArray[bytes_],
|
||||
other: S_co,
|
||||
) -> NDArray[np.bool]: ...
|
||||
|
||||
@overload
|
||||
def __lt__(
|
||||
self: _CharArray[str_],
|
||||
other: U_co,
|
||||
) -> NDArray[np.bool]: ...
|
||||
@overload
|
||||
def __lt__(
|
||||
self: _CharArray[bytes_],
|
||||
other: S_co,
|
||||
) -> NDArray[np.bool]: ...
|
||||
|
||||
@overload
|
||||
def __add__(
|
||||
self: _CharArray[str_],
|
||||
other: U_co,
|
||||
) -> _CharArray[str_]: ...
|
||||
@overload
|
||||
def __add__(
|
||||
self: _CharArray[bytes_],
|
||||
other: S_co,
|
||||
) -> _CharArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def __radd__(
|
||||
self: _CharArray[str_],
|
||||
other: U_co,
|
||||
) -> _CharArray[str_]: ...
|
||||
@overload
|
||||
def __radd__(
|
||||
self: _CharArray[bytes_],
|
||||
other: S_co,
|
||||
) -> _CharArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def center(
|
||||
self: _CharArray[str_],
|
||||
width: i_co,
|
||||
fillchar: U_co = ...,
|
||||
) -> _CharArray[str_]: ...
|
||||
@overload
|
||||
def center(
|
||||
self: _CharArray[bytes_],
|
||||
width: i_co,
|
||||
fillchar: S_co = ...,
|
||||
) -> _CharArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def count(
|
||||
self: _CharArray[str_],
|
||||
sub: U_co,
|
||||
start: i_co = ...,
|
||||
end: None | i_co = ...,
|
||||
) -> NDArray[int_]: ...
|
||||
@overload
|
||||
def count(
|
||||
self: _CharArray[bytes_],
|
||||
sub: S_co,
|
||||
start: i_co = ...,
|
||||
end: None | i_co = ...,
|
||||
) -> NDArray[int_]: ...
|
||||
|
||||
def decode(
|
||||
self: _CharArray[bytes_],
|
||||
encoding: None | str = ...,
|
||||
errors: None | str = ...,
|
||||
) -> _CharArray[str_]: ...
|
||||
|
||||
def encode(
|
||||
self: _CharArray[str_],
|
||||
encoding: None | str = ...,
|
||||
errors: None | str = ...,
|
||||
) -> _CharArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def endswith(
|
||||
self: _CharArray[str_],
|
||||
suffix: U_co,
|
||||
start: i_co = ...,
|
||||
end: None | i_co = ...,
|
||||
) -> NDArray[np.bool]: ...
|
||||
@overload
|
||||
def endswith(
|
||||
self: _CharArray[bytes_],
|
||||
suffix: S_co,
|
||||
start: i_co = ...,
|
||||
end: None | i_co = ...,
|
||||
) -> NDArray[np.bool]: ...
|
||||
|
||||
def expandtabs(
|
||||
self,
|
||||
tabsize: i_co = ...,
|
||||
) -> chararray[Any, _CharDType]: ...
|
||||
|
||||
@overload
|
||||
def find(
|
||||
self: _CharArray[str_],
|
||||
sub: U_co,
|
||||
start: i_co = ...,
|
||||
end: None | i_co = ...,
|
||||
) -> NDArray[int_]: ...
|
||||
@overload
|
||||
def find(
|
||||
self: _CharArray[bytes_],
|
||||
sub: S_co,
|
||||
start: i_co = ...,
|
||||
end: None | i_co = ...,
|
||||
) -> NDArray[int_]: ...
|
||||
|
||||
@overload
|
||||
def index(
|
||||
self: _CharArray[str_],
|
||||
sub: U_co,
|
||||
start: i_co = ...,
|
||||
end: None | i_co = ...,
|
||||
) -> NDArray[int_]: ...
|
||||
@overload
|
||||
def index(
|
||||
self: _CharArray[bytes_],
|
||||
sub: S_co,
|
||||
start: i_co = ...,
|
||||
end: None | i_co = ...,
|
||||
) -> NDArray[int_]: ...
|
||||
|
||||
@overload
|
||||
def join(
|
||||
self: _CharArray[str_],
|
||||
seq: U_co,
|
||||
) -> _CharArray[str_]: ...
|
||||
@overload
|
||||
def join(
|
||||
self: _CharArray[bytes_],
|
||||
seq: S_co,
|
||||
) -> _CharArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def ljust(
|
||||
self: _CharArray[str_],
|
||||
width: i_co,
|
||||
fillchar: U_co = ...,
|
||||
) -> _CharArray[str_]: ...
|
||||
@overload
|
||||
def ljust(
|
||||
self: _CharArray[bytes_],
|
||||
width: i_co,
|
||||
fillchar: S_co = ...,
|
||||
) -> _CharArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def lstrip(
|
||||
self: _CharArray[str_],
|
||||
chars: None | U_co = ...,
|
||||
) -> _CharArray[str_]: ...
|
||||
@overload
|
||||
def lstrip(
|
||||
self: _CharArray[bytes_],
|
||||
chars: None | S_co = ...,
|
||||
) -> _CharArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def partition(
|
||||
self: _CharArray[str_],
|
||||
sep: U_co,
|
||||
) -> _CharArray[str_]: ...
|
||||
@overload
|
||||
def partition(
|
||||
self: _CharArray[bytes_],
|
||||
sep: S_co,
|
||||
) -> _CharArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def replace(
|
||||
self: _CharArray[str_],
|
||||
old: U_co,
|
||||
new: U_co,
|
||||
count: None | i_co = ...,
|
||||
) -> _CharArray[str_]: ...
|
||||
@overload
|
||||
def replace(
|
||||
self: _CharArray[bytes_],
|
||||
old: S_co,
|
||||
new: S_co,
|
||||
count: None | i_co = ...,
|
||||
) -> _CharArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def rfind(
|
||||
self: _CharArray[str_],
|
||||
sub: U_co,
|
||||
start: i_co = ...,
|
||||
end: None | i_co = ...,
|
||||
) -> NDArray[int_]: ...
|
||||
@overload
|
||||
def rfind(
|
||||
self: _CharArray[bytes_],
|
||||
sub: S_co,
|
||||
start: i_co = ...,
|
||||
end: None | i_co = ...,
|
||||
) -> NDArray[int_]: ...
|
||||
|
||||
@overload
|
||||
def rindex(
|
||||
self: _CharArray[str_],
|
||||
sub: U_co,
|
||||
start: i_co = ...,
|
||||
end: None | i_co = ...,
|
||||
) -> NDArray[int_]: ...
|
||||
@overload
|
||||
def rindex(
|
||||
self: _CharArray[bytes_],
|
||||
sub: S_co,
|
||||
start: i_co = ...,
|
||||
end: None | i_co = ...,
|
||||
) -> NDArray[int_]: ...
|
||||
|
||||
@overload
|
||||
def rjust(
|
||||
self: _CharArray[str_],
|
||||
width: i_co,
|
||||
fillchar: U_co = ...,
|
||||
) -> _CharArray[str_]: ...
|
||||
@overload
|
||||
def rjust(
|
||||
self: _CharArray[bytes_],
|
||||
width: i_co,
|
||||
fillchar: S_co = ...,
|
||||
) -> _CharArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def rpartition(
|
||||
self: _CharArray[str_],
|
||||
sep: U_co,
|
||||
) -> _CharArray[str_]: ...
|
||||
@overload
|
||||
def rpartition(
|
||||
self: _CharArray[bytes_],
|
||||
sep: S_co,
|
||||
) -> _CharArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def rsplit(
|
||||
self: _CharArray[str_],
|
||||
sep: None | U_co = ...,
|
||||
maxsplit: None | i_co = ...,
|
||||
) -> NDArray[object_]: ...
|
||||
@overload
|
||||
def rsplit(
|
||||
self: _CharArray[bytes_],
|
||||
sep: None | S_co = ...,
|
||||
maxsplit: None | i_co = ...,
|
||||
) -> NDArray[object_]: ...
|
||||
|
||||
@overload
|
||||
def rstrip(
|
||||
self: _CharArray[str_],
|
||||
chars: None | U_co = ...,
|
||||
) -> _CharArray[str_]: ...
|
||||
@overload
|
||||
def rstrip(
|
||||
self: _CharArray[bytes_],
|
||||
chars: None | S_co = ...,
|
||||
) -> _CharArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def split(
|
||||
self: _CharArray[str_],
|
||||
sep: None | U_co = ...,
|
||||
maxsplit: None | i_co = ...,
|
||||
) -> NDArray[object_]: ...
|
||||
@overload
|
||||
def split(
|
||||
self: _CharArray[bytes_],
|
||||
sep: None | S_co = ...,
|
||||
maxsplit: None | i_co = ...,
|
||||
) -> NDArray[object_]: ...
|
||||
|
||||
def splitlines(self, keepends: None | b_co = ...) -> NDArray[object_]: ...
|
||||
|
||||
@overload
|
||||
def startswith(
|
||||
self: _CharArray[str_],
|
||||
prefix: U_co,
|
||||
start: i_co = ...,
|
||||
end: None | i_co = ...,
|
||||
) -> NDArray[np.bool]: ...
|
||||
@overload
|
||||
def startswith(
|
||||
self: _CharArray[bytes_],
|
||||
prefix: S_co,
|
||||
start: i_co = ...,
|
||||
end: None | i_co = ...,
|
||||
) -> NDArray[np.bool]: ...
|
||||
|
||||
@overload
|
||||
def strip(
|
||||
self: _CharArray[str_],
|
||||
chars: None | U_co = ...,
|
||||
) -> _CharArray[str_]: ...
|
||||
@overload
|
||||
def strip(
|
||||
self: _CharArray[bytes_],
|
||||
chars: None | S_co = ...,
|
||||
) -> _CharArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def translate(
|
||||
self: _CharArray[str_],
|
||||
table: U_co,
|
||||
deletechars: None | U_co = ...,
|
||||
) -> _CharArray[str_]: ...
|
||||
@overload
|
||||
def translate(
|
||||
self: _CharArray[bytes_],
|
||||
table: S_co,
|
||||
deletechars: None | S_co = ...,
|
||||
) -> _CharArray[bytes_]: ...
|
||||
|
||||
def zfill(self, width: _ArrayLikeInt_co) -> chararray[Any, _CharDType]: ...
|
||||
def capitalize(self) -> chararray[_ShapeType, _CharDType]: ...
|
||||
def title(self) -> chararray[_ShapeType, _CharDType]: ...
|
||||
def swapcase(self) -> chararray[_ShapeType, _CharDType]: ...
|
||||
def lower(self) -> chararray[_ShapeType, _CharDType]: ...
|
||||
def upper(self) -> chararray[_ShapeType, _CharDType]: ...
|
||||
def isalnum(self) -> ndarray[_ShapeType, dtype[np.bool]]: ...
|
||||
def isalpha(self) -> ndarray[_ShapeType, dtype[np.bool]]: ...
|
||||
def isdigit(self) -> ndarray[_ShapeType, dtype[np.bool]]: ...
|
||||
def islower(self) -> ndarray[_ShapeType, dtype[np.bool]]: ...
|
||||
def isspace(self) -> ndarray[_ShapeType, dtype[np.bool]]: ...
|
||||
def istitle(self) -> ndarray[_ShapeType, dtype[np.bool]]: ...
|
||||
def isupper(self) -> ndarray[_ShapeType, dtype[np.bool]]: ...
|
||||
def isnumeric(self) -> ndarray[_ShapeType, dtype[np.bool]]: ...
|
||||
def isdecimal(self) -> ndarray[_ShapeType, dtype[np.bool]]: ...
|
||||
|
||||
__all__: list[str]
|
||||
|
||||
# Comparison
|
||||
@overload
|
||||
def equal(x1: U_co, x2: U_co) -> NDArray[np.bool]: ...
|
||||
@overload
|
||||
def equal(x1: S_co, x2: S_co) -> NDArray[np.bool]: ...
|
||||
|
||||
@overload
|
||||
def not_equal(x1: U_co, x2: U_co) -> NDArray[np.bool]: ...
|
||||
@overload
|
||||
def not_equal(x1: S_co, x2: S_co) -> NDArray[np.bool]: ...
|
||||
|
||||
@overload
|
||||
def greater_equal(x1: U_co, x2: U_co) -> NDArray[np.bool]: ...
|
||||
@overload
|
||||
def greater_equal(x1: S_co, x2: S_co) -> NDArray[np.bool]: ...
|
||||
|
||||
@overload
|
||||
def less_equal(x1: U_co, x2: U_co) -> NDArray[np.bool]: ...
|
||||
@overload
|
||||
def less_equal(x1: S_co, x2: S_co) -> NDArray[np.bool]: ...
|
||||
|
||||
@overload
|
||||
def greater(x1: U_co, x2: U_co) -> NDArray[np.bool]: ...
|
||||
@overload
|
||||
def greater(x1: S_co, x2: S_co) -> NDArray[np.bool]: ...
|
||||
|
||||
@overload
|
||||
def less(x1: U_co, x2: U_co) -> NDArray[np.bool]: ...
|
||||
@overload
|
||||
def less(x1: S_co, x2: S_co) -> NDArray[np.bool]: ...
|
||||
|
||||
# String operations
|
||||
@overload
|
||||
def add(x1: U_co, x2: U_co) -> NDArray[str_]: ...
|
||||
@overload
|
||||
def add(x1: S_co, x2: S_co) -> NDArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def multiply(a: U_co, i: i_co) -> NDArray[str_]: ...
|
||||
@overload
|
||||
def multiply(a: S_co, i: i_co) -> NDArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def mod(a: U_co, value: Any) -> NDArray[str_]: ...
|
||||
@overload
|
||||
def mod(a: S_co, value: Any) -> NDArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def capitalize(a: U_co) -> NDArray[str_]: ...
|
||||
@overload
|
||||
def capitalize(a: S_co) -> NDArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def center(a: U_co, width: i_co, fillchar: U_co = ...) -> NDArray[str_]: ...
|
||||
@overload
|
||||
def center(a: S_co, width: i_co, fillchar: S_co = ...) -> NDArray[bytes_]: ...
|
||||
|
||||
def decode(
|
||||
a: S_co,
|
||||
encoding: None | str = ...,
|
||||
errors: None | str = ...,
|
||||
) -> NDArray[str_]: ...
|
||||
|
||||
def encode(
|
||||
a: U_co,
|
||||
encoding: None | str = ...,
|
||||
errors: None | str = ...,
|
||||
) -> NDArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def expandtabs(a: U_co, tabsize: i_co = ...) -> NDArray[str_]: ...
|
||||
@overload
|
||||
def expandtabs(a: S_co, tabsize: i_co = ...) -> NDArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def join(sep: U_co, seq: U_co) -> NDArray[str_]: ...
|
||||
@overload
|
||||
def join(sep: S_co, seq: S_co) -> NDArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def ljust(a: U_co, width: i_co, fillchar: U_co = ...) -> NDArray[str_]: ...
|
||||
@overload
|
||||
def ljust(a: S_co, width: i_co, fillchar: S_co = ...) -> NDArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def lower(a: U_co) -> NDArray[str_]: ...
|
||||
@overload
|
||||
def lower(a: S_co) -> NDArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def lstrip(a: U_co, chars: None | U_co = ...) -> NDArray[str_]: ...
|
||||
@overload
|
||||
def lstrip(a: S_co, chars: None | S_co = ...) -> NDArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def partition(a: U_co, sep: U_co) -> NDArray[str_]: ...
|
||||
@overload
|
||||
def partition(a: S_co, sep: S_co) -> NDArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def replace(
|
||||
a: U_co,
|
||||
old: U_co,
|
||||
new: U_co,
|
||||
count: None | i_co = ...,
|
||||
) -> NDArray[str_]: ...
|
||||
@overload
|
||||
def replace(
|
||||
a: S_co,
|
||||
old: S_co,
|
||||
new: S_co,
|
||||
count: None | i_co = ...,
|
||||
) -> NDArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def rjust(
|
||||
a: U_co,
|
||||
width: i_co,
|
||||
fillchar: U_co = ...,
|
||||
) -> NDArray[str_]: ...
|
||||
@overload
|
||||
def rjust(
|
||||
a: S_co,
|
||||
width: i_co,
|
||||
fillchar: S_co = ...,
|
||||
) -> NDArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def rpartition(a: U_co, sep: U_co) -> NDArray[str_]: ...
|
||||
@overload
|
||||
def rpartition(a: S_co, sep: S_co) -> NDArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def rsplit(
|
||||
a: U_co,
|
||||
sep: None | U_co = ...,
|
||||
maxsplit: None | i_co = ...,
|
||||
) -> NDArray[object_]: ...
|
||||
@overload
|
||||
def rsplit(
|
||||
a: S_co,
|
||||
sep: None | S_co = ...,
|
||||
maxsplit: None | i_co = ...,
|
||||
) -> NDArray[object_]: ...
|
||||
|
||||
@overload
|
||||
def rstrip(a: U_co, chars: None | U_co = ...) -> NDArray[str_]: ...
|
||||
@overload
|
||||
def rstrip(a: S_co, chars: None | S_co = ...) -> NDArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def split(
|
||||
a: U_co,
|
||||
sep: None | U_co = ...,
|
||||
maxsplit: None | i_co = ...,
|
||||
) -> NDArray[object_]: ...
|
||||
@overload
|
||||
def split(
|
||||
a: S_co,
|
||||
sep: None | S_co = ...,
|
||||
maxsplit: None | i_co = ...,
|
||||
) -> NDArray[object_]: ...
|
||||
|
||||
@overload
|
||||
def splitlines(a: U_co, keepends: None | b_co = ...) -> NDArray[object_]: ...
|
||||
@overload
|
||||
def splitlines(a: S_co, keepends: None | b_co = ...) -> NDArray[object_]: ...
|
||||
|
||||
@overload
|
||||
def strip(a: U_co, chars: None | U_co = ...) -> NDArray[str_]: ...
|
||||
@overload
|
||||
def strip(a: S_co, chars: None | S_co = ...) -> NDArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def swapcase(a: U_co) -> NDArray[str_]: ...
|
||||
@overload
|
||||
def swapcase(a: S_co) -> NDArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def title(a: U_co) -> NDArray[str_]: ...
|
||||
@overload
|
||||
def title(a: S_co) -> NDArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def translate(
|
||||
a: U_co,
|
||||
table: U_co,
|
||||
deletechars: None | U_co = ...,
|
||||
) -> NDArray[str_]: ...
|
||||
@overload
|
||||
def translate(
|
||||
a: S_co,
|
||||
table: S_co,
|
||||
deletechars: None | S_co = ...,
|
||||
) -> NDArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def upper(a: U_co) -> NDArray[str_]: ...
|
||||
@overload
|
||||
def upper(a: S_co) -> NDArray[bytes_]: ...
|
||||
|
||||
@overload
|
||||
def zfill(a: U_co, width: i_co) -> NDArray[str_]: ...
|
||||
@overload
|
||||
def zfill(a: S_co, width: i_co) -> NDArray[bytes_]: ...
|
||||
|
||||
# String information
|
||||
@overload
|
||||
def count(
|
||||
a: U_co,
|
||||
sub: U_co,
|
||||
start: i_co = ...,
|
||||
end: None | i_co = ...,
|
||||
) -> NDArray[int_]: ...
|
||||
@overload
|
||||
def count(
|
||||
a: S_co,
|
||||
sub: S_co,
|
||||
start: i_co = ...,
|
||||
end: None | i_co = ...,
|
||||
) -> NDArray[int_]: ...
|
||||
|
||||
@overload
|
||||
def endswith(
|
||||
a: U_co,
|
||||
suffix: U_co,
|
||||
start: i_co = ...,
|
||||
end: None | i_co = ...,
|
||||
) -> NDArray[np.bool]: ...
|
||||
@overload
|
||||
def endswith(
|
||||
a: S_co,
|
||||
suffix: S_co,
|
||||
start: i_co = ...,
|
||||
end: None | i_co = ...,
|
||||
) -> NDArray[np.bool]: ...
|
||||
|
||||
@overload
|
||||
def find(
|
||||
a: U_co,
|
||||
sub: U_co,
|
||||
start: i_co = ...,
|
||||
end: None | i_co = ...,
|
||||
) -> NDArray[int_]: ...
|
||||
@overload
|
||||
def find(
|
||||
a: S_co,
|
||||
sub: S_co,
|
||||
start: i_co = ...,
|
||||
end: None | i_co = ...,
|
||||
) -> NDArray[int_]: ...
|
||||
|
||||
@overload
|
||||
def index(
|
||||
a: U_co,
|
||||
sub: U_co,
|
||||
start: i_co = ...,
|
||||
end: None | i_co = ...,
|
||||
) -> NDArray[int_]: ...
|
||||
@overload
|
||||
def index(
|
||||
a: S_co,
|
||||
sub: S_co,
|
||||
start: i_co = ...,
|
||||
end: None | i_co = ...,
|
||||
) -> NDArray[int_]: ...
|
||||
|
||||
def isalpha(a: U_co | S_co) -> NDArray[np.bool]: ...
|
||||
def isalnum(a: U_co | S_co) -> NDArray[np.bool]: ...
|
||||
def isdecimal(a: U_co) -> NDArray[np.bool]: ...
|
||||
def isdigit(a: U_co | S_co) -> NDArray[np.bool]: ...
|
||||
def islower(a: U_co | S_co) -> NDArray[np.bool]: ...
|
||||
def isnumeric(a: U_co) -> NDArray[np.bool]: ...
|
||||
def isspace(a: U_co | S_co) -> NDArray[np.bool]: ...
|
||||
def istitle(a: U_co | S_co) -> NDArray[np.bool]: ...
|
||||
def isupper(a: U_co | S_co) -> NDArray[np.bool]: ...
|
||||
|
||||
@overload
|
||||
def rfind(
|
||||
a: U_co,
|
||||
sub: U_co,
|
||||
start: i_co = ...,
|
||||
end: None | i_co = ...,
|
||||
) -> NDArray[int_]: ...
|
||||
@overload
|
||||
def rfind(
|
||||
a: S_co,
|
||||
sub: S_co,
|
||||
start: i_co = ...,
|
||||
end: None | i_co = ...,
|
||||
) -> NDArray[int_]: ...
|
||||
|
||||
@overload
|
||||
def rindex(
|
||||
a: U_co,
|
||||
sub: U_co,
|
||||
start: i_co = ...,
|
||||
end: None | i_co = ...,
|
||||
) -> NDArray[int_]: ...
|
||||
@overload
|
||||
def rindex(
|
||||
a: S_co,
|
||||
sub: S_co,
|
||||
start: i_co = ...,
|
||||
end: None | i_co = ...,
|
||||
) -> NDArray[int_]: ...
|
||||
|
||||
@overload
|
||||
def startswith(
|
||||
a: U_co,
|
||||
prefix: U_co,
|
||||
start: i_co = ...,
|
||||
end: None | i_co = ...,
|
||||
) -> NDArray[np.bool]: ...
|
||||
@overload
|
||||
def startswith(
|
||||
a: S_co,
|
||||
prefix: S_co,
|
||||
start: i_co = ...,
|
||||
end: None | i_co = ...,
|
||||
) -> NDArray[np.bool]: ...
|
||||
|
||||
def str_len(A: U_co | S_co) -> NDArray[int_]: ...
|
||||
|
||||
# Overload 1 and 2: str- or bytes-based array-likes
|
||||
# overload 3: arbitrary object with unicode=False (-> bytes_)
|
||||
# overload 4: arbitrary object with unicode=True (-> str_)
|
||||
@overload
|
||||
def array(
|
||||
obj: U_co,
|
||||
itemsize: None | int = ...,
|
||||
copy: bool = ...,
|
||||
unicode: L[False] = ...,
|
||||
order: _OrderKACF = ...,
|
||||
) -> _CharArray[str_]: ...
|
||||
@overload
|
||||
def array(
|
||||
obj: S_co,
|
||||
itemsize: None | int = ...,
|
||||
copy: bool = ...,
|
||||
unicode: L[False] = ...,
|
||||
order: _OrderKACF = ...,
|
||||
) -> _CharArray[bytes_]: ...
|
||||
@overload
|
||||
def array(
|
||||
obj: object,
|
||||
itemsize: None | int = ...,
|
||||
copy: bool = ...,
|
||||
unicode: L[False] = ...,
|
||||
order: _OrderKACF = ...,
|
||||
) -> _CharArray[bytes_]: ...
|
||||
@overload
|
||||
def array(
|
||||
obj: object,
|
||||
itemsize: None | int = ...,
|
||||
copy: bool = ...,
|
||||
unicode: L[True] = ...,
|
||||
order: _OrderKACF = ...,
|
||||
) -> _CharArray[str_]: ...
|
||||
|
||||
@overload
|
||||
def asarray(
|
||||
obj: U_co,
|
||||
itemsize: None | int = ...,
|
||||
unicode: L[False] = ...,
|
||||
order: _OrderKACF = ...,
|
||||
) -> _CharArray[str_]: ...
|
||||
@overload
|
||||
def asarray(
|
||||
obj: S_co,
|
||||
itemsize: None | int = ...,
|
||||
unicode: L[False] = ...,
|
||||
order: _OrderKACF = ...,
|
||||
) -> _CharArray[bytes_]: ...
|
||||
@overload
|
||||
def asarray(
|
||||
obj: object,
|
||||
itemsize: None | int = ...,
|
||||
unicode: L[False] = ...,
|
||||
order: _OrderKACF = ...,
|
||||
) -> _CharArray[bytes_]: ...
|
||||
@overload
|
||||
def asarray(
|
||||
obj: object,
|
||||
itemsize: None | int = ...,
|
||||
unicode: L[True] = ...,
|
||||
order: _OrderKACF = ...,
|
||||
) -> _CharArray[str_]: ...
|
||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,183 @@
|
||||
from collections.abc import Sequence
|
||||
from typing import TypeVar, Any, overload, Literal
|
||||
|
||||
import numpy as np
|
||||
from numpy import number, _OrderKACF
|
||||
from numpy._typing import (
|
||||
NDArray,
|
||||
_ArrayLikeBool_co,
|
||||
_ArrayLikeUInt_co,
|
||||
_ArrayLikeInt_co,
|
||||
_ArrayLikeFloat_co,
|
||||
_ArrayLikeComplex_co,
|
||||
_ArrayLikeObject_co,
|
||||
_DTypeLikeBool,
|
||||
_DTypeLikeUInt,
|
||||
_DTypeLikeInt,
|
||||
_DTypeLikeFloat,
|
||||
_DTypeLikeComplex,
|
||||
_DTypeLikeComplex_co,
|
||||
_DTypeLikeObject,
|
||||
)
|
||||
|
||||
_ArrayType = TypeVar(
|
||||
"_ArrayType",
|
||||
bound=NDArray[np.bool | number[Any]],
|
||||
)
|
||||
|
||||
_OptimizeKind = None | bool | Literal["greedy", "optimal"] | Sequence[Any]
|
||||
_CastingSafe = Literal["no", "equiv", "safe", "same_kind"]
|
||||
_CastingUnsafe = Literal["unsafe"]
|
||||
|
||||
__all__: list[str]
|
||||
|
||||
# TODO: Properly handle the `casting`-based combinatorics
|
||||
# TODO: We need to evaluate the content `__subscripts` in order
|
||||
# to identify whether or an array or scalar is returned. At a cursory
|
||||
# glance this seems like something that can quite easily be done with
|
||||
# a mypy plugin.
|
||||
# Something like `is_scalar = bool(__subscripts.partition("->")[-1])`
|
||||
@overload
|
||||
def einsum(
|
||||
subscripts: str | _ArrayLikeInt_co,
|
||||
/,
|
||||
*operands: _ArrayLikeBool_co,
|
||||
out: None = ...,
|
||||
dtype: None | _DTypeLikeBool = ...,
|
||||
order: _OrderKACF = ...,
|
||||
casting: _CastingSafe = ...,
|
||||
optimize: _OptimizeKind = ...,
|
||||
) -> Any: ...
|
||||
@overload
|
||||
def einsum(
|
||||
subscripts: str | _ArrayLikeInt_co,
|
||||
/,
|
||||
*operands: _ArrayLikeUInt_co,
|
||||
out: None = ...,
|
||||
dtype: None | _DTypeLikeUInt = ...,
|
||||
order: _OrderKACF = ...,
|
||||
casting: _CastingSafe = ...,
|
||||
optimize: _OptimizeKind = ...,
|
||||
) -> Any: ...
|
||||
@overload
|
||||
def einsum(
|
||||
subscripts: str | _ArrayLikeInt_co,
|
||||
/,
|
||||
*operands: _ArrayLikeInt_co,
|
||||
out: None = ...,
|
||||
dtype: None | _DTypeLikeInt = ...,
|
||||
order: _OrderKACF = ...,
|
||||
casting: _CastingSafe = ...,
|
||||
optimize: _OptimizeKind = ...,
|
||||
) -> Any: ...
|
||||
@overload
|
||||
def einsum(
|
||||
subscripts: str | _ArrayLikeInt_co,
|
||||
/,
|
||||
*operands: _ArrayLikeFloat_co,
|
||||
out: None = ...,
|
||||
dtype: None | _DTypeLikeFloat = ...,
|
||||
order: _OrderKACF = ...,
|
||||
casting: _CastingSafe = ...,
|
||||
optimize: _OptimizeKind = ...,
|
||||
) -> Any: ...
|
||||
@overload
|
||||
def einsum(
|
||||
subscripts: str | _ArrayLikeInt_co,
|
||||
/,
|
||||
*operands: _ArrayLikeComplex_co,
|
||||
out: None = ...,
|
||||
dtype: None | _DTypeLikeComplex = ...,
|
||||
order: _OrderKACF = ...,
|
||||
casting: _CastingSafe = ...,
|
||||
optimize: _OptimizeKind = ...,
|
||||
) -> Any: ...
|
||||
@overload
|
||||
def einsum(
|
||||
subscripts: str | _ArrayLikeInt_co,
|
||||
/,
|
||||
*operands: Any,
|
||||
casting: _CastingUnsafe,
|
||||
dtype: None | _DTypeLikeComplex_co = ...,
|
||||
out: None = ...,
|
||||
order: _OrderKACF = ...,
|
||||
optimize: _OptimizeKind = ...,
|
||||
) -> Any: ...
|
||||
@overload
|
||||
def einsum(
|
||||
subscripts: str | _ArrayLikeInt_co,
|
||||
/,
|
||||
*operands: _ArrayLikeComplex_co,
|
||||
out: _ArrayType,
|
||||
dtype: None | _DTypeLikeComplex_co = ...,
|
||||
order: _OrderKACF = ...,
|
||||
casting: _CastingSafe = ...,
|
||||
optimize: _OptimizeKind = ...,
|
||||
) -> _ArrayType: ...
|
||||
@overload
|
||||
def einsum(
|
||||
subscripts: str | _ArrayLikeInt_co,
|
||||
/,
|
||||
*operands: Any,
|
||||
out: _ArrayType,
|
||||
casting: _CastingUnsafe,
|
||||
dtype: None | _DTypeLikeComplex_co = ...,
|
||||
order: _OrderKACF = ...,
|
||||
optimize: _OptimizeKind = ...,
|
||||
) -> _ArrayType: ...
|
||||
|
||||
@overload
|
||||
def einsum(
|
||||
subscripts: str | _ArrayLikeInt_co,
|
||||
/,
|
||||
*operands: _ArrayLikeObject_co,
|
||||
out: None = ...,
|
||||
dtype: None | _DTypeLikeObject = ...,
|
||||
order: _OrderKACF = ...,
|
||||
casting: _CastingSafe = ...,
|
||||
optimize: _OptimizeKind = ...,
|
||||
) -> Any: ...
|
||||
@overload
|
||||
def einsum(
|
||||
subscripts: str | _ArrayLikeInt_co,
|
||||
/,
|
||||
*operands: Any,
|
||||
casting: _CastingUnsafe,
|
||||
dtype: None | _DTypeLikeObject = ...,
|
||||
out: None = ...,
|
||||
order: _OrderKACF = ...,
|
||||
optimize: _OptimizeKind = ...,
|
||||
) -> Any: ...
|
||||
@overload
|
||||
def einsum(
|
||||
subscripts: str | _ArrayLikeInt_co,
|
||||
/,
|
||||
*operands: _ArrayLikeObject_co,
|
||||
out: _ArrayType,
|
||||
dtype: None | _DTypeLikeObject = ...,
|
||||
order: _OrderKACF = ...,
|
||||
casting: _CastingSafe = ...,
|
||||
optimize: _OptimizeKind = ...,
|
||||
) -> _ArrayType: ...
|
||||
@overload
|
||||
def einsum(
|
||||
subscripts: str | _ArrayLikeInt_co,
|
||||
/,
|
||||
*operands: Any,
|
||||
out: _ArrayType,
|
||||
casting: _CastingUnsafe,
|
||||
dtype: None | _DTypeLikeObject = ...,
|
||||
order: _OrderKACF = ...,
|
||||
optimize: _OptimizeKind = ...,
|
||||
) -> _ArrayType: ...
|
||||
|
||||
# NOTE: `einsum_call` is a hidden kwarg unavailable for public use.
|
||||
# It is therefore excluded from the signatures below.
|
||||
# NOTE: In practice the list consists of a `str` (first element)
|
||||
# and a variable number of integer tuples.
|
||||
def einsum_path(
|
||||
subscripts: str | _ArrayLikeInt_co,
|
||||
/,
|
||||
*operands: _ArrayLikeComplex_co | _DTypeLikeObject,
|
||||
optimize: _OptimizeKind = ...,
|
||||
) -> tuple[list[Any], str]: ...
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,559 @@
|
||||
import functools
|
||||
import warnings
|
||||
import operator
|
||||
import types
|
||||
|
||||
import numpy as np
|
||||
from . import numeric as _nx
|
||||
from .numeric import result_type, nan, asanyarray, ndim
|
||||
from numpy._core.multiarray import add_docstring
|
||||
from numpy._core._multiarray_umath import _array_converter
|
||||
from numpy._core import overrides
|
||||
|
||||
__all__ = ['logspace', 'linspace', 'geomspace']
|
||||
|
||||
|
||||
array_function_dispatch = functools.partial(
|
||||
overrides.array_function_dispatch, module='numpy')
|
||||
|
||||
|
||||
def _linspace_dispatcher(start, stop, num=None, endpoint=None, retstep=None,
|
||||
dtype=None, axis=None, *, device=None):
|
||||
return (start, stop)
|
||||
|
||||
|
||||
@array_function_dispatch(_linspace_dispatcher)
|
||||
def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None,
|
||||
axis=0, *, device=None):
|
||||
"""
|
||||
Return evenly spaced numbers over a specified interval.
|
||||
|
||||
Returns `num` evenly spaced samples, calculated over the
|
||||
interval [`start`, `stop`].
|
||||
|
||||
The endpoint of the interval can optionally be excluded.
|
||||
|
||||
.. versionchanged:: 1.16.0
|
||||
Non-scalar `start` and `stop` are now supported.
|
||||
|
||||
.. versionchanged:: 1.20.0
|
||||
Values are rounded towards ``-inf`` instead of ``0`` when an
|
||||
integer ``dtype`` is specified. The old behavior can
|
||||
still be obtained with ``np.linspace(start, stop, num).astype(int)``
|
||||
|
||||
Parameters
|
||||
----------
|
||||
start : array_like
|
||||
The starting value of the sequence.
|
||||
stop : array_like
|
||||
The end value of the sequence, unless `endpoint` is set to False.
|
||||
In that case, the sequence consists of all but the last of ``num + 1``
|
||||
evenly spaced samples, so that `stop` is excluded. Note that the step
|
||||
size changes when `endpoint` is False.
|
||||
num : int, optional
|
||||
Number of samples to generate. Default is 50. Must be non-negative.
|
||||
endpoint : bool, optional
|
||||
If True, `stop` is the last sample. Otherwise, it is not included.
|
||||
Default is True.
|
||||
retstep : bool, optional
|
||||
If True, return (`samples`, `step`), where `step` is the spacing
|
||||
between samples.
|
||||
dtype : dtype, optional
|
||||
The type of the output array. If `dtype` is not given, the data type
|
||||
is inferred from `start` and `stop`. The inferred dtype will never be
|
||||
an integer; `float` is chosen even if the arguments would produce an
|
||||
array of integers.
|
||||
|
||||
.. versionadded:: 1.9.0
|
||||
axis : int, optional
|
||||
The axis in the result to store the samples. Relevant only if start
|
||||
or stop are array-like. By default (0), the samples will be along a
|
||||
new axis inserted at the beginning. Use -1 to get an axis at the end.
|
||||
|
||||
.. versionadded:: 1.16.0
|
||||
device : str, optional
|
||||
The device on which to place the created array. Default: None.
|
||||
For Array-API interoperability only, so must be ``"cpu"`` if passed.
|
||||
|
||||
.. versionadded:: 2.0.0
|
||||
|
||||
Returns
|
||||
-------
|
||||
samples : ndarray
|
||||
There are `num` equally spaced samples in the closed interval
|
||||
``[start, stop]`` or the half-open interval ``[start, stop)``
|
||||
(depending on whether `endpoint` is True or False).
|
||||
step : float, optional
|
||||
Only returned if `retstep` is True
|
||||
|
||||
Size of spacing between samples.
|
||||
|
||||
|
||||
See Also
|
||||
--------
|
||||
arange : Similar to `linspace`, but uses a step size (instead of the
|
||||
number of samples).
|
||||
geomspace : Similar to `linspace`, but with numbers spaced evenly on a log
|
||||
scale (a geometric progression).
|
||||
logspace : Similar to `geomspace`, but with the end points specified as
|
||||
logarithms.
|
||||
:ref:`how-to-partition`
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> np.linspace(2.0, 3.0, num=5)
|
||||
array([2. , 2.25, 2.5 , 2.75, 3. ])
|
||||
>>> np.linspace(2.0, 3.0, num=5, endpoint=False)
|
||||
array([2. , 2.2, 2.4, 2.6, 2.8])
|
||||
>>> np.linspace(2.0, 3.0, num=5, retstep=True)
|
||||
(array([2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)
|
||||
|
||||
Graphical illustration:
|
||||
|
||||
>>> import matplotlib.pyplot as plt
|
||||
>>> N = 8
|
||||
>>> y = np.zeros(N)
|
||||
>>> x1 = np.linspace(0, 10, N, endpoint=True)
|
||||
>>> x2 = np.linspace(0, 10, N, endpoint=False)
|
||||
>>> plt.plot(x1, y, 'o')
|
||||
[<matplotlib.lines.Line2D object at 0x...>]
|
||||
>>> plt.plot(x2, y + 0.5, 'o')
|
||||
[<matplotlib.lines.Line2D object at 0x...>]
|
||||
>>> plt.ylim([-0.5, 1])
|
||||
(-0.5, 1)
|
||||
>>> plt.show()
|
||||
|
||||
"""
|
||||
num = operator.index(num)
|
||||
if num < 0:
|
||||
raise ValueError(
|
||||
"Number of samples, %s, must be non-negative." % num
|
||||
)
|
||||
div = (num - 1) if endpoint else num
|
||||
|
||||
conv = _array_converter(start, stop)
|
||||
start, stop = conv.as_arrays()
|
||||
dt = conv.result_type(ensure_inexact=True)
|
||||
|
||||
if dtype is None:
|
||||
dtype = dt
|
||||
integer_dtype = False
|
||||
else:
|
||||
integer_dtype = _nx.issubdtype(dtype, _nx.integer)
|
||||
|
||||
# Use `dtype=type(dt)` to enforce a floating point evaluation:
|
||||
delta = np.subtract(stop, start, dtype=type(dt))
|
||||
y = _nx.arange(
|
||||
0, num, dtype=dt, device=device
|
||||
).reshape((-1,) + (1,) * ndim(delta))
|
||||
|
||||
# In-place multiplication y *= delta/div is faster, but prevents
|
||||
# the multiplicant from overriding what class is produced, and thus
|
||||
# prevents, e.g. use of Quantities, see gh-7142. Hence, we multiply
|
||||
# in place only for standard scalar types.
|
||||
if div > 0:
|
||||
_mult_inplace = _nx.isscalar(delta)
|
||||
step = delta / div
|
||||
any_step_zero = (
|
||||
step == 0 if _mult_inplace else _nx.asanyarray(step == 0).any())
|
||||
if any_step_zero:
|
||||
# Special handling for denormal numbers, gh-5437
|
||||
y /= div
|
||||
if _mult_inplace:
|
||||
y *= delta
|
||||
else:
|
||||
y = y * delta
|
||||
else:
|
||||
if _mult_inplace:
|
||||
y *= step
|
||||
else:
|
||||
y = y * step
|
||||
else:
|
||||
# sequences with 0 items or 1 item with endpoint=True (i.e. div <= 0)
|
||||
# have an undefined step
|
||||
step = nan
|
||||
# Multiply with delta to allow possible override of output class.
|
||||
y = y * delta
|
||||
|
||||
y += start
|
||||
|
||||
if endpoint and num > 1:
|
||||
y[-1, ...] = stop
|
||||
|
||||
if axis != 0:
|
||||
y = _nx.moveaxis(y, 0, axis)
|
||||
|
||||
if integer_dtype:
|
||||
_nx.floor(y, out=y)
|
||||
|
||||
y = conv.wrap(y.astype(dtype, copy=False))
|
||||
if retstep:
|
||||
return y, step
|
||||
else:
|
||||
return y
|
||||
|
||||
|
||||
def _logspace_dispatcher(start, stop, num=None, endpoint=None, base=None,
|
||||
dtype=None, axis=None):
|
||||
return (start, stop, base)
|
||||
|
||||
|
||||
@array_function_dispatch(_logspace_dispatcher)
|
||||
def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None,
|
||||
axis=0):
|
||||
"""
|
||||
Return numbers spaced evenly on a log scale.
|
||||
|
||||
In linear space, the sequence starts at ``base ** start``
|
||||
(`base` to the power of `start`) and ends with ``base ** stop``
|
||||
(see `endpoint` below).
|
||||
|
||||
.. versionchanged:: 1.16.0
|
||||
Non-scalar `start` and `stop` are now supported.
|
||||
|
||||
.. versionchanged:: 1.25.0
|
||||
Non-scalar 'base` is now supported
|
||||
|
||||
Parameters
|
||||
----------
|
||||
start : array_like
|
||||
``base ** start`` is the starting value of the sequence.
|
||||
stop : array_like
|
||||
``base ** stop`` is the final value of the sequence, unless `endpoint`
|
||||
is False. In that case, ``num + 1`` values are spaced over the
|
||||
interval in log-space, of which all but the last (a sequence of
|
||||
length `num`) are returned.
|
||||
num : integer, optional
|
||||
Number of samples to generate. Default is 50.
|
||||
endpoint : boolean, optional
|
||||
If true, `stop` is the last sample. Otherwise, it is not included.
|
||||
Default is True.
|
||||
base : array_like, optional
|
||||
The base of the log space. The step size between the elements in
|
||||
``ln(samples) / ln(base)`` (or ``log_base(samples)``) is uniform.
|
||||
Default is 10.0.
|
||||
dtype : dtype
|
||||
The type of the output array. If `dtype` is not given, the data type
|
||||
is inferred from `start` and `stop`. The inferred type will never be
|
||||
an integer; `float` is chosen even if the arguments would produce an
|
||||
array of integers.
|
||||
axis : int, optional
|
||||
The axis in the result to store the samples. Relevant only if start,
|
||||
stop, or base are array-like. By default (0), the samples will be
|
||||
along a new axis inserted at the beginning. Use -1 to get an axis at
|
||||
the end.
|
||||
|
||||
.. versionadded:: 1.16.0
|
||||
|
||||
|
||||
Returns
|
||||
-------
|
||||
samples : ndarray
|
||||
`num` samples, equally spaced on a log scale.
|
||||
|
||||
See Also
|
||||
--------
|
||||
arange : Similar to linspace, with the step size specified instead of the
|
||||
number of samples. Note that, when used with a float endpoint, the
|
||||
endpoint may or may not be included.
|
||||
linspace : Similar to logspace, but with the samples uniformly distributed
|
||||
in linear space, instead of log space.
|
||||
geomspace : Similar to logspace, but with endpoints specified directly.
|
||||
:ref:`how-to-partition`
|
||||
|
||||
Notes
|
||||
-----
|
||||
If base is a scalar, logspace is equivalent to the code
|
||||
|
||||
>>> y = np.linspace(start, stop, num=num, endpoint=endpoint)
|
||||
... # doctest: +SKIP
|
||||
>>> power(base, y).astype(dtype)
|
||||
... # doctest: +SKIP
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> np.logspace(2.0, 3.0, num=4)
|
||||
array([ 100. , 215.443469 , 464.15888336, 1000. ])
|
||||
>>> np.logspace(2.0, 3.0, num=4, endpoint=False)
|
||||
array([100. , 177.827941 , 316.22776602, 562.34132519])
|
||||
>>> np.logspace(2.0, 3.0, num=4, base=2.0)
|
||||
array([4. , 5.0396842 , 6.34960421, 8. ])
|
||||
>>> np.logspace(2.0, 3.0, num=4, base=[2.0, 3.0], axis=-1)
|
||||
array([[ 4. , 5.0396842 , 6.34960421, 8. ],
|
||||
[ 9. , 12.98024613, 18.72075441, 27. ]])
|
||||
|
||||
Graphical illustration:
|
||||
|
||||
>>> import matplotlib.pyplot as plt
|
||||
>>> N = 10
|
||||
>>> x1 = np.logspace(0.1, 1, N, endpoint=True)
|
||||
>>> x2 = np.logspace(0.1, 1, N, endpoint=False)
|
||||
>>> y = np.zeros(N)
|
||||
>>> plt.plot(x1, y, 'o')
|
||||
[<matplotlib.lines.Line2D object at 0x...>]
|
||||
>>> plt.plot(x2, y + 0.5, 'o')
|
||||
[<matplotlib.lines.Line2D object at 0x...>]
|
||||
>>> plt.ylim([-0.5, 1])
|
||||
(-0.5, 1)
|
||||
>>> plt.show()
|
||||
|
||||
"""
|
||||
if not isinstance(base, (float, int)) and np.ndim(base):
|
||||
# If base is non-scalar, broadcast it with the others, since it
|
||||
# may influence how axis is interpreted.
|
||||
ndmax = np.broadcast(start, stop, base).ndim
|
||||
start, stop, base = (
|
||||
np.array(a, copy=None, subok=True, ndmin=ndmax)
|
||||
for a in (start, stop, base)
|
||||
)
|
||||
base = np.expand_dims(base, axis=axis)
|
||||
y = linspace(start, stop, num=num, endpoint=endpoint, axis=axis)
|
||||
if dtype is None:
|
||||
return _nx.power(base, y)
|
||||
return _nx.power(base, y).astype(dtype, copy=False)
|
||||
|
||||
|
||||
def _geomspace_dispatcher(start, stop, num=None, endpoint=None, dtype=None,
|
||||
axis=None):
|
||||
return (start, stop)
|
||||
|
||||
|
||||
@array_function_dispatch(_geomspace_dispatcher)
|
||||
def geomspace(start, stop, num=50, endpoint=True, dtype=None, axis=0):
|
||||
"""
|
||||
Return numbers spaced evenly on a log scale (a geometric progression).
|
||||
|
||||
This is similar to `logspace`, but with endpoints specified directly.
|
||||
Each output sample is a constant multiple of the previous.
|
||||
|
||||
.. versionchanged:: 1.16.0
|
||||
Non-scalar `start` and `stop` are now supported.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
start : array_like
|
||||
The starting value of the sequence.
|
||||
stop : array_like
|
||||
The final value of the sequence, unless `endpoint` is False.
|
||||
In that case, ``num + 1`` values are spaced over the
|
||||
interval in log-space, of which all but the last (a sequence of
|
||||
length `num`) are returned.
|
||||
num : integer, optional
|
||||
Number of samples to generate. Default is 50.
|
||||
endpoint : boolean, optional
|
||||
If true, `stop` is the last sample. Otherwise, it is not included.
|
||||
Default is True.
|
||||
dtype : dtype
|
||||
The type of the output array. If `dtype` is not given, the data type
|
||||
is inferred from `start` and `stop`. The inferred dtype will never be
|
||||
an integer; `float` is chosen even if the arguments would produce an
|
||||
array of integers.
|
||||
axis : int, optional
|
||||
The axis in the result to store the samples. Relevant only if start
|
||||
or stop are array-like. By default (0), the samples will be along a
|
||||
new axis inserted at the beginning. Use -1 to get an axis at the end.
|
||||
|
||||
.. versionadded:: 1.16.0
|
||||
|
||||
Returns
|
||||
-------
|
||||
samples : ndarray
|
||||
`num` samples, equally spaced on a log scale.
|
||||
|
||||
See Also
|
||||
--------
|
||||
logspace : Similar to geomspace, but with endpoints specified using log
|
||||
and base.
|
||||
linspace : Similar to geomspace, but with arithmetic instead of geometric
|
||||
progression.
|
||||
arange : Similar to linspace, with the step size specified instead of the
|
||||
number of samples.
|
||||
:ref:`how-to-partition`
|
||||
|
||||
Notes
|
||||
-----
|
||||
If the inputs or dtype are complex, the output will follow a logarithmic
|
||||
spiral in the complex plane. (There are an infinite number of spirals
|
||||
passing through two points; the output will follow the shortest such path.)
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> np.geomspace(1, 1000, num=4)
|
||||
array([ 1., 10., 100., 1000.])
|
||||
>>> np.geomspace(1, 1000, num=3, endpoint=False)
|
||||
array([ 1., 10., 100.])
|
||||
>>> np.geomspace(1, 1000, num=4, endpoint=False)
|
||||
array([ 1. , 5.62341325, 31.6227766 , 177.827941 ])
|
||||
>>> np.geomspace(1, 256, num=9)
|
||||
array([ 1., 2., 4., 8., 16., 32., 64., 128., 256.])
|
||||
|
||||
Note that the above may not produce exact integers:
|
||||
|
||||
>>> np.geomspace(1, 256, num=9, dtype=int)
|
||||
array([ 1, 2, 4, 7, 16, 32, 63, 127, 256])
|
||||
>>> np.around(np.geomspace(1, 256, num=9)).astype(int)
|
||||
array([ 1, 2, 4, 8, 16, 32, 64, 128, 256])
|
||||
|
||||
Negative, decreasing, and complex inputs are allowed:
|
||||
|
||||
>>> np.geomspace(1000, 1, num=4)
|
||||
array([1000., 100., 10., 1.])
|
||||
>>> np.geomspace(-1000, -1, num=4)
|
||||
array([-1000., -100., -10., -1.])
|
||||
>>> np.geomspace(1j, 1000j, num=4) # Straight line
|
||||
array([0. +1.j, 0. +10.j, 0. +100.j, 0.+1000.j])
|
||||
>>> np.geomspace(-1+0j, 1+0j, num=5) # Circle
|
||||
array([-1.00000000e+00+1.22464680e-16j, -7.07106781e-01+7.07106781e-01j,
|
||||
6.12323400e-17+1.00000000e+00j, 7.07106781e-01+7.07106781e-01j,
|
||||
1.00000000e+00+0.00000000e+00j])
|
||||
|
||||
Graphical illustration of `endpoint` parameter:
|
||||
|
||||
>>> import matplotlib.pyplot as plt
|
||||
>>> N = 10
|
||||
>>> y = np.zeros(N)
|
||||
>>> plt.semilogx(np.geomspace(1, 1000, N, endpoint=True), y + 1, 'o')
|
||||
[<matplotlib.lines.Line2D object at 0x...>]
|
||||
>>> plt.semilogx(np.geomspace(1, 1000, N, endpoint=False), y + 2, 'o')
|
||||
[<matplotlib.lines.Line2D object at 0x...>]
|
||||
>>> plt.axis([0.5, 2000, 0, 3])
|
||||
[0.5, 2000, 0, 3]
|
||||
>>> plt.grid(True, color='0.7', linestyle='-', which='both', axis='both')
|
||||
>>> plt.show()
|
||||
|
||||
"""
|
||||
start = asanyarray(start)
|
||||
stop = asanyarray(stop)
|
||||
if _nx.any(start == 0) or _nx.any(stop == 0):
|
||||
raise ValueError('Geometric sequence cannot include zero')
|
||||
|
||||
dt = result_type(start, stop, float(num), _nx.zeros((), dtype))
|
||||
if dtype is None:
|
||||
dtype = dt
|
||||
else:
|
||||
# complex to dtype('complex128'), for instance
|
||||
dtype = _nx.dtype(dtype)
|
||||
|
||||
# Promote both arguments to the same dtype in case, for instance, one is
|
||||
# complex and another is negative and log would produce NaN otherwise.
|
||||
# Copy since we may change things in-place further down.
|
||||
start = start.astype(dt, copy=True)
|
||||
stop = stop.astype(dt, copy=True)
|
||||
|
||||
# Allow negative real values and ensure a consistent result for complex
|
||||
# (including avoiding negligible real or imaginary parts in output) by
|
||||
# rotating start to positive real, calculating, then undoing rotation.
|
||||
out_sign = _nx.sign(start)
|
||||
start /= out_sign
|
||||
stop = stop / out_sign
|
||||
|
||||
log_start = _nx.log10(start)
|
||||
log_stop = _nx.log10(stop)
|
||||
result = logspace(log_start, log_stop, num=num,
|
||||
endpoint=endpoint, base=10.0, dtype=dt)
|
||||
|
||||
# Make sure the endpoints match the start and stop arguments. This is
|
||||
# necessary because np.exp(np.log(x)) is not necessarily equal to x.
|
||||
if num > 0:
|
||||
result[0] = start
|
||||
if num > 1 and endpoint:
|
||||
result[-1] = stop
|
||||
|
||||
result *= out_sign
|
||||
|
||||
if axis != 0:
|
||||
result = _nx.moveaxis(result, 0, axis)
|
||||
|
||||
return result.astype(dtype, copy=False)
|
||||
|
||||
|
||||
def _needs_add_docstring(obj):
|
||||
"""
|
||||
Returns true if the only way to set the docstring of `obj` from python is
|
||||
via add_docstring.
|
||||
|
||||
This function errs on the side of being overly conservative.
|
||||
"""
|
||||
Py_TPFLAGS_HEAPTYPE = 1 << 9
|
||||
|
||||
if isinstance(obj, (types.FunctionType, types.MethodType, property)):
|
||||
return False
|
||||
|
||||
if isinstance(obj, type) and obj.__flags__ & Py_TPFLAGS_HEAPTYPE:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def _add_docstring(obj, doc, warn_on_python):
|
||||
if warn_on_python and not _needs_add_docstring(obj):
|
||||
warnings.warn(
|
||||
"add_newdoc was used on a pure-python object {}. "
|
||||
"Prefer to attach it directly to the source."
|
||||
.format(obj),
|
||||
UserWarning,
|
||||
stacklevel=3)
|
||||
try:
|
||||
add_docstring(obj, doc)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def add_newdoc(place, obj, doc, warn_on_python=True):
|
||||
"""
|
||||
Add documentation to an existing object, typically one defined in C
|
||||
|
||||
The purpose is to allow easier editing of the docstrings without requiring
|
||||
a re-compile. This exists primarily for internal use within numpy itself.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
place : str
|
||||
The absolute name of the module to import from
|
||||
obj : str or None
|
||||
The name of the object to add documentation to, typically a class or
|
||||
function name.
|
||||
doc : {str, Tuple[str, str], List[Tuple[str, str]]}
|
||||
If a string, the documentation to apply to `obj`
|
||||
|
||||
If a tuple, then the first element is interpreted as an attribute
|
||||
of `obj` and the second as the docstring to apply -
|
||||
``(method, docstring)``
|
||||
|
||||
If a list, then each element of the list should be a tuple of length
|
||||
two - ``[(method1, docstring1), (method2, docstring2), ...]``
|
||||
warn_on_python : bool
|
||||
If True, the default, emit `UserWarning` if this is used to attach
|
||||
documentation to a pure-python object.
|
||||
|
||||
Notes
|
||||
-----
|
||||
This routine never raises an error if the docstring can't be written, but
|
||||
will raise an error if the object being documented does not exist.
|
||||
|
||||
This routine cannot modify read-only docstrings, as appear
|
||||
in new-style classes or built-in functions. Because this
|
||||
routine never raises an error the caller must check manually
|
||||
that the docstrings were changed.
|
||||
|
||||
Since this function grabs the ``char *`` from a c-level str object and puts
|
||||
it into the ``tp_doc`` slot of the type of `obj`, it violates a number of
|
||||
C-API best-practices, by:
|
||||
|
||||
- modifying a `PyTypeObject` after calling `PyType_Ready`
|
||||
- calling `Py_INCREF` on the str and losing the reference, so the str
|
||||
will never be released
|
||||
|
||||
If possible it should be avoided.
|
||||
"""
|
||||
new = getattr(__import__(place, globals(), {}, [obj]), obj)
|
||||
if isinstance(doc, str):
|
||||
_add_docstring(new, doc.strip(), warn_on_python)
|
||||
elif isinstance(doc, tuple):
|
||||
attr, docstring = doc
|
||||
_add_docstring(getattr(new, attr), docstring.strip(), warn_on_python)
|
||||
elif isinstance(doc, list):
|
||||
for attr, docstring in doc:
|
||||
_add_docstring(
|
||||
getattr(new, attr), docstring.strip(), warn_on_python
|
||||
)
|
||||
@ -0,0 +1,202 @@
|
||||
from typing import (
|
||||
Literal as L,
|
||||
overload,
|
||||
Any,
|
||||
SupportsIndex,
|
||||
TypeVar,
|
||||
)
|
||||
|
||||
from numpy import floating, complexfloating, generic
|
||||
from numpy._typing import (
|
||||
NDArray,
|
||||
DTypeLike,
|
||||
_DTypeLike,
|
||||
_ArrayLikeFloat_co,
|
||||
_ArrayLikeComplex_co,
|
||||
)
|
||||
|
||||
_SCT = TypeVar("_SCT", bound=generic)
|
||||
|
||||
__all__: list[str]
|
||||
|
||||
@overload
|
||||
def linspace(
|
||||
start: _ArrayLikeFloat_co,
|
||||
stop: _ArrayLikeFloat_co,
|
||||
num: SupportsIndex = ...,
|
||||
endpoint: bool = ...,
|
||||
retstep: L[False] = ...,
|
||||
dtype: None = ...,
|
||||
axis: SupportsIndex = ...,
|
||||
*,
|
||||
device: None | L["cpu"] = ...,
|
||||
) -> NDArray[floating[Any]]: ...
|
||||
@overload
|
||||
def linspace(
|
||||
start: _ArrayLikeComplex_co,
|
||||
stop: _ArrayLikeComplex_co,
|
||||
num: SupportsIndex = ...,
|
||||
endpoint: bool = ...,
|
||||
retstep: L[False] = ...,
|
||||
dtype: None = ...,
|
||||
axis: SupportsIndex = ...,
|
||||
*,
|
||||
device: None | L["cpu"] = ...,
|
||||
) -> NDArray[complexfloating[Any, Any]]: ...
|
||||
@overload
|
||||
def linspace(
|
||||
start: _ArrayLikeComplex_co,
|
||||
stop: _ArrayLikeComplex_co,
|
||||
num: SupportsIndex = ...,
|
||||
endpoint: bool = ...,
|
||||
retstep: L[False] = ...,
|
||||
dtype: _DTypeLike[_SCT] = ...,
|
||||
axis: SupportsIndex = ...,
|
||||
*,
|
||||
device: None | L["cpu"] = ...,
|
||||
) -> NDArray[_SCT]: ...
|
||||
@overload
|
||||
def linspace(
|
||||
start: _ArrayLikeComplex_co,
|
||||
stop: _ArrayLikeComplex_co,
|
||||
num: SupportsIndex = ...,
|
||||
endpoint: bool = ...,
|
||||
retstep: L[False] = ...,
|
||||
dtype: DTypeLike = ...,
|
||||
axis: SupportsIndex = ...,
|
||||
*,
|
||||
device: None | L["cpu"] = ...,
|
||||
) -> NDArray[Any]: ...
|
||||
@overload
|
||||
def linspace(
|
||||
start: _ArrayLikeFloat_co,
|
||||
stop: _ArrayLikeFloat_co,
|
||||
num: SupportsIndex = ...,
|
||||
endpoint: bool = ...,
|
||||
retstep: L[True] = ...,
|
||||
dtype: None = ...,
|
||||
axis: SupportsIndex = ...,
|
||||
*,
|
||||
device: None | L["cpu"] = ...,
|
||||
) -> tuple[NDArray[floating[Any]], floating[Any]]: ...
|
||||
@overload
|
||||
def linspace(
|
||||
start: _ArrayLikeComplex_co,
|
||||
stop: _ArrayLikeComplex_co,
|
||||
num: SupportsIndex = ...,
|
||||
endpoint: bool = ...,
|
||||
retstep: L[True] = ...,
|
||||
dtype: None = ...,
|
||||
axis: SupportsIndex = ...,
|
||||
*,
|
||||
device: None | L["cpu"] = ...,
|
||||
) -> tuple[NDArray[complexfloating[Any, Any]], complexfloating[Any, Any]]: ...
|
||||
@overload
|
||||
def linspace(
|
||||
start: _ArrayLikeComplex_co,
|
||||
stop: _ArrayLikeComplex_co,
|
||||
num: SupportsIndex = ...,
|
||||
endpoint: bool = ...,
|
||||
retstep: L[True] = ...,
|
||||
dtype: _DTypeLike[_SCT] = ...,
|
||||
axis: SupportsIndex = ...,
|
||||
*,
|
||||
device: None | L["cpu"] = ...,
|
||||
) -> tuple[NDArray[_SCT], _SCT]: ...
|
||||
@overload
|
||||
def linspace(
|
||||
start: _ArrayLikeComplex_co,
|
||||
stop: _ArrayLikeComplex_co,
|
||||
num: SupportsIndex = ...,
|
||||
endpoint: bool = ...,
|
||||
retstep: L[True] = ...,
|
||||
dtype: DTypeLike = ...,
|
||||
axis: SupportsIndex = ...,
|
||||
*,
|
||||
device: None | L["cpu"] = ...,
|
||||
) -> tuple[NDArray[Any], Any]: ...
|
||||
|
||||
@overload
|
||||
def logspace(
|
||||
start: _ArrayLikeFloat_co,
|
||||
stop: _ArrayLikeFloat_co,
|
||||
num: SupportsIndex = ...,
|
||||
endpoint: bool = ...,
|
||||
base: _ArrayLikeFloat_co = ...,
|
||||
dtype: None = ...,
|
||||
axis: SupportsIndex = ...,
|
||||
) -> NDArray[floating[Any]]: ...
|
||||
@overload
|
||||
def logspace(
|
||||
start: _ArrayLikeComplex_co,
|
||||
stop: _ArrayLikeComplex_co,
|
||||
num: SupportsIndex = ...,
|
||||
endpoint: bool = ...,
|
||||
base: _ArrayLikeComplex_co = ...,
|
||||
dtype: None = ...,
|
||||
axis: SupportsIndex = ...,
|
||||
) -> NDArray[complexfloating[Any, Any]]: ...
|
||||
@overload
|
||||
def logspace(
|
||||
start: _ArrayLikeComplex_co,
|
||||
stop: _ArrayLikeComplex_co,
|
||||
num: SupportsIndex = ...,
|
||||
endpoint: bool = ...,
|
||||
base: _ArrayLikeComplex_co = ...,
|
||||
dtype: _DTypeLike[_SCT] = ...,
|
||||
axis: SupportsIndex = ...,
|
||||
) -> NDArray[_SCT]: ...
|
||||
@overload
|
||||
def logspace(
|
||||
start: _ArrayLikeComplex_co,
|
||||
stop: _ArrayLikeComplex_co,
|
||||
num: SupportsIndex = ...,
|
||||
endpoint: bool = ...,
|
||||
base: _ArrayLikeComplex_co = ...,
|
||||
dtype: DTypeLike = ...,
|
||||
axis: SupportsIndex = ...,
|
||||
) -> NDArray[Any]: ...
|
||||
|
||||
@overload
|
||||
def geomspace(
|
||||
start: _ArrayLikeFloat_co,
|
||||
stop: _ArrayLikeFloat_co,
|
||||
num: SupportsIndex = ...,
|
||||
endpoint: bool = ...,
|
||||
dtype: None = ...,
|
||||
axis: SupportsIndex = ...,
|
||||
) -> NDArray[floating[Any]]: ...
|
||||
@overload
|
||||
def geomspace(
|
||||
start: _ArrayLikeComplex_co,
|
||||
stop: _ArrayLikeComplex_co,
|
||||
num: SupportsIndex = ...,
|
||||
endpoint: bool = ...,
|
||||
dtype: None = ...,
|
||||
axis: SupportsIndex = ...,
|
||||
) -> NDArray[complexfloating[Any, Any]]: ...
|
||||
@overload
|
||||
def geomspace(
|
||||
start: _ArrayLikeComplex_co,
|
||||
stop: _ArrayLikeComplex_co,
|
||||
num: SupportsIndex = ...,
|
||||
endpoint: bool = ...,
|
||||
dtype: _DTypeLike[_SCT] = ...,
|
||||
axis: SupportsIndex = ...,
|
||||
) -> NDArray[_SCT]: ...
|
||||
@overload
|
||||
def geomspace(
|
||||
start: _ArrayLikeComplex_co,
|
||||
stop: _ArrayLikeComplex_co,
|
||||
num: SupportsIndex = ...,
|
||||
endpoint: bool = ...,
|
||||
dtype: DTypeLike = ...,
|
||||
axis: SupportsIndex = ...,
|
||||
) -> NDArray[Any]: ...
|
||||
|
||||
def add_newdoc(
|
||||
place: str,
|
||||
obj: str,
|
||||
doc: str | tuple[str, str] | list[tuple[str, str]],
|
||||
warn_on_python: bool = ...,
|
||||
) -> None: ...
|
||||
@ -0,0 +1,738 @@
|
||||
"""Machine limits for Float32 and Float64 and (long double) if available...
|
||||
|
||||
"""
|
||||
__all__ = ['finfo', 'iinfo']
|
||||
|
||||
import warnings
|
||||
|
||||
from .._utils import set_module
|
||||
from ._machar import MachAr
|
||||
from . import numeric
|
||||
from . import numerictypes as ntypes
|
||||
from .numeric import array, inf, nan
|
||||
from .umath import log10, exp2, nextafter, isnan
|
||||
|
||||
|
||||
def _fr0(a):
|
||||
"""fix rank-0 --> rank-1"""
|
||||
if a.ndim == 0:
|
||||
a = a.copy()
|
||||
a.shape = (1,)
|
||||
return a
|
||||
|
||||
|
||||
def _fr1(a):
|
||||
"""fix rank > 0 --> rank-0"""
|
||||
if a.size == 1:
|
||||
a = a.copy()
|
||||
a.shape = ()
|
||||
return a
|
||||
|
||||
|
||||
class MachArLike:
|
||||
""" Object to simulate MachAr instance """
|
||||
def __init__(self, ftype, *, eps, epsneg, huge, tiny,
|
||||
ibeta, smallest_subnormal=None, **kwargs):
|
||||
self.params = _MACHAR_PARAMS[ftype]
|
||||
self.ftype = ftype
|
||||
self.title = self.params['title']
|
||||
# Parameter types same as for discovered MachAr object.
|
||||
if not smallest_subnormal:
|
||||
self._smallest_subnormal = nextafter(
|
||||
self.ftype(0), self.ftype(1), dtype=self.ftype)
|
||||
else:
|
||||
self._smallest_subnormal = smallest_subnormal
|
||||
self.epsilon = self.eps = self._float_to_float(eps)
|
||||
self.epsneg = self._float_to_float(epsneg)
|
||||
self.xmax = self.huge = self._float_to_float(huge)
|
||||
self.xmin = self._float_to_float(tiny)
|
||||
self.smallest_normal = self.tiny = self._float_to_float(tiny)
|
||||
self.ibeta = self.params['itype'](ibeta)
|
||||
self.__dict__.update(kwargs)
|
||||
self.precision = int(-log10(self.eps))
|
||||
self.resolution = self._float_to_float(
|
||||
self._float_conv(10) ** (-self.precision))
|
||||
self._str_eps = self._float_to_str(self.eps)
|
||||
self._str_epsneg = self._float_to_str(self.epsneg)
|
||||
self._str_xmin = self._float_to_str(self.xmin)
|
||||
self._str_xmax = self._float_to_str(self.xmax)
|
||||
self._str_resolution = self._float_to_str(self.resolution)
|
||||
self._str_smallest_normal = self._float_to_str(self.xmin)
|
||||
|
||||
@property
|
||||
def smallest_subnormal(self):
|
||||
"""Return the value for the smallest subnormal.
|
||||
|
||||
Returns
|
||||
-------
|
||||
smallest_subnormal : float
|
||||
value for the smallest subnormal.
|
||||
|
||||
Warns
|
||||
-----
|
||||
UserWarning
|
||||
If the calculated value for the smallest subnormal is zero.
|
||||
"""
|
||||
# Check that the calculated value is not zero, in case it raises a
|
||||
# warning.
|
||||
value = self._smallest_subnormal
|
||||
if self.ftype(0) == value:
|
||||
warnings.warn(
|
||||
'The value of the smallest subnormal for {} type '
|
||||
'is zero.'.format(self.ftype), UserWarning, stacklevel=2)
|
||||
|
||||
return self._float_to_float(value)
|
||||
|
||||
@property
|
||||
def _str_smallest_subnormal(self):
|
||||
"""Return the string representation of the smallest subnormal."""
|
||||
return self._float_to_str(self.smallest_subnormal)
|
||||
|
||||
def _float_to_float(self, value):
|
||||
"""Converts float to float.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
value : float
|
||||
value to be converted.
|
||||
"""
|
||||
return _fr1(self._float_conv(value))
|
||||
|
||||
def _float_conv(self, value):
|
||||
"""Converts float to conv.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
value : float
|
||||
value to be converted.
|
||||
"""
|
||||
return array([value], self.ftype)
|
||||
|
||||
def _float_to_str(self, value):
|
||||
"""Converts float to str.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
value : float
|
||||
value to be converted.
|
||||
"""
|
||||
return self.params['fmt'] % array(_fr0(value)[0], self.ftype)
|
||||
|
||||
|
||||
_convert_to_float = {
|
||||
ntypes.csingle: ntypes.single,
|
||||
ntypes.complex128: ntypes.float64,
|
||||
ntypes.clongdouble: ntypes.longdouble
|
||||
}
|
||||
|
||||
# Parameters for creating MachAr / MachAr-like objects
|
||||
_title_fmt = 'numpy {} precision floating point number'
|
||||
_MACHAR_PARAMS = {
|
||||
ntypes.double: dict(
|
||||
itype = ntypes.int64,
|
||||
fmt = '%24.16e',
|
||||
title = _title_fmt.format('double')),
|
||||
ntypes.single: dict(
|
||||
itype = ntypes.int32,
|
||||
fmt = '%15.7e',
|
||||
title = _title_fmt.format('single')),
|
||||
ntypes.longdouble: dict(
|
||||
itype = ntypes.longlong,
|
||||
fmt = '%s',
|
||||
title = _title_fmt.format('long double')),
|
||||
ntypes.half: dict(
|
||||
itype = ntypes.int16,
|
||||
fmt = '%12.5e',
|
||||
title = _title_fmt.format('half'))}
|
||||
|
||||
# Key to identify the floating point type. Key is result of
|
||||
# ftype('-0.1').newbyteorder('<').tobytes()
|
||||
#
|
||||
# 20230201 - use (ftype(-1.0) / ftype(10.0)).newbyteorder('<').tobytes()
|
||||
# instead because stold may have deficiencies on some platforms.
|
||||
# See:
|
||||
# https://perl5.git.perl.org/perl.git/blob/3118d7d684b56cbeb702af874f4326683c45f045:/Configure
|
||||
|
||||
_KNOWN_TYPES = {}
|
||||
def _register_type(machar, bytepat):
|
||||
_KNOWN_TYPES[bytepat] = machar
|
||||
|
||||
|
||||
_float_ma = {}
|
||||
|
||||
|
||||
def _register_known_types():
|
||||
# Known parameters for float16
|
||||
# See docstring of MachAr class for description of parameters.
|
||||
f16 = ntypes.float16
|
||||
float16_ma = MachArLike(f16,
|
||||
machep=-10,
|
||||
negep=-11,
|
||||
minexp=-14,
|
||||
maxexp=16,
|
||||
it=10,
|
||||
iexp=5,
|
||||
ibeta=2,
|
||||
irnd=5,
|
||||
ngrd=0,
|
||||
eps=exp2(f16(-10)),
|
||||
epsneg=exp2(f16(-11)),
|
||||
huge=f16(65504),
|
||||
tiny=f16(2 ** -14))
|
||||
_register_type(float16_ma, b'f\xae')
|
||||
_float_ma[16] = float16_ma
|
||||
|
||||
# Known parameters for float32
|
||||
f32 = ntypes.float32
|
||||
float32_ma = MachArLike(f32,
|
||||
machep=-23,
|
||||
negep=-24,
|
||||
minexp=-126,
|
||||
maxexp=128,
|
||||
it=23,
|
||||
iexp=8,
|
||||
ibeta=2,
|
||||
irnd=5,
|
||||
ngrd=0,
|
||||
eps=exp2(f32(-23)),
|
||||
epsneg=exp2(f32(-24)),
|
||||
huge=f32((1 - 2 ** -24) * 2**128),
|
||||
tiny=exp2(f32(-126)))
|
||||
_register_type(float32_ma, b'\xcd\xcc\xcc\xbd')
|
||||
_float_ma[32] = float32_ma
|
||||
|
||||
# Known parameters for float64
|
||||
f64 = ntypes.float64
|
||||
epsneg_f64 = 2.0 ** -53.0
|
||||
tiny_f64 = 2.0 ** -1022.0
|
||||
float64_ma = MachArLike(f64,
|
||||
machep=-52,
|
||||
negep=-53,
|
||||
minexp=-1022,
|
||||
maxexp=1024,
|
||||
it=52,
|
||||
iexp=11,
|
||||
ibeta=2,
|
||||
irnd=5,
|
||||
ngrd=0,
|
||||
eps=2.0 ** -52.0,
|
||||
epsneg=epsneg_f64,
|
||||
huge=(1.0 - epsneg_f64) / tiny_f64 * f64(4),
|
||||
tiny=tiny_f64)
|
||||
_register_type(float64_ma, b'\x9a\x99\x99\x99\x99\x99\xb9\xbf')
|
||||
_float_ma[64] = float64_ma
|
||||
|
||||
# Known parameters for IEEE 754 128-bit binary float
|
||||
ld = ntypes.longdouble
|
||||
epsneg_f128 = exp2(ld(-113))
|
||||
tiny_f128 = exp2(ld(-16382))
|
||||
# Ignore runtime error when this is not f128
|
||||
with numeric.errstate(all='ignore'):
|
||||
huge_f128 = (ld(1) - epsneg_f128) / tiny_f128 * ld(4)
|
||||
float128_ma = MachArLike(ld,
|
||||
machep=-112,
|
||||
negep=-113,
|
||||
minexp=-16382,
|
||||
maxexp=16384,
|
||||
it=112,
|
||||
iexp=15,
|
||||
ibeta=2,
|
||||
irnd=5,
|
||||
ngrd=0,
|
||||
eps=exp2(ld(-112)),
|
||||
epsneg=epsneg_f128,
|
||||
huge=huge_f128,
|
||||
tiny=tiny_f128)
|
||||
# IEEE 754 128-bit binary float
|
||||
_register_type(float128_ma,
|
||||
b'\x9a\x99\x99\x99\x99\x99\x99\x99\x99\x99\x99\x99\x99\x99\xfb\xbf')
|
||||
_float_ma[128] = float128_ma
|
||||
|
||||
# Known parameters for float80 (Intel 80-bit extended precision)
|
||||
epsneg_f80 = exp2(ld(-64))
|
||||
tiny_f80 = exp2(ld(-16382))
|
||||
# Ignore runtime error when this is not f80
|
||||
with numeric.errstate(all='ignore'):
|
||||
huge_f80 = (ld(1) - epsneg_f80) / tiny_f80 * ld(4)
|
||||
float80_ma = MachArLike(ld,
|
||||
machep=-63,
|
||||
negep=-64,
|
||||
minexp=-16382,
|
||||
maxexp=16384,
|
||||
it=63,
|
||||
iexp=15,
|
||||
ibeta=2,
|
||||
irnd=5,
|
||||
ngrd=0,
|
||||
eps=exp2(ld(-63)),
|
||||
epsneg=epsneg_f80,
|
||||
huge=huge_f80,
|
||||
tiny=tiny_f80)
|
||||
# float80, first 10 bytes containing actual storage
|
||||
_register_type(float80_ma, b'\xcd\xcc\xcc\xcc\xcc\xcc\xcc\xcc\xfb\xbf')
|
||||
_float_ma[80] = float80_ma
|
||||
|
||||
# Guessed / known parameters for double double; see:
|
||||
# https://en.wikipedia.org/wiki/Quadruple-precision_floating-point_format#Double-double_arithmetic
|
||||
# These numbers have the same exponent range as float64, but extended
|
||||
# number of digits in the significand.
|
||||
huge_dd = nextafter(ld(inf), ld(0), dtype=ld)
|
||||
# As the smallest_normal in double double is so hard to calculate we set
|
||||
# it to NaN.
|
||||
smallest_normal_dd = nan
|
||||
# Leave the same value for the smallest subnormal as double
|
||||
smallest_subnormal_dd = ld(nextafter(0., 1.))
|
||||
float_dd_ma = MachArLike(ld,
|
||||
machep=-105,
|
||||
negep=-106,
|
||||
minexp=-1022,
|
||||
maxexp=1024,
|
||||
it=105,
|
||||
iexp=11,
|
||||
ibeta=2,
|
||||
irnd=5,
|
||||
ngrd=0,
|
||||
eps=exp2(ld(-105)),
|
||||
epsneg=exp2(ld(-106)),
|
||||
huge=huge_dd,
|
||||
tiny=smallest_normal_dd,
|
||||
smallest_subnormal=smallest_subnormal_dd)
|
||||
# double double; low, high order (e.g. PPC 64)
|
||||
_register_type(float_dd_ma,
|
||||
b'\x9a\x99\x99\x99\x99\x99Y<\x9a\x99\x99\x99\x99\x99\xb9\xbf')
|
||||
# double double; high, low order (e.g. PPC 64 le)
|
||||
_register_type(float_dd_ma,
|
||||
b'\x9a\x99\x99\x99\x99\x99\xb9\xbf\x9a\x99\x99\x99\x99\x99Y<')
|
||||
_float_ma['dd'] = float_dd_ma
|
||||
|
||||
|
||||
def _get_machar(ftype):
|
||||
""" Get MachAr instance or MachAr-like instance
|
||||
|
||||
Get parameters for floating point type, by first trying signatures of
|
||||
various known floating point types, then, if none match, attempting to
|
||||
identify parameters by analysis.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
ftype : class
|
||||
Numpy floating point type class (e.g. ``np.float64``)
|
||||
|
||||
Returns
|
||||
-------
|
||||
ma_like : instance of :class:`MachAr` or :class:`MachArLike`
|
||||
Object giving floating point parameters for `ftype`.
|
||||
|
||||
Warns
|
||||
-----
|
||||
UserWarning
|
||||
If the binary signature of the float type is not in the dictionary of
|
||||
known float types.
|
||||
"""
|
||||
params = _MACHAR_PARAMS.get(ftype)
|
||||
if params is None:
|
||||
raise ValueError(repr(ftype))
|
||||
# Detect known / suspected types
|
||||
# ftype(-1.0) / ftype(10.0) is better than ftype('-0.1') because stold
|
||||
# may be deficient
|
||||
key = (ftype(-1.0) / ftype(10.))
|
||||
key = key.view(key.dtype.newbyteorder("<")).tobytes()
|
||||
ma_like = None
|
||||
if ftype == ntypes.longdouble:
|
||||
# Could be 80 bit == 10 byte extended precision, where last bytes can
|
||||
# be random garbage.
|
||||
# Comparing first 10 bytes to pattern first to avoid branching on the
|
||||
# random garbage.
|
||||
ma_like = _KNOWN_TYPES.get(key[:10])
|
||||
if ma_like is None:
|
||||
# see if the full key is known.
|
||||
ma_like = _KNOWN_TYPES.get(key)
|
||||
if ma_like is None and len(key) == 16:
|
||||
# machine limits could be f80 masquerading as np.float128,
|
||||
# find all keys with length 16 and make new dict, but make the keys
|
||||
# only 10 bytes long, the last bytes can be random garbage
|
||||
_kt = {k[:10]: v for k, v in _KNOWN_TYPES.items() if len(k) == 16}
|
||||
ma_like = _kt.get(key[:10])
|
||||
if ma_like is not None:
|
||||
return ma_like
|
||||
# Fall back to parameter discovery
|
||||
warnings.warn(
|
||||
f'Signature {key} for {ftype} does not match any known type: '
|
||||
'falling back to type probe function.\n'
|
||||
'This warnings indicates broken support for the dtype!',
|
||||
UserWarning, stacklevel=2)
|
||||
return _discovered_machar(ftype)
|
||||
|
||||
|
||||
def _discovered_machar(ftype):
|
||||
""" Create MachAr instance with found information on float types
|
||||
|
||||
TODO: MachAr should be retired completely ideally. We currently only
|
||||
ever use it system with broken longdouble (valgrind, WSL).
|
||||
"""
|
||||
params = _MACHAR_PARAMS[ftype]
|
||||
return MachAr(lambda v: array([v], ftype),
|
||||
lambda v: _fr0(v.astype(params['itype']))[0],
|
||||
lambda v: array(_fr0(v)[0], ftype),
|
||||
lambda v: params['fmt'] % array(_fr0(v)[0], ftype),
|
||||
params['title'])
|
||||
|
||||
|
||||
@set_module('numpy')
|
||||
class finfo:
|
||||
"""
|
||||
finfo(dtype)
|
||||
|
||||
Machine limits for floating point types.
|
||||
|
||||
Attributes
|
||||
----------
|
||||
bits : int
|
||||
The number of bits occupied by the type.
|
||||
dtype : dtype
|
||||
Returns the dtype for which `finfo` returns information. For complex
|
||||
input, the returned dtype is the associated ``float*`` dtype for its
|
||||
real and complex components.
|
||||
eps : float
|
||||
The difference between 1.0 and the next smallest representable float
|
||||
larger than 1.0. For example, for 64-bit binary floats in the IEEE-754
|
||||
standard, ``eps = 2**-52``, approximately 2.22e-16.
|
||||
epsneg : float
|
||||
The difference between 1.0 and the next smallest representable float
|
||||
less than 1.0. For example, for 64-bit binary floats in the IEEE-754
|
||||
standard, ``epsneg = 2**-53``, approximately 1.11e-16.
|
||||
iexp : int
|
||||
The number of bits in the exponent portion of the floating point
|
||||
representation.
|
||||
machep : int
|
||||
The exponent that yields `eps`.
|
||||
max : floating point number of the appropriate type
|
||||
The largest representable number.
|
||||
maxexp : int
|
||||
The smallest positive power of the base (2) that causes overflow.
|
||||
min : floating point number of the appropriate type
|
||||
The smallest representable number, typically ``-max``.
|
||||
minexp : int
|
||||
The most negative power of the base (2) consistent with there
|
||||
being no leading 0's in the mantissa.
|
||||
negep : int
|
||||
The exponent that yields `epsneg`.
|
||||
nexp : int
|
||||
The number of bits in the exponent including its sign and bias.
|
||||
nmant : int
|
||||
The number of bits in the mantissa.
|
||||
precision : int
|
||||
The approximate number of decimal digits to which this kind of
|
||||
float is precise.
|
||||
resolution : floating point number of the appropriate type
|
||||
The approximate decimal resolution of this type, i.e.,
|
||||
``10**-precision``.
|
||||
tiny : float
|
||||
An alias for `smallest_normal`, kept for backwards compatibility.
|
||||
smallest_normal : float
|
||||
The smallest positive floating point number with 1 as leading bit in
|
||||
the mantissa following IEEE-754 (see Notes).
|
||||
smallest_subnormal : float
|
||||
The smallest positive floating point number with 0 as leading bit in
|
||||
the mantissa following IEEE-754.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
dtype : float, dtype, or instance
|
||||
Kind of floating point or complex floating point
|
||||
data-type about which to get information.
|
||||
|
||||
See Also
|
||||
--------
|
||||
iinfo : The equivalent for integer data types.
|
||||
spacing : The distance between a value and the nearest adjacent number
|
||||
nextafter : The next floating point value after x1 towards x2
|
||||
|
||||
Notes
|
||||
-----
|
||||
For developers of NumPy: do not instantiate this at the module level.
|
||||
The initial calculation of these parameters is expensive and negatively
|
||||
impacts import times. These objects are cached, so calling ``finfo()``
|
||||
repeatedly inside your functions is not a problem.
|
||||
|
||||
Note that ``smallest_normal`` is not actually the smallest positive
|
||||
representable value in a NumPy floating point type. As in the IEEE-754
|
||||
standard [1]_, NumPy floating point types make use of subnormal numbers to
|
||||
fill the gap between 0 and ``smallest_normal``. However, subnormal numbers
|
||||
may have significantly reduced precision [2]_.
|
||||
|
||||
This function can also be used for complex data types as well. If used,
|
||||
the output will be the same as the corresponding real float type
|
||||
(e.g. numpy.finfo(numpy.csingle) is the same as numpy.finfo(numpy.single)).
|
||||
However, the output is true for the real and imaginary components.
|
||||
|
||||
References
|
||||
----------
|
||||
.. [1] IEEE Standard for Floating-Point Arithmetic, IEEE Std 754-2008,
|
||||
pp.1-70, 2008, https://doi.org/10.1109/IEEESTD.2008.4610935
|
||||
.. [2] Wikipedia, "Denormal Numbers",
|
||||
https://en.wikipedia.org/wiki/Denormal_number
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> np.finfo(np.float64).dtype
|
||||
dtype('float64')
|
||||
>>> np.finfo(np.complex64).dtype
|
||||
dtype('float32')
|
||||
|
||||
"""
|
||||
|
||||
_finfo_cache = {}
|
||||
|
||||
def __new__(cls, dtype):
|
||||
try:
|
||||
obj = cls._finfo_cache.get(dtype) # most common path
|
||||
if obj is not None:
|
||||
return obj
|
||||
except TypeError:
|
||||
pass
|
||||
|
||||
if dtype is None:
|
||||
# Deprecated in NumPy 1.25, 2023-01-16
|
||||
warnings.warn(
|
||||
"finfo() dtype cannot be None. This behavior will "
|
||||
"raise an error in the future. (Deprecated in NumPy 1.25)",
|
||||
DeprecationWarning,
|
||||
stacklevel=2
|
||||
)
|
||||
|
||||
try:
|
||||
dtype = numeric.dtype(dtype)
|
||||
except TypeError:
|
||||
# In case a float instance was given
|
||||
dtype = numeric.dtype(type(dtype))
|
||||
|
||||
obj = cls._finfo_cache.get(dtype)
|
||||
if obj is not None:
|
||||
return obj
|
||||
dtypes = [dtype]
|
||||
newdtype = ntypes.obj2sctype(dtype)
|
||||
if newdtype is not dtype:
|
||||
dtypes.append(newdtype)
|
||||
dtype = newdtype
|
||||
if not issubclass(dtype, numeric.inexact):
|
||||
raise ValueError("data type %r not inexact" % (dtype))
|
||||
obj = cls._finfo_cache.get(dtype)
|
||||
if obj is not None:
|
||||
return obj
|
||||
if not issubclass(dtype, numeric.floating):
|
||||
newdtype = _convert_to_float[dtype]
|
||||
if newdtype is not dtype:
|
||||
# dtype changed, for example from complex128 to float64
|
||||
dtypes.append(newdtype)
|
||||
dtype = newdtype
|
||||
|
||||
obj = cls._finfo_cache.get(dtype, None)
|
||||
if obj is not None:
|
||||
# the original dtype was not in the cache, but the new
|
||||
# dtype is in the cache. we add the original dtypes to
|
||||
# the cache and return the result
|
||||
for dt in dtypes:
|
||||
cls._finfo_cache[dt] = obj
|
||||
return obj
|
||||
obj = object.__new__(cls)._init(dtype)
|
||||
for dt in dtypes:
|
||||
cls._finfo_cache[dt] = obj
|
||||
return obj
|
||||
|
||||
def _init(self, dtype):
|
||||
self.dtype = numeric.dtype(dtype)
|
||||
machar = _get_machar(dtype)
|
||||
|
||||
for word in ['precision', 'iexp',
|
||||
'maxexp', 'minexp', 'negep',
|
||||
'machep']:
|
||||
setattr(self, word, getattr(machar, word))
|
||||
for word in ['resolution', 'epsneg', 'smallest_subnormal']:
|
||||
setattr(self, word, getattr(machar, word).flat[0])
|
||||
self.bits = self.dtype.itemsize * 8
|
||||
self.max = machar.huge.flat[0]
|
||||
self.min = -self.max
|
||||
self.eps = machar.eps.flat[0]
|
||||
self.nexp = machar.iexp
|
||||
self.nmant = machar.it
|
||||
self._machar = machar
|
||||
self._str_tiny = machar._str_xmin.strip()
|
||||
self._str_max = machar._str_xmax.strip()
|
||||
self._str_epsneg = machar._str_epsneg.strip()
|
||||
self._str_eps = machar._str_eps.strip()
|
||||
self._str_resolution = machar._str_resolution.strip()
|
||||
self._str_smallest_normal = machar._str_smallest_normal.strip()
|
||||
self._str_smallest_subnormal = machar._str_smallest_subnormal.strip()
|
||||
return self
|
||||
|
||||
def __str__(self):
|
||||
fmt = (
|
||||
'Machine parameters for %(dtype)s\n'
|
||||
'---------------------------------------------------------------\n'
|
||||
'precision = %(precision)3s resolution = %(_str_resolution)s\n'
|
||||
'machep = %(machep)6s eps = %(_str_eps)s\n'
|
||||
'negep = %(negep)6s epsneg = %(_str_epsneg)s\n'
|
||||
'minexp = %(minexp)6s tiny = %(_str_tiny)s\n'
|
||||
'maxexp = %(maxexp)6s max = %(_str_max)s\n'
|
||||
'nexp = %(nexp)6s min = -max\n'
|
||||
'smallest_normal = %(_str_smallest_normal)s '
|
||||
'smallest_subnormal = %(_str_smallest_subnormal)s\n'
|
||||
'---------------------------------------------------------------\n'
|
||||
)
|
||||
return fmt % self.__dict__
|
||||
|
||||
def __repr__(self):
|
||||
c = self.__class__.__name__
|
||||
d = self.__dict__.copy()
|
||||
d['klass'] = c
|
||||
return (("%(klass)s(resolution=%(resolution)s, min=-%(_str_max)s,"
|
||||
" max=%(_str_max)s, dtype=%(dtype)s)") % d)
|
||||
|
||||
@property
|
||||
def smallest_normal(self):
|
||||
"""Return the value for the smallest normal.
|
||||
|
||||
Returns
|
||||
-------
|
||||
smallest_normal : float
|
||||
Value for the smallest normal.
|
||||
|
||||
Warns
|
||||
-----
|
||||
UserWarning
|
||||
If the calculated value for the smallest normal is requested for
|
||||
double-double.
|
||||
"""
|
||||
# This check is necessary because the value for smallest_normal is
|
||||
# platform dependent for longdouble types.
|
||||
if isnan(self._machar.smallest_normal.flat[0]):
|
||||
warnings.warn(
|
||||
'The value of smallest normal is undefined for double double',
|
||||
UserWarning, stacklevel=2)
|
||||
return self._machar.smallest_normal.flat[0]
|
||||
|
||||
@property
|
||||
def tiny(self):
|
||||
"""Return the value for tiny, alias of smallest_normal.
|
||||
|
||||
Returns
|
||||
-------
|
||||
tiny : float
|
||||
Value for the smallest normal, alias of smallest_normal.
|
||||
|
||||
Warns
|
||||
-----
|
||||
UserWarning
|
||||
If the calculated value for the smallest normal is requested for
|
||||
double-double.
|
||||
"""
|
||||
return self.smallest_normal
|
||||
|
||||
|
||||
@set_module('numpy')
|
||||
class iinfo:
|
||||
"""
|
||||
iinfo(type)
|
||||
|
||||
Machine limits for integer types.
|
||||
|
||||
Attributes
|
||||
----------
|
||||
bits : int
|
||||
The number of bits occupied by the type.
|
||||
dtype : dtype
|
||||
Returns the dtype for which `iinfo` returns information.
|
||||
min : int
|
||||
The smallest integer expressible by the type.
|
||||
max : int
|
||||
The largest integer expressible by the type.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
int_type : integer type, dtype, or instance
|
||||
The kind of integer data type to get information about.
|
||||
|
||||
See Also
|
||||
--------
|
||||
finfo : The equivalent for floating point data types.
|
||||
|
||||
Examples
|
||||
--------
|
||||
With types:
|
||||
|
||||
>>> ii16 = np.iinfo(np.int16)
|
||||
>>> ii16.min
|
||||
-32768
|
||||
>>> ii16.max
|
||||
32767
|
||||
>>> ii32 = np.iinfo(np.int32)
|
||||
>>> ii32.min
|
||||
-2147483648
|
||||
>>> ii32.max
|
||||
2147483647
|
||||
|
||||
With instances:
|
||||
|
||||
>>> ii32 = np.iinfo(np.int32(10))
|
||||
>>> ii32.min
|
||||
-2147483648
|
||||
>>> ii32.max
|
||||
2147483647
|
||||
|
||||
"""
|
||||
|
||||
_min_vals = {}
|
||||
_max_vals = {}
|
||||
|
||||
def __init__(self, int_type):
|
||||
try:
|
||||
self.dtype = numeric.dtype(int_type)
|
||||
except TypeError:
|
||||
self.dtype = numeric.dtype(type(int_type))
|
||||
self.kind = self.dtype.kind
|
||||
self.bits = self.dtype.itemsize * 8
|
||||
self.key = "%s%d" % (self.kind, self.bits)
|
||||
if self.kind not in 'iu':
|
||||
raise ValueError("Invalid integer data type %r." % (self.kind,))
|
||||
|
||||
@property
|
||||
def min(self):
|
||||
"""Minimum value of given dtype."""
|
||||
if self.kind == 'u':
|
||||
return 0
|
||||
else:
|
||||
try:
|
||||
val = iinfo._min_vals[self.key]
|
||||
except KeyError:
|
||||
val = int(-(1 << (self.bits-1)))
|
||||
iinfo._min_vals[self.key] = val
|
||||
return val
|
||||
|
||||
@property
|
||||
def max(self):
|
||||
"""Maximum value of given dtype."""
|
||||
try:
|
||||
val = iinfo._max_vals[self.key]
|
||||
except KeyError:
|
||||
if self.kind == 'u':
|
||||
val = int((1 << self.bits) - 1)
|
||||
else:
|
||||
val = int((1 << (self.bits-1)) - 1)
|
||||
iinfo._max_vals[self.key] = val
|
||||
return val
|
||||
|
||||
def __str__(self):
|
||||
"""String representation."""
|
||||
fmt = (
|
||||
'Machine parameters for %(dtype)s\n'
|
||||
'---------------------------------------------------------------\n'
|
||||
'min = %(min)s\n'
|
||||
'max = %(max)s\n'
|
||||
'---------------------------------------------------------------\n'
|
||||
)
|
||||
return fmt % {'dtype': self.dtype, 'min': self.min, 'max': self.max}
|
||||
|
||||
def __repr__(self):
|
||||
return "%s(min=%s, max=%s, dtype=%s)" % (self.__class__.__name__,
|
||||
self.min, self.max, self.dtype)
|
||||
@ -0,0 +1,6 @@
|
||||
from numpy import (
|
||||
finfo as finfo,
|
||||
iinfo as iinfo,
|
||||
)
|
||||
|
||||
__all__: list[str]
|
||||
@ -0,0 +1,376 @@
|
||||
|
||||
/* These pointers will be stored in the C-object for use in other
|
||||
extension modules
|
||||
*/
|
||||
|
||||
void *PyArray_API[] = {
|
||||
(void *) PyArray_GetNDArrayCVersion,
|
||||
NULL,
|
||||
(void *) &PyArray_Type,
|
||||
(void *) &PyArrayDescr_Type,
|
||||
NULL,
|
||||
(void *) &PyArrayIter_Type,
|
||||
(void *) &PyArrayMultiIter_Type,
|
||||
(int *) &NPY_NUMUSERTYPES,
|
||||
(void *) &PyBoolArrType_Type,
|
||||
(void *) &_PyArrayScalar_BoolValues,
|
||||
(void *) &PyGenericArrType_Type,
|
||||
(void *) &PyNumberArrType_Type,
|
||||
(void *) &PyIntegerArrType_Type,
|
||||
(void *) &PySignedIntegerArrType_Type,
|
||||
(void *) &PyUnsignedIntegerArrType_Type,
|
||||
(void *) &PyInexactArrType_Type,
|
||||
(void *) &PyFloatingArrType_Type,
|
||||
(void *) &PyComplexFloatingArrType_Type,
|
||||
(void *) &PyFlexibleArrType_Type,
|
||||
(void *) &PyCharacterArrType_Type,
|
||||
(void *) &PyByteArrType_Type,
|
||||
(void *) &PyShortArrType_Type,
|
||||
(void *) &PyIntArrType_Type,
|
||||
(void *) &PyLongArrType_Type,
|
||||
(void *) &PyLongLongArrType_Type,
|
||||
(void *) &PyUByteArrType_Type,
|
||||
(void *) &PyUShortArrType_Type,
|
||||
(void *) &PyUIntArrType_Type,
|
||||
(void *) &PyULongArrType_Type,
|
||||
(void *) &PyULongLongArrType_Type,
|
||||
(void *) &PyFloatArrType_Type,
|
||||
(void *) &PyDoubleArrType_Type,
|
||||
(void *) &PyLongDoubleArrType_Type,
|
||||
(void *) &PyCFloatArrType_Type,
|
||||
(void *) &PyCDoubleArrType_Type,
|
||||
(void *) &PyCLongDoubleArrType_Type,
|
||||
(void *) &PyObjectArrType_Type,
|
||||
(void *) &PyStringArrType_Type,
|
||||
(void *) &PyUnicodeArrType_Type,
|
||||
(void *) &PyVoidArrType_Type,
|
||||
NULL,
|
||||
NULL,
|
||||
(void *) PyArray_INCREF,
|
||||
(void *) PyArray_XDECREF,
|
||||
(void *) PyArray_SetStringFunction,
|
||||
(void *) PyArray_DescrFromType,
|
||||
(void *) PyArray_TypeObjectFromType,
|
||||
(void *) PyArray_Zero,
|
||||
(void *) PyArray_One,
|
||||
(void *) PyArray_CastToType,
|
||||
(void *) PyArray_CopyInto,
|
||||
(void *) PyArray_CopyAnyInto,
|
||||
(void *) PyArray_CanCastSafely,
|
||||
(void *) PyArray_CanCastTo,
|
||||
(void *) PyArray_ObjectType,
|
||||
(void *) PyArray_DescrFromObject,
|
||||
(void *) PyArray_ConvertToCommonType,
|
||||
(void *) PyArray_DescrFromScalar,
|
||||
(void *) PyArray_DescrFromTypeObject,
|
||||
(void *) PyArray_Size,
|
||||
(void *) PyArray_Scalar,
|
||||
(void *) PyArray_FromScalar,
|
||||
(void *) PyArray_ScalarAsCtype,
|
||||
(void *) PyArray_CastScalarToCtype,
|
||||
(void *) PyArray_CastScalarDirect,
|
||||
(void *) PyArray_Pack,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
(void *) PyArray_FromAny,
|
||||
(void *) PyArray_EnsureArray,
|
||||
(void *) PyArray_EnsureAnyArray,
|
||||
(void *) PyArray_FromFile,
|
||||
(void *) PyArray_FromString,
|
||||
(void *) PyArray_FromBuffer,
|
||||
(void *) PyArray_FromIter,
|
||||
(void *) PyArray_Return,
|
||||
(void *) PyArray_GetField,
|
||||
(void *) PyArray_SetField,
|
||||
(void *) PyArray_Byteswap,
|
||||
(void *) PyArray_Resize,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
(void *) PyArray_CopyObject,
|
||||
(void *) PyArray_NewCopy,
|
||||
(void *) PyArray_ToList,
|
||||
(void *) PyArray_ToString,
|
||||
(void *) PyArray_ToFile,
|
||||
(void *) PyArray_Dump,
|
||||
(void *) PyArray_Dumps,
|
||||
(void *) PyArray_ValidType,
|
||||
(void *) PyArray_UpdateFlags,
|
||||
(void *) PyArray_New,
|
||||
(void *) PyArray_NewFromDescr,
|
||||
(void *) PyArray_DescrNew,
|
||||
(void *) PyArray_DescrNewFromType,
|
||||
(void *) PyArray_GetPriority,
|
||||
(void *) PyArray_IterNew,
|
||||
(void *) PyArray_MultiIterNew,
|
||||
(void *) PyArray_PyIntAsInt,
|
||||
(void *) PyArray_PyIntAsIntp,
|
||||
(void *) PyArray_Broadcast,
|
||||
NULL,
|
||||
(void *) PyArray_FillWithScalar,
|
||||
(void *) PyArray_CheckStrides,
|
||||
(void *) PyArray_DescrNewByteorder,
|
||||
(void *) PyArray_IterAllButAxis,
|
||||
(void *) PyArray_CheckFromAny,
|
||||
(void *) PyArray_FromArray,
|
||||
(void *) PyArray_FromInterface,
|
||||
(void *) PyArray_FromStructInterface,
|
||||
(void *) PyArray_FromArrayAttr,
|
||||
(void *) PyArray_ScalarKind,
|
||||
(void *) PyArray_CanCoerceScalar,
|
||||
NULL,
|
||||
(void *) PyArray_CanCastScalar,
|
||||
NULL,
|
||||
(void *) PyArray_RemoveSmallest,
|
||||
(void *) PyArray_ElementStrides,
|
||||
(void *) PyArray_Item_INCREF,
|
||||
(void *) PyArray_Item_XDECREF,
|
||||
NULL,
|
||||
(void *) PyArray_Transpose,
|
||||
(void *) PyArray_TakeFrom,
|
||||
(void *) PyArray_PutTo,
|
||||
(void *) PyArray_PutMask,
|
||||
(void *) PyArray_Repeat,
|
||||
(void *) PyArray_Choose,
|
||||
(void *) PyArray_Sort,
|
||||
(void *) PyArray_ArgSort,
|
||||
(void *) PyArray_SearchSorted,
|
||||
(void *) PyArray_ArgMax,
|
||||
(void *) PyArray_ArgMin,
|
||||
(void *) PyArray_Reshape,
|
||||
(void *) PyArray_Newshape,
|
||||
(void *) PyArray_Squeeze,
|
||||
(void *) PyArray_View,
|
||||
(void *) PyArray_SwapAxes,
|
||||
(void *) PyArray_Max,
|
||||
(void *) PyArray_Min,
|
||||
(void *) PyArray_Ptp,
|
||||
(void *) PyArray_Mean,
|
||||
(void *) PyArray_Trace,
|
||||
(void *) PyArray_Diagonal,
|
||||
(void *) PyArray_Clip,
|
||||
(void *) PyArray_Conjugate,
|
||||
(void *) PyArray_Nonzero,
|
||||
(void *) PyArray_Std,
|
||||
(void *) PyArray_Sum,
|
||||
(void *) PyArray_CumSum,
|
||||
(void *) PyArray_Prod,
|
||||
(void *) PyArray_CumProd,
|
||||
(void *) PyArray_All,
|
||||
(void *) PyArray_Any,
|
||||
(void *) PyArray_Compress,
|
||||
(void *) PyArray_Flatten,
|
||||
(void *) PyArray_Ravel,
|
||||
(void *) PyArray_MultiplyList,
|
||||
(void *) PyArray_MultiplyIntList,
|
||||
(void *) PyArray_GetPtr,
|
||||
(void *) PyArray_CompareLists,
|
||||
(void *) PyArray_AsCArray,
|
||||
NULL,
|
||||
NULL,
|
||||
(void *) PyArray_Free,
|
||||
(void *) PyArray_Converter,
|
||||
(void *) PyArray_IntpFromSequence,
|
||||
(void *) PyArray_Concatenate,
|
||||
(void *) PyArray_InnerProduct,
|
||||
(void *) PyArray_MatrixProduct,
|
||||
NULL,
|
||||
(void *) PyArray_Correlate,
|
||||
NULL,
|
||||
(void *) PyArray_DescrConverter,
|
||||
(void *) PyArray_DescrConverter2,
|
||||
(void *) PyArray_IntpConverter,
|
||||
(void *) PyArray_BufferConverter,
|
||||
(void *) PyArray_AxisConverter,
|
||||
(void *) PyArray_BoolConverter,
|
||||
(void *) PyArray_ByteorderConverter,
|
||||
(void *) PyArray_OrderConverter,
|
||||
(void *) PyArray_EquivTypes,
|
||||
(void *) PyArray_Zeros,
|
||||
(void *) PyArray_Empty,
|
||||
(void *) PyArray_Where,
|
||||
(void *) PyArray_Arange,
|
||||
(void *) PyArray_ArangeObj,
|
||||
(void *) PyArray_SortkindConverter,
|
||||
(void *) PyArray_LexSort,
|
||||
(void *) PyArray_Round,
|
||||
(void *) PyArray_EquivTypenums,
|
||||
(void *) PyArray_RegisterDataType,
|
||||
(void *) PyArray_RegisterCastFunc,
|
||||
(void *) PyArray_RegisterCanCast,
|
||||
(void *) PyArray_InitArrFuncs,
|
||||
(void *) PyArray_IntTupleFromIntp,
|
||||
NULL,
|
||||
(void *) PyArray_ClipmodeConverter,
|
||||
(void *) PyArray_OutputConverter,
|
||||
(void *) PyArray_BroadcastToShape,
|
||||
NULL,
|
||||
NULL,
|
||||
(void *) PyArray_DescrAlignConverter,
|
||||
(void *) PyArray_DescrAlignConverter2,
|
||||
(void *) PyArray_SearchsideConverter,
|
||||
(void *) PyArray_CheckAxis,
|
||||
(void *) PyArray_OverflowMultiplyList,
|
||||
NULL,
|
||||
(void *) PyArray_MultiIterFromObjects,
|
||||
(void *) PyArray_GetEndianness,
|
||||
(void *) PyArray_GetNDArrayCFeatureVersion,
|
||||
(void *) PyArray_Correlate2,
|
||||
(void *) PyArray_NeighborhoodIterNew,
|
||||
(void *) &PyTimeIntegerArrType_Type,
|
||||
(void *) &PyDatetimeArrType_Type,
|
||||
(void *) &PyTimedeltaArrType_Type,
|
||||
(void *) &PyHalfArrType_Type,
|
||||
(void *) &NpyIter_Type,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
(void *) NpyIter_New,
|
||||
(void *) NpyIter_MultiNew,
|
||||
(void *) NpyIter_AdvancedNew,
|
||||
(void *) NpyIter_Copy,
|
||||
(void *) NpyIter_Deallocate,
|
||||
(void *) NpyIter_HasDelayedBufAlloc,
|
||||
(void *) NpyIter_HasExternalLoop,
|
||||
(void *) NpyIter_EnableExternalLoop,
|
||||
(void *) NpyIter_GetInnerStrideArray,
|
||||
(void *) NpyIter_GetInnerLoopSizePtr,
|
||||
(void *) NpyIter_Reset,
|
||||
(void *) NpyIter_ResetBasePointers,
|
||||
(void *) NpyIter_ResetToIterIndexRange,
|
||||
(void *) NpyIter_GetNDim,
|
||||
(void *) NpyIter_GetNOp,
|
||||
(void *) NpyIter_GetIterNext,
|
||||
(void *) NpyIter_GetIterSize,
|
||||
(void *) NpyIter_GetIterIndexRange,
|
||||
(void *) NpyIter_GetIterIndex,
|
||||
(void *) NpyIter_GotoIterIndex,
|
||||
(void *) NpyIter_HasMultiIndex,
|
||||
(void *) NpyIter_GetShape,
|
||||
(void *) NpyIter_GetGetMultiIndex,
|
||||
(void *) NpyIter_GotoMultiIndex,
|
||||
(void *) NpyIter_RemoveMultiIndex,
|
||||
(void *) NpyIter_HasIndex,
|
||||
(void *) NpyIter_IsBuffered,
|
||||
(void *) NpyIter_IsGrowInner,
|
||||
(void *) NpyIter_GetBufferSize,
|
||||
(void *) NpyIter_GetIndexPtr,
|
||||
(void *) NpyIter_GotoIndex,
|
||||
(void *) NpyIter_GetDataPtrArray,
|
||||
(void *) NpyIter_GetDescrArray,
|
||||
(void *) NpyIter_GetOperandArray,
|
||||
(void *) NpyIter_GetIterView,
|
||||
(void *) NpyIter_GetReadFlags,
|
||||
(void *) NpyIter_GetWriteFlags,
|
||||
(void *) NpyIter_DebugPrint,
|
||||
(void *) NpyIter_IterationNeedsAPI,
|
||||
(void *) NpyIter_GetInnerFixedStrideArray,
|
||||
(void *) NpyIter_RemoveAxis,
|
||||
(void *) NpyIter_GetAxisStrideArray,
|
||||
(void *) NpyIter_RequiresBuffering,
|
||||
(void *) NpyIter_GetInitialDataPtrArray,
|
||||
(void *) NpyIter_CreateCompatibleStrides,
|
||||
(void *) PyArray_CastingConverter,
|
||||
(void *) PyArray_CountNonzero,
|
||||
(void *) PyArray_PromoteTypes,
|
||||
(void *) PyArray_MinScalarType,
|
||||
(void *) PyArray_ResultType,
|
||||
(void *) PyArray_CanCastArrayTo,
|
||||
(void *) PyArray_CanCastTypeTo,
|
||||
(void *) PyArray_EinsteinSum,
|
||||
(void *) PyArray_NewLikeArray,
|
||||
NULL,
|
||||
(void *) PyArray_ConvertClipmodeSequence,
|
||||
(void *) PyArray_MatrixProduct2,
|
||||
(void *) NpyIter_IsFirstVisit,
|
||||
(void *) PyArray_SetBaseObject,
|
||||
(void *) PyArray_CreateSortedStridePerm,
|
||||
(void *) PyArray_RemoveAxesInPlace,
|
||||
(void *) PyArray_DebugPrint,
|
||||
(void *) PyArray_FailUnlessWriteable,
|
||||
(void *) PyArray_SetUpdateIfCopyBase,
|
||||
(void *) PyDataMem_NEW,
|
||||
(void *) PyDataMem_FREE,
|
||||
(void *) PyDataMem_RENEW,
|
||||
NULL,
|
||||
(NPY_CASTING *) &NPY_DEFAULT_ASSIGN_CASTING,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
(void *) PyArray_Partition,
|
||||
(void *) PyArray_ArgPartition,
|
||||
(void *) PyArray_SelectkindConverter,
|
||||
(void *) PyDataMem_NEW_ZEROED,
|
||||
(void *) PyArray_CheckAnyScalarExact,
|
||||
NULL,
|
||||
(void *) PyArray_ResolveWritebackIfCopy,
|
||||
(void *) PyArray_SetWritebackIfCopyBase,
|
||||
(void *) PyDataMem_SetHandler,
|
||||
(void *) PyDataMem_GetHandler,
|
||||
(PyObject* *) &PyDataMem_DefaultHandler,
|
||||
(void *) NpyDatetime_ConvertDatetime64ToDatetimeStruct,
|
||||
(void *) NpyDatetime_ConvertDatetimeStructToDatetime64,
|
||||
(void *) NpyDatetime_ConvertPyDateTimeToDatetimeStruct,
|
||||
(void *) NpyDatetime_GetDatetimeISO8601StrLen,
|
||||
(void *) NpyDatetime_MakeISO8601Datetime,
|
||||
(void *) NpyDatetime_ParseISO8601Datetime,
|
||||
(void *) NpyString_load,
|
||||
(void *) NpyString_pack,
|
||||
(void *) NpyString_pack_null,
|
||||
(void *) NpyString_acquire_allocator,
|
||||
(void *) NpyString_acquire_allocators,
|
||||
(void *) NpyString_release_allocator,
|
||||
(void *) NpyString_release_allocators,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
(void *) PyArray_GetDefaultDescr,
|
||||
(void *) PyArrayInitDTypeMeta_FromSpec,
|
||||
(void *) PyArray_CommonDType,
|
||||
(void *) PyArray_PromoteDTypeSequence,
|
||||
(void *) _PyDataType_GetArrFuncs,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,54 @@
|
||||
|
||||
/* These pointers will be stored in the C-object for use in other
|
||||
extension modules
|
||||
*/
|
||||
|
||||
void *PyUFunc_API[] = {
|
||||
(void *) &PyUFunc_Type,
|
||||
(void *) PyUFunc_FromFuncAndData,
|
||||
(void *) PyUFunc_RegisterLoopForType,
|
||||
NULL,
|
||||
(void *) PyUFunc_f_f_As_d_d,
|
||||
(void *) PyUFunc_d_d,
|
||||
(void *) PyUFunc_f_f,
|
||||
(void *) PyUFunc_g_g,
|
||||
(void *) PyUFunc_F_F_As_D_D,
|
||||
(void *) PyUFunc_F_F,
|
||||
(void *) PyUFunc_D_D,
|
||||
(void *) PyUFunc_G_G,
|
||||
(void *) PyUFunc_O_O,
|
||||
(void *) PyUFunc_ff_f_As_dd_d,
|
||||
(void *) PyUFunc_ff_f,
|
||||
(void *) PyUFunc_dd_d,
|
||||
(void *) PyUFunc_gg_g,
|
||||
(void *) PyUFunc_FF_F_As_DD_D,
|
||||
(void *) PyUFunc_DD_D,
|
||||
(void *) PyUFunc_FF_F,
|
||||
(void *) PyUFunc_GG_G,
|
||||
(void *) PyUFunc_OO_O,
|
||||
(void *) PyUFunc_O_O_method,
|
||||
(void *) PyUFunc_OO_O_method,
|
||||
(void *) PyUFunc_On_Om,
|
||||
NULL,
|
||||
NULL,
|
||||
(void *) PyUFunc_clearfperr,
|
||||
(void *) PyUFunc_getfperr,
|
||||
NULL,
|
||||
(void *) PyUFunc_ReplaceLoopBySignature,
|
||||
(void *) PyUFunc_FromFuncAndDataAndSignature,
|
||||
NULL,
|
||||
(void *) PyUFunc_e_e,
|
||||
(void *) PyUFunc_e_e_As_f_f,
|
||||
(void *) PyUFunc_e_e_As_d_d,
|
||||
(void *) PyUFunc_ee_e,
|
||||
(void *) PyUFunc_ee_e_As_ff_f,
|
||||
(void *) PyUFunc_ee_e_As_dd_d,
|
||||
(void *) PyUFunc_DefaultTypeResolver,
|
||||
(void *) PyUFunc_ValidateCasting,
|
||||
(void *) PyUFunc_RegisterLoopForDescr,
|
||||
(void *) PyUFunc_FromFuncAndDataAndSignatureAndIdentity,
|
||||
(void *) PyUFunc_AddLoopFromSpec,
|
||||
(void *) PyUFunc_AddPromoter,
|
||||
(void *) PyUFunc_AddWrappingLoop,
|
||||
(void *) PyUFunc_GiveFloatingpointErrors
|
||||
};
|
||||
@ -0,0 +1,339 @@
|
||||
|
||||
#ifdef _UMATHMODULE
|
||||
|
||||
extern NPY_NO_EXPORT PyTypeObject PyUFunc_Type;
|
||||
|
||||
extern NPY_NO_EXPORT PyTypeObject PyUFunc_Type;
|
||||
|
||||
NPY_NO_EXPORT PyObject * PyUFunc_FromFuncAndData \
|
||||
(PyUFuncGenericFunction *, void *const *, const char *, int, int, int, int, const char *, const char *, int);
|
||||
NPY_NO_EXPORT int PyUFunc_RegisterLoopForType \
|
||||
(PyUFuncObject *, int, PyUFuncGenericFunction, const int *, void *);
|
||||
NPY_NO_EXPORT void PyUFunc_f_f_As_d_d \
|
||||
(char **, npy_intp const *, npy_intp const *, void *);
|
||||
NPY_NO_EXPORT void PyUFunc_d_d \
|
||||
(char **, npy_intp const *, npy_intp const *, void *);
|
||||
NPY_NO_EXPORT void PyUFunc_f_f \
|
||||
(char **, npy_intp const *, npy_intp const *, void *);
|
||||
NPY_NO_EXPORT void PyUFunc_g_g \
|
||||
(char **, npy_intp const *, npy_intp const *, void *);
|
||||
NPY_NO_EXPORT void PyUFunc_F_F_As_D_D \
|
||||
(char **, npy_intp const *, npy_intp const *, void *);
|
||||
NPY_NO_EXPORT void PyUFunc_F_F \
|
||||
(char **, npy_intp const *, npy_intp const *, void *);
|
||||
NPY_NO_EXPORT void PyUFunc_D_D \
|
||||
(char **, npy_intp const *, npy_intp const *, void *);
|
||||
NPY_NO_EXPORT void PyUFunc_G_G \
|
||||
(char **, npy_intp const *, npy_intp const *, void *);
|
||||
NPY_NO_EXPORT void PyUFunc_O_O \
|
||||
(char **, npy_intp const *, npy_intp const *, void *);
|
||||
NPY_NO_EXPORT void PyUFunc_ff_f_As_dd_d \
|
||||
(char **, npy_intp const *, npy_intp const *, void *);
|
||||
NPY_NO_EXPORT void PyUFunc_ff_f \
|
||||
(char **, npy_intp const *, npy_intp const *, void *);
|
||||
NPY_NO_EXPORT void PyUFunc_dd_d \
|
||||
(char **, npy_intp const *, npy_intp const *, void *);
|
||||
NPY_NO_EXPORT void PyUFunc_gg_g \
|
||||
(char **, npy_intp const *, npy_intp const *, void *);
|
||||
NPY_NO_EXPORT void PyUFunc_FF_F_As_DD_D \
|
||||
(char **, npy_intp const *, npy_intp const *, void *);
|
||||
NPY_NO_EXPORT void PyUFunc_DD_D \
|
||||
(char **, npy_intp const *, npy_intp const *, void *);
|
||||
NPY_NO_EXPORT void PyUFunc_FF_F \
|
||||
(char **, npy_intp const *, npy_intp const *, void *);
|
||||
NPY_NO_EXPORT void PyUFunc_GG_G \
|
||||
(char **, npy_intp const *, npy_intp const *, void *);
|
||||
NPY_NO_EXPORT void PyUFunc_OO_O \
|
||||
(char **, npy_intp const *, npy_intp const *, void *);
|
||||
NPY_NO_EXPORT void PyUFunc_O_O_method \
|
||||
(char **, npy_intp const *, npy_intp const *, void *);
|
||||
NPY_NO_EXPORT void PyUFunc_OO_O_method \
|
||||
(char **, npy_intp const *, npy_intp const *, void *);
|
||||
NPY_NO_EXPORT void PyUFunc_On_Om \
|
||||
(char **, npy_intp const *, npy_intp const *, void *);
|
||||
NPY_NO_EXPORT void PyUFunc_clearfperr \
|
||||
(void);
|
||||
NPY_NO_EXPORT int PyUFunc_getfperr \
|
||||
(void);
|
||||
NPY_NO_EXPORT int PyUFunc_ReplaceLoopBySignature \
|
||||
(PyUFuncObject *, PyUFuncGenericFunction, const int *, PyUFuncGenericFunction *);
|
||||
NPY_NO_EXPORT PyObject * PyUFunc_FromFuncAndDataAndSignature \
|
||||
(PyUFuncGenericFunction *, void *const *, const char *, int, int, int, int, const char *, const char *, int, const char *);
|
||||
NPY_NO_EXPORT void PyUFunc_e_e \
|
||||
(char **, npy_intp const *, npy_intp const *, void *);
|
||||
NPY_NO_EXPORT void PyUFunc_e_e_As_f_f \
|
||||
(char **, npy_intp const *, npy_intp const *, void *);
|
||||
NPY_NO_EXPORT void PyUFunc_e_e_As_d_d \
|
||||
(char **, npy_intp const *, npy_intp const *, void *);
|
||||
NPY_NO_EXPORT void PyUFunc_ee_e \
|
||||
(char **, npy_intp const *, npy_intp const *, void *);
|
||||
NPY_NO_EXPORT void PyUFunc_ee_e_As_ff_f \
|
||||
(char **, npy_intp const *, npy_intp const *, void *);
|
||||
NPY_NO_EXPORT void PyUFunc_ee_e_As_dd_d \
|
||||
(char **, npy_intp const *, npy_intp const *, void *);
|
||||
NPY_NO_EXPORT int PyUFunc_DefaultTypeResolver \
|
||||
(PyUFuncObject *, NPY_CASTING, PyArrayObject **, PyObject *, PyArray_Descr **);
|
||||
NPY_NO_EXPORT int PyUFunc_ValidateCasting \
|
||||
(PyUFuncObject *, NPY_CASTING, PyArrayObject **, PyArray_Descr *const *);
|
||||
NPY_NO_EXPORT int PyUFunc_RegisterLoopForDescr \
|
||||
(PyUFuncObject *, PyArray_Descr *, PyUFuncGenericFunction, PyArray_Descr **, void *);
|
||||
NPY_NO_EXPORT PyObject * PyUFunc_FromFuncAndDataAndSignatureAndIdentity \
|
||||
(PyUFuncGenericFunction *, void *const *, const char *, int, int, int, int, const char *, const char *, const int, const char *, PyObject *);
|
||||
NPY_NO_EXPORT int PyUFunc_AddLoopFromSpec \
|
||||
(PyObject *, PyArrayMethod_Spec *);
|
||||
NPY_NO_EXPORT int PyUFunc_AddPromoter \
|
||||
(PyObject *, PyObject *, PyObject *);
|
||||
NPY_NO_EXPORT int PyUFunc_AddWrappingLoop \
|
||||
(PyObject *, PyArray_DTypeMeta *new_dtypes[], PyArray_DTypeMeta *wrapped_dtypes[], PyArrayMethod_TranslateGivenDescriptors *, PyArrayMethod_TranslateLoopDescriptors *);
|
||||
NPY_NO_EXPORT int PyUFunc_GiveFloatingpointErrors \
|
||||
(const char *, int);
|
||||
|
||||
#else
|
||||
|
||||
#if defined(PY_UFUNC_UNIQUE_SYMBOL)
|
||||
#define PyUFunc_API PY_UFUNC_UNIQUE_SYMBOL
|
||||
#endif
|
||||
|
||||
#if defined(NO_IMPORT) || defined(NO_IMPORT_UFUNC)
|
||||
extern void **PyUFunc_API;
|
||||
#else
|
||||
#if defined(PY_UFUNC_UNIQUE_SYMBOL)
|
||||
void **PyUFunc_API;
|
||||
#else
|
||||
static void **PyUFunc_API=NULL;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define PyUFunc_Type (*(PyTypeObject *)PyUFunc_API[0])
|
||||
#define PyUFunc_FromFuncAndData \
|
||||
(*(PyObject * (*)(PyUFuncGenericFunction *, void *const *, const char *, int, int, int, int, const char *, const char *, int)) \
|
||||
PyUFunc_API[1])
|
||||
#define PyUFunc_RegisterLoopForType \
|
||||
(*(int (*)(PyUFuncObject *, int, PyUFuncGenericFunction, const int *, void *)) \
|
||||
PyUFunc_API[2])
|
||||
#define PyUFunc_f_f_As_d_d \
|
||||
(*(void (*)(char **, npy_intp const *, npy_intp const *, void *)) \
|
||||
PyUFunc_API[4])
|
||||
#define PyUFunc_d_d \
|
||||
(*(void (*)(char **, npy_intp const *, npy_intp const *, void *)) \
|
||||
PyUFunc_API[5])
|
||||
#define PyUFunc_f_f \
|
||||
(*(void (*)(char **, npy_intp const *, npy_intp const *, void *)) \
|
||||
PyUFunc_API[6])
|
||||
#define PyUFunc_g_g \
|
||||
(*(void (*)(char **, npy_intp const *, npy_intp const *, void *)) \
|
||||
PyUFunc_API[7])
|
||||
#define PyUFunc_F_F_As_D_D \
|
||||
(*(void (*)(char **, npy_intp const *, npy_intp const *, void *)) \
|
||||
PyUFunc_API[8])
|
||||
#define PyUFunc_F_F \
|
||||
(*(void (*)(char **, npy_intp const *, npy_intp const *, void *)) \
|
||||
PyUFunc_API[9])
|
||||
#define PyUFunc_D_D \
|
||||
(*(void (*)(char **, npy_intp const *, npy_intp const *, void *)) \
|
||||
PyUFunc_API[10])
|
||||
#define PyUFunc_G_G \
|
||||
(*(void (*)(char **, npy_intp const *, npy_intp const *, void *)) \
|
||||
PyUFunc_API[11])
|
||||
#define PyUFunc_O_O \
|
||||
(*(void (*)(char **, npy_intp const *, npy_intp const *, void *)) \
|
||||
PyUFunc_API[12])
|
||||
#define PyUFunc_ff_f_As_dd_d \
|
||||
(*(void (*)(char **, npy_intp const *, npy_intp const *, void *)) \
|
||||
PyUFunc_API[13])
|
||||
#define PyUFunc_ff_f \
|
||||
(*(void (*)(char **, npy_intp const *, npy_intp const *, void *)) \
|
||||
PyUFunc_API[14])
|
||||
#define PyUFunc_dd_d \
|
||||
(*(void (*)(char **, npy_intp const *, npy_intp const *, void *)) \
|
||||
PyUFunc_API[15])
|
||||
#define PyUFunc_gg_g \
|
||||
(*(void (*)(char **, npy_intp const *, npy_intp const *, void *)) \
|
||||
PyUFunc_API[16])
|
||||
#define PyUFunc_FF_F_As_DD_D \
|
||||
(*(void (*)(char **, npy_intp const *, npy_intp const *, void *)) \
|
||||
PyUFunc_API[17])
|
||||
#define PyUFunc_DD_D \
|
||||
(*(void (*)(char **, npy_intp const *, npy_intp const *, void *)) \
|
||||
PyUFunc_API[18])
|
||||
#define PyUFunc_FF_F \
|
||||
(*(void (*)(char **, npy_intp const *, npy_intp const *, void *)) \
|
||||
PyUFunc_API[19])
|
||||
#define PyUFunc_GG_G \
|
||||
(*(void (*)(char **, npy_intp const *, npy_intp const *, void *)) \
|
||||
PyUFunc_API[20])
|
||||
#define PyUFunc_OO_O \
|
||||
(*(void (*)(char **, npy_intp const *, npy_intp const *, void *)) \
|
||||
PyUFunc_API[21])
|
||||
#define PyUFunc_O_O_method \
|
||||
(*(void (*)(char **, npy_intp const *, npy_intp const *, void *)) \
|
||||
PyUFunc_API[22])
|
||||
#define PyUFunc_OO_O_method \
|
||||
(*(void (*)(char **, npy_intp const *, npy_intp const *, void *)) \
|
||||
PyUFunc_API[23])
|
||||
#define PyUFunc_On_Om \
|
||||
(*(void (*)(char **, npy_intp const *, npy_intp const *, void *)) \
|
||||
PyUFunc_API[24])
|
||||
#define PyUFunc_clearfperr \
|
||||
(*(void (*)(void)) \
|
||||
PyUFunc_API[27])
|
||||
#define PyUFunc_getfperr \
|
||||
(*(int (*)(void)) \
|
||||
PyUFunc_API[28])
|
||||
#define PyUFunc_ReplaceLoopBySignature \
|
||||
(*(int (*)(PyUFuncObject *, PyUFuncGenericFunction, const int *, PyUFuncGenericFunction *)) \
|
||||
PyUFunc_API[30])
|
||||
#define PyUFunc_FromFuncAndDataAndSignature \
|
||||
(*(PyObject * (*)(PyUFuncGenericFunction *, void *const *, const char *, int, int, int, int, const char *, const char *, int, const char *)) \
|
||||
PyUFunc_API[31])
|
||||
#define PyUFunc_e_e \
|
||||
(*(void (*)(char **, npy_intp const *, npy_intp const *, void *)) \
|
||||
PyUFunc_API[33])
|
||||
#define PyUFunc_e_e_As_f_f \
|
||||
(*(void (*)(char **, npy_intp const *, npy_intp const *, void *)) \
|
||||
PyUFunc_API[34])
|
||||
#define PyUFunc_e_e_As_d_d \
|
||||
(*(void (*)(char **, npy_intp const *, npy_intp const *, void *)) \
|
||||
PyUFunc_API[35])
|
||||
#define PyUFunc_ee_e \
|
||||
(*(void (*)(char **, npy_intp const *, npy_intp const *, void *)) \
|
||||
PyUFunc_API[36])
|
||||
#define PyUFunc_ee_e_As_ff_f \
|
||||
(*(void (*)(char **, npy_intp const *, npy_intp const *, void *)) \
|
||||
PyUFunc_API[37])
|
||||
#define PyUFunc_ee_e_As_dd_d \
|
||||
(*(void (*)(char **, npy_intp const *, npy_intp const *, void *)) \
|
||||
PyUFunc_API[38])
|
||||
#define PyUFunc_DefaultTypeResolver \
|
||||
(*(int (*)(PyUFuncObject *, NPY_CASTING, PyArrayObject **, PyObject *, PyArray_Descr **)) \
|
||||
PyUFunc_API[39])
|
||||
#define PyUFunc_ValidateCasting \
|
||||
(*(int (*)(PyUFuncObject *, NPY_CASTING, PyArrayObject **, PyArray_Descr *const *)) \
|
||||
PyUFunc_API[40])
|
||||
#define PyUFunc_RegisterLoopForDescr \
|
||||
(*(int (*)(PyUFuncObject *, PyArray_Descr *, PyUFuncGenericFunction, PyArray_Descr **, void *)) \
|
||||
PyUFunc_API[41])
|
||||
|
||||
#if NPY_FEATURE_VERSION >= NPY_1_16_API_VERSION
|
||||
#define PyUFunc_FromFuncAndDataAndSignatureAndIdentity \
|
||||
(*(PyObject * (*)(PyUFuncGenericFunction *, void *const *, const char *, int, int, int, int, const char *, const char *, const int, const char *, PyObject *)) \
|
||||
PyUFunc_API[42])
|
||||
#endif
|
||||
|
||||
#if NPY_FEATURE_VERSION >= NPY_2_0_API_VERSION
|
||||
#define PyUFunc_AddLoopFromSpec \
|
||||
(*(int (*)(PyObject *, PyArrayMethod_Spec *)) \
|
||||
PyUFunc_API[43])
|
||||
#endif
|
||||
|
||||
#if NPY_FEATURE_VERSION >= NPY_2_0_API_VERSION
|
||||
#define PyUFunc_AddPromoter \
|
||||
(*(int (*)(PyObject *, PyObject *, PyObject *)) \
|
||||
PyUFunc_API[44])
|
||||
#endif
|
||||
|
||||
#if NPY_FEATURE_VERSION >= NPY_2_0_API_VERSION
|
||||
#define PyUFunc_AddWrappingLoop \
|
||||
(*(int (*)(PyObject *, PyArray_DTypeMeta *new_dtypes[], PyArray_DTypeMeta *wrapped_dtypes[], PyArrayMethod_TranslateGivenDescriptors *, PyArrayMethod_TranslateLoopDescriptors *)) \
|
||||
PyUFunc_API[45])
|
||||
#endif
|
||||
|
||||
#if NPY_FEATURE_VERSION >= NPY_2_0_API_VERSION
|
||||
#define PyUFunc_GiveFloatingpointErrors \
|
||||
(*(int (*)(const char *, int)) \
|
||||
PyUFunc_API[46])
|
||||
#endif
|
||||
|
||||
static inline int
|
||||
_import_umath(void)
|
||||
{
|
||||
PyObject *numpy = PyImport_ImportModule("numpy._core._multiarray_umath");
|
||||
if (numpy == NULL && PyErr_ExceptionMatches(PyExc_ModuleNotFoundError)) {
|
||||
PyErr_Clear();
|
||||
numpy = PyImport_ImportModule("numpy._core._multiarray_umath");
|
||||
if (numpy == NULL && PyErr_ExceptionMatches(PyExc_ModuleNotFoundError)) {
|
||||
PyErr_Clear();
|
||||
numpy = PyImport_ImportModule("numpy.core._multiarray_umath");
|
||||
}
|
||||
}
|
||||
|
||||
if (numpy == NULL) {
|
||||
PyErr_SetString(PyExc_ImportError,
|
||||
"_multiarray_umath failed to import");
|
||||
return -1;
|
||||
}
|
||||
|
||||
PyObject *c_api = PyObject_GetAttrString(numpy, "_UFUNC_API");
|
||||
Py_DECREF(numpy);
|
||||
if (c_api == NULL) {
|
||||
PyErr_SetString(PyExc_AttributeError, "_UFUNC_API not found");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!PyCapsule_CheckExact(c_api)) {
|
||||
PyErr_SetString(PyExc_RuntimeError, "_UFUNC_API is not PyCapsule object");
|
||||
Py_DECREF(c_api);
|
||||
return -1;
|
||||
}
|
||||
PyUFunc_API = (void **)PyCapsule_GetPointer(c_api, NULL);
|
||||
Py_DECREF(c_api);
|
||||
if (PyUFunc_API == NULL) {
|
||||
PyErr_SetString(PyExc_RuntimeError, "_UFUNC_API is NULL pointer");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define import_umath() \
|
||||
do {\
|
||||
UFUNC_NOFPE\
|
||||
if (_import_umath() < 0) {\
|
||||
PyErr_Print();\
|
||||
PyErr_SetString(PyExc_ImportError,\
|
||||
"numpy._core.umath failed to import");\
|
||||
return NULL;\
|
||||
}\
|
||||
} while(0)
|
||||
|
||||
#define import_umath1(ret) \
|
||||
do {\
|
||||
UFUNC_NOFPE\
|
||||
if (_import_umath() < 0) {\
|
||||
PyErr_Print();\
|
||||
PyErr_SetString(PyExc_ImportError,\
|
||||
"numpy._core.umath failed to import");\
|
||||
return ret;\
|
||||
}\
|
||||
} while(0)
|
||||
|
||||
#define import_umath2(ret, msg) \
|
||||
do {\
|
||||
UFUNC_NOFPE\
|
||||
if (_import_umath() < 0) {\
|
||||
PyErr_Print();\
|
||||
PyErr_SetString(PyExc_ImportError, msg);\
|
||||
return ret;\
|
||||
}\
|
||||
} while(0)
|
||||
|
||||
#define import_ufunc() \
|
||||
do {\
|
||||
UFUNC_NOFPE\
|
||||
if (_import_umath() < 0) {\
|
||||
PyErr_Print();\
|
||||
PyErr_SetString(PyExc_ImportError,\
|
||||
"numpy._core.umath failed to import");\
|
||||
}\
|
||||
} while(0)
|
||||
|
||||
|
||||
static inline int
|
||||
PyUFunc_ImportUFuncAPI()
|
||||
{
|
||||
if (NPY_UNLIKELY(PyUFunc_API == NULL)) {
|
||||
import_umath1(-1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
@ -0,0 +1,90 @@
|
||||
#ifndef NUMPY_CORE_INCLUDE_NUMPY__NEIGHBORHOOD_IMP_H_
|
||||
#error You should not include this header directly
|
||||
#endif
|
||||
/*
|
||||
* Private API (here for inline)
|
||||
*/
|
||||
static inline int
|
||||
_PyArrayNeighborhoodIter_IncrCoord(PyArrayNeighborhoodIterObject* iter);
|
||||
|
||||
/*
|
||||
* Update to next item of the iterator
|
||||
*
|
||||
* Note: this simply increment the coordinates vector, last dimension
|
||||
* incremented first , i.e, for dimension 3
|
||||
* ...
|
||||
* -1, -1, -1
|
||||
* -1, -1, 0
|
||||
* -1, -1, 1
|
||||
* ....
|
||||
* -1, 0, -1
|
||||
* -1, 0, 0
|
||||
* ....
|
||||
* 0, -1, -1
|
||||
* 0, -1, 0
|
||||
* ....
|
||||
*/
|
||||
#define _UPDATE_COORD_ITER(c) \
|
||||
wb = iter->coordinates[c] < iter->bounds[c][1]; \
|
||||
if (wb) { \
|
||||
iter->coordinates[c] += 1; \
|
||||
return 0; \
|
||||
} \
|
||||
else { \
|
||||
iter->coordinates[c] = iter->bounds[c][0]; \
|
||||
}
|
||||
|
||||
static inline int
|
||||
_PyArrayNeighborhoodIter_IncrCoord(PyArrayNeighborhoodIterObject* iter)
|
||||
{
|
||||
npy_intp i, wb;
|
||||
|
||||
for (i = iter->nd - 1; i >= 0; --i) {
|
||||
_UPDATE_COORD_ITER(i)
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Version optimized for 2d arrays, manual loop unrolling
|
||||
*/
|
||||
static inline int
|
||||
_PyArrayNeighborhoodIter_IncrCoord2D(PyArrayNeighborhoodIterObject* iter)
|
||||
{
|
||||
npy_intp wb;
|
||||
|
||||
_UPDATE_COORD_ITER(1)
|
||||
_UPDATE_COORD_ITER(0)
|
||||
|
||||
return 0;
|
||||
}
|
||||
#undef _UPDATE_COORD_ITER
|
||||
|
||||
/*
|
||||
* Advance to the next neighbour
|
||||
*/
|
||||
static inline int
|
||||
PyArrayNeighborhoodIter_Next(PyArrayNeighborhoodIterObject* iter)
|
||||
{
|
||||
_PyArrayNeighborhoodIter_IncrCoord (iter);
|
||||
iter->dataptr = iter->translate((PyArrayIterObject*)iter, iter->coordinates);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Reset functions
|
||||
*/
|
||||
static inline int
|
||||
PyArrayNeighborhoodIter_Reset(PyArrayNeighborhoodIterObject* iter)
|
||||
{
|
||||
npy_intp i;
|
||||
|
||||
for (i = 0; i < iter->nd; ++i) {
|
||||
iter->coordinates[i] = iter->bounds[i][0];
|
||||
}
|
||||
iter->dataptr = iter->translate((PyArrayIterObject*)iter, iter->coordinates);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -0,0 +1,33 @@
|
||||
/* #undef NPY_HAVE_ENDIAN_H */
|
||||
|
||||
#define NPY_SIZEOF_SHORT 2
|
||||
#define NPY_SIZEOF_INT 4
|
||||
#define NPY_SIZEOF_LONG 4
|
||||
#define NPY_SIZEOF_FLOAT 4
|
||||
#define NPY_SIZEOF_COMPLEX_FLOAT 8
|
||||
#define NPY_SIZEOF_DOUBLE 8
|
||||
#define NPY_SIZEOF_COMPLEX_DOUBLE 16
|
||||
#define NPY_SIZEOF_LONGDOUBLE 8
|
||||
#define NPY_SIZEOF_COMPLEX_LONGDOUBLE 16
|
||||
#define NPY_SIZEOF_PY_INTPTR_T 8
|
||||
#define NPY_SIZEOF_INTP 8
|
||||
#define NPY_SIZEOF_UINTP 8
|
||||
#define NPY_SIZEOF_WCHAR_T 2
|
||||
#define NPY_SIZEOF_OFF_T 4
|
||||
#define NPY_SIZEOF_PY_LONG_LONG 8
|
||||
#define NPY_SIZEOF_LONGLONG 8
|
||||
|
||||
/*
|
||||
* Defined to 1 or 0. Note that Pyodide hardcodes NPY_NO_SMP (and other defines
|
||||
* in this header) for better cross-compilation, so don't rename them without a
|
||||
* good reason.
|
||||
*/
|
||||
#define NPY_NO_SMP 0
|
||||
|
||||
#define NPY_VISIBILITY_HIDDEN
|
||||
#define NPY_ABI_VERSION 0x02000000
|
||||
#define NPY_API_VERSION 0x00000012
|
||||
|
||||
#ifndef __STDC_FORMAT_MACROS
|
||||
#define __STDC_FORMAT_MACROS 1
|
||||
#endif
|
||||
@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Public exposure of the DType Classes. These are tricky to expose
|
||||
* via the Python API, so they are exposed through this header for now.
|
||||
*
|
||||
* These definitions are only relevant for the public API and we reserve
|
||||
* the slots 320-360 in the API table generation for this (currently).
|
||||
*
|
||||
* TODO: This file should be consolidated with the API table generation
|
||||
* (although not sure the current generation is worth preserving).
|
||||
*/
|
||||
#ifndef NUMPY_CORE_INCLUDE_NUMPY__PUBLIC_DTYPE_API_TABLE_H_
|
||||
#define NUMPY_CORE_INCLUDE_NUMPY__PUBLIC_DTYPE_API_TABLE_H_
|
||||
|
||||
#if !(defined(NPY_INTERNAL_BUILD) && NPY_INTERNAL_BUILD)
|
||||
|
||||
/* All of these require NumPy 2.0 support */
|
||||
#if NPY_FEATURE_VERSION >= NPY_2_0_API_VERSION
|
||||
|
||||
/*
|
||||
* The type of the DType metaclass
|
||||
*/
|
||||
#define PyArrayDTypeMeta_Type (*(PyTypeObject *)(PyArray_API + 320)[0])
|
||||
/*
|
||||
* NumPy's builtin DTypes:
|
||||
*/
|
||||
#define PyArray_BoolDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[1])
|
||||
/* Integers */
|
||||
#define PyArray_ByteDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[2])
|
||||
#define PyArray_UByteDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[3])
|
||||
#define PyArray_ShortDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[4])
|
||||
#define PyArray_UShortDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[5])
|
||||
#define PyArray_IntDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[6])
|
||||
#define PyArray_UIntDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[7])
|
||||
#define PyArray_LongDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[8])
|
||||
#define PyArray_ULongDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[9])
|
||||
#define PyArray_LongLongDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[10])
|
||||
#define PyArray_ULongLongDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[11])
|
||||
/* Integer aliases */
|
||||
#define PyArray_Int8DType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[12])
|
||||
#define PyArray_UInt8DType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[13])
|
||||
#define PyArray_Int16DType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[14])
|
||||
#define PyArray_UInt16DType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[15])
|
||||
#define PyArray_Int32DType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[16])
|
||||
#define PyArray_UInt32DType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[17])
|
||||
#define PyArray_Int64DType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[18])
|
||||
#define PyArray_UInt64DType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[19])
|
||||
#define PyArray_IntpDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[20])
|
||||
#define PyArray_UIntpDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[21])
|
||||
/* Floats */
|
||||
#define PyArray_HalfDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[22])
|
||||
#define PyArray_FloatDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[23])
|
||||
#define PyArray_DoubleDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[24])
|
||||
#define PyArray_LongDoubleDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[25])
|
||||
/* Complex */
|
||||
#define PyArray_CFloatDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[26])
|
||||
#define PyArray_CDoubleDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[27])
|
||||
#define PyArray_CLongDoubleDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[28])
|
||||
/* String/Bytes */
|
||||
#define PyArray_BytesDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[29])
|
||||
#define PyArray_UnicodeDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[30])
|
||||
/* Datetime/Timedelta */
|
||||
#define PyArray_DatetimeDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[31])
|
||||
#define PyArray_TimedeltaDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[32])
|
||||
/* Object/Void */
|
||||
#define PyArray_ObjectDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[33])
|
||||
#define PyArray_VoidDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[34])
|
||||
/* Python types (used as markers for scalars) */
|
||||
#define PyArray_PyLongDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[35])
|
||||
#define PyArray_PyFloatDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[36])
|
||||
#define PyArray_PyComplexDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[37])
|
||||
/* Default integer type */
|
||||
#define PyArray_DefaultIntDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[38])
|
||||
/* New non-legacy DTypes follow in the order they were added */
|
||||
#define PyArray_StringDType (*(PyArray_DTypeMeta *)(PyArray_API + 320)[39])
|
||||
|
||||
/* NOTE: offset 40 is free */
|
||||
|
||||
/* Need to start with a larger offset again for the abstract classes: */
|
||||
#define PyArray_IntAbstractDType (*(PyArray_DTypeMeta *)PyArray_API[366])
|
||||
#define PyArray_FloatAbstractDType (*(PyArray_DTypeMeta *)PyArray_API[367])
|
||||
#define PyArray_ComplexAbstractDType (*(PyArray_DTypeMeta *)PyArray_API[368])
|
||||
|
||||
#endif /* NPY_FEATURE_VERSION >= NPY_2_0_API_VERSION */
|
||||
|
||||
#endif /* NPY_INTERNAL_BUILD */
|
||||
#endif /* NUMPY_CORE_INCLUDE_NUMPY__PUBLIC_DTYPE_API_TABLE_H_ */
|
||||
@ -0,0 +1,7 @@
|
||||
#ifndef NUMPY_CORE_INCLUDE_NUMPY_ARRAYOBJECT_H_
|
||||
#define NUMPY_CORE_INCLUDE_NUMPY_ARRAYOBJECT_H_
|
||||
#define Py_ARRAYOBJECT_H
|
||||
|
||||
#include "ndarrayobject.h"
|
||||
|
||||
#endif /* NUMPY_CORE_INCLUDE_NUMPY_ARRAYOBJECT_H_ */
|
||||
@ -0,0 +1,196 @@
|
||||
#ifndef NUMPY_CORE_INCLUDE_NUMPY_ARRAYSCALARS_H_
|
||||
#define NUMPY_CORE_INCLUDE_NUMPY_ARRAYSCALARS_H_
|
||||
|
||||
#ifndef _MULTIARRAYMODULE
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
npy_bool obval;
|
||||
} PyBoolScalarObject;
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
signed char obval;
|
||||
} PyByteScalarObject;
|
||||
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
short obval;
|
||||
} PyShortScalarObject;
|
||||
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
int obval;
|
||||
} PyIntScalarObject;
|
||||
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
long obval;
|
||||
} PyLongScalarObject;
|
||||
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
npy_longlong obval;
|
||||
} PyLongLongScalarObject;
|
||||
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
unsigned char obval;
|
||||
} PyUByteScalarObject;
|
||||
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
unsigned short obval;
|
||||
} PyUShortScalarObject;
|
||||
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
unsigned int obval;
|
||||
} PyUIntScalarObject;
|
||||
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
unsigned long obval;
|
||||
} PyULongScalarObject;
|
||||
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
npy_ulonglong obval;
|
||||
} PyULongLongScalarObject;
|
||||
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
npy_half obval;
|
||||
} PyHalfScalarObject;
|
||||
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
float obval;
|
||||
} PyFloatScalarObject;
|
||||
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
double obval;
|
||||
} PyDoubleScalarObject;
|
||||
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
npy_longdouble obval;
|
||||
} PyLongDoubleScalarObject;
|
||||
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
npy_cfloat obval;
|
||||
} PyCFloatScalarObject;
|
||||
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
npy_cdouble obval;
|
||||
} PyCDoubleScalarObject;
|
||||
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
npy_clongdouble obval;
|
||||
} PyCLongDoubleScalarObject;
|
||||
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
PyObject * obval;
|
||||
} PyObjectScalarObject;
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
npy_datetime obval;
|
||||
PyArray_DatetimeMetaData obmeta;
|
||||
} PyDatetimeScalarObject;
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
npy_timedelta obval;
|
||||
PyArray_DatetimeMetaData obmeta;
|
||||
} PyTimedeltaScalarObject;
|
||||
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
char obval;
|
||||
} PyScalarObject;
|
||||
|
||||
#define PyStringScalarObject PyBytesObject
|
||||
#ifndef Py_LIMITED_API
|
||||
typedef struct {
|
||||
/* note that the PyObject_HEAD macro lives right here */
|
||||
PyUnicodeObject base;
|
||||
Py_UCS4 *obval;
|
||||
#if NPY_FEATURE_VERSION >= NPY_1_20_API_VERSION
|
||||
char *buffer_fmt;
|
||||
#endif
|
||||
} PyUnicodeScalarObject;
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct {
|
||||
PyObject_VAR_HEAD
|
||||
char *obval;
|
||||
#if defined(NPY_INTERNAL_BUILD) && NPY_INTERNAL_BUILD
|
||||
/* Internally use the subclass to allow accessing names/fields */
|
||||
_PyArray_LegacyDescr *descr;
|
||||
#else
|
||||
PyArray_Descr *descr;
|
||||
#endif
|
||||
int flags;
|
||||
PyObject *base;
|
||||
#if NPY_FEATURE_VERSION >= NPY_1_20_API_VERSION
|
||||
void *_buffer_info; /* private buffer info, tagged to allow warning */
|
||||
#endif
|
||||
} PyVoidScalarObject;
|
||||
|
||||
/* Macros
|
||||
Py<Cls><bitsize>ScalarObject
|
||||
Py<Cls><bitsize>ArrType_Type
|
||||
are defined in ndarrayobject.h
|
||||
*/
|
||||
|
||||
#define PyArrayScalar_False ((PyObject *)(&(_PyArrayScalar_BoolValues[0])))
|
||||
#define PyArrayScalar_True ((PyObject *)(&(_PyArrayScalar_BoolValues[1])))
|
||||
#define PyArrayScalar_FromLong(i) \
|
||||
((PyObject *)(&(_PyArrayScalar_BoolValues[((i)!=0)])))
|
||||
#define PyArrayScalar_RETURN_BOOL_FROM_LONG(i) \
|
||||
return Py_INCREF(PyArrayScalar_FromLong(i)), \
|
||||
PyArrayScalar_FromLong(i)
|
||||
#define PyArrayScalar_RETURN_FALSE \
|
||||
return Py_INCREF(PyArrayScalar_False), \
|
||||
PyArrayScalar_False
|
||||
#define PyArrayScalar_RETURN_TRUE \
|
||||
return Py_INCREF(PyArrayScalar_True), \
|
||||
PyArrayScalar_True
|
||||
|
||||
#define PyArrayScalar_New(cls) \
|
||||
Py##cls##ArrType_Type.tp_alloc(&Py##cls##ArrType_Type, 0)
|
||||
#ifndef Py_LIMITED_API
|
||||
/* For the limited API, use PyArray_ScalarAsCtype instead */
|
||||
#define PyArrayScalar_VAL(obj, cls) \
|
||||
((Py##cls##ScalarObject *)obj)->obval
|
||||
#define PyArrayScalar_ASSIGN(obj, cls, val) \
|
||||
PyArrayScalar_VAL(obj, cls) = val
|
||||
#endif
|
||||
|
||||
#endif /* NUMPY_CORE_INCLUDE_NUMPY_ARRAYSCALARS_H_ */
|
||||
@ -0,0 +1,479 @@
|
||||
/*
|
||||
* The public DType API
|
||||
*/
|
||||
|
||||
#ifndef NUMPY_CORE_INCLUDE_NUMPY___DTYPE_API_H_
|
||||
#define NUMPY_CORE_INCLUDE_NUMPY___DTYPE_API_H_
|
||||
|
||||
struct PyArrayMethodObject_tag;
|
||||
|
||||
/*
|
||||
* Largely opaque struct for DType classes (i.e. metaclass instances).
|
||||
* The internal definition is currently in `ndarraytypes.h` (export is a bit
|
||||
* more complex because `PyArray_Descr` is a DTypeMeta internally but not
|
||||
* externally).
|
||||
*/
|
||||
#if !(defined(NPY_INTERNAL_BUILD) && NPY_INTERNAL_BUILD)
|
||||
|
||||
#ifndef Py_LIMITED_API
|
||||
|
||||
typedef struct PyArray_DTypeMeta_tag {
|
||||
PyHeapTypeObject super;
|
||||
|
||||
/*
|
||||
* Most DTypes will have a singleton default instance, for the
|
||||
* parametric legacy DTypes (bytes, string, void, datetime) this
|
||||
* may be a pointer to the *prototype* instance?
|
||||
*/
|
||||
PyArray_Descr *singleton;
|
||||
/* Copy of the legacy DTypes type number, usually invalid. */
|
||||
int type_num;
|
||||
|
||||
/* The type object of the scalar instances (may be NULL?) */
|
||||
PyTypeObject *scalar_type;
|
||||
/*
|
||||
* DType flags to signal legacy, parametric, or
|
||||
* abstract. But plenty of space for additional information/flags.
|
||||
*/
|
||||
npy_uint64 flags;
|
||||
|
||||
/*
|
||||
* Use indirection in order to allow a fixed size for this struct.
|
||||
* A stable ABI size makes creating a static DType less painful
|
||||
* while also ensuring flexibility for all opaque API (with one
|
||||
* indirection due the pointer lookup).
|
||||
*/
|
||||
void *dt_slots;
|
||||
/* Allow growing (at the moment also beyond this) */
|
||||
void *reserved[3];
|
||||
} PyArray_DTypeMeta;
|
||||
|
||||
#else
|
||||
|
||||
typedef PyTypeObject PyArray_DTypeMeta;
|
||||
|
||||
#endif /* Py_LIMITED_API */
|
||||
|
||||
#endif /* not internal build */
|
||||
|
||||
/*
|
||||
* ******************************************************
|
||||
* ArrayMethod API (Casting and UFuncs)
|
||||
* ******************************************************
|
||||
*/
|
||||
|
||||
|
||||
typedef enum {
|
||||
/* Flag for whether the GIL is required */
|
||||
NPY_METH_REQUIRES_PYAPI = 1 << 0,
|
||||
/*
|
||||
* Some functions cannot set floating point error flags, this flag
|
||||
* gives us the option (not requirement) to skip floating point error
|
||||
* setup/check. No function should set error flags and ignore them
|
||||
* since it would interfere with chaining operations (e.g. casting).
|
||||
*/
|
||||
NPY_METH_NO_FLOATINGPOINT_ERRORS = 1 << 1,
|
||||
/* Whether the method supports unaligned access (not runtime) */
|
||||
NPY_METH_SUPPORTS_UNALIGNED = 1 << 2,
|
||||
/*
|
||||
* Used for reductions to allow reordering the operation. At this point
|
||||
* assume that if set, it also applies to normal operations though!
|
||||
*/
|
||||
NPY_METH_IS_REORDERABLE = 1 << 3,
|
||||
/*
|
||||
* Private flag for now for *logic* functions. The logical functions
|
||||
* `logical_or` and `logical_and` can always cast the inputs to booleans
|
||||
* "safely" (because that is how the cast to bool is defined).
|
||||
* @seberg: I am not sure this is the best way to handle this, so its
|
||||
* private for now (also it is very limited anyway).
|
||||
* There is one "exception". NA aware dtypes cannot cast to bool
|
||||
* (hopefully), so the `??->?` loop should error even with this flag.
|
||||
* But a second NA fallback loop will be necessary.
|
||||
*/
|
||||
_NPY_METH_FORCE_CAST_INPUTS = 1 << 17,
|
||||
|
||||
/* All flags which can change at runtime */
|
||||
NPY_METH_RUNTIME_FLAGS = (
|
||||
NPY_METH_REQUIRES_PYAPI |
|
||||
NPY_METH_NO_FLOATINGPOINT_ERRORS),
|
||||
} NPY_ARRAYMETHOD_FLAGS;
|
||||
|
||||
|
||||
typedef struct PyArrayMethod_Context_tag {
|
||||
/* The caller, which is typically the original ufunc. May be NULL */
|
||||
PyObject *caller;
|
||||
/* The method "self". Currently an opaque object. */
|
||||
struct PyArrayMethodObject_tag *method;
|
||||
|
||||
/* Operand descriptors, filled in by resolve_descriptors */
|
||||
PyArray_Descr *const *descriptors;
|
||||
/* Structure may grow (this is harmless for DType authors) */
|
||||
} PyArrayMethod_Context;
|
||||
|
||||
|
||||
/*
|
||||
* The main object for creating a new ArrayMethod. We use the typical `slots`
|
||||
* mechanism used by the Python limited API (see below for the slot defs).
|
||||
*/
|
||||
typedef struct {
|
||||
const char *name;
|
||||
int nin, nout;
|
||||
NPY_CASTING casting;
|
||||
NPY_ARRAYMETHOD_FLAGS flags;
|
||||
PyArray_DTypeMeta **dtypes;
|
||||
PyType_Slot *slots;
|
||||
} PyArrayMethod_Spec;
|
||||
|
||||
|
||||
/*
|
||||
* ArrayMethod slots
|
||||
* -----------------
|
||||
*
|
||||
* SLOTS IDs For the ArrayMethod creation, once fully public, IDs are fixed
|
||||
* but can be deprecated and arbitrarily extended.
|
||||
*/
|
||||
#define _NPY_METH_resolve_descriptors_with_scalars 1
|
||||
#define NPY_METH_resolve_descriptors 2
|
||||
#define NPY_METH_get_loop 3
|
||||
#define NPY_METH_get_reduction_initial 4
|
||||
/* specific loops for constructions/default get_loop: */
|
||||
#define NPY_METH_strided_loop 5
|
||||
#define NPY_METH_contiguous_loop 6
|
||||
#define NPY_METH_unaligned_strided_loop 7
|
||||
#define NPY_METH_unaligned_contiguous_loop 8
|
||||
#define NPY_METH_contiguous_indexed_loop 9
|
||||
#define _NPY_METH_static_data 10
|
||||
|
||||
|
||||
/*
|
||||
* The resolve descriptors function, must be able to handle NULL values for
|
||||
* all output (but not input) `given_descrs` and fill `loop_descrs`.
|
||||
* Return -1 on error or 0 if the operation is not possible without an error
|
||||
* set. (This may still be in flux.)
|
||||
* Otherwise must return the "casting safety", for normal functions, this is
|
||||
* almost always "safe" (or even "equivalent"?).
|
||||
*
|
||||
* `resolve_descriptors` is optional if all output DTypes are non-parametric.
|
||||
*/
|
||||
typedef NPY_CASTING (PyArrayMethod_ResolveDescriptors)(
|
||||
/* "method" is currently opaque (necessary e.g. to wrap Python) */
|
||||
struct PyArrayMethodObject_tag *method,
|
||||
/* DTypes the method was created for */
|
||||
PyArray_DTypeMeta *const *dtypes,
|
||||
/* Input descriptors (instances). Outputs may be NULL. */
|
||||
PyArray_Descr *const *given_descrs,
|
||||
/* Exact loop descriptors to use, must not hold references on error */
|
||||
PyArray_Descr **loop_descrs,
|
||||
npy_intp *view_offset);
|
||||
|
||||
|
||||
/*
|
||||
* Rarely needed, slightly more powerful version of `resolve_descriptors`.
|
||||
* See also `PyArrayMethod_ResolveDescriptors` for details on shared arguments.
|
||||
*
|
||||
* NOTE: This function is private now as it is unclear how and what to pass
|
||||
* exactly as additional information to allow dealing with the scalars.
|
||||
* See also gh-24915.
|
||||
*/
|
||||
typedef NPY_CASTING (PyArrayMethod_ResolveDescriptorsWithScalar)(
|
||||
struct PyArrayMethodObject_tag *method,
|
||||
PyArray_DTypeMeta *const *dtypes,
|
||||
/* Unlike above, these can have any DType and we may allow NULL. */
|
||||
PyArray_Descr *const *given_descrs,
|
||||
/*
|
||||
* Input scalars or NULL. Only ever passed for python scalars.
|
||||
* WARNING: In some cases, a loop may be explicitly selected and the
|
||||
* value passed is not available (NULL) or does not have the
|
||||
* expected type.
|
||||
*/
|
||||
PyObject *const *input_scalars,
|
||||
PyArray_Descr **loop_descrs,
|
||||
npy_intp *view_offset);
|
||||
|
||||
|
||||
|
||||
typedef int (PyArrayMethod_StridedLoop)(PyArrayMethod_Context *context,
|
||||
char *const *data, const npy_intp *dimensions, const npy_intp *strides,
|
||||
NpyAuxData *transferdata);
|
||||
|
||||
|
||||
typedef int (PyArrayMethod_GetLoop)(
|
||||
PyArrayMethod_Context *context,
|
||||
int aligned, int move_references,
|
||||
const npy_intp *strides,
|
||||
PyArrayMethod_StridedLoop **out_loop,
|
||||
NpyAuxData **out_transferdata,
|
||||
NPY_ARRAYMETHOD_FLAGS *flags);
|
||||
|
||||
/**
|
||||
* Query an ArrayMethod for the initial value for use in reduction.
|
||||
*
|
||||
* @param context The arraymethod context, mainly to access the descriptors.
|
||||
* @param reduction_is_empty Whether the reduction is empty. When it is, the
|
||||
* value returned may differ. In this case it is a "default" value that
|
||||
* may differ from the "identity" value normally used. For example:
|
||||
* - `0.0` is the default for `sum([])`. But `-0.0` is the correct
|
||||
* identity otherwise as it preserves the sign for `sum([-0.0])`.
|
||||
* - We use no identity for object, but return the default of `0` and `1`
|
||||
* for the empty `sum([], dtype=object)` and `prod([], dtype=object)`.
|
||||
* This allows `np.sum(np.array(["a", "b"], dtype=object))` to work.
|
||||
* - `-inf` or `INT_MIN` for `max` is an identity, but at least `INT_MIN`
|
||||
* not a good *default* when there are no items.
|
||||
* @param initial Pointer to initial data to be filled (if possible)
|
||||
*
|
||||
* @returns -1, 0, or 1 indicating error, no initial value, and initial being
|
||||
* successfully filled. Errors must not be given where 0 is correct, NumPy
|
||||
* may call this even when not strictly necessary.
|
||||
*/
|
||||
typedef int (PyArrayMethod_GetReductionInitial)(
|
||||
PyArrayMethod_Context *context, npy_bool reduction_is_empty,
|
||||
void *initial);
|
||||
|
||||
/*
|
||||
* The following functions are only used by the wrapping array method defined
|
||||
* in umath/wrapping_array_method.c
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* The function to convert the given descriptors (passed in to
|
||||
* `resolve_descriptors`) and translates them for the wrapped loop.
|
||||
* The new descriptors MUST be viewable with the old ones, `NULL` must be
|
||||
* supported (for outputs) and should normally be forwarded.
|
||||
*
|
||||
* The function must clean up on error.
|
||||
*
|
||||
* NOTE: We currently assume that this translation gives "viewable" results.
|
||||
* I.e. there is no additional casting related to the wrapping process.
|
||||
* In principle that could be supported, but not sure it is useful.
|
||||
* This currently also means that e.g. alignment must apply identically
|
||||
* to the new dtypes.
|
||||
*
|
||||
* TODO: Due to the fact that `resolve_descriptors` is also used for `can_cast`
|
||||
* there is no way to "pass out" the result of this function. This means
|
||||
* it will be called twice for every ufunc call.
|
||||
* (I am considering including `auxdata` as an "optional" parameter to
|
||||
* `resolve_descriptors`, so that it can be filled there if not NULL.)
|
||||
*/
|
||||
typedef int (PyArrayMethod_TranslateGivenDescriptors)(int nin, int nout,
|
||||
PyArray_DTypeMeta *const wrapped_dtypes[],
|
||||
PyArray_Descr *const given_descrs[], PyArray_Descr *new_descrs[]);
|
||||
|
||||
/**
|
||||
* The function to convert the actual loop descriptors (as returned by the
|
||||
* original `resolve_descriptors` function) to the ones the output array
|
||||
* should use.
|
||||
* This function must return "viewable" types, it must not mutate them in any
|
||||
* form that would break the inner-loop logic. Does not need to support NULL.
|
||||
*
|
||||
* The function must clean up on error.
|
||||
*
|
||||
* @param nargs Number of arguments
|
||||
* @param new_dtypes The DTypes of the output (usually probably not needed)
|
||||
* @param given_descrs Original given_descrs to the resolver, necessary to
|
||||
* fetch any information related to the new dtypes from the original.
|
||||
* @param original_descrs The `loop_descrs` returned by the wrapped loop.
|
||||
* @param loop_descrs The output descriptors, compatible to `original_descrs`.
|
||||
*
|
||||
* @returns 0 on success, -1 on failure.
|
||||
*/
|
||||
typedef int (PyArrayMethod_TranslateLoopDescriptors)(int nin, int nout,
|
||||
PyArray_DTypeMeta *const new_dtypes[], PyArray_Descr *const given_descrs[],
|
||||
PyArray_Descr *original_descrs[], PyArray_Descr *loop_descrs[]);
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* A traverse loop working on a single array. This is similar to the general
|
||||
* strided-loop function. This is designed for loops that need to visit every
|
||||
* element of a single array.
|
||||
*
|
||||
* Currently this is used for array clearing, via the NPY_DT_get_clear_loop
|
||||
* API hook, and zero-filling, via the NPY_DT_get_fill_zero_loop API hook.
|
||||
* These are most useful for handling arrays storing embedded references to
|
||||
* python objects or heap-allocated data.
|
||||
*
|
||||
* The `void *traverse_context` is passed in because we may need to pass in
|
||||
* Interpreter state or similar in the future, but we don't want to pass in
|
||||
* a full context (with pointers to dtypes, method, caller which all make
|
||||
* no sense for a traverse function).
|
||||
*
|
||||
* We assume for now that this context can be just passed through in the
|
||||
* the future (for structured dtypes).
|
||||
*
|
||||
*/
|
||||
typedef int (PyArrayMethod_TraverseLoop)(
|
||||
void *traverse_context, const PyArray_Descr *descr, char *data,
|
||||
npy_intp size, npy_intp stride, NpyAuxData *auxdata);
|
||||
|
||||
|
||||
/*
|
||||
* Simplified get_loop function specific to dtype traversal
|
||||
*
|
||||
* It should set the flags needed for the traversal loop and set out_loop to the
|
||||
* loop function, which must be a valid PyArrayMethod_TraverseLoop
|
||||
* pointer. Currently this is used for zero-filling and clearing arrays storing
|
||||
* embedded references.
|
||||
*
|
||||
*/
|
||||
typedef int (PyArrayMethod_GetTraverseLoop)(
|
||||
void *traverse_context, const PyArray_Descr *descr,
|
||||
int aligned, npy_intp fixed_stride,
|
||||
PyArrayMethod_TraverseLoop **out_loop, NpyAuxData **out_auxdata,
|
||||
NPY_ARRAYMETHOD_FLAGS *flags);
|
||||
|
||||
|
||||
/*
|
||||
* Type of the C promoter function, which must be wrapped into a
|
||||
* PyCapsule with name "numpy._ufunc_promoter".
|
||||
*
|
||||
* Note that currently the output dtypes are always NULL unless they are
|
||||
* also part of the signature. This is an implementation detail and could
|
||||
* change in the future. However, in general promoters should not have a
|
||||
* need for output dtypes.
|
||||
* (There are potential use-cases, these are currently unsupported.)
|
||||
*/
|
||||
typedef int (PyArrayMethod_PromoterFunction)(PyObject *ufunc,
|
||||
PyArray_DTypeMeta *const op_dtypes[], PyArray_DTypeMeta *const signature[],
|
||||
PyArray_DTypeMeta *new_op_dtypes[]);
|
||||
|
||||
/*
|
||||
* ****************************
|
||||
* DTYPE API
|
||||
* ****************************
|
||||
*/
|
||||
|
||||
#define NPY_DT_ABSTRACT 1 << 1
|
||||
#define NPY_DT_PARAMETRIC 1 << 2
|
||||
#define NPY_DT_NUMERIC 1 << 3
|
||||
|
||||
/*
|
||||
* These correspond to slots in the NPY_DType_Slots struct and must
|
||||
* be in the same order as the members of that struct. If new slots
|
||||
* get added or old slots get removed NPY_NUM_DTYPE_SLOTS must also
|
||||
* be updated
|
||||
*/
|
||||
|
||||
#define NPY_DT_discover_descr_from_pyobject 1
|
||||
// this slot is considered private because its API hasn't been decided
|
||||
#define _NPY_DT_is_known_scalar_type 2
|
||||
#define NPY_DT_default_descr 3
|
||||
#define NPY_DT_common_dtype 4
|
||||
#define NPY_DT_common_instance 5
|
||||
#define NPY_DT_ensure_canonical 6
|
||||
#define NPY_DT_setitem 7
|
||||
#define NPY_DT_getitem 8
|
||||
#define NPY_DT_get_clear_loop 9
|
||||
#define NPY_DT_get_fill_zero_loop 10
|
||||
#define NPY_DT_finalize_descr 11
|
||||
|
||||
// These PyArray_ArrFunc slots will be deprecated and replaced eventually
|
||||
// getitem and setitem can be defined as a performance optimization;
|
||||
// by default the user dtypes call `legacy_getitem_using_DType` and
|
||||
// `legacy_setitem_using_DType`, respectively. This functionality is
|
||||
// only supported for basic NumPy DTypes.
|
||||
|
||||
|
||||
// used to separate dtype slots from arrfuncs slots
|
||||
// intended only for internal use but defined here for clarity
|
||||
#define _NPY_DT_ARRFUNCS_OFFSET (1 << 10)
|
||||
|
||||
// Cast is disabled
|
||||
// #define NPY_DT_PyArray_ArrFuncs_cast 0 + _NPY_DT_ARRFUNCS_OFFSET
|
||||
|
||||
#define NPY_DT_PyArray_ArrFuncs_getitem 1 + _NPY_DT_ARRFUNCS_OFFSET
|
||||
#define NPY_DT_PyArray_ArrFuncs_setitem 2 + _NPY_DT_ARRFUNCS_OFFSET
|
||||
|
||||
// Copyswap is disabled
|
||||
// #define NPY_DT_PyArray_ArrFuncs_copyswapn 3 + _NPY_DT_ARRFUNCS_OFFSET
|
||||
// #define NPY_DT_PyArray_ArrFuncs_copyswap 4 + _NPY_DT_ARRFUNCS_OFFSET
|
||||
#define NPY_DT_PyArray_ArrFuncs_compare 5 + _NPY_DT_ARRFUNCS_OFFSET
|
||||
#define NPY_DT_PyArray_ArrFuncs_argmax 6 + _NPY_DT_ARRFUNCS_OFFSET
|
||||
#define NPY_DT_PyArray_ArrFuncs_dotfunc 7 + _NPY_DT_ARRFUNCS_OFFSET
|
||||
#define NPY_DT_PyArray_ArrFuncs_scanfunc 8 + _NPY_DT_ARRFUNCS_OFFSET
|
||||
#define NPY_DT_PyArray_ArrFuncs_fromstr 9 + _NPY_DT_ARRFUNCS_OFFSET
|
||||
#define NPY_DT_PyArray_ArrFuncs_nonzero 10 + _NPY_DT_ARRFUNCS_OFFSET
|
||||
#define NPY_DT_PyArray_ArrFuncs_fill 11 + _NPY_DT_ARRFUNCS_OFFSET
|
||||
#define NPY_DT_PyArray_ArrFuncs_fillwithscalar 12 + _NPY_DT_ARRFUNCS_OFFSET
|
||||
#define NPY_DT_PyArray_ArrFuncs_sort 13 + _NPY_DT_ARRFUNCS_OFFSET
|
||||
#define NPY_DT_PyArray_ArrFuncs_argsort 14 + _NPY_DT_ARRFUNCS_OFFSET
|
||||
|
||||
// Casting related slots are disabled. See
|
||||
// https://github.com/numpy/numpy/pull/23173#discussion_r1101098163
|
||||
// #define NPY_DT_PyArray_ArrFuncs_castdict 15 + _NPY_DT_ARRFUNCS_OFFSET
|
||||
// #define NPY_DT_PyArray_ArrFuncs_scalarkind 16 + _NPY_DT_ARRFUNCS_OFFSET
|
||||
// #define NPY_DT_PyArray_ArrFuncs_cancastscalarkindto 17 + _NPY_DT_ARRFUNCS_OFFSET
|
||||
// #define NPY_DT_PyArray_ArrFuncs_cancastto 18 + _NPY_DT_ARRFUNCS_OFFSET
|
||||
|
||||
// These are deprecated in NumPy 1.19, so are disabled here.
|
||||
// #define NPY_DT_PyArray_ArrFuncs_fastclip 19 + _NPY_DT_ARRFUNCS_OFFSET
|
||||
// #define NPY_DT_PyArray_ArrFuncs_fastputmask 20 + _NPY_DT_ARRFUNCS_OFFSET
|
||||
// #define NPY_DT_PyArray_ArrFuncs_fasttake 21 + _NPY_DT_ARRFUNCS_OFFSET
|
||||
#define NPY_DT_PyArray_ArrFuncs_argmin 22 + _NPY_DT_ARRFUNCS_OFFSET
|
||||
|
||||
|
||||
// TODO: These slots probably still need some thought, and/or a way to "grow"?
|
||||
typedef struct {
|
||||
PyTypeObject *typeobj; /* type of python scalar or NULL */
|
||||
int flags; /* flags, including parametric and abstract */
|
||||
/* NULL terminated cast definitions. Use NULL for the newly created DType */
|
||||
PyArrayMethod_Spec **casts;
|
||||
PyType_Slot *slots;
|
||||
/* Baseclass or NULL (will always subclass `np.dtype`) */
|
||||
PyTypeObject *baseclass;
|
||||
} PyArrayDTypeMeta_Spec;
|
||||
|
||||
|
||||
typedef PyArray_Descr *(PyArrayDTypeMeta_DiscoverDescrFromPyobject)(
|
||||
PyArray_DTypeMeta *cls, PyObject *obj);
|
||||
|
||||
/*
|
||||
* Before making this public, we should decide whether it should pass
|
||||
* the type, or allow looking at the object. A possible use-case:
|
||||
* `np.array(np.array([0]), dtype=np.ndarray)`
|
||||
* Could consider arrays that are not `dtype=ndarray` "scalars".
|
||||
*/
|
||||
typedef int (PyArrayDTypeMeta_IsKnownScalarType)(
|
||||
PyArray_DTypeMeta *cls, PyTypeObject *obj);
|
||||
|
||||
typedef PyArray_Descr *(PyArrayDTypeMeta_DefaultDescriptor)(PyArray_DTypeMeta *cls);
|
||||
typedef PyArray_DTypeMeta *(PyArrayDTypeMeta_CommonDType)(
|
||||
PyArray_DTypeMeta *dtype1, PyArray_DTypeMeta *dtype2);
|
||||
|
||||
|
||||
/*
|
||||
* Convenience utility for getting a reference to the DType metaclass associated
|
||||
* with a dtype instance.
|
||||
*/
|
||||
#define NPY_DTYPE(descr) ((PyArray_DTypeMeta *)Py_TYPE(descr))
|
||||
|
||||
static inline PyArray_DTypeMeta *
|
||||
NPY_DT_NewRef(PyArray_DTypeMeta *o) {
|
||||
Py_INCREF((PyObject *)o);
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
typedef PyArray_Descr *(PyArrayDTypeMeta_CommonInstance)(
|
||||
PyArray_Descr *dtype1, PyArray_Descr *dtype2);
|
||||
typedef PyArray_Descr *(PyArrayDTypeMeta_EnsureCanonical)(PyArray_Descr *dtype);
|
||||
/*
|
||||
* Returns either a new reference to *dtype* or a new descriptor instance
|
||||
* initialized with the same parameters as *dtype*. The caller cannot know
|
||||
* which choice a dtype will make. This function is called just before the
|
||||
* array buffer is created for a newly created array, it is not called for
|
||||
* views and the descriptor returned by this function is attached to the array.
|
||||
*/
|
||||
typedef PyArray_Descr *(PyArrayDTypeMeta_FinalizeDescriptor)(PyArray_Descr *dtype);
|
||||
|
||||
/*
|
||||
* TODO: These two functions are currently only used for experimental DType
|
||||
* API support. Their relation should be "reversed": NumPy should
|
||||
* always use them internally.
|
||||
* There are open points about "casting safety" though, e.g. setting
|
||||
* elements is currently always unsafe.
|
||||
*/
|
||||
typedef int(PyArrayDTypeMeta_SetItem)(PyArray_Descr *, PyObject *, char *);
|
||||
typedef PyObject *(PyArrayDTypeMeta_GetItem)(PyArray_Descr *, char *);
|
||||
|
||||
#endif /* NUMPY_CORE_INCLUDE_NUMPY___DTYPE_API_H_ */
|
||||
@ -0,0 +1,70 @@
|
||||
#ifndef NUMPY_CORE_INCLUDE_NUMPY_HALFFLOAT_H_
|
||||
#define NUMPY_CORE_INCLUDE_NUMPY_HALFFLOAT_H_
|
||||
|
||||
#include <Python.h>
|
||||
#include <numpy/npy_math.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Half-precision routines
|
||||
*/
|
||||
|
||||
/* Conversions */
|
||||
float npy_half_to_float(npy_half h);
|
||||
double npy_half_to_double(npy_half h);
|
||||
npy_half npy_float_to_half(float f);
|
||||
npy_half npy_double_to_half(double d);
|
||||
/* Comparisons */
|
||||
int npy_half_eq(npy_half h1, npy_half h2);
|
||||
int npy_half_ne(npy_half h1, npy_half h2);
|
||||
int npy_half_le(npy_half h1, npy_half h2);
|
||||
int npy_half_lt(npy_half h1, npy_half h2);
|
||||
int npy_half_ge(npy_half h1, npy_half h2);
|
||||
int npy_half_gt(npy_half h1, npy_half h2);
|
||||
/* faster *_nonan variants for when you know h1 and h2 are not NaN */
|
||||
int npy_half_eq_nonan(npy_half h1, npy_half h2);
|
||||
int npy_half_lt_nonan(npy_half h1, npy_half h2);
|
||||
int npy_half_le_nonan(npy_half h1, npy_half h2);
|
||||
/* Miscellaneous functions */
|
||||
int npy_half_iszero(npy_half h);
|
||||
int npy_half_isnan(npy_half h);
|
||||
int npy_half_isinf(npy_half h);
|
||||
int npy_half_isfinite(npy_half h);
|
||||
int npy_half_signbit(npy_half h);
|
||||
npy_half npy_half_copysign(npy_half x, npy_half y);
|
||||
npy_half npy_half_spacing(npy_half h);
|
||||
npy_half npy_half_nextafter(npy_half x, npy_half y);
|
||||
npy_half npy_half_divmod(npy_half x, npy_half y, npy_half *modulus);
|
||||
|
||||
/*
|
||||
* Half-precision constants
|
||||
*/
|
||||
|
||||
#define NPY_HALF_ZERO (0x0000u)
|
||||
#define NPY_HALF_PZERO (0x0000u)
|
||||
#define NPY_HALF_NZERO (0x8000u)
|
||||
#define NPY_HALF_ONE (0x3c00u)
|
||||
#define NPY_HALF_NEGONE (0xbc00u)
|
||||
#define NPY_HALF_PINF (0x7c00u)
|
||||
#define NPY_HALF_NINF (0xfc00u)
|
||||
#define NPY_HALF_NAN (0x7e00u)
|
||||
|
||||
#define NPY_MAX_HALF (0x7bffu)
|
||||
|
||||
/*
|
||||
* Bit-level conversions
|
||||
*/
|
||||
|
||||
npy_uint16 npy_floatbits_to_halfbits(npy_uint32 f);
|
||||
npy_uint16 npy_doublebits_to_halfbits(npy_uint64 d);
|
||||
npy_uint32 npy_halfbits_to_floatbits(npy_uint16 h);
|
||||
npy_uint64 npy_halfbits_to_doublebits(npy_uint16 h);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* NUMPY_CORE_INCLUDE_NUMPY_HALFFLOAT_H_ */
|
||||
@ -0,0 +1,304 @@
|
||||
/*
|
||||
* DON'T INCLUDE THIS DIRECTLY.
|
||||
*/
|
||||
#ifndef NUMPY_CORE_INCLUDE_NUMPY_NDARRAYOBJECT_H_
|
||||
#define NUMPY_CORE_INCLUDE_NUMPY_NDARRAYOBJECT_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <Python.h>
|
||||
#include "ndarraytypes.h"
|
||||
#include "dtype_api.h"
|
||||
|
||||
/* Includes the "function" C-API -- these are all stored in a
|
||||
list of pointers --- one for each file
|
||||
The two lists are concatenated into one in multiarray.
|
||||
|
||||
They are available as import_array()
|
||||
*/
|
||||
|
||||
#include "__multiarray_api.h"
|
||||
|
||||
/*
|
||||
* Include any definitions which are defined differently for 1.x and 2.x
|
||||
* (Symbols only available on 2.x are not there, but rather guarded.)
|
||||
*/
|
||||
#include "npy_2_compat.h"
|
||||
|
||||
/* C-API that requires previous API to be defined */
|
||||
|
||||
#define PyArray_DescrCheck(op) PyObject_TypeCheck(op, &PyArrayDescr_Type)
|
||||
|
||||
#define PyArray_Check(op) PyObject_TypeCheck(op, &PyArray_Type)
|
||||
#define PyArray_CheckExact(op) (((PyObject*)(op))->ob_type == &PyArray_Type)
|
||||
|
||||
#define PyArray_HasArrayInterfaceType(op, type, context, out) \
|
||||
((((out)=PyArray_FromStructInterface(op)) != Py_NotImplemented) || \
|
||||
(((out)=PyArray_FromInterface(op)) != Py_NotImplemented) || \
|
||||
(((out)=PyArray_FromArrayAttr(op, type, context)) != \
|
||||
Py_NotImplemented))
|
||||
|
||||
#define PyArray_HasArrayInterface(op, out) \
|
||||
PyArray_HasArrayInterfaceType(op, NULL, NULL, out)
|
||||
|
||||
#define PyArray_IsZeroDim(op) (PyArray_Check(op) && \
|
||||
(PyArray_NDIM((PyArrayObject *)op) == 0))
|
||||
|
||||
#define PyArray_IsScalar(obj, cls) \
|
||||
(PyObject_TypeCheck(obj, &Py##cls##ArrType_Type))
|
||||
|
||||
#define PyArray_CheckScalar(m) (PyArray_IsScalar(m, Generic) || \
|
||||
PyArray_IsZeroDim(m))
|
||||
#define PyArray_IsPythonNumber(obj) \
|
||||
(PyFloat_Check(obj) || PyComplex_Check(obj) || \
|
||||
PyLong_Check(obj) || PyBool_Check(obj))
|
||||
#define PyArray_IsIntegerScalar(obj) (PyLong_Check(obj) \
|
||||
|| PyArray_IsScalar((obj), Integer))
|
||||
#define PyArray_IsPythonScalar(obj) \
|
||||
(PyArray_IsPythonNumber(obj) || PyBytes_Check(obj) || \
|
||||
PyUnicode_Check(obj))
|
||||
|
||||
#define PyArray_IsAnyScalar(obj) \
|
||||
(PyArray_IsScalar(obj, Generic) || PyArray_IsPythonScalar(obj))
|
||||
|
||||
#define PyArray_CheckAnyScalar(obj) (PyArray_IsPythonScalar(obj) || \
|
||||
PyArray_CheckScalar(obj))
|
||||
|
||||
|
||||
#define PyArray_GETCONTIGUOUS(m) (PyArray_ISCONTIGUOUS(m) ? \
|
||||
Py_INCREF(m), (m) : \
|
||||
(PyArrayObject *)(PyArray_Copy(m)))
|
||||
|
||||
#define PyArray_SAMESHAPE(a1,a2) ((PyArray_NDIM(a1) == PyArray_NDIM(a2)) && \
|
||||
PyArray_CompareLists(PyArray_DIMS(a1), \
|
||||
PyArray_DIMS(a2), \
|
||||
PyArray_NDIM(a1)))
|
||||
|
||||
#define PyArray_SIZE(m) PyArray_MultiplyList(PyArray_DIMS(m), PyArray_NDIM(m))
|
||||
#define PyArray_NBYTES(m) (PyArray_ITEMSIZE(m) * PyArray_SIZE(m))
|
||||
#define PyArray_FROM_O(m) PyArray_FromAny(m, NULL, 0, 0, 0, NULL)
|
||||
|
||||
#define PyArray_FROM_OF(m,flags) PyArray_CheckFromAny(m, NULL, 0, 0, flags, \
|
||||
NULL)
|
||||
|
||||
#define PyArray_FROM_OT(m,type) PyArray_FromAny(m, \
|
||||
PyArray_DescrFromType(type), 0, 0, 0, NULL)
|
||||
|
||||
#define PyArray_FROM_OTF(m, type, flags) \
|
||||
PyArray_FromAny(m, PyArray_DescrFromType(type), 0, 0, \
|
||||
(((flags) & NPY_ARRAY_ENSURECOPY) ? \
|
||||
((flags) | NPY_ARRAY_DEFAULT) : (flags)), NULL)
|
||||
|
||||
#define PyArray_FROMANY(m, type, min, max, flags) \
|
||||
PyArray_FromAny(m, PyArray_DescrFromType(type), min, max, \
|
||||
(((flags) & NPY_ARRAY_ENSURECOPY) ? \
|
||||
(flags) | NPY_ARRAY_DEFAULT : (flags)), NULL)
|
||||
|
||||
#define PyArray_ZEROS(m, dims, type, is_f_order) \
|
||||
PyArray_Zeros(m, dims, PyArray_DescrFromType(type), is_f_order)
|
||||
|
||||
#define PyArray_EMPTY(m, dims, type, is_f_order) \
|
||||
PyArray_Empty(m, dims, PyArray_DescrFromType(type), is_f_order)
|
||||
|
||||
#define PyArray_FILLWBYTE(obj, val) memset(PyArray_DATA(obj), val, \
|
||||
PyArray_NBYTES(obj))
|
||||
|
||||
#define PyArray_ContiguousFromAny(op, type, min_depth, max_depth) \
|
||||
PyArray_FromAny(op, PyArray_DescrFromType(type), min_depth, \
|
||||
max_depth, NPY_ARRAY_DEFAULT, NULL)
|
||||
|
||||
#define PyArray_EquivArrTypes(a1, a2) \
|
||||
PyArray_EquivTypes(PyArray_DESCR(a1), PyArray_DESCR(a2))
|
||||
|
||||
#define PyArray_EquivByteorders(b1, b2) \
|
||||
(((b1) == (b2)) || (PyArray_ISNBO(b1) == PyArray_ISNBO(b2)))
|
||||
|
||||
#define PyArray_SimpleNew(nd, dims, typenum) \
|
||||
PyArray_New(&PyArray_Type, nd, dims, typenum, NULL, NULL, 0, 0, NULL)
|
||||
|
||||
#define PyArray_SimpleNewFromData(nd, dims, typenum, data) \
|
||||
PyArray_New(&PyArray_Type, nd, dims, typenum, NULL, \
|
||||
data, 0, NPY_ARRAY_CARRAY, NULL)
|
||||
|
||||
#define PyArray_SimpleNewFromDescr(nd, dims, descr) \
|
||||
PyArray_NewFromDescr(&PyArray_Type, descr, nd, dims, \
|
||||
NULL, NULL, 0, NULL)
|
||||
|
||||
#define PyArray_ToScalar(data, arr) \
|
||||
PyArray_Scalar(data, PyArray_DESCR(arr), (PyObject *)arr)
|
||||
|
||||
|
||||
/* These might be faster without the dereferencing of obj
|
||||
going on inside -- of course an optimizing compiler should
|
||||
inline the constants inside a for loop making it a moot point
|
||||
*/
|
||||
|
||||
#define PyArray_GETPTR1(obj, i) ((void *)(PyArray_BYTES(obj) + \
|
||||
(i)*PyArray_STRIDES(obj)[0]))
|
||||
|
||||
#define PyArray_GETPTR2(obj, i, j) ((void *)(PyArray_BYTES(obj) + \
|
||||
(i)*PyArray_STRIDES(obj)[0] + \
|
||||
(j)*PyArray_STRIDES(obj)[1]))
|
||||
|
||||
#define PyArray_GETPTR3(obj, i, j, k) ((void *)(PyArray_BYTES(obj) + \
|
||||
(i)*PyArray_STRIDES(obj)[0] + \
|
||||
(j)*PyArray_STRIDES(obj)[1] + \
|
||||
(k)*PyArray_STRIDES(obj)[2]))
|
||||
|
||||
#define PyArray_GETPTR4(obj, i, j, k, l) ((void *)(PyArray_BYTES(obj) + \
|
||||
(i)*PyArray_STRIDES(obj)[0] + \
|
||||
(j)*PyArray_STRIDES(obj)[1] + \
|
||||
(k)*PyArray_STRIDES(obj)[2] + \
|
||||
(l)*PyArray_STRIDES(obj)[3]))
|
||||
|
||||
static inline void
|
||||
PyArray_DiscardWritebackIfCopy(PyArrayObject *arr)
|
||||
{
|
||||
PyArrayObject_fields *fa = (PyArrayObject_fields *)arr;
|
||||
if (fa && fa->base) {
|
||||
if (fa->flags & NPY_ARRAY_WRITEBACKIFCOPY) {
|
||||
PyArray_ENABLEFLAGS((PyArrayObject*)fa->base, NPY_ARRAY_WRITEABLE);
|
||||
Py_DECREF(fa->base);
|
||||
fa->base = NULL;
|
||||
PyArray_CLEARFLAGS(arr, NPY_ARRAY_WRITEBACKIFCOPY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define PyArray_DESCR_REPLACE(descr) do { \
|
||||
PyArray_Descr *_new_; \
|
||||
_new_ = PyArray_DescrNew(descr); \
|
||||
Py_XDECREF(descr); \
|
||||
descr = _new_; \
|
||||
} while(0)
|
||||
|
||||
/* Copy should always return contiguous array */
|
||||
#define PyArray_Copy(obj) PyArray_NewCopy(obj, NPY_CORDER)
|
||||
|
||||
#define PyArray_FromObject(op, type, min_depth, max_depth) \
|
||||
PyArray_FromAny(op, PyArray_DescrFromType(type), min_depth, \
|
||||
max_depth, NPY_ARRAY_BEHAVED | \
|
||||
NPY_ARRAY_ENSUREARRAY, NULL)
|
||||
|
||||
#define PyArray_ContiguousFromObject(op, type, min_depth, max_depth) \
|
||||
PyArray_FromAny(op, PyArray_DescrFromType(type), min_depth, \
|
||||
max_depth, NPY_ARRAY_DEFAULT | \
|
||||
NPY_ARRAY_ENSUREARRAY, NULL)
|
||||
|
||||
#define PyArray_CopyFromObject(op, type, min_depth, max_depth) \
|
||||
PyArray_FromAny(op, PyArray_DescrFromType(type), min_depth, \
|
||||
max_depth, NPY_ARRAY_ENSURECOPY | \
|
||||
NPY_ARRAY_DEFAULT | \
|
||||
NPY_ARRAY_ENSUREARRAY, NULL)
|
||||
|
||||
#define PyArray_Cast(mp, type_num) \
|
||||
PyArray_CastToType(mp, PyArray_DescrFromType(type_num), 0)
|
||||
|
||||
#define PyArray_Take(ap, items, axis) \
|
||||
PyArray_TakeFrom(ap, items, axis, NULL, NPY_RAISE)
|
||||
|
||||
#define PyArray_Put(ap, items, values) \
|
||||
PyArray_PutTo(ap, items, values, NPY_RAISE)
|
||||
|
||||
|
||||
/*
|
||||
Check to see if this key in the dictionary is the "title"
|
||||
entry of the tuple (i.e. a duplicate dictionary entry in the fields
|
||||
dict).
|
||||
*/
|
||||
|
||||
static inline int
|
||||
NPY_TITLE_KEY_check(PyObject *key, PyObject *value)
|
||||
{
|
||||
PyObject *title;
|
||||
if (PyTuple_Size(value) != 3) {
|
||||
return 0;
|
||||
}
|
||||
title = PyTuple_GetItem(value, 2);
|
||||
if (key == title) {
|
||||
return 1;
|
||||
}
|
||||
#ifdef PYPY_VERSION
|
||||
/*
|
||||
* On PyPy, dictionary keys do not always preserve object identity.
|
||||
* Fall back to comparison by value.
|
||||
*/
|
||||
if (PyUnicode_Check(title) && PyUnicode_Check(key)) {
|
||||
return PyUnicode_Compare(title, key) == 0 ? 1 : 0;
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Macro, for backward compat with "if NPY_TITLE_KEY(key, value) { ..." */
|
||||
#define NPY_TITLE_KEY(key, value) (NPY_TITLE_KEY_check((key), (value)))
|
||||
|
||||
#define DEPRECATE(msg) PyErr_WarnEx(PyExc_DeprecationWarning,msg,1)
|
||||
#define DEPRECATE_FUTUREWARNING(msg) PyErr_WarnEx(PyExc_FutureWarning,msg,1)
|
||||
|
||||
|
||||
/*
|
||||
* These macros and functions unfortunately require runtime version checks
|
||||
* that are only defined in `npy_2_compat.h`. For that reasons they cannot be
|
||||
* part of `ndarraytypes.h` which tries to be self contained.
|
||||
*/
|
||||
|
||||
static inline npy_intp
|
||||
PyArray_ITEMSIZE(const PyArrayObject *arr)
|
||||
{
|
||||
return PyDataType_ELSIZE(((PyArrayObject_fields *)arr)->descr);
|
||||
}
|
||||
|
||||
#define PyDataType_HASFIELDS(obj) (PyDataType_ISLEGACY((PyArray_Descr*)(obj)) && PyDataType_NAMES((PyArray_Descr*)(obj)) != NULL)
|
||||
#define PyDataType_HASSUBARRAY(dtype) (PyDataType_ISLEGACY(dtype) && PyDataType_SUBARRAY(dtype) != NULL)
|
||||
#define PyDataType_ISUNSIZED(dtype) ((dtype)->elsize == 0 && \
|
||||
!PyDataType_HASFIELDS(dtype))
|
||||
|
||||
#define PyDataType_FLAGCHK(dtype, flag) \
|
||||
((PyDataType_FLAGS(dtype) & (flag)) == (flag))
|
||||
|
||||
#define PyDataType_REFCHK(dtype) \
|
||||
PyDataType_FLAGCHK(dtype, NPY_ITEM_REFCOUNT)
|
||||
|
||||
#define NPY_BEGIN_THREADS_DESCR(dtype) \
|
||||
do {if (!(PyDataType_FLAGCHK((dtype), NPY_NEEDS_PYAPI))) \
|
||||
NPY_BEGIN_THREADS;} while (0);
|
||||
|
||||
#define NPY_END_THREADS_DESCR(dtype) \
|
||||
do {if (!(PyDataType_FLAGCHK((dtype), NPY_NEEDS_PYAPI))) \
|
||||
NPY_END_THREADS; } while (0);
|
||||
|
||||
#if !(defined(NPY_INTERNAL_BUILD) && NPY_INTERNAL_BUILD)
|
||||
/* The internal copy of this is now defined in `dtypemeta.h` */
|
||||
/*
|
||||
* `PyArray_Scalar` is the same as this function but converts will convert
|
||||
* most NumPy types to Python scalars.
|
||||
*/
|
||||
static inline PyObject *
|
||||
PyArray_GETITEM(const PyArrayObject *arr, const char *itemptr)
|
||||
{
|
||||
return PyDataType_GetArrFuncs(((PyArrayObject_fields *)arr)->descr)->getitem(
|
||||
(void *)itemptr, (PyArrayObject *)arr);
|
||||
}
|
||||
|
||||
/*
|
||||
* SETITEM should only be used if it is known that the value is a scalar
|
||||
* and of a type understood by the arrays dtype.
|
||||
* Use `PyArray_Pack` if the value may be of a different dtype.
|
||||
*/
|
||||
static inline int
|
||||
PyArray_SETITEM(PyArrayObject *arr, char *itemptr, PyObject *v)
|
||||
{
|
||||
return PyDataType_GetArrFuncs(((PyArrayObject_fields *)arr)->descr)->setitem(v, itemptr, arr);
|
||||
}
|
||||
#endif /* not internal */
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* NUMPY_CORE_INCLUDE_NUMPY_NDARRAYOBJECT_H_ */
|
||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,112 @@
|
||||
#ifndef NPY_DEPRECATED_INCLUDES
|
||||
#error "Should never include npy_*_*_deprecated_api directly."
|
||||
#endif
|
||||
|
||||
#ifndef NUMPY_CORE_INCLUDE_NUMPY_NPY_1_7_DEPRECATED_API_H_
|
||||
#define NUMPY_CORE_INCLUDE_NUMPY_NPY_1_7_DEPRECATED_API_H_
|
||||
|
||||
/* Emit a warning if the user did not specifically request the old API */
|
||||
#ifndef NPY_NO_DEPRECATED_API
|
||||
#if defined(_WIN32)
|
||||
#define _WARN___STR2__(x) #x
|
||||
#define _WARN___STR1__(x) _WARN___STR2__(x)
|
||||
#define _WARN___LOC__ __FILE__ "(" _WARN___STR1__(__LINE__) ") : Warning Msg: "
|
||||
#pragma message(_WARN___LOC__"Using deprecated NumPy API, disable it with " \
|
||||
"#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION")
|
||||
#else
|
||||
#warning "Using deprecated NumPy API, disable it with " \
|
||||
"#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/*
|
||||
* This header exists to collect all dangerous/deprecated NumPy API
|
||||
* as of NumPy 1.7.
|
||||
*
|
||||
* This is an attempt to remove bad API, the proliferation of macros,
|
||||
* and namespace pollution currently produced by the NumPy headers.
|
||||
*/
|
||||
|
||||
/* These array flags are deprecated as of NumPy 1.7 */
|
||||
#define NPY_CONTIGUOUS NPY_ARRAY_C_CONTIGUOUS
|
||||
#define NPY_FORTRAN NPY_ARRAY_F_CONTIGUOUS
|
||||
|
||||
/*
|
||||
* The consistent NPY_ARRAY_* names which don't pollute the NPY_*
|
||||
* namespace were added in NumPy 1.7.
|
||||
*
|
||||
* These versions of the carray flags are deprecated, but
|
||||
* probably should only be removed after two releases instead of one.
|
||||
*/
|
||||
#define NPY_C_CONTIGUOUS NPY_ARRAY_C_CONTIGUOUS
|
||||
#define NPY_F_CONTIGUOUS NPY_ARRAY_F_CONTIGUOUS
|
||||
#define NPY_OWNDATA NPY_ARRAY_OWNDATA
|
||||
#define NPY_FORCECAST NPY_ARRAY_FORCECAST
|
||||
#define NPY_ENSURECOPY NPY_ARRAY_ENSURECOPY
|
||||
#define NPY_ENSUREARRAY NPY_ARRAY_ENSUREARRAY
|
||||
#define NPY_ELEMENTSTRIDES NPY_ARRAY_ELEMENTSTRIDES
|
||||
#define NPY_ALIGNED NPY_ARRAY_ALIGNED
|
||||
#define NPY_NOTSWAPPED NPY_ARRAY_NOTSWAPPED
|
||||
#define NPY_WRITEABLE NPY_ARRAY_WRITEABLE
|
||||
#define NPY_BEHAVED NPY_ARRAY_BEHAVED
|
||||
#define NPY_BEHAVED_NS NPY_ARRAY_BEHAVED_NS
|
||||
#define NPY_CARRAY NPY_ARRAY_CARRAY
|
||||
#define NPY_CARRAY_RO NPY_ARRAY_CARRAY_RO
|
||||
#define NPY_FARRAY NPY_ARRAY_FARRAY
|
||||
#define NPY_FARRAY_RO NPY_ARRAY_FARRAY_RO
|
||||
#define NPY_DEFAULT NPY_ARRAY_DEFAULT
|
||||
#define NPY_IN_ARRAY NPY_ARRAY_IN_ARRAY
|
||||
#define NPY_OUT_ARRAY NPY_ARRAY_OUT_ARRAY
|
||||
#define NPY_INOUT_ARRAY NPY_ARRAY_INOUT_ARRAY
|
||||
#define NPY_IN_FARRAY NPY_ARRAY_IN_FARRAY
|
||||
#define NPY_OUT_FARRAY NPY_ARRAY_OUT_FARRAY
|
||||
#define NPY_INOUT_FARRAY NPY_ARRAY_INOUT_FARRAY
|
||||
#define NPY_UPDATE_ALL NPY_ARRAY_UPDATE_ALL
|
||||
|
||||
/* This way of accessing the default type is deprecated as of NumPy 1.7 */
|
||||
#define PyArray_DEFAULT NPY_DEFAULT_TYPE
|
||||
|
||||
/*
|
||||
* Deprecated as of NumPy 1.7, this kind of shortcut doesn't
|
||||
* belong in the public API.
|
||||
*/
|
||||
#define NPY_AO PyArrayObject
|
||||
|
||||
/*
|
||||
* Deprecated as of NumPy 1.7, an all-lowercase macro doesn't
|
||||
* belong in the public API.
|
||||
*/
|
||||
#define fortran fortran_
|
||||
|
||||
/*
|
||||
* Deprecated as of NumPy 1.7, as it is a namespace-polluting
|
||||
* macro.
|
||||
*/
|
||||
#define FORTRAN_IF PyArray_FORTRAN_IF
|
||||
|
||||
/* Deprecated as of NumPy 1.7, datetime64 uses c_metadata instead */
|
||||
#define NPY_METADATA_DTSTR "__timeunit__"
|
||||
|
||||
/*
|
||||
* Deprecated as of NumPy 1.7.
|
||||
* The reasoning:
|
||||
* - These are for datetime, but there's no datetime "namespace".
|
||||
* - They just turn NPY_STR_<x> into "<x>", which is just
|
||||
* making something simple be indirected.
|
||||
*/
|
||||
#define NPY_STR_Y "Y"
|
||||
#define NPY_STR_M "M"
|
||||
#define NPY_STR_W "W"
|
||||
#define NPY_STR_D "D"
|
||||
#define NPY_STR_h "h"
|
||||
#define NPY_STR_m "m"
|
||||
#define NPY_STR_s "s"
|
||||
#define NPY_STR_ms "ms"
|
||||
#define NPY_STR_us "us"
|
||||
#define NPY_STR_ns "ns"
|
||||
#define NPY_STR_ps "ps"
|
||||
#define NPY_STR_fs "fs"
|
||||
#define NPY_STR_as "as"
|
||||
|
||||
|
||||
#endif /* NUMPY_CORE_INCLUDE_NUMPY_NPY_1_7_DEPRECATED_API_H_ */
|
||||
@ -0,0 +1,249 @@
|
||||
/*
|
||||
* This header file defines relevant features which:
|
||||
* - Require runtime inspection depending on the NumPy version.
|
||||
* - May be needed when compiling with an older version of NumPy to allow
|
||||
* a smooth transition.
|
||||
*
|
||||
* As such, it is shipped with NumPy 2.0, but designed to be vendored in full
|
||||
* or parts by downstream projects.
|
||||
*
|
||||
* It must be included after any other includes. `import_array()` must have
|
||||
* been called in the scope or version dependency will misbehave, even when
|
||||
* only `PyUFunc_` API is used.
|
||||
*
|
||||
* If required complicated defs (with inline functions) should be written as:
|
||||
*
|
||||
* #if NPY_FEATURE_VERSION >= NPY_2_0_API_VERSION
|
||||
* Simple definition when NumPy 2.0 API is guaranteed.
|
||||
* #else
|
||||
* static inline definition of a 1.x compatibility shim
|
||||
* #if NPY_ABI_VERSION < 0x02000000
|
||||
* Make 1.x compatibility shim the public API (1.x only branch)
|
||||
* #else
|
||||
* Runtime dispatched version (1.x or 2.x)
|
||||
* #endif
|
||||
* #endif
|
||||
*
|
||||
* An internal build always passes NPY_FEATURE_VERSION >= NPY_2_0_API_VERSION
|
||||
*/
|
||||
|
||||
#ifndef NUMPY_CORE_INCLUDE_NUMPY_NPY_2_COMPAT_H_
|
||||
#define NUMPY_CORE_INCLUDE_NUMPY_NPY_2_COMPAT_H_
|
||||
|
||||
/*
|
||||
* New macros for accessing real and complex part of a complex number can be
|
||||
* found in "npy_2_complexcompat.h".
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* This header is meant to be included by downstream directly for 1.x compat.
|
||||
* In that case we need to ensure that users first included the full headers
|
||||
* and not just `ndarraytypes.h`.
|
||||
*/
|
||||
|
||||
#ifndef NPY_FEATURE_VERSION
|
||||
#error "The NumPy 2 compat header requires `import_array()` for which " \
|
||||
"the `ndarraytypes.h` header include is not sufficient. Please " \
|
||||
"include it after `numpy/ndarrayobject.h` or similar.\n" \
|
||||
"To simplify inclusion, you may use `PyArray_ImportNumPy()` " \
|
||||
"which is defined in the compat header and is lightweight (can be)."
|
||||
#endif
|
||||
|
||||
#if NPY_ABI_VERSION < 0x02000000
|
||||
/*
|
||||
* Define 2.0 feature version as it is needed below to decide whether we
|
||||
* compile for both 1.x and 2.x (defining it gaurantees 1.x only).
|
||||
*/
|
||||
#define NPY_2_0_API_VERSION 0x00000012
|
||||
/*
|
||||
* If we are compiling with NumPy 1.x, PyArray_RUNTIME_VERSION so we
|
||||
* pretend the `PyArray_RUNTIME_VERSION` is `NPY_FEATURE_VERSION`.
|
||||
* This allows downstream to use `PyArray_RUNTIME_VERSION` if they need to.
|
||||
*/
|
||||
#define PyArray_RUNTIME_VERSION NPY_FEATURE_VERSION
|
||||
/* Compiling on NumPy 1.x where these are the same: */
|
||||
#define PyArray_DescrProto PyArray_Descr
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
* Define a better way to call `_import_array()` to simplify backporting as
|
||||
* we now require imports more often (necessary to make ABI flexible).
|
||||
*/
|
||||
#ifdef import_array1
|
||||
|
||||
static inline int
|
||||
PyArray_ImportNumPyAPI(void)
|
||||
{
|
||||
if (NPY_UNLIKELY(PyArray_API == NULL)) {
|
||||
import_array1(-1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* import_array1 */
|
||||
|
||||
|
||||
/*
|
||||
* NPY_DEFAULT_INT
|
||||
*
|
||||
* The default integer has changed, `NPY_DEFAULT_INT` is available at runtime
|
||||
* for use as type number, e.g. `PyArray_DescrFromType(NPY_DEFAULT_INT)`.
|
||||
*
|
||||
* NPY_RAVEL_AXIS
|
||||
*
|
||||
* This was introduced in NumPy 2.0 to allow indicating that an axis should be
|
||||
* raveled in an operation. Before NumPy 2.0, NPY_MAXDIMS was used for this purpose.
|
||||
*
|
||||
* NPY_MAXDIMS
|
||||
*
|
||||
* A constant indicating the maximum number dimensions allowed when creating
|
||||
* an ndarray.
|
||||
*
|
||||
* NPY_NTYPES_LEGACY
|
||||
*
|
||||
* The number of built-in NumPy dtypes.
|
||||
*/
|
||||
#if NPY_FEATURE_VERSION >= NPY_2_0_API_VERSION
|
||||
#define NPY_DEFAULT_INT NPY_INTP
|
||||
#define NPY_RAVEL_AXIS NPY_MIN_INT
|
||||
#define NPY_MAXARGS 64
|
||||
|
||||
#elif NPY_ABI_VERSION < 0x02000000
|
||||
#define NPY_DEFAULT_INT NPY_LONG
|
||||
#define NPY_RAVEL_AXIS 32
|
||||
#define NPY_MAXARGS 32
|
||||
|
||||
/* Aliases of 2.x names to 1.x only equivalent names */
|
||||
#define NPY_NTYPES NPY_NTYPES_LEGACY
|
||||
#define PyArray_DescrProto PyArray_Descr
|
||||
#define _PyArray_LegacyDescr PyArray_Descr
|
||||
/* NumPy 2 definition always works, but add it for 1.x only */
|
||||
#define PyDataType_ISLEGACY(dtype) (1)
|
||||
#else
|
||||
#define NPY_DEFAULT_INT \
|
||||
(PyArray_RUNTIME_VERSION >= NPY_2_0_API_VERSION ? NPY_INTP : NPY_LONG)
|
||||
#define NPY_RAVEL_AXIS \
|
||||
(PyArray_RUNTIME_VERSION >= NPY_2_0_API_VERSION ? NPY_MIN_INT : 32)
|
||||
#define NPY_MAXARGS \
|
||||
(PyArray_RUNTIME_VERSION >= NPY_2_0_API_VERSION ? 64 : 32)
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
* Access inline functions for descriptor fields. Except for the first
|
||||
* few fields, these needed to be moved (elsize, alignment) for
|
||||
* additional space. Or they are descriptor specific and are not generally
|
||||
* available anymore (metadata, c_metadata, subarray, names, fields).
|
||||
*
|
||||
* Most of these are defined via the `DESCR_ACCESSOR` macro helper.
|
||||
*/
|
||||
#if NPY_FEATURE_VERSION >= NPY_2_0_API_VERSION || NPY_ABI_VERSION < 0x02000000
|
||||
/* Compiling for 1.x or 2.x only, direct field access is OK: */
|
||||
|
||||
static inline void
|
||||
PyDataType_SET_ELSIZE(PyArray_Descr *dtype, npy_intp size)
|
||||
{
|
||||
dtype->elsize = size;
|
||||
}
|
||||
|
||||
static inline npy_uint64
|
||||
PyDataType_FLAGS(const PyArray_Descr *dtype)
|
||||
{
|
||||
#if NPY_FEATURE_VERSION >= NPY_2_0_API_VERSION
|
||||
return dtype->flags;
|
||||
#else
|
||||
return (unsigned char)dtype->flags; /* Need unsigned cast on 1.x */
|
||||
#endif
|
||||
}
|
||||
|
||||
#define DESCR_ACCESSOR(FIELD, field, type, legacy_only) \
|
||||
static inline type \
|
||||
PyDataType_##FIELD(const PyArray_Descr *dtype) { \
|
||||
if (legacy_only && !PyDataType_ISLEGACY(dtype)) { \
|
||||
return (type)0; \
|
||||
} \
|
||||
return ((_PyArray_LegacyDescr *)dtype)->field; \
|
||||
}
|
||||
#else /* compiling for both 1.x and 2.x */
|
||||
|
||||
static inline void
|
||||
PyDataType_SET_ELSIZE(PyArray_Descr *dtype, npy_intp size)
|
||||
{
|
||||
if (PyArray_RUNTIME_VERSION >= NPY_2_0_API_VERSION) {
|
||||
((_PyArray_DescrNumPy2 *)dtype)->elsize = size;
|
||||
}
|
||||
else {
|
||||
((PyArray_DescrProto *)dtype)->elsize = (int)size;
|
||||
}
|
||||
}
|
||||
|
||||
static inline npy_uint64
|
||||
PyDataType_FLAGS(const PyArray_Descr *dtype)
|
||||
{
|
||||
if (PyArray_RUNTIME_VERSION >= NPY_2_0_API_VERSION) {
|
||||
return ((_PyArray_DescrNumPy2 *)dtype)->flags;
|
||||
}
|
||||
else {
|
||||
return (unsigned char)((PyArray_DescrProto *)dtype)->flags;
|
||||
}
|
||||
}
|
||||
|
||||
/* Cast to LegacyDescr always fine but needed when `legacy_only` */
|
||||
#define DESCR_ACCESSOR(FIELD, field, type, legacy_only) \
|
||||
static inline type \
|
||||
PyDataType_##FIELD(const PyArray_Descr *dtype) { \
|
||||
if (legacy_only && !PyDataType_ISLEGACY(dtype)) { \
|
||||
return (type)0; \
|
||||
} \
|
||||
if (PyArray_RUNTIME_VERSION >= NPY_2_0_API_VERSION) { \
|
||||
return ((_PyArray_LegacyDescr *)dtype)->field; \
|
||||
} \
|
||||
else { \
|
||||
return ((PyArray_DescrProto *)dtype)->field; \
|
||||
} \
|
||||
}
|
||||
#endif
|
||||
|
||||
DESCR_ACCESSOR(ELSIZE, elsize, npy_intp, 0)
|
||||
DESCR_ACCESSOR(ALIGNMENT, alignment, npy_intp, 0)
|
||||
DESCR_ACCESSOR(METADATA, metadata, PyObject *, 1)
|
||||
DESCR_ACCESSOR(SUBARRAY, subarray, PyArray_ArrayDescr *, 1)
|
||||
DESCR_ACCESSOR(NAMES, names, PyObject *, 1)
|
||||
DESCR_ACCESSOR(FIELDS, fields, PyObject *, 1)
|
||||
DESCR_ACCESSOR(C_METADATA, c_metadata, NpyAuxData *, 1)
|
||||
|
||||
#undef DESCR_ACCESSOR
|
||||
|
||||
|
||||
#if !(defined(NPY_INTERNAL_BUILD) && NPY_INTERNAL_BUILD)
|
||||
#if NPY_FEATURE_VERSION >= NPY_2_0_API_VERSION
|
||||
static inline PyArray_ArrFuncs *
|
||||
PyDataType_GetArrFuncs(const PyArray_Descr *descr)
|
||||
{
|
||||
return _PyDataType_GetArrFuncs(descr);
|
||||
}
|
||||
#elif NPY_ABI_VERSION < 0x02000000
|
||||
static inline PyArray_ArrFuncs *
|
||||
PyDataType_GetArrFuncs(const PyArray_Descr *descr)
|
||||
{
|
||||
return descr->f;
|
||||
}
|
||||
#else
|
||||
static inline PyArray_ArrFuncs *
|
||||
PyDataType_GetArrFuncs(const PyArray_Descr *descr)
|
||||
{
|
||||
if (PyArray_RUNTIME_VERSION >= NPY_2_0_API_VERSION) {
|
||||
return _PyDataType_GetArrFuncs(descr);
|
||||
}
|
||||
else {
|
||||
return ((PyArray_DescrProto *)descr)->f;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* not internal build */
|
||||
|
||||
#endif /* NUMPY_CORE_INCLUDE_NUMPY_NPY_2_COMPAT_H_ */
|
||||
@ -0,0 +1,28 @@
|
||||
/* This header is designed to be copy-pasted into downstream packages, since it provides
|
||||
a compatibility layer between the old C struct complex types and the new native C99
|
||||
complex types. The new macros are in numpy/npy_math.h, which is why it is included here. */
|
||||
#ifndef NUMPY_CORE_INCLUDE_NUMPY_NPY_2_COMPLEXCOMPAT_H_
|
||||
#define NUMPY_CORE_INCLUDE_NUMPY_NPY_2_COMPLEXCOMPAT_H_
|
||||
|
||||
#include <numpy/npy_math.h>
|
||||
|
||||
#ifndef NPY_CSETREALF
|
||||
#define NPY_CSETREALF(c, r) (c)->real = (r)
|
||||
#endif
|
||||
#ifndef NPY_CSETIMAGF
|
||||
#define NPY_CSETIMAGF(c, i) (c)->imag = (i)
|
||||
#endif
|
||||
#ifndef NPY_CSETREAL
|
||||
#define NPY_CSETREAL(c, r) (c)->real = (r)
|
||||
#endif
|
||||
#ifndef NPY_CSETIMAG
|
||||
#define NPY_CSETIMAG(c, i) (c)->imag = (i)
|
||||
#endif
|
||||
#ifndef NPY_CSETREALL
|
||||
#define NPY_CSETREALL(c, r) (c)->real = (r)
|
||||
#endif
|
||||
#ifndef NPY_CSETIMAGL
|
||||
#define NPY_CSETIMAGL(c, i) (c)->imag = (i)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@ -0,0 +1,595 @@
|
||||
/*
|
||||
* This is a convenience header file providing compatibility utilities
|
||||
* for supporting different minor versions of Python 3.
|
||||
* It was originally used to support the transition from Python 2,
|
||||
* hence the "3k" naming.
|
||||
*
|
||||
* If you want to use this for your own projects, it's recommended to make a
|
||||
* copy of it. Although the stuff below is unlikely to change, we don't provide
|
||||
* strong backwards compatibility guarantees at the moment.
|
||||
*/
|
||||
|
||||
#ifndef NUMPY_CORE_INCLUDE_NUMPY_NPY_3KCOMPAT_H_
|
||||
#define NUMPY_CORE_INCLUDE_NUMPY_NPY_3KCOMPAT_H_
|
||||
|
||||
#include <Python.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifndef NPY_PY3K
|
||||
#define NPY_PY3K 1
|
||||
#endif
|
||||
|
||||
#include "numpy/npy_common.h"
|
||||
#include "numpy/ndarrayobject.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
* PyInt -> PyLong
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* This is a renamed copy of the Python non-limited API function _PyLong_AsInt. It is
|
||||
* included here because it is missing from the PyPy API. It completes the PyLong_As*
|
||||
* group of functions and can be useful in replacing PyInt_Check.
|
||||
*/
|
||||
static inline int
|
||||
Npy__PyLong_AsInt(PyObject *obj)
|
||||
{
|
||||
int overflow;
|
||||
long result = PyLong_AsLongAndOverflow(obj, &overflow);
|
||||
|
||||
/* INT_MAX and INT_MIN are defined in Python.h */
|
||||
if (overflow || result > INT_MAX || result < INT_MIN) {
|
||||
/* XXX: could be cute and give a different
|
||||
message for overflow == -1 */
|
||||
PyErr_SetString(PyExc_OverflowError,
|
||||
"Python int too large to convert to C int");
|
||||
return -1;
|
||||
}
|
||||
return (int)result;
|
||||
}
|
||||
|
||||
|
||||
#if defined(NPY_PY3K)
|
||||
/* Return True only if the long fits in a C long */
|
||||
static inline int PyInt_Check(PyObject *op) {
|
||||
int overflow = 0;
|
||||
if (!PyLong_Check(op)) {
|
||||
return 0;
|
||||
}
|
||||
PyLong_AsLongAndOverflow(op, &overflow);
|
||||
return (overflow == 0);
|
||||
}
|
||||
|
||||
|
||||
#define PyInt_FromLong PyLong_FromLong
|
||||
#define PyInt_AsLong PyLong_AsLong
|
||||
#define PyInt_AS_LONG PyLong_AsLong
|
||||
#define PyInt_AsSsize_t PyLong_AsSsize_t
|
||||
#define PyNumber_Int PyNumber_Long
|
||||
|
||||
/* NOTE:
|
||||
*
|
||||
* Since the PyLong type is very different from the fixed-range PyInt,
|
||||
* we don't define PyInt_Type -> PyLong_Type.
|
||||
*/
|
||||
#endif /* NPY_PY3K */
|
||||
|
||||
/* Py3 changes PySlice_GetIndicesEx' first argument's type to PyObject* */
|
||||
#ifdef NPY_PY3K
|
||||
# define NpySlice_GetIndicesEx PySlice_GetIndicesEx
|
||||
#else
|
||||
# define NpySlice_GetIndicesEx(op, nop, start, end, step, slicelength) \
|
||||
PySlice_GetIndicesEx((PySliceObject *)op, nop, start, end, step, slicelength)
|
||||
#endif
|
||||
|
||||
#if PY_VERSION_HEX < 0x030900a4
|
||||
/* Introduced in https://github.com/python/cpython/commit/d2ec81a8c99796b51fb8c49b77a7fe369863226f */
|
||||
#define Py_SET_TYPE(obj, type) ((Py_TYPE(obj) = (type)), (void)0)
|
||||
/* Introduced in https://github.com/python/cpython/commit/b10dc3e7a11fcdb97e285882eba6da92594f90f9 */
|
||||
#define Py_SET_SIZE(obj, size) ((Py_SIZE(obj) = (size)), (void)0)
|
||||
/* Introduced in https://github.com/python/cpython/commit/c86a11221df7e37da389f9c6ce6e47ea22dc44ff */
|
||||
#define Py_SET_REFCNT(obj, refcnt) ((Py_REFCNT(obj) = (refcnt)), (void)0)
|
||||
#endif
|
||||
|
||||
|
||||
#define Npy_EnterRecursiveCall(x) Py_EnterRecursiveCall(x)
|
||||
|
||||
/*
|
||||
* PyString -> PyBytes
|
||||
*/
|
||||
|
||||
#if defined(NPY_PY3K)
|
||||
|
||||
#define PyString_Type PyBytes_Type
|
||||
#define PyString_Check PyBytes_Check
|
||||
#define PyStringObject PyBytesObject
|
||||
#define PyString_FromString PyBytes_FromString
|
||||
#define PyString_FromStringAndSize PyBytes_FromStringAndSize
|
||||
#define PyString_AS_STRING PyBytes_AS_STRING
|
||||
#define PyString_AsStringAndSize PyBytes_AsStringAndSize
|
||||
#define PyString_FromFormat PyBytes_FromFormat
|
||||
#define PyString_Concat PyBytes_Concat
|
||||
#define PyString_ConcatAndDel PyBytes_ConcatAndDel
|
||||
#define PyString_AsString PyBytes_AsString
|
||||
#define PyString_GET_SIZE PyBytes_GET_SIZE
|
||||
#define PyString_Size PyBytes_Size
|
||||
|
||||
#define PyUString_Type PyUnicode_Type
|
||||
#define PyUString_Check PyUnicode_Check
|
||||
#define PyUStringObject PyUnicodeObject
|
||||
#define PyUString_FromString PyUnicode_FromString
|
||||
#define PyUString_FromStringAndSize PyUnicode_FromStringAndSize
|
||||
#define PyUString_FromFormat PyUnicode_FromFormat
|
||||
#define PyUString_Concat PyUnicode_Concat2
|
||||
#define PyUString_ConcatAndDel PyUnicode_ConcatAndDel
|
||||
#define PyUString_GET_SIZE PyUnicode_GET_SIZE
|
||||
#define PyUString_Size PyUnicode_Size
|
||||
#define PyUString_InternFromString PyUnicode_InternFromString
|
||||
#define PyUString_Format PyUnicode_Format
|
||||
|
||||
#define PyBaseString_Check(obj) (PyUnicode_Check(obj))
|
||||
|
||||
#else
|
||||
|
||||
#define PyBytes_Type PyString_Type
|
||||
#define PyBytes_Check PyString_Check
|
||||
#define PyBytesObject PyStringObject
|
||||
#define PyBytes_FromString PyString_FromString
|
||||
#define PyBytes_FromStringAndSize PyString_FromStringAndSize
|
||||
#define PyBytes_AS_STRING PyString_AS_STRING
|
||||
#define PyBytes_AsStringAndSize PyString_AsStringAndSize
|
||||
#define PyBytes_FromFormat PyString_FromFormat
|
||||
#define PyBytes_Concat PyString_Concat
|
||||
#define PyBytes_ConcatAndDel PyString_ConcatAndDel
|
||||
#define PyBytes_AsString PyString_AsString
|
||||
#define PyBytes_GET_SIZE PyString_GET_SIZE
|
||||
#define PyBytes_Size PyString_Size
|
||||
|
||||
#define PyUString_Type PyString_Type
|
||||
#define PyUString_Check PyString_Check
|
||||
#define PyUStringObject PyStringObject
|
||||
#define PyUString_FromString PyString_FromString
|
||||
#define PyUString_FromStringAndSize PyString_FromStringAndSize
|
||||
#define PyUString_FromFormat PyString_FromFormat
|
||||
#define PyUString_Concat PyString_Concat
|
||||
#define PyUString_ConcatAndDel PyString_ConcatAndDel
|
||||
#define PyUString_GET_SIZE PyString_GET_SIZE
|
||||
#define PyUString_Size PyString_Size
|
||||
#define PyUString_InternFromString PyString_InternFromString
|
||||
#define PyUString_Format PyString_Format
|
||||
|
||||
#define PyBaseString_Check(obj) (PyBytes_Check(obj) || PyUnicode_Check(obj))
|
||||
|
||||
#endif /* NPY_PY3K */
|
||||
|
||||
/*
|
||||
* Macros to protect CRT calls against instant termination when passed an
|
||||
* invalid parameter (https://bugs.python.org/issue23524).
|
||||
*/
|
||||
#if defined _MSC_VER && _MSC_VER >= 1900
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
extern _invalid_parameter_handler _Py_silent_invalid_parameter_handler;
|
||||
#define NPY_BEGIN_SUPPRESS_IPH { _invalid_parameter_handler _Py_old_handler = \
|
||||
_set_thread_local_invalid_parameter_handler(_Py_silent_invalid_parameter_handler);
|
||||
#define NPY_END_SUPPRESS_IPH _set_thread_local_invalid_parameter_handler(_Py_old_handler); }
|
||||
|
||||
#else
|
||||
|
||||
#define NPY_BEGIN_SUPPRESS_IPH
|
||||
#define NPY_END_SUPPRESS_IPH
|
||||
|
||||
#endif /* _MSC_VER >= 1900 */
|
||||
|
||||
|
||||
static inline void
|
||||
PyUnicode_ConcatAndDel(PyObject **left, PyObject *right)
|
||||
{
|
||||
Py_SETREF(*left, PyUnicode_Concat(*left, right));
|
||||
Py_DECREF(right);
|
||||
}
|
||||
|
||||
static inline void
|
||||
PyUnicode_Concat2(PyObject **left, PyObject *right)
|
||||
{
|
||||
Py_SETREF(*left, PyUnicode_Concat(*left, right));
|
||||
}
|
||||
|
||||
/*
|
||||
* PyFile_* compatibility
|
||||
*/
|
||||
|
||||
/*
|
||||
* Get a FILE* handle to the file represented by the Python object
|
||||
*/
|
||||
static inline FILE*
|
||||
npy_PyFile_Dup2(PyObject *file, char *mode, npy_off_t *orig_pos)
|
||||
{
|
||||
int fd, fd2, unbuf;
|
||||
Py_ssize_t fd2_tmp;
|
||||
PyObject *ret, *os, *io, *io_raw;
|
||||
npy_off_t pos;
|
||||
FILE *handle;
|
||||
|
||||
/* For Python 2 PyFileObject, use PyFile_AsFile */
|
||||
#if !defined(NPY_PY3K)
|
||||
if (PyFile_Check(file)) {
|
||||
return PyFile_AsFile(file);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Flush first to ensure things end up in the file in the correct order */
|
||||
ret = PyObject_CallMethod(file, "flush", "");
|
||||
if (ret == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
Py_DECREF(ret);
|
||||
fd = PyObject_AsFileDescriptor(file);
|
||||
if (fd == -1) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* The handle needs to be dup'd because we have to call fclose
|
||||
* at the end
|
||||
*/
|
||||
os = PyImport_ImportModule("os");
|
||||
if (os == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
ret = PyObject_CallMethod(os, "dup", "i", fd);
|
||||
Py_DECREF(os);
|
||||
if (ret == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
fd2_tmp = PyNumber_AsSsize_t(ret, PyExc_IOError);
|
||||
Py_DECREF(ret);
|
||||
if (fd2_tmp == -1 && PyErr_Occurred()) {
|
||||
return NULL;
|
||||
}
|
||||
if (fd2_tmp < INT_MIN || fd2_tmp > INT_MAX) {
|
||||
PyErr_SetString(PyExc_IOError,
|
||||
"Getting an 'int' from os.dup() failed");
|
||||
return NULL;
|
||||
}
|
||||
fd2 = (int)fd2_tmp;
|
||||
|
||||
/* Convert to FILE* handle */
|
||||
#ifdef _WIN32
|
||||
NPY_BEGIN_SUPPRESS_IPH
|
||||
handle = _fdopen(fd2, mode);
|
||||
NPY_END_SUPPRESS_IPH
|
||||
#else
|
||||
handle = fdopen(fd2, mode);
|
||||
#endif
|
||||
if (handle == NULL) {
|
||||
PyErr_SetString(PyExc_IOError,
|
||||
"Getting a FILE* from a Python file object via "
|
||||
"_fdopen failed. If you built NumPy, you probably "
|
||||
"linked with the wrong debug/release runtime");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Record the original raw file handle position */
|
||||
*orig_pos = npy_ftell(handle);
|
||||
if (*orig_pos == -1) {
|
||||
/* The io module is needed to determine if buffering is used */
|
||||
io = PyImport_ImportModule("io");
|
||||
if (io == NULL) {
|
||||
fclose(handle);
|
||||
return NULL;
|
||||
}
|
||||
/* File object instances of RawIOBase are unbuffered */
|
||||
io_raw = PyObject_GetAttrString(io, "RawIOBase");
|
||||
Py_DECREF(io);
|
||||
if (io_raw == NULL) {
|
||||
fclose(handle);
|
||||
return NULL;
|
||||
}
|
||||
unbuf = PyObject_IsInstance(file, io_raw);
|
||||
Py_DECREF(io_raw);
|
||||
if (unbuf == 1) {
|
||||
/* Succeed if the IO is unbuffered */
|
||||
return handle;
|
||||
}
|
||||
else {
|
||||
PyErr_SetString(PyExc_IOError, "obtaining file position failed");
|
||||
fclose(handle);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* Seek raw handle to the Python-side position */
|
||||
ret = PyObject_CallMethod(file, "tell", "");
|
||||
if (ret == NULL) {
|
||||
fclose(handle);
|
||||
return NULL;
|
||||
}
|
||||
pos = PyLong_AsLongLong(ret);
|
||||
Py_DECREF(ret);
|
||||
if (PyErr_Occurred()) {
|
||||
fclose(handle);
|
||||
return NULL;
|
||||
}
|
||||
if (npy_fseek(handle, pos, SEEK_SET) == -1) {
|
||||
PyErr_SetString(PyExc_IOError, "seeking file failed");
|
||||
fclose(handle);
|
||||
return NULL;
|
||||
}
|
||||
return handle;
|
||||
}
|
||||
|
||||
/*
|
||||
* Close the dup-ed file handle, and seek the Python one to the current position
|
||||
*/
|
||||
static inline int
|
||||
npy_PyFile_DupClose2(PyObject *file, FILE* handle, npy_off_t orig_pos)
|
||||
{
|
||||
int fd, unbuf;
|
||||
PyObject *ret, *io, *io_raw;
|
||||
npy_off_t position;
|
||||
|
||||
/* For Python 2 PyFileObject, do nothing */
|
||||
#if !defined(NPY_PY3K)
|
||||
if (PyFile_Check(file)) {
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
position = npy_ftell(handle);
|
||||
|
||||
/* Close the FILE* handle */
|
||||
fclose(handle);
|
||||
|
||||
/*
|
||||
* Restore original file handle position, in order to not confuse
|
||||
* Python-side data structures
|
||||
*/
|
||||
fd = PyObject_AsFileDescriptor(file);
|
||||
if (fd == -1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (npy_lseek(fd, orig_pos, SEEK_SET) == -1) {
|
||||
|
||||
/* The io module is needed to determine if buffering is used */
|
||||
io = PyImport_ImportModule("io");
|
||||
if (io == NULL) {
|
||||
return -1;
|
||||
}
|
||||
/* File object instances of RawIOBase are unbuffered */
|
||||
io_raw = PyObject_GetAttrString(io, "RawIOBase");
|
||||
Py_DECREF(io);
|
||||
if (io_raw == NULL) {
|
||||
return -1;
|
||||
}
|
||||
unbuf = PyObject_IsInstance(file, io_raw);
|
||||
Py_DECREF(io_raw);
|
||||
if (unbuf == 1) {
|
||||
/* Succeed if the IO is unbuffered */
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
PyErr_SetString(PyExc_IOError, "seeking file failed");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (position == -1) {
|
||||
PyErr_SetString(PyExc_IOError, "obtaining file position failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Seek Python-side handle to the FILE* handle position */
|
||||
ret = PyObject_CallMethod(file, "seek", NPY_OFF_T_PYFMT "i", position, 0);
|
||||
if (ret == NULL) {
|
||||
return -1;
|
||||
}
|
||||
Py_DECREF(ret);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int
|
||||
npy_PyFile_Check(PyObject *file)
|
||||
{
|
||||
int fd;
|
||||
/* For Python 2, check if it is a PyFileObject */
|
||||
#if !defined(NPY_PY3K)
|
||||
if (PyFile_Check(file)) {
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
fd = PyObject_AsFileDescriptor(file);
|
||||
if (fd == -1) {
|
||||
PyErr_Clear();
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static inline PyObject*
|
||||
npy_PyFile_OpenFile(PyObject *filename, const char *mode)
|
||||
{
|
||||
PyObject *open;
|
||||
open = PyDict_GetItemString(PyEval_GetBuiltins(), "open");
|
||||
if (open == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
return PyObject_CallFunction(open, "Os", filename, mode);
|
||||
}
|
||||
|
||||
static inline int
|
||||
npy_PyFile_CloseFile(PyObject *file)
|
||||
{
|
||||
PyObject *ret;
|
||||
|
||||
ret = PyObject_CallMethod(file, "close", NULL);
|
||||
if (ret == NULL) {
|
||||
return -1;
|
||||
}
|
||||
Py_DECREF(ret);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* This is a copy of _PyErr_ChainExceptions
|
||||
*/
|
||||
static inline void
|
||||
npy_PyErr_ChainExceptions(PyObject *exc, PyObject *val, PyObject *tb)
|
||||
{
|
||||
if (exc == NULL)
|
||||
return;
|
||||
|
||||
if (PyErr_Occurred()) {
|
||||
/* only py3 supports this anyway */
|
||||
#ifdef NPY_PY3K
|
||||
PyObject *exc2, *val2, *tb2;
|
||||
PyErr_Fetch(&exc2, &val2, &tb2);
|
||||
PyErr_NormalizeException(&exc, &val, &tb);
|
||||
if (tb != NULL) {
|
||||
PyException_SetTraceback(val, tb);
|
||||
Py_DECREF(tb);
|
||||
}
|
||||
Py_DECREF(exc);
|
||||
PyErr_NormalizeException(&exc2, &val2, &tb2);
|
||||
PyException_SetContext(val2, val);
|
||||
PyErr_Restore(exc2, val2, tb2);
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
PyErr_Restore(exc, val, tb);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* This is a copy of _PyErr_ChainExceptions, with:
|
||||
* - a minimal implementation for python 2
|
||||
* - __cause__ used instead of __context__
|
||||
*/
|
||||
static inline void
|
||||
npy_PyErr_ChainExceptionsCause(PyObject *exc, PyObject *val, PyObject *tb)
|
||||
{
|
||||
if (exc == NULL)
|
||||
return;
|
||||
|
||||
if (PyErr_Occurred()) {
|
||||
/* only py3 supports this anyway */
|
||||
#ifdef NPY_PY3K
|
||||
PyObject *exc2, *val2, *tb2;
|
||||
PyErr_Fetch(&exc2, &val2, &tb2);
|
||||
PyErr_NormalizeException(&exc, &val, &tb);
|
||||
if (tb != NULL) {
|
||||
PyException_SetTraceback(val, tb);
|
||||
Py_DECREF(tb);
|
||||
}
|
||||
Py_DECREF(exc);
|
||||
PyErr_NormalizeException(&exc2, &val2, &tb2);
|
||||
PyException_SetCause(val2, val);
|
||||
PyErr_Restore(exc2, val2, tb2);
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
PyErr_Restore(exc, val, tb);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* PyObject_Cmp
|
||||
*/
|
||||
#if defined(NPY_PY3K)
|
||||
static inline int
|
||||
PyObject_Cmp(PyObject *i1, PyObject *i2, int *cmp)
|
||||
{
|
||||
int v;
|
||||
v = PyObject_RichCompareBool(i1, i2, Py_LT);
|
||||
if (v == 1) {
|
||||
*cmp = -1;
|
||||
return 1;
|
||||
}
|
||||
else if (v == -1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
v = PyObject_RichCompareBool(i1, i2, Py_GT);
|
||||
if (v == 1) {
|
||||
*cmp = 1;
|
||||
return 1;
|
||||
}
|
||||
else if (v == -1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
v = PyObject_RichCompareBool(i1, i2, Py_EQ);
|
||||
if (v == 1) {
|
||||
*cmp = 0;
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
*cmp = 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* PyCObject functions adapted to PyCapsules.
|
||||
*
|
||||
* The main job here is to get rid of the improved error handling
|
||||
* of PyCapsules. It's a shame...
|
||||
*/
|
||||
static inline PyObject *
|
||||
NpyCapsule_FromVoidPtr(void *ptr, void (*dtor)(PyObject *))
|
||||
{
|
||||
PyObject *ret = PyCapsule_New(ptr, NULL, dtor);
|
||||
if (ret == NULL) {
|
||||
PyErr_Clear();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline PyObject *
|
||||
NpyCapsule_FromVoidPtrAndDesc(void *ptr, void* context, void (*dtor)(PyObject *))
|
||||
{
|
||||
PyObject *ret = NpyCapsule_FromVoidPtr(ptr, dtor);
|
||||
if (ret != NULL && PyCapsule_SetContext(ret, context) != 0) {
|
||||
PyErr_Clear();
|
||||
Py_DECREF(ret);
|
||||
ret = NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline void *
|
||||
NpyCapsule_AsVoidPtr(PyObject *obj)
|
||||
{
|
||||
void *ret = PyCapsule_GetPointer(obj, NULL);
|
||||
if (ret == NULL) {
|
||||
PyErr_Clear();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline void *
|
||||
NpyCapsule_GetDesc(PyObject *obj)
|
||||
{
|
||||
return PyCapsule_GetContext(obj);
|
||||
}
|
||||
|
||||
static inline int
|
||||
NpyCapsule_Check(PyObject *ptr)
|
||||
{
|
||||
return PyCapsule_CheckExact(ptr);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* NUMPY_CORE_INCLUDE_NUMPY_NPY_3KCOMPAT_H_ */
|
||||
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue