Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
Files changed (50)
- lib/python3.12/site-packages/cloudpickle/__init__.pyAdded+18
- lib/python3.12/site-packages/cloudpickle/__pycache__/__init__.cpython-312.pycAdded
- lib/python3.12/site-packages/cloudpickle/__pycache__/cloudpickle.cpython-312.pycAdded
- lib/python3.12/site-packages/cloudpickle/__pycache__/cloudpickle_fast.cpython-312.pycAdded
- lib/python3.12/site-packages/cloudpickle/cloudpickle.pyAdded+1552
- lib/python3.12/site-packages/cloudpickle/cloudpickle_fast.pyAdded+14
- lib/python3.12/site-packages/ninja-1.13.0.dist-info/INSTALLERAdded+1
- lib/python3.12/site-packages/ninja-1.13.0.dist-info/METADATAAdded+102
- lib/python3.12/site-packages/ninja-1.13.0.dist-info/RECORDAdded+18
- lib/python3.12/site-packages/ninja-1.13.0.dist-info/WHEELAdded+6
- lib/python3.12/site-packages/ninja-1.13.0.dist-info/licenses/AUTHORS.rstAdded+5
- lib/python3.12/site-packages/ninja-1.13.0.dist-info/licenses/LICENSE_Apache_20Added+191
- lib/python3.12/site-packages/numpy/__config__.pyAdded+162
- lib/python3.12/site-packages/numpy/__init__.cython-30.pxdAdded+1050
- lib/python3.12/site-packages/numpy/__init__.pxdAdded+1015
- lib/python3.12/site-packages/numpy/__init__.pyAdded+461
- lib/python3.12/site-packages/numpy/__init__.pyiAdded
- lib/python3.12/site-packages/numpy/_distributor_init.pyAdded+15
- lib/python3.12/site-packages/numpy/_globals.pyAdded+95
- lib/python3.12/site-packages/numpy/_pytesttester.pyAdded+207
- lib/python3.12/site-packages/numpy/_pytesttester.pyiAdded+18
- lib/python3.12/site-packages/numpy/conftest.pyAdded+138
- lib/python3.12/site-packages/numpy/ctypeslib.pyAdded+545
- lib/python3.12/site-packages/numpy/ctypeslib.pyiAdded+251
- lib/python3.12/site-packages/numpy/dtypes.pyAdded+77
- lib/python3.12/site-packages/numpy/dtypes.pyiAdded+43
- lib/python3.12/site-packages/numpy/exceptions.pyAdded+231
- lib/python3.12/site-packages/numpy/exceptions.pyiAdded+18
- lib/python3.12/site-packages/numpy/matlib.pyAdded+378
- lib/python3.12/site-packages/numpy/polynomial/__init__.pyAdded+185
- lib/python3.12/site-packages/numpy/polynomial/__init__.pyiAdded+22
- lib/python3.12/site-packages/numpy/polynomial/_polybase.pyAdded+1206
- lib/python3.12/site-packages/numpy/polynomial/_polybase.pyiAdded+71
- lib/python3.12/site-packages/numpy/polynomial/chebyshev.pyAdded+2082
- lib/python3.12/site-packages/numpy/polynomial/chebyshev.pyiAdded+51
- lib/python3.12/site-packages/numpy/polynomial/hermite.pyAdded+1703
- lib/python3.12/site-packages/numpy/polynomial/hermite.pyiAdded+46
- lib/python3.12/site-packages/numpy/polynomial/hermite_e.pyAdded+1695
- lib/python3.12/site-packages/numpy/polynomial/hermite_e.pyiAdded+46
- lib/python3.12/site-packages/numpy/polynomial/laguerre.pyAdded+1651
- lib/python3.12/site-packages/numpy/polynomial/laguerre.pyiAdded+46
- lib/python3.12/site-packages/numpy/polynomial/legendre.pyAdded+1664
- lib/python3.12/site-packages/numpy/polynomial/legendre.pyiAdded+46
- lib/python3.12/site-packages/numpy/polynomial/polynomial.pyAdded+1542
- lib/python3.12/site-packages/numpy/polynomial/polynomial.pyiAdded+41
- lib/python3.12/site-packages/numpy/polynomial/polyutils.pyAdded+789
- lib/python3.12/site-packages/numpy/polynomial/polyutils.pyiAdded+11
- lib/python3.12/site-packages/numpy/polynomial/setup.pyAdded+10
- lib/python3.12/site-packages/numpy/polynomial/tests/__init__.pyAdded
- lib/python3.12/site-packages/numpy/polynomial/tests/__pycache__/test_classes.cpython-312.pycAdded
123456789101112131415161718from . import cloudpicklefrom .cloudpickle import * # noqa
__doc__ = cloudpickle.__doc__
__version__ = "3.1.2"
__all__ = [ # noqa "__version__", "Pickler", "CloudPickler", "dumps", "loads", "dump", "load", "register_pickle_by_value", "unregister_pickle_by_value",]1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552"""Pickler class to extend the standard pickle.Pickler functionality
The main objective is to make it natural to perform distributed computing onclusters (such as PySpark, Dask, Ray...) with interactively defined code(functions, classes, ...) written in notebooks or console.
In particular this pickler adds the following features:- serialize interactively-defined or locally-defined functions, classes, enums, typevars, lambdas and nested functions to compiled byte code;- deal with some other non-serializable objects in an ad-hoc manner where applicable.
This pickler is therefore meant to be used for the communication between shortlived Python processes running the same version of Python and libraries. Inparticular, it is not meant to be used for long term storage of Python objects.
It does not include an unpickler, as standard Python unpickling suffices.
This module was extracted from the `cloud` package, developed by `PiCloud, Inc.<https://web.archive.org/web/20140626004012/http://www.picloud.com/>`_.
Copyright (c) 2012-now, CloudPickle developers and contributors.Copyright (c) 2012, Regents of the University of California.Copyright (c) 2009 `PiCloud, Inc. <https://web.archive.org/web/20140626004012/http://www.picloud.com/>`_.All rights reserved.
Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditionsare 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 University of California, Berkeley 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 NOTLIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FORA PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHTHOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITEDTO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, ORPROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDINGNEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THISSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."""
import _collections_abcfrom collections import ChainMap, OrderedDictimport abcimport builtinsimport copyregimport dataclassesimport disfrom enum import Enumimport ioimport itertoolsimport loggingimport opcodeimport picklefrom pickle import _getattribute as _pickle_getattributeimport platformimport structimport sysimport threadingimport typesimport typingimport uuidimport warningsimport weakref
# The following import is required to be imported in the cloudpickle# namespace to be able to load pickle files generated with older versions of# cloudpickle. See: tests/test_backward_compat.pyfrom types import CellType # noqa: F401
# cloudpickle is meant for inter process communication: we expect all# communicating processes to run the same Python version hence we favor# communication speed over compatibility:DEFAULT_PROTOCOL = pickle.HIGHEST_PROTOCOL
# Names of modules whose resources should be treated as dynamic._PICKLE_BY_VALUE_MODULES = set()
# Track the provenance of reconstructed dynamic classes to make it possible to# reconstruct instances from the matching singleton class definition when# appropriate and preserve the usual "isinstance" semantics of Python objects._DYNAMIC_CLASS_TRACKER_BY_CLASS = weakref.WeakKeyDictionary()_DYNAMIC_CLASS_TRACKER_BY_ID = weakref.WeakValueDictionary()_DYNAMIC_CLASS_TRACKER_LOCK = threading.Lock()
PYPY = platform.python_implementation() == "PyPy"
builtin_code_type = Noneif PYPY: # builtin-code objects only exist in pypy builtin_code_type = type(float.__new__.__code__)
_extract_code_globals_cache = weakref.WeakKeyDictionary()
def _get_or_create_tracker_id(class_def): with _DYNAMIC_CLASS_TRACKER_LOCK: class_tracker_id = _DYNAMIC_CLASS_TRACKER_BY_CLASS.get(class_def) if class_tracker_id is None: class_tracker_id = uuid.uuid4().hex _DYNAMIC_CLASS_TRACKER_BY_CLASS[class_def] = class_tracker_id _DYNAMIC_CLASS_TRACKER_BY_ID[class_tracker_id] = class_def return class_tracker_id
def _lookup_class_or_track(class_tracker_id, class_def): if class_tracker_id is not None: with _DYNAMIC_CLASS_TRACKER_LOCK: class_def = _DYNAMIC_CLASS_TRACKER_BY_ID.setdefault( class_tracker_id, class_def ) _DYNAMIC_CLASS_TRACKER_BY_CLASS[class_def] = class_tracker_id return class_def
def register_pickle_by_value(module): """Register a module to make its functions and classes picklable by value.
By default, functions and classes that are attributes of an importable module are to be pickled by reference, that is relying on re-importing the attribute from the module at load time.
If `register_pickle_by_value(module)` is called, all its functions and classes are subsequently to be pickled by value, meaning that they can be loaded in Python processes where the module is not importable.
This is especially useful when developing a module in a distributed execution environment: restarting the client Python process with the new source code is enough: there is no need to re-install the new version of the module on all the worker nodes nor to restart the workers.
Note: this feature is considered experimental. See the cloudpickle README.md file for more details and limitations. """ if not isinstance(module, types.ModuleType): raise ValueError(f"Input should be a module object, got {str(module)} instead") # In the future, cloudpickle may need a way to access any module registered # for pickling by value in order to introspect relative imports inside # functions pickled by value. (see # https://github.com/cloudpipe/cloudpickle/pull/417#issuecomment-873684633). # This access can be ensured by checking that module is present in # sys.modules at registering time and assuming that it will still be in # there when accessed during pickling. Another alternative would be to # store a weakref to the module. Even though cloudpickle does not implement # this introspection yet, in order to avoid a possible breaking change # later, we still enforce the presence of module inside sys.modules. if module.__name__ not in sys.modules: raise ValueError( f"{module} was not imported correctly, have you used an " "`import` statement to access it?" ) _PICKLE_BY_VALUE_MODULES.add(module.__name__)
def unregister_pickle_by_value(module): """Unregister that the input module should be pickled by value.""" if not isinstance(module, types.ModuleType): raise ValueError(f"Input should be a module object, got {str(module)} instead") if module.__name__ not in _PICKLE_BY_VALUE_MODULES: raise ValueError(f"{module} is not registered for pickle by value") else: _PICKLE_BY_VALUE_MODULES.remove(module.__name__)
def list_registry_pickle_by_value(): return _PICKLE_BY_VALUE_MODULES.copy()
def _is_registered_pickle_by_value(module): module_name = module.__name__ if module_name in _PICKLE_BY_VALUE_MODULES: return True while True: parent_name = module_name.rsplit(".", 1)[0] if parent_name == module_name: break if parent_name in _PICKLE_BY_VALUE_MODULES: return True module_name = parent_name return False
if sys.version_info >= (3, 14): def _getattribute(obj, name): return _pickle_getattribute(obj, name.split('.'))else: def _getattribute(obj, name): return _pickle_getattribute(obj, name)[0]
def _whichmodule(obj, name): """Find the module an object belongs to.
This function differs from ``pickle.whichmodule`` in two ways: - it does not mangle the cases where obj's module is __main__ and obj was not found in any module. - Errors arising during module introspection are ignored, as those errors are considered unwanted side effects. """ module_name = getattr(obj, "__module__", None)
if module_name is not None: return module_name # Protect the iteration by using a copy of sys.modules against dynamic # modules that trigger imports of other modules upon calls to getattr or # other threads importing at the same time. for module_name, module in sys.modules.copy().items(): # Some modules such as coverage can inject non-module objects inside # sys.modules if ( module_name == "__main__" or module_name == "__mp_main__" or module is None or not isinstance(module, types.ModuleType) ): continue try: if _getattribute(module, name) is obj: return module_name except Exception: pass return None
def _should_pickle_by_reference(obj, name=None): """Test whether an function or a class should be pickled by reference
Pickling by reference means by that the object (typically a function or a class) is an attribute of a module that is assumed to be importable in the target Python environment. Loading will therefore rely on importing the module and then calling `getattr` on it to access the function or class.
Pickling by reference is the only option to pickle functions and classes in the standard library. In cloudpickle the alternative option is to pickle by value (for instance for interactively or locally defined functions and classes or for attributes of modules that have been explicitly registered to be pickled by value. """ if isinstance(obj, types.FunctionType) or issubclass(type(obj), type): module_and_name = _lookup_module_and_qualname(obj, name=name) if module_and_name is None: return False module, name = module_and_name return not _is_registered_pickle_by_value(module)
elif isinstance(obj, types.ModuleType): # We assume that sys.modules is primarily used as a cache mechanism for # the Python import machinery. Checking if a module has been added in # is sys.modules therefore a cheap and simple heuristic to tell us # whether we can assume that a given module could be imported by name # in another Python process. if _is_registered_pickle_by_value(obj): return False return obj.__name__ in sys.modules else: raise TypeError( "cannot check importability of {} instances".format(type(obj).__name__) )
def _lookup_module_and_qualname(obj, name=None): if name is None: name = getattr(obj, "__qualname__", None) if name is None: # pragma: no cover # This used to be needed for Python 2.7 support but is probably not # needed anymore. However we keep the __name__ introspection in case # users of cloudpickle rely on this old behavior for unknown reasons. name = getattr(obj, "__name__", None)
module_name = _whichmodule(obj, name)
if module_name is None: # In this case, obj.__module__ is None AND obj was not found in any # imported module. obj is thus treated as dynamic. return None
if module_name == "__main__": return None
# Note: if module_name is in sys.modules, the corresponding module is # assumed importable at unpickling time. See #357 module = sys.modules.get(module_name, None) if module is None: # The main reason why obj's module would not be imported is that this # module has been dynamically created, using for example # types.ModuleType. The other possibility is that module was removed # from sys.modules after obj was created/imported. But this case is not # supported, as the standard pickle does not support it either. return None
try: obj2 = _getattribute(module, name) except AttributeError: # obj was not found inside the module it points to return None if obj2 is not obj: return None return module, name
def _extract_code_globals(co): """Find all globals names read or written to by codeblock co.""" out_names = _extract_code_globals_cache.get(co) if out_names is None: # We use a dict with None values instead of a set to get a # deterministic order and avoid introducing non-deterministic pickle # bytes as a results. out_names = {name: None for name in _walk_global_ops(co)}
# Declaring a function inside another one using the "def ..." syntax # generates a constant code object corresponding to the one of the # nested function's As the nested function may itself need global # variables, we need to introspect its code, extract its globals, (look # for code object in it's co_consts attribute..) and add the result to # code_globals if co.co_consts: for const in co.co_consts: if isinstance(const, types.CodeType): out_names.update(_extract_code_globals(const))
_extract_code_globals_cache[co] = out_names
return out_names
def _find_imported_submodules(code, top_level_dependencies): """Find currently imported submodules used by a function.
Submodules used by a function need to be detected and referenced for the function to work correctly at depickling time. Because submodules can be referenced as attribute of their parent package (``package.submodule``), we need a special introspection technique that does not rely on GLOBAL-related opcodes to find references of them in a code object.
Example: ``` import concurrent.futures import cloudpickle def func(): x = concurrent.futures.ThreadPoolExecutor if __name__ == '__main__': cloudpickle.dumps(func) ``` The globals extracted by cloudpickle in the function's state include the concurrent package, but not its submodule (here, concurrent.futures), which is the module used by func. Find_imported_submodules will detect the usage of concurrent.futures. Saving this module alongside with func will ensure that calling func once depickled does not fail due to concurrent.futures not being imported """
subimports = [] # check if any known dependency is an imported package for x in top_level_dependencies: if ( isinstance(x, types.ModuleType) and hasattr(x, "__package__") and x.__package__ ): # check if the package has any currently loaded sub-imports prefix = x.__name__ + "." # A concurrent thread could mutate sys.modules, # make sure we iterate over a copy to avoid exceptions for name in list(sys.modules): # Older versions of pytest will add a "None" module to # sys.modules. if name is not None and name.startswith(prefix): # check whether the function can address the sub-module tokens = set(name[len(prefix) :].split(".")) if not tokens - set(code.co_names): subimports.append(sys.modules[name]) return subimports
# relevant opcodesSTORE_GLOBAL = opcode.opmap["STORE_GLOBAL"]DELETE_GLOBAL = opcode.opmap["DELETE_GLOBAL"]LOAD_GLOBAL = opcode.opmap["LOAD_GLOBAL"]GLOBAL_OPS = (STORE_GLOBAL, DELETE_GLOBAL, LOAD_GLOBAL)HAVE_ARGUMENT = dis.HAVE_ARGUMENTEXTENDED_ARG = dis.EXTENDED_ARG
_BUILTIN_TYPE_NAMES = {}for k, v in types.__dict__.items(): if type(v) is type: _BUILTIN_TYPE_NAMES[v] = k
def _builtin_type(name): if name == "ClassType": # pragma: no cover # Backward compat to load pickle files generated with cloudpickle # < 1.3 even if loading pickle files from older versions is not # officially supported. return type return getattr(types, name)
def _walk_global_ops(code): """Yield referenced name for global-referencing instructions in code.""" for instr in dis.get_instructions(code): op = instr.opcode if op in GLOBAL_OPS: yield instr.argval
def _extract_class_dict(cls): """Retrieve a copy of the dict of a class without the inherited method.""" # Hack to circumvent non-predictable memoization caused by string interning. # See the inline comment in _class_setstate for details. clsdict = {"".join(k): cls.__dict__[k] for k in sorted(cls.__dict__)}
if len(cls.__bases__) == 1: inherited_dict = cls.__bases__[0].__dict__ else: inherited_dict = {} for base in reversed(cls.__bases__): inherited_dict.update(base.__dict__) to_remove = [] for name, value in clsdict.items(): try: base_value = inherited_dict[name] if value is base_value: to_remove.append(name) except KeyError: pass for name in to_remove: clsdict.pop(name) return clsdict
def is_tornado_coroutine(func): """Return whether `func` is a Tornado coroutine function.
Running coroutines are not supported. """ warnings.warn( "is_tornado_coroutine is deprecated in cloudpickle 3.0 and will be " "removed in cloudpickle 4.0. Use tornado.gen.is_coroutine_function " "directly instead.", category=DeprecationWarning, ) if "tornado.gen" not in sys.modules: return False gen = sys.modules["tornado.gen"] if not hasattr(gen, "is_coroutine_function"): # Tornado version is too old return False return gen.is_coroutine_function(func)
def subimport(name): # We cannot do simply: `return __import__(name)`: Indeed, if ``name`` is # the name of a submodule, __import__ will return the top-level root module # of this submodule. For instance, __import__('os.path') returns the `os` # module. __import__(name) return sys.modules[name]
def dynamic_subimport(name, vars): mod = types.ModuleType(name) mod.__dict__.update(vars) mod.__dict__["__builtins__"] = builtins.__dict__ return mod
def _get_cell_contents(cell): try: return cell.cell_contents except ValueError: # Handle empty cells explicitly with a sentinel value. return _empty_cell_value
def instance(cls): """Create a new instance of a class.
Parameters ---------- cls : type The class to create an instance of.
Returns ------- instance : cls A new instance of ``cls``. """ return cls()
@instanceclass _empty_cell_value: """Sentinel for empty closures."""
@classmethod def __reduce__(cls): return cls.__name__
def _make_function(code, globals, name, argdefs, closure): # Setting __builtins__ in globals is needed for nogil CPython. globals["__builtins__"] = __builtins__ return types.FunctionType(code, globals, name, argdefs, closure)
def _make_empty_cell(): if False: # trick the compiler into creating an empty cell in our lambda cell = None raise AssertionError("this route should not be executed")
return (lambda: cell).__closure__[0]
def _make_cell(value=_empty_cell_value): cell = _make_empty_cell() if value is not _empty_cell_value: cell.cell_contents = value return cell
def _make_skeleton_class( type_constructor, name, bases, type_kwargs, class_tracker_id, extra): """Build dynamic class with an empty __dict__ to be filled once memoized
If class_tracker_id is not None, try to lookup an existing class definition matching that id. If none is found, track a newly reconstructed class definition under that id so that other instances stemming from the same class id will also reuse this class definition.
The "extra" variable is meant to be a dict (or None) that can be used for forward compatibility shall the need arise. """ # We need to intern the keys of the type_kwargs dict to avoid having # different pickles for the same dynamic class depending on whether it was # dynamically created or reconstructed from a pickled stream. type_kwargs = {sys.intern(k): v for k, v in type_kwargs.items()}
skeleton_class = types.new_class( name, bases, {"metaclass": type_constructor}, lambda ns: ns.update(type_kwargs) )
return _lookup_class_or_track(class_tracker_id, skeleton_class)
def _make_skeleton_enum( bases, name, qualname, members, module, class_tracker_id, extra): """Build dynamic enum with an empty __dict__ to be filled once memoized
The creation of the enum class is inspired by the code of EnumMeta._create_.
If class_tracker_id is not None, try to lookup an existing enum definition matching that id. If none is found, track a newly reconstructed enum definition under that id so that other instances stemming from the same class id will also reuse this enum definition.
The "extra" variable is meant to be a dict (or None) that can be used for forward compatibility shall the need arise. """ # enums always inherit from their base Enum class at the last position in # the list of base classes: enum_base = bases[-1] metacls = enum_base.__class__ classdict = metacls.__prepare__(name, bases)
for member_name, member_value in members.items(): classdict[member_name] = member_value enum_class = metacls.__new__(metacls, name, bases, classdict) enum_class.__module__ = module enum_class.__qualname__ = qualname
return _lookup_class_or_track(class_tracker_id, enum_class)
def _make_typevar(name, bound, constraints, covariant, contravariant, class_tracker_id): tv = typing.TypeVar( name, *constraints, bound=bound, covariant=covariant, contravariant=contravariant, ) return _lookup_class_or_track(class_tracker_id, tv)
def _decompose_typevar(obj): return ( obj.__name__, obj.__bound__, obj.__constraints__, obj.__covariant__, obj.__contravariant__, _get_or_create_tracker_id(obj), )
def _typevar_reduce(obj): # TypeVar instances require the module information hence why we # are not using the _should_pickle_by_reference directly module_and_name = _lookup_module_and_qualname(obj, name=obj.__name__)
if module_and_name is None: return (_make_typevar, _decompose_typevar(obj)) elif _is_registered_pickle_by_value(module_and_name[0]): return (_make_typevar, _decompose_typevar(obj))
return (getattr, module_and_name)
def _get_bases(typ): if "__orig_bases__" in getattr(typ, "__dict__", {}): # For generic types (see PEP 560) # Note that simply checking `hasattr(typ, '__orig_bases__')` is not # correct. Subclasses of a fully-parameterized generic class does not # have `__orig_bases__` defined, but `hasattr(typ, '__orig_bases__')` # will return True because it's defined in the base class. bases_attr = "__orig_bases__" else: # For regular class objects bases_attr = "__bases__" return getattr(typ, bases_attr)
def _make_dict_keys(obj, is_ordered=False): if is_ordered: return OrderedDict.fromkeys(obj).keys() else: return dict.fromkeys(obj).keys()
def _make_dict_values(obj, is_ordered=False): if is_ordered: return OrderedDict((i, _) for i, _ in enumerate(obj)).values() else: return {i: _ for i, _ in enumerate(obj)}.values()
def _make_dict_items(obj, is_ordered=False): if is_ordered: return OrderedDict(obj).items() else: return obj.items()
# COLLECTION OF OBJECTS __getnewargs__-LIKE METHODS# -------------------------------------------------
def _class_getnewargs(obj): type_kwargs = {} if "__module__" in obj.__dict__: type_kwargs["__module__"] = obj.__module__
__dict__ = obj.__dict__.get("__dict__", None) if isinstance(__dict__, property): type_kwargs["__dict__"] = __dict__
return ( type(obj), obj.__name__, _get_bases(obj), type_kwargs, _get_or_create_tracker_id(obj), None, )
def _enum_getnewargs(obj): members = {e.name: e.value for e in obj} return ( obj.__bases__, obj.__name__, obj.__qualname__, members, obj.__module__, _get_or_create_tracker_id(obj), None, )
# COLLECTION OF OBJECTS RECONSTRUCTORS# ------------------------------------def _file_reconstructor(retval): return retval
# COLLECTION OF OBJECTS STATE GETTERS# -----------------------------------
def _function_getstate(func): # - Put func's dynamic attributes (stored in func.__dict__) in state. These # attributes will be restored at unpickling time using # f.__dict__.update(state) # - Put func's members into slotstate. Such attributes will be restored at # unpickling time by iterating over slotstate and calling setattr(func, # slotname, slotvalue) slotstate = { # Hack to circumvent non-predictable memoization caused by string interning. # See the inline comment in _class_setstate for details. "__name__": "".join(func.__name__), "__qualname__": "".join(func.__qualname__), "__annotations__": func.__annotations__, "__kwdefaults__": func.__kwdefaults__, "__defaults__": func.__defaults__, "__module__": func.__module__, "__doc__": func.__doc__, "__closure__": func.__closure__, }
f_globals_ref = _extract_code_globals(func.__code__) f_globals = {k: func.__globals__[k] for k in f_globals_ref if k in func.__globals__}
if func.__closure__ is not None: closure_values = list(map(_get_cell_contents, func.__closure__)) else: closure_values = ()
# Extract currently-imported submodules used by func. Storing these modules # in a smoke _cloudpickle_subimports attribute of the object's state will # trigger the side effect of importing these modules at unpickling time # (which is necessary for func to work correctly once depickled) slotstate["_cloudpickle_submodules"] = _find_imported_submodules( func.__code__, itertools.chain(f_globals.values(), closure_values) ) slotstate["__globals__"] = f_globals
# Hack to circumvent non-predictable memoization caused by string interning. # See the inline comment in _class_setstate for details. state = {"".join(k): v for k, v in func.__dict__.items()} return state, slotstate
def _class_getstate(obj): clsdict = _extract_class_dict(obj) clsdict.pop("__weakref__", None)
if issubclass(type(obj), abc.ABCMeta): # If obj is an instance of an ABCMeta subclass, don't pickle the # cache/negative caches populated during isinstance/issubclass # checks, but pickle the list of registered subclasses of obj. clsdict.pop("_abc_cache", None) clsdict.pop("_abc_negative_cache", None) clsdict.pop("_abc_negative_cache_version", None) registry = clsdict.pop("_abc_registry", None) if registry is None: # The abc caches and registered subclasses of a # class are bundled into the single _abc_impl attribute clsdict.pop("_abc_impl", None) (registry, _, _, _) = abc._get_dump(obj)
clsdict["_abc_impl"] = [subclass_weakref() for subclass_weakref in registry] else: # In the above if clause, registry is a set of weakrefs -- in # this case, registry is a WeakSet clsdict["_abc_impl"] = [type_ for type_ in registry]
if "__slots__" in clsdict: # pickle string length optimization: member descriptors of obj are # created automatically from obj's __slots__ attribute, no need to # save them in obj's state if isinstance(obj.__slots__, str): clsdict.pop(obj.__slots__) else: for k in obj.__slots__: clsdict.pop(k, None)
clsdict.pop("__dict__", None) # unpicklable property object
if sys.version_info >= (3, 14): # PEP-649/749: __annotate_func__ contains a closure that references the class # dict. We need to exclude it from pickling. Python will recreate it when # __annotations__ is accessed at unpickling time. clsdict.pop("__annotate_func__", None)
return (clsdict, {})
def _enum_getstate(obj): clsdict, slotstate = _class_getstate(obj)
members = {e.name: e.value for e in obj} # Cleanup the clsdict that will be passed to _make_skeleton_enum: # Those attributes are already handled by the metaclass. for attrname in [ "_generate_next_value_", "_member_names_", "_member_map_", "_member_type_", "_value2member_map_", ]: clsdict.pop(attrname, None) for member in members: clsdict.pop(member) # Special handling of Enum subclasses return clsdict, slotstate
# COLLECTIONS OF OBJECTS REDUCERS# -------------------------------# A reducer is a function taking a single argument (obj), and that returns a# tuple with all the necessary data to re-construct obj. Apart from a few# exceptions (list, dict, bytes, int, etc.), a reducer is necessary to# correctly pickle an object.# While many built-in objects (Exceptions objects, instances of the "object"# class, etc), are shipped with their own built-in reducer (invoked using# obj.__reduce__), some do not. The following methods were created to "fill# these holes".
def _code_reduce(obj): """code object reducer.""" # If you are not sure about the order of arguments, take a look at help # of the specific type from types, for example: # >>> from types import CodeType # >>> help(CodeType)
# Hack to circumvent non-predictable memoization caused by string interning. # See the inline comment in _class_setstate for details. co_name = "".join(obj.co_name)
# Create shallow copies of these tuple to make cloudpickle payload deterministic. # When creating a code object during load, copies of these four tuples are # created, while in the main process, these tuples can be shared. # By always creating copies, we make sure the resulting payload is deterministic. co_names = tuple(name for name in obj.co_names) co_varnames = tuple(name for name in obj.co_varnames) co_freevars = tuple(name for name in obj.co_freevars) co_cellvars = tuple(name for name in obj.co_cellvars) if hasattr(obj, "co_exceptiontable"): # Python 3.11 and later: there are some new attributes # related to the enhanced exceptions. args = ( obj.co_argcount, obj.co_posonlyargcount, obj.co_kwonlyargcount, obj.co_nlocals, obj.co_stacksize, obj.co_flags, obj.co_code, obj.co_consts, co_names, co_varnames, obj.co_filename, co_name, obj.co_qualname, obj.co_firstlineno, obj.co_linetable, obj.co_exceptiontable, co_freevars, co_cellvars, ) elif hasattr(obj, "co_linetable"): # Python 3.10 and later: obj.co_lnotab is deprecated and constructor # expects obj.co_linetable instead. args = ( obj.co_argcount, obj.co_posonlyargcount, obj.co_kwonlyargcount, obj.co_nlocals, obj.co_stacksize, obj.co_flags, obj.co_code, obj.co_consts, co_names, co_varnames, obj.co_filename, co_name, obj.co_firstlineno, obj.co_linetable, co_freevars, co_cellvars, ) elif hasattr(obj, "co_nmeta"): # pragma: no cover # "nogil" Python: modified attributes from 3.9 args = ( obj.co_argcount, obj.co_posonlyargcount, obj.co_kwonlyargcount, obj.co_nlocals, obj.co_framesize, obj.co_ndefaultargs, obj.co_nmeta, obj.co_flags, obj.co_code, obj.co_consts, co_varnames, obj.co_filename, co_name, obj.co_firstlineno, obj.co_lnotab, obj.co_exc_handlers, obj.co_jump_table, co_freevars, co_cellvars, obj.co_free2reg, obj.co_cell2reg, ) else: # Backward compat for 3.8 and 3.9 args = ( obj.co_argcount, obj.co_posonlyargcount, obj.co_kwonlyargcount, obj.co_nlocals, obj.co_stacksize, obj.co_flags, obj.co_code, obj.co_consts, co_names, co_varnames, obj.co_filename, co_name, obj.co_firstlineno, obj.co_lnotab, co_freevars, co_cellvars, ) return types.CodeType, args
def _cell_reduce(obj): """Cell (containing values of a function's free variables) reducer.""" try: obj.cell_contents except ValueError: # cell is empty return _make_empty_cell, () else: return _make_cell, (obj.cell_contents,)
def _classmethod_reduce(obj): orig_func = obj.__func__ return type(obj), (orig_func,)
def _file_reduce(obj): """Save a file.""" import io
if not hasattr(obj, "name") or not hasattr(obj, "mode"): raise pickle.PicklingError( "Cannot pickle files that do not map to an actual file" ) if obj is sys.stdout: return getattr, (sys, "stdout") if obj is sys.stderr: return getattr, (sys, "stderr") if obj is sys.stdin: raise pickle.PicklingError("Cannot pickle standard input") if obj.closed: raise pickle.PicklingError("Cannot pickle closed files") if hasattr(obj, "isatty") and obj.isatty(): raise pickle.PicklingError("Cannot pickle files that map to tty objects") if "r" not in obj.mode and "+" not in obj.mode: raise pickle.PicklingError( "Cannot pickle files that are not opened for reading: %s" % obj.mode )
name = obj.name
retval = io.StringIO()
try: # Read the whole file curloc = obj.tell() obj.seek(0) contents = obj.read() obj.seek(curloc) except OSError as e: raise pickle.PicklingError( "Cannot pickle file %s as it cannot be read" % name ) from e retval.write(contents) retval.seek(curloc)
retval.name = name return _file_reconstructor, (retval,)
def _getset_descriptor_reduce(obj): return getattr, (obj.__objclass__, obj.__name__)
def _mappingproxy_reduce(obj): return types.MappingProxyType, (dict(obj),)
def _memoryview_reduce(obj): return bytes, (obj.tobytes(),)
def _module_reduce(obj): if _should_pickle_by_reference(obj): return subimport, (obj.__name__,) else: # Some external libraries can populate the "__builtins__" entry of a # module's `__dict__` with unpicklable objects (see #316). For that # reason, we do not attempt to pickle the "__builtins__" entry, and # restore a default value for it at unpickling time. state = obj.__dict__.copy() state.pop("__builtins__", None) return dynamic_subimport, (obj.__name__, state)
def _method_reduce(obj): return (types.MethodType, (obj.__func__, obj.__self__))
def _logger_reduce(obj): return logging.getLogger, (obj.name,)
def _root_logger_reduce(obj): return logging.getLogger, ()
def _property_reduce(obj): return property, (obj.fget, obj.fset, obj.fdel, obj.__doc__)
def _weakset_reduce(obj): return weakref.WeakSet, (list(obj),)
def _dynamic_class_reduce(obj): """Save a class that can't be referenced as a module attribute.
This method is used to serialize classes that are defined inside functions, or that otherwise can't be serialized as attribute lookups from importable modules. """ if Enum is not None and issubclass(obj, Enum): return ( _make_skeleton_enum, _enum_getnewargs(obj), _enum_getstate(obj), None, None, _class_setstate, ) else: return ( _make_skeleton_class, _class_getnewargs(obj), _class_getstate(obj), None, None, _class_setstate, )
def _class_reduce(obj): """Select the reducer depending on the dynamic nature of the class obj.""" if obj is type(None): # noqa return type, (None,) elif obj is type(Ellipsis): return type, (Ellipsis,) elif obj is type(NotImplemented): return type, (NotImplemented,) elif obj in _BUILTIN_TYPE_NAMES: return _builtin_type, (_BUILTIN_TYPE_NAMES[obj],) elif not _should_pickle_by_reference(obj): return _dynamic_class_reduce(obj) return NotImplemented
def _dict_keys_reduce(obj): # Safer not to ship the full dict as sending the rest might # be unintended and could potentially cause leaking of # sensitive information return _make_dict_keys, (list(obj),)
def _dict_values_reduce(obj): # Safer not to ship the full dict as sending the rest might # be unintended and could potentially cause leaking of # sensitive information return _make_dict_values, (list(obj),)
def _dict_items_reduce(obj): return _make_dict_items, (dict(obj),)
def _odict_keys_reduce(obj): # Safer not to ship the full dict as sending the rest might # be unintended and could potentially cause leaking of # sensitive information return _make_dict_keys, (list(obj), True)
def _odict_values_reduce(obj): # Safer not to ship the full dict as sending the rest might # be unintended and could potentially cause leaking of # sensitive information return _make_dict_values, (list(obj), True)
def _odict_items_reduce(obj): return _make_dict_items, (dict(obj), True)
def _dataclass_field_base_reduce(obj): return _get_dataclass_field_type_sentinel, (obj.name,)
# COLLECTIONS OF OBJECTS STATE SETTERS# ------------------------------------# state setters are called at unpickling time, once the object is created and# it has to be updated to how it was at unpickling time.
def _function_setstate(obj, state): """Update the state of a dynamic function.
As __closure__ and __globals__ are readonly attributes of a function, we cannot rely on the native setstate routine of pickle.load_build, that calls setattr on items of the slotstate. Instead, we have to modify them inplace. """ state, slotstate = state obj.__dict__.update(state)
obj_globals = slotstate.pop("__globals__") obj_closure = slotstate.pop("__closure__") # _cloudpickle_subimports is a set of submodules that must be loaded for # the pickled function to work correctly at unpickling time. Now that these # submodules are depickled (hence imported), they can be removed from the # object's state (the object state only served as a reference holder to # these submodules) slotstate.pop("_cloudpickle_submodules")
obj.__globals__.update(obj_globals) obj.__globals__["__builtins__"] = __builtins__
if obj_closure is not None: for i, cell in enumerate(obj_closure): try: value = cell.cell_contents except ValueError: # cell is empty continue obj.__closure__[i].cell_contents = value
for k, v in slotstate.items(): setattr(obj, k, v)
def _class_setstate(obj, state): state, slotstate = state registry = None for attrname, attr in state.items(): if attrname == "_abc_impl": registry = attr else: # Note: setting attribute names on a class automatically triggers their # interning in CPython: # https://github.com/python/cpython/blob/v3.12.0/Objects/object.c#L957 # # This means that to get deterministic pickling for a dynamic class that # was initially defined in a different Python process, the pickler # needs to ensure that dynamic class and function attribute names are # systematically copied into a non-interned version to avoid # unpredictable pickle payloads. # # Indeed the Pickler's memoizer relies on physical object identity to break # cycles in the reference graph of the object being serialized. setattr(obj, attrname, attr)
if sys.version_info >= (3, 13) and "__firstlineno__" in state: # Set the Python 3.13+ only __firstlineno__ attribute one more time, as it # will be automatically deleted by the `setattr(obj, attrname, attr)` call # above when `attrname` is "__firstlineno__". We assume that preserving this # information might be important for some users and that it not stale in the # context of cloudpickle usage, hence legitimate to propagate. Furthermore it # is necessary to do so to keep deterministic chained pickling as tested in # test_deterministic_str_interning_for_chained_dynamic_class_pickling. obj.__firstlineno__ = state["__firstlineno__"]
if registry is not None: for subclass in registry: obj.register(subclass)
# PEP-649/749: During pickling, we excluded the __annotate_func__ attribute but it # will be created by Python. Subsequently, annotations will be recreated when # __annotations__ is accessed.
return obj
# COLLECTION OF DATACLASS UTILITIES# ---------------------------------# There are some internal sentinel values whose identity must be preserved when# unpickling dataclass fields. Each sentinel value has a unique name that we can# use to retrieve its identity at unpickling time.
_DATACLASSE_FIELD_TYPE_SENTINELS = { dataclasses._FIELD.name: dataclasses._FIELD, dataclasses._FIELD_CLASSVAR.name: dataclasses._FIELD_CLASSVAR, dataclasses._FIELD_INITVAR.name: dataclasses._FIELD_INITVAR,}
def _get_dataclass_field_type_sentinel(name): return _DATACLASSE_FIELD_TYPE_SENTINELS[name]
class Pickler(pickle.Pickler): # set of reducers defined and used by cloudpickle (private) _dispatch_table = {} _dispatch_table[classmethod] = _classmethod_reduce _dispatch_table[io.TextIOWrapper] = _file_reduce _dispatch_table[logging.Logger] = _logger_reduce _dispatch_table[logging.RootLogger] = _root_logger_reduce _dispatch_table[memoryview] = _memoryview_reduce _dispatch_table[property] = _property_reduce _dispatch_table[staticmethod] = _classmethod_reduce _dispatch_table[CellType] = _cell_reduce _dispatch_table[types.CodeType] = _code_reduce _dispatch_table[types.GetSetDescriptorType] = _getset_descriptor_reduce _dispatch_table[types.ModuleType] = _module_reduce _dispatch_table[types.MethodType] = _method_reduce _dispatch_table[types.MappingProxyType] = _mappingproxy_reduce _dispatch_table[weakref.WeakSet] = _weakset_reduce _dispatch_table[typing.TypeVar] = _typevar_reduce _dispatch_table[_collections_abc.dict_keys] = _dict_keys_reduce _dispatch_table[_collections_abc.dict_values] = _dict_values_reduce _dispatch_table[_collections_abc.dict_items] = _dict_items_reduce _dispatch_table[type(OrderedDict().keys())] = _odict_keys_reduce _dispatch_table[type(OrderedDict().values())] = _odict_values_reduce _dispatch_table[type(OrderedDict().items())] = _odict_items_reduce _dispatch_table[abc.abstractmethod] = _classmethod_reduce _dispatch_table[abc.abstractclassmethod] = _classmethod_reduce _dispatch_table[abc.abstractstaticmethod] = _classmethod_reduce _dispatch_table[abc.abstractproperty] = _property_reduce _dispatch_table[dataclasses._FIELD_BASE] = _dataclass_field_base_reduce
dispatch_table = ChainMap(_dispatch_table, copyreg.dispatch_table)
# function reducers are defined as instance methods of cloudpickle.Pickler # objects, as they rely on a cloudpickle.Pickler attribute (globals_ref) def _dynamic_function_reduce(self, func): """Reduce a function that is not pickleable via attribute lookup.""" newargs = self._function_getnewargs(func) state = _function_getstate(func) return (_make_function, newargs, state, None, None, _function_setstate)
def _function_reduce(self, obj): """Reducer for function objects.
If obj is a top-level attribute of a file-backed module, this reducer returns NotImplemented, making the cloudpickle.Pickler fall back to traditional pickle.Pickler routines to save obj. Otherwise, it reduces obj using a custom cloudpickle reducer designed specifically to handle dynamic functions. """ if _should_pickle_by_reference(obj): return NotImplemented else: return self._dynamic_function_reduce(obj)
def _function_getnewargs(self, func): code = func.__code__
# base_globals represents the future global namespace of func at # unpickling time. Looking it up and storing it in # cloudpickle.Pickler.globals_ref allow functions sharing the same # globals at pickling time to also share them once unpickled, at one # condition: since globals_ref is an attribute of a cloudpickle.Pickler # instance, and that a new cloudpickle.Pickler is created each time # cloudpickle.dump or cloudpickle.dumps is called, functions also need # to be saved within the same invocation of # cloudpickle.dump/cloudpickle.dumps (for example: # cloudpickle.dumps([f1, f2])). There is no such limitation when using # cloudpickle.Pickler.dump, as long as the multiple invocations are # bound to the same cloudpickle.Pickler instance. base_globals = self.globals_ref.setdefault(id(func.__globals__), {})
if base_globals == {}: # Add module attributes used to resolve relative imports # instructions inside func. for k in ["__package__", "__name__", "__path__", "__file__"]: if k in func.__globals__: base_globals[k] = func.__globals__[k]
# Do not bind the free variables before the function is created to # avoid infinite recursion. if func.__closure__ is None: closure = None else: closure = tuple(_make_empty_cell() for _ in range(len(code.co_freevars)))
return code, base_globals, None, None, closure
def dump(self, obj): try: return super().dump(obj) except RecursionError as e: msg = "Could not pickle object as excessively deep recursion required." raise pickle.PicklingError(msg) from e
def __init__(self, file, protocol=None, buffer_callback=None): if protocol is None: protocol = DEFAULT_PROTOCOL super().__init__(file, protocol=protocol, buffer_callback=buffer_callback) # map functions __globals__ attribute ids, to ensure that functions # sharing the same global namespace at pickling time also share # their global namespace at unpickling time. self.globals_ref = {} self.proto = int(protocol)
if not PYPY: # pickle.Pickler is the C implementation of the CPython pickler and # therefore we rely on reduce_override method to customize the pickler # behavior.
# `cloudpickle.Pickler.dispatch` is only left for backward # compatibility - note that when using protocol 5, # `cloudpickle.Pickler.dispatch` is not an extension of # `pickle._Pickler.dispatch` dictionary, because `cloudpickle.Pickler` # subclasses the C-implemented `pickle.Pickler`, which does not expose # a `dispatch` attribute. Earlier versions of `cloudpickle.Pickler` # used `cloudpickle.Pickler.dispatch` as a class-level attribute # storing all reducers implemented by cloudpickle, but the attribute # name was not a great choice given because it would collide with a # similarly named attribute in the pure-Python `pickle._Pickler` # implementation in the standard library. dispatch = dispatch_table
# Implementation of the reducer_override callback, in order to # efficiently serialize dynamic functions and classes by subclassing # the C-implemented `pickle.Pickler`. # TODO: decorrelate reducer_override (which is tied to CPython's # implementation - would it make sense to backport it to pypy? - and # pickle's protocol 5 which is implementation agnostic. Currently, the # availability of both notions coincide on CPython's pickle, but it may # not be the case anymore when pypy implements protocol 5.
def reducer_override(self, obj): """Type-agnostic reducing callback for function and classes.
For performance reasons, subclasses of the C `pickle.Pickler` class cannot register custom reducers for functions and classes in the dispatch_table attribute. Reducers for such types must instead implemented via the special `reducer_override` method.
Note that this method will be called for any object except a few builtin-types (int, lists, dicts etc.), which differs from reducers in the Pickler's dispatch_table, each of them being invoked for objects of a specific type only.
This property comes in handy for classes: although most classes are instances of the ``type`` metaclass, some of them can be instances of other custom metaclasses (such as enum.EnumMeta for example). In particular, the metaclass will likely not be known in advance, and thus cannot be special-cased using an entry in the dispatch_table. reducer_override, among other things, allows us to register a reducer that will be called for any class, independently of its type.
Notes:
* reducer_override has the priority over dispatch_table-registered reducers. * reducer_override can be used to fix other limitations of cloudpickle for other types that suffered from type-specific reducers, such as Exceptions. See https://github.com/cloudpipe/cloudpickle/issues/248 """ t = type(obj) try: is_anyclass = issubclass(t, type) except TypeError: # t is not a class (old Boost; see SF #502085) is_anyclass = False
if is_anyclass: return _class_reduce(obj) elif isinstance(obj, types.FunctionType): return self._function_reduce(obj) else: # fallback to save_global, including the Pickler's # dispatch_table return NotImplemented
else: # When reducer_override is not available, hack the pure-Python # Pickler's types.FunctionType and type savers. Note: the type saver # must override Pickler.save_global, because pickle.py contains a # hard-coded call to save_global when pickling meta-classes. dispatch = pickle.Pickler.dispatch.copy()
def _save_reduce_pickle5( self, func, args, state=None, listitems=None, dictitems=None, state_setter=None, obj=None, ): save = self.save write = self.write self.save_reduce( func, args, state=None, listitems=listitems, dictitems=dictitems, obj=obj, ) # backport of the Python 3.8 state_setter pickle operations save(state_setter) save(obj) # simple BINGET opcode as obj is already memoized. save(state) write(pickle.TUPLE2) # Trigger a state_setter(obj, state) function call. write(pickle.REDUCE) # The purpose of state_setter is to carry-out an # inplace modification of obj. We do not care about what the # method might return, so its output is eventually removed from # the stack. write(pickle.POP)
def save_global(self, obj, name=None, pack=struct.pack): """Main dispatch method.
The name of this method is somewhat misleading: all types get dispatched here. """ if obj is type(None): # noqa return self.save_reduce(type, (None,), obj=obj) elif obj is type(Ellipsis): return self.save_reduce(type, (Ellipsis,), obj=obj) elif obj is type(NotImplemented): return self.save_reduce(type, (NotImplemented,), obj=obj) elif obj in _BUILTIN_TYPE_NAMES: return self.save_reduce( _builtin_type, (_BUILTIN_TYPE_NAMES[obj],), obj=obj )
if name is not None: super().save_global(obj, name=name) elif not _should_pickle_by_reference(obj, name=name): self._save_reduce_pickle5(*_dynamic_class_reduce(obj), obj=obj) else: super().save_global(obj, name=name)
dispatch[type] = save_global
def save_function(self, obj, name=None): """Registered with the dispatch to handle all function types.
Determines what kind of function obj is (e.g. lambda, defined at interactive prompt, etc) and handles the pickling appropriately. """ if _should_pickle_by_reference(obj, name=name): return super().save_global(obj, name=name) elif PYPY and isinstance(obj.__code__, builtin_code_type): return self.save_pypy_builtin_func(obj) else: return self._save_reduce_pickle5( *self._dynamic_function_reduce(obj), obj=obj )
def save_pypy_builtin_func(self, obj): """Save pypy equivalent of builtin functions.
PyPy does not have the concept of builtin-functions. Instead, builtin-functions are simple function instances, but with a builtin-code attribute. Most of the time, builtin functions should be pickled by attribute. But PyPy has flaky support for __qualname__, so some builtin functions such as float.__new__ will be classified as dynamic. For this reason only, we created this special routine. Because builtin-functions are not expected to have closure or globals, there is no additional hack (compared the one already implemented in pickle) to protect ourselves from reference cycles. A simple (reconstructor, newargs, obj.__dict__) tuple is save_reduced. Note also that PyPy improved their support for __qualname__ in v3.6, so this routing should be removed when cloudpickle supports only PyPy 3.6 and later. """ rv = ( types.FunctionType, (obj.__code__, {}, obj.__name__, obj.__defaults__, obj.__closure__), obj.__dict__, ) self.save_reduce(*rv, obj=obj)
dispatch[types.FunctionType] = save_function
# Shorthands similar to pickle.dump/pickle.dumps
def dump(obj, file, protocol=None, buffer_callback=None): """Serialize obj as bytes streamed into file
protocol defaults to cloudpickle.DEFAULT_PROTOCOL which is an alias to pickle.HIGHEST_PROTOCOL. This setting favors maximum communication speed between processes running the same Python version.
Set protocol=pickle.DEFAULT_PROTOCOL instead if you need to ensure compatibility with older versions of Python (although this is not always guaranteed to work because cloudpickle relies on some internal implementation details that can change from one Python version to the next). """ Pickler(file, protocol=protocol, buffer_callback=buffer_callback).dump(obj)
def dumps(obj, protocol=None, buffer_callback=None): """Serialize obj as a string of bytes allocated in memory
protocol defaults to cloudpickle.DEFAULT_PROTOCOL which is an alias to pickle.HIGHEST_PROTOCOL. This setting favors maximum communication speed between processes running the same Python version.
Set protocol=pickle.DEFAULT_PROTOCOL instead if you need to ensure compatibility with older versions of Python (although this is not always guaranteed to work because cloudpickle relies on some internal implementation details that can change from one Python version to the next). """ with io.BytesIO() as file: cp = Pickler(file, protocol=protocol, buffer_callback=buffer_callback) cp.dump(obj) return file.getvalue()
# Include pickles unloading functions in this namespace for convenience.load, loads = pickle.load, pickle.loads
# Backward compat alias.CloudPickler = Pickler1234567891011121314"""Compatibility module.
It can be necessary to load files generated by previous versions of cloudpicklethat rely on symbols being defined under the `cloudpickle.cloudpickle_fast`namespace.
See: tests/test_backward_compat.py"""
from . import cloudpickle
def __getattr__(name): return getattr(cloudpickle, name)1pip123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102Metadata-Version: 2.1Name: ninjaVersion: 1.13.0Summary: Ninja is a small build system with a focus on speedKeywords: build,c++,cross-compilation,cross-platform,fortran,ninjaAuthor-Email: Jean-Christophe Fillion-Robin <scikit-build@googlegroups.com>, Henry Schreiner <henryfs@princeton.edu>Classifier: Development Status :: 5 - Production/StableClassifier: Intended Audience :: DevelopersClassifier: License :: OSI Approved :: Apache Software LicenseClassifier: License :: OSI Approved :: BSD LicenseClassifier: Operating System :: OS IndependentClassifier: Programming Language :: CClassifier: Programming Language :: C++Classifier: Programming Language :: FortranClassifier: Programming Language :: PythonClassifier: Topic :: Software Development :: Build ToolsClassifier: Typing :: TypedProject-URL: Bug Tracker, https://github.com/scikit-build/ninja-python-distributions/issuesProject-URL: Documentation, https://github.com/scikit-build/ninja-python-distributions#readmeProject-URL: Download, https://github.com/ninja-build/ninja/releasesProject-URL: Homepage, http://ninja-build.org/Project-URL: Mailing list, https://groups.google.com/forum/#!forum/scikit-buildProject-URL: Source Code, https://github.com/scikit-build/ninja-python-distributionsRequires-Python: >=3.8Description-Content-Type: text/x-rst
==========================Ninja Python Distributions==========================
`Ninja <http://www.ninja-build.org>`_ is a small build system with a focus on speed.
The latest Ninja python wheels provide `ninja 1.13.0.gd74ef.kitware.jobserver-pipe-1 <https://ninja-build.org/manual.html>`_ executableand `ninja_syntax.py` for generating `.ninja` files.
.. image:: https://raw.githubusercontent.com/scikit-build/ninja-python-distributions/master/ninja-python-distributions-logo.png
Latest Release--------------
.. table::
+----------------------------------------------------------------------+---------------------------------------------------------------------------+ | Versions | Downloads | +======================================================================+===========================================================================+ | .. image:: https://img.shields.io/pypi/v/ninja.svg | .. image:: https://img.shields.io/badge/downloads-2535k%20total-green.svg | | :target: https://pypi.python.org/pypi/ninja | :target: https://pypi.python.org/pypi/ninja | +----------------------------------------------------------------------+---------------------------------------------------------------------------+
Build Status------------
.. table::
+---------------+-------------------------------------------------------------------------------------------------------------+ | | GitHub Actions (Windows, macOS, Linux) | +===============+=============================================================================================================+ | PyPI | .. image:: https://github.com/scikit-build/ninja-python-distributions/actions/workflows/build.yml/badge.svg | | | :target: https://github.com/scikit-build/ninja-python-distributions/actions/workflows/build.yml | +---------------+-------------------------------------------------------------------------------------------------------------+
Maintainers-----------
* `How to update ninja version ? <https://github.com/scikit-build/ninja-python-distributions/blob/master/docs/update_ninja_version.rst>`_
* `How to make a release ? <https://github.com/scikit-build/ninja-python-distributions/blob/master/docs/make_a_release.rst>`_
Miscellaneous-------------
* Documentation: https://github.com/scikit-build/ninja-python-distributions#readme* Source code: https://github.com/scikit-build/ninja-python-distributions* Mailing list: https://groups.google.com/forum/#!forum/scikit-build
Python Version Support----------------------
Versions after 1.11.1.1 no longer support Python 2-3.6, and require manylinux2010+ on linux.Versions after 1.13 no longer support Python 3.7, and require manylinux2014+/musllinux_1_2+ on linux.
License-------
This project is maintained by Jean-Christophe Fillion-Robin from Kitware Inc.It is covered by the `Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0>`_.
Ninja is also distributed under the `Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0>`_.For more information about Ninja, visit https://ninja-build.org
Logo was originally created by Libby Rose from Kitware Inc.It is covered by `CC BY 4.0 <https://creativecommons.org/licenses/by/4.0/>`_.
History-------
ninja-python-distributions was initially developed in November 2016 byJean-Christophe Fillion-Robin to facilitate the distribution of project using`scikit-build <http://scikit-build.readthedocs.io/>`_ and depending on CMakeand Ninja.123456789101112131415161718../../../bin/ninja,sha256=aW-WKKednOUDFM-VVtfNGh0exSuP1Sgo9vnbFxlWW2c,372384
ninja-1.13.0.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
ninja-1.13.0.dist-info/METADATA,sha256=jXb8Tjgs7c0ivK6qdIrzmz-nkxtyRCOW0VyxgWWdCk4,5148
ninja-1.13.0.dist-info/RECORD,,
ninja-1.13.0.dist-info/WHEEL,sha256=zzOwTeuxXsHOT9QV_vcZk1hX9KJAUdqJD7e-2jGJxLA,150
ninja-1.13.0.dist-info/licenses/AUTHORS.rst,sha256=bGE1t_Lhm2ir8S7n_jbLDohP84fpJ5sNCuxvDVsKNQg,142
ninja-1.13.0.dist-info/licenses/LICENSE_Apache_20,sha256=c7p036pSC0mkAbXSFFmoUjoUbzt1GKgz7qXvqFEwv2g,10273
ninja/__init__.py,sha256=taDHI20kpmjxOT72PiSyE42Pvz3KLXlwMfTZvvnRMKM,1533
ninja/__main__.py,sha256=6iPLwHHAc2TMbojFVcUzERrzN0RvIsywuZc4KpJCg_4,100
ninja/__pycache__/__init__.cpython-312.pyc,,
ninja/__pycache__/__main__.cpython-312.pyc,,
ninja/__pycache__/_version.cpython-312.pyc,,
ninja/__pycache__/ninja_syntax.cpython-312.pyc,,
ninja/_version.py,sha256=M85oP8JJdZ4yZHcp9qfGYLKUYvnN3kTyQosVcYPCPow,19
ninja/_version.pyi,sha256=j5kbzfm6lOn8BzASXWjGIA1yT0OlHTWqlbyZ8Si_o0E,118
ninja/ninja_syntax.py,sha256=RCXZ6Roda3lwnbhtQw6I6bJcpE98mXUZ9jocnvC4IQY,8148
ninja/ninja_syntax.pyi,sha256=5IHH7N9CTKnMDDetFKHOsPK0SOSK_7Q4YtgzhIMNLE0,1576
ninja/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0123456Wheel-Version: 1.0Generator: scikit-build-core 0.11.5Root-Is-Purelib: falseTag: py3-none-manylinux_2_17_x86_64Tag: py3-none-manylinux2014_x86_64
12345=======Credits=======
Please see the GitHub project page at https://github.com/scikit-build/ninja-python-distributions/graphs/contributors123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191Apache LicenseVersion 2.0, January 2004http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction, anddistribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by the copyrightowner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all other entitiesthat control, are controlled by, or are under common control with that entity.For the purposes of this definition, "control" means (i) the power, direct orindirect, to cause the direction or management of such entity, whether bycontract or otherwise, or (ii) ownership of fifty percent (50%) or more of theoutstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity exercisingpermissions granted by this License.
"Source" form shall mean the preferred form for making modifications, includingbut not limited to software source code, documentation source, and configurationfiles.
"Object" form shall mean any form resulting from mechanical transformation ortranslation 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, madeavailable under the License, as indicated by a copyright notice that is includedin 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, thatis based on (or derived from) the Work and for which the editorial revisions,annotations, elaborations, or other modifications represent, as a whole, anoriginal work of authorship. For the purposes of this License, Derivative Worksshall not include works that remain separable from, or merely link (or bind byname) to the interfaces of, the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including the original versionof the Work and any modifications or additions to that Work or Derivative Worksthereof, that is intentionally submitted to Licensor for inclusion in the Workby the copyright owner or by an individual or Legal Entity authorized to submiton behalf of the copyright owner. For the purposes of this definition,"submitted" means any form of electronic, verbal, or written communication sentto the Licensor or its representatives, including but not limited tocommunication on electronic mailing lists, source code control systems, andissue tracking systems that are managed by, or on behalf of, the Licensor forthe purpose of discussing and improving the Work, but excluding communicationthat is conspicuously marked or otherwise designated in writing by the copyrightowner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity on behalfof whom a Contribution has been received by Licensor and subsequentlyincorporated within the Work.
2. Grant of Copyright License.
Subject to the terms and conditions of this License, each Contributor herebygrants 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 suchDerivative Works in Source or Object form.
3. Grant of Patent License.
Subject to the terms and conditions of this License, each Contributor herebygrants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,irrevocable (except as stated in this section) patent license to make, havemade, use, offer to sell, sell, import, and otherwise transfer the Work, wheresuch license applies only to those patent claims licensable by such Contributorthat are necessarily infringed by their Contribution(s) alone or by combinationof their Contribution(s) with the Work to which such Contribution(s) wassubmitted. If You institute patent litigation against any entity (including across-claim or counterclaim in a lawsuit) alleging that the Work or aContribution incorporated within the Work constitutes direct or contributorypatent infringement, then any patent licenses granted to You under this Licensefor 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 thereofin any medium, with or without modifications, and in Source or Object form,provided that You meet the following conditions:
You must give any other recipients of the Work or Derivative Works a copy ofthis License; andYou must cause any modified files to carry prominent notices stating that Youchanged the files; andYou must retain, in the Source form of any Derivative Works that You distribute,all copyright, patent, trademark, and attribution notices from the Source formof the Work, excluding those notices that do not pertain to any part of theDerivative Works; andIf the Work includes a "NOTICE" text file as part of its distribution, then anyDerivative Works that You distribute must include a readable copy of theattribution notices contained within such NOTICE file, excluding those noticesthat do not pertain to any part of the Derivative Works, in at least one of thefollowing places: within a NOTICE text file distributed as part of theDerivative Works; within the Source form or documentation, if provided alongwith the Derivative Works; or, within a display generated by the DerivativeWorks, if and wherever such third-party notices normally appear. The contents ofthe NOTICE file are for informational purposes only and do not modify theLicense. You may add Your own attribution notices within Derivative Works thatYou distribute, alongside or as an addendum to the NOTICE text from the Work,provided that such additional attribution notices cannot be construed asmodifying the License.You may add Your own copyright statement to Your modifications and may provideadditional or different license terms and conditions for use, reproduction, ordistribution of Your modifications, or for any such Derivative Works as a whole,provided Your use, reproduction, and distribution of the Work otherwise complieswith the conditions stated in this License.
5. Submission of Contributions.
Unless You explicitly state otherwise, any Contribution intentionally submittedfor inclusion in the Work by You to the Licensor shall be under the terms andconditions of this License, without any additional terms or conditions.Notwithstanding the above, nothing herein shall supersede or modify the terms ofany separate license agreement you may have executed with Licensor regardingsuch 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 forreasonable and customary use in describing the origin of the Work andreproducing the content of the NOTICE file.
7. Disclaimer of Warranty.
Unless required by applicable law or agreed to in writing, Licensor provides theWork (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 aresolely responsible for determining the appropriateness of using orredistributing the Work and assume any risks associated with Your exercise ofpermissions 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 deliberateand grossly negligent acts) or agreed to in writing, shall any Contributor beliable to You for damages, including any direct, indirect, special, incidental,or consequential damages of any character arising as a result of this License orout of the use or inability to use the Work (including but not limited todamages for loss of goodwill, work stoppage, computer failure or malfunction, orany and all other commercial damages or losses), even if such Contributor hasbeen advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability.
While redistributing the Work or Derivative Works thereof, You may choose tooffer, and charge a fee for, acceptance of support, warranty, indemnity, orother liability obligations and/or rights consistent with this License. However,in accepting such obligations, You may act only on Your own behalf and on Yoursole responsibility, not on behalf of any other Contributor, and only if Youagree to indemnify, defend, and hold each Contributor harmless for any liabilityincurred by, or claims asserted against, such Contributor by reason of youraccepting 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 boilerplatenotice, with the fields enclosed by brackets "[]" replaced with your ownidentifying information. (Don't include the brackets!) The text should beenclosed in the appropriate comment syntax for the file format. We alsorecommend that a file or class name and description of purpose be included onthe same "printed page" as the copyright notice for easier identification withinthird-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.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162# This file is generated by numpy's build process# It contains system_info results at the time of building this package.from enum import Enumfrom 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": "gcc", "linker": r"ld.bfd", "version": "10.2.1", "commands": r"cc", "args": r"-fno-strict-aliasing", "linker args": r"-Wl,--strip-debug, -fno-strict-aliasing", }, "cython": { "name": "cython", "linker": r"cython", "version": "3.0.8", "commands": r"cython", "args": r"", "linker args": r"", }, "c++": { "name": "gcc", "linker": r"ld.bfd", "version": "10.2.1", "commands": r"c++", "args": r"", "linker args": r"-Wl,--strip-debug", }, }, "Machine Information": { "host": { "cpu": "x86_64", "family": "x86_64", "endian": "little", "system": "linux", }, "build": { "cpu": "x86_64", "family": "x86_64", "endian": "little", "system": "linux", }, "cross-compiled": bool("False".lower().replace("false", "")), }, "Build Dependencies": { "blas": { "name": "openblas64", "found": bool("True".lower().replace("false", "")), "version": "0.3.23.dev", "detection method": "pkgconfig", "include directory": r"/usr/local/include", "lib directory": r"/usr/local/lib", "openblas configuration": r"USE_64BITINT=1 DYNAMIC_ARCH=1 DYNAMIC_OLDER= NO_CBLAS= NO_LAPACK= NO_LAPACKE= NO_AFFINITY=1 USE_OPENMP= HASWELL MAX_THREADS=2", "pc file directory": r"/usr/local/lib/pkgconfig", }, "lapack": { "name": "dep140551260102944", "found": bool("True".lower().replace("false", "")), "version": "1.26.4", "detection method": "internal", "include directory": r"unknown", "lib directory": r"unknown", "openblas configuration": r"unknown", "pc file directory": r"unknown", }, }, "Python Information": { "path": r"/opt/python/cp312-cp312/bin/python", "version": "3.12", }, "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])}" )123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050# NumPy static imports for Cython >= 3.0## If any of the PyArray_* functions are called, import_array must be# called first. This is done automatically by Cython 3.0+ if a call# is not detected inside of the module.## Author: Dag Sverre Seljebotn#
from cpython.ref cimport Py_INCREFfrom cpython.object cimport PyObject, PyTypeObject, PyObject_TypeCheckcimport libc.stdio as stdio
cdef extern from *: # Leave a marker that the NumPy declarations came from NumPy itself and not from Cython. # See https://github.com/cython/cython/issues/3573 """ /* Using NumPy API declarations from "numpy/__init__.cython-30.pxd" */ """
cdef extern from "Python.h": ctypedef int Py_intptr_t
cdef extern from "numpy/arrayobject.h": ctypedef Py_intptr_t npy_intp ctypedef size_t npy_uintp
cdef enum NPY_TYPES: NPY_BOOL NPY_BYTE NPY_UBYTE NPY_SHORT NPY_USHORT NPY_INT NPY_UINT NPY_LONG NPY_ULONG NPY_LONGLONG NPY_ULONGLONG NPY_FLOAT NPY_DOUBLE NPY_LONGDOUBLE NPY_CFLOAT NPY_CDOUBLE NPY_CLONGDOUBLE NPY_OBJECT NPY_STRING NPY_UNICODE NPY_VOID NPY_DATETIME NPY_TIMEDELTA NPY_NTYPES NPY_NOTYPE
NPY_INT8 NPY_INT16 NPY_INT32 NPY_INT64 NPY_INT128 NPY_INT256 NPY_UINT8 NPY_UINT16 NPY_UINT32 NPY_UINT64 NPY_UINT128 NPY_UINT256 NPY_FLOAT16 NPY_FLOAT32 NPY_FLOAT64 NPY_FLOAT80 NPY_FLOAT96 NPY_FLOAT128 NPY_FLOAT256 NPY_COMPLEX32 NPY_COMPLEX64 NPY_COMPLEX128 NPY_COMPLEX160 NPY_COMPLEX192 NPY_COMPLEX256 NPY_COMPLEX512
NPY_INTP
ctypedef enum NPY_ORDER: NPY_ANYORDER NPY_CORDER NPY_FORTRANORDER NPY_KEEPORDER
ctypedef enum NPY_CASTING: NPY_NO_CASTING NPY_EQUIV_CASTING NPY_SAFE_CASTING NPY_SAME_KIND_CASTING NPY_UNSAFE_CASTING
ctypedef enum NPY_CLIPMODE: NPY_CLIP NPY_WRAP NPY_RAISE
ctypedef enum NPY_SCALARKIND: NPY_NOSCALAR, NPY_BOOL_SCALAR, NPY_INTPOS_SCALAR, NPY_INTNEG_SCALAR, NPY_FLOAT_SCALAR, NPY_COMPLEX_SCALAR, NPY_OBJECT_SCALAR
ctypedef enum NPY_SORTKIND: NPY_QUICKSORT NPY_HEAPSORT NPY_MERGESORT
ctypedef enum NPY_SEARCHSIDE: NPY_SEARCHLEFT NPY_SEARCHRIGHT
enum: # DEPRECATED since NumPy 1.7 ! Do not use in new code! NPY_C_CONTIGUOUS NPY_F_CONTIGUOUS NPY_CONTIGUOUS NPY_FORTRAN NPY_OWNDATA NPY_FORCECAST NPY_ENSURECOPY NPY_ENSUREARRAY NPY_ELEMENTSTRIDES NPY_ALIGNED NPY_NOTSWAPPED NPY_WRITEABLE NPY_ARR_HAS_DESCR
NPY_BEHAVED NPY_BEHAVED_NS NPY_CARRAY NPY_CARRAY_RO NPY_FARRAY NPY_FARRAY_RO NPY_DEFAULT
NPY_IN_ARRAY NPY_OUT_ARRAY NPY_INOUT_ARRAY NPY_IN_FARRAY NPY_OUT_FARRAY NPY_INOUT_FARRAY
NPY_UPDATE_ALL
enum: # Added in NumPy 1.7 to replace the deprecated enums above. NPY_ARRAY_C_CONTIGUOUS NPY_ARRAY_F_CONTIGUOUS NPY_ARRAY_OWNDATA NPY_ARRAY_FORCECAST NPY_ARRAY_ENSURECOPY NPY_ARRAY_ENSUREARRAY NPY_ARRAY_ELEMENTSTRIDES NPY_ARRAY_ALIGNED NPY_ARRAY_NOTSWAPPED NPY_ARRAY_WRITEABLE NPY_ARRAY_WRITEBACKIFCOPY
NPY_ARRAY_BEHAVED NPY_ARRAY_BEHAVED_NS NPY_ARRAY_CARRAY NPY_ARRAY_CARRAY_RO NPY_ARRAY_FARRAY NPY_ARRAY_FARRAY_RO NPY_ARRAY_DEFAULT
NPY_ARRAY_IN_ARRAY NPY_ARRAY_OUT_ARRAY NPY_ARRAY_INOUT_ARRAY NPY_ARRAY_IN_FARRAY NPY_ARRAY_OUT_FARRAY NPY_ARRAY_INOUT_FARRAY
NPY_ARRAY_UPDATE_ALL
cdef enum: NPY_MAXDIMS
npy_intp NPY_MAX_ELSIZE
ctypedef void (*PyArray_VectorUnaryFunc)(void *, void *, npy_intp, void *, void *)
ctypedef struct PyArray_ArrayDescr: # shape is a tuple, but Cython doesn't support "tuple shape" # inside a non-PyObject declaration, so we have to declare it # as just a PyObject*. PyObject* shape
ctypedef struct PyArray_Descr: pass
ctypedef class numpy.dtype [object PyArray_Descr, check_size ignore]: # Use PyDataType_* macros when possible, however there are no macros # for accessing some of the fields, so some are defined. cdef PyTypeObject* typeobj cdef char kind cdef char type # Numpy sometimes mutates this without warning (e.g. it'll # sometimes change "|" to "<" in shared dtype objects on # little-endian machines). If this matters to you, use # PyArray_IsNativeByteOrder(dtype.byteorder) instead of # directly accessing this field. cdef char byteorder cdef char flags cdef int type_num cdef int itemsize "elsize" cdef int alignment cdef object fields cdef tuple names # Use PyDataType_HASSUBARRAY to test whether this field is # valid (the pointer can be NULL). Most users should access # this field via the inline helper method PyDataType_SHAPE. cdef PyArray_ArrayDescr* subarray
ctypedef class numpy.flatiter [object PyArrayIterObject, check_size ignore]: # Use through macros pass
ctypedef class numpy.broadcast [object PyArrayMultiIterObject, check_size ignore]: # Use through macros pass
ctypedef struct PyArrayObject: # For use in situations where ndarray can't replace PyArrayObject*, # like PyArrayObject**. pass
ctypedef class numpy.ndarray [object PyArrayObject, check_size ignore]: cdef __cythonbufferdefaults__ = {"mode": "strided"}
# NOTE: no field declarations since direct access is deprecated since NumPy 1.7 # Instead, we use properties that map to the corresponding C-API functions.
@property cdef inline PyObject* base(self) nogil: """Returns a borrowed reference to the object owning the data/memory. """ return PyArray_BASE(self)
@property cdef inline dtype descr(self): """Returns an owned reference to the dtype of the array. """ return <dtype>PyArray_DESCR(self)
@property cdef inline int ndim(self) nogil: """Returns the number of dimensions in the array. """ return PyArray_NDIM(self)
@property cdef inline npy_intp *shape(self) nogil: """Returns a pointer to the dimensions/shape of the array. The number of elements matches the number of dimensions of the array (ndim). Can return NULL for 0-dimensional arrays. """ return PyArray_DIMS(self)
@property cdef inline npy_intp *strides(self) nogil: """Returns a pointer to the strides of the array. The number of elements matches the number of dimensions of the array (ndim). """ return PyArray_STRIDES(self)
@property cdef inline npy_intp size(self) nogil: """Returns the total size (in number of elements) of the array. """ return PyArray_SIZE(self)
@property cdef inline char* data(self) nogil: """The pointer to the data buffer as a char*. This is provided for legacy reasons to avoid direct struct field access. For new code that needs this access, you probably want to cast the result of `PyArray_DATA()` instead, which returns a 'void*'. """ return PyArray_BYTES(self)
ctypedef unsigned char npy_bool
ctypedef signed char npy_byte ctypedef signed short npy_short ctypedef signed int npy_int ctypedef signed long npy_long ctypedef signed long long npy_longlong
ctypedef unsigned char npy_ubyte ctypedef unsigned short npy_ushort ctypedef unsigned int npy_uint ctypedef unsigned long npy_ulong ctypedef unsigned long long npy_ulonglong
ctypedef float npy_float ctypedef double npy_double ctypedef long double npy_longdouble
ctypedef signed char npy_int8 ctypedef signed short npy_int16 ctypedef signed int npy_int32 ctypedef signed long long npy_int64 ctypedef signed long long npy_int96 ctypedef signed long long npy_int128
ctypedef unsigned char npy_uint8 ctypedef unsigned short npy_uint16 ctypedef unsigned int npy_uint32 ctypedef unsigned long long npy_uint64 ctypedef unsigned long long npy_uint96 ctypedef unsigned long long npy_uint128
ctypedef float npy_float32 ctypedef double npy_float64 ctypedef long double npy_float80 ctypedef long double npy_float96 ctypedef long double npy_float128
ctypedef struct npy_cfloat: float real float imag
ctypedef struct npy_cdouble: double real double imag
ctypedef struct npy_clongdouble: long double real long double imag
ctypedef struct npy_complex64: float real float imag
ctypedef struct npy_complex128: double real double imag
ctypedef struct npy_complex160: long double real long double imag
ctypedef struct npy_complex192: long double real long double imag
ctypedef struct npy_complex256: long double real long double imag
ctypedef struct PyArray_Dims: npy_intp *ptr int len
int _import_array() except -1 # A second definition so _import_array isn't marked as used when we use it here. # Do not use - subject to change any time. int __pyx_import_array "_import_array"() except -1
# # Macros from ndarrayobject.h # bint PyArray_CHKFLAGS(ndarray m, int flags) nogil bint PyArray_IS_C_CONTIGUOUS(ndarray arr) nogil bint PyArray_IS_F_CONTIGUOUS(ndarray arr) nogil bint PyArray_ISCONTIGUOUS(ndarray m) nogil bint PyArray_ISWRITEABLE(ndarray m) nogil bint PyArray_ISALIGNED(ndarray m) nogil
int PyArray_NDIM(ndarray) nogil bint PyArray_ISONESEGMENT(ndarray) nogil bint PyArray_ISFORTRAN(ndarray) nogil int PyArray_FORTRANIF(ndarray) nogil
void* PyArray_DATA(ndarray) nogil char* PyArray_BYTES(ndarray) nogil
npy_intp* PyArray_DIMS(ndarray) nogil npy_intp* PyArray_STRIDES(ndarray) nogil npy_intp PyArray_DIM(ndarray, size_t) nogil npy_intp PyArray_STRIDE(ndarray, size_t) nogil
PyObject *PyArray_BASE(ndarray) nogil # returns borrowed reference! PyArray_Descr *PyArray_DESCR(ndarray) nogil # returns borrowed reference to dtype! PyArray_Descr *PyArray_DTYPE(ndarray) nogil # returns borrowed reference to dtype! NP 1.7+ alias for descr. int PyArray_FLAGS(ndarray) nogil void PyArray_CLEARFLAGS(ndarray, int flags) nogil # Added in NumPy 1.7 void PyArray_ENABLEFLAGS(ndarray, int flags) nogil # Added in NumPy 1.7 npy_intp PyArray_ITEMSIZE(ndarray) nogil int PyArray_TYPE(ndarray arr) nogil
object PyArray_GETITEM(ndarray arr, void *itemptr) int PyArray_SETITEM(ndarray arr, void *itemptr, object obj) except -1
bint PyTypeNum_ISBOOL(int) nogil bint PyTypeNum_ISUNSIGNED(int) nogil bint PyTypeNum_ISSIGNED(int) nogil bint PyTypeNum_ISINTEGER(int) nogil bint PyTypeNum_ISFLOAT(int) nogil bint PyTypeNum_ISNUMBER(int) nogil bint PyTypeNum_ISSTRING(int) nogil bint PyTypeNum_ISCOMPLEX(int) nogil bint PyTypeNum_ISPYTHON(int) nogil bint PyTypeNum_ISFLEXIBLE(int) nogil bint PyTypeNum_ISUSERDEF(int) nogil bint PyTypeNum_ISEXTENDED(int) nogil bint PyTypeNum_ISOBJECT(int) nogil
bint PyDataType_ISBOOL(dtype) nogil bint PyDataType_ISUNSIGNED(dtype) nogil bint PyDataType_ISSIGNED(dtype) nogil bint PyDataType_ISINTEGER(dtype) nogil bint PyDataType_ISFLOAT(dtype) nogil bint PyDataType_ISNUMBER(dtype) nogil bint PyDataType_ISSTRING(dtype) nogil bint PyDataType_ISCOMPLEX(dtype) nogil bint PyDataType_ISPYTHON(dtype) nogil bint PyDataType_ISFLEXIBLE(dtype) nogil bint PyDataType_ISUSERDEF(dtype) nogil bint PyDataType_ISEXTENDED(dtype) nogil bint PyDataType_ISOBJECT(dtype) nogil bint PyDataType_HASFIELDS(dtype) nogil bint PyDataType_HASSUBARRAY(dtype) nogil
bint PyArray_ISBOOL(ndarray) nogil bint PyArray_ISUNSIGNED(ndarray) nogil bint PyArray_ISSIGNED(ndarray) nogil bint PyArray_ISINTEGER(ndarray) nogil bint PyArray_ISFLOAT(ndarray) nogil bint PyArray_ISNUMBER(ndarray) nogil bint PyArray_ISSTRING(ndarray) nogil bint PyArray_ISCOMPLEX(ndarray) nogil bint PyArray_ISPYTHON(ndarray) nogil bint PyArray_ISFLEXIBLE(ndarray) nogil bint PyArray_ISUSERDEF(ndarray) nogil bint PyArray_ISEXTENDED(ndarray) nogil bint PyArray_ISOBJECT(ndarray) nogil bint PyArray_HASFIELDS(ndarray) nogil
bint PyArray_ISVARIABLE(ndarray) nogil
bint PyArray_SAFEALIGNEDCOPY(ndarray) nogil bint PyArray_ISNBO(char) nogil # works on ndarray.byteorder bint PyArray_IsNativeByteOrder(char) nogil # works on ndarray.byteorder bint PyArray_ISNOTSWAPPED(ndarray) nogil bint PyArray_ISBYTESWAPPED(ndarray) nogil
bint PyArray_FLAGSWAP(ndarray, int) nogil
bint PyArray_ISCARRAY(ndarray) nogil bint PyArray_ISCARRAY_RO(ndarray) nogil bint PyArray_ISFARRAY(ndarray) nogil bint PyArray_ISFARRAY_RO(ndarray) nogil bint PyArray_ISBEHAVED(ndarray) nogil bint PyArray_ISBEHAVED_RO(ndarray) nogil
bint PyDataType_ISNOTSWAPPED(dtype) nogil bint PyDataType_ISBYTESWAPPED(dtype) nogil
bint PyArray_DescrCheck(object)
bint PyArray_Check(object) bint PyArray_CheckExact(object)
# Cannot be supported due to out arg: # bint PyArray_HasArrayInterfaceType(object, dtype, object, object&) # bint PyArray_HasArrayInterface(op, out)
bint PyArray_IsZeroDim(object) # Cannot be supported due to ## ## in macro: # bint PyArray_IsScalar(object, verbatim work) bint PyArray_CheckScalar(object) bint PyArray_IsPythonNumber(object) bint PyArray_IsPythonScalar(object) bint PyArray_IsAnyScalar(object) bint PyArray_CheckAnyScalar(object)
ndarray PyArray_GETCONTIGUOUS(ndarray) bint PyArray_SAMESHAPE(ndarray, ndarray) nogil npy_intp PyArray_SIZE(ndarray) nogil npy_intp PyArray_NBYTES(ndarray) nogil
object PyArray_FROM_O(object) object PyArray_FROM_OF(object m, int flags) object PyArray_FROM_OT(object m, int type) object PyArray_FROM_OTF(object m, int type, int flags) object PyArray_FROMANY(object m, int type, int min, int max, int flags) object PyArray_ZEROS(int nd, npy_intp* dims, int type, int fortran) object PyArray_EMPTY(int nd, npy_intp* dims, int type, int fortran) void PyArray_FILLWBYTE(object, int val) npy_intp PyArray_REFCOUNT(object) object PyArray_ContiguousFromAny(op, int, int min_depth, int max_depth) unsigned char PyArray_EquivArrTypes(ndarray a1, ndarray a2) bint PyArray_EquivByteorders(int b1, int b2) nogil object PyArray_SimpleNew(int nd, npy_intp* dims, int typenum) object PyArray_SimpleNewFromData(int nd, npy_intp* dims, int typenum, void* data) #object PyArray_SimpleNewFromDescr(int nd, npy_intp* dims, dtype descr) object PyArray_ToScalar(void* data, ndarray arr)
void* PyArray_GETPTR1(ndarray m, npy_intp i) nogil void* PyArray_GETPTR2(ndarray m, npy_intp i, npy_intp j) nogil void* PyArray_GETPTR3(ndarray m, npy_intp i, npy_intp j, npy_intp k) nogil void* PyArray_GETPTR4(ndarray m, npy_intp i, npy_intp j, npy_intp k, npy_intp l) nogil
# Cannot be supported due to out arg # void PyArray_DESCR_REPLACE(descr)
object PyArray_Copy(ndarray) object PyArray_FromObject(object op, int type, int min_depth, int max_depth) object PyArray_ContiguousFromObject(object op, int type, int min_depth, int max_depth) object PyArray_CopyFromObject(object op, int type, int min_depth, int max_depth)
object PyArray_Cast(ndarray mp, int type_num) object PyArray_Take(ndarray ap, object items, int axis) object PyArray_Put(ndarray ap, object items, object values)
void PyArray_ITER_RESET(flatiter it) nogil void PyArray_ITER_NEXT(flatiter it) nogil void PyArray_ITER_GOTO(flatiter it, npy_intp* destination) nogil void PyArray_ITER_GOTO1D(flatiter it, npy_intp ind) nogil void* PyArray_ITER_DATA(flatiter it) nogil bint PyArray_ITER_NOTDONE(flatiter it) nogil
void PyArray_MultiIter_RESET(broadcast multi) nogil void PyArray_MultiIter_NEXT(broadcast multi) nogil void PyArray_MultiIter_GOTO(broadcast multi, npy_intp dest) nogil void PyArray_MultiIter_GOTO1D(broadcast multi, npy_intp ind) nogil void* PyArray_MultiIter_DATA(broadcast multi, npy_intp i) nogil void PyArray_MultiIter_NEXTi(broadcast multi, npy_intp i) nogil bint PyArray_MultiIter_NOTDONE(broadcast multi) nogil
# Functions from __multiarray_api.h
# Functions taking dtype and returning object/ndarray are disabled # for now as they steal dtype references. I'm conservative and disable # more than is probably needed until it can be checked further. int PyArray_SetNumericOps (object) except -1 object PyArray_GetNumericOps () int PyArray_INCREF (ndarray) except * # uses PyArray_Item_INCREF... int PyArray_XDECREF (ndarray) except * # uses PyArray_Item_DECREF... void PyArray_SetStringFunction (object, int) dtype PyArray_DescrFromType (int) object PyArray_TypeObjectFromType (int) char * PyArray_Zero (ndarray) char * PyArray_One (ndarray) #object PyArray_CastToType (ndarray, dtype, int) int PyArray_CastTo (ndarray, ndarray) except -1 int PyArray_CastAnyTo (ndarray, ndarray) except -1 int PyArray_CanCastSafely (int, int) # writes errors npy_bool PyArray_CanCastTo (dtype, dtype) # writes errors int PyArray_ObjectType (object, int) except 0 dtype PyArray_DescrFromObject (object, dtype) #ndarray* PyArray_ConvertToCommonType (object, int *) dtype PyArray_DescrFromScalar (object) dtype PyArray_DescrFromTypeObject (object) npy_intp PyArray_Size (object) #object PyArray_Scalar (void *, dtype, object) #object PyArray_FromScalar (object, dtype) void PyArray_ScalarAsCtype (object, void *) #int PyArray_CastScalarToCtype (object, void *, dtype) #int PyArray_CastScalarDirect (object, dtype, void *, int) object PyArray_ScalarFromObject (object) #PyArray_VectorUnaryFunc * PyArray_GetCastFunc (dtype, int) object PyArray_FromDims (int, int *, int) #object PyArray_FromDimsAndDataAndDescr (int, int *, dtype, char *) #object PyArray_FromAny (object, dtype, int, int, int, object) object PyArray_EnsureArray (object) object PyArray_EnsureAnyArray (object) #object PyArray_FromFile (stdio.FILE *, dtype, npy_intp, char *) #object PyArray_FromString (char *, npy_intp, dtype, npy_intp, char *) #object PyArray_FromBuffer (object, dtype, npy_intp, npy_intp) #object PyArray_FromIter (object, dtype, npy_intp) object PyArray_Return (ndarray) #object PyArray_GetField (ndarray, dtype, int) #int PyArray_SetField (ndarray, dtype, int, object) except -1 object PyArray_Byteswap (ndarray, npy_bool) object PyArray_Resize (ndarray, PyArray_Dims *, int, NPY_ORDER) int PyArray_MoveInto (ndarray, ndarray) except -1 int PyArray_CopyInto (ndarray, ndarray) except -1 int PyArray_CopyAnyInto (ndarray, ndarray) except -1 int PyArray_CopyObject (ndarray, object) except -1 object PyArray_NewCopy (ndarray, NPY_ORDER) object PyArray_ToList (ndarray) object PyArray_ToString (ndarray, NPY_ORDER) int PyArray_ToFile (ndarray, stdio.FILE *, char *, char *) except -1 int PyArray_Dump (object, object, int) except -1 object PyArray_Dumps (object, int) int PyArray_ValidType (int) # Cannot error void PyArray_UpdateFlags (ndarray, int) object PyArray_New (type, int, npy_intp *, int, npy_intp *, void *, int, int, object) #object PyArray_NewFromDescr (type, dtype, int, npy_intp *, npy_intp *, void *, int, object) #dtype PyArray_DescrNew (dtype) dtype PyArray_DescrNewFromType (int) double PyArray_GetPriority (object, double) # clears errors as of 1.25 object PyArray_IterNew (object) object PyArray_MultiIterNew (int, ...)
int PyArray_PyIntAsInt (object) except? -1 npy_intp PyArray_PyIntAsIntp (object) int PyArray_Broadcast (broadcast) except -1 void PyArray_FillObjectArray (ndarray, object) except * int PyArray_FillWithScalar (ndarray, object) except -1 npy_bool PyArray_CheckStrides (int, int, npy_intp, npy_intp, npy_intp *, npy_intp *) dtype PyArray_DescrNewByteorder (dtype, char) object PyArray_IterAllButAxis (object, int *) #object PyArray_CheckFromAny (object, dtype, int, int, int, object) #object PyArray_FromArray (ndarray, dtype, int) object PyArray_FromInterface (object) object PyArray_FromStructInterface (object) #object PyArray_FromArrayAttr (object, dtype, object) #NPY_SCALARKIND PyArray_ScalarKind (int, ndarray*) int PyArray_CanCoerceScalar (int, int, NPY_SCALARKIND) object PyArray_NewFlagsObject (object) npy_bool PyArray_CanCastScalar (type, type) #int PyArray_CompareUCS4 (npy_ucs4 *, npy_ucs4 *, register size_t) int PyArray_RemoveSmallest (broadcast) except -1 int PyArray_ElementStrides (object) void PyArray_Item_INCREF (char *, dtype) except * void PyArray_Item_XDECREF (char *, dtype) except * object PyArray_FieldNames (object) object PyArray_Transpose (ndarray, PyArray_Dims *) object PyArray_TakeFrom (ndarray, object, int, ndarray, NPY_CLIPMODE) object PyArray_PutTo (ndarray, object, object, NPY_CLIPMODE) object PyArray_PutMask (ndarray, object, object) object PyArray_Repeat (ndarray, object, int) object PyArray_Choose (ndarray, object, ndarray, NPY_CLIPMODE) int PyArray_Sort (ndarray, int, NPY_SORTKIND) except -1 object PyArray_ArgSort (ndarray, int, NPY_SORTKIND) object PyArray_SearchSorted (ndarray, object, NPY_SEARCHSIDE, PyObject *) object PyArray_ArgMax (ndarray, int, ndarray) object PyArray_ArgMin (ndarray, int, ndarray) object PyArray_Reshape (ndarray, object) object PyArray_Newshape (ndarray, PyArray_Dims *, NPY_ORDER) object PyArray_Squeeze (ndarray) #object PyArray_View (ndarray, dtype, type) object PyArray_SwapAxes (ndarray, int, int) object PyArray_Max (ndarray, int, ndarray) object PyArray_Min (ndarray, int, ndarray) object PyArray_Ptp (ndarray, int, ndarray) object PyArray_Mean (ndarray, int, int, ndarray) object PyArray_Trace (ndarray, int, int, int, int, ndarray) object PyArray_Diagonal (ndarray, int, int, int) object PyArray_Clip (ndarray, object, object, ndarray) object PyArray_Conjugate (ndarray, ndarray) object PyArray_Nonzero (ndarray) object PyArray_Std (ndarray, int, int, ndarray, int) object PyArray_Sum (ndarray, int, int, ndarray) object PyArray_CumSum (ndarray, int, int, ndarray) object PyArray_Prod (ndarray, int, int, ndarray) object PyArray_CumProd (ndarray, int, int, ndarray) object PyArray_All (ndarray, int, ndarray) object PyArray_Any (ndarray, int, ndarray) object PyArray_Compress (ndarray, object, int, ndarray) object PyArray_Flatten (ndarray, NPY_ORDER) object PyArray_Ravel (ndarray, NPY_ORDER) npy_intp PyArray_MultiplyList (npy_intp *, int) int PyArray_MultiplyIntList (int *, int) void * PyArray_GetPtr (ndarray, npy_intp*) int PyArray_CompareLists (npy_intp *, npy_intp *, int) #int PyArray_AsCArray (object*, void *, npy_intp *, int, dtype) #int PyArray_As1D (object*, char **, int *, int) #int PyArray_As2D (object*, char ***, int *, int *, int) int PyArray_Free (object, void *) #int PyArray_Converter (object, object*) int PyArray_IntpFromSequence (object, npy_intp *, int) except -1 object PyArray_Concatenate (object, int) object PyArray_InnerProduct (object, object) object PyArray_MatrixProduct (object, object) object PyArray_CopyAndTranspose (object) object PyArray_Correlate (object, object, int) int PyArray_TypestrConvert (int, int) #int PyArray_DescrConverter (object, dtype*) except 0 #int PyArray_DescrConverter2 (object, dtype*) except 0 int PyArray_IntpConverter (object, PyArray_Dims *) except 0 #int PyArray_BufferConverter (object, chunk) except 0 int PyArray_AxisConverter (object, int *) except 0 int PyArray_BoolConverter (object, npy_bool *) except 0 int PyArray_ByteorderConverter (object, char *) except 0 int PyArray_OrderConverter (object, NPY_ORDER *) except 0 unsigned char PyArray_EquivTypes (dtype, dtype) # clears errors #object PyArray_Zeros (int, npy_intp *, dtype, int) #object PyArray_Empty (int, npy_intp *, dtype, int) object PyArray_Where (object, object, object) object PyArray_Arange (double, double, double, int) #object PyArray_ArangeObj (object, object, object, dtype) int PyArray_SortkindConverter (object, NPY_SORTKIND *) except 0 object PyArray_LexSort (object, int) object PyArray_Round (ndarray, int, ndarray) unsigned char PyArray_EquivTypenums (int, int) int PyArray_RegisterDataType (dtype) except -1 int PyArray_RegisterCastFunc (dtype, int, PyArray_VectorUnaryFunc *) except -1 int PyArray_RegisterCanCast (dtype, int, NPY_SCALARKIND) except -1 #void PyArray_InitArrFuncs (PyArray_ArrFuncs *) object PyArray_IntTupleFromIntp (int, npy_intp *) int PyArray_TypeNumFromName (char *) int PyArray_ClipmodeConverter (object, NPY_CLIPMODE *) except 0 #int PyArray_OutputConverter (object, ndarray*) except 0 object PyArray_BroadcastToShape (object, npy_intp *, int) void _PyArray_SigintHandler (int) void* _PyArray_GetSigintBuf () #int PyArray_DescrAlignConverter (object, dtype*) except 0 #int PyArray_DescrAlignConverter2 (object, dtype*) except 0 int PyArray_SearchsideConverter (object, void *) except 0 object PyArray_CheckAxis (ndarray, int *, int) npy_intp PyArray_OverflowMultiplyList (npy_intp *, int) int PyArray_CompareString (char *, char *, size_t) int PyArray_SetBaseObject(ndarray, base) except -1 # NOTE: steals a reference to base! Use "set_array_base()" instead.
# Typedefs that matches the runtime dtype objects in# the numpy module.
# The ones that are commented out needs an IFDEF function# in Cython to enable them only on the right systems.
ctypedef npy_int8 int8_tctypedef npy_int16 int16_tctypedef npy_int32 int32_tctypedef npy_int64 int64_t#ctypedef npy_int96 int96_t#ctypedef npy_int128 int128_t
ctypedef npy_uint8 uint8_tctypedef npy_uint16 uint16_tctypedef npy_uint32 uint32_tctypedef npy_uint64 uint64_t#ctypedef npy_uint96 uint96_t#ctypedef npy_uint128 uint128_t
ctypedef npy_float32 float32_tctypedef npy_float64 float64_t#ctypedef npy_float80 float80_t#ctypedef npy_float128 float128_t
ctypedef float complex complex64_tctypedef double complex complex128_t
# The int types are mapped a bit surprising --# numpy.int corresponds to 'l' and numpy.long to 'q'ctypedef npy_long int_tctypedef npy_longlong longlong_t
ctypedef npy_ulong uint_tctypedef npy_ulonglong ulonglong_t
ctypedef npy_intp intp_tctypedef npy_uintp uintp_t
ctypedef npy_double float_tctypedef npy_double double_tctypedef npy_longdouble longdouble_t
ctypedef npy_cfloat cfloat_tctypedef npy_cdouble cdouble_tctypedef npy_clongdouble clongdouble_t
ctypedef npy_cdouble complex_t
cdef inline object PyArray_MultiIterNew1(a): return PyArray_MultiIterNew(1, <void*>a)
cdef inline object PyArray_MultiIterNew2(a, b): return PyArray_MultiIterNew(2, <void*>a, <void*>b)
cdef inline object PyArray_MultiIterNew3(a, b, c): return PyArray_MultiIterNew(3, <void*>a, <void*>b, <void*> c)
cdef inline object PyArray_MultiIterNew4(a, b, c, d): return PyArray_MultiIterNew(4, <void*>a, <void*>b, <void*>c, <void*> d)
cdef inline object PyArray_MultiIterNew5(a, b, c, d, e): return PyArray_MultiIterNew(5, <void*>a, <void*>b, <void*>c, <void*> d, <void*> e)
cdef inline tuple PyDataType_SHAPE(dtype d): if PyDataType_HASSUBARRAY(d): return <tuple>d.subarray.shape else: return ()
cdef extern from "numpy/ndarrayobject.h": PyTypeObject PyTimedeltaArrType_Type PyTypeObject PyDatetimeArrType_Type ctypedef int64_t npy_timedelta ctypedef int64_t npy_datetime
cdef extern from "numpy/ndarraytypes.h": ctypedef struct PyArray_DatetimeMetaData: NPY_DATETIMEUNIT base int64_t num
cdef extern from "numpy/arrayscalars.h":
# abstract types ctypedef class numpy.generic [object PyObject]: pass ctypedef class numpy.number [object PyObject]: pass ctypedef class numpy.integer [object PyObject]: pass ctypedef class numpy.signedinteger [object PyObject]: pass ctypedef class numpy.unsignedinteger [object PyObject]: pass ctypedef class numpy.inexact [object PyObject]: pass ctypedef class numpy.floating [object PyObject]: pass ctypedef class numpy.complexfloating [object PyObject]: pass ctypedef class numpy.flexible [object PyObject]: pass ctypedef class numpy.character [object PyObject]: pass
ctypedef struct PyDatetimeScalarObject: # PyObject_HEAD npy_datetime obval PyArray_DatetimeMetaData obmeta
ctypedef struct PyTimedeltaScalarObject: # PyObject_HEAD npy_timedelta obval PyArray_DatetimeMetaData obmeta
ctypedef enum NPY_DATETIMEUNIT: NPY_FR_Y NPY_FR_M NPY_FR_W NPY_FR_D NPY_FR_B NPY_FR_h NPY_FR_m NPY_FR_s NPY_FR_ms NPY_FR_us NPY_FR_ns NPY_FR_ps NPY_FR_fs NPY_FR_as NPY_FR_GENERIC
## ufunc API#
cdef extern from "numpy/ufuncobject.h":
ctypedef void (*PyUFuncGenericFunction) (char **, npy_intp *, npy_intp *, void *)
ctypedef class numpy.ufunc [object PyUFuncObject, check_size ignore]: cdef: int nin, nout, nargs int identity PyUFuncGenericFunction *functions void **data int ntypes int check_return char *name char *types char *doc void *ptr PyObject *obj PyObject *userloops
cdef enum: PyUFunc_Zero PyUFunc_One PyUFunc_None UFUNC_ERR_IGNORE UFUNC_ERR_WARN UFUNC_ERR_RAISE UFUNC_ERR_CALL UFUNC_ERR_PRINT UFUNC_ERR_LOG UFUNC_MASK_DIVIDEBYZERO UFUNC_MASK_OVERFLOW UFUNC_MASK_UNDERFLOW UFUNC_MASK_INVALID UFUNC_SHIFT_DIVIDEBYZERO UFUNC_SHIFT_OVERFLOW UFUNC_SHIFT_UNDERFLOW UFUNC_SHIFT_INVALID UFUNC_FPE_DIVIDEBYZERO UFUNC_FPE_OVERFLOW UFUNC_FPE_UNDERFLOW UFUNC_FPE_INVALID UFUNC_ERR_DEFAULT UFUNC_ERR_DEFAULT2
object PyUFunc_FromFuncAndData(PyUFuncGenericFunction *, void **, char *, int, int, int, int, char *, char *, int) int PyUFunc_RegisterLoopForType(ufunc, int, PyUFuncGenericFunction, int *, void *) except -1 void PyUFunc_f_f_As_d_d \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_d_d \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_f_f \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_g_g \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_F_F_As_D_D \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_F_F \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_D_D \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_G_G \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_O_O \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_ff_f_As_dd_d \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_ff_f \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_dd_d \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_gg_g \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_FF_F_As_DD_D \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_DD_D \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_FF_F \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_GG_G \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_OO_O \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_O_O_method \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_OO_O_method \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_On_Om \ (char **, npy_intp *, npy_intp *, void *) int PyUFunc_GetPyValues \ (char *, int *, int *, PyObject **) int PyUFunc_checkfperr \ (int, PyObject *, int *) void PyUFunc_clearfperr() int PyUFunc_getfperr() int PyUFunc_handlefperr \ (int, PyObject *, int, int *) except -1 int PyUFunc_ReplaceLoopBySignature \ (ufunc, PyUFuncGenericFunction, int *, PyUFuncGenericFunction *) object PyUFunc_FromFuncAndDataAndSignature \ (PyUFuncGenericFunction *, void **, char *, int, int, int, int, char *, char *, int, char *)
int _import_umath() except -1
cdef inline void set_array_base(ndarray arr, object base): Py_INCREF(base) # important to do this before stealing the reference below! PyArray_SetBaseObject(arr, base)
cdef inline object get_array_base(ndarray arr): base = PyArray_BASE(arr) if base is NULL: return None return <object>base
# Versions of the import_* functions which are more suitable for# Cython code.cdef inline int import_array() except -1: try: __pyx_import_array() except Exception: raise ImportError("numpy.core.multiarray failed to import")
cdef inline int import_umath() except -1: try: _import_umath() except Exception: raise ImportError("numpy.core.umath failed to import")
cdef inline int import_ufunc() except -1: try: _import_umath() except Exception: raise ImportError("numpy.core.umath failed to import")
cdef inline bint is_timedelta64_object(object obj): """ Cython equivalent of `isinstance(obj, np.timedelta64)`
Parameters ---------- obj : object
Returns ------- bool """ return PyObject_TypeCheck(obj, &PyTimedeltaArrType_Type)
cdef inline bint is_datetime64_object(object obj): """ Cython equivalent of `isinstance(obj, np.datetime64)`
Parameters ---------- obj : object
Returns ------- bool """ return PyObject_TypeCheck(obj, &PyDatetimeArrType_Type)
cdef inline npy_datetime get_datetime64_value(object obj) nogil: """ returns the int64 value underlying scalar numpy datetime64 object
Note that to interpret this as a datetime, the corresponding unit is also needed. That can be found using `get_datetime64_unit`. """ return (<PyDatetimeScalarObject*>obj).obval
cdef inline npy_timedelta get_timedelta64_value(object obj) nogil: """ returns the int64 value underlying scalar numpy timedelta64 object """ return (<PyTimedeltaScalarObject*>obj).obval
cdef inline NPY_DATETIMEUNIT get_datetime64_unit(object obj) nogil: """ returns the unit part of the dtype for a numpy datetime64 object. """ return <NPY_DATETIMEUNIT>(<PyDatetimeScalarObject*>obj).obmeta.base1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015# NumPy static imports for Cython < 3.0## If any of the PyArray_* functions are called, import_array must be# called first.## Author: Dag Sverre Seljebotn#
DEF _buffer_format_string_len = 255
cimport cpython.buffer as pybuffrom cpython.ref cimport Py_INCREFfrom cpython.mem cimport PyObject_Malloc, PyObject_Freefrom cpython.object cimport PyObject, PyTypeObjectfrom cpython.buffer cimport PyObject_GetBufferfrom cpython.type cimport typecimport libc.stdio as stdio
cdef extern from "Python.h": ctypedef int Py_intptr_t bint PyObject_TypeCheck(object obj, PyTypeObject* type)
cdef extern from "numpy/arrayobject.h": ctypedef Py_intptr_t npy_intp ctypedef size_t npy_uintp
cdef enum NPY_TYPES: NPY_BOOL NPY_BYTE NPY_UBYTE NPY_SHORT NPY_USHORT NPY_INT NPY_UINT NPY_LONG NPY_ULONG NPY_LONGLONG NPY_ULONGLONG NPY_FLOAT NPY_DOUBLE NPY_LONGDOUBLE NPY_CFLOAT NPY_CDOUBLE NPY_CLONGDOUBLE NPY_OBJECT NPY_STRING NPY_UNICODE NPY_VOID NPY_DATETIME NPY_TIMEDELTA NPY_NTYPES NPY_NOTYPE
NPY_INT8 NPY_INT16 NPY_INT32 NPY_INT64 NPY_INT128 NPY_INT256 NPY_UINT8 NPY_UINT16 NPY_UINT32 NPY_UINT64 NPY_UINT128 NPY_UINT256 NPY_FLOAT16 NPY_FLOAT32 NPY_FLOAT64 NPY_FLOAT80 NPY_FLOAT96 NPY_FLOAT128 NPY_FLOAT256 NPY_COMPLEX32 NPY_COMPLEX64 NPY_COMPLEX128 NPY_COMPLEX160 NPY_COMPLEX192 NPY_COMPLEX256 NPY_COMPLEX512
NPY_INTP
ctypedef enum NPY_ORDER: NPY_ANYORDER NPY_CORDER NPY_FORTRANORDER NPY_KEEPORDER
ctypedef enum NPY_CASTING: NPY_NO_CASTING NPY_EQUIV_CASTING NPY_SAFE_CASTING NPY_SAME_KIND_CASTING NPY_UNSAFE_CASTING
ctypedef enum NPY_CLIPMODE: NPY_CLIP NPY_WRAP NPY_RAISE
ctypedef enum NPY_SCALARKIND: NPY_NOSCALAR, NPY_BOOL_SCALAR, NPY_INTPOS_SCALAR, NPY_INTNEG_SCALAR, NPY_FLOAT_SCALAR, NPY_COMPLEX_SCALAR, NPY_OBJECT_SCALAR
ctypedef enum NPY_SORTKIND: NPY_QUICKSORT NPY_HEAPSORT NPY_MERGESORT
ctypedef enum NPY_SEARCHSIDE: NPY_SEARCHLEFT NPY_SEARCHRIGHT
enum: # DEPRECATED since NumPy 1.7 ! Do not use in new code! NPY_C_CONTIGUOUS NPY_F_CONTIGUOUS NPY_CONTIGUOUS NPY_FORTRAN NPY_OWNDATA NPY_FORCECAST NPY_ENSURECOPY NPY_ENSUREARRAY NPY_ELEMENTSTRIDES NPY_ALIGNED NPY_NOTSWAPPED NPY_WRITEABLE NPY_ARR_HAS_DESCR
NPY_BEHAVED NPY_BEHAVED_NS NPY_CARRAY NPY_CARRAY_RO NPY_FARRAY NPY_FARRAY_RO NPY_DEFAULT
NPY_IN_ARRAY NPY_OUT_ARRAY NPY_INOUT_ARRAY NPY_IN_FARRAY NPY_OUT_FARRAY NPY_INOUT_FARRAY
NPY_UPDATE_ALL
enum: # Added in NumPy 1.7 to replace the deprecated enums above. NPY_ARRAY_C_CONTIGUOUS NPY_ARRAY_F_CONTIGUOUS NPY_ARRAY_OWNDATA NPY_ARRAY_FORCECAST NPY_ARRAY_ENSURECOPY NPY_ARRAY_ENSUREARRAY NPY_ARRAY_ELEMENTSTRIDES NPY_ARRAY_ALIGNED NPY_ARRAY_NOTSWAPPED NPY_ARRAY_WRITEABLE NPY_ARRAY_WRITEBACKIFCOPY
NPY_ARRAY_BEHAVED NPY_ARRAY_BEHAVED_NS NPY_ARRAY_CARRAY NPY_ARRAY_CARRAY_RO NPY_ARRAY_FARRAY NPY_ARRAY_FARRAY_RO NPY_ARRAY_DEFAULT
NPY_ARRAY_IN_ARRAY NPY_ARRAY_OUT_ARRAY NPY_ARRAY_INOUT_ARRAY NPY_ARRAY_IN_FARRAY NPY_ARRAY_OUT_FARRAY NPY_ARRAY_INOUT_FARRAY
NPY_ARRAY_UPDATE_ALL
cdef enum: NPY_MAXDIMS
npy_intp NPY_MAX_ELSIZE
ctypedef void (*PyArray_VectorUnaryFunc)(void *, void *, npy_intp, void *, void *)
ctypedef struct PyArray_ArrayDescr: # shape is a tuple, but Cython doesn't support "tuple shape" # inside a non-PyObject declaration, so we have to declare it # as just a PyObject*. PyObject* shape
ctypedef struct PyArray_Descr: pass
ctypedef class numpy.dtype [object PyArray_Descr, check_size ignore]: # Use PyDataType_* macros when possible, however there are no macros # for accessing some of the fields, so some are defined. cdef PyTypeObject* typeobj cdef char kind cdef char type # Numpy sometimes mutates this without warning (e.g. it'll # sometimes change "|" to "<" in shared dtype objects on # little-endian machines). If this matters to you, use # PyArray_IsNativeByteOrder(dtype.byteorder) instead of # directly accessing this field. cdef char byteorder cdef char flags cdef int type_num cdef int itemsize "elsize" cdef int alignment cdef object fields cdef tuple names # Use PyDataType_HASSUBARRAY to test whether this field is # valid (the pointer can be NULL). Most users should access # this field via the inline helper method PyDataType_SHAPE. cdef PyArray_ArrayDescr* subarray
ctypedef class numpy.flatiter [object PyArrayIterObject, check_size ignore]: # Use through macros pass
ctypedef class numpy.broadcast [object PyArrayMultiIterObject, check_size ignore]: cdef int numiter cdef npy_intp size, index cdef int nd cdef npy_intp *dimensions cdef void **iters
ctypedef struct PyArrayObject: # For use in situations where ndarray can't replace PyArrayObject*, # like PyArrayObject**. pass
ctypedef class numpy.ndarray [object PyArrayObject, check_size ignore]: cdef __cythonbufferdefaults__ = {"mode": "strided"}
cdef: # Only taking a few of the most commonly used and stable fields. # One should use PyArray_* macros instead to access the C fields. char *data int ndim "nd" npy_intp *shape "dimensions" npy_intp *strides dtype descr # deprecated since NumPy 1.7 ! PyObject* base # NOT PUBLIC, DO NOT USE !
ctypedef unsigned char npy_bool
ctypedef signed char npy_byte ctypedef signed short npy_short ctypedef signed int npy_int ctypedef signed long npy_long ctypedef signed long long npy_longlong
ctypedef unsigned char npy_ubyte ctypedef unsigned short npy_ushort ctypedef unsigned int npy_uint ctypedef unsigned long npy_ulong ctypedef unsigned long long npy_ulonglong
ctypedef float npy_float ctypedef double npy_double ctypedef long double npy_longdouble
ctypedef signed char npy_int8 ctypedef signed short npy_int16 ctypedef signed int npy_int32 ctypedef signed long long npy_int64 ctypedef signed long long npy_int96 ctypedef signed long long npy_int128
ctypedef unsigned char npy_uint8 ctypedef unsigned short npy_uint16 ctypedef unsigned int npy_uint32 ctypedef unsigned long long npy_uint64 ctypedef unsigned long long npy_uint96 ctypedef unsigned long long npy_uint128
ctypedef float npy_float32 ctypedef double npy_float64 ctypedef long double npy_float80 ctypedef long double npy_float96 ctypedef long double npy_float128
ctypedef struct npy_cfloat: float real float imag
ctypedef struct npy_cdouble: double real double imag
ctypedef struct npy_clongdouble: long double real long double imag
ctypedef struct npy_complex64: float real float imag
ctypedef struct npy_complex128: double real double imag
ctypedef struct npy_complex160: long double real long double imag
ctypedef struct npy_complex192: long double real long double imag
ctypedef struct npy_complex256: long double real long double imag
ctypedef struct PyArray_Dims: npy_intp *ptr int len
int _import_array() except -1 # A second definition so _import_array isn't marked as used when we use it here. # Do not use - subject to change any time. int __pyx_import_array "_import_array"() except -1
# # Macros from ndarrayobject.h # bint PyArray_CHKFLAGS(ndarray m, int flags) nogil bint PyArray_IS_C_CONTIGUOUS(ndarray arr) nogil bint PyArray_IS_F_CONTIGUOUS(ndarray arr) nogil bint PyArray_ISCONTIGUOUS(ndarray m) nogil bint PyArray_ISWRITEABLE(ndarray m) nogil bint PyArray_ISALIGNED(ndarray m) nogil
int PyArray_NDIM(ndarray) nogil bint PyArray_ISONESEGMENT(ndarray) nogil bint PyArray_ISFORTRAN(ndarray) nogil int PyArray_FORTRANIF(ndarray) nogil
void* PyArray_DATA(ndarray) nogil char* PyArray_BYTES(ndarray) nogil
npy_intp* PyArray_DIMS(ndarray) nogil npy_intp* PyArray_STRIDES(ndarray) nogil npy_intp PyArray_DIM(ndarray, size_t) nogil npy_intp PyArray_STRIDE(ndarray, size_t) nogil
PyObject *PyArray_BASE(ndarray) nogil # returns borrowed reference! PyArray_Descr *PyArray_DESCR(ndarray) nogil # returns borrowed reference to dtype! int PyArray_FLAGS(ndarray) nogil npy_intp PyArray_ITEMSIZE(ndarray) nogil int PyArray_TYPE(ndarray arr) nogil
object PyArray_GETITEM(ndarray arr, void *itemptr) int PyArray_SETITEM(ndarray arr, void *itemptr, object obj) except -1
bint PyTypeNum_ISBOOL(int) nogil bint PyTypeNum_ISUNSIGNED(int) nogil bint PyTypeNum_ISSIGNED(int) nogil bint PyTypeNum_ISINTEGER(int) nogil bint PyTypeNum_ISFLOAT(int) nogil bint PyTypeNum_ISNUMBER(int) nogil bint PyTypeNum_ISSTRING(int) nogil bint PyTypeNum_ISCOMPLEX(int) nogil bint PyTypeNum_ISPYTHON(int) nogil bint PyTypeNum_ISFLEXIBLE(int) nogil bint PyTypeNum_ISUSERDEF(int) nogil bint PyTypeNum_ISEXTENDED(int) nogil bint PyTypeNum_ISOBJECT(int) nogil
bint PyDataType_ISBOOL(dtype) nogil bint PyDataType_ISUNSIGNED(dtype) nogil bint PyDataType_ISSIGNED(dtype) nogil bint PyDataType_ISINTEGER(dtype) nogil bint PyDataType_ISFLOAT(dtype) nogil bint PyDataType_ISNUMBER(dtype) nogil bint PyDataType_ISSTRING(dtype) nogil bint PyDataType_ISCOMPLEX(dtype) nogil bint PyDataType_ISPYTHON(dtype) nogil bint PyDataType_ISFLEXIBLE(dtype) nogil bint PyDataType_ISUSERDEF(dtype) nogil bint PyDataType_ISEXTENDED(dtype) nogil bint PyDataType_ISOBJECT(dtype) nogil bint PyDataType_HASFIELDS(dtype) nogil bint PyDataType_HASSUBARRAY(dtype) nogil
bint PyArray_ISBOOL(ndarray) nogil bint PyArray_ISUNSIGNED(ndarray) nogil bint PyArray_ISSIGNED(ndarray) nogil bint PyArray_ISINTEGER(ndarray) nogil bint PyArray_ISFLOAT(ndarray) nogil bint PyArray_ISNUMBER(ndarray) nogil bint PyArray_ISSTRING(ndarray) nogil bint PyArray_ISCOMPLEX(ndarray) nogil bint PyArray_ISPYTHON(ndarray) nogil bint PyArray_ISFLEXIBLE(ndarray) nogil bint PyArray_ISUSERDEF(ndarray) nogil bint PyArray_ISEXTENDED(ndarray) nogil bint PyArray_ISOBJECT(ndarray) nogil bint PyArray_HASFIELDS(ndarray) nogil
bint PyArray_ISVARIABLE(ndarray) nogil
bint PyArray_SAFEALIGNEDCOPY(ndarray) nogil bint PyArray_ISNBO(char) nogil # works on ndarray.byteorder bint PyArray_IsNativeByteOrder(char) nogil # works on ndarray.byteorder bint PyArray_ISNOTSWAPPED(ndarray) nogil bint PyArray_ISBYTESWAPPED(ndarray) nogil
bint PyArray_FLAGSWAP(ndarray, int) nogil
bint PyArray_ISCARRAY(ndarray) nogil bint PyArray_ISCARRAY_RO(ndarray) nogil bint PyArray_ISFARRAY(ndarray) nogil bint PyArray_ISFARRAY_RO(ndarray) nogil bint PyArray_ISBEHAVED(ndarray) nogil bint PyArray_ISBEHAVED_RO(ndarray) nogil
bint PyDataType_ISNOTSWAPPED(dtype) nogil bint PyDataType_ISBYTESWAPPED(dtype) nogil
bint PyArray_DescrCheck(object)
bint PyArray_Check(object) bint PyArray_CheckExact(object)
# Cannot be supported due to out arg: # bint PyArray_HasArrayInterfaceType(object, dtype, object, object&) # bint PyArray_HasArrayInterface(op, out)
bint PyArray_IsZeroDim(object) # Cannot be supported due to ## ## in macro: # bint PyArray_IsScalar(object, verbatim work) bint PyArray_CheckScalar(object) bint PyArray_IsPythonNumber(object) bint PyArray_IsPythonScalar(object) bint PyArray_IsAnyScalar(object) bint PyArray_CheckAnyScalar(object)
ndarray PyArray_GETCONTIGUOUS(ndarray) bint PyArray_SAMESHAPE(ndarray, ndarray) nogil npy_intp PyArray_SIZE(ndarray) nogil npy_intp PyArray_NBYTES(ndarray) nogil
object PyArray_FROM_O(object) object PyArray_FROM_OF(object m, int flags) object PyArray_FROM_OT(object m, int type) object PyArray_FROM_OTF(object m, int type, int flags) object PyArray_FROMANY(object m, int type, int min, int max, int flags) object PyArray_ZEROS(int nd, npy_intp* dims, int type, int fortran) object PyArray_EMPTY(int nd, npy_intp* dims, int type, int fortran) void PyArray_FILLWBYTE(object, int val) npy_intp PyArray_REFCOUNT(object) object PyArray_ContiguousFromAny(op, int, int min_depth, int max_depth) unsigned char PyArray_EquivArrTypes(ndarray a1, ndarray a2) bint PyArray_EquivByteorders(int b1, int b2) nogil object PyArray_SimpleNew(int nd, npy_intp* dims, int typenum) object PyArray_SimpleNewFromData(int nd, npy_intp* dims, int typenum, void* data) #object PyArray_SimpleNewFromDescr(int nd, npy_intp* dims, dtype descr) object PyArray_ToScalar(void* data, ndarray arr)
void* PyArray_GETPTR1(ndarray m, npy_intp i) nogil void* PyArray_GETPTR2(ndarray m, npy_intp i, npy_intp j) nogil void* PyArray_GETPTR3(ndarray m, npy_intp i, npy_intp j, npy_intp k) nogil void* PyArray_GETPTR4(ndarray m, npy_intp i, npy_intp j, npy_intp k, npy_intp l) nogil
# Cannot be supported due to out arg # void PyArray_DESCR_REPLACE(descr)
object PyArray_Copy(ndarray) object PyArray_FromObject(object op, int type, int min_depth, int max_depth) object PyArray_ContiguousFromObject(object op, int type, int min_depth, int max_depth) object PyArray_CopyFromObject(object op, int type, int min_depth, int max_depth)
object PyArray_Cast(ndarray mp, int type_num) object PyArray_Take(ndarray ap, object items, int axis) object PyArray_Put(ndarray ap, object items, object values)
void PyArray_ITER_RESET(flatiter it) nogil void PyArray_ITER_NEXT(flatiter it) nogil void PyArray_ITER_GOTO(flatiter it, npy_intp* destination) nogil void PyArray_ITER_GOTO1D(flatiter it, npy_intp ind) nogil void* PyArray_ITER_DATA(flatiter it) nogil bint PyArray_ITER_NOTDONE(flatiter it) nogil
void PyArray_MultiIter_RESET(broadcast multi) nogil void PyArray_MultiIter_NEXT(broadcast multi) nogil void PyArray_MultiIter_GOTO(broadcast multi, npy_intp dest) nogil void PyArray_MultiIter_GOTO1D(broadcast multi, npy_intp ind) nogil void* PyArray_MultiIter_DATA(broadcast multi, npy_intp i) nogil void PyArray_MultiIter_NEXTi(broadcast multi, npy_intp i) nogil bint PyArray_MultiIter_NOTDONE(broadcast multi) nogil
# Functions from __multiarray_api.h
# Functions taking dtype and returning object/ndarray are disabled # for now as they steal dtype references. I'm conservative and disable # more than is probably needed until it can be checked further. int PyArray_SetNumericOps (object) except -1 object PyArray_GetNumericOps () int PyArray_INCREF (ndarray) except * # uses PyArray_Item_INCREF... int PyArray_XDECREF (ndarray) except * # uses PyArray_Item_DECREF... void PyArray_SetStringFunction (object, int) dtype PyArray_DescrFromType (int) object PyArray_TypeObjectFromType (int) char * PyArray_Zero (ndarray) char * PyArray_One (ndarray) #object PyArray_CastToType (ndarray, dtype, int) int PyArray_CastTo (ndarray, ndarray) except -1 int PyArray_CastAnyTo (ndarray, ndarray) except -1 int PyArray_CanCastSafely (int, int) # writes errors npy_bool PyArray_CanCastTo (dtype, dtype) # writes errors int PyArray_ObjectType (object, int) except 0 dtype PyArray_DescrFromObject (object, dtype) #ndarray* PyArray_ConvertToCommonType (object, int *) dtype PyArray_DescrFromScalar (object) dtype PyArray_DescrFromTypeObject (object) npy_intp PyArray_Size (object) #object PyArray_Scalar (void *, dtype, object) #object PyArray_FromScalar (object, dtype) void PyArray_ScalarAsCtype (object, void *) #int PyArray_CastScalarToCtype (object, void *, dtype) #int PyArray_CastScalarDirect (object, dtype, void *, int) object PyArray_ScalarFromObject (object) #PyArray_VectorUnaryFunc * PyArray_GetCastFunc (dtype, int) object PyArray_FromDims (int, int *, int) #object PyArray_FromDimsAndDataAndDescr (int, int *, dtype, char *) #object PyArray_FromAny (object, dtype, int, int, int, object) object PyArray_EnsureArray (object) object PyArray_EnsureAnyArray (object) #object PyArray_FromFile (stdio.FILE *, dtype, npy_intp, char *) #object PyArray_FromString (char *, npy_intp, dtype, npy_intp, char *) #object PyArray_FromBuffer (object, dtype, npy_intp, npy_intp) #object PyArray_FromIter (object, dtype, npy_intp) object PyArray_Return (ndarray) #object PyArray_GetField (ndarray, dtype, int) #int PyArray_SetField (ndarray, dtype, int, object) except -1 object PyArray_Byteswap (ndarray, npy_bool) object PyArray_Resize (ndarray, PyArray_Dims *, int, NPY_ORDER) int PyArray_MoveInto (ndarray, ndarray) except -1 int PyArray_CopyInto (ndarray, ndarray) except -1 int PyArray_CopyAnyInto (ndarray, ndarray) except -1 int PyArray_CopyObject (ndarray, object) except -1 object PyArray_NewCopy (ndarray, NPY_ORDER) object PyArray_ToList (ndarray) object PyArray_ToString (ndarray, NPY_ORDER) int PyArray_ToFile (ndarray, stdio.FILE *, char *, char *) except -1 int PyArray_Dump (object, object, int) except -1 object PyArray_Dumps (object, int) int PyArray_ValidType (int) # Cannot error void PyArray_UpdateFlags (ndarray, int) object PyArray_New (type, int, npy_intp *, int, npy_intp *, void *, int, int, object) #object PyArray_NewFromDescr (type, dtype, int, npy_intp *, npy_intp *, void *, int, object) #dtype PyArray_DescrNew (dtype) dtype PyArray_DescrNewFromType (int) double PyArray_GetPriority (object, double) # clears errors as of 1.25 object PyArray_IterNew (object) object PyArray_MultiIterNew (int, ...)
int PyArray_PyIntAsInt (object) except? -1 npy_intp PyArray_PyIntAsIntp (object) int PyArray_Broadcast (broadcast) except -1 void PyArray_FillObjectArray (ndarray, object) except * int PyArray_FillWithScalar (ndarray, object) except -1 npy_bool PyArray_CheckStrides (int, int, npy_intp, npy_intp, npy_intp *, npy_intp *) dtype PyArray_DescrNewByteorder (dtype, char) object PyArray_IterAllButAxis (object, int *) #object PyArray_CheckFromAny (object, dtype, int, int, int, object) #object PyArray_FromArray (ndarray, dtype, int) object PyArray_FromInterface (object) object PyArray_FromStructInterface (object) #object PyArray_FromArrayAttr (object, dtype, object) #NPY_SCALARKIND PyArray_ScalarKind (int, ndarray*) int PyArray_CanCoerceScalar (int, int, NPY_SCALARKIND) object PyArray_NewFlagsObject (object) npy_bool PyArray_CanCastScalar (type, type) #int PyArray_CompareUCS4 (npy_ucs4 *, npy_ucs4 *, register size_t) int PyArray_RemoveSmallest (broadcast) except -1 int PyArray_ElementStrides (object) void PyArray_Item_INCREF (char *, dtype) except * void PyArray_Item_XDECREF (char *, dtype) except * object PyArray_FieldNames (object) object PyArray_Transpose (ndarray, PyArray_Dims *) object PyArray_TakeFrom (ndarray, object, int, ndarray, NPY_CLIPMODE) object PyArray_PutTo (ndarray, object, object, NPY_CLIPMODE) object PyArray_PutMask (ndarray, object, object) object PyArray_Repeat (ndarray, object, int) object PyArray_Choose (ndarray, object, ndarray, NPY_CLIPMODE) int PyArray_Sort (ndarray, int, NPY_SORTKIND) except -1 object PyArray_ArgSort (ndarray, int, NPY_SORTKIND) object PyArray_SearchSorted (ndarray, object, NPY_SEARCHSIDE, PyObject *) object PyArray_ArgMax (ndarray, int, ndarray) object PyArray_ArgMin (ndarray, int, ndarray) object PyArray_Reshape (ndarray, object) object PyArray_Newshape (ndarray, PyArray_Dims *, NPY_ORDER) object PyArray_Squeeze (ndarray) #object PyArray_View (ndarray, dtype, type) object PyArray_SwapAxes (ndarray, int, int) object PyArray_Max (ndarray, int, ndarray) object PyArray_Min (ndarray, int, ndarray) object PyArray_Ptp (ndarray, int, ndarray) object PyArray_Mean (ndarray, int, int, ndarray) object PyArray_Trace (ndarray, int, int, int, int, ndarray) object PyArray_Diagonal (ndarray, int, int, int) object PyArray_Clip (ndarray, object, object, ndarray) object PyArray_Conjugate (ndarray, ndarray) object PyArray_Nonzero (ndarray) object PyArray_Std (ndarray, int, int, ndarray, int) object PyArray_Sum (ndarray, int, int, ndarray) object PyArray_CumSum (ndarray, int, int, ndarray) object PyArray_Prod (ndarray, int, int, ndarray) object PyArray_CumProd (ndarray, int, int, ndarray) object PyArray_All (ndarray, int, ndarray) object PyArray_Any (ndarray, int, ndarray) object PyArray_Compress (ndarray, object, int, ndarray) object PyArray_Flatten (ndarray, NPY_ORDER) object PyArray_Ravel (ndarray, NPY_ORDER) npy_intp PyArray_MultiplyList (npy_intp *, int) int PyArray_MultiplyIntList (int *, int) void * PyArray_GetPtr (ndarray, npy_intp*) int PyArray_CompareLists (npy_intp *, npy_intp *, int) #int PyArray_AsCArray (object*, void *, npy_intp *, int, dtype) #int PyArray_As1D (object*, char **, int *, int) #int PyArray_As2D (object*, char ***, int *, int *, int) int PyArray_Free (object, void *) #int PyArray_Converter (object, object*) int PyArray_IntpFromSequence (object, npy_intp *, int) except -1 object PyArray_Concatenate (object, int) object PyArray_InnerProduct (object, object) object PyArray_MatrixProduct (object, object) object PyArray_CopyAndTranspose (object) object PyArray_Correlate (object, object, int) int PyArray_TypestrConvert (int, int) #int PyArray_DescrConverter (object, dtype*) except 0 #int PyArray_DescrConverter2 (object, dtype*) except 0 int PyArray_IntpConverter (object, PyArray_Dims *) except 0 #int PyArray_BufferConverter (object, chunk) except 0 int PyArray_AxisConverter (object, int *) except 0 int PyArray_BoolConverter (object, npy_bool *) except 0 int PyArray_ByteorderConverter (object, char *) except 0 int PyArray_OrderConverter (object, NPY_ORDER *) except 0 unsigned char PyArray_EquivTypes (dtype, dtype) # clears errors #object PyArray_Zeros (int, npy_intp *, dtype, int) #object PyArray_Empty (int, npy_intp *, dtype, int) object PyArray_Where (object, object, object) object PyArray_Arange (double, double, double, int) #object PyArray_ArangeObj (object, object, object, dtype) int PyArray_SortkindConverter (object, NPY_SORTKIND *) except 0 object PyArray_LexSort (object, int) object PyArray_Round (ndarray, int, ndarray) unsigned char PyArray_EquivTypenums (int, int) int PyArray_RegisterDataType (dtype) except -1 int PyArray_RegisterCastFunc (dtype, int, PyArray_VectorUnaryFunc *) except -1 int PyArray_RegisterCanCast (dtype, int, NPY_SCALARKIND) except -1 #void PyArray_InitArrFuncs (PyArray_ArrFuncs *) object PyArray_IntTupleFromIntp (int, npy_intp *) int PyArray_TypeNumFromName (char *) int PyArray_ClipmodeConverter (object, NPY_CLIPMODE *) except 0 #int PyArray_OutputConverter (object, ndarray*) except 0 object PyArray_BroadcastToShape (object, npy_intp *, int) void _PyArray_SigintHandler (int) void* _PyArray_GetSigintBuf () #int PyArray_DescrAlignConverter (object, dtype*) except 0 #int PyArray_DescrAlignConverter2 (object, dtype*) except 0 int PyArray_SearchsideConverter (object, void *) except 0 object PyArray_CheckAxis (ndarray, int *, int) npy_intp PyArray_OverflowMultiplyList (npy_intp *, int) int PyArray_CompareString (char *, char *, size_t) int PyArray_SetBaseObject(ndarray, base) except -1 # NOTE: steals a reference to base! Use "set_array_base()" instead.
# Typedefs that matches the runtime dtype objects in# the numpy module.
# The ones that are commented out needs an IFDEF function# in Cython to enable them only on the right systems.
ctypedef npy_int8 int8_tctypedef npy_int16 int16_tctypedef npy_int32 int32_tctypedef npy_int64 int64_t#ctypedef npy_int96 int96_t#ctypedef npy_int128 int128_t
ctypedef npy_uint8 uint8_tctypedef npy_uint16 uint16_tctypedef npy_uint32 uint32_tctypedef npy_uint64 uint64_t#ctypedef npy_uint96 uint96_t#ctypedef npy_uint128 uint128_t
ctypedef npy_float32 float32_tctypedef npy_float64 float64_t#ctypedef npy_float80 float80_t#ctypedef npy_float128 float128_t
ctypedef float complex complex64_tctypedef double complex complex128_t
# The int types are mapped a bit surprising --# numpy.int corresponds to 'l' and numpy.long to 'q'ctypedef npy_long int_tctypedef npy_longlong longlong_t
ctypedef npy_ulong uint_tctypedef npy_ulonglong ulonglong_t
ctypedef npy_intp intp_tctypedef npy_uintp uintp_t
ctypedef npy_double float_tctypedef npy_double double_tctypedef npy_longdouble longdouble_t
ctypedef npy_cfloat cfloat_tctypedef npy_cdouble cdouble_tctypedef npy_clongdouble clongdouble_t
ctypedef npy_cdouble complex_t
cdef inline object PyArray_MultiIterNew1(a): return PyArray_MultiIterNew(1, <void*>a)
cdef inline object PyArray_MultiIterNew2(a, b): return PyArray_MultiIterNew(2, <void*>a, <void*>b)
cdef inline object PyArray_MultiIterNew3(a, b, c): return PyArray_MultiIterNew(3, <void*>a, <void*>b, <void*> c)
cdef inline object PyArray_MultiIterNew4(a, b, c, d): return PyArray_MultiIterNew(4, <void*>a, <void*>b, <void*>c, <void*> d)
cdef inline object PyArray_MultiIterNew5(a, b, c, d, e): return PyArray_MultiIterNew(5, <void*>a, <void*>b, <void*>c, <void*> d, <void*> e)
cdef inline tuple PyDataType_SHAPE(dtype d): if PyDataType_HASSUBARRAY(d): return <tuple>d.subarray.shape else: return ()
cdef extern from "numpy/ndarrayobject.h": PyTypeObject PyTimedeltaArrType_Type PyTypeObject PyDatetimeArrType_Type ctypedef int64_t npy_timedelta ctypedef int64_t npy_datetime
cdef extern from "numpy/ndarraytypes.h": ctypedef struct PyArray_DatetimeMetaData: NPY_DATETIMEUNIT base int64_t num
cdef extern from "numpy/arrayscalars.h":
# abstract types ctypedef class numpy.generic [object PyObject]: pass ctypedef class numpy.number [object PyObject]: pass ctypedef class numpy.integer [object PyObject]: pass ctypedef class numpy.signedinteger [object PyObject]: pass ctypedef class numpy.unsignedinteger [object PyObject]: pass ctypedef class numpy.inexact [object PyObject]: pass ctypedef class numpy.floating [object PyObject]: pass ctypedef class numpy.complexfloating [object PyObject]: pass ctypedef class numpy.flexible [object PyObject]: pass ctypedef class numpy.character [object PyObject]: pass
ctypedef struct PyDatetimeScalarObject: # PyObject_HEAD npy_datetime obval PyArray_DatetimeMetaData obmeta
ctypedef struct PyTimedeltaScalarObject: # PyObject_HEAD npy_timedelta obval PyArray_DatetimeMetaData obmeta
ctypedef enum NPY_DATETIMEUNIT: NPY_FR_Y NPY_FR_M NPY_FR_W NPY_FR_D NPY_FR_B NPY_FR_h NPY_FR_m NPY_FR_s NPY_FR_ms NPY_FR_us NPY_FR_ns NPY_FR_ps NPY_FR_fs NPY_FR_as NPY_FR_GENERIC
## ufunc API#
cdef extern from "numpy/ufuncobject.h":
ctypedef void (*PyUFuncGenericFunction) (char **, npy_intp *, npy_intp *, void *)
ctypedef class numpy.ufunc [object PyUFuncObject, check_size ignore]: cdef: int nin, nout, nargs int identity PyUFuncGenericFunction *functions void **data int ntypes int check_return char *name char *types char *doc void *ptr PyObject *obj PyObject *userloops
cdef enum: PyUFunc_Zero PyUFunc_One PyUFunc_None UFUNC_ERR_IGNORE UFUNC_ERR_WARN UFUNC_ERR_RAISE UFUNC_ERR_CALL UFUNC_ERR_PRINT UFUNC_ERR_LOG UFUNC_MASK_DIVIDEBYZERO UFUNC_MASK_OVERFLOW UFUNC_MASK_UNDERFLOW UFUNC_MASK_INVALID UFUNC_SHIFT_DIVIDEBYZERO UFUNC_SHIFT_OVERFLOW UFUNC_SHIFT_UNDERFLOW UFUNC_SHIFT_INVALID UFUNC_FPE_DIVIDEBYZERO UFUNC_FPE_OVERFLOW UFUNC_FPE_UNDERFLOW UFUNC_FPE_INVALID UFUNC_ERR_DEFAULT UFUNC_ERR_DEFAULT2
object PyUFunc_FromFuncAndData(PyUFuncGenericFunction *, void **, char *, int, int, int, int, char *, char *, int) int PyUFunc_RegisterLoopForType(ufunc, int, PyUFuncGenericFunction, int *, void *) except -1 void PyUFunc_f_f_As_d_d \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_d_d \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_f_f \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_g_g \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_F_F_As_D_D \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_F_F \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_D_D \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_G_G \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_O_O \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_ff_f_As_dd_d \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_ff_f \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_dd_d \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_gg_g \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_FF_F_As_DD_D \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_DD_D \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_FF_F \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_GG_G \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_OO_O \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_O_O_method \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_OO_O_method \ (char **, npy_intp *, npy_intp *, void *) void PyUFunc_On_Om \ (char **, npy_intp *, npy_intp *, void *) int PyUFunc_GetPyValues \ (char *, int *, int *, PyObject **) int PyUFunc_checkfperr \ (int, PyObject *, int *) void PyUFunc_clearfperr() int PyUFunc_getfperr() int PyUFunc_handlefperr \ (int, PyObject *, int, int *) except -1 int PyUFunc_ReplaceLoopBySignature \ (ufunc, PyUFuncGenericFunction, int *, PyUFuncGenericFunction *) object PyUFunc_FromFuncAndDataAndSignature \ (PyUFuncGenericFunction *, void **, char *, int, int, int, int, char *, char *, int, char *)
int _import_umath() except -1
cdef inline void set_array_base(ndarray arr, object base): Py_INCREF(base) # important to do this before stealing the reference below! PyArray_SetBaseObject(arr, base)
cdef inline object get_array_base(ndarray arr): base = PyArray_BASE(arr) if base is NULL: return None return <object>base
# Versions of the import_* functions which are more suitable for# Cython code.cdef inline int import_array() except -1: try: __pyx_import_array() except Exception: raise ImportError("numpy.core.multiarray failed to import")
cdef inline int import_umath() except -1: try: _import_umath() except Exception: raise ImportError("numpy.core.umath failed to import")
cdef inline int import_ufunc() except -1: try: _import_umath() except Exception: raise ImportError("numpy.core.umath failed to import")
cdef extern from *: # Leave a marker that the NumPy declarations came from this file # See https://github.com/cython/cython/issues/3573 """ /* NumPy API declarations from "numpy/__init__.pxd" */ """
cdef inline bint is_timedelta64_object(object obj): """ Cython equivalent of `isinstance(obj, np.timedelta64)`
Parameters ---------- obj : object
Returns ------- bool """ return PyObject_TypeCheck(obj, &PyTimedeltaArrType_Type)
cdef inline bint is_datetime64_object(object obj): """ Cython equivalent of `isinstance(obj, np.datetime64)`
Parameters ---------- obj : object
Returns ------- bool """ return PyObject_TypeCheck(obj, &PyDatetimeArrType_Type)
cdef inline npy_datetime get_datetime64_value(object obj) nogil: """ returns the int64 value underlying scalar numpy datetime64 object
Note that to interpret this as a datetime, the corresponding unit is also needed. That can be found using `get_datetime64_unit`. """ return (<PyDatetimeScalarObject*>obj).obval
cdef inline npy_timedelta get_timedelta64_value(object obj) nogil: """ returns the int64 value underlying scalar numpy timedelta64 object """ return (<PyTimedeltaScalarObject*>obj).obval
cdef inline NPY_DATETIMEUNIT get_datetime64_unit(object obj) nogil: """ returns the unit part of the dtype for a numpy datetime64 object. """ return <NPY_DATETIMEUNIT>(<PyDatetimeScalarObject*>obj).obmeta.base123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461"""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 providedwith 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 withTAB-completion and introspection capabilities. See below for furtherinstructions.
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 isparticularly true if you see the line "Help on ufunc object:" at the topof 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 ournp.info() function does.
To search for documents containing a keyword, do::
>>> np.lookfor('keyword') ... # doctest: +SKIP
General-purpose documents like a glossary and help on the basic conceptsof numpy are available under the ``doc`` sub-module::
>>> from numpy import doc >>> help(doc) ... # doctest: +SKIP
Available subpackages---------------------lib Basic functions used by several sub-packages.random Core Random Toolslinalg Core Linear Algebra Toolsfft Core FFT routinespolynomial Polynomial toolstesting NumPy testing toolsdistutils Enhancements to distutils with support for Fortran compilers support and more (for Python <= 3.11).
Utilities---------test Run numpy unittestsshow_config Show numpy build configurationmatlib Make everything matrices.__version__ NumPy version string
Viewing documentation using IPython-----------------------------------
Start IPython and import `numpy` usually under the alias ``np``: `importnumpy as np`. Then, directly past or use the ``%cpaste`` magic to pasteexamples 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 narrowdown the list. To view the docstring for a function, use``np.cos?<ENTER>`` (to view the docstring) and ``np.cos??<ENTER>`` (to viewthe 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 oftenavailable as array methods, i.e. ``x = np.array([1,2,3]); x.sort()``.Exceptions to this rule are documented.
"""import sysimport warnings
from ._globals import _NoValue, _CopyMode# These exceptions were moved in 1.25 and are hidden from __dir__()from .exceptions import ( ComplexWarning, ModuleDeprecationWarning, VisibleDeprecationWarning, TooHardError, AxisError)
# If a version with git hash was stored, use that insteadfrom . import versionfrom .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
__all__ = [ 'exceptions', 'ModuleDeprecationWarning', 'VisibleDeprecationWarning', 'ComplexWarning', 'TooHardError', 'AxisError']
# mapping of {name: (value, deprecation_msg)} __deprecated_attrs__ = {}
from . import core from .core import * from . import compat from . import exceptions from . import dtypes from . import lib # NOTE: to be revisited following future namespace cleanup. # See gh-14454 and gh-15672 for discussion. from .lib import *
from . import linalg from . import fft from . import polynomial from . import random from . import ctypeslib from . import ma from . import matrixlib as _mat from .matrixlib import *
# Deprecations introduced in NumPy 1.20.0, 2020-06-06 import builtins as _builtins
_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. ("bool", _specific_msg.format("bool_")), ("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 }
# Future warning introduced in NumPy 1.24.0, 2022-11-17 _msg = ( "`np.{n}` is a deprecated alias for `{an}`. (Deprecated NumPy 1.24)")
# Some of these are awkward (since `np.str` may be preferable in the long # term), but overall the names ending in 0 seem undesirable _type_info = [ ("bool8", bool_, "np.bool_"), ("int0", intp, "np.intp"), ("uint0", uintp, "np.uintp"), ("str0", str_, "np.str_"), ("bytes0", bytes_, "np.bytes_"), ("void0", void, "np.void"), ("object0", object_, "`np.object0` is a deprecated alias for `np.object_`. " "`object` can be used instead. (Deprecated NumPy 1.24)")]
# 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__ = {"bool", "long", "ulong", "str", "bytes", "object"}
__deprecated_attrs__.update({ n: (alias, _msg.format(n=n, an=an)) for n, alias, an in _type_info})
import math
__deprecated_attrs__['math'] = (math, "`np.math` is a deprecated alias for the standard library `math` " "module (Deprecated Numpy 1.25). Replace usages of `np.math` with " "`math`")
del math, _msg, _type_info
from .core import abs # now that numpy modules are imported, can initialize limits core.getlimits._register_known_types()
__all__.extend(['__version__', 'show_config']) __all__.extend(core.__all__) __all__.extend(_mat.__all__) __all__.extend(lib.__all__) __all__.extend(['linalg', 'fft', 'random', 'ctypeslib', 'ma'])
# Remove min and max from __all__ to avoid `from numpy import *` override # the builtins min/max. Temporary fix for 1.25.x/1.26.x, see gh-24229. __all__.remove('min') __all__.remove('max') __all__.remove('round')
# Remove one of the two occurrences of `issubdtype`, which is exposed as # both `numpy.core.issubdtype` and `numpy.lib.issubdtype`. __all__.remove('issubdtype')
# These are exported by np.core, but are replaced by the builtins below # remove them to ensure that we don't end up with `np.long == np.int_`, # which would be a breaking change. del long, unicode __all__.remove('long') __all__.remove('unicode')
# Remove things that are in the numpy.lib but not in the numpy namespace # Note that there is a test (numpy/tests/test_public_api.py:test_numpy_namespace) # that prevents adding more things to the main namespace by accident. # The list below will grow until the `from .lib import *` fixme above is # taken care of __all__.remove('Arrayterator') del Arrayterator
# These names were removed in NumPy 1.20. For at least one release, # attempts to access these names in the numpy namespace will trigger # a warning, and calling the function will raise an exception. _financial_names = ['fv', 'ipmt', 'irr', 'mirr', 'nper', 'npv', 'pmt', 'ppmt', 'pv', 'rate'] __expired_functions__ = { name: (f'In accordance with NEP 32, the function {name} was removed ' 'from NumPy version 1.20. A replacement for this function ' 'is available in the numpy_financial library: ' 'https://pypi.org/project/numpy-financial') for name in _financial_names}
# 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")
# oldnumeric and numarray were removed in 1.9. In case some packages import # but do not use them, we define them here for backward compatibility. oldnumeric = 'removed' numarray = 'removed'
def __getattr__(attr): # Warn for expired attributes, and return a dummy function # that always raises an exception. import warnings import math try: msg = __expired_functions__[attr] except KeyError: pass else: warnings.warn(msg, DeprecationWarning, stacklevel=2)
def _expired(*args, **kwds): raise RuntimeError(msg)
return _expired
# Emit warnings for deprecated attributes try: val, msg = __deprecated_attrs__[attr] except KeyError: pass else: warnings.warn(msg, DeprecationWarning, stacklevel=2) return val
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 == 'testing': import numpy.testing as testing return testing elif attr == 'Tester': "Removed in NumPy 1.25.0" raise RuntimeError("Tester was removed in NumPy 1.25.")
raise AttributeError("module {!r} has no attribute " "{!r}".format(__name__, attr))
def __dir__(): public_symbols = globals().keys() | {'testing'} public_symbols -= { "core", "matrixlib", # These were moved in 1.25 and may be deprecated eventually: "ModuleDeprecationWarning", "VisibleDeprecationWarning", "ComplexWarning", "TooHardError", "AxisError" } 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
# 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 import os 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_hugepages 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_hugepages = 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)
# Note that this will currently only make a difference on Linux core.multiarray._set_madvise_hugepage(use_hugepage) del use_hugepage
# 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()
# default to "weak" promotion for "NumPy 2". core._set_promotion_state( os.environ.get("NPY_PROMOTION_STATE", "weak" if _using_numpy2_behavior() else "legacy"))
# 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
# Remove symbols imported for internal usedel sys, warnings123456789101112131415""" Distributor init file
Distributors: you can add custom code here to support particular distributionsof numpy.
For example, this is a good place to put any BLAS/LAPACK initialization code.
The numpy standard source distribution will not put code in this file, so youcan safely replace this file with your own version."""
try: from . import _distributor_init_localexcept ImportError: pass1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495"""Module defining global singleton classes.
This module raises a RuntimeError if an attempt to reload it is made. In thatway the identities of the classes defined here are fixed and will remain soeven if numpy itself is reloaded. In particular, a function like the followingwill still work correctly after numpy is reloaded::
def foo(arg=np._NoValue): if arg is np._NoValue: ...
That was not the case when the singleton classes were defined in the numpy``__init__.py`` file. See gh-7844 for a discussion of the reload problem thatmotivated this module.
"""import enum
from ._utils import set_module as _set_module
__all__ = ['_NoValue', '_CopyMode']
# Disallow reloading this module so as to preserve the identities of the# classes defined here.if '_is_loaded' in globals(): raise RuntimeError('Reloading numpy._globals is not allowed')_is_loaded = True
class _NoValueType: """Special keyword value.
The instance of this class may be used as the default value assigned to a keyword if no other obvious default (e.g., `None`) is suitable,
Common reasons for using this keyword are:
- A new keyword is added to a function, and that function forwards its inputs to another function or method which can be defined outside of NumPy. For example, ``np.std(x)`` calls ``x.std``, so when a ``keepdims`` keyword was added that could only be forwarded if the user explicitly specified ``keepdims``; downstream array libraries may not have added the same keyword, so adding ``x.std(..., keepdims=keepdims)`` unconditionally could have broken previously working code. - A keyword is being deprecated, and a deprecation warning must only be emitted when the keyword is used.
""" __instance = None def __new__(cls): # ensure that only one instance exists if not cls.__instance: cls.__instance = super().__new__(cls) return cls.__instance
def __repr__(self): return "<no value>"
_NoValue = _NoValueType()
@_set_module("numpy")class _CopyMode(enum.Enum): """ An enumeration for the copy modes supported by numpy.copy() and numpy.array(). The following three modes are supported,
- ALWAYS: This means that a deep copy of the input array will always be taken. - IF_NEEDED: This means that a deep copy of the input array will be taken only if necessary. - NEVER: This means that the deep copy will never be taken. If a copy cannot be avoided then a `ValueError` will be raised.
Note that the buffer-protocol could in theory do copies. NumPy currently assumes an object exporting the buffer protocol will never do this. """
ALWAYS = True IF_NEEDED = False NEVER = 2
def __bool__(self): # For backwards compatibility if self == _CopyMode.ALWAYS: return True
if self == _CopyMode.IF_NEEDED: return False
raise ValueError(f"{self} is neither True nor False.")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207"""Pytest test running.
This module implements the ``test()`` function for NumPy modules. The usualboiler plate for doing that is to put the following in the module``__init__.py`` file::
from numpy._pytesttester import PytestTester test = PytestTester(__name__) del PytestTester
Warnings filtering and other runtime settings should be dealt with in the``pytest.ini`` file in the numpy repo root. The behavior of the test depends onwhether or not that file is found as follows:
* ``pytest.ini`` is present (develop mode) All warnings except those explicitly filtered out are raised as error.* ``pytest.ini`` is absent (release mode) DeprecationWarnings and PendingDeprecationWarnings are ignored, other warnings are passed through.
In practice, tests run from the numpy repo are run in develop mode. Thatincludes the standard ``python runtests.py`` invocation.
This module is imported by every numpy subpackage, so lies at the top level tosimplify circular import issues. For the same reason, it contains no numpyimports at module scope, instead importing numpy within function calls."""import sysimport os
__all__ = ['PytestTester']
def _show_numpy_info(): import numpy as np
print("NumPy version %s" % np.__version__) relaxed_strides = np.ones((10, 1), order="C").flags.f_contiguous print("NumPy relaxed strides checking option:", relaxed_strides) info = np.lib.utils._opt_info() print("NumPy CPU features: ", (info if info else 'nothing enabled'))
class PytestTester: """ Pytest test runner.
A test function is typically added to a package's __init__.py like so::
from numpy._pytesttester import PytestTester test = PytestTester(__name__).test del PytestTester
Calling this test function finds and runs all tests associated with the module and all its sub-modules.
Attributes ---------- module_name : str Full path to the package to test.
Parameters ---------- module_name : module name The name of the module to test.
Notes ----- Unlike the previous ``nose``-based implementation, this class is not publicly exposed as it performs some ``numpy``-specific warning suppression.
""" def __init__(self, module_name): self.module_name = module_name
def __call__(self, label='fast', verbose=1, extra_argv=None, doctests=False, coverage=False, durations=-1, tests=None): """ Run tests for module using pytest.
Parameters ---------- label : {'fast', 'full'}, optional Identifies the tests to run. When set to 'fast', tests decorated with `pytest.mark.slow` are skipped, when 'full', the slow marker is ignored. verbose : int, optional Verbosity value for test outputs, in the range 1-3. Default is 1. extra_argv : list, optional List with any extra arguments to pass to pytests. doctests : bool, optional .. note:: Not supported coverage : bool, optional If True, report coverage of NumPy code. Default is False. Requires installation of (pip) pytest-cov. durations : int, optional If < 0, do nothing, If 0, report time of all tests, if > 0, report the time of the slowest `timer` tests. Default is -1. tests : test or list of tests Tests to be executed with pytest '--pyargs'
Returns ------- result : bool Return True on success, false otherwise.
Notes ----- Each NumPy module exposes `test` in its namespace to run all tests for it. For example, to run all tests for numpy.lib:
>>> np.lib.test() #doctest: +SKIP
Examples -------- >>> result = np.lib.test() #doctest: +SKIP ... 1023 passed, 2 skipped, 6 deselected, 1 xfailed in 10.39 seconds >>> result True
""" import pytest import warnings
module = sys.modules[self.module_name] module_path = os.path.abspath(module.__path__[0])
# setup the pytest arguments pytest_args = ["-l"]
# offset verbosity. The "-q" cancels a "-v". pytest_args += ["-q"]
if sys.version_info < (3, 12): with warnings.catch_warnings(): warnings.simplefilter("always") # Filter out distutils cpu warnings (could be localized to # distutils tests). ASV has problems with top level import, # so fetch module for suppression here. from numpy.distutils import cpuinfo
with warnings.catch_warnings(record=True): # Ignore the warning from importing the array_api submodule. This # warning is done on import, so it would break pytest collection, # but importing it early here prevents the warning from being # issued when it imported again. import numpy.array_api
# Filter out annoying import messages. Want these in both develop and # release mode. pytest_args += [ "-W ignore:Not importing directory", "-W ignore:numpy.dtype size changed", "-W ignore:numpy.ufunc size changed", "-W ignore::UserWarning:cpuinfo", ]
# When testing matrices, ignore their PendingDeprecationWarnings pytest_args += [ "-W ignore:the matrix subclass is not", "-W ignore:Importing from numpy.matlib is", ]
if doctests: pytest_args += ["--doctest-modules"]
if extra_argv: pytest_args += list(extra_argv)
if verbose > 1: pytest_args += ["-" + "v"*(verbose - 1)]
if coverage: pytest_args += ["--cov=" + module_path]
if label == "fast": # not importing at the top level to avoid circular import of module from numpy.testing import IS_PYPY if IS_PYPY: pytest_args += ["-m", "not slow and not slow_pypy"] else: pytest_args += ["-m", "not slow"]
elif label != "full": pytest_args += ["-m", label]
if durations >= 0: pytest_args += ["--durations=%s" % durations]
if tests is None: tests = [self.module_name]
pytest_args += ["--pyargs"] + list(tests)
# run tests. _show_numpy_info()
try: code = pytest.main(pytest_args) except SystemExit as exc: code = exc.code
return code == 0123456789101112131415161718from collections.abc import Iterablefrom typing import Literal as L
__all__: list[str]
class PytestTester: module_name: str def __init__(self, module_name: str) -> None: ... def __call__( self, label: L["fast", "full"] = ..., verbose: int = ..., extra_argv: None | Iterable[str] = ..., doctests: L[False] = ..., coverage: bool = ..., durations: int = ..., tests: None | Iterable[str] = ..., ) -> bool: ...123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138"""Pytest configuration and fixtures for the Numpy test suite."""import osimport tempfile
import hypothesisimport pytestimport numpy
from numpy.core._multiarray_tests import get_fpu_mode
_old_fpu_mode = None_collect_results = {}
# Use a known and persistent tmpdir for hypothesis' caches, which# can be automatically cleared by the OS or user.hypothesis.configuration.set_hypothesis_home_dir( os.path.join(tempfile.gettempdir(), ".hypothesis"))
# We register two custom profiles for Numpy - for details see# https://hypothesis.readthedocs.io/en/latest/settings.html# The first is designed for our own CI runs; the latter also # forces determinism and is designed for use via np.test()hypothesis.settings.register_profile( name="numpy-profile", deadline=None, print_blob=True,)hypothesis.settings.register_profile( name="np.test() profile", deadline=None, print_blob=True, database=None, derandomize=True, suppress_health_check=list(hypothesis.HealthCheck),)# Note that the default profile is chosen based on the presence # of pytest.ini, but can be overridden by passing the # --hypothesis-profile=NAME argument to pytest._pytest_ini = os.path.join(os.path.dirname(__file__), "..", "pytest.ini")hypothesis.settings.load_profile( "numpy-profile" if os.path.isfile(_pytest_ini) else "np.test() profile")
# The experimentalAPI is used in _umath_testsos.environ["NUMPY_EXPERIMENTAL_DTYPE_API"] = "1"
def pytest_configure(config): config.addinivalue_line("markers", "valgrind_error: Tests that are known to error under valgrind.") config.addinivalue_line("markers", "leaks_references: Tests that are known to leak references.") config.addinivalue_line("markers", "slow: Tests that are very slow.") config.addinivalue_line("markers", "slow_pypy: Tests that are very slow on pypy.")
def pytest_addoption(parser): parser.addoption("--available-memory", action="store", default=None, help=("Set amount of memory available for running the " "test suite. This can result to tests requiring " "especially large amounts of memory to be skipped. " "Equivalent to setting environment variable " "NPY_AVAILABLE_MEM. Default: determined" "automatically."))
def pytest_sessionstart(session): available_mem = session.config.getoption('available_memory') if available_mem is not None: os.environ['NPY_AVAILABLE_MEM'] = available_mem
#FIXME when yield tests are gone.@pytest.hookimpl()def pytest_itemcollected(item): """ Check FPU precision mode was not changed during test collection.
The clumsy way we do it here is mainly necessary because numpy still uses yield tests, which can execute code at test collection time. """ global _old_fpu_mode
mode = get_fpu_mode()
if _old_fpu_mode is None: _old_fpu_mode = mode elif mode != _old_fpu_mode: _collect_results[item] = (_old_fpu_mode, mode) _old_fpu_mode = mode
@pytest.fixture(scope="function", autouse=True)def check_fpu_mode(request): """ Check FPU precision mode was not changed during the test. """ old_mode = get_fpu_mode() yield new_mode = get_fpu_mode()
if old_mode != new_mode: raise AssertionError("FPU precision mode changed from {0:#x} to {1:#x}" " during the test".format(old_mode, new_mode))
collect_result = _collect_results.get(request.node) if collect_result is not None: old_mode, new_mode = collect_result raise AssertionError("FPU precision mode changed from {0:#x} to {1:#x}" " when collecting the test".format(old_mode, new_mode))
@pytest.fixture(autouse=True)def add_np(doctest_namespace): doctest_namespace['np'] = numpy
@pytest.fixture(autouse=True)def env_setup(monkeypatch): monkeypatch.setenv('PYTHONHASHSEED', '0')
@pytest.fixture(params=[True, False])def weak_promotion(request): """ Fixture to ensure "legacy" promotion state or change it to use the new weak promotion (plus warning). `old_promotion` should be used as a parameter in the function. """ state = numpy._get_promotion_state() if request.param: numpy._set_promotion_state("weak_and_warn") else: numpy._set_promotion_state("legacy")
yield request.param numpy._set_promotion_state(state)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545"""============================``ctypes`` Utility Functions============================
See Also--------load_library : Load a C library.ndpointer : Array restype/argtype with verification.as_ctypes : Create a ctypes array from an ndarray.as_array : Create an ndarray from a ctypes array.
References----------.. [1] "SciPy Cookbook: ctypes", https://scipy-cookbook.readthedocs.io/items/Ctypes.html
Examples--------Load the C library:
>>> _lib = np.ctypeslib.load_library('libmystuff', '.') #doctest: +SKIP
Our result type, an ndarray that must be of type double, be 1-dimensionaland is C-contiguous in memory:
>>> array_1d_double = np.ctypeslib.ndpointer(... dtype=np.double,... ndim=1, flags='CONTIGUOUS') #doctest: +SKIP
Our C-function typically takes an array and updates its valuesin-place. For example::
void foo_func(double* x, int length) { int i; for (i = 0; i < length; i++) { x[i] = i*i; } }
We wrap it using:
>>> _lib.foo_func.restype = None #doctest: +SKIP>>> _lib.foo_func.argtypes = [array_1d_double, c_int] #doctest: +SKIP
Then, we're ready to call ``foo_func``:
>>> out = np.empty(15, dtype=np.double)>>> _lib.foo_func(out, len(out)) #doctest: +SKIP
"""__all__ = ['load_library', 'ndpointer', 'c_intp', 'as_ctypes', 'as_array', 'as_ctypes_type']
import osfrom numpy import ( integer, ndarray, dtype as _dtype, asarray, frombuffer)from numpy.core.multiarray import _flagdict, flagsobj
try: import ctypesexcept ImportError: ctypes = None
if ctypes is None: def _dummy(*args, **kwds): """ Dummy object that raises an ImportError if ctypes is not available.
Raises ------ ImportError If ctypes is not available.
""" raise ImportError("ctypes is not available.") load_library = _dummy as_ctypes = _dummy as_array = _dummy from numpy import intp as c_intp _ndptr_base = objectelse: import numpy.core._internal as nic c_intp = nic._getintp_ctype() del nic _ndptr_base = ctypes.c_void_p
# Adapted from Albert Strasheim def load_library(libname, loader_path): """ It is possible to load a library using
>>> lib = ctypes.cdll[<full_path_name>] # doctest: +SKIP
But there are cross-platform considerations, such as library file extensions, plus the fact Windows will just load the first library it finds with that name. NumPy supplies the load_library function as a convenience.
.. versionchanged:: 1.20.0 Allow libname and loader_path to take any :term:`python:path-like object`.
Parameters ---------- libname : path-like Name of the library, which can have 'lib' as a prefix, but without an extension. loader_path : path-like Where the library can be found.
Returns ------- ctypes.cdll[libpath] : library object A ctypes library object
Raises ------ OSError If there is no library with the expected extension, or the library is defective and cannot be loaded. """ # Convert path-like objects into strings libname = os.fsdecode(libname) loader_path = os.fsdecode(loader_path)
ext = os.path.splitext(libname)[1] if not ext: import sys import sysconfig # Try to load library with platform-specific name, otherwise # default to libname.[so|dll|dylib]. Sometimes, these files are # built erroneously on non-linux platforms. base_ext = ".so" if sys.platform.startswith("darwin"): base_ext = ".dylib" elif sys.platform.startswith("win"): base_ext = ".dll" libname_ext = [libname + base_ext] so_ext = sysconfig.get_config_var("EXT_SUFFIX") if not so_ext == base_ext: libname_ext.insert(0, libname + so_ext) else: libname_ext = [libname]
loader_path = os.path.abspath(loader_path) if not os.path.isdir(loader_path): libdir = os.path.dirname(loader_path) else: libdir = loader_path
for ln in libname_ext: libpath = os.path.join(libdir, ln) if os.path.exists(libpath): try: return ctypes.cdll[libpath] except OSError: ## defective lib file raise ## if no successful return in the libname_ext loop: raise OSError("no file with expected extension")
def _num_fromflags(flaglist): num = 0 for val in flaglist: num += _flagdict[val] return num
_flagnames = ['C_CONTIGUOUS', 'F_CONTIGUOUS', 'ALIGNED', 'WRITEABLE', 'OWNDATA', 'WRITEBACKIFCOPY']def _flags_fromnum(num): res = [] for key in _flagnames: value = _flagdict[key] if (num & value): res.append(key) return res
class _ndptr(_ndptr_base): @classmethod def from_param(cls, obj): if not isinstance(obj, ndarray): raise TypeError("argument must be an ndarray") if cls._dtype_ is not None \ and obj.dtype != cls._dtype_: raise TypeError("array must have data type %s" % cls._dtype_) if cls._ndim_ is not None \ and obj.ndim != cls._ndim_: raise TypeError("array must have %d dimension(s)" % cls._ndim_) if cls._shape_ is not None \ and obj.shape != cls._shape_: raise TypeError("array must have shape %s" % str(cls._shape_)) if cls._flags_ is not None \ and ((obj.flags.num & cls._flags_) != cls._flags_): raise TypeError("array must have flags %s" % _flags_fromnum(cls._flags_)) return obj.ctypes
class _concrete_ndptr(_ndptr): """ Like _ndptr, but with `_shape_` and `_dtype_` specified.
Notably, this means the pointer has enough information to reconstruct the array, which is not generally true. """ def _check_retval_(self): """ This method is called when this class is used as the .restype attribute for a shared-library function, to automatically wrap the pointer into an array. """ return self.contents
@property def contents(self): """ Get an ndarray viewing the data pointed to by this pointer.
This mirrors the `contents` attribute of a normal ctypes pointer """ full_dtype = _dtype((self._dtype_, self._shape_)) full_ctype = ctypes.c_char * full_dtype.itemsize buffer = ctypes.cast(self, ctypes.POINTER(full_ctype)).contents return frombuffer(buffer, dtype=full_dtype).squeeze(axis=0)
# Factory for an array-checking class with from_param defined for# use with ctypes argtypes mechanism_pointer_type_cache = {}def ndpointer(dtype=None, ndim=None, shape=None, flags=None): """ Array-checking restype/argtypes.
An ndpointer instance is used to describe an ndarray in restypes and argtypes specifications. This approach is more flexible than using, for example, ``POINTER(c_double)``, since several restrictions can be specified, which are verified upon calling the ctypes function. These include data type, number of dimensions, shape and flags. If a given array does not satisfy the specified restrictions, a ``TypeError`` is raised.
Parameters ---------- dtype : data-type, optional Array data-type. ndim : int, optional Number of array dimensions. shape : tuple of ints, optional Array shape. flags : str or tuple of str Array flags; may be one or more of:
- C_CONTIGUOUS / C / CONTIGUOUS - F_CONTIGUOUS / F / FORTRAN - OWNDATA / O - WRITEABLE / W - ALIGNED / A - WRITEBACKIFCOPY / X
Returns ------- klass : ndpointer type object A type object, which is an ``_ndtpr`` instance containing dtype, ndim, shape and flags information.
Raises ------ TypeError If a given array does not satisfy the specified restrictions.
Examples -------- >>> clib.somefunc.argtypes = [np.ctypeslib.ndpointer(dtype=np.float64, ... ndim=1, ... flags='C_CONTIGUOUS')] ... #doctest: +SKIP >>> clib.somefunc(np.array([1, 2, 3], dtype=np.float64)) ... #doctest: +SKIP
"""
# normalize dtype to an Optional[dtype] if dtype is not None: dtype = _dtype(dtype)
# normalize flags to an Optional[int] num = None if flags is not None: if isinstance(flags, str): flags = flags.split(',') elif isinstance(flags, (int, integer)): num = flags flags = _flags_fromnum(num) elif isinstance(flags, flagsobj): num = flags.num flags = _flags_fromnum(num) if num is None: try: flags = [x.strip().upper() for x in flags] except Exception as e: raise TypeError("invalid flags specification") from e num = _num_fromflags(flags)
# normalize shape to an Optional[tuple] if shape is not None: try: shape = tuple(shape) except TypeError: # single integer -> 1-tuple shape = (shape,)
cache_key = (dtype, ndim, shape, num)
try: return _pointer_type_cache[cache_key] except KeyError: pass
# produce a name for the new type if dtype is None: name = 'any' elif dtype.names is not None: name = str(id(dtype)) else: name = dtype.str if ndim is not None: name += "_%dd" % ndim if shape is not None: name += "_"+"x".join(str(x) for x in shape) if flags is not None: name += "_"+"_".join(flags)
if dtype is not None and shape is not None: base = _concrete_ndptr else: base = _ndptr
klass = type("ndpointer_%s"%name, (base,), {"_dtype_": dtype, "_shape_" : shape, "_ndim_" : ndim, "_flags_" : num}) _pointer_type_cache[cache_key] = klass return klass
if ctypes is not None: def _ctype_ndarray(element_type, shape): """ Create an ndarray of the given element type and shape """ for dim in shape[::-1]: element_type = dim * element_type # prevent the type name include np.ctypeslib element_type.__module__ = None return element_type
def _get_scalar_type_map(): """ Return a dictionary mapping native endian scalar dtype to ctypes types """ ct = ctypes simple_types = [ ct.c_byte, ct.c_short, ct.c_int, ct.c_long, ct.c_longlong, ct.c_ubyte, ct.c_ushort, ct.c_uint, ct.c_ulong, ct.c_ulonglong, ct.c_float, ct.c_double, ct.c_bool, ] return {_dtype(ctype): ctype for ctype in simple_types}
_scalar_type_map = _get_scalar_type_map()
def _ctype_from_dtype_scalar(dtype): # swapping twice ensure that `=` is promoted to <, >, or | dtype_with_endian = dtype.newbyteorder('S').newbyteorder('S') dtype_native = dtype.newbyteorder('=') try: ctype = _scalar_type_map[dtype_native] except KeyError as e: raise NotImplementedError( "Converting {!r} to a ctypes type".format(dtype) ) from None
if dtype_with_endian.byteorder == '>': ctype = ctype.__ctype_be__ elif dtype_with_endian.byteorder == '<': ctype = ctype.__ctype_le__
return ctype
def _ctype_from_dtype_subarray(dtype): element_dtype, shape = dtype.subdtype ctype = _ctype_from_dtype(element_dtype) return _ctype_ndarray(ctype, shape)
def _ctype_from_dtype_structured(dtype): # extract offsets of each field field_data = [] for name in dtype.names: field_dtype, offset = dtype.fields[name][:2] field_data.append((offset, name, _ctype_from_dtype(field_dtype)))
# ctypes doesn't care about field order field_data = sorted(field_data, key=lambda f: f[0])
if len(field_data) > 1 and all(offset == 0 for offset, name, ctype in field_data): # union, if multiple fields all at address 0 size = 0 _fields_ = [] for offset, name, ctype in field_data: _fields_.append((name, ctype)) size = max(size, ctypes.sizeof(ctype))
# pad to the right size if dtype.itemsize != size: _fields_.append(('', ctypes.c_char * dtype.itemsize))
# we inserted manual padding, so always `_pack_` return type('union', (ctypes.Union,), dict( _fields_=_fields_, _pack_=1, __module__=None, )) else: last_offset = 0 _fields_ = [] for offset, name, ctype in field_data: padding = offset - last_offset if padding < 0: raise NotImplementedError("Overlapping fields") if padding > 0: _fields_.append(('', ctypes.c_char * padding))
_fields_.append((name, ctype)) last_offset = offset + ctypes.sizeof(ctype)
padding = dtype.itemsize - last_offset if padding > 0: _fields_.append(('', ctypes.c_char * padding))
# we inserted manual padding, so always `_pack_` return type('struct', (ctypes.Structure,), dict( _fields_=_fields_, _pack_=1, __module__=None, ))
def _ctype_from_dtype(dtype): if dtype.fields is not None: return _ctype_from_dtype_structured(dtype) elif dtype.subdtype is not None: return _ctype_from_dtype_subarray(dtype) else: return _ctype_from_dtype_scalar(dtype)
def as_ctypes_type(dtype): r""" Convert a dtype into a ctypes type.
Parameters ---------- dtype : dtype The dtype to convert
Returns ------- ctype A ctype scalar, union, array, or struct
Raises ------ NotImplementedError If the conversion is not possible
Notes ----- This function does not losslessly round-trip in either direction.
``np.dtype(as_ctypes_type(dt))`` will:
- insert padding fields - reorder fields to be sorted by offset - discard field titles
``as_ctypes_type(np.dtype(ctype))`` will:
- discard the class names of `ctypes.Structure`\ s and `ctypes.Union`\ s - convert single-element `ctypes.Union`\ s into single-element `ctypes.Structure`\ s - insert padding fields
""" return _ctype_from_dtype(_dtype(dtype))
def as_array(obj, shape=None): """ Create a numpy array from a ctypes array or POINTER.
The numpy array shares the memory with the ctypes object.
The shape parameter must be given if converting from a ctypes POINTER. The shape parameter is ignored if converting from a ctypes array """ if isinstance(obj, ctypes._Pointer): # convert pointers to an array of the desired shape if shape is None: raise TypeError( 'as_array() requires a shape argument when called on a ' 'pointer') p_arr_type = ctypes.POINTER(_ctype_ndarray(obj._type_, shape)) obj = ctypes.cast(obj, p_arr_type).contents
return asarray(obj)
def as_ctypes(obj): """Create and return a ctypes object from a numpy array. Actually anything that exposes the __array_interface__ is accepted.""" ai = obj.__array_interface__ if ai["strides"]: raise TypeError("strided arrays not supported") if ai["version"] != 3: raise TypeError("only __array_interface__ version 3 supported") addr, readonly = ai["data"] if readonly: raise TypeError("readonly arrays unsupported")
# can't use `_dtype((ai["typestr"], ai["shape"]))` here, as it overflows # dtype.itemsize (gh-14214) ctype_scalar = as_ctypes_type(ai["typestr"]) result_type = _ctype_ndarray(ctype_scalar, ai["shape"]) result = result_type.from_address(addr) result.__keep = obj return result123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251# NOTE: Numpy's mypy plugin is used for importing the correct# platform-specific `ctypes._SimpleCData[int]` sub-typefrom ctypes import c_int64 as _c_intp
import osimport sysimport ctypesfrom collections.abc import Iterable, Sequencefrom typing import ( Literal as L, Any, Union, TypeVar, Generic, overload, ClassVar,)
from numpy import ( ndarray, dtype, generic, bool_, byte, short, intc, int_, longlong, ubyte, ushort, uintc, uint, ulonglong, single, double, longdouble, void,)from numpy.core._internal import _ctypesfrom numpy.core.multiarray import flagsobjfrom numpy._typing import ( # Arrays NDArray, _ArrayLike,
# Shapes _ShapeLike,
# DTypes DTypeLike, _DTypeLike, _VoidDTypeLike, _BoolCodes, _UByteCodes, _UShortCodes, _UIntCCodes, _UIntCodes, _ULongLongCodes, _ByteCodes, _ShortCodes, _IntCCodes, _IntCodes, _LongLongCodes, _SingleCodes, _DoubleCodes, _LongDoubleCodes,)
# TODO: Add a proper `_Shape` bound once we've got variadic typevars_DType = TypeVar("_DType", bound=dtype[Any])_DTypeOptional = TypeVar("_DTypeOptional", bound=None | dtype[Any])_SCT = TypeVar("_SCT", bound=generic)
_FlagsKind = L[ 'C_CONTIGUOUS', 'CONTIGUOUS', 'C', 'F_CONTIGUOUS', 'FORTRAN', 'F', 'ALIGNED', 'A', 'WRITEABLE', 'W', 'OWNDATA', 'O', 'WRITEBACKIFCOPY', 'X',]
# TODO: Add a shape typevar once we have variadic typevars (PEP 646)class _ndptr(ctypes.c_void_p, Generic[_DTypeOptional]): # In practice these 4 classvars are defined in the dynamic class # returned by `ndpointer` _dtype_: ClassVar[_DTypeOptional] _shape_: ClassVar[None] _ndim_: ClassVar[None | int] _flags_: ClassVar[None | list[_FlagsKind]]
@overload @classmethod def from_param(cls: type[_ndptr[None]], obj: ndarray[Any, Any]) -> _ctypes[Any]: ... @overload @classmethod def from_param(cls: type[_ndptr[_DType]], obj: ndarray[Any, _DType]) -> _ctypes[Any]: ...
class _concrete_ndptr(_ndptr[_DType]): _dtype_: ClassVar[_DType] _shape_: ClassVar[tuple[int, ...]] @property def contents(self) -> ndarray[Any, _DType]: ...
def load_library( libname: str | bytes | os.PathLike[str] | os.PathLike[bytes], loader_path: str | bytes | os.PathLike[str] | os.PathLike[bytes],) -> ctypes.CDLL: ...
__all__: list[str]
c_intp = _c_intp
@overloaddef ndpointer( dtype: None = ..., ndim: int = ..., shape: None | _ShapeLike = ..., flags: None | _FlagsKind | Iterable[_FlagsKind] | int | flagsobj = ...,) -> type[_ndptr[None]]: ...@overloaddef ndpointer( dtype: _DTypeLike[_SCT], ndim: int = ..., *, shape: _ShapeLike, flags: None | _FlagsKind | Iterable[_FlagsKind] | int | flagsobj = ...,) -> type[_concrete_ndptr[dtype[_SCT]]]: ...@overloaddef ndpointer( dtype: DTypeLike, ndim: int = ..., *, shape: _ShapeLike, flags: None | _FlagsKind | Iterable[_FlagsKind] | int | flagsobj = ...,) -> type[_concrete_ndptr[dtype[Any]]]: ...@overloaddef ndpointer( dtype: _DTypeLike[_SCT], ndim: int = ..., shape: None = ..., flags: None | _FlagsKind | Iterable[_FlagsKind] | int | flagsobj = ...,) -> type[_ndptr[dtype[_SCT]]]: ...@overloaddef ndpointer( dtype: DTypeLike, ndim: int = ..., shape: None = ..., flags: None | _FlagsKind | Iterable[_FlagsKind] | int | flagsobj = ...,) -> type[_ndptr[dtype[Any]]]: ...
@overloaddef as_ctypes_type(dtype: _BoolCodes | _DTypeLike[bool_] | type[ctypes.c_bool]) -> type[ctypes.c_bool]: ...@overloaddef as_ctypes_type(dtype: _ByteCodes | _DTypeLike[byte] | type[ctypes.c_byte]) -> type[ctypes.c_byte]: ...@overloaddef as_ctypes_type(dtype: _ShortCodes | _DTypeLike[short] | type[ctypes.c_short]) -> type[ctypes.c_short]: ...@overloaddef as_ctypes_type(dtype: _IntCCodes | _DTypeLike[intc] | type[ctypes.c_int]) -> type[ctypes.c_int]: ...@overloaddef as_ctypes_type(dtype: _IntCodes | _DTypeLike[int_] | type[int | ctypes.c_long]) -> type[ctypes.c_long]: ...@overloaddef as_ctypes_type(dtype: _LongLongCodes | _DTypeLike[longlong] | type[ctypes.c_longlong]) -> type[ctypes.c_longlong]: ...@overloaddef as_ctypes_type(dtype: _UByteCodes | _DTypeLike[ubyte] | type[ctypes.c_ubyte]) -> type[ctypes.c_ubyte]: ...@overloaddef as_ctypes_type(dtype: _UShortCodes | _DTypeLike[ushort] | type[ctypes.c_ushort]) -> type[ctypes.c_ushort]: ...@overloaddef as_ctypes_type(dtype: _UIntCCodes | _DTypeLike[uintc] | type[ctypes.c_uint]) -> type[ctypes.c_uint]: ...@overloaddef as_ctypes_type(dtype: _UIntCodes | _DTypeLike[uint] | type[ctypes.c_ulong]) -> type[ctypes.c_ulong]: ...@overloaddef as_ctypes_type(dtype: _ULongLongCodes | _DTypeLike[ulonglong] | type[ctypes.c_ulonglong]) -> type[ctypes.c_ulonglong]: ...@overloaddef as_ctypes_type(dtype: _SingleCodes | _DTypeLike[single] | type[ctypes.c_float]) -> type[ctypes.c_float]: ...@overloaddef as_ctypes_type(dtype: _DoubleCodes | _DTypeLike[double] | type[float | ctypes.c_double]) -> type[ctypes.c_double]: ...@overloaddef as_ctypes_type(dtype: _LongDoubleCodes | _DTypeLike[longdouble] | type[ctypes.c_longdouble]) -> type[ctypes.c_longdouble]: ...@overloaddef as_ctypes_type(dtype: _VoidDTypeLike) -> type[Any]: ... # `ctypes.Union` or `ctypes.Structure`@overloaddef as_ctypes_type(dtype: str) -> type[Any]: ...
@overloaddef as_array(obj: ctypes._PointerLike, shape: Sequence[int]) -> NDArray[Any]: ...@overloaddef as_array(obj: _ArrayLike[_SCT], shape: None | _ShapeLike = ...) -> NDArray[_SCT]: ...@overloaddef as_array(obj: object, shape: None | _ShapeLike = ...) -> NDArray[Any]: ...
@overloaddef as_ctypes(obj: bool_) -> ctypes.c_bool: ...@overloaddef as_ctypes(obj: byte) -> ctypes.c_byte: ...@overloaddef as_ctypes(obj: short) -> ctypes.c_short: ...@overloaddef as_ctypes(obj: intc) -> ctypes.c_int: ...@overloaddef as_ctypes(obj: int_) -> ctypes.c_long: ...@overloaddef as_ctypes(obj: longlong) -> ctypes.c_longlong: ...@overloaddef as_ctypes(obj: ubyte) -> ctypes.c_ubyte: ...@overloaddef as_ctypes(obj: ushort) -> ctypes.c_ushort: ...@overloaddef as_ctypes(obj: uintc) -> ctypes.c_uint: ...@overloaddef as_ctypes(obj: uint) -> ctypes.c_ulong: ...@overloaddef as_ctypes(obj: ulonglong) -> ctypes.c_ulonglong: ...@overloaddef as_ctypes(obj: single) -> ctypes.c_float: ...@overloaddef as_ctypes(obj: double) -> ctypes.c_double: ...@overloaddef as_ctypes(obj: longdouble) -> ctypes.c_longdouble: ...@overloaddef as_ctypes(obj: void) -> Any: ... # `ctypes.Union` or `ctypes.Structure`@overloaddef as_ctypes(obj: NDArray[bool_]) -> ctypes.Array[ctypes.c_bool]: ...@overloaddef as_ctypes(obj: NDArray[byte]) -> ctypes.Array[ctypes.c_byte]: ...@overloaddef as_ctypes(obj: NDArray[short]) -> ctypes.Array[ctypes.c_short]: ...@overloaddef as_ctypes(obj: NDArray[intc]) -> ctypes.Array[ctypes.c_int]: ...@overloaddef as_ctypes(obj: NDArray[int_]) -> ctypes.Array[ctypes.c_long]: ...@overloaddef as_ctypes(obj: NDArray[longlong]) -> ctypes.Array[ctypes.c_longlong]: ...@overloaddef as_ctypes(obj: NDArray[ubyte]) -> ctypes.Array[ctypes.c_ubyte]: ...@overloaddef as_ctypes(obj: NDArray[ushort]) -> ctypes.Array[ctypes.c_ushort]: ...@overloaddef as_ctypes(obj: NDArray[uintc]) -> ctypes.Array[ctypes.c_uint]: ...@overloaddef as_ctypes(obj: NDArray[uint]) -> ctypes.Array[ctypes.c_ulong]: ...@overloaddef as_ctypes(obj: NDArray[ulonglong]) -> ctypes.Array[ctypes.c_ulonglong]: ...@overloaddef as_ctypes(obj: NDArray[single]) -> ctypes.Array[ctypes.c_float]: ...@overloaddef as_ctypes(obj: NDArray[double]) -> ctypes.Array[ctypes.c_double]: ...@overloaddef as_ctypes(obj: NDArray[longdouble]) -> ctypes.Array[ctypes.c_longdouble]: ...@overloaddef as_ctypes(obj: NDArray[void]) -> ctypes.Array[Any]: ... # `ctypes.Union` or `ctypes.Structure`1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677"""DType classes and utility (:mod:`numpy.dtypes`)===============================================
This module is home to specific dtypes related functionality and their classes.For more general information about dtypes, also see `numpy.dtype` and:ref:`arrays.dtypes`.
Similar to the builtin ``types`` module, this submodule defines types (classes)that are not widely used directly.
.. versionadded:: NumPy 1.25
The dtypes module is new in NumPy 1.25. Previously DType classes were only accessible indirectly.
DType classes-------------
The following are the classes of the corresponding NumPy dtype instances andNumPy scalar types. The classes can be used in ``isinstance`` checks and canalso be instantiated or used directly. Direct use of these classes is nottypical, since their scalar counterparts (e.g. ``np.float64``) or stringslike ``"float64"`` can be used.
.. list-table:: :header-rows: 1
* - Group - DType class
* - Boolean - ``BoolDType``
* - Bit-sized integers - ``Int8DType``, ``UInt8DType``, ``Int16DType``, ``UInt16DType``, ``Int32DType``, ``UInt32DType``, ``Int64DType``, ``UInt64DType``
* - C-named integers (may be aliases) - ``ByteDType``, ``UByteDType``, ``ShortDType``, ``UShortDType``, ``IntDType``, ``UIntDType``, ``LongDType``, ``ULongDType``, ``LongLongDType``, ``ULongLongDType``
* - Floating point - ``Float16DType``, ``Float32DType``, ``Float64DType``, ``LongDoubleDType``
* - Complex - ``Complex64DType``, ``Complex128DType``, ``CLongDoubleDType``
* - Strings - ``BytesDType``, ``BytesDType``
* - Times - ``DateTime64DType``, ``TimeDelta64DType``
* - Others - ``ObjectDType``, ``VoidDType``
"""
__all__ = []
def _add_dtype_helper(DType, alias): # Function to add DTypes a bit more conveniently without channeling them # through `numpy.core._multiarray_umath` namespace or similar. from numpy import dtypes
setattr(dtypes, DType.__name__, DType) __all__.append(DType.__name__)
if alias: alias = alias.removeprefix("numpy.dtypes.") setattr(dtypes, alias, DType) __all__.append(alias)12345678910111213141516171819202122232425262728293031323334353637383940414243import numpy as np
__all__: list[str]
# Boolean:BoolDType = np.dtype[np.bool_]# Sized integers:Int8DType = np.dtype[np.int8]UInt8DType = np.dtype[np.uint8]Int16DType = np.dtype[np.int16]UInt16DType = np.dtype[np.uint16]Int32DType = np.dtype[np.int32]UInt32DType = np.dtype[np.uint32]Int64DType = np.dtype[np.int64]UInt64DType = np.dtype[np.uint64]# Standard C-named version/alias:ByteDType = np.dtype[np.byte]UByteDType = np.dtype[np.ubyte]ShortDType = np.dtype[np.short]UShortDType = np.dtype[np.ushort]IntDType = np.dtype[np.intc]UIntDType = np.dtype[np.uintc]LongDType = np.dtype[np.int_] # Unfortunately, the correct scalarULongDType = np.dtype[np.uint] # Unfortunately, the correct scalarLongLongDType = np.dtype[np.longlong]ULongLongDType = np.dtype[np.ulonglong]# FloatsFloat16DType = np.dtype[np.float16]Float32DType = np.dtype[np.float32]Float64DType = np.dtype[np.float64]LongDoubleDType = np.dtype[np.longdouble]# Complex:Complex64DType = np.dtype[np.complex64]Complex128DType = np.dtype[np.complex128]CLongDoubleDType = np.dtype[np.clongdouble]# Others:ObjectDType = np.dtype[np.object_]BytesDType = np.dtype[np.bytes_]StrDType = np.dtype[np.str_]VoidDType = np.dtype[np.void]DateTime64DType = np.dtype[np.datetime64]TimeDelta64DType = np.dtype[np.timedelta64]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231"""Exceptions and Warnings (:mod:`numpy.exceptions`)=================================================
General exceptions used by NumPy. Note that some exceptions may be modulespecific, such as linear algebra errors.
.. versionadded:: NumPy 1.25
The exceptions module is new in NumPy 1.25. Older exceptions remain available through the main NumPy namespace for compatibility.
.. currentmodule:: numpy.exceptions
Warnings--------.. autosummary:: :toctree: generated/
ComplexWarning Given when converting complex to real. VisibleDeprecationWarning Same as a DeprecationWarning, but more visible.
Exceptions----------.. autosummary:: :toctree: generated/
AxisError Given when an axis was invalid. DTypePromotionError Given when no common dtype could be found. TooHardError Error specific to `numpy.shares_memory`.
"""
__all__ = [ "ComplexWarning", "VisibleDeprecationWarning", "ModuleDeprecationWarning", "TooHardError", "AxisError", "DTypePromotionError"]
# Disallow reloading this module so as to preserve the identities of the# classes defined here.if '_is_loaded' in globals(): raise RuntimeError('Reloading numpy._globals is not allowed')_is_loaded = True
class ComplexWarning(RuntimeWarning): """ The warning raised when casting a complex dtype to a real dtype.
As implemented, casting a complex number to a real discards its imaginary part, but this behavior may not be what the user actually wants.
""" pass
class ModuleDeprecationWarning(DeprecationWarning): """Module deprecation warning.
.. warning::
This warning should not be used, since nose testing is not relevant anymore.
The nose tester turns ordinary Deprecation warnings into test failures. That makes it hard to deprecate whole modules, because they get imported by default. So this is a special Deprecation warning that the nose tester will let pass without making tests fail.
"""
class VisibleDeprecationWarning(UserWarning): """Visible deprecation warning.
By default, python will not show deprecation warnings, so this class can be used when a very visible warning is helpful, for example because the usage is most likely a user bug.
"""
# Exception used in shares_memory()class TooHardError(RuntimeError): """max_work was exceeded.
This is raised whenever the maximum number of candidate solutions to consider specified by the ``max_work`` parameter is exceeded. Assigning a finite number to max_work may have caused the operation to fail.
"""
pass
class AxisError(ValueError, IndexError): """Axis supplied was invalid.
This is raised whenever an ``axis`` parameter is specified that is larger than the number of array dimensions. For compatibility with code written against older numpy versions, which raised a mixture of `ValueError` and `IndexError` for this situation, this exception subclasses both to ensure that ``except ValueError`` and ``except IndexError`` statements continue to catch `AxisError`.
.. versionadded:: 1.13
Parameters ---------- axis : int or str The out of bounds axis or a custom exception message. If an axis is provided, then `ndim` should be specified as well. ndim : int, optional The number of array dimensions. msg_prefix : str, optional A prefix for the exception message.
Attributes ---------- axis : int, optional The out of bounds axis or ``None`` if a custom exception message was provided. This should be the axis as passed by the user, before any normalization to resolve negative indices.
.. versionadded:: 1.22 ndim : int, optional The number of array dimensions or ``None`` if a custom exception message was provided.
.. versionadded:: 1.22
Examples -------- >>> array_1d = np.arange(10) >>> np.cumsum(array_1d, axis=1) Traceback (most recent call last): ... numpy.exceptions.AxisError: axis 1 is out of bounds for array of dimension 1
Negative axes are preserved:
>>> np.cumsum(array_1d, axis=-2) Traceback (most recent call last): ... numpy.exceptions.AxisError: axis -2 is out of bounds for array of dimension 1
The class constructor generally takes the axis and arrays' dimensionality as arguments:
>>> print(np.AxisError(2, 1, msg_prefix='error')) error: axis 2 is out of bounds for array of dimension 1
Alternatively, a custom exception message can be passed:
>>> print(np.AxisError('Custom error message')) Custom error message
"""
__slots__ = ("axis", "ndim", "_msg")
def __init__(self, axis, ndim=None, msg_prefix=None): if ndim is msg_prefix is None: # single-argument form: directly set the error message self._msg = axis self.axis = None self.ndim = None else: self._msg = msg_prefix self.axis = axis self.ndim = ndim
def __str__(self): axis = self.axis ndim = self.ndim
if axis is ndim is None: return self._msg else: msg = f"axis {axis} is out of bounds for array of dimension {ndim}" if self._msg is not None: msg = f"{self._msg}: {msg}" return msg
class DTypePromotionError(TypeError): """Multiple DTypes could not be converted to a common one.
This exception derives from ``TypeError`` and is raised whenever dtypes cannot be converted to a single common one. This can be because they are of a different category/class or incompatible instances of the same one (see Examples).
Notes ----- Many functions will use promotion to find the correct result and implementation. For these functions the error will typically be chained with a more specific error indicating that no implementation was found for the input dtypes.
Typically promotion should be considered "invalid" between the dtypes of two arrays when `arr1 == arr2` can safely return all ``False`` because the dtypes are fundamentally different.
Examples -------- Datetimes and complex numbers are incompatible classes and cannot be promoted:
>>> np.result_type(np.dtype("M8[s]"), np.complex128) DTypePromotionError: The DType <class 'numpy.dtype[datetime64]'> could not be promoted by <class 'numpy.dtype[complex128]'>. This means that no common DType exists for the given inputs. For example they cannot be stored in a single array unless the dtype is `object`. The full list of DTypes is: (<class 'numpy.dtype[datetime64]'>, <class 'numpy.dtype[complex128]'>)
For example for structured dtypes, the structure can mismatch and the same ``DTypePromotionError`` is given when two structured dtypes with a mismatch in their number of fields is given:
>>> dtype1 = np.dtype([("field1", np.float64), ("field2", np.int64)]) >>> dtype2 = np.dtype([("field1", np.float64)]) >>> np.promote_types(dtype1, dtype2) DTypePromotionError: field names `('field1', 'field2')` and `('field1',)` mismatch.
""" pass123456789101112131415161718from typing import overload
__all__: list[str]
class ComplexWarning(RuntimeWarning): ...class ModuleDeprecationWarning(DeprecationWarning): ...class VisibleDeprecationWarning(UserWarning): ...class TooHardError(RuntimeError): ...class DTypePromotionError(TypeError): ...
class AxisError(ValueError, IndexError): axis: None | int ndim: None | int @overload def __init__(self, axis: str, ndim: None = ..., msg_prefix: None = ...) -> None: ... @overload def __init__(self, axis: int, ndim: int, msg_prefix: None | str = ...) -> None: ... def __str__(self) -> str: ...123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378import warnings
# 2018-05-29, PendingDeprecationWarning added to matrix.__new__# 2020-01-23, numpy 1.19.0 PendingDeprecatonWarningwarnings.warn("Importing from numpy.matlib is deprecated since 1.19.0. " "The matrix subclass is not the recommended way to represent " "matrices or deal with linear algebra (see " "https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). " "Please adjust your code to use regular ndarray. ", PendingDeprecationWarning, stacklevel=2)
import numpy as npfrom numpy.matrixlib.defmatrix import matrix, asmatrix# Matlib.py contains all functions in the numpy namespace with a few# replacements. See doc/source/reference/routines.matlib.rst for details.# Need * as we're copying the numpy namespace.from numpy import * # noqa: F403
__version__ = np.__version__
__all__ = np.__all__[:] # copy numpy namespace__all__ += ['rand', 'randn', 'repmat']
def empty(shape, dtype=None, order='C'): """Return a new matrix of given shape and type, without initializing entries.
Parameters ---------- shape : int or tuple of int Shape of the empty matrix. dtype : data-type, optional Desired output data-type. order : {'C', 'F'}, optional Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory.
See Also -------- empty_like, zeros
Notes ----- `empty`, unlike `zeros`, does not set the matrix values to zero, and may therefore be marginally faster. On the other hand, it requires the user to manually set all the values in the array, and should be used with caution.
Examples -------- >>> import numpy.matlib >>> np.matlib.empty((2, 2)) # filled with random data matrix([[ 6.76425276e-320, 9.79033856e-307], # random [ 7.39337286e-309, 3.22135945e-309]]) >>> np.matlib.empty((2, 2), dtype=int) matrix([[ 6600475, 0], # random [ 6586976, 22740995]])
""" return ndarray.__new__(matrix, shape, dtype, order=order)
def ones(shape, dtype=None, order='C'): """ Matrix of ones.
Return a matrix of given shape and type, filled with ones.
Parameters ---------- shape : {sequence of ints, int} Shape of the matrix dtype : data-type, optional The desired data-type for the matrix, default is np.float64. order : {'C', 'F'}, optional Whether to store matrix in C- or Fortran-contiguous order, default is 'C'.
Returns ------- out : matrix Matrix of ones of given shape, dtype, and order.
See Also -------- ones : Array of ones. matlib.zeros : Zero matrix.
Notes ----- If `shape` has length one i.e. ``(N,)``, or is a scalar ``N``, `out` becomes a single row matrix of shape ``(1,N)``.
Examples -------- >>> np.matlib.ones((2,3)) matrix([[1., 1., 1.], [1., 1., 1.]])
>>> np.matlib.ones(2) matrix([[1., 1.]])
""" a = ndarray.__new__(matrix, shape, dtype, order=order) a.fill(1) return a
def zeros(shape, dtype=None, order='C'): """ Return a matrix of given shape and type, filled with zeros.
Parameters ---------- shape : int or sequence of ints Shape of the matrix dtype : data-type, optional The desired data-type for the matrix, default is float. order : {'C', 'F'}, optional Whether to store the result in C- or Fortran-contiguous order, default is 'C'.
Returns ------- out : matrix Zero matrix of given shape, dtype, and order.
See Also -------- numpy.zeros : Equivalent array function. matlib.ones : Return a matrix of ones.
Notes ----- If `shape` has length one i.e. ``(N,)``, or is a scalar ``N``, `out` becomes a single row matrix of shape ``(1,N)``.
Examples -------- >>> import numpy.matlib >>> np.matlib.zeros((2, 3)) matrix([[0., 0., 0.], [0., 0., 0.]])
>>> np.matlib.zeros(2) matrix([[0., 0.]])
""" a = ndarray.__new__(matrix, shape, dtype, order=order) a.fill(0) return a
def identity(n,dtype=None): """ Returns the square identity matrix of given size.
Parameters ---------- n : int Size of the returned identity matrix. dtype : data-type, optional Data-type of the output. Defaults to ``float``.
Returns ------- out : matrix `n` x `n` matrix with its main diagonal set to one, and all other elements zero.
See Also -------- numpy.identity : Equivalent array function. matlib.eye : More general matrix identity function.
Examples -------- >>> import numpy.matlib >>> np.matlib.identity(3, dtype=int) matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
""" a = array([1]+n*[0], dtype=dtype) b = empty((n, n), dtype=dtype) b.flat = a return b
def eye(n,M=None, k=0, dtype=float, order='C'): """ Return a matrix with ones on the diagonal and zeros elsewhere.
Parameters ---------- n : int Number of rows in the output. M : int, optional Number of columns in the output, defaults to `n`. k : int, optional Index of the diagonal: 0 refers to the main diagonal, a positive value refers to an upper diagonal, and a negative value to a lower diagonal. dtype : dtype, optional Data-type of the returned matrix. order : {'C', 'F'}, optional Whether the output should be stored in row-major (C-style) or column-major (Fortran-style) order in memory.
.. versionadded:: 1.14.0
Returns ------- I : matrix A `n` x `M` matrix where all elements are equal to zero, except for the `k`-th diagonal, whose values are equal to one.
See Also -------- numpy.eye : Equivalent array function. identity : Square identity matrix.
Examples -------- >>> import numpy.matlib >>> np.matlib.eye(3, k=1, dtype=float) matrix([[0., 1., 0.], [0., 0., 1.], [0., 0., 0.]])
""" return asmatrix(np.eye(n, M=M, k=k, dtype=dtype, order=order))
def rand(*args): """ Return a matrix of random values with given shape.
Create a matrix of the given shape and propagate it with random samples from a uniform distribution over ``[0, 1)``.
Parameters ---------- \\*args : Arguments Shape of the output. If given as N integers, each integer specifies the size of one dimension. If given as a tuple, this tuple gives the complete shape.
Returns ------- out : ndarray The matrix of random values with shape given by `\\*args`.
See Also -------- randn, numpy.random.RandomState.rand
Examples -------- >>> np.random.seed(123) >>> import numpy.matlib >>> np.matlib.rand(2, 3) matrix([[0.69646919, 0.28613933, 0.22685145], [0.55131477, 0.71946897, 0.42310646]]) >>> np.matlib.rand((2, 3)) matrix([[0.9807642 , 0.68482974, 0.4809319 ], [0.39211752, 0.34317802, 0.72904971]])
If the first argument is a tuple, other arguments are ignored:
>>> np.matlib.rand((2, 3), 4) matrix([[0.43857224, 0.0596779 , 0.39804426], [0.73799541, 0.18249173, 0.17545176]])
""" if isinstance(args[0], tuple): args = args[0] return asmatrix(np.random.rand(*args))
def randn(*args): """ Return a random matrix with data from the "standard normal" distribution.
`randn` generates a matrix filled with random floats sampled from a univariate "normal" (Gaussian) distribution of mean 0 and variance 1.
Parameters ---------- \\*args : Arguments Shape of the output. If given as N integers, each integer specifies the size of one dimension. If given as a tuple, this tuple gives the complete shape.
Returns ------- Z : matrix of floats A matrix of floating-point samples drawn from the standard normal distribution.
See Also -------- rand, numpy.random.RandomState.randn
Notes ----- For random samples from the normal distribution with mean ``mu`` and standard deviation ``sigma``, use::
sigma * np.matlib.randn(...) + mu
Examples -------- >>> np.random.seed(123) >>> import numpy.matlib >>> np.matlib.randn(1) matrix([[-1.0856306]]) >>> np.matlib.randn(1, 2, 3) matrix([[ 0.99734545, 0.2829785 , -1.50629471], [-0.57860025, 1.65143654, -2.42667924]])
Two-by-four matrix of samples from the normal distribution with mean 3 and standard deviation 2.5:
>>> 2.5 * np.matlib.randn((2, 4)) + 3 matrix([[1.92771843, 6.16484065, 0.83314899, 1.30278462], [2.76322758, 6.72847407, 1.40274501, 1.8900451 ]])
""" if isinstance(args[0], tuple): args = args[0] return asmatrix(np.random.randn(*args))
def repmat(a, m, n): """ Repeat a 0-D to 2-D array or matrix MxN times.
Parameters ---------- a : array_like The array or matrix to be repeated. m, n : int The number of times `a` is repeated along the first and second axes.
Returns ------- out : ndarray The result of repeating `a`.
Examples -------- >>> import numpy.matlib >>> a0 = np.array(1) >>> np.matlib.repmat(a0, 2, 3) array([[1, 1, 1], [1, 1, 1]])
>>> a1 = np.arange(4) >>> np.matlib.repmat(a1, 2, 2) array([[0, 1, 2, 3, 0, 1, 2, 3], [0, 1, 2, 3, 0, 1, 2, 3]])
>>> a2 = np.asmatrix(np.arange(6).reshape(2, 3)) >>> np.matlib.repmat(a2, 2, 3) matrix([[0, 1, 2, 0, 1, 2, 0, 1, 2], [3, 4, 5, 3, 4, 5, 3, 4, 5], [0, 1, 2, 0, 1, 2, 0, 1, 2], [3, 4, 5, 3, 4, 5, 3, 4, 5]])
""" a = asanyarray(a) ndim = a.ndim if ndim == 0: origrows, origcols = (1, 1) elif ndim == 1: origrows, origcols = (1, a.shape[0]) else: origrows, origcols = a.shape rows = origrows * m cols = origcols * n c = a.reshape(1, a.size).repeat(m, 0).reshape(rows, origcols).repeat(n, 0) return c.reshape(rows, cols)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185"""A sub-package for efficiently dealing with polynomials.
Within the documentation for this sub-package, a "finite power series,"i.e., a polynomial (also referred to simply as a "series") is representedby a 1-D numpy array of the polynomial's coefficients, ordered from lowestorder term to highest. For example, array([1,2,3]) represents``P_0 + 2*P_1 + 3*P_2``, where P_n is the n-th order basis polynomialapplicable to the specific module in question, e.g., `polynomial` (which"wraps" the "standard" basis) or `chebyshev`. For optimal performance,all operations on polynomials, including evaluation at an argument, areimplemented as operations on the coefficients. Additional (module-specific)information can be found in the docstring for the module of interest.
This package provides *convenience classes* for each of six different kindsof polynomials:
======================== ================ **Name** **Provides** ======================== ================ `~polynomial.Polynomial` Power series `~chebyshev.Chebyshev` Chebyshev series `~legendre.Legendre` Legendre series `~laguerre.Laguerre` Laguerre series `~hermite.Hermite` Hermite series `~hermite_e.HermiteE` HermiteE series ======================== ================
These *convenience classes* provide a consistent interface for creating,manipulating, and fitting data with polynomials of different bases.The convenience classes are the preferred interface for the `~numpy.polynomial`package, and are available from the ``numpy.polynomial`` namespace.This eliminates the need to navigate to the corresponding submodules, e.g.``np.polynomial.Polynomial`` or ``np.polynomial.Chebyshev`` instead of``np.polynomial.polynomial.Polynomial`` or``np.polynomial.chebyshev.Chebyshev``, respectively.The classes provide a more consistent and concise interface than thetype-specific functions defined in the submodules for each type of polynomial.For example, to fit a Chebyshev polynomial with degree ``1`` to data givenby arrays ``xdata`` and ``ydata``, the`~chebyshev.Chebyshev.fit` class method::
>>> from numpy.polynomial import Chebyshev >>> c = Chebyshev.fit(xdata, ydata, deg=1)
is preferred over the `chebyshev.chebfit` function from the``np.polynomial.chebyshev`` module::
>>> from numpy.polynomial.chebyshev import chebfit >>> c = chebfit(xdata, ydata, deg=1)
See :doc:`routines.polynomials.classes` for more details.
Convenience Classes===================
The following lists the various constants and methods common to all ofthe classes representing the various kinds of polynomials. In the following,the term ``Poly`` represents any one of the convenience classes (e.g.`~polynomial.Polynomial`, `~chebyshev.Chebyshev`, `~hermite.Hermite`, etc.)while the lowercase ``p`` represents an **instance** of a polynomial class.
Constants---------
- ``Poly.domain`` -- Default domain- ``Poly.window`` -- Default window- ``Poly.basis_name`` -- String used to represent the basis- ``Poly.maxpower`` -- Maximum value ``n`` such that ``p**n`` is allowed- ``Poly.nickname`` -- String used in printing
Creation--------
Methods for creating polynomial instances.
- ``Poly.basis(degree)`` -- Basis polynomial of given degree- ``Poly.identity()`` -- ``p`` where ``p(x) = x`` for all ``x``- ``Poly.fit(x, y, deg)`` -- ``p`` of degree ``deg`` with coefficients determined by the least-squares fit to the data ``x``, ``y``- ``Poly.fromroots(roots)`` -- ``p`` with specified roots- ``p.copy()`` -- Create a copy of ``p``
Conversion----------
Methods for converting a polynomial instance of one kind to another.
- ``p.cast(Poly)`` -- Convert ``p`` to instance of kind ``Poly``- ``p.convert(Poly)`` -- Convert ``p`` to instance of kind ``Poly`` or map between ``domain`` and ``window``
Calculus--------- ``p.deriv()`` -- Take the derivative of ``p``- ``p.integ()`` -- Integrate ``p``
Validation----------- ``Poly.has_samecoef(p1, p2)`` -- Check if coefficients match- ``Poly.has_samedomain(p1, p2)`` -- Check if domains match- ``Poly.has_sametype(p1, p2)`` -- Check if types match- ``Poly.has_samewindow(p1, p2)`` -- Check if windows match
Misc----- ``p.linspace()`` -- Return ``x, p(x)`` at equally-spaced points in ``domain``- ``p.mapparms()`` -- Return the parameters for the linear mapping between ``domain`` and ``window``.- ``p.roots()`` -- Return the roots of `p`.- ``p.trim()`` -- Remove trailing coefficients.- ``p.cutdeg(degree)`` -- Truncate p to given degree- ``p.truncate(size)`` -- Truncate p to given size
"""from .polynomial import Polynomialfrom .chebyshev import Chebyshevfrom .legendre import Legendrefrom .hermite import Hermitefrom .hermite_e import HermiteEfrom .laguerre import Laguerre
__all__ = [ "set_default_printstyle", "polynomial", "Polynomial", "chebyshev", "Chebyshev", "legendre", "Legendre", "hermite", "Hermite", "hermite_e", "HermiteE", "laguerre", "Laguerre",]
def set_default_printstyle(style): """ Set the default format for the string representation of polynomials.
Values for ``style`` must be valid inputs to ``__format__``, i.e. 'ascii' or 'unicode'.
Parameters ---------- style : str Format string for default printing style. Must be either 'ascii' or 'unicode'.
Notes ----- The default format depends on the platform: 'unicode' is used on Unix-based systems and 'ascii' on Windows. This determination is based on default font support for the unicode superscript and subscript ranges.
Examples -------- >>> p = np.polynomial.Polynomial([1, 2, 3]) >>> c = np.polynomial.Chebyshev([1, 2, 3]) >>> np.polynomial.set_default_printstyle('unicode') >>> print(p) 1.0 + 2.0·x + 3.0·x² >>> print(c) 1.0 + 2.0·T₁(x) + 3.0·T₂(x) >>> np.polynomial.set_default_printstyle('ascii') >>> print(p) 1.0 + 2.0 x + 3.0 x**2 >>> print(c) 1.0 + 2.0 T_1(x) + 3.0 T_2(x) >>> # Formatting supersedes all class/package-level defaults >>> print(f"{p:unicode}") 1.0 + 2.0·x + 3.0·x² """ if style not in ('unicode', 'ascii'): raise ValueError( f"Unsupported format string '{style}'. Valid options are 'ascii' " f"and 'unicode'" ) _use_unicode = True if style == 'ascii': _use_unicode = False from ._polybase import ABCPolyBase ABCPolyBase._use_unicode = _use_unicode
from numpy._pytesttester import PytestTestertest = PytestTester(__name__)del PytestTester12345678910111213141516171819202122from numpy._pytesttester import PytestTester
from numpy.polynomial import ( chebyshev as chebyshev, hermite as hermite, hermite_e as hermite_e, laguerre as laguerre, legendre as legendre, polynomial as polynomial,)from numpy.polynomial.chebyshev import Chebyshev as Chebyshevfrom numpy.polynomial.hermite import Hermite as Hermitefrom numpy.polynomial.hermite_e import HermiteE as HermiteEfrom numpy.polynomial.laguerre import Laguerre as Laguerrefrom numpy.polynomial.legendre import Legendre as Legendrefrom numpy.polynomial.polynomial import Polynomial as Polynomial
__all__: list[str]__path__: list[str]test: PytestTester
def set_default_printstyle(style): ...123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206"""Abstract base class for the various polynomial Classes.
The ABCPolyBase class provides the methods needed to implement the common APIfor the various polynomial classes. It operates as a mixin, but uses theabc module from the stdlib, hence it is only available for Python >= 2.6.
"""import osimport abcimport numbers
import numpy as npfrom . import polyutils as pu
__all__ = ['ABCPolyBase']
class ABCPolyBase(abc.ABC): """An abstract base class for immutable series classes.
ABCPolyBase provides the standard Python numerical methods '+', '-', '*', '//', '%', 'divmod', '**', and '()' along with the methods listed below.
.. versionadded:: 1.9.0
Parameters ---------- coef : array_like Series coefficients in order of increasing degree, i.e., ``(1, 2, 3)`` gives ``1*P_0(x) + 2*P_1(x) + 3*P_2(x)``, where ``P_i`` is the basis polynomials of degree ``i``. domain : (2,) array_like, optional Domain to use. The interval ``[domain[0], domain[1]]`` is mapped to the interval ``[window[0], window[1]]`` by shifting and scaling. The default value is the derived class domain. window : (2,) array_like, optional Window, see domain for its use. The default value is the derived class window. symbol : str, optional Symbol used to represent the independent variable in string representations of the polynomial expression, e.g. for printing. The symbol must be a valid Python identifier. Default value is 'x'.
.. versionadded:: 1.24
Attributes ---------- coef : (N,) ndarray Series coefficients in order of increasing degree. domain : (2,) ndarray Domain that is mapped to window. window : (2,) ndarray Window that domain is mapped to. symbol : str Symbol representing the independent variable.
Class Attributes ---------------- maxpower : int Maximum power allowed, i.e., the largest number ``n`` such that ``p(x)**n`` is allowed. This is to limit runaway polynomial size. domain : (2,) ndarray Default domain of the class. window : (2,) ndarray Default window of the class.
"""
# Not hashable __hash__ = None
# Opt out of numpy ufuncs and Python ops with ndarray subclasses. __array_ufunc__ = None
# Limit runaway size. T_n^m has degree n*m maxpower = 100
# Unicode character mappings for improved __str__ _superscript_mapping = str.maketrans({ "0": "⁰", "1": "¹", "2": "²", "3": "³", "4": "⁴", "5": "⁵", "6": "⁶", "7": "⁷", "8": "⁸", "9": "⁹" }) _subscript_mapping = str.maketrans({ "0": "₀", "1": "₁", "2": "₂", "3": "₃", "4": "₄", "5": "₅", "6": "₆", "7": "₇", "8": "₈", "9": "₉" }) # Some fonts don't support full unicode character ranges necessary for # the full set of superscripts and subscripts, including common/default # fonts in Windows shells/terminals. Therefore, default to ascii-only # printing on windows. _use_unicode = not os.name == 'nt'
@property def symbol(self): return self._symbol
@property @abc.abstractmethod def domain(self): pass
@property @abc.abstractmethod def window(self): pass
@property @abc.abstractmethod def basis_name(self): pass
@staticmethod @abc.abstractmethod def _add(c1, c2): pass
@staticmethod @abc.abstractmethod def _sub(c1, c2): pass
@staticmethod @abc.abstractmethod def _mul(c1, c2): pass
@staticmethod @abc.abstractmethod def _div(c1, c2): pass
@staticmethod @abc.abstractmethod def _pow(c, pow, maxpower=None): pass
@staticmethod @abc.abstractmethod def _val(x, c): pass
@staticmethod @abc.abstractmethod def _int(c, m, k, lbnd, scl): pass
@staticmethod @abc.abstractmethod def _der(c, m, scl): pass
@staticmethod @abc.abstractmethod def _fit(x, y, deg, rcond, full): pass
@staticmethod @abc.abstractmethod def _line(off, scl): pass
@staticmethod @abc.abstractmethod def _roots(c): pass
@staticmethod @abc.abstractmethod def _fromroots(r): pass
def has_samecoef(self, other): """Check if coefficients match.
.. versionadded:: 1.6.0
Parameters ---------- other : class instance The other class must have the ``coef`` attribute.
Returns ------- bool : boolean True if the coefficients are the same, False otherwise.
""" if len(self.coef) != len(other.coef): return False elif not np.all(self.coef == other.coef): return False else: return True
def has_samedomain(self, other): """Check if domains match.
.. versionadded:: 1.6.0
Parameters ---------- other : class instance The other class must have the ``domain`` attribute.
Returns ------- bool : boolean True if the domains are the same, False otherwise.
""" return np.all(self.domain == other.domain)
def has_samewindow(self, other): """Check if windows match.
.. versionadded:: 1.6.0
Parameters ---------- other : class instance The other class must have the ``window`` attribute.
Returns ------- bool : boolean True if the windows are the same, False otherwise.
""" return np.all(self.window == other.window)
def has_sametype(self, other): """Check if types match.
.. versionadded:: 1.7.0
Parameters ---------- other : object Class instance.
Returns ------- bool : boolean True if other is same class as self
""" return isinstance(other, self.__class__)
def _get_coefficients(self, other): """Interpret other as polynomial coefficients.
The `other` argument is checked to see if it is of the same class as self with identical domain and window. If so, return its coefficients, otherwise return `other`.
.. versionadded:: 1.9.0
Parameters ---------- other : anything Object to be checked.
Returns ------- coef The coefficients of`other` if it is a compatible instance, of ABCPolyBase, otherwise `other`.
Raises ------ TypeError When `other` is an incompatible instance of ABCPolyBase.
""" if isinstance(other, ABCPolyBase): if not isinstance(other, self.__class__): raise TypeError("Polynomial types differ") elif not np.all(self.domain == other.domain): raise TypeError("Domains differ") elif not np.all(self.window == other.window): raise TypeError("Windows differ") elif self.symbol != other.symbol: raise ValueError("Polynomial symbols differ") return other.coef return other
def __init__(self, coef, domain=None, window=None, symbol='x'): [coef] = pu.as_series([coef], trim=False) self.coef = coef
if domain is not None: [domain] = pu.as_series([domain], trim=False) if len(domain) != 2: raise ValueError("Domain has wrong number of elements.") self.domain = domain
if window is not None: [window] = pu.as_series([window], trim=False) if len(window) != 2: raise ValueError("Window has wrong number of elements.") self.window = window
# Validation for symbol try: if not symbol.isidentifier(): raise ValueError( "Symbol string must be a valid Python identifier" ) # If a user passes in something other than a string, the above # results in an AttributeError. Catch this and raise a more # informative exception except AttributeError: raise TypeError("Symbol must be a non-empty string")
self._symbol = symbol
def __repr__(self): coef = repr(self.coef)[6:-1] domain = repr(self.domain)[6:-1] window = repr(self.window)[6:-1] name = self.__class__.__name__ return (f"{name}({coef}, domain={domain}, window={window}, " f"symbol='{self.symbol}')")
def __format__(self, fmt_str): if fmt_str == '': return self.__str__() if fmt_str not in ('ascii', 'unicode'): raise ValueError( f"Unsupported format string '{fmt_str}' passed to " f"{self.__class__}.__format__. Valid options are " f"'ascii' and 'unicode'" ) if fmt_str == 'ascii': return self._generate_string(self._str_term_ascii) return self._generate_string(self._str_term_unicode)
def __str__(self): if self._use_unicode: return self._generate_string(self._str_term_unicode) return self._generate_string(self._str_term_ascii)
def _generate_string(self, term_method): """ Generate the full string representation of the polynomial, using ``term_method`` to generate each polynomial term. """ # Get configuration for line breaks linewidth = np.get_printoptions().get('linewidth', 75) if linewidth < 1: linewidth = 1 out = pu.format_float(self.coef[0]) for i, coef in enumerate(self.coef[1:]): out += " " power = str(i + 1) # Polynomial coefficient # The coefficient array can be an object array with elements that # will raise a TypeError with >= 0 (e.g. strings or Python # complex). In this case, represent the coefficient as-is. try: if coef >= 0: next_term = f"+ " + pu.format_float(coef, parens=True) else: next_term = f"- " + pu.format_float(-coef, parens=True) except TypeError: next_term = f"+ {coef}" # Polynomial term next_term += term_method(power, self.symbol) # Length of the current line with next term added line_len = len(out.split('\n')[-1]) + len(next_term) # If not the last term in the polynomial, it will be two # characters longer due to the +/- with the next term if i < len(self.coef[1:]) - 1: line_len += 2 # Handle linebreaking if line_len >= linewidth: next_term = next_term.replace(" ", "\n", 1) out += next_term return out
@classmethod def _str_term_unicode(cls, i, arg_str): """ String representation of single polynomial term using unicode characters for superscripts and subscripts. """ if cls.basis_name is None: raise NotImplementedError( "Subclasses must define either a basis_name, or override " "_str_term_unicode(cls, i, arg_str)" ) return (f"·{cls.basis_name}{i.translate(cls._subscript_mapping)}" f"({arg_str})")
@classmethod def _str_term_ascii(cls, i, arg_str): """ String representation of a single polynomial term using ** and _ to represent superscripts and subscripts, respectively. """ if cls.basis_name is None: raise NotImplementedError( "Subclasses must define either a basis_name, or override " "_str_term_ascii(cls, i, arg_str)" ) return f" {cls.basis_name}_{i}({arg_str})"
@classmethod def _repr_latex_term(cls, i, arg_str, needs_parens): if cls.basis_name is None: raise NotImplementedError( "Subclasses must define either a basis name, or override " "_repr_latex_term(i, arg_str, needs_parens)") # since we always add parens, we don't care if the expression needs them return f"{{{cls.basis_name}}}_{{{i}}}({arg_str})"
@staticmethod def _repr_latex_scalar(x, parens=False): # TODO: we're stuck with disabling math formatting until we handle # exponents in this function return r'\text{{{}}}'.format(pu.format_float(x, parens=parens))
def _repr_latex_(self): # get the scaled argument string to the basis functions off, scale = self.mapparms() if off == 0 and scale == 1: term = self.symbol needs_parens = False elif scale == 1: term = f"{self._repr_latex_scalar(off)} + {self.symbol}" needs_parens = True elif off == 0: term = f"{self._repr_latex_scalar(scale)}{self.symbol}" needs_parens = True else: term = ( f"{self._repr_latex_scalar(off)} + " f"{self._repr_latex_scalar(scale)}{self.symbol}" ) needs_parens = True
mute = r"\color{{LightGray}}{{{}}}".format
parts = [] for i, c in enumerate(self.coef): # prevent duplication of + and - signs if i == 0: coef_str = f"{self._repr_latex_scalar(c)}" elif not isinstance(c, numbers.Real): coef_str = f" + ({self._repr_latex_scalar(c)})" elif not np.signbit(c): coef_str = f" + {self._repr_latex_scalar(c, parens=True)}" else: coef_str = f" - {self._repr_latex_scalar(-c, parens=True)}"
# produce the string for the term term_str = self._repr_latex_term(i, term, needs_parens) if term_str == '1': part = coef_str else: part = rf"{coef_str}\,{term_str}"
if c == 0: part = mute(part)
parts.append(part)
if parts: body = ''.join(parts) else: # in case somehow there are no coefficients at all body = '0'
return rf"${self.symbol} \mapsto {body}$"
# Pickle and copy
def __getstate__(self): ret = self.__dict__.copy() ret['coef'] = self.coef.copy() ret['domain'] = self.domain.copy() ret['window'] = self.window.copy() ret['symbol'] = self.symbol return ret
def __setstate__(self, dict): self.__dict__ = dict
# Call
def __call__(self, arg): off, scl = pu.mapparms(self.domain, self.window) arg = off + scl*arg return self._val(arg, self.coef)
def __iter__(self): return iter(self.coef)
def __len__(self): return len(self.coef)
# Numeric properties.
def __neg__(self): return self.__class__( -self.coef, self.domain, self.window, self.symbol )
def __pos__(self): return self
def __add__(self, other): othercoef = self._get_coefficients(other) try: coef = self._add(self.coef, othercoef) except Exception: return NotImplemented return self.__class__(coef, self.domain, self.window, self.symbol)
def __sub__(self, other): othercoef = self._get_coefficients(other) try: coef = self._sub(self.coef, othercoef) except Exception: return NotImplemented return self.__class__(coef, self.domain, self.window, self.symbol)
def __mul__(self, other): othercoef = self._get_coefficients(other) try: coef = self._mul(self.coef, othercoef) except Exception: return NotImplemented return self.__class__(coef, self.domain, self.window, self.symbol)
def __truediv__(self, other): # there is no true divide if the rhs is not a Number, although it # could return the first n elements of an infinite series. # It is hard to see where n would come from, though. if not isinstance(other, numbers.Number) or isinstance(other, bool): raise TypeError( f"unsupported types for true division: " f"'{type(self)}', '{type(other)}'" ) return self.__floordiv__(other)
def __floordiv__(self, other): res = self.__divmod__(other) if res is NotImplemented: return res return res[0]
def __mod__(self, other): res = self.__divmod__(other) if res is NotImplemented: return res return res[1]
def __divmod__(self, other): othercoef = self._get_coefficients(other) try: quo, rem = self._div(self.coef, othercoef) except ZeroDivisionError: raise except Exception: return NotImplemented quo = self.__class__(quo, self.domain, self.window, self.symbol) rem = self.__class__(rem, self.domain, self.window, self.symbol) return quo, rem
def __pow__(self, other): coef = self._pow(self.coef, other, maxpower=self.maxpower) res = self.__class__(coef, self.domain, self.window, self.symbol) return res
def __radd__(self, other): try: coef = self._add(other, self.coef) except Exception: return NotImplemented return self.__class__(coef, self.domain, self.window, self.symbol)
def __rsub__(self, other): try: coef = self._sub(other, self.coef) except Exception: return NotImplemented return self.__class__(coef, self.domain, self.window, self.symbol)
def __rmul__(self, other): try: coef = self._mul(other, self.coef) except Exception: return NotImplemented return self.__class__(coef, self.domain, self.window, self.symbol)
def __rdiv__(self, other): # set to __floordiv__ /. return self.__rfloordiv__(other)
def __rtruediv__(self, other): # An instance of ABCPolyBase is not considered a # Number. return NotImplemented
def __rfloordiv__(self, other): res = self.__rdivmod__(other) if res is NotImplemented: return res return res[0]
def __rmod__(self, other): res = self.__rdivmod__(other) if res is NotImplemented: return res return res[1]
def __rdivmod__(self, other): try: quo, rem = self._div(other, self.coef) except ZeroDivisionError: raise except Exception: return NotImplemented quo = self.__class__(quo, self.domain, self.window, self.symbol) rem = self.__class__(rem, self.domain, self.window, self.symbol) return quo, rem
def __eq__(self, other): res = (isinstance(other, self.__class__) and np.all(self.domain == other.domain) and np.all(self.window == other.window) and (self.coef.shape == other.coef.shape) and np.all(self.coef == other.coef) and (self.symbol == other.symbol)) return res
def __ne__(self, other): return not self.__eq__(other)
# # Extra methods. #
def copy(self): """Return a copy.
Returns ------- new_series : series Copy of self.
""" return self.__class__(self.coef, self.domain, self.window, self.symbol)
def degree(self): """The degree of the series.
.. versionadded:: 1.5.0
Returns ------- degree : int Degree of the series, one less than the number of coefficients.
Examples --------
Create a polynomial object for ``1 + 7*x + 4*x**2``:
>>> poly = np.polynomial.Polynomial([1, 7, 4]) >>> print(poly) 1.0 + 7.0·x + 4.0·x² >>> poly.degree() 2
Note that this method does not check for non-zero coefficients. You must trim the polynomial to remove any trailing zeroes:
>>> poly = np.polynomial.Polynomial([1, 7, 0]) >>> print(poly) 1.0 + 7.0·x + 0.0·x² >>> poly.degree() 2 >>> poly.trim().degree() 1
""" return len(self) - 1
def cutdeg(self, deg): """Truncate series to the given degree.
Reduce the degree of the series to `deg` by discarding the high order terms. If `deg` is greater than the current degree a copy of the current series is returned. This can be useful in least squares where the coefficients of the high degree terms may be very small.
.. versionadded:: 1.5.0
Parameters ---------- deg : non-negative int The series is reduced to degree `deg` by discarding the high order terms. The value of `deg` must be a non-negative integer.
Returns ------- new_series : series New instance of series with reduced degree.
""" return self.truncate(deg + 1)
def trim(self, tol=0): """Remove trailing coefficients
Remove trailing coefficients until a coefficient is reached whose absolute value greater than `tol` or the beginning of the series is reached. If all the coefficients would be removed the series is set to ``[0]``. A new series instance is returned with the new coefficients. The current instance remains unchanged.
Parameters ---------- tol : non-negative number. All trailing coefficients less than `tol` will be removed.
Returns ------- new_series : series New instance of series with trimmed coefficients.
""" coef = pu.trimcoef(self.coef, tol) return self.__class__(coef, self.domain, self.window, self.symbol)
def truncate(self, size): """Truncate series to length `size`.
Reduce the series to length `size` by discarding the high degree terms. The value of `size` must be a positive integer. This can be useful in least squares where the coefficients of the high degree terms may be very small.
Parameters ---------- size : positive int The series is reduced to length `size` by discarding the high degree terms. The value of `size` must be a positive integer.
Returns ------- new_series : series New instance of series with truncated coefficients.
""" isize = int(size) if isize != size or isize < 1: raise ValueError("size must be a positive integer") if isize >= len(self.coef): coef = self.coef else: coef = self.coef[:isize] return self.__class__(coef, self.domain, self.window, self.symbol)
def convert(self, domain=None, kind=None, window=None): """Convert series to a different kind and/or domain and/or window.
Parameters ---------- domain : array_like, optional The domain of the converted series. If the value is None, the default domain of `kind` is used. kind : class, optional The polynomial series type class to which the current instance should be converted. If kind is None, then the class of the current instance is used. window : array_like, optional The window of the converted series. If the value is None, the default window of `kind` is used.
Returns ------- new_series : series The returned class can be of different type than the current instance and/or have a different domain and/or different window.
Notes ----- Conversion between domains and class types can result in numerically ill defined series.
""" if kind is None: kind = self.__class__ if domain is None: domain = kind.domain if window is None: window = kind.window return self(kind.identity(domain, window=window, symbol=self.symbol))
def mapparms(self): """Return the mapping parameters.
The returned values define a linear map ``off + scl*x`` that is applied to the input arguments before the series is evaluated. The map depends on the ``domain`` and ``window``; if the current ``domain`` is equal to the ``window`` the resulting map is the identity. If the coefficients of the series instance are to be used by themselves outside this class, then the linear function must be substituted for the ``x`` in the standard representation of the base polynomials.
Returns ------- off, scl : float or complex The mapping function is defined by ``off + scl*x``.
Notes ----- If the current domain is the interval ``[l1, r1]`` and the window is ``[l2, r2]``, then the linear mapping function ``L`` is defined by the equations::
L(l1) = l2 L(r1) = r2
""" return pu.mapparms(self.domain, self.window)
def integ(self, m=1, k=[], lbnd=None): """Integrate.
Return a series instance that is the definite integral of the current series.
Parameters ---------- m : non-negative int The number of integrations to perform. k : array_like Integration constants. The first constant is applied to the first integration, the second to the second, and so on. The list of values must less than or equal to `m` in length and any missing values are set to zero. lbnd : Scalar The lower bound of the definite integral.
Returns ------- new_series : series A new series representing the integral. The domain is the same as the domain of the integrated series.
""" off, scl = self.mapparms() if lbnd is None: lbnd = 0 else: lbnd = off + scl*lbnd coef = self._int(self.coef, m, k, lbnd, 1./scl) return self.__class__(coef, self.domain, self.window, self.symbol)
def deriv(self, m=1): """Differentiate.
Return a series instance of that is the derivative of the current series.
Parameters ---------- m : non-negative int Find the derivative of order `m`.
Returns ------- new_series : series A new series representing the derivative. The domain is the same as the domain of the differentiated series.
""" off, scl = self.mapparms() coef = self._der(self.coef, m, scl) return self.__class__(coef, self.domain, self.window, self.symbol)
def roots(self): """Return the roots of the series polynomial.
Compute the roots for the series. Note that the accuracy of the roots decreases the further outside the `domain` they lie.
Returns ------- roots : ndarray Array containing the roots of the series.
""" roots = self._roots(self.coef) return pu.mapdomain(roots, self.window, self.domain)
def linspace(self, n=100, domain=None): """Return x, y values at equally spaced points in domain.
Returns the x, y values at `n` linearly spaced points across the domain. Here y is the value of the polynomial at the points x. By default the domain is the same as that of the series instance. This method is intended mostly as a plotting aid.
.. versionadded:: 1.5.0
Parameters ---------- n : int, optional Number of point pairs to return. The default value is 100. domain : {None, array_like}, optional If not None, the specified domain is used instead of that of the calling instance. It should be of the form ``[beg,end]``. The default is None which case the class domain is used.
Returns ------- x, y : ndarray x is equal to linspace(self.domain[0], self.domain[1], n) and y is the series evaluated at element of x.
""" if domain is None: domain = self.domain x = np.linspace(domain[0], domain[1], n) y = self(x) return x, y
@classmethod def fit(cls, x, y, deg, domain=None, rcond=None, full=False, w=None, window=None, symbol='x'): """Least squares fit to data.
Return a series instance that is the least squares fit to the data `y` sampled at `x`. The domain of the returned instance can be specified and this will often result in a superior fit with less chance of ill conditioning.
Parameters ---------- x : array_like, shape (M,) x-coordinates of the M sample points ``(x[i], y[i])``. y : array_like, shape (M,) y-coordinates of the M sample points ``(x[i], y[i])``. deg : int or 1-D array_like Degree(s) of the fitting polynomials. If `deg` is a single integer all terms up to and including the `deg`'th term are included in the fit. For NumPy versions >= 1.11.0 a list of integers specifying the degrees of the terms to include may be used instead. domain : {None, [beg, end], []}, optional Domain to use for the returned series. If ``None``, then a minimal domain that covers the points `x` is chosen. If ``[]`` the class domain is used. The default value was the class domain in NumPy 1.4 and ``None`` in later versions. The ``[]`` option was added in numpy 1.5.0. rcond : float, optional Relative condition number of the fit. Singular values smaller than this relative to the largest singular value will be ignored. The default value is len(x)*eps, where eps is the relative precision of the float type, about 2e-16 in most cases. full : bool, optional Switch determining nature of return value. When it is False (the default) just the coefficients are returned, when True diagnostic information from the singular value decomposition is also returned. w : array_like, shape (M,), optional Weights. If not None, the weight ``w[i]`` applies to the unsquared residual ``y[i] - y_hat[i]`` at ``x[i]``. Ideally the weights are chosen so that the errors of the products ``w[i]*y[i]`` all have the same variance. When using inverse-variance weighting, use ``w[i] = 1/sigma(y[i])``. The default value is None.
.. versionadded:: 1.5.0 window : {[beg, end]}, optional Window to use for the returned series. The default value is the default class domain
.. versionadded:: 1.6.0 symbol : str, optional Symbol representing the independent variable. Default is 'x'.
Returns ------- new_series : series A series that represents the least squares fit to the data and has the domain and window specified in the call. If the coefficients for the unscaled and unshifted basis polynomials are of interest, do ``new_series.convert().coef``.
[resid, rank, sv, rcond] : list These values are only returned if ``full == True``
- resid -- sum of squared residuals of the least squares fit - rank -- the numerical rank of the scaled Vandermonde matrix - sv -- singular values of the scaled Vandermonde matrix - rcond -- value of `rcond`.
For more details, see `linalg.lstsq`.
""" if domain is None: domain = pu.getdomain(x) elif type(domain) is list and len(domain) == 0: domain = cls.domain
if window is None: window = cls.window
xnew = pu.mapdomain(x, domain, window) res = cls._fit(xnew, y, deg, w=w, rcond=rcond, full=full) if full: [coef, status] = res return ( cls(coef, domain=domain, window=window, symbol=symbol), status ) else: coef = res return cls(coef, domain=domain, window=window, symbol=symbol)
@classmethod def fromroots(cls, roots, domain=[], window=None, symbol='x'): """Return series instance that has the specified roots.
Returns a series representing the product ``(x - r[0])*(x - r[1])*...*(x - r[n-1])``, where ``r`` is a list of roots.
Parameters ---------- roots : array_like List of roots. domain : {[], None, array_like}, optional Domain for the resulting series. If None the domain is the interval from the smallest root to the largest. If [] the domain is the class domain. The default is []. window : {None, array_like}, optional Window for the returned series. If None the class window is used. The default is None. symbol : str, optional Symbol representing the independent variable. Default is 'x'.
Returns ------- new_series : series Series with the specified roots.
""" [roots] = pu.as_series([roots], trim=False) if domain is None: domain = pu.getdomain(roots) elif type(domain) is list and len(domain) == 0: domain = cls.domain
if window is None: window = cls.window
deg = len(roots) off, scl = pu.mapparms(domain, window) rnew = off + scl*roots coef = cls._fromroots(rnew) / scl**deg return cls(coef, domain=domain, window=window, symbol=symbol)
@classmethod def identity(cls, domain=None, window=None, symbol='x'): """Identity function.
If ``p`` is the returned series, then ``p(x) == x`` for all values of x.
Parameters ---------- domain : {None, array_like}, optional If given, the array must be of the form ``[beg, end]``, where ``beg`` and ``end`` are the endpoints of the domain. If None is given then the class domain is used. The default is None. window : {None, array_like}, optional If given, the resulting array must be if the form ``[beg, end]``, where ``beg`` and ``end`` are the endpoints of the window. If None is given then the class window is used. The default is None. symbol : str, optional Symbol representing the independent variable. Default is 'x'.
Returns ------- new_series : series Series of representing the identity.
""" if domain is None: domain = cls.domain if window is None: window = cls.window off, scl = pu.mapparms(window, domain) coef = cls._line(off, scl) return cls(coef, domain, window, symbol)
@classmethod def basis(cls, deg, domain=None, window=None, symbol='x'): """Series basis polynomial of degree `deg`.
Returns the series representing the basis polynomial of degree `deg`.
.. versionadded:: 1.7.0
Parameters ---------- deg : int Degree of the basis polynomial for the series. Must be >= 0. domain : {None, array_like}, optional If given, the array must be of the form ``[beg, end]``, where ``beg`` and ``end`` are the endpoints of the domain. If None is given then the class domain is used. The default is None. window : {None, array_like}, optional If given, the resulting array must be if the form ``[beg, end]``, where ``beg`` and ``end`` are the endpoints of the window. If None is given then the class window is used. The default is None. symbol : str, optional Symbol representing the independent variable. Default is 'x'.
Returns ------- new_series : series A series with the coefficient of the `deg` term set to one and all others zero.
""" if domain is None: domain = cls.domain if window is None: window = cls.window ideg = int(deg)
if ideg != deg or ideg < 0: raise ValueError("deg must be non-negative integer") return cls([0]*ideg + [1], domain, window, symbol)
@classmethod def cast(cls, series, domain=None, window=None): """Convert series to series of this class.
The `series` is expected to be an instance of some polynomial series of one of the types supported by by the numpy.polynomial module, but could be some other class that supports the convert method.
.. versionadded:: 1.7.0
Parameters ---------- series : series The series instance to be converted. domain : {None, array_like}, optional If given, the array must be of the form ``[beg, end]``, where ``beg`` and ``end`` are the endpoints of the domain. If None is given then the class domain is used. The default is None. window : {None, array_like}, optional If given, the resulting array must be if the form ``[beg, end]``, where ``beg`` and ``end`` are the endpoints of the window. If None is given then the class window is used. The default is None.
Returns ------- new_series : series A series of the same kind as the calling class and equal to `series` when evaluated.
See Also -------- convert : similar instance method
""" if domain is None: domain = cls.domain if window is None: window = cls.window return series.convert(domain, cls, window)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071import abcfrom typing import Any, ClassVar
__all__: list[str]
class ABCPolyBase(abc.ABC): __hash__: ClassVar[None] # type: ignore[assignment] __array_ufunc__: ClassVar[None] maxpower: ClassVar[int] coef: Any @property def symbol(self) -> str: ... @property @abc.abstractmethod def domain(self): ... @property @abc.abstractmethod def window(self): ... @property @abc.abstractmethod def basis_name(self): ... def has_samecoef(self, other): ... def has_samedomain(self, other): ... def has_samewindow(self, other): ... def has_sametype(self, other): ... def __init__(self, coef, domain=..., window=..., symbol: str = ...) -> None: ... def __format__(self, fmt_str): ... def __call__(self, arg): ... def __iter__(self): ... def __len__(self): ... def __neg__(self): ... def __pos__(self): ... def __add__(self, other): ... def __sub__(self, other): ... def __mul__(self, other): ... def __truediv__(self, other): ... def __floordiv__(self, other): ... def __mod__(self, other): ... def __divmod__(self, other): ... def __pow__(self, other): ... def __radd__(self, other): ... def __rsub__(self, other): ... def __rmul__(self, other): ... def __rdiv__(self, other): ... def __rtruediv__(self, other): ... def __rfloordiv__(self, other): ... def __rmod__(self, other): ... def __rdivmod__(self, other): ... def __eq__(self, other): ... def __ne__(self, other): ... def copy(self): ... def degree(self): ... def cutdeg(self, deg): ... def trim(self, tol=...): ... def truncate(self, size): ... def convert(self, domain=..., kind=..., window=...): ... def mapparms(self): ... def integ(self, m=..., k = ..., lbnd=...): ... def deriv(self, m=...): ... def roots(self): ... def linspace(self, n=..., domain=...): ... @classmethod def fit(cls, x, y, deg, domain=..., rcond=..., full=..., w=..., window=...): ... @classmethod def fromroots(cls, roots, domain = ..., window=...): ... @classmethod def identity(cls, domain=..., window=...): ... @classmethod def basis(cls, deg, domain=..., window=...): ... @classmethod def cast(cls, series, domain=..., window=...): ...123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082"""====================================================Chebyshev Series (:mod:`numpy.polynomial.chebyshev`)====================================================
This module provides a number of objects (mostly functions) useful fordealing with Chebyshev series, including a `Chebyshev` class thatencapsulates the usual arithmetic operations. (General informationon how this module represents and works with such polynomials is in thedocstring for its "parent" sub-package, `numpy.polynomial`).
Classes-------
.. autosummary:: :toctree: generated/
Chebyshev
Constants---------
.. autosummary:: :toctree: generated/
chebdomain chebzero chebone chebx
Arithmetic----------
.. autosummary:: :toctree: generated/
chebadd chebsub chebmulx chebmul chebdiv chebpow chebval chebval2d chebval3d chebgrid2d chebgrid3d
Calculus--------
.. autosummary:: :toctree: generated/
chebder chebint
Misc Functions--------------
.. autosummary:: :toctree: generated/
chebfromroots chebroots chebvander chebvander2d chebvander3d chebgauss chebweight chebcompanion chebfit chebpts1 chebpts2 chebtrim chebline cheb2poly poly2cheb chebinterpolate
See also--------`numpy.polynomial`
Notes-----The implementations of multiplication, division, integration, anddifferentiation use the algebraic identities [1]_:
.. math:: T_n(x) = \\frac{z^n + z^{-n}}{2} \\\\ z\\frac{dx}{dz} = \\frac{z - z^{-1}}{2}.
where
.. math:: x = \\frac{z + z^{-1}}{2}.
These identities allow a Chebyshev series to be expressed as a finite,symmetric Laurent series. In this module, this sort of Laurent seriesis referred to as a "z-series."
References----------.. [1] A. T. Benjamin, et al., "Combinatorial Trigonometry with Chebyshev Polynomials," *Journal of Statistical Planning and Inference 14*, 2008 (https://web.archive.org/web/20080221202153/https://www.math.hmc.edu/~benjamin/papers/CombTrig.pdf, pg. 4)
"""import numpy as npimport numpy.linalg as lafrom numpy.core.multiarray import normalize_axis_index
from . import polyutils as pufrom ._polybase import ABCPolyBase
__all__ = [ 'chebzero', 'chebone', 'chebx', 'chebdomain', 'chebline', 'chebadd', 'chebsub', 'chebmulx', 'chebmul', 'chebdiv', 'chebpow', 'chebval', 'chebder', 'chebint', 'cheb2poly', 'poly2cheb', 'chebfromroots', 'chebvander', 'chebfit', 'chebtrim', 'chebroots', 'chebpts1', 'chebpts2', 'Chebyshev', 'chebval2d', 'chebval3d', 'chebgrid2d', 'chebgrid3d', 'chebvander2d', 'chebvander3d', 'chebcompanion', 'chebgauss', 'chebweight', 'chebinterpolate']
chebtrim = pu.trimcoef
## A collection of functions for manipulating z-series. These are private# functions and do minimal error checking.#
def _cseries_to_zseries(c): """Convert Chebyshev series to z-series.
Convert a Chebyshev series to the equivalent z-series. The result is never an empty array. The dtype of the return is the same as that of the input. No checks are run on the arguments as this routine is for internal use.
Parameters ---------- c : 1-D ndarray Chebyshev coefficients, ordered from low to high
Returns ------- zs : 1-D ndarray Odd length symmetric z-series, ordered from low to high.
""" n = c.size zs = np.zeros(2*n-1, dtype=c.dtype) zs[n-1:] = c/2 return zs + zs[::-1]
def _zseries_to_cseries(zs): """Convert z-series to a Chebyshev series.
Convert a z series to the equivalent Chebyshev series. The result is never an empty array. The dtype of the return is the same as that of the input. No checks are run on the arguments as this routine is for internal use.
Parameters ---------- zs : 1-D ndarray Odd length symmetric z-series, ordered from low to high.
Returns ------- c : 1-D ndarray Chebyshev coefficients, ordered from low to high.
""" n = (zs.size + 1)//2 c = zs[n-1:].copy() c[1:n] *= 2 return c
def _zseries_mul(z1, z2): """Multiply two z-series.
Multiply two z-series to produce a z-series.
Parameters ---------- z1, z2 : 1-D ndarray The arrays must be 1-D but this is not checked.
Returns ------- product : 1-D ndarray The product z-series.
Notes ----- This is simply convolution. If symmetric/anti-symmetric z-series are denoted by S/A then the following rules apply:
S*S, A*A -> S S*A, A*S -> A
""" return np.convolve(z1, z2)
def _zseries_div(z1, z2): """Divide the first z-series by the second.
Divide `z1` by `z2` and return the quotient and remainder as z-series. Warning: this implementation only applies when both z1 and z2 have the same symmetry, which is sufficient for present purposes.
Parameters ---------- z1, z2 : 1-D ndarray The arrays must be 1-D and have the same symmetry, but this is not checked.
Returns -------
(quotient, remainder) : 1-D ndarrays Quotient and remainder as z-series.
Notes ----- This is not the same as polynomial division on account of the desired form of the remainder. If symmetric/anti-symmetric z-series are denoted by S/A then the following rules apply:
S/S -> S,S A/A -> S,A
The restriction to types of the same symmetry could be fixed but seems like unneeded generality. There is no natural form for the remainder in the case where there is no symmetry.
""" z1 = z1.copy() z2 = z2.copy() lc1 = len(z1) lc2 = len(z2) if lc2 == 1: z1 /= z2 return z1, z1[:1]*0 elif lc1 < lc2: return z1[:1]*0, z1 else: dlen = lc1 - lc2 scl = z2[0] z2 /= scl quo = np.empty(dlen + 1, dtype=z1.dtype) i = 0 j = dlen while i < j: r = z1[i] quo[i] = z1[i] quo[dlen - i] = r tmp = r*z2 z1[i:i+lc2] -= tmp z1[j:j+lc2] -= tmp i += 1 j -= 1 r = z1[i] quo[i] = r tmp = r*z2 z1[i:i+lc2] -= tmp quo /= scl rem = z1[i+1:i-1+lc2].copy() return quo, rem
def _zseries_der(zs): """Differentiate a z-series.
The derivative is with respect to x, not z. This is achieved using the chain rule and the value of dx/dz given in the module notes.
Parameters ---------- zs : z-series The z-series to differentiate.
Returns ------- derivative : z-series The derivative
Notes ----- The zseries for x (ns) has been multiplied by two in order to avoid using floats that are incompatible with Decimal and likely other specialized scalar types. This scaling has been compensated by multiplying the value of zs by two also so that the two cancels in the division.
""" n = len(zs)//2 ns = np.array([-1, 0, 1], dtype=zs.dtype) zs *= np.arange(-n, n+1)*2 d, r = _zseries_div(zs, ns) return d
def _zseries_int(zs): """Integrate a z-series.
The integral is with respect to x, not z. This is achieved by a change of variable using dx/dz given in the module notes.
Parameters ---------- zs : z-series The z-series to integrate
Returns ------- integral : z-series The indefinite integral
Notes ----- The zseries for x (ns) has been multiplied by two in order to avoid using floats that are incompatible with Decimal and likely other specialized scalar types. This scaling has been compensated by dividing the resulting zs by two.
""" n = 1 + len(zs)//2 ns = np.array([-1, 0, 1], dtype=zs.dtype) zs = _zseries_mul(zs, ns) div = np.arange(-n, n+1)*2 zs[:n] /= div[:n] zs[n+1:] /= div[n+1:] zs[n] = 0 return zs
## Chebyshev series functions#
def poly2cheb(pol): """ Convert a polynomial to a Chebyshev series.
Convert an array representing the coefficients of a polynomial (relative to the "standard" basis) ordered from lowest degree to highest, to an array of the coefficients of the equivalent Chebyshev series, ordered from lowest to highest degree.
Parameters ---------- pol : array_like 1-D array containing the polynomial coefficients
Returns ------- c : ndarray 1-D array containing the coefficients of the equivalent Chebyshev series.
See Also -------- cheb2poly
Notes ----- The easy way to do conversions between polynomial basis sets is to use the convert method of a class instance.
Examples -------- >>> from numpy import polynomial as P >>> p = P.Polynomial(range(4)) >>> p Polynomial([0., 1., 2., 3.], domain=[-1, 1], window=[-1, 1]) >>> c = p.convert(kind=P.Chebyshev) >>> c Chebyshev([1. , 3.25, 1. , 0.75], domain=[-1., 1.], window=[-1., 1.]) >>> P.chebyshev.poly2cheb(range(4)) array([1. , 3.25, 1. , 0.75])
""" [pol] = pu.as_series([pol]) deg = len(pol) - 1 res = 0 for i in range(deg, -1, -1): res = chebadd(chebmulx(res), pol[i]) return res
def cheb2poly(c): """ Convert a Chebyshev series to a polynomial.
Convert an array representing the coefficients of a Chebyshev series, ordered from lowest degree to highest, to an array of the coefficients of the equivalent polynomial (relative to the "standard" basis) ordered from lowest to highest degree.
Parameters ---------- c : array_like 1-D array containing the Chebyshev series coefficients, ordered from lowest order term to highest.
Returns ------- pol : ndarray 1-D array containing the coefficients of the equivalent polynomial (relative to the "standard" basis) ordered from lowest order term to highest.
See Also -------- poly2cheb
Notes ----- The easy way to do conversions between polynomial basis sets is to use the convert method of a class instance.
Examples -------- >>> from numpy import polynomial as P >>> c = P.Chebyshev(range(4)) >>> c Chebyshev([0., 1., 2., 3.], domain=[-1, 1], window=[-1, 1]) >>> p = c.convert(kind=P.Polynomial) >>> p Polynomial([-2., -8., 4., 12.], domain=[-1., 1.], window=[-1., 1.]) >>> P.chebyshev.cheb2poly(range(4)) array([-2., -8., 4., 12.])
""" from .polynomial import polyadd, polysub, polymulx
[c] = pu.as_series([c]) n = len(c) if n < 3: return c else: c0 = c[-2] c1 = c[-1] # i is the current degree of c1 for i in range(n - 1, 1, -1): tmp = c0 c0 = polysub(c[i - 2], c1) c1 = polyadd(tmp, polymulx(c1)*2) return polyadd(c0, polymulx(c1))
## These are constant arrays are of integer type so as to be compatible# with the widest range of other types, such as Decimal.#
# Chebyshev default domain.chebdomain = np.array([-1, 1])
# Chebyshev coefficients representing zero.chebzero = np.array([0])
# Chebyshev coefficients representing one.chebone = np.array([1])
# Chebyshev coefficients representing the identity x.chebx = np.array([0, 1])
def chebline(off, scl): """ Chebyshev series whose graph is a straight line.
Parameters ---------- off, scl : scalars The specified line is given by ``off + scl*x``.
Returns ------- y : ndarray This module's representation of the Chebyshev series for ``off + scl*x``.
See Also -------- numpy.polynomial.polynomial.polyline numpy.polynomial.legendre.legline numpy.polynomial.laguerre.lagline numpy.polynomial.hermite.hermline numpy.polynomial.hermite_e.hermeline
Examples -------- >>> import numpy.polynomial.chebyshev as C >>> C.chebline(3,2) array([3, 2]) >>> C.chebval(-3, C.chebline(3,2)) # should be -3 -3.0
""" if scl != 0: return np.array([off, scl]) else: return np.array([off])
def chebfromroots(roots): """ Generate a Chebyshev series with given roots.
The function returns the coefficients of the polynomial
.. math:: p(x) = (x - r_0) * (x - r_1) * ... * (x - r_n),
in Chebyshev form, where the `r_n` are the roots specified in `roots`. If a zero has multiplicity n, then it must appear in `roots` n times. For instance, if 2 is a root of multiplicity three and 3 is a root of multiplicity 2, then `roots` looks something like [2, 2, 2, 3, 3]. The roots can appear in any order.
If the returned coefficients are `c`, then
.. math:: p(x) = c_0 + c_1 * T_1(x) + ... + c_n * T_n(x)
The coefficient of the last term is not generally 1 for monic polynomials in Chebyshev form.
Parameters ---------- roots : array_like Sequence containing the roots.
Returns ------- out : ndarray 1-D array of coefficients. If all roots are real then `out` is a real array, if some of the roots are complex, then `out` is complex even if all the coefficients in the result are real (see Examples below).
See Also -------- numpy.polynomial.polynomial.polyfromroots numpy.polynomial.legendre.legfromroots numpy.polynomial.laguerre.lagfromroots numpy.polynomial.hermite.hermfromroots numpy.polynomial.hermite_e.hermefromroots
Examples -------- >>> import numpy.polynomial.chebyshev as C >>> C.chebfromroots((-1,0,1)) # x^3 - x relative to the standard basis array([ 0. , -0.25, 0. , 0.25]) >>> j = complex(0,1) >>> C.chebfromroots((-j,j)) # x^2 + 1 relative to the standard basis array([1.5+0.j, 0. +0.j, 0.5+0.j])
""" return pu._fromroots(chebline, chebmul, roots)
def chebadd(c1, c2): """ Add one Chebyshev series to another.
Returns the sum of two Chebyshev series `c1` + `c2`. The arguments are sequences of coefficients ordered from lowest order term to highest, i.e., [1,2,3] represents the series ``T_0 + 2*T_1 + 3*T_2``.
Parameters ---------- c1, c2 : array_like 1-D arrays of Chebyshev series coefficients ordered from low to high.
Returns ------- out : ndarray Array representing the Chebyshev series of their sum.
See Also -------- chebsub, chebmulx, chebmul, chebdiv, chebpow
Notes ----- Unlike multiplication, division, etc., the sum of two Chebyshev series is a Chebyshev series (without having to "reproject" the result onto the basis set) so addition, just like that of "standard" polynomials, is simply "component-wise."
Examples -------- >>> from numpy.polynomial import chebyshev as C >>> c1 = (1,2,3) >>> c2 = (3,2,1) >>> C.chebadd(c1,c2) array([4., 4., 4.])
""" return pu._add(c1, c2)
def chebsub(c1, c2): """ Subtract one Chebyshev series from another.
Returns the difference of two Chebyshev series `c1` - `c2`. The sequences of coefficients are from lowest order term to highest, i.e., [1,2,3] represents the series ``T_0 + 2*T_1 + 3*T_2``.
Parameters ---------- c1, c2 : array_like 1-D arrays of Chebyshev series coefficients ordered from low to high.
Returns ------- out : ndarray Of Chebyshev series coefficients representing their difference.
See Also -------- chebadd, chebmulx, chebmul, chebdiv, chebpow
Notes ----- Unlike multiplication, division, etc., the difference of two Chebyshev series is a Chebyshev series (without having to "reproject" the result onto the basis set) so subtraction, just like that of "standard" polynomials, is simply "component-wise."
Examples -------- >>> from numpy.polynomial import chebyshev as C >>> c1 = (1,2,3) >>> c2 = (3,2,1) >>> C.chebsub(c1,c2) array([-2., 0., 2.]) >>> C.chebsub(c2,c1) # -C.chebsub(c1,c2) array([ 2., 0., -2.])
""" return pu._sub(c1, c2)
def chebmulx(c): """Multiply a Chebyshev series by x.
Multiply the polynomial `c` by x, where x is the independent variable.
Parameters ---------- c : array_like 1-D array of Chebyshev series coefficients ordered from low to high.
Returns ------- out : ndarray Array representing the result of the multiplication.
Notes -----
.. versionadded:: 1.5.0
Examples -------- >>> from numpy.polynomial import chebyshev as C >>> C.chebmulx([1,2,3]) array([1. , 2.5, 1. , 1.5])
""" # c is a trimmed copy [c] = pu.as_series([c]) # The zero series needs special treatment if len(c) == 1 and c[0] == 0: return c
prd = np.empty(len(c) + 1, dtype=c.dtype) prd[0] = c[0]*0 prd[1] = c[0] if len(c) > 1: tmp = c[1:]/2 prd[2:] = tmp prd[0:-2] += tmp return prd
def chebmul(c1, c2): """ Multiply one Chebyshev series by another.
Returns the product of two Chebyshev series `c1` * `c2`. The arguments are sequences of coefficients, from lowest order "term" to highest, e.g., [1,2,3] represents the series ``T_0 + 2*T_1 + 3*T_2``.
Parameters ---------- c1, c2 : array_like 1-D arrays of Chebyshev series coefficients ordered from low to high.
Returns ------- out : ndarray Of Chebyshev series coefficients representing their product.
See Also -------- chebadd, chebsub, chebmulx, chebdiv, chebpow
Notes ----- In general, the (polynomial) product of two C-series results in terms that are not in the Chebyshev polynomial basis set. Thus, to express the product as a C-series, it is typically necessary to "reproject" the product onto said basis set, which typically produces "unintuitive live" (but correct) results; see Examples section below.
Examples -------- >>> from numpy.polynomial import chebyshev as C >>> c1 = (1,2,3) >>> c2 = (3,2,1) >>> C.chebmul(c1,c2) # multiplication requires "reprojection" array([ 6.5, 12. , 12. , 4. , 1.5])
""" # c1, c2 are trimmed copies [c1, c2] = pu.as_series([c1, c2]) z1 = _cseries_to_zseries(c1) z2 = _cseries_to_zseries(c2) prd = _zseries_mul(z1, z2) ret = _zseries_to_cseries(prd) return pu.trimseq(ret)
def chebdiv(c1, c2): """ Divide one Chebyshev series by another.
Returns the quotient-with-remainder of two Chebyshev series `c1` / `c2`. The arguments are sequences of coefficients from lowest order "term" to highest, e.g., [1,2,3] represents the series ``T_0 + 2*T_1 + 3*T_2``.
Parameters ---------- c1, c2 : array_like 1-D arrays of Chebyshev series coefficients ordered from low to high.
Returns ------- [quo, rem] : ndarrays Of Chebyshev series coefficients representing the quotient and remainder.
See Also -------- chebadd, chebsub, chebmulx, chebmul, chebpow
Notes ----- In general, the (polynomial) division of one C-series by another results in quotient and remainder terms that are not in the Chebyshev polynomial basis set. Thus, to express these results as C-series, it is typically necessary to "reproject" the results onto said basis set, which typically produces "unintuitive" (but correct) results; see Examples section below.
Examples -------- >>> from numpy.polynomial import chebyshev as C >>> c1 = (1,2,3) >>> c2 = (3,2,1) >>> C.chebdiv(c1,c2) # quotient "intuitive," remainder not (array([3.]), array([-8., -4.])) >>> c2 = (0,1,2,3) >>> C.chebdiv(c2,c1) # neither "intuitive" (array([0., 2.]), array([-2., -4.]))
""" # c1, c2 are trimmed copies [c1, c2] = pu.as_series([c1, c2]) if c2[-1] == 0: raise ZeroDivisionError()
# note: this is more efficient than `pu._div(chebmul, c1, c2)` lc1 = len(c1) lc2 = len(c2) if lc1 < lc2: return c1[:1]*0, c1 elif lc2 == 1: return c1/c2[-1], c1[:1]*0 else: z1 = _cseries_to_zseries(c1) z2 = _cseries_to_zseries(c2) quo, rem = _zseries_div(z1, z2) quo = pu.trimseq(_zseries_to_cseries(quo)) rem = pu.trimseq(_zseries_to_cseries(rem)) return quo, rem
def chebpow(c, pow, maxpower=16): """Raise a Chebyshev series to a power.
Returns the Chebyshev series `c` raised to the power `pow`. The argument `c` is a sequence of coefficients ordered from low to high. i.e., [1,2,3] is the series ``T_0 + 2*T_1 + 3*T_2.``
Parameters ---------- c : array_like 1-D array of Chebyshev series coefficients ordered from low to high. pow : integer Power to which the series will be raised maxpower : integer, optional Maximum power allowed. This is mainly to limit growth of the series to unmanageable size. Default is 16
Returns ------- coef : ndarray Chebyshev series of power.
See Also -------- chebadd, chebsub, chebmulx, chebmul, chebdiv
Examples -------- >>> from numpy.polynomial import chebyshev as C >>> C.chebpow([1, 2, 3, 4], 2) array([15.5, 22. , 16. , ..., 12.5, 12. , 8. ])
""" # note: this is more efficient than `pu._pow(chebmul, c1, c2)`, as it # avoids converting between z and c series repeatedly
# c is a trimmed copy [c] = pu.as_series([c]) power = int(pow) if power != pow or power < 0: raise ValueError("Power must be a non-negative integer.") elif maxpower is not None and power > maxpower: raise ValueError("Power is too large") elif power == 0: return np.array([1], dtype=c.dtype) elif power == 1: return c else: # This can be made more efficient by using powers of two # in the usual way. zs = _cseries_to_zseries(c) prd = zs for i in range(2, power + 1): prd = np.convolve(prd, zs) return _zseries_to_cseries(prd)
def chebder(c, m=1, scl=1, axis=0): """ Differentiate a Chebyshev series.
Returns the Chebyshev series coefficients `c` differentiated `m` times along `axis`. At each iteration the result is multiplied by `scl` (the scaling factor is for use in a linear change of variable). The argument `c` is an array of coefficients from low to high degree along each axis, e.g., [1,2,3] represents the series ``1*T_0 + 2*T_1 + 3*T_2`` while [[1,2],[1,2]] represents ``1*T_0(x)*T_0(y) + 1*T_1(x)*T_0(y) + 2*T_0(x)*T_1(y) + 2*T_1(x)*T_1(y)`` if axis=0 is ``x`` and axis=1 is ``y``.
Parameters ---------- c : array_like Array of Chebyshev series coefficients. If c is multidimensional the different axis correspond to different variables with the degree in each axis given by the corresponding index. m : int, optional Number of derivatives taken, must be non-negative. (Default: 1) scl : scalar, optional Each differentiation is multiplied by `scl`. The end result is multiplication by ``scl**m``. This is for use in a linear change of variable. (Default: 1) axis : int, optional Axis over which the derivative is taken. (Default: 0).
.. versionadded:: 1.7.0
Returns ------- der : ndarray Chebyshev series of the derivative.
See Also -------- chebint
Notes ----- In general, the result of differentiating a C-series needs to be "reprojected" onto the C-series basis set. Thus, typically, the result of this function is "unintuitive," albeit correct; see Examples section below.
Examples -------- >>> from numpy.polynomial import chebyshev as C >>> c = (1,2,3,4) >>> C.chebder(c) array([14., 12., 24.]) >>> C.chebder(c,3) array([96.]) >>> C.chebder(c,scl=-1) array([-14., -12., -24.]) >>> C.chebder(c,2,-1) array([12., 96.])
""" c = np.array(c, ndmin=1, copy=True) if c.dtype.char in '?bBhHiIlLqQpP': c = c.astype(np.double) cnt = pu._deprecate_as_int(m, "the order of derivation") iaxis = pu._deprecate_as_int(axis, "the axis") if cnt < 0: raise ValueError("The order of derivation must be non-negative") iaxis = normalize_axis_index(iaxis, c.ndim)
if cnt == 0: return c
c = np.moveaxis(c, iaxis, 0) n = len(c) if cnt >= n: c = c[:1]*0 else: for i in range(cnt): n = n - 1 c *= scl der = np.empty((n,) + c.shape[1:], dtype=c.dtype) for j in range(n, 2, -1): der[j - 1] = (2*j)*c[j] c[j - 2] += (j*c[j])/(j - 2) if n > 1: der[1] = 4*c[2] der[0] = c[1] c = der c = np.moveaxis(c, 0, iaxis) return c
def chebint(c, m=1, k=[], lbnd=0, scl=1, axis=0): """ Integrate a Chebyshev series.
Returns the Chebyshev series coefficients `c` integrated `m` times from `lbnd` along `axis`. At each iteration the resulting series is **multiplied** by `scl` and an integration constant, `k`, is added. The scaling factor is for use in a linear change of variable. ("Buyer beware": note that, depending on what one is doing, one may want `scl` to be the reciprocal of what one might expect; for more information, see the Notes section below.) The argument `c` is an array of coefficients from low to high degree along each axis, e.g., [1,2,3] represents the series ``T_0 + 2*T_1 + 3*T_2`` while [[1,2],[1,2]] represents ``1*T_0(x)*T_0(y) + 1*T_1(x)*T_0(y) + 2*T_0(x)*T_1(y) + 2*T_1(x)*T_1(y)`` if axis=0 is ``x`` and axis=1 is ``y``.
Parameters ---------- c : array_like Array of Chebyshev series coefficients. If c is multidimensional the different axis correspond to different variables with the degree in each axis given by the corresponding index. m : int, optional Order of integration, must be positive. (Default: 1) k : {[], list, scalar}, optional Integration constant(s). The value of the first integral at zero is the first value in the list, the value of the second integral at zero is the second value, etc. If ``k == []`` (the default), all constants are set to zero. If ``m == 1``, a single scalar can be given instead of a list. lbnd : scalar, optional The lower bound of the integral. (Default: 0) scl : scalar, optional Following each integration the result is *multiplied* by `scl` before the integration constant is added. (Default: 1) axis : int, optional Axis over which the integral is taken. (Default: 0).
.. versionadded:: 1.7.0
Returns ------- S : ndarray C-series coefficients of the integral.
Raises ------ ValueError If ``m < 1``, ``len(k) > m``, ``np.ndim(lbnd) != 0``, or ``np.ndim(scl) != 0``.
See Also -------- chebder
Notes ----- Note that the result of each integration is *multiplied* by `scl`. Why is this important to note? Say one is making a linear change of variable :math:`u = ax + b` in an integral relative to `x`. Then :math:`dx = du/a`, so one will need to set `scl` equal to :math:`1/a`- perhaps not what one would have first thought.
Also note that, in general, the result of integrating a C-series needs to be "reprojected" onto the C-series basis set. Thus, typically, the result of this function is "unintuitive," albeit correct; see Examples section below.
Examples -------- >>> from numpy.polynomial import chebyshev as C >>> c = (1,2,3) >>> C.chebint(c) array([ 0.5, -0.5, 0.5, 0.5]) >>> C.chebint(c,3) array([ 0.03125 , -0.1875 , 0.04166667, -0.05208333, 0.01041667, # may vary 0.00625 ]) >>> C.chebint(c, k=3) array([ 3.5, -0.5, 0.5, 0.5]) >>> C.chebint(c,lbnd=-2) array([ 8.5, -0.5, 0.5, 0.5]) >>> C.chebint(c,scl=-2) array([-1., 1., -1., -1.])
""" c = np.array(c, ndmin=1, copy=True) if c.dtype.char in '?bBhHiIlLqQpP': c = c.astype(np.double) if not np.iterable(k): k = [k] cnt = pu._deprecate_as_int(m, "the order of integration") iaxis = pu._deprecate_as_int(axis, "the axis") if cnt < 0: raise ValueError("The order of integration must be non-negative") if len(k) > cnt: raise ValueError("Too many integration constants") if np.ndim(lbnd) != 0: raise ValueError("lbnd must be a scalar.") if np.ndim(scl) != 0: raise ValueError("scl must be a scalar.") iaxis = normalize_axis_index(iaxis, c.ndim)
if cnt == 0: return c
c = np.moveaxis(c, iaxis, 0) k = list(k) + [0]*(cnt - len(k)) for i in range(cnt): n = len(c) c *= scl if n == 1 and np.all(c[0] == 0): c[0] += k[i] else: tmp = np.empty((n + 1,) + c.shape[1:], dtype=c.dtype) tmp[0] = c[0]*0 tmp[1] = c[0] if n > 1: tmp[2] = c[1]/4 for j in range(2, n): tmp[j + 1] = c[j]/(2*(j + 1)) tmp[j - 1] -= c[j]/(2*(j - 1)) tmp[0] += k[i] - chebval(lbnd, tmp) c = tmp c = np.moveaxis(c, 0, iaxis) return c
def chebval(x, c, tensor=True): """ Evaluate a Chebyshev series at points x.
If `c` is of length `n + 1`, this function returns the value:
.. math:: p(x) = c_0 * T_0(x) + c_1 * T_1(x) + ... + c_n * T_n(x)
The parameter `x` is converted to an array only if it is a tuple or a list, otherwise it is treated as a scalar. In either case, either `x` or its elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` is a 1-D array, then `p(x)` will have the same shape as `x`. If `c` is multidimensional, then the shape of the result depends on the value of `tensor`. If `tensor` is true the shape will be c.shape[1:] + x.shape. If `tensor` is false the shape will be c.shape[1:]. Note that scalars have shape (,).
Trailing zeros in the coefficients will be used in the evaluation, so they should be avoided if efficiency is a concern.
Parameters ---------- x : array_like, compatible object If `x` is a list or tuple, it is converted to an ndarray, otherwise it is left unchanged and treated as a scalar. In either case, `x` or its elements must support addition and multiplication with themselves and with the elements of `c`. c : array_like Array of coefficients ordered so that the coefficients for terms of degree n are contained in c[n]. If `c` is multidimensional the remaining indices enumerate multiple polynomials. In the two dimensional case the coefficients may be thought of as stored in the columns of `c`. tensor : boolean, optional If True, the shape of the coefficient array is extended with ones on the right, one for each dimension of `x`. Scalars have dimension 0 for this action. The result is that every column of coefficients in `c` is evaluated for every element of `x`. If False, `x` is broadcast over the columns of `c` for the evaluation. This keyword is useful when `c` is multidimensional. The default value is True.
.. versionadded:: 1.7.0
Returns ------- values : ndarray, algebra_like The shape of the return value is described above.
See Also -------- chebval2d, chebgrid2d, chebval3d, chebgrid3d
Notes ----- The evaluation uses Clenshaw recursion, aka synthetic division.
""" c = np.array(c, ndmin=1, copy=True) if c.dtype.char in '?bBhHiIlLqQpP': c = c.astype(np.double) if isinstance(x, (tuple, list)): x = np.asarray(x) if isinstance(x, np.ndarray) and tensor: c = c.reshape(c.shape + (1,)*x.ndim)
if len(c) == 1: c0 = c[0] c1 = 0 elif len(c) == 2: c0 = c[0] c1 = c[1] else: x2 = 2*x c0 = c[-2] c1 = c[-1] for i in range(3, len(c) + 1): tmp = c0 c0 = c[-i] - c1 c1 = tmp + c1*x2 return c0 + c1*x
def chebval2d(x, y, c): """ Evaluate a 2-D Chebyshev series at points (x, y).
This function returns the values:
.. math:: p(x,y) = \\sum_{i,j} c_{i,j} * T_i(x) * T_j(y)
The parameters `x` and `y` are converted to arrays only if they are tuples or a lists, otherwise they are treated as a scalars and they must have the same shape after conversion. In either case, either `x` and `y` or their elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` is a 1-D array a one is implicitly appended to its shape to make it 2-D. The shape of the result will be c.shape[2:] + x.shape.
Parameters ---------- x, y : array_like, compatible objects The two dimensional series is evaluated at the points `(x, y)`, where `x` and `y` must have the same shape. If `x` or `y` is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and if it isn't an ndarray it is treated as a scalar. c : array_like Array of coefficients ordered so that the coefficient of the term of multi-degree i,j is contained in ``c[i,j]``. If `c` has dimension greater than 2 the remaining indices enumerate multiple sets of coefficients.
Returns ------- values : ndarray, compatible object The values of the two dimensional Chebyshev series at points formed from pairs of corresponding values from `x` and `y`.
See Also -------- chebval, chebgrid2d, chebval3d, chebgrid3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._valnd(chebval, c, x, y)
def chebgrid2d(x, y, c): """ Evaluate a 2-D Chebyshev series on the Cartesian product of x and y.
This function returns the values:
.. math:: p(a,b) = \\sum_{i,j} c_{i,j} * T_i(a) * T_j(b),
where the points `(a, b)` consist of all pairs formed by taking `a` from `x` and `b` from `y`. The resulting points form a grid with `x` in the first dimension and `y` in the second.
The parameters `x` and `y` are converted to arrays only if they are tuples or a lists, otherwise they are treated as a scalars. In either case, either `x` and `y` or their elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` has fewer than two dimensions, ones are implicitly appended to its shape to make it 2-D. The shape of the result will be c.shape[2:] + x.shape + y.shape.
Parameters ---------- x, y : array_like, compatible objects The two dimensional series is evaluated at the points in the Cartesian product of `x` and `y`. If `x` or `y` is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and, if it isn't an ndarray, it is treated as a scalar. c : array_like Array of coefficients ordered so that the coefficient of the term of multi-degree i,j is contained in `c[i,j]`. If `c` has dimension greater than two the remaining indices enumerate multiple sets of coefficients.
Returns ------- values : ndarray, compatible object The values of the two dimensional Chebyshev series at points in the Cartesian product of `x` and `y`.
See Also -------- chebval, chebval2d, chebval3d, chebgrid3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._gridnd(chebval, c, x, y)
def chebval3d(x, y, z, c): """ Evaluate a 3-D Chebyshev series at points (x, y, z).
This function returns the values:
.. math:: p(x,y,z) = \\sum_{i,j,k} c_{i,j,k} * T_i(x) * T_j(y) * T_k(z)
The parameters `x`, `y`, and `z` are converted to arrays only if they are tuples or a lists, otherwise they are treated as a scalars and they must have the same shape after conversion. In either case, either `x`, `y`, and `z` or their elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` has fewer than 3 dimensions, ones are implicitly appended to its shape to make it 3-D. The shape of the result will be c.shape[3:] + x.shape.
Parameters ---------- x, y, z : array_like, compatible object The three dimensional series is evaluated at the points `(x, y, z)`, where `x`, `y`, and `z` must have the same shape. If any of `x`, `y`, or `z` is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and if it isn't an ndarray it is treated as a scalar. c : array_like Array of coefficients ordered so that the coefficient of the term of multi-degree i,j,k is contained in ``c[i,j,k]``. If `c` has dimension greater than 3 the remaining indices enumerate multiple sets of coefficients.
Returns ------- values : ndarray, compatible object The values of the multidimensional polynomial on points formed with triples of corresponding values from `x`, `y`, and `z`.
See Also -------- chebval, chebval2d, chebgrid2d, chebgrid3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._valnd(chebval, c, x, y, z)
def chebgrid3d(x, y, z, c): """ Evaluate a 3-D Chebyshev series on the Cartesian product of x, y, and z.
This function returns the values:
.. math:: p(a,b,c) = \\sum_{i,j,k} c_{i,j,k} * T_i(a) * T_j(b) * T_k(c)
where the points `(a, b, c)` consist of all triples formed by taking `a` from `x`, `b` from `y`, and `c` from `z`. The resulting points form a grid with `x` in the first dimension, `y` in the second, and `z` in the third.
The parameters `x`, `y`, and `z` are converted to arrays only if they are tuples or a lists, otherwise they are treated as a scalars. In either case, either `x`, `y`, and `z` or their elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` has fewer than three dimensions, ones are implicitly appended to its shape to make it 3-D. The shape of the result will be c.shape[3:] + x.shape + y.shape + z.shape.
Parameters ---------- x, y, z : array_like, compatible objects The three dimensional series is evaluated at the points in the Cartesian product of `x`, `y`, and `z`. If `x`,`y`, or `z` is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and, if it isn't an ndarray, it is treated as a scalar. c : array_like Array of coefficients ordered so that the coefficients for terms of degree i,j are contained in ``c[i,j]``. If `c` has dimension greater than two the remaining indices enumerate multiple sets of coefficients.
Returns ------- values : ndarray, compatible object The values of the two dimensional polynomial at points in the Cartesian product of `x` and `y`.
See Also -------- chebval, chebval2d, chebgrid2d, chebval3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._gridnd(chebval, c, x, y, z)
def chebvander(x, deg): """Pseudo-Vandermonde matrix of given degree.
Returns the pseudo-Vandermonde matrix of degree `deg` and sample points `x`. The pseudo-Vandermonde matrix is defined by
.. math:: V[..., i] = T_i(x),
where `0 <= i <= deg`. The leading indices of `V` index the elements of `x` and the last index is the degree of the Chebyshev polynomial.
If `c` is a 1-D array of coefficients of length `n + 1` and `V` is the matrix ``V = chebvander(x, n)``, then ``np.dot(V, c)`` and ``chebval(x, c)`` are the same up to roundoff. This equivalence is useful both for least squares fitting and for the evaluation of a large number of Chebyshev series of the same degree and sample points.
Parameters ---------- x : array_like Array of points. The dtype is converted to float64 or complex128 depending on whether any of the elements are complex. If `x` is scalar it is converted to a 1-D array. deg : int Degree of the resulting matrix.
Returns ------- vander : ndarray The pseudo Vandermonde matrix. The shape of the returned matrix is ``x.shape + (deg + 1,)``, where The last index is the degree of the corresponding Chebyshev polynomial. The dtype will be the same as the converted `x`.
""" ideg = pu._deprecate_as_int(deg, "deg") if ideg < 0: raise ValueError("deg must be non-negative")
x = np.array(x, copy=False, ndmin=1) + 0.0 dims = (ideg + 1,) + x.shape dtyp = x.dtype v = np.empty(dims, dtype=dtyp) # Use forward recursion to generate the entries. v[0] = x*0 + 1 if ideg > 0: x2 = 2*x v[1] = x for i in range(2, ideg + 1): v[i] = v[i-1]*x2 - v[i-2] return np.moveaxis(v, 0, -1)
def chebvander2d(x, y, deg): """Pseudo-Vandermonde matrix of given degrees.
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample points `(x, y)`. The pseudo-Vandermonde matrix is defined by
.. math:: V[..., (deg[1] + 1)*i + j] = T_i(x) * T_j(y),
where `0 <= i <= deg[0]` and `0 <= j <= deg[1]`. The leading indices of `V` index the points `(x, y)` and the last index encodes the degrees of the Chebyshev polynomials.
If ``V = chebvander2d(x, y, [xdeg, ydeg])``, then the columns of `V` correspond to the elements of a 2-D coefficient array `c` of shape (xdeg + 1, ydeg + 1) in the order
.. math:: c_{00}, c_{01}, c_{02} ... , c_{10}, c_{11}, c_{12} ...
and ``np.dot(V, c.flat)`` and ``chebval2d(x, y, c)`` will be the same up to roundoff. This equivalence is useful both for least squares fitting and for the evaluation of a large number of 2-D Chebyshev series of the same degrees and sample points.
Parameters ---------- x, y : array_like Arrays of point coordinates, all of the same shape. The dtypes will be converted to either float64 or complex128 depending on whether any of the elements are complex. Scalars are converted to 1-D arrays. deg : list of ints List of maximum degrees of the form [x_deg, y_deg].
Returns ------- vander2d : ndarray The shape of the returned matrix is ``x.shape + (order,)``, where :math:`order = (deg[0]+1)*(deg[1]+1)`. The dtype will be the same as the converted `x` and `y`.
See Also -------- chebvander, chebvander3d, chebval2d, chebval3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._vander_nd_flat((chebvander, chebvander), (x, y), deg)
def chebvander3d(x, y, z, deg): """Pseudo-Vandermonde matrix of given degrees.
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample points `(x, y, z)`. If `l, m, n` are the given degrees in `x, y, z`, then The pseudo-Vandermonde matrix is defined by
.. math:: V[..., (m+1)(n+1)i + (n+1)j + k] = T_i(x)*T_j(y)*T_k(z),
where `0 <= i <= l`, `0 <= j <= m`, and `0 <= j <= n`. The leading indices of `V` index the points `(x, y, z)` and the last index encodes the degrees of the Chebyshev polynomials.
If ``V = chebvander3d(x, y, z, [xdeg, ydeg, zdeg])``, then the columns of `V` correspond to the elements of a 3-D coefficient array `c` of shape (xdeg + 1, ydeg + 1, zdeg + 1) in the order
.. math:: c_{000}, c_{001}, c_{002},... , c_{010}, c_{011}, c_{012},...
and ``np.dot(V, c.flat)`` and ``chebval3d(x, y, z, c)`` will be the same up to roundoff. This equivalence is useful both for least squares fitting and for the evaluation of a large number of 3-D Chebyshev series of the same degrees and sample points.
Parameters ---------- x, y, z : array_like Arrays of point coordinates, all of the same shape. The dtypes will be converted to either float64 or complex128 depending on whether any of the elements are complex. Scalars are converted to 1-D arrays. deg : list of ints List of maximum degrees of the form [x_deg, y_deg, z_deg].
Returns ------- vander3d : ndarray The shape of the returned matrix is ``x.shape + (order,)``, where :math:`order = (deg[0]+1)*(deg[1]+1)*(deg[2]+1)`. The dtype will be the same as the converted `x`, `y`, and `z`.
See Also -------- chebvander, chebvander3d, chebval2d, chebval3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._vander_nd_flat((chebvander, chebvander, chebvander), (x, y, z), deg)
def chebfit(x, y, deg, rcond=None, full=False, w=None): """ Least squares fit of Chebyshev series to data.
Return the coefficients of a Chebyshev series of degree `deg` that is the least squares fit to the data values `y` given at points `x`. If `y` is 1-D the returned coefficients will also be 1-D. If `y` is 2-D multiple fits are done, one for each column of `y`, and the resulting coefficients are stored in the corresponding columns of a 2-D return. The fitted polynomial(s) are in the form
.. math:: p(x) = c_0 + c_1 * T_1(x) + ... + c_n * T_n(x),
where `n` is `deg`.
Parameters ---------- x : array_like, shape (M,) x-coordinates of the M sample points ``(x[i], y[i])``. y : array_like, shape (M,) or (M, K) y-coordinates of the sample points. Several data sets of sample points sharing the same x-coordinates can be fitted at once by passing in a 2D-array that contains one dataset per column. deg : int or 1-D array_like Degree(s) of the fitting polynomials. If `deg` is a single integer, all terms up to and including the `deg`'th term are included in the fit. For NumPy versions >= 1.11.0 a list of integers specifying the degrees of the terms to include may be used instead. rcond : float, optional Relative condition number of the fit. Singular values smaller than this relative to the largest singular value will be ignored. The default value is len(x)*eps, where eps is the relative precision of the float type, about 2e-16 in most cases. full : bool, optional Switch determining nature of return value. When it is False (the default) just the coefficients are returned, when True diagnostic information from the singular value decomposition is also returned. w : array_like, shape (`M`,), optional Weights. If not None, the weight ``w[i]`` applies to the unsquared residual ``y[i] - y_hat[i]`` at ``x[i]``. Ideally the weights are chosen so that the errors of the products ``w[i]*y[i]`` all have the same variance. When using inverse-variance weighting, use ``w[i] = 1/sigma(y[i])``. The default value is None.
.. versionadded:: 1.5.0
Returns ------- coef : ndarray, shape (M,) or (M, K) Chebyshev coefficients ordered from low to high. If `y` was 2-D, the coefficients for the data in column k of `y` are in column `k`.
[residuals, rank, singular_values, rcond] : list These values are only returned if ``full == True``
- residuals -- sum of squared residuals of the least squares fit - rank -- the numerical rank of the scaled Vandermonde matrix - singular_values -- singular values of the scaled Vandermonde matrix - rcond -- value of `rcond`.
For more details, see `numpy.linalg.lstsq`.
Warns ----- RankWarning The rank of the coefficient matrix in the least-squares fit is deficient. The warning is only raised if ``full == False``. The warnings can be turned off by
>>> import warnings >>> warnings.simplefilter('ignore', np.RankWarning)
See Also -------- numpy.polynomial.polynomial.polyfit numpy.polynomial.legendre.legfit numpy.polynomial.laguerre.lagfit numpy.polynomial.hermite.hermfit numpy.polynomial.hermite_e.hermefit chebval : Evaluates a Chebyshev series. chebvander : Vandermonde matrix of Chebyshev series. chebweight : Chebyshev weight function. numpy.linalg.lstsq : Computes a least-squares fit from the matrix. scipy.interpolate.UnivariateSpline : Computes spline fits.
Notes ----- The solution is the coefficients of the Chebyshev series `p` that minimizes the sum of the weighted squared errors
.. math:: E = \\sum_j w_j^2 * |y_j - p(x_j)|^2,
where :math:`w_j` are the weights. This problem is solved by setting up as the (typically) overdetermined matrix equation
.. math:: V(x) * c = w * y,
where `V` is the weighted pseudo Vandermonde matrix of `x`, `c` are the coefficients to be solved for, `w` are the weights, and `y` are the observed values. This equation is then solved using the singular value decomposition of `V`.
If some of the singular values of `V` are so small that they are neglected, then a `RankWarning` will be issued. This means that the coefficient values may be poorly determined. Using a lower order fit will usually get rid of the warning. The `rcond` parameter can also be set to a value smaller than its default, but the resulting fit may be spurious and have large contributions from roundoff error.
Fits using Chebyshev series are usually better conditioned than fits using power series, but much can depend on the distribution of the sample points and the smoothness of the data. If the quality of the fit is inadequate splines may be a good alternative.
References ---------- .. [1] Wikipedia, "Curve fitting", https://en.wikipedia.org/wiki/Curve_fitting
Examples --------
""" return pu._fit(chebvander, x, y, deg, rcond, full, w)
def chebcompanion(c): """Return the scaled companion matrix of c.
The basis polynomials are scaled so that the companion matrix is symmetric when `c` is a Chebyshev basis polynomial. This provides better eigenvalue estimates than the unscaled case and for basis polynomials the eigenvalues are guaranteed to be real if `numpy.linalg.eigvalsh` is used to obtain them.
Parameters ---------- c : array_like 1-D array of Chebyshev series coefficients ordered from low to high degree.
Returns ------- mat : ndarray Scaled companion matrix of dimensions (deg, deg).
Notes -----
.. versionadded:: 1.7.0
""" # c is a trimmed copy [c] = pu.as_series([c]) if len(c) < 2: raise ValueError('Series must have maximum degree of at least 1.') if len(c) == 2: return np.array([[-c[0]/c[1]]])
n = len(c) - 1 mat = np.zeros((n, n), dtype=c.dtype) scl = np.array([1.] + [np.sqrt(.5)]*(n-1)) top = mat.reshape(-1)[1::n+1] bot = mat.reshape(-1)[n::n+1] top[0] = np.sqrt(.5) top[1:] = 1/2 bot[...] = top mat[:, -1] -= (c[:-1]/c[-1])*(scl/scl[-1])*.5 return mat
def chebroots(c): """ Compute the roots of a Chebyshev series.
Return the roots (a.k.a. "zeros") of the polynomial
.. math:: p(x) = \\sum_i c[i] * T_i(x).
Parameters ---------- c : 1-D array_like 1-D array of coefficients.
Returns ------- out : ndarray Array of the roots of the series. If all the roots are real, then `out` is also real, otherwise it is complex.
See Also -------- numpy.polynomial.polynomial.polyroots numpy.polynomial.legendre.legroots numpy.polynomial.laguerre.lagroots numpy.polynomial.hermite.hermroots numpy.polynomial.hermite_e.hermeroots
Notes ----- The root estimates are obtained as the eigenvalues of the companion matrix, Roots far from the origin of the complex plane may have large errors due to the numerical instability of the series for such values. Roots with multiplicity greater than 1 will also show larger errors as the value of the series near such points is relatively insensitive to errors in the roots. Isolated roots near the origin can be improved by a few iterations of Newton's method.
The Chebyshev series basis polynomials aren't powers of `x` so the results of this function may seem unintuitive.
Examples -------- >>> import numpy.polynomial.chebyshev as cheb >>> cheb.chebroots((-1, 1,-1, 1)) # T3 - T2 + T1 - T0 has real roots array([ -5.00000000e-01, 2.60860684e-17, 1.00000000e+00]) # may vary
""" # c is a trimmed copy [c] = pu.as_series([c]) if len(c) < 2: return np.array([], dtype=c.dtype) if len(c) == 2: return np.array([-c[0]/c[1]])
# rotated companion matrix reduces error m = chebcompanion(c)[::-1,::-1] r = la.eigvals(m) r.sort() return r
def chebinterpolate(func, deg, args=()): """Interpolate a function at the Chebyshev points of the first kind.
Returns the Chebyshev series that interpolates `func` at the Chebyshev points of the first kind in the interval [-1, 1]. The interpolating series tends to a minmax approximation to `func` with increasing `deg` if the function is continuous in the interval.
.. versionadded:: 1.14.0
Parameters ---------- func : function The function to be approximated. It must be a function of a single variable of the form ``f(x, a, b, c...)``, where ``a, b, c...`` are extra arguments passed in the `args` parameter. deg : int Degree of the interpolating polynomial args : tuple, optional Extra arguments to be used in the function call. Default is no extra arguments.
Returns ------- coef : ndarray, shape (deg + 1,) Chebyshev coefficients of the interpolating series ordered from low to high.
Examples -------- >>> import numpy.polynomial.chebyshev as C >>> C.chebfromfunction(lambda x: np.tanh(x) + 0.5, 8) array([ 5.00000000e-01, 8.11675684e-01, -9.86864911e-17, -5.42457905e-02, -2.71387850e-16, 4.51658839e-03, 2.46716228e-17, -3.79694221e-04, -3.26899002e-16])
Notes -----
The Chebyshev polynomials used in the interpolation are orthogonal when sampled at the Chebyshev points of the first kind. If it is desired to constrain some of the coefficients they can simply be set to the desired value after the interpolation, no new interpolation or fit is needed. This is especially useful if it is known apriori that some of coefficients are zero. For instance, if the function is even then the coefficients of the terms of odd degree in the result can be set to zero.
""" deg = np.asarray(deg)
# check arguments. if deg.ndim > 0 or deg.dtype.kind not in 'iu' or deg.size == 0: raise TypeError("deg must be an int") if deg < 0: raise ValueError("expected deg >= 0")
order = deg + 1 xcheb = chebpts1(order) yfunc = func(xcheb, *args) m = chebvander(xcheb, deg) c = np.dot(m.T, yfunc) c[0] /= order c[1:] /= 0.5*order
return c
def chebgauss(deg): """ Gauss-Chebyshev quadrature.
Computes the sample points and weights for Gauss-Chebyshev quadrature. These sample points and weights will correctly integrate polynomials of degree :math:`2*deg - 1` or less over the interval :math:`[-1, 1]` with the weight function :math:`f(x) = 1/\\sqrt{1 - x^2}`.
Parameters ---------- deg : int Number of sample points and weights. It must be >= 1.
Returns ------- x : ndarray 1-D ndarray containing the sample points. y : ndarray 1-D ndarray containing the weights.
Notes -----
.. versionadded:: 1.7.0
The results have only been tested up to degree 100, higher degrees may be problematic. For Gauss-Chebyshev there are closed form solutions for the sample points and weights. If n = `deg`, then
.. math:: x_i = \\cos(\\pi (2 i - 1) / (2 n))
.. math:: w_i = \\pi / n
""" ideg = pu._deprecate_as_int(deg, "deg") if ideg <= 0: raise ValueError("deg must be a positive integer")
x = np.cos(np.pi * np.arange(1, 2*ideg, 2) / (2.0*ideg)) w = np.ones(ideg)*(np.pi/ideg)
return x, w
def chebweight(x): """ The weight function of the Chebyshev polynomials.
The weight function is :math:`1/\\sqrt{1 - x^2}` and the interval of integration is :math:`[-1, 1]`. The Chebyshev polynomials are orthogonal, but not normalized, with respect to this weight function.
Parameters ---------- x : array_like Values at which the weight function will be computed.
Returns ------- w : ndarray The weight function at `x`.
Notes -----
.. versionadded:: 1.7.0
""" w = 1./(np.sqrt(1. + x) * np.sqrt(1. - x)) return w
def chebpts1(npts): """ Chebyshev points of the first kind.
The Chebyshev points of the first kind are the points ``cos(x)``, where ``x = [pi*(k + .5)/npts for k in range(npts)]``.
Parameters ---------- npts : int Number of sample points desired.
Returns ------- pts : ndarray The Chebyshev points of the first kind.
See Also -------- chebpts2
Notes -----
.. versionadded:: 1.5.0
""" _npts = int(npts) if _npts != npts: raise ValueError("npts must be integer") if _npts < 1: raise ValueError("npts must be >= 1")
x = 0.5 * np.pi / _npts * np.arange(-_npts+1, _npts+1, 2) return np.sin(x)
def chebpts2(npts): """ Chebyshev points of the second kind.
The Chebyshev points of the second kind are the points ``cos(x)``, where ``x = [pi*k/(npts - 1) for k in range(npts)]`` sorted in ascending order.
Parameters ---------- npts : int Number of sample points desired.
Returns ------- pts : ndarray The Chebyshev points of the second kind.
Notes -----
.. versionadded:: 1.5.0
""" _npts = int(npts) if _npts != npts: raise ValueError("npts must be integer") if _npts < 2: raise ValueError("npts must be >= 2")
x = np.linspace(-np.pi, 0, _npts) return np.cos(x)
## Chebyshev series class#
class Chebyshev(ABCPolyBase): """A Chebyshev series class.
The Chebyshev class provides the standard Python numerical methods '+', '-', '*', '//', '%', 'divmod', '**', and '()' as well as the methods listed below.
Parameters ---------- coef : array_like Chebyshev coefficients in order of increasing degree, i.e., ``(1, 2, 3)`` gives ``1*T_0(x) + 2*T_1(x) + 3*T_2(x)``. domain : (2,) array_like, optional Domain to use. The interval ``[domain[0], domain[1]]`` is mapped to the interval ``[window[0], window[1]]`` by shifting and scaling. The default value is [-1, 1]. window : (2,) array_like, optional Window, see `domain` for its use. The default value is [-1, 1].
.. versionadded:: 1.6.0 symbol : str, optional Symbol used to represent the independent variable in string representations of the polynomial expression, e.g. for printing. The symbol must be a valid Python identifier. Default value is 'x'.
.. versionadded:: 1.24
""" # Virtual Functions _add = staticmethod(chebadd) _sub = staticmethod(chebsub) _mul = staticmethod(chebmul) _div = staticmethod(chebdiv) _pow = staticmethod(chebpow) _val = staticmethod(chebval) _int = staticmethod(chebint) _der = staticmethod(chebder) _fit = staticmethod(chebfit) _line = staticmethod(chebline) _roots = staticmethod(chebroots) _fromroots = staticmethod(chebfromroots)
@classmethod def interpolate(cls, func, deg, domain=None, args=()): """Interpolate a function at the Chebyshev points of the first kind.
Returns the series that interpolates `func` at the Chebyshev points of the first kind scaled and shifted to the `domain`. The resulting series tends to a minmax approximation of `func` when the function is continuous in the domain.
.. versionadded:: 1.14.0
Parameters ---------- func : function The function to be interpolated. It must be a function of a single variable of the form ``f(x, a, b, c...)``, where ``a, b, c...`` are extra arguments passed in the `args` parameter. deg : int Degree of the interpolating polynomial. domain : {None, [beg, end]}, optional Domain over which `func` is interpolated. The default is None, in which case the domain is [-1, 1]. args : tuple, optional Extra arguments to be used in the function call. Default is no extra arguments.
Returns ------- polynomial : Chebyshev instance Interpolating Chebyshev instance.
Notes ----- See `numpy.polynomial.chebfromfunction` for more details.
""" if domain is None: domain = cls.domain xfunc = lambda x: func(pu.mapdomain(x, cls.window, domain), *args) coef = chebinterpolate(xfunc, deg) return cls(coef, domain=domain)
# Virtual properties domain = np.array(chebdomain) window = np.array(chebdomain) basis_name = 'T'123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051from typing import Any
from numpy import ndarray, dtype, int_from numpy.polynomial._polybase import ABCPolyBasefrom numpy.polynomial.polyutils import trimcoef
__all__: list[str]
chebtrim = trimcoef
def poly2cheb(pol): ...def cheb2poly(c): ...
chebdomain: ndarray[Any, dtype[int_]]chebzero: ndarray[Any, dtype[int_]]chebone: ndarray[Any, dtype[int_]]chebx: ndarray[Any, dtype[int_]]
def chebline(off, scl): ...def chebfromroots(roots): ...def chebadd(c1, c2): ...def chebsub(c1, c2): ...def chebmulx(c): ...def chebmul(c1, c2): ...def chebdiv(c1, c2): ...def chebpow(c, pow, maxpower=...): ...def chebder(c, m=..., scl=..., axis=...): ...def chebint(c, m=..., k = ..., lbnd=..., scl=..., axis=...): ...def chebval(x, c, tensor=...): ...def chebval2d(x, y, c): ...def chebgrid2d(x, y, c): ...def chebval3d(x, y, z, c): ...def chebgrid3d(x, y, z, c): ...def chebvander(x, deg): ...def chebvander2d(x, y, deg): ...def chebvander3d(x, y, z, deg): ...def chebfit(x, y, deg, rcond=..., full=..., w=...): ...def chebcompanion(c): ...def chebroots(c): ...def chebinterpolate(func, deg, args = ...): ...def chebgauss(deg): ...def chebweight(x): ...def chebpts1(npts): ...def chebpts2(npts): ...
class Chebyshev(ABCPolyBase): @classmethod def interpolate(cls, func, deg, domain=..., args = ...): ... domain: Any window: Any basis_name: Any12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703"""==============================================================Hermite Series, "Physicists" (:mod:`numpy.polynomial.hermite`)==============================================================
This module provides a number of objects (mostly functions) useful fordealing with Hermite series, including a `Hermite` class thatencapsulates the usual arithmetic operations. (General informationon how this module represents and works with such polynomials is in thedocstring for its "parent" sub-package, `numpy.polynomial`).
Classes-------.. autosummary:: :toctree: generated/
Hermite
Constants---------.. autosummary:: :toctree: generated/
hermdomain hermzero hermone hermx
Arithmetic----------.. autosummary:: :toctree: generated/
hermadd hermsub hermmulx hermmul hermdiv hermpow hermval hermval2d hermval3d hermgrid2d hermgrid3d
Calculus--------.. autosummary:: :toctree: generated/
hermder hermint
Misc Functions--------------.. autosummary:: :toctree: generated/
hermfromroots hermroots hermvander hermvander2d hermvander3d hermgauss hermweight hermcompanion hermfit hermtrim hermline herm2poly poly2herm
See also--------`numpy.polynomial`
"""import numpy as npimport numpy.linalg as lafrom numpy.core.multiarray import normalize_axis_index
from . import polyutils as pufrom ._polybase import ABCPolyBase
__all__ = [ 'hermzero', 'hermone', 'hermx', 'hermdomain', 'hermline', 'hermadd', 'hermsub', 'hermmulx', 'hermmul', 'hermdiv', 'hermpow', 'hermval', 'hermder', 'hermint', 'herm2poly', 'poly2herm', 'hermfromroots', 'hermvander', 'hermfit', 'hermtrim', 'hermroots', 'Hermite', 'hermval2d', 'hermval3d', 'hermgrid2d', 'hermgrid3d', 'hermvander2d', 'hermvander3d', 'hermcompanion', 'hermgauss', 'hermweight']
hermtrim = pu.trimcoef
def poly2herm(pol): """ poly2herm(pol)
Convert a polynomial to a Hermite series.
Convert an array representing the coefficients of a polynomial (relative to the "standard" basis) ordered from lowest degree to highest, to an array of the coefficients of the equivalent Hermite series, ordered from lowest to highest degree.
Parameters ---------- pol : array_like 1-D array containing the polynomial coefficients
Returns ------- c : ndarray 1-D array containing the coefficients of the equivalent Hermite series.
See Also -------- herm2poly
Notes ----- The easy way to do conversions between polynomial basis sets is to use the convert method of a class instance.
Examples -------- >>> from numpy.polynomial.hermite import poly2herm >>> poly2herm(np.arange(4)) array([1. , 2.75 , 0.5 , 0.375])
""" [pol] = pu.as_series([pol]) deg = len(pol) - 1 res = 0 for i in range(deg, -1, -1): res = hermadd(hermmulx(res), pol[i]) return res
def herm2poly(c): """ Convert a Hermite series to a polynomial.
Convert an array representing the coefficients of a Hermite series, ordered from lowest degree to highest, to an array of the coefficients of the equivalent polynomial (relative to the "standard" basis) ordered from lowest to highest degree.
Parameters ---------- c : array_like 1-D array containing the Hermite series coefficients, ordered from lowest order term to highest.
Returns ------- pol : ndarray 1-D array containing the coefficients of the equivalent polynomial (relative to the "standard" basis) ordered from lowest order term to highest.
See Also -------- poly2herm
Notes ----- The easy way to do conversions between polynomial basis sets is to use the convert method of a class instance.
Examples -------- >>> from numpy.polynomial.hermite import herm2poly >>> herm2poly([ 1. , 2.75 , 0.5 , 0.375]) array([0., 1., 2., 3.])
""" from .polynomial import polyadd, polysub, polymulx
[c] = pu.as_series([c]) n = len(c) if n == 1: return c if n == 2: c[1] *= 2 return c else: c0 = c[-2] c1 = c[-1] # i is the current degree of c1 for i in range(n - 1, 1, -1): tmp = c0 c0 = polysub(c[i - 2], c1*(2*(i - 1))) c1 = polyadd(tmp, polymulx(c1)*2) return polyadd(c0, polymulx(c1)*2)
## These are constant arrays are of integer type so as to be compatible# with the widest range of other types, such as Decimal.#
# Hermitehermdomain = np.array([-1, 1])
# Hermite coefficients representing zero.hermzero = np.array([0])
# Hermite coefficients representing one.hermone = np.array([1])
# Hermite coefficients representing the identity x.hermx = np.array([0, 1/2])
def hermline(off, scl): """ Hermite series whose graph is a straight line.
Parameters ---------- off, scl : scalars The specified line is given by ``off + scl*x``.
Returns ------- y : ndarray This module's representation of the Hermite series for ``off + scl*x``.
See Also -------- numpy.polynomial.polynomial.polyline numpy.polynomial.chebyshev.chebline numpy.polynomial.legendre.legline numpy.polynomial.laguerre.lagline numpy.polynomial.hermite_e.hermeline
Examples -------- >>> from numpy.polynomial.hermite import hermline, hermval >>> hermval(0,hermline(3, 2)) 3.0 >>> hermval(1,hermline(3, 2)) 5.0
""" if scl != 0: return np.array([off, scl/2]) else: return np.array([off])
def hermfromroots(roots): """ Generate a Hermite series with given roots.
The function returns the coefficients of the polynomial
.. math:: p(x) = (x - r_0) * (x - r_1) * ... * (x - r_n),
in Hermite form, where the `r_n` are the roots specified in `roots`. If a zero has multiplicity n, then it must appear in `roots` n times. For instance, if 2 is a root of multiplicity three and 3 is a root of multiplicity 2, then `roots` looks something like [2, 2, 2, 3, 3]. The roots can appear in any order.
If the returned coefficients are `c`, then
.. math:: p(x) = c_0 + c_1 * H_1(x) + ... + c_n * H_n(x)
The coefficient of the last term is not generally 1 for monic polynomials in Hermite form.
Parameters ---------- roots : array_like Sequence containing the roots.
Returns ------- out : ndarray 1-D array of coefficients. If all roots are real then `out` is a real array, if some of the roots are complex, then `out` is complex even if all the coefficients in the result are real (see Examples below).
See Also -------- numpy.polynomial.polynomial.polyfromroots numpy.polynomial.legendre.legfromroots numpy.polynomial.laguerre.lagfromroots numpy.polynomial.chebyshev.chebfromroots numpy.polynomial.hermite_e.hermefromroots
Examples -------- >>> from numpy.polynomial.hermite import hermfromroots, hermval >>> coef = hermfromroots((-1, 0, 1)) >>> hermval((-1, 0, 1), coef) array([0., 0., 0.]) >>> coef = hermfromroots((-1j, 1j)) >>> hermval((-1j, 1j), coef) array([0.+0.j, 0.+0.j])
""" return pu._fromroots(hermline, hermmul, roots)
def hermadd(c1, c2): """ Add one Hermite series to another.
Returns the sum of two Hermite series `c1` + `c2`. The arguments are sequences of coefficients ordered from lowest order term to highest, i.e., [1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
Parameters ---------- c1, c2 : array_like 1-D arrays of Hermite series coefficients ordered from low to high.
Returns ------- out : ndarray Array representing the Hermite series of their sum.
See Also -------- hermsub, hermmulx, hermmul, hermdiv, hermpow
Notes ----- Unlike multiplication, division, etc., the sum of two Hermite series is a Hermite series (without having to "reproject" the result onto the basis set) so addition, just like that of "standard" polynomials, is simply "component-wise."
Examples -------- >>> from numpy.polynomial.hermite import hermadd >>> hermadd([1, 2, 3], [1, 2, 3, 4]) array([2., 4., 6., 4.])
""" return pu._add(c1, c2)
def hermsub(c1, c2): """ Subtract one Hermite series from another.
Returns the difference of two Hermite series `c1` - `c2`. The sequences of coefficients are from lowest order term to highest, i.e., [1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
Parameters ---------- c1, c2 : array_like 1-D arrays of Hermite series coefficients ordered from low to high.
Returns ------- out : ndarray Of Hermite series coefficients representing their difference.
See Also -------- hermadd, hermmulx, hermmul, hermdiv, hermpow
Notes ----- Unlike multiplication, division, etc., the difference of two Hermite series is a Hermite series (without having to "reproject" the result onto the basis set) so subtraction, just like that of "standard" polynomials, is simply "component-wise."
Examples -------- >>> from numpy.polynomial.hermite import hermsub >>> hermsub([1, 2, 3, 4], [1, 2, 3]) array([0., 0., 0., 4.])
""" return pu._sub(c1, c2)
def hermmulx(c): """Multiply a Hermite series by x.
Multiply the Hermite series `c` by x, where x is the independent variable.
Parameters ---------- c : array_like 1-D array of Hermite series coefficients ordered from low to high.
Returns ------- out : ndarray Array representing the result of the multiplication.
See Also -------- hermadd, hermsub, hermmul, hermdiv, hermpow
Notes ----- The multiplication uses the recursion relationship for Hermite polynomials in the form
.. math::
xP_i(x) = (P_{i + 1}(x)/2 + i*P_{i - 1}(x))
Examples -------- >>> from numpy.polynomial.hermite import hermmulx >>> hermmulx([1, 2, 3]) array([2. , 6.5, 1. , 1.5])
""" # c is a trimmed copy [c] = pu.as_series([c]) # The zero series needs special treatment if len(c) == 1 and c[0] == 0: return c
prd = np.empty(len(c) + 1, dtype=c.dtype) prd[0] = c[0]*0 prd[1] = c[0]/2 for i in range(1, len(c)): prd[i + 1] = c[i]/2 prd[i - 1] += c[i]*i return prd
def hermmul(c1, c2): """ Multiply one Hermite series by another.
Returns the product of two Hermite series `c1` * `c2`. The arguments are sequences of coefficients, from lowest order "term" to highest, e.g., [1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
Parameters ---------- c1, c2 : array_like 1-D arrays of Hermite series coefficients ordered from low to high.
Returns ------- out : ndarray Of Hermite series coefficients representing their product.
See Also -------- hermadd, hermsub, hermmulx, hermdiv, hermpow
Notes ----- In general, the (polynomial) product of two C-series results in terms that are not in the Hermite polynomial basis set. Thus, to express the product as a Hermite series, it is necessary to "reproject" the product onto said basis set, which may produce "unintuitive" (but correct) results; see Examples section below.
Examples -------- >>> from numpy.polynomial.hermite import hermmul >>> hermmul([1, 2, 3], [0, 1, 2]) array([52., 29., 52., 7., 6.])
""" # s1, s2 are trimmed copies [c1, c2] = pu.as_series([c1, c2])
if len(c1) > len(c2): c = c2 xs = c1 else: c = c1 xs = c2
if len(c) == 1: c0 = c[0]*xs c1 = 0 elif len(c) == 2: c0 = c[0]*xs c1 = c[1]*xs else: nd = len(c) c0 = c[-2]*xs c1 = c[-1]*xs for i in range(3, len(c) + 1): tmp = c0 nd = nd - 1 c0 = hermsub(c[-i]*xs, c1*(2*(nd - 1))) c1 = hermadd(tmp, hermmulx(c1)*2) return hermadd(c0, hermmulx(c1)*2)
def hermdiv(c1, c2): """ Divide one Hermite series by another.
Returns the quotient-with-remainder of two Hermite series `c1` / `c2`. The arguments are sequences of coefficients from lowest order "term" to highest, e.g., [1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
Parameters ---------- c1, c2 : array_like 1-D arrays of Hermite series coefficients ordered from low to high.
Returns ------- [quo, rem] : ndarrays Of Hermite series coefficients representing the quotient and remainder.
See Also -------- hermadd, hermsub, hermmulx, hermmul, hermpow
Notes ----- In general, the (polynomial) division of one Hermite series by another results in quotient and remainder terms that are not in the Hermite polynomial basis set. Thus, to express these results as a Hermite series, it is necessary to "reproject" the results onto the Hermite basis set, which may produce "unintuitive" (but correct) results; see Examples section below.
Examples -------- >>> from numpy.polynomial.hermite import hermdiv >>> hermdiv([ 52., 29., 52., 7., 6.], [0, 1, 2]) (array([1., 2., 3.]), array([0.])) >>> hermdiv([ 54., 31., 52., 7., 6.], [0, 1, 2]) (array([1., 2., 3.]), array([2., 2.])) >>> hermdiv([ 53., 30., 52., 7., 6.], [0, 1, 2]) (array([1., 2., 3.]), array([1., 1.]))
""" return pu._div(hermmul, c1, c2)
def hermpow(c, pow, maxpower=16): """Raise a Hermite series to a power.
Returns the Hermite series `c` raised to the power `pow`. The argument `c` is a sequence of coefficients ordered from low to high. i.e., [1,2,3] is the series ``P_0 + 2*P_1 + 3*P_2.``
Parameters ---------- c : array_like 1-D array of Hermite series coefficients ordered from low to high. pow : integer Power to which the series will be raised maxpower : integer, optional Maximum power allowed. This is mainly to limit growth of the series to unmanageable size. Default is 16
Returns ------- coef : ndarray Hermite series of power.
See Also -------- hermadd, hermsub, hermmulx, hermmul, hermdiv
Examples -------- >>> from numpy.polynomial.hermite import hermpow >>> hermpow([1, 2, 3], 2) array([81., 52., 82., 12., 9.])
""" return pu._pow(hermmul, c, pow, maxpower)
def hermder(c, m=1, scl=1, axis=0): """ Differentiate a Hermite series.
Returns the Hermite series coefficients `c` differentiated `m` times along `axis`. At each iteration the result is multiplied by `scl` (the scaling factor is for use in a linear change of variable). The argument `c` is an array of coefficients from low to high degree along each axis, e.g., [1,2,3] represents the series ``1*H_0 + 2*H_1 + 3*H_2`` while [[1,2],[1,2]] represents ``1*H_0(x)*H_0(y) + 1*H_1(x)*H_0(y) + 2*H_0(x)*H_1(y) + 2*H_1(x)*H_1(y)`` if axis=0 is ``x`` and axis=1 is ``y``.
Parameters ---------- c : array_like Array of Hermite series coefficients. If `c` is multidimensional the different axis correspond to different variables with the degree in each axis given by the corresponding index. m : int, optional Number of derivatives taken, must be non-negative. (Default: 1) scl : scalar, optional Each differentiation is multiplied by `scl`. The end result is multiplication by ``scl**m``. This is for use in a linear change of variable. (Default: 1) axis : int, optional Axis over which the derivative is taken. (Default: 0).
.. versionadded:: 1.7.0
Returns ------- der : ndarray Hermite series of the derivative.
See Also -------- hermint
Notes ----- In general, the result of differentiating a Hermite series does not resemble the same operation on a power series. Thus the result of this function may be "unintuitive," albeit correct; see Examples section below.
Examples -------- >>> from numpy.polynomial.hermite import hermder >>> hermder([ 1. , 0.5, 0.5, 0.5]) array([1., 2., 3.]) >>> hermder([-0.5, 1./2., 1./8., 1./12., 1./16.], m=2) array([1., 2., 3.])
""" c = np.array(c, ndmin=1, copy=True) if c.dtype.char in '?bBhHiIlLqQpP': c = c.astype(np.double) cnt = pu._deprecate_as_int(m, "the order of derivation") iaxis = pu._deprecate_as_int(axis, "the axis") if cnt < 0: raise ValueError("The order of derivation must be non-negative") iaxis = normalize_axis_index(iaxis, c.ndim)
if cnt == 0: return c
c = np.moveaxis(c, iaxis, 0) n = len(c) if cnt >= n: c = c[:1]*0 else: for i in range(cnt): n = n - 1 c *= scl der = np.empty((n,) + c.shape[1:], dtype=c.dtype) for j in range(n, 0, -1): der[j - 1] = (2*j)*c[j] c = der c = np.moveaxis(c, 0, iaxis) return c
def hermint(c, m=1, k=[], lbnd=0, scl=1, axis=0): """ Integrate a Hermite series.
Returns the Hermite series coefficients `c` integrated `m` times from `lbnd` along `axis`. At each iteration the resulting series is **multiplied** by `scl` and an integration constant, `k`, is added. The scaling factor is for use in a linear change of variable. ("Buyer beware": note that, depending on what one is doing, one may want `scl` to be the reciprocal of what one might expect; for more information, see the Notes section below.) The argument `c` is an array of coefficients from low to high degree along each axis, e.g., [1,2,3] represents the series ``H_0 + 2*H_1 + 3*H_2`` while [[1,2],[1,2]] represents ``1*H_0(x)*H_0(y) + 1*H_1(x)*H_0(y) + 2*H_0(x)*H_1(y) + 2*H_1(x)*H_1(y)`` if axis=0 is ``x`` and axis=1 is ``y``.
Parameters ---------- c : array_like Array of Hermite series coefficients. If c is multidimensional the different axis correspond to different variables with the degree in each axis given by the corresponding index. m : int, optional Order of integration, must be positive. (Default: 1) k : {[], list, scalar}, optional Integration constant(s). The value of the first integral at ``lbnd`` is the first value in the list, the value of the second integral at ``lbnd`` is the second value, etc. If ``k == []`` (the default), all constants are set to zero. If ``m == 1``, a single scalar can be given instead of a list. lbnd : scalar, optional The lower bound of the integral. (Default: 0) scl : scalar, optional Following each integration the result is *multiplied* by `scl` before the integration constant is added. (Default: 1) axis : int, optional Axis over which the integral is taken. (Default: 0).
.. versionadded:: 1.7.0
Returns ------- S : ndarray Hermite series coefficients of the integral.
Raises ------ ValueError If ``m < 0``, ``len(k) > m``, ``np.ndim(lbnd) != 0``, or ``np.ndim(scl) != 0``.
See Also -------- hermder
Notes ----- Note that the result of each integration is *multiplied* by `scl`. Why is this important to note? Say one is making a linear change of variable :math:`u = ax + b` in an integral relative to `x`. Then :math:`dx = du/a`, so one will need to set `scl` equal to :math:`1/a` - perhaps not what one would have first thought.
Also note that, in general, the result of integrating a C-series needs to be "reprojected" onto the C-series basis set. Thus, typically, the result of this function is "unintuitive," albeit correct; see Examples section below.
Examples -------- >>> from numpy.polynomial.hermite import hermint >>> hermint([1,2,3]) # integrate once, value 0 at 0. array([1. , 0.5, 0.5, 0.5]) >>> hermint([1,2,3], m=2) # integrate twice, value & deriv 0 at 0 array([-0.5 , 0.5 , 0.125 , 0.08333333, 0.0625 ]) # may vary >>> hermint([1,2,3], k=1) # integrate once, value 1 at 0. array([2. , 0.5, 0.5, 0.5]) >>> hermint([1,2,3], lbnd=-1) # integrate once, value 0 at -1 array([-2. , 0.5, 0.5, 0.5]) >>> hermint([1,2,3], m=2, k=[1,2], lbnd=-1) array([ 1.66666667, -0.5 , 0.125 , 0.08333333, 0.0625 ]) # may vary
""" c = np.array(c, ndmin=1, copy=True) if c.dtype.char in '?bBhHiIlLqQpP': c = c.astype(np.double) if not np.iterable(k): k = [k] cnt = pu._deprecate_as_int(m, "the order of integration") iaxis = pu._deprecate_as_int(axis, "the axis") if cnt < 0: raise ValueError("The order of integration must be non-negative") if len(k) > cnt: raise ValueError("Too many integration constants") if np.ndim(lbnd) != 0: raise ValueError("lbnd must be a scalar.") if np.ndim(scl) != 0: raise ValueError("scl must be a scalar.") iaxis = normalize_axis_index(iaxis, c.ndim)
if cnt == 0: return c
c = np.moveaxis(c, iaxis, 0) k = list(k) + [0]*(cnt - len(k)) for i in range(cnt): n = len(c) c *= scl if n == 1 and np.all(c[0] == 0): c[0] += k[i] else: tmp = np.empty((n + 1,) + c.shape[1:], dtype=c.dtype) tmp[0] = c[0]*0 tmp[1] = c[0]/2 for j in range(1, n): tmp[j + 1] = c[j]/(2*(j + 1)) tmp[0] += k[i] - hermval(lbnd, tmp) c = tmp c = np.moveaxis(c, 0, iaxis) return c
def hermval(x, c, tensor=True): """ Evaluate an Hermite series at points x.
If `c` is of length `n + 1`, this function returns the value:
.. math:: p(x) = c_0 * H_0(x) + c_1 * H_1(x) + ... + c_n * H_n(x)
The parameter `x` is converted to an array only if it is a tuple or a list, otherwise it is treated as a scalar. In either case, either `x` or its elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` is a 1-D array, then `p(x)` will have the same shape as `x`. If `c` is multidimensional, then the shape of the result depends on the value of `tensor`. If `tensor` is true the shape will be c.shape[1:] + x.shape. If `tensor` is false the shape will be c.shape[1:]. Note that scalars have shape (,).
Trailing zeros in the coefficients will be used in the evaluation, so they should be avoided if efficiency is a concern.
Parameters ---------- x : array_like, compatible object If `x` is a list or tuple, it is converted to an ndarray, otherwise it is left unchanged and treated as a scalar. In either case, `x` or its elements must support addition and multiplication with themselves and with the elements of `c`. c : array_like Array of coefficients ordered so that the coefficients for terms of degree n are contained in c[n]. If `c` is multidimensional the remaining indices enumerate multiple polynomials. In the two dimensional case the coefficients may be thought of as stored in the columns of `c`. tensor : boolean, optional If True, the shape of the coefficient array is extended with ones on the right, one for each dimension of `x`. Scalars have dimension 0 for this action. The result is that every column of coefficients in `c` is evaluated for every element of `x`. If False, `x` is broadcast over the columns of `c` for the evaluation. This keyword is useful when `c` is multidimensional. The default value is True.
.. versionadded:: 1.7.0
Returns ------- values : ndarray, algebra_like The shape of the return value is described above.
See Also -------- hermval2d, hermgrid2d, hermval3d, hermgrid3d
Notes ----- The evaluation uses Clenshaw recursion, aka synthetic division.
Examples -------- >>> from numpy.polynomial.hermite import hermval >>> coef = [1,2,3] >>> hermval(1, coef) 11.0 >>> hermval([[1,2],[3,4]], coef) array([[ 11., 51.], [115., 203.]])
""" c = np.array(c, ndmin=1, copy=False) if c.dtype.char in '?bBhHiIlLqQpP': c = c.astype(np.double) if isinstance(x, (tuple, list)): x = np.asarray(x) if isinstance(x, np.ndarray) and tensor: c = c.reshape(c.shape + (1,)*x.ndim)
x2 = x*2 if len(c) == 1: c0 = c[0] c1 = 0 elif len(c) == 2: c0 = c[0] c1 = c[1] else: nd = len(c) c0 = c[-2] c1 = c[-1] for i in range(3, len(c) + 1): tmp = c0 nd = nd - 1 c0 = c[-i] - c1*(2*(nd - 1)) c1 = tmp + c1*x2 return c0 + c1*x2
def hermval2d(x, y, c): """ Evaluate a 2-D Hermite series at points (x, y).
This function returns the values:
.. math:: p(x,y) = \\sum_{i,j} c_{i,j} * H_i(x) * H_j(y)
The parameters `x` and `y` are converted to arrays only if they are tuples or a lists, otherwise they are treated as a scalars and they must have the same shape after conversion. In either case, either `x` and `y` or their elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` is a 1-D array a one is implicitly appended to its shape to make it 2-D. The shape of the result will be c.shape[2:] + x.shape.
Parameters ---------- x, y : array_like, compatible objects The two dimensional series is evaluated at the points `(x, y)`, where `x` and `y` must have the same shape. If `x` or `y` is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and if it isn't an ndarray it is treated as a scalar. c : array_like Array of coefficients ordered so that the coefficient of the term of multi-degree i,j is contained in ``c[i,j]``. If `c` has dimension greater than two the remaining indices enumerate multiple sets of coefficients.
Returns ------- values : ndarray, compatible object The values of the two dimensional polynomial at points formed with pairs of corresponding values from `x` and `y`.
See Also -------- hermval, hermgrid2d, hermval3d, hermgrid3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._valnd(hermval, c, x, y)
def hermgrid2d(x, y, c): """ Evaluate a 2-D Hermite series on the Cartesian product of x and y.
This function returns the values:
.. math:: p(a,b) = \\sum_{i,j} c_{i,j} * H_i(a) * H_j(b)
where the points `(a, b)` consist of all pairs formed by taking `a` from `x` and `b` from `y`. The resulting points form a grid with `x` in the first dimension and `y` in the second.
The parameters `x` and `y` are converted to arrays only if they are tuples or a lists, otherwise they are treated as a scalars. In either case, either `x` and `y` or their elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` has fewer than two dimensions, ones are implicitly appended to its shape to make it 2-D. The shape of the result will be c.shape[2:] + x.shape.
Parameters ---------- x, y : array_like, compatible objects The two dimensional series is evaluated at the points in the Cartesian product of `x` and `y`. If `x` or `y` is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and, if it isn't an ndarray, it is treated as a scalar. c : array_like Array of coefficients ordered so that the coefficients for terms of degree i,j are contained in ``c[i,j]``. If `c` has dimension greater than two the remaining indices enumerate multiple sets of coefficients.
Returns ------- values : ndarray, compatible object The values of the two dimensional polynomial at points in the Cartesian product of `x` and `y`.
See Also -------- hermval, hermval2d, hermval3d, hermgrid3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._gridnd(hermval, c, x, y)
def hermval3d(x, y, z, c): """ Evaluate a 3-D Hermite series at points (x, y, z).
This function returns the values:
.. math:: p(x,y,z) = \\sum_{i,j,k} c_{i,j,k} * H_i(x) * H_j(y) * H_k(z)
The parameters `x`, `y`, and `z` are converted to arrays only if they are tuples or a lists, otherwise they are treated as a scalars and they must have the same shape after conversion. In either case, either `x`, `y`, and `z` or their elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` has fewer than 3 dimensions, ones are implicitly appended to its shape to make it 3-D. The shape of the result will be c.shape[3:] + x.shape.
Parameters ---------- x, y, z : array_like, compatible object The three dimensional series is evaluated at the points `(x, y, z)`, where `x`, `y`, and `z` must have the same shape. If any of `x`, `y`, or `z` is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and if it isn't an ndarray it is treated as a scalar. c : array_like Array of coefficients ordered so that the coefficient of the term of multi-degree i,j,k is contained in ``c[i,j,k]``. If `c` has dimension greater than 3 the remaining indices enumerate multiple sets of coefficients.
Returns ------- values : ndarray, compatible object The values of the multidimensional polynomial on points formed with triples of corresponding values from `x`, `y`, and `z`.
See Also -------- hermval, hermval2d, hermgrid2d, hermgrid3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._valnd(hermval, c, x, y, z)
def hermgrid3d(x, y, z, c): """ Evaluate a 3-D Hermite series on the Cartesian product of x, y, and z.
This function returns the values:
.. math:: p(a,b,c) = \\sum_{i,j,k} c_{i,j,k} * H_i(a) * H_j(b) * H_k(c)
where the points `(a, b, c)` consist of all triples formed by taking `a` from `x`, `b` from `y`, and `c` from `z`. The resulting points form a grid with `x` in the first dimension, `y` in the second, and `z` in the third.
The parameters `x`, `y`, and `z` are converted to arrays only if they are tuples or a lists, otherwise they are treated as a scalars. In either case, either `x`, `y`, and `z` or their elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` has fewer than three dimensions, ones are implicitly appended to its shape to make it 3-D. The shape of the result will be c.shape[3:] + x.shape + y.shape + z.shape.
Parameters ---------- x, y, z : array_like, compatible objects The three dimensional series is evaluated at the points in the Cartesian product of `x`, `y`, and `z`. If `x`,`y`, or `z` is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and, if it isn't an ndarray, it is treated as a scalar. c : array_like Array of coefficients ordered so that the coefficients for terms of degree i,j are contained in ``c[i,j]``. If `c` has dimension greater than two the remaining indices enumerate multiple sets of coefficients.
Returns ------- values : ndarray, compatible object The values of the two dimensional polynomial at points in the Cartesian product of `x` and `y`.
See Also -------- hermval, hermval2d, hermgrid2d, hermval3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._gridnd(hermval, c, x, y, z)
def hermvander(x, deg): """Pseudo-Vandermonde matrix of given degree.
Returns the pseudo-Vandermonde matrix of degree `deg` and sample points `x`. The pseudo-Vandermonde matrix is defined by
.. math:: V[..., i] = H_i(x),
where `0 <= i <= deg`. The leading indices of `V` index the elements of `x` and the last index is the degree of the Hermite polynomial.
If `c` is a 1-D array of coefficients of length `n + 1` and `V` is the array ``V = hermvander(x, n)``, then ``np.dot(V, c)`` and ``hermval(x, c)`` are the same up to roundoff. This equivalence is useful both for least squares fitting and for the evaluation of a large number of Hermite series of the same degree and sample points.
Parameters ---------- x : array_like Array of points. The dtype is converted to float64 or complex128 depending on whether any of the elements are complex. If `x` is scalar it is converted to a 1-D array. deg : int Degree of the resulting matrix.
Returns ------- vander : ndarray The pseudo-Vandermonde matrix. The shape of the returned matrix is ``x.shape + (deg + 1,)``, where The last index is the degree of the corresponding Hermite polynomial. The dtype will be the same as the converted `x`.
Examples -------- >>> from numpy.polynomial.hermite import hermvander >>> x = np.array([-1, 0, 1]) >>> hermvander(x, 3) array([[ 1., -2., 2., 4.], [ 1., 0., -2., -0.], [ 1., 2., 2., -4.]])
""" ideg = pu._deprecate_as_int(deg, "deg") if ideg < 0: raise ValueError("deg must be non-negative")
x = np.array(x, copy=False, ndmin=1) + 0.0 dims = (ideg + 1,) + x.shape dtyp = x.dtype v = np.empty(dims, dtype=dtyp) v[0] = x*0 + 1 if ideg > 0: x2 = x*2 v[1] = x2 for i in range(2, ideg + 1): v[i] = (v[i-1]*x2 - v[i-2]*(2*(i - 1))) return np.moveaxis(v, 0, -1)
def hermvander2d(x, y, deg): """Pseudo-Vandermonde matrix of given degrees.
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample points `(x, y)`. The pseudo-Vandermonde matrix is defined by
.. math:: V[..., (deg[1] + 1)*i + j] = H_i(x) * H_j(y),
where `0 <= i <= deg[0]` and `0 <= j <= deg[1]`. The leading indices of `V` index the points `(x, y)` and the last index encodes the degrees of the Hermite polynomials.
If ``V = hermvander2d(x, y, [xdeg, ydeg])``, then the columns of `V` correspond to the elements of a 2-D coefficient array `c` of shape (xdeg + 1, ydeg + 1) in the order
.. math:: c_{00}, c_{01}, c_{02} ... , c_{10}, c_{11}, c_{12} ...
and ``np.dot(V, c.flat)`` and ``hermval2d(x, y, c)`` will be the same up to roundoff. This equivalence is useful both for least squares fitting and for the evaluation of a large number of 2-D Hermite series of the same degrees and sample points.
Parameters ---------- x, y : array_like Arrays of point coordinates, all of the same shape. The dtypes will be converted to either float64 or complex128 depending on whether any of the elements are complex. Scalars are converted to 1-D arrays. deg : list of ints List of maximum degrees of the form [x_deg, y_deg].
Returns ------- vander2d : ndarray The shape of the returned matrix is ``x.shape + (order,)``, where :math:`order = (deg[0]+1)*(deg[1]+1)`. The dtype will be the same as the converted `x` and `y`.
See Also -------- hermvander, hermvander3d, hermval2d, hermval3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._vander_nd_flat((hermvander, hermvander), (x, y), deg)
def hermvander3d(x, y, z, deg): """Pseudo-Vandermonde matrix of given degrees.
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample points `(x, y, z)`. If `l, m, n` are the given degrees in `x, y, z`, then The pseudo-Vandermonde matrix is defined by
.. math:: V[..., (m+1)(n+1)i + (n+1)j + k] = H_i(x)*H_j(y)*H_k(z),
where `0 <= i <= l`, `0 <= j <= m`, and `0 <= j <= n`. The leading indices of `V` index the points `(x, y, z)` and the last index encodes the degrees of the Hermite polynomials.
If ``V = hermvander3d(x, y, z, [xdeg, ydeg, zdeg])``, then the columns of `V` correspond to the elements of a 3-D coefficient array `c` of shape (xdeg + 1, ydeg + 1, zdeg + 1) in the order
.. math:: c_{000}, c_{001}, c_{002},... , c_{010}, c_{011}, c_{012},...
and ``np.dot(V, c.flat)`` and ``hermval3d(x, y, z, c)`` will be the same up to roundoff. This equivalence is useful both for least squares fitting and for the evaluation of a large number of 3-D Hermite series of the same degrees and sample points.
Parameters ---------- x, y, z : array_like Arrays of point coordinates, all of the same shape. The dtypes will be converted to either float64 or complex128 depending on whether any of the elements are complex. Scalars are converted to 1-D arrays. deg : list of ints List of maximum degrees of the form [x_deg, y_deg, z_deg].
Returns ------- vander3d : ndarray The shape of the returned matrix is ``x.shape + (order,)``, where :math:`order = (deg[0]+1)*(deg[1]+1)*(deg[2]+1)`. The dtype will be the same as the converted `x`, `y`, and `z`.
See Also -------- hermvander, hermvander3d, hermval2d, hermval3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._vander_nd_flat((hermvander, hermvander, hermvander), (x, y, z), deg)
def hermfit(x, y, deg, rcond=None, full=False, w=None): """ Least squares fit of Hermite series to data.
Return the coefficients of a Hermite series of degree `deg` that is the least squares fit to the data values `y` given at points `x`. If `y` is 1-D the returned coefficients will also be 1-D. If `y` is 2-D multiple fits are done, one for each column of `y`, and the resulting coefficients are stored in the corresponding columns of a 2-D return. The fitted polynomial(s) are in the form
.. math:: p(x) = c_0 + c_1 * H_1(x) + ... + c_n * H_n(x),
where `n` is `deg`.
Parameters ---------- x : array_like, shape (M,) x-coordinates of the M sample points ``(x[i], y[i])``. y : array_like, shape (M,) or (M, K) y-coordinates of the sample points. Several data sets of sample points sharing the same x-coordinates can be fitted at once by passing in a 2D-array that contains one dataset per column. deg : int or 1-D array_like Degree(s) of the fitting polynomials. If `deg` is a single integer all terms up to and including the `deg`'th term are included in the fit. For NumPy versions >= 1.11.0 a list of integers specifying the degrees of the terms to include may be used instead. rcond : float, optional Relative condition number of the fit. Singular values smaller than this relative to the largest singular value will be ignored. The default value is len(x)*eps, where eps is the relative precision of the float type, about 2e-16 in most cases. full : bool, optional Switch determining nature of return value. When it is False (the default) just the coefficients are returned, when True diagnostic information from the singular value decomposition is also returned. w : array_like, shape (`M`,), optional Weights. If not None, the weight ``w[i]`` applies to the unsquared residual ``y[i] - y_hat[i]`` at ``x[i]``. Ideally the weights are chosen so that the errors of the products ``w[i]*y[i]`` all have the same variance. When using inverse-variance weighting, use ``w[i] = 1/sigma(y[i])``. The default value is None.
Returns ------- coef : ndarray, shape (M,) or (M, K) Hermite coefficients ordered from low to high. If `y` was 2-D, the coefficients for the data in column k of `y` are in column `k`.
[residuals, rank, singular_values, rcond] : list These values are only returned if ``full == True``
- residuals -- sum of squared residuals of the least squares fit - rank -- the numerical rank of the scaled Vandermonde matrix - singular_values -- singular values of the scaled Vandermonde matrix - rcond -- value of `rcond`.
For more details, see `numpy.linalg.lstsq`.
Warns ----- RankWarning The rank of the coefficient matrix in the least-squares fit is deficient. The warning is only raised if ``full == False``. The warnings can be turned off by
>>> import warnings >>> warnings.simplefilter('ignore', np.RankWarning)
See Also -------- numpy.polynomial.chebyshev.chebfit numpy.polynomial.legendre.legfit numpy.polynomial.laguerre.lagfit numpy.polynomial.polynomial.polyfit numpy.polynomial.hermite_e.hermefit hermval : Evaluates a Hermite series. hermvander : Vandermonde matrix of Hermite series. hermweight : Hermite weight function numpy.linalg.lstsq : Computes a least-squares fit from the matrix. scipy.interpolate.UnivariateSpline : Computes spline fits.
Notes ----- The solution is the coefficients of the Hermite series `p` that minimizes the sum of the weighted squared errors
.. math:: E = \\sum_j w_j^2 * |y_j - p(x_j)|^2,
where the :math:`w_j` are the weights. This problem is solved by setting up the (typically) overdetermined matrix equation
.. math:: V(x) * c = w * y,
where `V` is the weighted pseudo Vandermonde matrix of `x`, `c` are the coefficients to be solved for, `w` are the weights, `y` are the observed values. This equation is then solved using the singular value decomposition of `V`.
If some of the singular values of `V` are so small that they are neglected, then a `RankWarning` will be issued. This means that the coefficient values may be poorly determined. Using a lower order fit will usually get rid of the warning. The `rcond` parameter can also be set to a value smaller than its default, but the resulting fit may be spurious and have large contributions from roundoff error.
Fits using Hermite series are probably most useful when the data can be approximated by ``sqrt(w(x)) * p(x)``, where `w(x)` is the Hermite weight. In that case the weight ``sqrt(w(x[i]))`` should be used together with data values ``y[i]/sqrt(w(x[i]))``. The weight function is available as `hermweight`.
References ---------- .. [1] Wikipedia, "Curve fitting", https://en.wikipedia.org/wiki/Curve_fitting
Examples -------- >>> from numpy.polynomial.hermite import hermfit, hermval >>> x = np.linspace(-10, 10) >>> err = np.random.randn(len(x))/10 >>> y = hermval(x, [1, 2, 3]) + err >>> hermfit(x, y, 2) array([1.0218, 1.9986, 2.9999]) # may vary
""" return pu._fit(hermvander, x, y, deg, rcond, full, w)
def hermcompanion(c): """Return the scaled companion matrix of c.
The basis polynomials are scaled so that the companion matrix is symmetric when `c` is an Hermite basis polynomial. This provides better eigenvalue estimates than the unscaled case and for basis polynomials the eigenvalues are guaranteed to be real if `numpy.linalg.eigvalsh` is used to obtain them.
Parameters ---------- c : array_like 1-D array of Hermite series coefficients ordered from low to high degree.
Returns ------- mat : ndarray Scaled companion matrix of dimensions (deg, deg).
Notes -----
.. versionadded:: 1.7.0
""" # c is a trimmed copy [c] = pu.as_series([c]) if len(c) < 2: raise ValueError('Series must have maximum degree of at least 1.') if len(c) == 2: return np.array([[-.5*c[0]/c[1]]])
n = len(c) - 1 mat = np.zeros((n, n), dtype=c.dtype) scl = np.hstack((1., 1./np.sqrt(2.*np.arange(n - 1, 0, -1)))) scl = np.multiply.accumulate(scl)[::-1] top = mat.reshape(-1)[1::n+1] bot = mat.reshape(-1)[n::n+1] top[...] = np.sqrt(.5*np.arange(1, n)) bot[...] = top mat[:, -1] -= scl*c[:-1]/(2.0*c[-1]) return mat
def hermroots(c): """ Compute the roots of a Hermite series.
Return the roots (a.k.a. "zeros") of the polynomial
.. math:: p(x) = \\sum_i c[i] * H_i(x).
Parameters ---------- c : 1-D array_like 1-D array of coefficients.
Returns ------- out : ndarray Array of the roots of the series. If all the roots are real, then `out` is also real, otherwise it is complex.
See Also -------- numpy.polynomial.polynomial.polyroots numpy.polynomial.legendre.legroots numpy.polynomial.laguerre.lagroots numpy.polynomial.chebyshev.chebroots numpy.polynomial.hermite_e.hermeroots
Notes ----- The root estimates are obtained as the eigenvalues of the companion matrix, Roots far from the origin of the complex plane may have large errors due to the numerical instability of the series for such values. Roots with multiplicity greater than 1 will also show larger errors as the value of the series near such points is relatively insensitive to errors in the roots. Isolated roots near the origin can be improved by a few iterations of Newton's method.
The Hermite series basis polynomials aren't powers of `x` so the results of this function may seem unintuitive.
Examples -------- >>> from numpy.polynomial.hermite import hermroots, hermfromroots >>> coef = hermfromroots([-1, 0, 1]) >>> coef array([0. , 0.25 , 0. , 0.125]) >>> hermroots(coef) array([-1.00000000e+00, -1.38777878e-17, 1.00000000e+00])
""" # c is a trimmed copy [c] = pu.as_series([c]) if len(c) <= 1: return np.array([], dtype=c.dtype) if len(c) == 2: return np.array([-.5*c[0]/c[1]])
# rotated companion matrix reduces error m = hermcompanion(c)[::-1,::-1] r = la.eigvals(m) r.sort() return r
def _normed_hermite_n(x, n): """ Evaluate a normalized Hermite polynomial.
Compute the value of the normalized Hermite polynomial of degree ``n`` at the points ``x``.
Parameters ---------- x : ndarray of double. Points at which to evaluate the function n : int Degree of the normalized Hermite function to be evaluated.
Returns ------- values : ndarray The shape of the return value is described above.
Notes ----- .. versionadded:: 1.10.0
This function is needed for finding the Gauss points and integration weights for high degrees. The values of the standard Hermite functions overflow when n >= 207.
""" if n == 0: return np.full(x.shape, 1/np.sqrt(np.sqrt(np.pi)))
c0 = 0. c1 = 1./np.sqrt(np.sqrt(np.pi)) nd = float(n) for i in range(n - 1): tmp = c0 c0 = -c1*np.sqrt((nd - 1.)/nd) c1 = tmp + c1*x*np.sqrt(2./nd) nd = nd - 1.0 return c0 + c1*x*np.sqrt(2)
def hermgauss(deg): """ Gauss-Hermite quadrature.
Computes the sample points and weights for Gauss-Hermite quadrature. These sample points and weights will correctly integrate polynomials of degree :math:`2*deg - 1` or less over the interval :math:`[-\\inf, \\inf]` with the weight function :math:`f(x) = \\exp(-x^2)`.
Parameters ---------- deg : int Number of sample points and weights. It must be >= 1.
Returns ------- x : ndarray 1-D ndarray containing the sample points. y : ndarray 1-D ndarray containing the weights.
Notes -----
.. versionadded:: 1.7.0
The results have only been tested up to degree 100, higher degrees may be problematic. The weights are determined by using the fact that
.. math:: w_k = c / (H'_n(x_k) * H_{n-1}(x_k))
where :math:`c` is a constant independent of :math:`k` and :math:`x_k` is the k'th root of :math:`H_n`, and then scaling the results to get the right value when integrating 1.
""" ideg = pu._deprecate_as_int(deg, "deg") if ideg <= 0: raise ValueError("deg must be a positive integer")
# first approximation of roots. We use the fact that the companion # matrix is symmetric in this case in order to obtain better zeros. c = np.array([0]*deg + [1], dtype=np.float64) m = hermcompanion(c) x = la.eigvalsh(m)
# improve roots by one application of Newton dy = _normed_hermite_n(x, ideg) df = _normed_hermite_n(x, ideg - 1) * np.sqrt(2*ideg) x -= dy/df
# compute the weights. We scale the factor to avoid possible numerical # overflow. fm = _normed_hermite_n(x, ideg - 1) fm /= np.abs(fm).max() w = 1/(fm * fm)
# for Hermite we can also symmetrize w = (w + w[::-1])/2 x = (x - x[::-1])/2
# scale w to get the right value w *= np.sqrt(np.pi) / w.sum()
return x, w
def hermweight(x): """ Weight function of the Hermite polynomials.
The weight function is :math:`\\exp(-x^2)` and the interval of integration is :math:`[-\\inf, \\inf]`. the Hermite polynomials are orthogonal, but not normalized, with respect to this weight function.
Parameters ---------- x : array_like Values at which the weight function will be computed.
Returns ------- w : ndarray The weight function at `x`.
Notes -----
.. versionadded:: 1.7.0
""" w = np.exp(-x**2) return w
## Hermite series class#
class Hermite(ABCPolyBase): """An Hermite series class.
The Hermite class provides the standard Python numerical methods '+', '-', '*', '//', '%', 'divmod', '**', and '()' as well as the attributes and methods listed in the `ABCPolyBase` documentation.
Parameters ---------- coef : array_like Hermite coefficients in order of increasing degree, i.e, ``(1, 2, 3)`` gives ``1*H_0(x) + 2*H_1(X) + 3*H_2(x)``. domain : (2,) array_like, optional Domain to use. The interval ``[domain[0], domain[1]]`` is mapped to the interval ``[window[0], window[1]]`` by shifting and scaling. The default value is [-1, 1]. window : (2,) array_like, optional Window, see `domain` for its use. The default value is [-1, 1].
.. versionadded:: 1.6.0 symbol : str, optional Symbol used to represent the independent variable in string representations of the polynomial expression, e.g. for printing. The symbol must be a valid Python identifier. Default value is 'x'.
.. versionadded:: 1.24
""" # Virtual Functions _add = staticmethod(hermadd) _sub = staticmethod(hermsub) _mul = staticmethod(hermmul) _div = staticmethod(hermdiv) _pow = staticmethod(hermpow) _val = staticmethod(hermval) _int = staticmethod(hermint) _der = staticmethod(hermder) _fit = staticmethod(hermfit) _line = staticmethod(hermline) _roots = staticmethod(hermroots) _fromroots = staticmethod(hermfromroots)
# Virtual properties domain = np.array(hermdomain) window = np.array(hermdomain) basis_name = 'H'12345678910111213141516171819202122232425262728293031323334353637383940414243444546from typing import Any
from numpy import ndarray, dtype, int_, float_from numpy.polynomial._polybase import ABCPolyBasefrom numpy.polynomial.polyutils import trimcoef
__all__: list[str]
hermtrim = trimcoef
def poly2herm(pol): ...def herm2poly(c): ...
hermdomain: ndarray[Any, dtype[int_]]hermzero: ndarray[Any, dtype[int_]]hermone: ndarray[Any, dtype[int_]]hermx: ndarray[Any, dtype[float_]]
def hermline(off, scl): ...def hermfromroots(roots): ...def hermadd(c1, c2): ...def hermsub(c1, c2): ...def hermmulx(c): ...def hermmul(c1, c2): ...def hermdiv(c1, c2): ...def hermpow(c, pow, maxpower=...): ...def hermder(c, m=..., scl=..., axis=...): ...def hermint(c, m=..., k = ..., lbnd=..., scl=..., axis=...): ...def hermval(x, c, tensor=...): ...def hermval2d(x, y, c): ...def hermgrid2d(x, y, c): ...def hermval3d(x, y, z, c): ...def hermgrid3d(x, y, z, c): ...def hermvander(x, deg): ...def hermvander2d(x, y, deg): ...def hermvander3d(x, y, z, deg): ...def hermfit(x, y, deg, rcond=..., full=..., w=...): ...def hermcompanion(c): ...def hermroots(c): ...def hermgauss(deg): ...def hermweight(x): ...
class Hermite(ABCPolyBase): domain: Any window: Any basis_name: Any123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695"""===================================================================HermiteE Series, "Probabilists" (:mod:`numpy.polynomial.hermite_e`)===================================================================
This module provides a number of objects (mostly functions) useful fordealing with Hermite_e series, including a `HermiteE` class thatencapsulates the usual arithmetic operations. (General informationon how this module represents and works with such polynomials is in thedocstring for its "parent" sub-package, `numpy.polynomial`).
Classes-------.. autosummary:: :toctree: generated/
HermiteE
Constants---------.. autosummary:: :toctree: generated/
hermedomain hermezero hermeone hermex
Arithmetic----------.. autosummary:: :toctree: generated/
hermeadd hermesub hermemulx hermemul hermediv hermepow hermeval hermeval2d hermeval3d hermegrid2d hermegrid3d
Calculus--------.. autosummary:: :toctree: generated/
hermeder hermeint
Misc Functions--------------.. autosummary:: :toctree: generated/
hermefromroots hermeroots hermevander hermevander2d hermevander3d hermegauss hermeweight hermecompanion hermefit hermetrim hermeline herme2poly poly2herme
See also--------`numpy.polynomial`
"""import numpy as npimport numpy.linalg as lafrom numpy.core.multiarray import normalize_axis_index
from . import polyutils as pufrom ._polybase import ABCPolyBase
__all__ = [ 'hermezero', 'hermeone', 'hermex', 'hermedomain', 'hermeline', 'hermeadd', 'hermesub', 'hermemulx', 'hermemul', 'hermediv', 'hermepow', 'hermeval', 'hermeder', 'hermeint', 'herme2poly', 'poly2herme', 'hermefromroots', 'hermevander', 'hermefit', 'hermetrim', 'hermeroots', 'HermiteE', 'hermeval2d', 'hermeval3d', 'hermegrid2d', 'hermegrid3d', 'hermevander2d', 'hermevander3d', 'hermecompanion', 'hermegauss', 'hermeweight']
hermetrim = pu.trimcoef
def poly2herme(pol): """ poly2herme(pol)
Convert a polynomial to a Hermite series.
Convert an array representing the coefficients of a polynomial (relative to the "standard" basis) ordered from lowest degree to highest, to an array of the coefficients of the equivalent Hermite series, ordered from lowest to highest degree.
Parameters ---------- pol : array_like 1-D array containing the polynomial coefficients
Returns ------- c : ndarray 1-D array containing the coefficients of the equivalent Hermite series.
See Also -------- herme2poly
Notes ----- The easy way to do conversions between polynomial basis sets is to use the convert method of a class instance.
Examples -------- >>> from numpy.polynomial.hermite_e import poly2herme >>> poly2herme(np.arange(4)) array([ 2., 10., 2., 3.])
""" [pol] = pu.as_series([pol]) deg = len(pol) - 1 res = 0 for i in range(deg, -1, -1): res = hermeadd(hermemulx(res), pol[i]) return res
def herme2poly(c): """ Convert a Hermite series to a polynomial.
Convert an array representing the coefficients of a Hermite series, ordered from lowest degree to highest, to an array of the coefficients of the equivalent polynomial (relative to the "standard" basis) ordered from lowest to highest degree.
Parameters ---------- c : array_like 1-D array containing the Hermite series coefficients, ordered from lowest order term to highest.
Returns ------- pol : ndarray 1-D array containing the coefficients of the equivalent polynomial (relative to the "standard" basis) ordered from lowest order term to highest.
See Also -------- poly2herme
Notes ----- The easy way to do conversions between polynomial basis sets is to use the convert method of a class instance.
Examples -------- >>> from numpy.polynomial.hermite_e import herme2poly >>> herme2poly([ 2., 10., 2., 3.]) array([0., 1., 2., 3.])
""" from .polynomial import polyadd, polysub, polymulx
[c] = pu.as_series([c]) n = len(c) if n == 1: return c if n == 2: return c else: c0 = c[-2] c1 = c[-1] # i is the current degree of c1 for i in range(n - 1, 1, -1): tmp = c0 c0 = polysub(c[i - 2], c1*(i - 1)) c1 = polyadd(tmp, polymulx(c1)) return polyadd(c0, polymulx(c1))
## These are constant arrays are of integer type so as to be compatible# with the widest range of other types, such as Decimal.#
# Hermitehermedomain = np.array([-1, 1])
# Hermite coefficients representing zero.hermezero = np.array([0])
# Hermite coefficients representing one.hermeone = np.array([1])
# Hermite coefficients representing the identity x.hermex = np.array([0, 1])
def hermeline(off, scl): """ Hermite series whose graph is a straight line.
Parameters ---------- off, scl : scalars The specified line is given by ``off + scl*x``.
Returns ------- y : ndarray This module's representation of the Hermite series for ``off + scl*x``.
See Also -------- numpy.polynomial.polynomial.polyline numpy.polynomial.chebyshev.chebline numpy.polynomial.legendre.legline numpy.polynomial.laguerre.lagline numpy.polynomial.hermite.hermline
Examples -------- >>> from numpy.polynomial.hermite_e import hermeline >>> from numpy.polynomial.hermite_e import hermeline, hermeval >>> hermeval(0,hermeline(3, 2)) 3.0 >>> hermeval(1,hermeline(3, 2)) 5.0
""" if scl != 0: return np.array([off, scl]) else: return np.array([off])
def hermefromroots(roots): """ Generate a HermiteE series with given roots.
The function returns the coefficients of the polynomial
.. math:: p(x) = (x - r_0) * (x - r_1) * ... * (x - r_n),
in HermiteE form, where the `r_n` are the roots specified in `roots`. If a zero has multiplicity n, then it must appear in `roots` n times. For instance, if 2 is a root of multiplicity three and 3 is a root of multiplicity 2, then `roots` looks something like [2, 2, 2, 3, 3]. The roots can appear in any order.
If the returned coefficients are `c`, then
.. math:: p(x) = c_0 + c_1 * He_1(x) + ... + c_n * He_n(x)
The coefficient of the last term is not generally 1 for monic polynomials in HermiteE form.
Parameters ---------- roots : array_like Sequence containing the roots.
Returns ------- out : ndarray 1-D array of coefficients. If all roots are real then `out` is a real array, if some of the roots are complex, then `out` is complex even if all the coefficients in the result are real (see Examples below).
See Also -------- numpy.polynomial.polynomial.polyfromroots numpy.polynomial.legendre.legfromroots numpy.polynomial.laguerre.lagfromroots numpy.polynomial.hermite.hermfromroots numpy.polynomial.chebyshev.chebfromroots
Examples -------- >>> from numpy.polynomial.hermite_e import hermefromroots, hermeval >>> coef = hermefromroots((-1, 0, 1)) >>> hermeval((-1, 0, 1), coef) array([0., 0., 0.]) >>> coef = hermefromroots((-1j, 1j)) >>> hermeval((-1j, 1j), coef) array([0.+0.j, 0.+0.j])
""" return pu._fromroots(hermeline, hermemul, roots)
def hermeadd(c1, c2): """ Add one Hermite series to another.
Returns the sum of two Hermite series `c1` + `c2`. The arguments are sequences of coefficients ordered from lowest order term to highest, i.e., [1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
Parameters ---------- c1, c2 : array_like 1-D arrays of Hermite series coefficients ordered from low to high.
Returns ------- out : ndarray Array representing the Hermite series of their sum.
See Also -------- hermesub, hermemulx, hermemul, hermediv, hermepow
Notes ----- Unlike multiplication, division, etc., the sum of two Hermite series is a Hermite series (without having to "reproject" the result onto the basis set) so addition, just like that of "standard" polynomials, is simply "component-wise."
Examples -------- >>> from numpy.polynomial.hermite_e import hermeadd >>> hermeadd([1, 2, 3], [1, 2, 3, 4]) array([2., 4., 6., 4.])
""" return pu._add(c1, c2)
def hermesub(c1, c2): """ Subtract one Hermite series from another.
Returns the difference of two Hermite series `c1` - `c2`. The sequences of coefficients are from lowest order term to highest, i.e., [1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
Parameters ---------- c1, c2 : array_like 1-D arrays of Hermite series coefficients ordered from low to high.
Returns ------- out : ndarray Of Hermite series coefficients representing their difference.
See Also -------- hermeadd, hermemulx, hermemul, hermediv, hermepow
Notes ----- Unlike multiplication, division, etc., the difference of two Hermite series is a Hermite series (without having to "reproject" the result onto the basis set) so subtraction, just like that of "standard" polynomials, is simply "component-wise."
Examples -------- >>> from numpy.polynomial.hermite_e import hermesub >>> hermesub([1, 2, 3, 4], [1, 2, 3]) array([0., 0., 0., 4.])
""" return pu._sub(c1, c2)
def hermemulx(c): """Multiply a Hermite series by x.
Multiply the Hermite series `c` by x, where x is the independent variable.
Parameters ---------- c : array_like 1-D array of Hermite series coefficients ordered from low to high.
Returns ------- out : ndarray Array representing the result of the multiplication.
Notes ----- The multiplication uses the recursion relationship for Hermite polynomials in the form
.. math::
xP_i(x) = (P_{i + 1}(x) + iP_{i - 1}(x)))
Examples -------- >>> from numpy.polynomial.hermite_e import hermemulx >>> hermemulx([1, 2, 3]) array([2., 7., 2., 3.])
""" # c is a trimmed copy [c] = pu.as_series([c]) # The zero series needs special treatment if len(c) == 1 and c[0] == 0: return c
prd = np.empty(len(c) + 1, dtype=c.dtype) prd[0] = c[0]*0 prd[1] = c[0] for i in range(1, len(c)): prd[i + 1] = c[i] prd[i - 1] += c[i]*i return prd
def hermemul(c1, c2): """ Multiply one Hermite series by another.
Returns the product of two Hermite series `c1` * `c2`. The arguments are sequences of coefficients, from lowest order "term" to highest, e.g., [1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
Parameters ---------- c1, c2 : array_like 1-D arrays of Hermite series coefficients ordered from low to high.
Returns ------- out : ndarray Of Hermite series coefficients representing their product.
See Also -------- hermeadd, hermesub, hermemulx, hermediv, hermepow
Notes ----- In general, the (polynomial) product of two C-series results in terms that are not in the Hermite polynomial basis set. Thus, to express the product as a Hermite series, it is necessary to "reproject" the product onto said basis set, which may produce "unintuitive" (but correct) results; see Examples section below.
Examples -------- >>> from numpy.polynomial.hermite_e import hermemul >>> hermemul([1, 2, 3], [0, 1, 2]) array([14., 15., 28., 7., 6.])
""" # s1, s2 are trimmed copies [c1, c2] = pu.as_series([c1, c2])
if len(c1) > len(c2): c = c2 xs = c1 else: c = c1 xs = c2
if len(c) == 1: c0 = c[0]*xs c1 = 0 elif len(c) == 2: c0 = c[0]*xs c1 = c[1]*xs else: nd = len(c) c0 = c[-2]*xs c1 = c[-1]*xs for i in range(3, len(c) + 1): tmp = c0 nd = nd - 1 c0 = hermesub(c[-i]*xs, c1*(nd - 1)) c1 = hermeadd(tmp, hermemulx(c1)) return hermeadd(c0, hermemulx(c1))
def hermediv(c1, c2): """ Divide one Hermite series by another.
Returns the quotient-with-remainder of two Hermite series `c1` / `c2`. The arguments are sequences of coefficients from lowest order "term" to highest, e.g., [1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
Parameters ---------- c1, c2 : array_like 1-D arrays of Hermite series coefficients ordered from low to high.
Returns ------- [quo, rem] : ndarrays Of Hermite series coefficients representing the quotient and remainder.
See Also -------- hermeadd, hermesub, hermemulx, hermemul, hermepow
Notes ----- In general, the (polynomial) division of one Hermite series by another results in quotient and remainder terms that are not in the Hermite polynomial basis set. Thus, to express these results as a Hermite series, it is necessary to "reproject" the results onto the Hermite basis set, which may produce "unintuitive" (but correct) results; see Examples section below.
Examples -------- >>> from numpy.polynomial.hermite_e import hermediv >>> hermediv([ 14., 15., 28., 7., 6.], [0, 1, 2]) (array([1., 2., 3.]), array([0.])) >>> hermediv([ 15., 17., 28., 7., 6.], [0, 1, 2]) (array([1., 2., 3.]), array([1., 2.]))
""" return pu._div(hermemul, c1, c2)
def hermepow(c, pow, maxpower=16): """Raise a Hermite series to a power.
Returns the Hermite series `c` raised to the power `pow`. The argument `c` is a sequence of coefficients ordered from low to high. i.e., [1,2,3] is the series ``P_0 + 2*P_1 + 3*P_2.``
Parameters ---------- c : array_like 1-D array of Hermite series coefficients ordered from low to high. pow : integer Power to which the series will be raised maxpower : integer, optional Maximum power allowed. This is mainly to limit growth of the series to unmanageable size. Default is 16
Returns ------- coef : ndarray Hermite series of power.
See Also -------- hermeadd, hermesub, hermemulx, hermemul, hermediv
Examples -------- >>> from numpy.polynomial.hermite_e import hermepow >>> hermepow([1, 2, 3], 2) array([23., 28., 46., 12., 9.])
""" return pu._pow(hermemul, c, pow, maxpower)
def hermeder(c, m=1, scl=1, axis=0): """ Differentiate a Hermite_e series.
Returns the series coefficients `c` differentiated `m` times along `axis`. At each iteration the result is multiplied by `scl` (the scaling factor is for use in a linear change of variable). The argument `c` is an array of coefficients from low to high degree along each axis, e.g., [1,2,3] represents the series ``1*He_0 + 2*He_1 + 3*He_2`` while [[1,2],[1,2]] represents ``1*He_0(x)*He_0(y) + 1*He_1(x)*He_0(y) + 2*He_0(x)*He_1(y) + 2*He_1(x)*He_1(y)`` if axis=0 is ``x`` and axis=1 is ``y``.
Parameters ---------- c : array_like Array of Hermite_e series coefficients. If `c` is multidimensional the different axis correspond to different variables with the degree in each axis given by the corresponding index. m : int, optional Number of derivatives taken, must be non-negative. (Default: 1) scl : scalar, optional Each differentiation is multiplied by `scl`. The end result is multiplication by ``scl**m``. This is for use in a linear change of variable. (Default: 1) axis : int, optional Axis over which the derivative is taken. (Default: 0).
.. versionadded:: 1.7.0
Returns ------- der : ndarray Hermite series of the derivative.
See Also -------- hermeint
Notes ----- In general, the result of differentiating a Hermite series does not resemble the same operation on a power series. Thus the result of this function may be "unintuitive," albeit correct; see Examples section below.
Examples -------- >>> from numpy.polynomial.hermite_e import hermeder >>> hermeder([ 1., 1., 1., 1.]) array([1., 2., 3.]) >>> hermeder([-0.25, 1., 1./2., 1./3., 1./4 ], m=2) array([1., 2., 3.])
""" c = np.array(c, ndmin=1, copy=True) if c.dtype.char in '?bBhHiIlLqQpP': c = c.astype(np.double) cnt = pu._deprecate_as_int(m, "the order of derivation") iaxis = pu._deprecate_as_int(axis, "the axis") if cnt < 0: raise ValueError("The order of derivation must be non-negative") iaxis = normalize_axis_index(iaxis, c.ndim)
if cnt == 0: return c
c = np.moveaxis(c, iaxis, 0) n = len(c) if cnt >= n: return c[:1]*0 else: for i in range(cnt): n = n - 1 c *= scl der = np.empty((n,) + c.shape[1:], dtype=c.dtype) for j in range(n, 0, -1): der[j - 1] = j*c[j] c = der c = np.moveaxis(c, 0, iaxis) return c
def hermeint(c, m=1, k=[], lbnd=0, scl=1, axis=0): """ Integrate a Hermite_e series.
Returns the Hermite_e series coefficients `c` integrated `m` times from `lbnd` along `axis`. At each iteration the resulting series is **multiplied** by `scl` and an integration constant, `k`, is added. The scaling factor is for use in a linear change of variable. ("Buyer beware": note that, depending on what one is doing, one may want `scl` to be the reciprocal of what one might expect; for more information, see the Notes section below.) The argument `c` is an array of coefficients from low to high degree along each axis, e.g., [1,2,3] represents the series ``H_0 + 2*H_1 + 3*H_2`` while [[1,2],[1,2]] represents ``1*H_0(x)*H_0(y) + 1*H_1(x)*H_0(y) + 2*H_0(x)*H_1(y) + 2*H_1(x)*H_1(y)`` if axis=0 is ``x`` and axis=1 is ``y``.
Parameters ---------- c : array_like Array of Hermite_e series coefficients. If c is multidimensional the different axis correspond to different variables with the degree in each axis given by the corresponding index. m : int, optional Order of integration, must be positive. (Default: 1) k : {[], list, scalar}, optional Integration constant(s). The value of the first integral at ``lbnd`` is the first value in the list, the value of the second integral at ``lbnd`` is the second value, etc. If ``k == []`` (the default), all constants are set to zero. If ``m == 1``, a single scalar can be given instead of a list. lbnd : scalar, optional The lower bound of the integral. (Default: 0) scl : scalar, optional Following each integration the result is *multiplied* by `scl` before the integration constant is added. (Default: 1) axis : int, optional Axis over which the integral is taken. (Default: 0).
.. versionadded:: 1.7.0
Returns ------- S : ndarray Hermite_e series coefficients of the integral.
Raises ------ ValueError If ``m < 0``, ``len(k) > m``, ``np.ndim(lbnd) != 0``, or ``np.ndim(scl) != 0``.
See Also -------- hermeder
Notes ----- Note that the result of each integration is *multiplied* by `scl`. Why is this important to note? Say one is making a linear change of variable :math:`u = ax + b` in an integral relative to `x`. Then :math:`dx = du/a`, so one will need to set `scl` equal to :math:`1/a` - perhaps not what one would have first thought.
Also note that, in general, the result of integrating a C-series needs to be "reprojected" onto the C-series basis set. Thus, typically, the result of this function is "unintuitive," albeit correct; see Examples section below.
Examples -------- >>> from numpy.polynomial.hermite_e import hermeint >>> hermeint([1, 2, 3]) # integrate once, value 0 at 0. array([1., 1., 1., 1.]) >>> hermeint([1, 2, 3], m=2) # integrate twice, value & deriv 0 at 0 array([-0.25 , 1. , 0.5 , 0.33333333, 0.25 ]) # may vary >>> hermeint([1, 2, 3], k=1) # integrate once, value 1 at 0. array([2., 1., 1., 1.]) >>> hermeint([1, 2, 3], lbnd=-1) # integrate once, value 0 at -1 array([-1., 1., 1., 1.]) >>> hermeint([1, 2, 3], m=2, k=[1, 2], lbnd=-1) array([ 1.83333333, 0. , 0.5 , 0.33333333, 0.25 ]) # may vary
""" c = np.array(c, ndmin=1, copy=True) if c.dtype.char in '?bBhHiIlLqQpP': c = c.astype(np.double) if not np.iterable(k): k = [k] cnt = pu._deprecate_as_int(m, "the order of integration") iaxis = pu._deprecate_as_int(axis, "the axis") if cnt < 0: raise ValueError("The order of integration must be non-negative") if len(k) > cnt: raise ValueError("Too many integration constants") if np.ndim(lbnd) != 0: raise ValueError("lbnd must be a scalar.") if np.ndim(scl) != 0: raise ValueError("scl must be a scalar.") iaxis = normalize_axis_index(iaxis, c.ndim)
if cnt == 0: return c
c = np.moveaxis(c, iaxis, 0) k = list(k) + [0]*(cnt - len(k)) for i in range(cnt): n = len(c) c *= scl if n == 1 and np.all(c[0] == 0): c[0] += k[i] else: tmp = np.empty((n + 1,) + c.shape[1:], dtype=c.dtype) tmp[0] = c[0]*0 tmp[1] = c[0] for j in range(1, n): tmp[j + 1] = c[j]/(j + 1) tmp[0] += k[i] - hermeval(lbnd, tmp) c = tmp c = np.moveaxis(c, 0, iaxis) return c
def hermeval(x, c, tensor=True): """ Evaluate an HermiteE series at points x.
If `c` is of length `n + 1`, this function returns the value:
.. math:: p(x) = c_0 * He_0(x) + c_1 * He_1(x) + ... + c_n * He_n(x)
The parameter `x` is converted to an array only if it is a tuple or a list, otherwise it is treated as a scalar. In either case, either `x` or its elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` is a 1-D array, then `p(x)` will have the same shape as `x`. If `c` is multidimensional, then the shape of the result depends on the value of `tensor`. If `tensor` is true the shape will be c.shape[1:] + x.shape. If `tensor` is false the shape will be c.shape[1:]. Note that scalars have shape (,).
Trailing zeros in the coefficients will be used in the evaluation, so they should be avoided if efficiency is a concern.
Parameters ---------- x : array_like, compatible object If `x` is a list or tuple, it is converted to an ndarray, otherwise it is left unchanged and treated as a scalar. In either case, `x` or its elements must support addition and multiplication with with themselves and with the elements of `c`. c : array_like Array of coefficients ordered so that the coefficients for terms of degree n are contained in c[n]. If `c` is multidimensional the remaining indices enumerate multiple polynomials. In the two dimensional case the coefficients may be thought of as stored in the columns of `c`. tensor : boolean, optional If True, the shape of the coefficient array is extended with ones on the right, one for each dimension of `x`. Scalars have dimension 0 for this action. The result is that every column of coefficients in `c` is evaluated for every element of `x`. If False, `x` is broadcast over the columns of `c` for the evaluation. This keyword is useful when `c` is multidimensional. The default value is True.
.. versionadded:: 1.7.0
Returns ------- values : ndarray, algebra_like The shape of the return value is described above.
See Also -------- hermeval2d, hermegrid2d, hermeval3d, hermegrid3d
Notes ----- The evaluation uses Clenshaw recursion, aka synthetic division.
Examples -------- >>> from numpy.polynomial.hermite_e import hermeval >>> coef = [1,2,3] >>> hermeval(1, coef) 3.0 >>> hermeval([[1,2],[3,4]], coef) array([[ 3., 14.], [31., 54.]])
""" c = np.array(c, ndmin=1, copy=False) if c.dtype.char in '?bBhHiIlLqQpP': c = c.astype(np.double) if isinstance(x, (tuple, list)): x = np.asarray(x) if isinstance(x, np.ndarray) and tensor: c = c.reshape(c.shape + (1,)*x.ndim)
if len(c) == 1: c0 = c[0] c1 = 0 elif len(c) == 2: c0 = c[0] c1 = c[1] else: nd = len(c) c0 = c[-2] c1 = c[-1] for i in range(3, len(c) + 1): tmp = c0 nd = nd - 1 c0 = c[-i] - c1*(nd - 1) c1 = tmp + c1*x return c0 + c1*x
def hermeval2d(x, y, c): """ Evaluate a 2-D HermiteE series at points (x, y).
This function returns the values:
.. math:: p(x,y) = \\sum_{i,j} c_{i,j} * He_i(x) * He_j(y)
The parameters `x` and `y` are converted to arrays only if they are tuples or a lists, otherwise they are treated as a scalars and they must have the same shape after conversion. In either case, either `x` and `y` or their elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` is a 1-D array a one is implicitly appended to its shape to make it 2-D. The shape of the result will be c.shape[2:] + x.shape.
Parameters ---------- x, y : array_like, compatible objects The two dimensional series is evaluated at the points `(x, y)`, where `x` and `y` must have the same shape. If `x` or `y` is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and if it isn't an ndarray it is treated as a scalar. c : array_like Array of coefficients ordered so that the coefficient of the term of multi-degree i,j is contained in ``c[i,j]``. If `c` has dimension greater than two the remaining indices enumerate multiple sets of coefficients.
Returns ------- values : ndarray, compatible object The values of the two dimensional polynomial at points formed with pairs of corresponding values from `x` and `y`.
See Also -------- hermeval, hermegrid2d, hermeval3d, hermegrid3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._valnd(hermeval, c, x, y)
def hermegrid2d(x, y, c): """ Evaluate a 2-D HermiteE series on the Cartesian product of x and y.
This function returns the values:
.. math:: p(a,b) = \\sum_{i,j} c_{i,j} * H_i(a) * H_j(b)
where the points `(a, b)` consist of all pairs formed by taking `a` from `x` and `b` from `y`. The resulting points form a grid with `x` in the first dimension and `y` in the second.
The parameters `x` and `y` are converted to arrays only if they are tuples or a lists, otherwise they are treated as a scalars. In either case, either `x` and `y` or their elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` has fewer than two dimensions, ones are implicitly appended to its shape to make it 2-D. The shape of the result will be c.shape[2:] + x.shape.
Parameters ---------- x, y : array_like, compatible objects The two dimensional series is evaluated at the points in the Cartesian product of `x` and `y`. If `x` or `y` is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and, if it isn't an ndarray, it is treated as a scalar. c : array_like Array of coefficients ordered so that the coefficients for terms of degree i,j are contained in ``c[i,j]``. If `c` has dimension greater than two the remaining indices enumerate multiple sets of coefficients.
Returns ------- values : ndarray, compatible object The values of the two dimensional polynomial at points in the Cartesian product of `x` and `y`.
See Also -------- hermeval, hermeval2d, hermeval3d, hermegrid3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._gridnd(hermeval, c, x, y)
def hermeval3d(x, y, z, c): """ Evaluate a 3-D Hermite_e series at points (x, y, z).
This function returns the values:
.. math:: p(x,y,z) = \\sum_{i,j,k} c_{i,j,k} * He_i(x) * He_j(y) * He_k(z)
The parameters `x`, `y`, and `z` are converted to arrays only if they are tuples or a lists, otherwise they are treated as a scalars and they must have the same shape after conversion. In either case, either `x`, `y`, and `z` or their elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` has fewer than 3 dimensions, ones are implicitly appended to its shape to make it 3-D. The shape of the result will be c.shape[3:] + x.shape.
Parameters ---------- x, y, z : array_like, compatible object The three dimensional series is evaluated at the points `(x, y, z)`, where `x`, `y`, and `z` must have the same shape. If any of `x`, `y`, or `z` is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and if it isn't an ndarray it is treated as a scalar. c : array_like Array of coefficients ordered so that the coefficient of the term of multi-degree i,j,k is contained in ``c[i,j,k]``. If `c` has dimension greater than 3 the remaining indices enumerate multiple sets of coefficients.
Returns ------- values : ndarray, compatible object The values of the multidimensional polynomial on points formed with triples of corresponding values from `x`, `y`, and `z`.
See Also -------- hermeval, hermeval2d, hermegrid2d, hermegrid3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._valnd(hermeval, c, x, y, z)
def hermegrid3d(x, y, z, c): """ Evaluate a 3-D HermiteE series on the Cartesian product of x, y, and z.
This function returns the values:
.. math:: p(a,b,c) = \\sum_{i,j,k} c_{i,j,k} * He_i(a) * He_j(b) * He_k(c)
where the points `(a, b, c)` consist of all triples formed by taking `a` from `x`, `b` from `y`, and `c` from `z`. The resulting points form a grid with `x` in the first dimension, `y` in the second, and `z` in the third.
The parameters `x`, `y`, and `z` are converted to arrays only if they are tuples or a lists, otherwise they are treated as a scalars. In either case, either `x`, `y`, and `z` or their elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` has fewer than three dimensions, ones are implicitly appended to its shape to make it 3-D. The shape of the result will be c.shape[3:] + x.shape + y.shape + z.shape.
Parameters ---------- x, y, z : array_like, compatible objects The three dimensional series is evaluated at the points in the Cartesian product of `x`, `y`, and `z`. If `x`,`y`, or `z` is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and, if it isn't an ndarray, it is treated as a scalar. c : array_like Array of coefficients ordered so that the coefficients for terms of degree i,j are contained in ``c[i,j]``. If `c` has dimension greater than two the remaining indices enumerate multiple sets of coefficients.
Returns ------- values : ndarray, compatible object The values of the two dimensional polynomial at points in the Cartesian product of `x` and `y`.
See Also -------- hermeval, hermeval2d, hermegrid2d, hermeval3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._gridnd(hermeval, c, x, y, z)
def hermevander(x, deg): """Pseudo-Vandermonde matrix of given degree.
Returns the pseudo-Vandermonde matrix of degree `deg` and sample points `x`. The pseudo-Vandermonde matrix is defined by
.. math:: V[..., i] = He_i(x),
where `0 <= i <= deg`. The leading indices of `V` index the elements of `x` and the last index is the degree of the HermiteE polynomial.
If `c` is a 1-D array of coefficients of length `n + 1` and `V` is the array ``V = hermevander(x, n)``, then ``np.dot(V, c)`` and ``hermeval(x, c)`` are the same up to roundoff. This equivalence is useful both for least squares fitting and for the evaluation of a large number of HermiteE series of the same degree and sample points.
Parameters ---------- x : array_like Array of points. The dtype is converted to float64 or complex128 depending on whether any of the elements are complex. If `x` is scalar it is converted to a 1-D array. deg : int Degree of the resulting matrix.
Returns ------- vander : ndarray The pseudo-Vandermonde matrix. The shape of the returned matrix is ``x.shape + (deg + 1,)``, where The last index is the degree of the corresponding HermiteE polynomial. The dtype will be the same as the converted `x`.
Examples -------- >>> from numpy.polynomial.hermite_e import hermevander >>> x = np.array([-1, 0, 1]) >>> hermevander(x, 3) array([[ 1., -1., 0., 2.], [ 1., 0., -1., -0.], [ 1., 1., 0., -2.]])
""" ideg = pu._deprecate_as_int(deg, "deg") if ideg < 0: raise ValueError("deg must be non-negative")
x = np.array(x, copy=False, ndmin=1) + 0.0 dims = (ideg + 1,) + x.shape dtyp = x.dtype v = np.empty(dims, dtype=dtyp) v[0] = x*0 + 1 if ideg > 0: v[1] = x for i in range(2, ideg + 1): v[i] = (v[i-1]*x - v[i-2]*(i - 1)) return np.moveaxis(v, 0, -1)
def hermevander2d(x, y, deg): """Pseudo-Vandermonde matrix of given degrees.
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample points `(x, y)`. The pseudo-Vandermonde matrix is defined by
.. math:: V[..., (deg[1] + 1)*i + j] = He_i(x) * He_j(y),
where `0 <= i <= deg[0]` and `0 <= j <= deg[1]`. The leading indices of `V` index the points `(x, y)` and the last index encodes the degrees of the HermiteE polynomials.
If ``V = hermevander2d(x, y, [xdeg, ydeg])``, then the columns of `V` correspond to the elements of a 2-D coefficient array `c` of shape (xdeg + 1, ydeg + 1) in the order
.. math:: c_{00}, c_{01}, c_{02} ... , c_{10}, c_{11}, c_{12} ...
and ``np.dot(V, c.flat)`` and ``hermeval2d(x, y, c)`` will be the same up to roundoff. This equivalence is useful both for least squares fitting and for the evaluation of a large number of 2-D HermiteE series of the same degrees and sample points.
Parameters ---------- x, y : array_like Arrays of point coordinates, all of the same shape. The dtypes will be converted to either float64 or complex128 depending on whether any of the elements are complex. Scalars are converted to 1-D arrays. deg : list of ints List of maximum degrees of the form [x_deg, y_deg].
Returns ------- vander2d : ndarray The shape of the returned matrix is ``x.shape + (order,)``, where :math:`order = (deg[0]+1)*(deg[1]+1)`. The dtype will be the same as the converted `x` and `y`.
See Also -------- hermevander, hermevander3d, hermeval2d, hermeval3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._vander_nd_flat((hermevander, hermevander), (x, y), deg)
def hermevander3d(x, y, z, deg): """Pseudo-Vandermonde matrix of given degrees.
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample points `(x, y, z)`. If `l, m, n` are the given degrees in `x, y, z`, then Hehe pseudo-Vandermonde matrix is defined by
.. math:: V[..., (m+1)(n+1)i + (n+1)j + k] = He_i(x)*He_j(y)*He_k(z),
where `0 <= i <= l`, `0 <= j <= m`, and `0 <= j <= n`. The leading indices of `V` index the points `(x, y, z)` and the last index encodes the degrees of the HermiteE polynomials.
If ``V = hermevander3d(x, y, z, [xdeg, ydeg, zdeg])``, then the columns of `V` correspond to the elements of a 3-D coefficient array `c` of shape (xdeg + 1, ydeg + 1, zdeg + 1) in the order
.. math:: c_{000}, c_{001}, c_{002},... , c_{010}, c_{011}, c_{012},...
and ``np.dot(V, c.flat)`` and ``hermeval3d(x, y, z, c)`` will be the same up to roundoff. This equivalence is useful both for least squares fitting and for the evaluation of a large number of 3-D HermiteE series of the same degrees and sample points.
Parameters ---------- x, y, z : array_like Arrays of point coordinates, all of the same shape. The dtypes will be converted to either float64 or complex128 depending on whether any of the elements are complex. Scalars are converted to 1-D arrays. deg : list of ints List of maximum degrees of the form [x_deg, y_deg, z_deg].
Returns ------- vander3d : ndarray The shape of the returned matrix is ``x.shape + (order,)``, where :math:`order = (deg[0]+1)*(deg[1]+1)*(deg[2]+1)`. The dtype will be the same as the converted `x`, `y`, and `z`.
See Also -------- hermevander, hermevander3d, hermeval2d, hermeval3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._vander_nd_flat((hermevander, hermevander, hermevander), (x, y, z), deg)
def hermefit(x, y, deg, rcond=None, full=False, w=None): """ Least squares fit of Hermite series to data.
Return the coefficients of a HermiteE series of degree `deg` that is the least squares fit to the data values `y` given at points `x`. If `y` is 1-D the returned coefficients will also be 1-D. If `y` is 2-D multiple fits are done, one for each column of `y`, and the resulting coefficients are stored in the corresponding columns of a 2-D return. The fitted polynomial(s) are in the form
.. math:: p(x) = c_0 + c_1 * He_1(x) + ... + c_n * He_n(x),
where `n` is `deg`.
Parameters ---------- x : array_like, shape (M,) x-coordinates of the M sample points ``(x[i], y[i])``. y : array_like, shape (M,) or (M, K) y-coordinates of the sample points. Several data sets of sample points sharing the same x-coordinates can be fitted at once by passing in a 2D-array that contains one dataset per column. deg : int or 1-D array_like Degree(s) of the fitting polynomials. If `deg` is a single integer all terms up to and including the `deg`'th term are included in the fit. For NumPy versions >= 1.11.0 a list of integers specifying the degrees of the terms to include may be used instead. rcond : float, optional Relative condition number of the fit. Singular values smaller than this relative to the largest singular value will be ignored. The default value is len(x)*eps, where eps is the relative precision of the float type, about 2e-16 in most cases. full : bool, optional Switch determining nature of return value. When it is False (the default) just the coefficients are returned, when True diagnostic information from the singular value decomposition is also returned. w : array_like, shape (`M`,), optional Weights. If not None, the weight ``w[i]`` applies to the unsquared residual ``y[i] - y_hat[i]`` at ``x[i]``. Ideally the weights are chosen so that the errors of the products ``w[i]*y[i]`` all have the same variance. When using inverse-variance weighting, use ``w[i] = 1/sigma(y[i])``. The default value is None.
Returns ------- coef : ndarray, shape (M,) or (M, K) Hermite coefficients ordered from low to high. If `y` was 2-D, the coefficients for the data in column k of `y` are in column `k`.
[residuals, rank, singular_values, rcond] : list These values are only returned if ``full == True``
- residuals -- sum of squared residuals of the least squares fit - rank -- the numerical rank of the scaled Vandermonde matrix - singular_values -- singular values of the scaled Vandermonde matrix - rcond -- value of `rcond`.
For more details, see `numpy.linalg.lstsq`.
Warns ----- RankWarning The rank of the coefficient matrix in the least-squares fit is deficient. The warning is only raised if ``full = False``. The warnings can be turned off by
>>> import warnings >>> warnings.simplefilter('ignore', np.RankWarning)
See Also -------- numpy.polynomial.chebyshev.chebfit numpy.polynomial.legendre.legfit numpy.polynomial.polynomial.polyfit numpy.polynomial.hermite.hermfit numpy.polynomial.laguerre.lagfit hermeval : Evaluates a Hermite series. hermevander : pseudo Vandermonde matrix of Hermite series. hermeweight : HermiteE weight function. numpy.linalg.lstsq : Computes a least-squares fit from the matrix. scipy.interpolate.UnivariateSpline : Computes spline fits.
Notes ----- The solution is the coefficients of the HermiteE series `p` that minimizes the sum of the weighted squared errors
.. math:: E = \\sum_j w_j^2 * |y_j - p(x_j)|^2,
where the :math:`w_j` are the weights. This problem is solved by setting up the (typically) overdetermined matrix equation
.. math:: V(x) * c = w * y,
where `V` is the pseudo Vandermonde matrix of `x`, the elements of `c` are the coefficients to be solved for, and the elements of `y` are the observed values. This equation is then solved using the singular value decomposition of `V`.
If some of the singular values of `V` are so small that they are neglected, then a `RankWarning` will be issued. This means that the coefficient values may be poorly determined. Using a lower order fit will usually get rid of the warning. The `rcond` parameter can also be set to a value smaller than its default, but the resulting fit may be spurious and have large contributions from roundoff error.
Fits using HermiteE series are probably most useful when the data can be approximated by ``sqrt(w(x)) * p(x)``, where `w(x)` is the HermiteE weight. In that case the weight ``sqrt(w(x[i]))`` should be used together with data values ``y[i]/sqrt(w(x[i]))``. The weight function is available as `hermeweight`.
References ---------- .. [1] Wikipedia, "Curve fitting", https://en.wikipedia.org/wiki/Curve_fitting
Examples -------- >>> from numpy.polynomial.hermite_e import hermefit, hermeval >>> x = np.linspace(-10, 10) >>> np.random.seed(123) >>> err = np.random.randn(len(x))/10 >>> y = hermeval(x, [1, 2, 3]) + err >>> hermefit(x, y, 2) array([ 1.01690445, 1.99951418, 2.99948696]) # may vary
""" return pu._fit(hermevander, x, y, deg, rcond, full, w)
def hermecompanion(c): """ Return the scaled companion matrix of c.
The basis polynomials are scaled so that the companion matrix is symmetric when `c` is an HermiteE basis polynomial. This provides better eigenvalue estimates than the unscaled case and for basis polynomials the eigenvalues are guaranteed to be real if `numpy.linalg.eigvalsh` is used to obtain them.
Parameters ---------- c : array_like 1-D array of HermiteE series coefficients ordered from low to high degree.
Returns ------- mat : ndarray Scaled companion matrix of dimensions (deg, deg).
Notes -----
.. versionadded:: 1.7.0
""" # c is a trimmed copy [c] = pu.as_series([c]) if len(c) < 2: raise ValueError('Series must have maximum degree of at least 1.') if len(c) == 2: return np.array([[-c[0]/c[1]]])
n = len(c) - 1 mat = np.zeros((n, n), dtype=c.dtype) scl = np.hstack((1., 1./np.sqrt(np.arange(n - 1, 0, -1)))) scl = np.multiply.accumulate(scl)[::-1] top = mat.reshape(-1)[1::n+1] bot = mat.reshape(-1)[n::n+1] top[...] = np.sqrt(np.arange(1, n)) bot[...] = top mat[:, -1] -= scl*c[:-1]/c[-1] return mat
def hermeroots(c): """ Compute the roots of a HermiteE series.
Return the roots (a.k.a. "zeros") of the polynomial
.. math:: p(x) = \\sum_i c[i] * He_i(x).
Parameters ---------- c : 1-D array_like 1-D array of coefficients.
Returns ------- out : ndarray Array of the roots of the series. If all the roots are real, then `out` is also real, otherwise it is complex.
See Also -------- numpy.polynomial.polynomial.polyroots numpy.polynomial.legendre.legroots numpy.polynomial.laguerre.lagroots numpy.polynomial.hermite.hermroots numpy.polynomial.chebyshev.chebroots
Notes ----- The root estimates are obtained as the eigenvalues of the companion matrix, Roots far from the origin of the complex plane may have large errors due to the numerical instability of the series for such values. Roots with multiplicity greater than 1 will also show larger errors as the value of the series near such points is relatively insensitive to errors in the roots. Isolated roots near the origin can be improved by a few iterations of Newton's method.
The HermiteE series basis polynomials aren't powers of `x` so the results of this function may seem unintuitive.
Examples -------- >>> from numpy.polynomial.hermite_e import hermeroots, hermefromroots >>> coef = hermefromroots([-1, 0, 1]) >>> coef array([0., 2., 0., 1.]) >>> hermeroots(coef) array([-1., 0., 1.]) # may vary
""" # c is a trimmed copy [c] = pu.as_series([c]) if len(c) <= 1: return np.array([], dtype=c.dtype) if len(c) == 2: return np.array([-c[0]/c[1]])
# rotated companion matrix reduces error m = hermecompanion(c)[::-1,::-1] r = la.eigvals(m) r.sort() return r
def _normed_hermite_e_n(x, n): """ Evaluate a normalized HermiteE polynomial.
Compute the value of the normalized HermiteE polynomial of degree ``n`` at the points ``x``.
Parameters ---------- x : ndarray of double. Points at which to evaluate the function n : int Degree of the normalized HermiteE function to be evaluated.
Returns ------- values : ndarray The shape of the return value is described above.
Notes ----- .. versionadded:: 1.10.0
This function is needed for finding the Gauss points and integration weights for high degrees. The values of the standard HermiteE functions overflow when n >= 207.
""" if n == 0: return np.full(x.shape, 1/np.sqrt(np.sqrt(2*np.pi)))
c0 = 0. c1 = 1./np.sqrt(np.sqrt(2*np.pi)) nd = float(n) for i in range(n - 1): tmp = c0 c0 = -c1*np.sqrt((nd - 1.)/nd) c1 = tmp + c1*x*np.sqrt(1./nd) nd = nd - 1.0 return c0 + c1*x
def hermegauss(deg): """ Gauss-HermiteE quadrature.
Computes the sample points and weights for Gauss-HermiteE quadrature. These sample points and weights will correctly integrate polynomials of degree :math:`2*deg - 1` or less over the interval :math:`[-\\inf, \\inf]` with the weight function :math:`f(x) = \\exp(-x^2/2)`.
Parameters ---------- deg : int Number of sample points and weights. It must be >= 1.
Returns ------- x : ndarray 1-D ndarray containing the sample points. y : ndarray 1-D ndarray containing the weights.
Notes -----
.. versionadded:: 1.7.0
The results have only been tested up to degree 100, higher degrees may be problematic. The weights are determined by using the fact that
.. math:: w_k = c / (He'_n(x_k) * He_{n-1}(x_k))
where :math:`c` is a constant independent of :math:`k` and :math:`x_k` is the k'th root of :math:`He_n`, and then scaling the results to get the right value when integrating 1.
""" ideg = pu._deprecate_as_int(deg, "deg") if ideg <= 0: raise ValueError("deg must be a positive integer")
# first approximation of roots. We use the fact that the companion # matrix is symmetric in this case in order to obtain better zeros. c = np.array([0]*deg + [1]) m = hermecompanion(c) x = la.eigvalsh(m)
# improve roots by one application of Newton dy = _normed_hermite_e_n(x, ideg) df = _normed_hermite_e_n(x, ideg - 1) * np.sqrt(ideg) x -= dy/df
# compute the weights. We scale the factor to avoid possible numerical # overflow. fm = _normed_hermite_e_n(x, ideg - 1) fm /= np.abs(fm).max() w = 1/(fm * fm)
# for Hermite_e we can also symmetrize w = (w + w[::-1])/2 x = (x - x[::-1])/2
# scale w to get the right value w *= np.sqrt(2*np.pi) / w.sum()
return x, w
def hermeweight(x): """Weight function of the Hermite_e polynomials.
The weight function is :math:`\\exp(-x^2/2)` and the interval of integration is :math:`[-\\inf, \\inf]`. the HermiteE polynomials are orthogonal, but not normalized, with respect to this weight function.
Parameters ---------- x : array_like Values at which the weight function will be computed.
Returns ------- w : ndarray The weight function at `x`.
Notes -----
.. versionadded:: 1.7.0
""" w = np.exp(-.5*x**2) return w
## HermiteE series class#
class HermiteE(ABCPolyBase): """An HermiteE series class.
The HermiteE class provides the standard Python numerical methods '+', '-', '*', '//', '%', 'divmod', '**', and '()' as well as the attributes and methods listed in the `ABCPolyBase` documentation.
Parameters ---------- coef : array_like HermiteE coefficients in order of increasing degree, i.e, ``(1, 2, 3)`` gives ``1*He_0(x) + 2*He_1(X) + 3*He_2(x)``. domain : (2,) array_like, optional Domain to use. The interval ``[domain[0], domain[1]]`` is mapped to the interval ``[window[0], window[1]]`` by shifting and scaling. The default value is [-1, 1]. window : (2,) array_like, optional Window, see `domain` for its use. The default value is [-1, 1].
.. versionadded:: 1.6.0 symbol : str, optional Symbol used to represent the independent variable in string representations of the polynomial expression, e.g. for printing. The symbol must be a valid Python identifier. Default value is 'x'.
.. versionadded:: 1.24
""" # Virtual Functions _add = staticmethod(hermeadd) _sub = staticmethod(hermesub) _mul = staticmethod(hermemul) _div = staticmethod(hermediv) _pow = staticmethod(hermepow) _val = staticmethod(hermeval) _int = staticmethod(hermeint) _der = staticmethod(hermeder) _fit = staticmethod(hermefit) _line = staticmethod(hermeline) _roots = staticmethod(hermeroots) _fromroots = staticmethod(hermefromroots)
# Virtual properties domain = np.array(hermedomain) window = np.array(hermedomain) basis_name = 'He'12345678910111213141516171819202122232425262728293031323334353637383940414243444546from typing import Any
from numpy import ndarray, dtype, int_from numpy.polynomial._polybase import ABCPolyBasefrom numpy.polynomial.polyutils import trimcoef
__all__: list[str]
hermetrim = trimcoef
def poly2herme(pol): ...def herme2poly(c): ...
hermedomain: ndarray[Any, dtype[int_]]hermezero: ndarray[Any, dtype[int_]]hermeone: ndarray[Any, dtype[int_]]hermex: ndarray[Any, dtype[int_]]
def hermeline(off, scl): ...def hermefromroots(roots): ...def hermeadd(c1, c2): ...def hermesub(c1, c2): ...def hermemulx(c): ...def hermemul(c1, c2): ...def hermediv(c1, c2): ...def hermepow(c, pow, maxpower=...): ...def hermeder(c, m=..., scl=..., axis=...): ...def hermeint(c, m=..., k = ..., lbnd=..., scl=..., axis=...): ...def hermeval(x, c, tensor=...): ...def hermeval2d(x, y, c): ...def hermegrid2d(x, y, c): ...def hermeval3d(x, y, z, c): ...def hermegrid3d(x, y, z, c): ...def hermevander(x, deg): ...def hermevander2d(x, y, deg): ...def hermevander3d(x, y, z, deg): ...def hermefit(x, y, deg, rcond=..., full=..., w=...): ...def hermecompanion(c): ...def hermeroots(c): ...def hermegauss(deg): ...def hermeweight(x): ...
class HermiteE(ABCPolyBase): domain: Any window: Any basis_name: Any1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651"""==================================================Laguerre Series (:mod:`numpy.polynomial.laguerre`)==================================================
This module provides a number of objects (mostly functions) useful fordealing with Laguerre series, including a `Laguerre` class thatencapsulates the usual arithmetic operations. (General informationon how this module represents and works with such polynomials is in thedocstring for its "parent" sub-package, `numpy.polynomial`).
Classes-------.. autosummary:: :toctree: generated/
Laguerre
Constants---------.. autosummary:: :toctree: generated/
lagdomain lagzero lagone lagx
Arithmetic----------.. autosummary:: :toctree: generated/
lagadd lagsub lagmulx lagmul lagdiv lagpow lagval lagval2d lagval3d laggrid2d laggrid3d
Calculus--------.. autosummary:: :toctree: generated/
lagder lagint
Misc Functions--------------.. autosummary:: :toctree: generated/
lagfromroots lagroots lagvander lagvander2d lagvander3d laggauss lagweight lagcompanion lagfit lagtrim lagline lag2poly poly2lag
See also--------`numpy.polynomial`
"""import numpy as npimport numpy.linalg as lafrom numpy.core.multiarray import normalize_axis_index
from . import polyutils as pufrom ._polybase import ABCPolyBase
__all__ = [ 'lagzero', 'lagone', 'lagx', 'lagdomain', 'lagline', 'lagadd', 'lagsub', 'lagmulx', 'lagmul', 'lagdiv', 'lagpow', 'lagval', 'lagder', 'lagint', 'lag2poly', 'poly2lag', 'lagfromroots', 'lagvander', 'lagfit', 'lagtrim', 'lagroots', 'Laguerre', 'lagval2d', 'lagval3d', 'laggrid2d', 'laggrid3d', 'lagvander2d', 'lagvander3d', 'lagcompanion', 'laggauss', 'lagweight']
lagtrim = pu.trimcoef
def poly2lag(pol): """ poly2lag(pol)
Convert a polynomial to a Laguerre series.
Convert an array representing the coefficients of a polynomial (relative to the "standard" basis) ordered from lowest degree to highest, to an array of the coefficients of the equivalent Laguerre series, ordered from lowest to highest degree.
Parameters ---------- pol : array_like 1-D array containing the polynomial coefficients
Returns ------- c : ndarray 1-D array containing the coefficients of the equivalent Laguerre series.
See Also -------- lag2poly
Notes ----- The easy way to do conversions between polynomial basis sets is to use the convert method of a class instance.
Examples -------- >>> from numpy.polynomial.laguerre import poly2lag >>> poly2lag(np.arange(4)) array([ 23., -63., 58., -18.])
""" [pol] = pu.as_series([pol]) res = 0 for p in pol[::-1]: res = lagadd(lagmulx(res), p) return res
def lag2poly(c): """ Convert a Laguerre series to a polynomial.
Convert an array representing the coefficients of a Laguerre series, ordered from lowest degree to highest, to an array of the coefficients of the equivalent polynomial (relative to the "standard" basis) ordered from lowest to highest degree.
Parameters ---------- c : array_like 1-D array containing the Laguerre series coefficients, ordered from lowest order term to highest.
Returns ------- pol : ndarray 1-D array containing the coefficients of the equivalent polynomial (relative to the "standard" basis) ordered from lowest order term to highest.
See Also -------- poly2lag
Notes ----- The easy way to do conversions between polynomial basis sets is to use the convert method of a class instance.
Examples -------- >>> from numpy.polynomial.laguerre import lag2poly >>> lag2poly([ 23., -63., 58., -18.]) array([0., 1., 2., 3.])
""" from .polynomial import polyadd, polysub, polymulx
[c] = pu.as_series([c]) n = len(c) if n == 1: return c else: c0 = c[-2] c1 = c[-1] # i is the current degree of c1 for i in range(n - 1, 1, -1): tmp = c0 c0 = polysub(c[i - 2], (c1*(i - 1))/i) c1 = polyadd(tmp, polysub((2*i - 1)*c1, polymulx(c1))/i) return polyadd(c0, polysub(c1, polymulx(c1)))
## These are constant arrays are of integer type so as to be compatible# with the widest range of other types, such as Decimal.#
# Laguerrelagdomain = np.array([0, 1])
# Laguerre coefficients representing zero.lagzero = np.array([0])
# Laguerre coefficients representing one.lagone = np.array([1])
# Laguerre coefficients representing the identity x.lagx = np.array([1, -1])
def lagline(off, scl): """ Laguerre series whose graph is a straight line.
Parameters ---------- off, scl : scalars The specified line is given by ``off + scl*x``.
Returns ------- y : ndarray This module's representation of the Laguerre series for ``off + scl*x``.
See Also -------- numpy.polynomial.polynomial.polyline numpy.polynomial.chebyshev.chebline numpy.polynomial.legendre.legline numpy.polynomial.hermite.hermline numpy.polynomial.hermite_e.hermeline
Examples -------- >>> from numpy.polynomial.laguerre import lagline, lagval >>> lagval(0,lagline(3, 2)) 3.0 >>> lagval(1,lagline(3, 2)) 5.0
""" if scl != 0: return np.array([off + scl, -scl]) else: return np.array([off])
def lagfromroots(roots): """ Generate a Laguerre series with given roots.
The function returns the coefficients of the polynomial
.. math:: p(x) = (x - r_0) * (x - r_1) * ... * (x - r_n),
in Laguerre form, where the `r_n` are the roots specified in `roots`. If a zero has multiplicity n, then it must appear in `roots` n times. For instance, if 2 is a root of multiplicity three and 3 is a root of multiplicity 2, then `roots` looks something like [2, 2, 2, 3, 3]. The roots can appear in any order.
If the returned coefficients are `c`, then
.. math:: p(x) = c_0 + c_1 * L_1(x) + ... + c_n * L_n(x)
The coefficient of the last term is not generally 1 for monic polynomials in Laguerre form.
Parameters ---------- roots : array_like Sequence containing the roots.
Returns ------- out : ndarray 1-D array of coefficients. If all roots are real then `out` is a real array, if some of the roots are complex, then `out` is complex even if all the coefficients in the result are real (see Examples below).
See Also -------- numpy.polynomial.polynomial.polyfromroots numpy.polynomial.legendre.legfromroots numpy.polynomial.chebyshev.chebfromroots numpy.polynomial.hermite.hermfromroots numpy.polynomial.hermite_e.hermefromroots
Examples -------- >>> from numpy.polynomial.laguerre import lagfromroots, lagval >>> coef = lagfromroots((-1, 0, 1)) >>> lagval((-1, 0, 1), coef) array([0., 0., 0.]) >>> coef = lagfromroots((-1j, 1j)) >>> lagval((-1j, 1j), coef) array([0.+0.j, 0.+0.j])
""" return pu._fromroots(lagline, lagmul, roots)
def lagadd(c1, c2): """ Add one Laguerre series to another.
Returns the sum of two Laguerre series `c1` + `c2`. The arguments are sequences of coefficients ordered from lowest order term to highest, i.e., [1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
Parameters ---------- c1, c2 : array_like 1-D arrays of Laguerre series coefficients ordered from low to high.
Returns ------- out : ndarray Array representing the Laguerre series of their sum.
See Also -------- lagsub, lagmulx, lagmul, lagdiv, lagpow
Notes ----- Unlike multiplication, division, etc., the sum of two Laguerre series is a Laguerre series (without having to "reproject" the result onto the basis set) so addition, just like that of "standard" polynomials, is simply "component-wise."
Examples -------- >>> from numpy.polynomial.laguerre import lagadd >>> lagadd([1, 2, 3], [1, 2, 3, 4]) array([2., 4., 6., 4.])
""" return pu._add(c1, c2)
def lagsub(c1, c2): """ Subtract one Laguerre series from another.
Returns the difference of two Laguerre series `c1` - `c2`. The sequences of coefficients are from lowest order term to highest, i.e., [1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
Parameters ---------- c1, c2 : array_like 1-D arrays of Laguerre series coefficients ordered from low to high.
Returns ------- out : ndarray Of Laguerre series coefficients representing their difference.
See Also -------- lagadd, lagmulx, lagmul, lagdiv, lagpow
Notes ----- Unlike multiplication, division, etc., the difference of two Laguerre series is a Laguerre series (without having to "reproject" the result onto the basis set) so subtraction, just like that of "standard" polynomials, is simply "component-wise."
Examples -------- >>> from numpy.polynomial.laguerre import lagsub >>> lagsub([1, 2, 3, 4], [1, 2, 3]) array([0., 0., 0., 4.])
""" return pu._sub(c1, c2)
def lagmulx(c): """Multiply a Laguerre series by x.
Multiply the Laguerre series `c` by x, where x is the independent variable.
Parameters ---------- c : array_like 1-D array of Laguerre series coefficients ordered from low to high.
Returns ------- out : ndarray Array representing the result of the multiplication.
See Also -------- lagadd, lagsub, lagmul, lagdiv, lagpow
Notes ----- The multiplication uses the recursion relationship for Laguerre polynomials in the form
.. math::
xP_i(x) = (-(i + 1)*P_{i + 1}(x) + (2i + 1)P_{i}(x) - iP_{i - 1}(x))
Examples -------- >>> from numpy.polynomial.laguerre import lagmulx >>> lagmulx([1, 2, 3]) array([-1., -1., 11., -9.])
""" # c is a trimmed copy [c] = pu.as_series([c]) # The zero series needs special treatment if len(c) == 1 and c[0] == 0: return c
prd = np.empty(len(c) + 1, dtype=c.dtype) prd[0] = c[0] prd[1] = -c[0] for i in range(1, len(c)): prd[i + 1] = -c[i]*(i + 1) prd[i] += c[i]*(2*i + 1) prd[i - 1] -= c[i]*i return prd
def lagmul(c1, c2): """ Multiply one Laguerre series by another.
Returns the product of two Laguerre series `c1` * `c2`. The arguments are sequences of coefficients, from lowest order "term" to highest, e.g., [1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
Parameters ---------- c1, c2 : array_like 1-D arrays of Laguerre series coefficients ordered from low to high.
Returns ------- out : ndarray Of Laguerre series coefficients representing their product.
See Also -------- lagadd, lagsub, lagmulx, lagdiv, lagpow
Notes ----- In general, the (polynomial) product of two C-series results in terms that are not in the Laguerre polynomial basis set. Thus, to express the product as a Laguerre series, it is necessary to "reproject" the product onto said basis set, which may produce "unintuitive" (but correct) results; see Examples section below.
Examples -------- >>> from numpy.polynomial.laguerre import lagmul >>> lagmul([1, 2, 3], [0, 1, 2]) array([ 8., -13., 38., -51., 36.])
""" # s1, s2 are trimmed copies [c1, c2] = pu.as_series([c1, c2])
if len(c1) > len(c2): c = c2 xs = c1 else: c = c1 xs = c2
if len(c) == 1: c0 = c[0]*xs c1 = 0 elif len(c) == 2: c0 = c[0]*xs c1 = c[1]*xs else: nd = len(c) c0 = c[-2]*xs c1 = c[-1]*xs for i in range(3, len(c) + 1): tmp = c0 nd = nd - 1 c0 = lagsub(c[-i]*xs, (c1*(nd - 1))/nd) c1 = lagadd(tmp, lagsub((2*nd - 1)*c1, lagmulx(c1))/nd) return lagadd(c0, lagsub(c1, lagmulx(c1)))
def lagdiv(c1, c2): """ Divide one Laguerre series by another.
Returns the quotient-with-remainder of two Laguerre series `c1` / `c2`. The arguments are sequences of coefficients from lowest order "term" to highest, e.g., [1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
Parameters ---------- c1, c2 : array_like 1-D arrays of Laguerre series coefficients ordered from low to high.
Returns ------- [quo, rem] : ndarrays Of Laguerre series coefficients representing the quotient and remainder.
See Also -------- lagadd, lagsub, lagmulx, lagmul, lagpow
Notes ----- In general, the (polynomial) division of one Laguerre series by another results in quotient and remainder terms that are not in the Laguerre polynomial basis set. Thus, to express these results as a Laguerre series, it is necessary to "reproject" the results onto the Laguerre basis set, which may produce "unintuitive" (but correct) results; see Examples section below.
Examples -------- >>> from numpy.polynomial.laguerre import lagdiv >>> lagdiv([ 8., -13., 38., -51., 36.], [0, 1, 2]) (array([1., 2., 3.]), array([0.])) >>> lagdiv([ 9., -12., 38., -51., 36.], [0, 1, 2]) (array([1., 2., 3.]), array([1., 1.]))
""" return pu._div(lagmul, c1, c2)
def lagpow(c, pow, maxpower=16): """Raise a Laguerre series to a power.
Returns the Laguerre series `c` raised to the power `pow`. The argument `c` is a sequence of coefficients ordered from low to high. i.e., [1,2,3] is the series ``P_0 + 2*P_1 + 3*P_2.``
Parameters ---------- c : array_like 1-D array of Laguerre series coefficients ordered from low to high. pow : integer Power to which the series will be raised maxpower : integer, optional Maximum power allowed. This is mainly to limit growth of the series to unmanageable size. Default is 16
Returns ------- coef : ndarray Laguerre series of power.
See Also -------- lagadd, lagsub, lagmulx, lagmul, lagdiv
Examples -------- >>> from numpy.polynomial.laguerre import lagpow >>> lagpow([1, 2, 3], 2) array([ 14., -16., 56., -72., 54.])
""" return pu._pow(lagmul, c, pow, maxpower)
def lagder(c, m=1, scl=1, axis=0): """ Differentiate a Laguerre series.
Returns the Laguerre series coefficients `c` differentiated `m` times along `axis`. At each iteration the result is multiplied by `scl` (the scaling factor is for use in a linear change of variable). The argument `c` is an array of coefficients from low to high degree along each axis, e.g., [1,2,3] represents the series ``1*L_0 + 2*L_1 + 3*L_2`` while [[1,2],[1,2]] represents ``1*L_0(x)*L_0(y) + 1*L_1(x)*L_0(y) + 2*L_0(x)*L_1(y) + 2*L_1(x)*L_1(y)`` if axis=0 is ``x`` and axis=1 is ``y``.
Parameters ---------- c : array_like Array of Laguerre series coefficients. If `c` is multidimensional the different axis correspond to different variables with the degree in each axis given by the corresponding index. m : int, optional Number of derivatives taken, must be non-negative. (Default: 1) scl : scalar, optional Each differentiation is multiplied by `scl`. The end result is multiplication by ``scl**m``. This is for use in a linear change of variable. (Default: 1) axis : int, optional Axis over which the derivative is taken. (Default: 0).
.. versionadded:: 1.7.0
Returns ------- der : ndarray Laguerre series of the derivative.
See Also -------- lagint
Notes ----- In general, the result of differentiating a Laguerre series does not resemble the same operation on a power series. Thus the result of this function may be "unintuitive," albeit correct; see Examples section below.
Examples -------- >>> from numpy.polynomial.laguerre import lagder >>> lagder([ 1., 1., 1., -3.]) array([1., 2., 3.]) >>> lagder([ 1., 0., 0., -4., 3.], m=2) array([1., 2., 3.])
""" c = np.array(c, ndmin=1, copy=True) if c.dtype.char in '?bBhHiIlLqQpP': c = c.astype(np.double)
cnt = pu._deprecate_as_int(m, "the order of derivation") iaxis = pu._deprecate_as_int(axis, "the axis") if cnt < 0: raise ValueError("The order of derivation must be non-negative") iaxis = normalize_axis_index(iaxis, c.ndim)
if cnt == 0: return c
c = np.moveaxis(c, iaxis, 0) n = len(c) if cnt >= n: c = c[:1]*0 else: for i in range(cnt): n = n - 1 c *= scl der = np.empty((n,) + c.shape[1:], dtype=c.dtype) for j in range(n, 1, -1): der[j - 1] = -c[j] c[j - 1] += c[j] der[0] = -c[1] c = der c = np.moveaxis(c, 0, iaxis) return c
def lagint(c, m=1, k=[], lbnd=0, scl=1, axis=0): """ Integrate a Laguerre series.
Returns the Laguerre series coefficients `c` integrated `m` times from `lbnd` along `axis`. At each iteration the resulting series is **multiplied** by `scl` and an integration constant, `k`, is added. The scaling factor is for use in a linear change of variable. ("Buyer beware": note that, depending on what one is doing, one may want `scl` to be the reciprocal of what one might expect; for more information, see the Notes section below.) The argument `c` is an array of coefficients from low to high degree along each axis, e.g., [1,2,3] represents the series ``L_0 + 2*L_1 + 3*L_2`` while [[1,2],[1,2]] represents ``1*L_0(x)*L_0(y) + 1*L_1(x)*L_0(y) + 2*L_0(x)*L_1(y) + 2*L_1(x)*L_1(y)`` if axis=0 is ``x`` and axis=1 is ``y``.
Parameters ---------- c : array_like Array of Laguerre series coefficients. If `c` is multidimensional the different axis correspond to different variables with the degree in each axis given by the corresponding index. m : int, optional Order of integration, must be positive. (Default: 1) k : {[], list, scalar}, optional Integration constant(s). The value of the first integral at ``lbnd`` is the first value in the list, the value of the second integral at ``lbnd`` is the second value, etc. If ``k == []`` (the default), all constants are set to zero. If ``m == 1``, a single scalar can be given instead of a list. lbnd : scalar, optional The lower bound of the integral. (Default: 0) scl : scalar, optional Following each integration the result is *multiplied* by `scl` before the integration constant is added. (Default: 1) axis : int, optional Axis over which the integral is taken. (Default: 0).
.. versionadded:: 1.7.0
Returns ------- S : ndarray Laguerre series coefficients of the integral.
Raises ------ ValueError If ``m < 0``, ``len(k) > m``, ``np.ndim(lbnd) != 0``, or ``np.ndim(scl) != 0``.
See Also -------- lagder
Notes ----- Note that the result of each integration is *multiplied* by `scl`. Why is this important to note? Say one is making a linear change of variable :math:`u = ax + b` in an integral relative to `x`. Then :math:`dx = du/a`, so one will need to set `scl` equal to :math:`1/a` - perhaps not what one would have first thought.
Also note that, in general, the result of integrating a C-series needs to be "reprojected" onto the C-series basis set. Thus, typically, the result of this function is "unintuitive," albeit correct; see Examples section below.
Examples -------- >>> from numpy.polynomial.laguerre import lagint >>> lagint([1,2,3]) array([ 1., 1., 1., -3.]) >>> lagint([1,2,3], m=2) array([ 1., 0., 0., -4., 3.]) >>> lagint([1,2,3], k=1) array([ 2., 1., 1., -3.]) >>> lagint([1,2,3], lbnd=-1) array([11.5, 1. , 1. , -3. ]) >>> lagint([1,2], m=2, k=[1,2], lbnd=-1) array([ 11.16666667, -5. , -3. , 2. ]) # may vary
""" c = np.array(c, ndmin=1, copy=True) if c.dtype.char in '?bBhHiIlLqQpP': c = c.astype(np.double) if not np.iterable(k): k = [k] cnt = pu._deprecate_as_int(m, "the order of integration") iaxis = pu._deprecate_as_int(axis, "the axis") if cnt < 0: raise ValueError("The order of integration must be non-negative") if len(k) > cnt: raise ValueError("Too many integration constants") if np.ndim(lbnd) != 0: raise ValueError("lbnd must be a scalar.") if np.ndim(scl) != 0: raise ValueError("scl must be a scalar.") iaxis = normalize_axis_index(iaxis, c.ndim)
if cnt == 0: return c
c = np.moveaxis(c, iaxis, 0) k = list(k) + [0]*(cnt - len(k)) for i in range(cnt): n = len(c) c *= scl if n == 1 and np.all(c[0] == 0): c[0] += k[i] else: tmp = np.empty((n + 1,) + c.shape[1:], dtype=c.dtype) tmp[0] = c[0] tmp[1] = -c[0] for j in range(1, n): tmp[j] += c[j] tmp[j + 1] = -c[j] tmp[0] += k[i] - lagval(lbnd, tmp) c = tmp c = np.moveaxis(c, 0, iaxis) return c
def lagval(x, c, tensor=True): """ Evaluate a Laguerre series at points x.
If `c` is of length `n + 1`, this function returns the value:
.. math:: p(x) = c_0 * L_0(x) + c_1 * L_1(x) + ... + c_n * L_n(x)
The parameter `x` is converted to an array only if it is a tuple or a list, otherwise it is treated as a scalar. In either case, either `x` or its elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` is a 1-D array, then `p(x)` will have the same shape as `x`. If `c` is multidimensional, then the shape of the result depends on the value of `tensor`. If `tensor` is true the shape will be c.shape[1:] + x.shape. If `tensor` is false the shape will be c.shape[1:]. Note that scalars have shape (,).
Trailing zeros in the coefficients will be used in the evaluation, so they should be avoided if efficiency is a concern.
Parameters ---------- x : array_like, compatible object If `x` is a list or tuple, it is converted to an ndarray, otherwise it is left unchanged and treated as a scalar. In either case, `x` or its elements must support addition and multiplication with themselves and with the elements of `c`. c : array_like Array of coefficients ordered so that the coefficients for terms of degree n are contained in c[n]. If `c` is multidimensional the remaining indices enumerate multiple polynomials. In the two dimensional case the coefficients may be thought of as stored in the columns of `c`. tensor : boolean, optional If True, the shape of the coefficient array is extended with ones on the right, one for each dimension of `x`. Scalars have dimension 0 for this action. The result is that every column of coefficients in `c` is evaluated for every element of `x`. If False, `x` is broadcast over the columns of `c` for the evaluation. This keyword is useful when `c` is multidimensional. The default value is True.
.. versionadded:: 1.7.0
Returns ------- values : ndarray, algebra_like The shape of the return value is described above.
See Also -------- lagval2d, laggrid2d, lagval3d, laggrid3d
Notes ----- The evaluation uses Clenshaw recursion, aka synthetic division.
Examples -------- >>> from numpy.polynomial.laguerre import lagval >>> coef = [1,2,3] >>> lagval(1, coef) -0.5 >>> lagval([[1,2],[3,4]], coef) array([[-0.5, -4. ], [-4.5, -2. ]])
""" c = np.array(c, ndmin=1, copy=False) if c.dtype.char in '?bBhHiIlLqQpP': c = c.astype(np.double) if isinstance(x, (tuple, list)): x = np.asarray(x) if isinstance(x, np.ndarray) and tensor: c = c.reshape(c.shape + (1,)*x.ndim)
if len(c) == 1: c0 = c[0] c1 = 0 elif len(c) == 2: c0 = c[0] c1 = c[1] else: nd = len(c) c0 = c[-2] c1 = c[-1] for i in range(3, len(c) + 1): tmp = c0 nd = nd - 1 c0 = c[-i] - (c1*(nd - 1))/nd c1 = tmp + (c1*((2*nd - 1) - x))/nd return c0 + c1*(1 - x)
def lagval2d(x, y, c): """ Evaluate a 2-D Laguerre series at points (x, y).
This function returns the values:
.. math:: p(x,y) = \\sum_{i,j} c_{i,j} * L_i(x) * L_j(y)
The parameters `x` and `y` are converted to arrays only if they are tuples or a lists, otherwise they are treated as a scalars and they must have the same shape after conversion. In either case, either `x` and `y` or their elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` is a 1-D array a one is implicitly appended to its shape to make it 2-D. The shape of the result will be c.shape[2:] + x.shape.
Parameters ---------- x, y : array_like, compatible objects The two dimensional series is evaluated at the points `(x, y)`, where `x` and `y` must have the same shape. If `x` or `y` is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and if it isn't an ndarray it is treated as a scalar. c : array_like Array of coefficients ordered so that the coefficient of the term of multi-degree i,j is contained in ``c[i,j]``. If `c` has dimension greater than two the remaining indices enumerate multiple sets of coefficients.
Returns ------- values : ndarray, compatible object The values of the two dimensional polynomial at points formed with pairs of corresponding values from `x` and `y`.
See Also -------- lagval, laggrid2d, lagval3d, laggrid3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._valnd(lagval, c, x, y)
def laggrid2d(x, y, c): """ Evaluate a 2-D Laguerre series on the Cartesian product of x and y.
This function returns the values:
.. math:: p(a,b) = \\sum_{i,j} c_{i,j} * L_i(a) * L_j(b)
where the points `(a, b)` consist of all pairs formed by taking `a` from `x` and `b` from `y`. The resulting points form a grid with `x` in the first dimension and `y` in the second.
The parameters `x` and `y` are converted to arrays only if they are tuples or a lists, otherwise they are treated as a scalars. In either case, either `x` and `y` or their elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` has fewer than two dimensions, ones are implicitly appended to its shape to make it 2-D. The shape of the result will be c.shape[2:] + x.shape + y.shape.
Parameters ---------- x, y : array_like, compatible objects The two dimensional series is evaluated at the points in the Cartesian product of `x` and `y`. If `x` or `y` is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and, if it isn't an ndarray, it is treated as a scalar. c : array_like Array of coefficients ordered so that the coefficient of the term of multi-degree i,j is contained in `c[i,j]`. If `c` has dimension greater than two the remaining indices enumerate multiple sets of coefficients.
Returns ------- values : ndarray, compatible object The values of the two dimensional Chebyshev series at points in the Cartesian product of `x` and `y`.
See Also -------- lagval, lagval2d, lagval3d, laggrid3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._gridnd(lagval, c, x, y)
def lagval3d(x, y, z, c): """ Evaluate a 3-D Laguerre series at points (x, y, z).
This function returns the values:
.. math:: p(x,y,z) = \\sum_{i,j,k} c_{i,j,k} * L_i(x) * L_j(y) * L_k(z)
The parameters `x`, `y`, and `z` are converted to arrays only if they are tuples or a lists, otherwise they are treated as a scalars and they must have the same shape after conversion. In either case, either `x`, `y`, and `z` or their elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` has fewer than 3 dimensions, ones are implicitly appended to its shape to make it 3-D. The shape of the result will be c.shape[3:] + x.shape.
Parameters ---------- x, y, z : array_like, compatible object The three dimensional series is evaluated at the points `(x, y, z)`, where `x`, `y`, and `z` must have the same shape. If any of `x`, `y`, or `z` is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and if it isn't an ndarray it is treated as a scalar. c : array_like Array of coefficients ordered so that the coefficient of the term of multi-degree i,j,k is contained in ``c[i,j,k]``. If `c` has dimension greater than 3 the remaining indices enumerate multiple sets of coefficients.
Returns ------- values : ndarray, compatible object The values of the multidimensional polynomial on points formed with triples of corresponding values from `x`, `y`, and `z`.
See Also -------- lagval, lagval2d, laggrid2d, laggrid3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._valnd(lagval, c, x, y, z)
def laggrid3d(x, y, z, c): """ Evaluate a 3-D Laguerre series on the Cartesian product of x, y, and z.
This function returns the values:
.. math:: p(a,b,c) = \\sum_{i,j,k} c_{i,j,k} * L_i(a) * L_j(b) * L_k(c)
where the points `(a, b, c)` consist of all triples formed by taking `a` from `x`, `b` from `y`, and `c` from `z`. The resulting points form a grid with `x` in the first dimension, `y` in the second, and `z` in the third.
The parameters `x`, `y`, and `z` are converted to arrays only if they are tuples or a lists, otherwise they are treated as a scalars. In either case, either `x`, `y`, and `z` or their elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` has fewer than three dimensions, ones are implicitly appended to its shape to make it 3-D. The shape of the result will be c.shape[3:] + x.shape + y.shape + z.shape.
Parameters ---------- x, y, z : array_like, compatible objects The three dimensional series is evaluated at the points in the Cartesian product of `x`, `y`, and `z`. If `x`,`y`, or `z` is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and, if it isn't an ndarray, it is treated as a scalar. c : array_like Array of coefficients ordered so that the coefficients for terms of degree i,j are contained in ``c[i,j]``. If `c` has dimension greater than two the remaining indices enumerate multiple sets of coefficients.
Returns ------- values : ndarray, compatible object The values of the two dimensional polynomial at points in the Cartesian product of `x` and `y`.
See Also -------- lagval, lagval2d, laggrid2d, lagval3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._gridnd(lagval, c, x, y, z)
def lagvander(x, deg): """Pseudo-Vandermonde matrix of given degree.
Returns the pseudo-Vandermonde matrix of degree `deg` and sample points `x`. The pseudo-Vandermonde matrix is defined by
.. math:: V[..., i] = L_i(x)
where `0 <= i <= deg`. The leading indices of `V` index the elements of `x` and the last index is the degree of the Laguerre polynomial.
If `c` is a 1-D array of coefficients of length `n + 1` and `V` is the array ``V = lagvander(x, n)``, then ``np.dot(V, c)`` and ``lagval(x, c)`` are the same up to roundoff. This equivalence is useful both for least squares fitting and for the evaluation of a large number of Laguerre series of the same degree and sample points.
Parameters ---------- x : array_like Array of points. The dtype is converted to float64 or complex128 depending on whether any of the elements are complex. If `x` is scalar it is converted to a 1-D array. deg : int Degree of the resulting matrix.
Returns ------- vander : ndarray The pseudo-Vandermonde matrix. The shape of the returned matrix is ``x.shape + (deg + 1,)``, where The last index is the degree of the corresponding Laguerre polynomial. The dtype will be the same as the converted `x`.
Examples -------- >>> from numpy.polynomial.laguerre import lagvander >>> x = np.array([0, 1, 2]) >>> lagvander(x, 3) array([[ 1. , 1. , 1. , 1. ], [ 1. , 0. , -0.5 , -0.66666667], [ 1. , -1. , -1. , -0.33333333]])
""" ideg = pu._deprecate_as_int(deg, "deg") if ideg < 0: raise ValueError("deg must be non-negative")
x = np.array(x, copy=False, ndmin=1) + 0.0 dims = (ideg + 1,) + x.shape dtyp = x.dtype v = np.empty(dims, dtype=dtyp) v[0] = x*0 + 1 if ideg > 0: v[1] = 1 - x for i in range(2, ideg + 1): v[i] = (v[i-1]*(2*i - 1 - x) - v[i-2]*(i - 1))/i return np.moveaxis(v, 0, -1)
def lagvander2d(x, y, deg): """Pseudo-Vandermonde matrix of given degrees.
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample points `(x, y)`. The pseudo-Vandermonde matrix is defined by
.. math:: V[..., (deg[1] + 1)*i + j] = L_i(x) * L_j(y),
where `0 <= i <= deg[0]` and `0 <= j <= deg[1]`. The leading indices of `V` index the points `(x, y)` and the last index encodes the degrees of the Laguerre polynomials.
If ``V = lagvander2d(x, y, [xdeg, ydeg])``, then the columns of `V` correspond to the elements of a 2-D coefficient array `c` of shape (xdeg + 1, ydeg + 1) in the order
.. math:: c_{00}, c_{01}, c_{02} ... , c_{10}, c_{11}, c_{12} ...
and ``np.dot(V, c.flat)`` and ``lagval2d(x, y, c)`` will be the same up to roundoff. This equivalence is useful both for least squares fitting and for the evaluation of a large number of 2-D Laguerre series of the same degrees and sample points.
Parameters ---------- x, y : array_like Arrays of point coordinates, all of the same shape. The dtypes will be converted to either float64 or complex128 depending on whether any of the elements are complex. Scalars are converted to 1-D arrays. deg : list of ints List of maximum degrees of the form [x_deg, y_deg].
Returns ------- vander2d : ndarray The shape of the returned matrix is ``x.shape + (order,)``, where :math:`order = (deg[0]+1)*(deg[1]+1)`. The dtype will be the same as the converted `x` and `y`.
See Also -------- lagvander, lagvander3d, lagval2d, lagval3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._vander_nd_flat((lagvander, lagvander), (x, y), deg)
def lagvander3d(x, y, z, deg): """Pseudo-Vandermonde matrix of given degrees.
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample points `(x, y, z)`. If `l, m, n` are the given degrees in `x, y, z`, then The pseudo-Vandermonde matrix is defined by
.. math:: V[..., (m+1)(n+1)i + (n+1)j + k] = L_i(x)*L_j(y)*L_k(z),
where `0 <= i <= l`, `0 <= j <= m`, and `0 <= j <= n`. The leading indices of `V` index the points `(x, y, z)` and the last index encodes the degrees of the Laguerre polynomials.
If ``V = lagvander3d(x, y, z, [xdeg, ydeg, zdeg])``, then the columns of `V` correspond to the elements of a 3-D coefficient array `c` of shape (xdeg + 1, ydeg + 1, zdeg + 1) in the order
.. math:: c_{000}, c_{001}, c_{002},... , c_{010}, c_{011}, c_{012},...
and ``np.dot(V, c.flat)`` and ``lagval3d(x, y, z, c)`` will be the same up to roundoff. This equivalence is useful both for least squares fitting and for the evaluation of a large number of 3-D Laguerre series of the same degrees and sample points.
Parameters ---------- x, y, z : array_like Arrays of point coordinates, all of the same shape. The dtypes will be converted to either float64 or complex128 depending on whether any of the elements are complex. Scalars are converted to 1-D arrays. deg : list of ints List of maximum degrees of the form [x_deg, y_deg, z_deg].
Returns ------- vander3d : ndarray The shape of the returned matrix is ``x.shape + (order,)``, where :math:`order = (deg[0]+1)*(deg[1]+1)*(deg[2]+1)`. The dtype will be the same as the converted `x`, `y`, and `z`.
See Also -------- lagvander, lagvander3d, lagval2d, lagval3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._vander_nd_flat((lagvander, lagvander, lagvander), (x, y, z), deg)
def lagfit(x, y, deg, rcond=None, full=False, w=None): """ Least squares fit of Laguerre series to data.
Return the coefficients of a Laguerre series of degree `deg` that is the least squares fit to the data values `y` given at points `x`. If `y` is 1-D the returned coefficients will also be 1-D. If `y` is 2-D multiple fits are done, one for each column of `y`, and the resulting coefficients are stored in the corresponding columns of a 2-D return. The fitted polynomial(s) are in the form
.. math:: p(x) = c_0 + c_1 * L_1(x) + ... + c_n * L_n(x),
where ``n`` is `deg`.
Parameters ---------- x : array_like, shape (M,) x-coordinates of the M sample points ``(x[i], y[i])``. y : array_like, shape (M,) or (M, K) y-coordinates of the sample points. Several data sets of sample points sharing the same x-coordinates can be fitted at once by passing in a 2D-array that contains one dataset per column. deg : int or 1-D array_like Degree(s) of the fitting polynomials. If `deg` is a single integer all terms up to and including the `deg`'th term are included in the fit. For NumPy versions >= 1.11.0 a list of integers specifying the degrees of the terms to include may be used instead. rcond : float, optional Relative condition number of the fit. Singular values smaller than this relative to the largest singular value will be ignored. The default value is len(x)*eps, where eps is the relative precision of the float type, about 2e-16 in most cases. full : bool, optional Switch determining nature of return value. When it is False (the default) just the coefficients are returned, when True diagnostic information from the singular value decomposition is also returned. w : array_like, shape (`M`,), optional Weights. If not None, the weight ``w[i]`` applies to the unsquared residual ``y[i] - y_hat[i]`` at ``x[i]``. Ideally the weights are chosen so that the errors of the products ``w[i]*y[i]`` all have the same variance. When using inverse-variance weighting, use ``w[i] = 1/sigma(y[i])``. The default value is None.
Returns ------- coef : ndarray, shape (M,) or (M, K) Laguerre coefficients ordered from low to high. If `y` was 2-D, the coefficients for the data in column *k* of `y` are in column *k*.
[residuals, rank, singular_values, rcond] : list These values are only returned if ``full == True``
- residuals -- sum of squared residuals of the least squares fit - rank -- the numerical rank of the scaled Vandermonde matrix - singular_values -- singular values of the scaled Vandermonde matrix - rcond -- value of `rcond`.
For more details, see `numpy.linalg.lstsq`.
Warns ----- RankWarning The rank of the coefficient matrix in the least-squares fit is deficient. The warning is only raised if ``full == False``. The warnings can be turned off by
>>> import warnings >>> warnings.simplefilter('ignore', np.RankWarning)
See Also -------- numpy.polynomial.polynomial.polyfit numpy.polynomial.legendre.legfit numpy.polynomial.chebyshev.chebfit numpy.polynomial.hermite.hermfit numpy.polynomial.hermite_e.hermefit lagval : Evaluates a Laguerre series. lagvander : pseudo Vandermonde matrix of Laguerre series. lagweight : Laguerre weight function. numpy.linalg.lstsq : Computes a least-squares fit from the matrix. scipy.interpolate.UnivariateSpline : Computes spline fits.
Notes ----- The solution is the coefficients of the Laguerre series ``p`` that minimizes the sum of the weighted squared errors
.. math:: E = \\sum_j w_j^2 * |y_j - p(x_j)|^2,
where the :math:`w_j` are the weights. This problem is solved by setting up as the (typically) overdetermined matrix equation
.. math:: V(x) * c = w * y,
where ``V`` is the weighted pseudo Vandermonde matrix of `x`, ``c`` are the coefficients to be solved for, `w` are the weights, and `y` are the observed values. This equation is then solved using the singular value decomposition of ``V``.
If some of the singular values of `V` are so small that they are neglected, then a `RankWarning` will be issued. This means that the coefficient values may be poorly determined. Using a lower order fit will usually get rid of the warning. The `rcond` parameter can also be set to a value smaller than its default, but the resulting fit may be spurious and have large contributions from roundoff error.
Fits using Laguerre series are probably most useful when the data can be approximated by ``sqrt(w(x)) * p(x)``, where ``w(x)`` is the Laguerre weight. In that case the weight ``sqrt(w(x[i]))`` should be used together with data values ``y[i]/sqrt(w(x[i]))``. The weight function is available as `lagweight`.
References ---------- .. [1] Wikipedia, "Curve fitting", https://en.wikipedia.org/wiki/Curve_fitting
Examples -------- >>> from numpy.polynomial.laguerre import lagfit, lagval >>> x = np.linspace(0, 10) >>> err = np.random.randn(len(x))/10 >>> y = lagval(x, [1, 2, 3]) + err >>> lagfit(x, y, 2) array([ 0.96971004, 2.00193749, 3.00288744]) # may vary
""" return pu._fit(lagvander, x, y, deg, rcond, full, w)
def lagcompanion(c): """ Return the companion matrix of c.
The usual companion matrix of the Laguerre polynomials is already symmetric when `c` is a basis Laguerre polynomial, so no scaling is applied.
Parameters ---------- c : array_like 1-D array of Laguerre series coefficients ordered from low to high degree.
Returns ------- mat : ndarray Companion matrix of dimensions (deg, deg).
Notes -----
.. versionadded:: 1.7.0
""" # c is a trimmed copy [c] = pu.as_series([c]) if len(c) < 2: raise ValueError('Series must have maximum degree of at least 1.') if len(c) == 2: return np.array([[1 + c[0]/c[1]]])
n = len(c) - 1 mat = np.zeros((n, n), dtype=c.dtype) top = mat.reshape(-1)[1::n+1] mid = mat.reshape(-1)[0::n+1] bot = mat.reshape(-1)[n::n+1] top[...] = -np.arange(1, n) mid[...] = 2.*np.arange(n) + 1. bot[...] = top mat[:, -1] += (c[:-1]/c[-1])*n return mat
def lagroots(c): """ Compute the roots of a Laguerre series.
Return the roots (a.k.a. "zeros") of the polynomial
.. math:: p(x) = \\sum_i c[i] * L_i(x).
Parameters ---------- c : 1-D array_like 1-D array of coefficients.
Returns ------- out : ndarray Array of the roots of the series. If all the roots are real, then `out` is also real, otherwise it is complex.
See Also -------- numpy.polynomial.polynomial.polyroots numpy.polynomial.legendre.legroots numpy.polynomial.chebyshev.chebroots numpy.polynomial.hermite.hermroots numpy.polynomial.hermite_e.hermeroots
Notes ----- The root estimates are obtained as the eigenvalues of the companion matrix, Roots far from the origin of the complex plane may have large errors due to the numerical instability of the series for such values. Roots with multiplicity greater than 1 will also show larger errors as the value of the series near such points is relatively insensitive to errors in the roots. Isolated roots near the origin can be improved by a few iterations of Newton's method.
The Laguerre series basis polynomials aren't powers of `x` so the results of this function may seem unintuitive.
Examples -------- >>> from numpy.polynomial.laguerre import lagroots, lagfromroots >>> coef = lagfromroots([0, 1, 2]) >>> coef array([ 2., -8., 12., -6.]) >>> lagroots(coef) array([-4.4408921e-16, 1.0000000e+00, 2.0000000e+00])
""" # c is a trimmed copy [c] = pu.as_series([c]) if len(c) <= 1: return np.array([], dtype=c.dtype) if len(c) == 2: return np.array([1 + c[0]/c[1]])
# rotated companion matrix reduces error m = lagcompanion(c)[::-1,::-1] r = la.eigvals(m) r.sort() return r
def laggauss(deg): """ Gauss-Laguerre quadrature.
Computes the sample points and weights for Gauss-Laguerre quadrature. These sample points and weights will correctly integrate polynomials of degree :math:`2*deg - 1` or less over the interval :math:`[0, \\inf]` with the weight function :math:`f(x) = \\exp(-x)`.
Parameters ---------- deg : int Number of sample points and weights. It must be >= 1.
Returns ------- x : ndarray 1-D ndarray containing the sample points. y : ndarray 1-D ndarray containing the weights.
Notes -----
.. versionadded:: 1.7.0
The results have only been tested up to degree 100 higher degrees may be problematic. The weights are determined by using the fact that
.. math:: w_k = c / (L'_n(x_k) * L_{n-1}(x_k))
where :math:`c` is a constant independent of :math:`k` and :math:`x_k` is the k'th root of :math:`L_n`, and then scaling the results to get the right value when integrating 1.
""" ideg = pu._deprecate_as_int(deg, "deg") if ideg <= 0: raise ValueError("deg must be a positive integer")
# first approximation of roots. We use the fact that the companion # matrix is symmetric in this case in order to obtain better zeros. c = np.array([0]*deg + [1]) m = lagcompanion(c) x = la.eigvalsh(m)
# improve roots by one application of Newton dy = lagval(x, c) df = lagval(x, lagder(c)) x -= dy/df
# compute the weights. We scale the factor to avoid possible numerical # overflow. fm = lagval(x, c[1:]) fm /= np.abs(fm).max() df /= np.abs(df).max() w = 1/(fm * df)
# scale w to get the right value, 1 in this case w /= w.sum()
return x, w
def lagweight(x): """Weight function of the Laguerre polynomials.
The weight function is :math:`exp(-x)` and the interval of integration is :math:`[0, \\inf]`. The Laguerre polynomials are orthogonal, but not normalized, with respect to this weight function.
Parameters ---------- x : array_like Values at which the weight function will be computed.
Returns ------- w : ndarray The weight function at `x`.
Notes -----
.. versionadded:: 1.7.0
""" w = np.exp(-x) return w
## Laguerre series class#
class Laguerre(ABCPolyBase): """A Laguerre series class.
The Laguerre class provides the standard Python numerical methods '+', '-', '*', '//', '%', 'divmod', '**', and '()' as well as the attributes and methods listed in the `ABCPolyBase` documentation.
Parameters ---------- coef : array_like Laguerre coefficients in order of increasing degree, i.e, ``(1, 2, 3)`` gives ``1*L_0(x) + 2*L_1(X) + 3*L_2(x)``. domain : (2,) array_like, optional Domain to use. The interval ``[domain[0], domain[1]]`` is mapped to the interval ``[window[0], window[1]]`` by shifting and scaling. The default value is [0, 1]. window : (2,) array_like, optional Window, see `domain` for its use. The default value is [0, 1].
.. versionadded:: 1.6.0 symbol : str, optional Symbol used to represent the independent variable in string representations of the polynomial expression, e.g. for printing. The symbol must be a valid Python identifier. Default value is 'x'.
.. versionadded:: 1.24
""" # Virtual Functions _add = staticmethod(lagadd) _sub = staticmethod(lagsub) _mul = staticmethod(lagmul) _div = staticmethod(lagdiv) _pow = staticmethod(lagpow) _val = staticmethod(lagval) _int = staticmethod(lagint) _der = staticmethod(lagder) _fit = staticmethod(lagfit) _line = staticmethod(lagline) _roots = staticmethod(lagroots) _fromroots = staticmethod(lagfromroots)
# Virtual properties domain = np.array(lagdomain) window = np.array(lagdomain) basis_name = 'L'12345678910111213141516171819202122232425262728293031323334353637383940414243444546from typing import Any
from numpy import ndarray, dtype, int_from numpy.polynomial._polybase import ABCPolyBasefrom numpy.polynomial.polyutils import trimcoef
__all__: list[str]
lagtrim = trimcoef
def poly2lag(pol): ...def lag2poly(c): ...
lagdomain: ndarray[Any, dtype[int_]]lagzero: ndarray[Any, dtype[int_]]lagone: ndarray[Any, dtype[int_]]lagx: ndarray[Any, dtype[int_]]
def lagline(off, scl): ...def lagfromroots(roots): ...def lagadd(c1, c2): ...def lagsub(c1, c2): ...def lagmulx(c): ...def lagmul(c1, c2): ...def lagdiv(c1, c2): ...def lagpow(c, pow, maxpower=...): ...def lagder(c, m=..., scl=..., axis=...): ...def lagint(c, m=..., k = ..., lbnd=..., scl=..., axis=...): ...def lagval(x, c, tensor=...): ...def lagval2d(x, y, c): ...def laggrid2d(x, y, c): ...def lagval3d(x, y, z, c): ...def laggrid3d(x, y, z, c): ...def lagvander(x, deg): ...def lagvander2d(x, y, deg): ...def lagvander3d(x, y, z, deg): ...def lagfit(x, y, deg, rcond=..., full=..., w=...): ...def lagcompanion(c): ...def lagroots(c): ...def laggauss(deg): ...def lagweight(x): ...
class Laguerre(ABCPolyBase): domain: Any window: Any basis_name: Any12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664"""==================================================Legendre Series (:mod:`numpy.polynomial.legendre`)==================================================
This module provides a number of objects (mostly functions) useful fordealing with Legendre series, including a `Legendre` class thatencapsulates the usual arithmetic operations. (General informationon how this module represents and works with such polynomials is in thedocstring for its "parent" sub-package, `numpy.polynomial`).
Classes-------.. autosummary:: :toctree: generated/
Legendre
Constants---------
.. autosummary:: :toctree: generated/
legdomain legzero legone legx
Arithmetic----------
.. autosummary:: :toctree: generated/
legadd legsub legmulx legmul legdiv legpow legval legval2d legval3d leggrid2d leggrid3d
Calculus--------
.. autosummary:: :toctree: generated/
legder legint
Misc Functions--------------
.. autosummary:: :toctree: generated/
legfromroots legroots legvander legvander2d legvander3d leggauss legweight legcompanion legfit legtrim legline leg2poly poly2leg
See also--------numpy.polynomial
"""import numpy as npimport numpy.linalg as lafrom numpy.core.multiarray import normalize_axis_index
from . import polyutils as pufrom ._polybase import ABCPolyBase
__all__ = [ 'legzero', 'legone', 'legx', 'legdomain', 'legline', 'legadd', 'legsub', 'legmulx', 'legmul', 'legdiv', 'legpow', 'legval', 'legder', 'legint', 'leg2poly', 'poly2leg', 'legfromroots', 'legvander', 'legfit', 'legtrim', 'legroots', 'Legendre', 'legval2d', 'legval3d', 'leggrid2d', 'leggrid3d', 'legvander2d', 'legvander3d', 'legcompanion', 'leggauss', 'legweight']
legtrim = pu.trimcoef
def poly2leg(pol): """ Convert a polynomial to a Legendre series.
Convert an array representing the coefficients of a polynomial (relative to the "standard" basis) ordered from lowest degree to highest, to an array of the coefficients of the equivalent Legendre series, ordered from lowest to highest degree.
Parameters ---------- pol : array_like 1-D array containing the polynomial coefficients
Returns ------- c : ndarray 1-D array containing the coefficients of the equivalent Legendre series.
See Also -------- leg2poly
Notes ----- The easy way to do conversions between polynomial basis sets is to use the convert method of a class instance.
Examples -------- >>> from numpy import polynomial as P >>> p = P.Polynomial(np.arange(4)) >>> p Polynomial([0., 1., 2., 3.], domain=[-1, 1], window=[-1, 1]) >>> c = P.Legendre(P.legendre.poly2leg(p.coef)) >>> c Legendre([ 1. , 3.25, 1. , 0.75], domain=[-1, 1], window=[-1, 1]) # may vary
""" [pol] = pu.as_series([pol]) deg = len(pol) - 1 res = 0 for i in range(deg, -1, -1): res = legadd(legmulx(res), pol[i]) return res
def leg2poly(c): """ Convert a Legendre series to a polynomial.
Convert an array representing the coefficients of a Legendre series, ordered from lowest degree to highest, to an array of the coefficients of the equivalent polynomial (relative to the "standard" basis) ordered from lowest to highest degree.
Parameters ---------- c : array_like 1-D array containing the Legendre series coefficients, ordered from lowest order term to highest.
Returns ------- pol : ndarray 1-D array containing the coefficients of the equivalent polynomial (relative to the "standard" basis) ordered from lowest order term to highest.
See Also -------- poly2leg
Notes ----- The easy way to do conversions between polynomial basis sets is to use the convert method of a class instance.
Examples -------- >>> from numpy import polynomial as P >>> c = P.Legendre(range(4)) >>> c Legendre([0., 1., 2., 3.], domain=[-1, 1], window=[-1, 1]) >>> p = c.convert(kind=P.Polynomial) >>> p Polynomial([-1. , -3.5, 3. , 7.5], domain=[-1., 1.], window=[-1., 1.]) >>> P.legendre.leg2poly(range(4)) array([-1. , -3.5, 3. , 7.5])
""" from .polynomial import polyadd, polysub, polymulx
[c] = pu.as_series([c]) n = len(c) if n < 3: return c else: c0 = c[-2] c1 = c[-1] # i is the current degree of c1 for i in range(n - 1, 1, -1): tmp = c0 c0 = polysub(c[i - 2], (c1*(i - 1))/i) c1 = polyadd(tmp, (polymulx(c1)*(2*i - 1))/i) return polyadd(c0, polymulx(c1))
## These are constant arrays are of integer type so as to be compatible# with the widest range of other types, such as Decimal.#
# Legendrelegdomain = np.array([-1, 1])
# Legendre coefficients representing zero.legzero = np.array([0])
# Legendre coefficients representing one.legone = np.array([1])
# Legendre coefficients representing the identity x.legx = np.array([0, 1])
def legline(off, scl): """ Legendre series whose graph is a straight line.
Parameters ---------- off, scl : scalars The specified line is given by ``off + scl*x``.
Returns ------- y : ndarray This module's representation of the Legendre series for ``off + scl*x``.
See Also -------- numpy.polynomial.polynomial.polyline numpy.polynomial.chebyshev.chebline numpy.polynomial.laguerre.lagline numpy.polynomial.hermite.hermline numpy.polynomial.hermite_e.hermeline
Examples -------- >>> import numpy.polynomial.legendre as L >>> L.legline(3,2) array([3, 2]) >>> L.legval(-3, L.legline(3,2)) # should be -3 -3.0
""" if scl != 0: return np.array([off, scl]) else: return np.array([off])
def legfromroots(roots): """ Generate a Legendre series with given roots.
The function returns the coefficients of the polynomial
.. math:: p(x) = (x - r_0) * (x - r_1) * ... * (x - r_n),
in Legendre form, where the `r_n` are the roots specified in `roots`. If a zero has multiplicity n, then it must appear in `roots` n times. For instance, if 2 is a root of multiplicity three and 3 is a root of multiplicity 2, then `roots` looks something like [2, 2, 2, 3, 3]. The roots can appear in any order.
If the returned coefficients are `c`, then
.. math:: p(x) = c_0 + c_1 * L_1(x) + ... + c_n * L_n(x)
The coefficient of the last term is not generally 1 for monic polynomials in Legendre form.
Parameters ---------- roots : array_like Sequence containing the roots.
Returns ------- out : ndarray 1-D array of coefficients. If all roots are real then `out` is a real array, if some of the roots are complex, then `out` is complex even if all the coefficients in the result are real (see Examples below).
See Also -------- numpy.polynomial.polynomial.polyfromroots numpy.polynomial.chebyshev.chebfromroots numpy.polynomial.laguerre.lagfromroots numpy.polynomial.hermite.hermfromroots numpy.polynomial.hermite_e.hermefromroots
Examples -------- >>> import numpy.polynomial.legendre as L >>> L.legfromroots((-1,0,1)) # x^3 - x relative to the standard basis array([ 0. , -0.4, 0. , 0.4]) >>> j = complex(0,1) >>> L.legfromroots((-j,j)) # x^2 + 1 relative to the standard basis array([ 1.33333333+0.j, 0.00000000+0.j, 0.66666667+0.j]) # may vary
""" return pu._fromroots(legline, legmul, roots)
def legadd(c1, c2): """ Add one Legendre series to another.
Returns the sum of two Legendre series `c1` + `c2`. The arguments are sequences of coefficients ordered from lowest order term to highest, i.e., [1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
Parameters ---------- c1, c2 : array_like 1-D arrays of Legendre series coefficients ordered from low to high.
Returns ------- out : ndarray Array representing the Legendre series of their sum.
See Also -------- legsub, legmulx, legmul, legdiv, legpow
Notes ----- Unlike multiplication, division, etc., the sum of two Legendre series is a Legendre series (without having to "reproject" the result onto the basis set) so addition, just like that of "standard" polynomials, is simply "component-wise."
Examples -------- >>> from numpy.polynomial import legendre as L >>> c1 = (1,2,3) >>> c2 = (3,2,1) >>> L.legadd(c1,c2) array([4., 4., 4.])
""" return pu._add(c1, c2)
def legsub(c1, c2): """ Subtract one Legendre series from another.
Returns the difference of two Legendre series `c1` - `c2`. The sequences of coefficients are from lowest order term to highest, i.e., [1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
Parameters ---------- c1, c2 : array_like 1-D arrays of Legendre series coefficients ordered from low to high.
Returns ------- out : ndarray Of Legendre series coefficients representing their difference.
See Also -------- legadd, legmulx, legmul, legdiv, legpow
Notes ----- Unlike multiplication, division, etc., the difference of two Legendre series is a Legendre series (without having to "reproject" the result onto the basis set) so subtraction, just like that of "standard" polynomials, is simply "component-wise."
Examples -------- >>> from numpy.polynomial import legendre as L >>> c1 = (1,2,3) >>> c2 = (3,2,1) >>> L.legsub(c1,c2) array([-2., 0., 2.]) >>> L.legsub(c2,c1) # -C.legsub(c1,c2) array([ 2., 0., -2.])
""" return pu._sub(c1, c2)
def legmulx(c): """Multiply a Legendre series by x.
Multiply the Legendre series `c` by x, where x is the independent variable.
Parameters ---------- c : array_like 1-D array of Legendre series coefficients ordered from low to high.
Returns ------- out : ndarray Array representing the result of the multiplication.
See Also -------- legadd, legmul, legdiv, legpow
Notes ----- The multiplication uses the recursion relationship for Legendre polynomials in the form
.. math::
xP_i(x) = ((i + 1)*P_{i + 1}(x) + i*P_{i - 1}(x))/(2i + 1)
Examples -------- >>> from numpy.polynomial import legendre as L >>> L.legmulx([1,2,3]) array([ 0.66666667, 2.2, 1.33333333, 1.8]) # may vary
""" # c is a trimmed copy [c] = pu.as_series([c]) # The zero series needs special treatment if len(c) == 1 and c[0] == 0: return c
prd = np.empty(len(c) + 1, dtype=c.dtype) prd[0] = c[0]*0 prd[1] = c[0] for i in range(1, len(c)): j = i + 1 k = i - 1 s = i + j prd[j] = (c[i]*j)/s prd[k] += (c[i]*i)/s return prd
def legmul(c1, c2): """ Multiply one Legendre series by another.
Returns the product of two Legendre series `c1` * `c2`. The arguments are sequences of coefficients, from lowest order "term" to highest, e.g., [1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
Parameters ---------- c1, c2 : array_like 1-D arrays of Legendre series coefficients ordered from low to high.
Returns ------- out : ndarray Of Legendre series coefficients representing their product.
See Also -------- legadd, legsub, legmulx, legdiv, legpow
Notes ----- In general, the (polynomial) product of two C-series results in terms that are not in the Legendre polynomial basis set. Thus, to express the product as a Legendre series, it is necessary to "reproject" the product onto said basis set, which may produce "unintuitive" (but correct) results; see Examples section below.
Examples -------- >>> from numpy.polynomial import legendre as L >>> c1 = (1,2,3) >>> c2 = (3,2) >>> L.legmul(c1,c2) # multiplication requires "reprojection" array([ 4.33333333, 10.4 , 11.66666667, 3.6 ]) # may vary
""" # s1, s2 are trimmed copies [c1, c2] = pu.as_series([c1, c2])
if len(c1) > len(c2): c = c2 xs = c1 else: c = c1 xs = c2
if len(c) == 1: c0 = c[0]*xs c1 = 0 elif len(c) == 2: c0 = c[0]*xs c1 = c[1]*xs else: nd = len(c) c0 = c[-2]*xs c1 = c[-1]*xs for i in range(3, len(c) + 1): tmp = c0 nd = nd - 1 c0 = legsub(c[-i]*xs, (c1*(nd - 1))/nd) c1 = legadd(tmp, (legmulx(c1)*(2*nd - 1))/nd) return legadd(c0, legmulx(c1))
def legdiv(c1, c2): """ Divide one Legendre series by another.
Returns the quotient-with-remainder of two Legendre series `c1` / `c2`. The arguments are sequences of coefficients from lowest order "term" to highest, e.g., [1,2,3] represents the series ``P_0 + 2*P_1 + 3*P_2``.
Parameters ---------- c1, c2 : array_like 1-D arrays of Legendre series coefficients ordered from low to high.
Returns ------- quo, rem : ndarrays Of Legendre series coefficients representing the quotient and remainder.
See Also -------- legadd, legsub, legmulx, legmul, legpow
Notes ----- In general, the (polynomial) division of one Legendre series by another results in quotient and remainder terms that are not in the Legendre polynomial basis set. Thus, to express these results as a Legendre series, it is necessary to "reproject" the results onto the Legendre basis set, which may produce "unintuitive" (but correct) results; see Examples section below.
Examples -------- >>> from numpy.polynomial import legendre as L >>> c1 = (1,2,3) >>> c2 = (3,2,1) >>> L.legdiv(c1,c2) # quotient "intuitive," remainder not (array([3.]), array([-8., -4.])) >>> c2 = (0,1,2,3) >>> L.legdiv(c2,c1) # neither "intuitive" (array([-0.07407407, 1.66666667]), array([-1.03703704, -2.51851852])) # may vary
""" return pu._div(legmul, c1, c2)
def legpow(c, pow, maxpower=16): """Raise a Legendre series to a power.
Returns the Legendre series `c` raised to the power `pow`. The argument `c` is a sequence of coefficients ordered from low to high. i.e., [1,2,3] is the series ``P_0 + 2*P_1 + 3*P_2.``
Parameters ---------- c : array_like 1-D array of Legendre series coefficients ordered from low to high. pow : integer Power to which the series will be raised maxpower : integer, optional Maximum power allowed. This is mainly to limit growth of the series to unmanageable size. Default is 16
Returns ------- coef : ndarray Legendre series of power.
See Also -------- legadd, legsub, legmulx, legmul, legdiv
""" return pu._pow(legmul, c, pow, maxpower)
def legder(c, m=1, scl=1, axis=0): """ Differentiate a Legendre series.
Returns the Legendre series coefficients `c` differentiated `m` times along `axis`. At each iteration the result is multiplied by `scl` (the scaling factor is for use in a linear change of variable). The argument `c` is an array of coefficients from low to high degree along each axis, e.g., [1,2,3] represents the series ``1*L_0 + 2*L_1 + 3*L_2`` while [[1,2],[1,2]] represents ``1*L_0(x)*L_0(y) + 1*L_1(x)*L_0(y) + 2*L_0(x)*L_1(y) + 2*L_1(x)*L_1(y)`` if axis=0 is ``x`` and axis=1 is ``y``.
Parameters ---------- c : array_like Array of Legendre series coefficients. If c is multidimensional the different axis correspond to different variables with the degree in each axis given by the corresponding index. m : int, optional Number of derivatives taken, must be non-negative. (Default: 1) scl : scalar, optional Each differentiation is multiplied by `scl`. The end result is multiplication by ``scl**m``. This is for use in a linear change of variable. (Default: 1) axis : int, optional Axis over which the derivative is taken. (Default: 0).
.. versionadded:: 1.7.0
Returns ------- der : ndarray Legendre series of the derivative.
See Also -------- legint
Notes ----- In general, the result of differentiating a Legendre series does not resemble the same operation on a power series. Thus the result of this function may be "unintuitive," albeit correct; see Examples section below.
Examples -------- >>> from numpy.polynomial import legendre as L >>> c = (1,2,3,4) >>> L.legder(c) array([ 6., 9., 20.]) >>> L.legder(c, 3) array([60.]) >>> L.legder(c, scl=-1) array([ -6., -9., -20.]) >>> L.legder(c, 2,-1) array([ 9., 60.])
""" c = np.array(c, ndmin=1, copy=True) if c.dtype.char in '?bBhHiIlLqQpP': c = c.astype(np.double) cnt = pu._deprecate_as_int(m, "the order of derivation") iaxis = pu._deprecate_as_int(axis, "the axis") if cnt < 0: raise ValueError("The order of derivation must be non-negative") iaxis = normalize_axis_index(iaxis, c.ndim)
if cnt == 0: return c
c = np.moveaxis(c, iaxis, 0) n = len(c) if cnt >= n: c = c[:1]*0 else: for i in range(cnt): n = n - 1 c *= scl der = np.empty((n,) + c.shape[1:], dtype=c.dtype) for j in range(n, 2, -1): der[j - 1] = (2*j - 1)*c[j] c[j - 2] += c[j] if n > 1: der[1] = 3*c[2] der[0] = c[1] c = der c = np.moveaxis(c, 0, iaxis) return c
def legint(c, m=1, k=[], lbnd=0, scl=1, axis=0): """ Integrate a Legendre series.
Returns the Legendre series coefficients `c` integrated `m` times from `lbnd` along `axis`. At each iteration the resulting series is **multiplied** by `scl` and an integration constant, `k`, is added. The scaling factor is for use in a linear change of variable. ("Buyer beware": note that, depending on what one is doing, one may want `scl` to be the reciprocal of what one might expect; for more information, see the Notes section below.) The argument `c` is an array of coefficients from low to high degree along each axis, e.g., [1,2,3] represents the series ``L_0 + 2*L_1 + 3*L_2`` while [[1,2],[1,2]] represents ``1*L_0(x)*L_0(y) + 1*L_1(x)*L_0(y) + 2*L_0(x)*L_1(y) + 2*L_1(x)*L_1(y)`` if axis=0 is ``x`` and axis=1 is ``y``.
Parameters ---------- c : array_like Array of Legendre series coefficients. If c is multidimensional the different axis correspond to different variables with the degree in each axis given by the corresponding index. m : int, optional Order of integration, must be positive. (Default: 1) k : {[], list, scalar}, optional Integration constant(s). The value of the first integral at ``lbnd`` is the first value in the list, the value of the second integral at ``lbnd`` is the second value, etc. If ``k == []`` (the default), all constants are set to zero. If ``m == 1``, a single scalar can be given instead of a list. lbnd : scalar, optional The lower bound of the integral. (Default: 0) scl : scalar, optional Following each integration the result is *multiplied* by `scl` before the integration constant is added. (Default: 1) axis : int, optional Axis over which the integral is taken. (Default: 0).
.. versionadded:: 1.7.0
Returns ------- S : ndarray Legendre series coefficient array of the integral.
Raises ------ ValueError If ``m < 0``, ``len(k) > m``, ``np.ndim(lbnd) != 0``, or ``np.ndim(scl) != 0``.
See Also -------- legder
Notes ----- Note that the result of each integration is *multiplied* by `scl`. Why is this important to note? Say one is making a linear change of variable :math:`u = ax + b` in an integral relative to `x`. Then :math:`dx = du/a`, so one will need to set `scl` equal to :math:`1/a` - perhaps not what one would have first thought.
Also note that, in general, the result of integrating a C-series needs to be "reprojected" onto the C-series basis set. Thus, typically, the result of this function is "unintuitive," albeit correct; see Examples section below.
Examples -------- >>> from numpy.polynomial import legendre as L >>> c = (1,2,3) >>> L.legint(c) array([ 0.33333333, 0.4 , 0.66666667, 0.6 ]) # may vary >>> L.legint(c, 3) array([ 1.66666667e-02, -1.78571429e-02, 4.76190476e-02, # may vary -1.73472348e-18, 1.90476190e-02, 9.52380952e-03]) >>> L.legint(c, k=3) array([ 3.33333333, 0.4 , 0.66666667, 0.6 ]) # may vary >>> L.legint(c, lbnd=-2) array([ 7.33333333, 0.4 , 0.66666667, 0.6 ]) # may vary >>> L.legint(c, scl=2) array([ 0.66666667, 0.8 , 1.33333333, 1.2 ]) # may vary
""" c = np.array(c, ndmin=1, copy=True) if c.dtype.char in '?bBhHiIlLqQpP': c = c.astype(np.double) if not np.iterable(k): k = [k] cnt = pu._deprecate_as_int(m, "the order of integration") iaxis = pu._deprecate_as_int(axis, "the axis") if cnt < 0: raise ValueError("The order of integration must be non-negative") if len(k) > cnt: raise ValueError("Too many integration constants") if np.ndim(lbnd) != 0: raise ValueError("lbnd must be a scalar.") if np.ndim(scl) != 0: raise ValueError("scl must be a scalar.") iaxis = normalize_axis_index(iaxis, c.ndim)
if cnt == 0: return c
c = np.moveaxis(c, iaxis, 0) k = list(k) + [0]*(cnt - len(k)) for i in range(cnt): n = len(c) c *= scl if n == 1 and np.all(c[0] == 0): c[0] += k[i] else: tmp = np.empty((n + 1,) + c.shape[1:], dtype=c.dtype) tmp[0] = c[0]*0 tmp[1] = c[0] if n > 1: tmp[2] = c[1]/3 for j in range(2, n): t = c[j]/(2*j + 1) tmp[j + 1] = t tmp[j - 1] -= t tmp[0] += k[i] - legval(lbnd, tmp) c = tmp c = np.moveaxis(c, 0, iaxis) return c
def legval(x, c, tensor=True): """ Evaluate a Legendre series at points x.
If `c` is of length `n + 1`, this function returns the value:
.. math:: p(x) = c_0 * L_0(x) + c_1 * L_1(x) + ... + c_n * L_n(x)
The parameter `x` is converted to an array only if it is a tuple or a list, otherwise it is treated as a scalar. In either case, either `x` or its elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` is a 1-D array, then `p(x)` will have the same shape as `x`. If `c` is multidimensional, then the shape of the result depends on the value of `tensor`. If `tensor` is true the shape will be c.shape[1:] + x.shape. If `tensor` is false the shape will be c.shape[1:]. Note that scalars have shape (,).
Trailing zeros in the coefficients will be used in the evaluation, so they should be avoided if efficiency is a concern.
Parameters ---------- x : array_like, compatible object If `x` is a list or tuple, it is converted to an ndarray, otherwise it is left unchanged and treated as a scalar. In either case, `x` or its elements must support addition and multiplication with themselves and with the elements of `c`. c : array_like Array of coefficients ordered so that the coefficients for terms of degree n are contained in c[n]. If `c` is multidimensional the remaining indices enumerate multiple polynomials. In the two dimensional case the coefficients may be thought of as stored in the columns of `c`. tensor : boolean, optional If True, the shape of the coefficient array is extended with ones on the right, one for each dimension of `x`. Scalars have dimension 0 for this action. The result is that every column of coefficients in `c` is evaluated for every element of `x`. If False, `x` is broadcast over the columns of `c` for the evaluation. This keyword is useful when `c` is multidimensional. The default value is True.
.. versionadded:: 1.7.0
Returns ------- values : ndarray, algebra_like The shape of the return value is described above.
See Also -------- legval2d, leggrid2d, legval3d, leggrid3d
Notes ----- The evaluation uses Clenshaw recursion, aka synthetic division.
""" c = np.array(c, ndmin=1, copy=False) if c.dtype.char in '?bBhHiIlLqQpP': c = c.astype(np.double) if isinstance(x, (tuple, list)): x = np.asarray(x) if isinstance(x, np.ndarray) and tensor: c = c.reshape(c.shape + (1,)*x.ndim)
if len(c) == 1: c0 = c[0] c1 = 0 elif len(c) == 2: c0 = c[0] c1 = c[1] else: nd = len(c) c0 = c[-2] c1 = c[-1] for i in range(3, len(c) + 1): tmp = c0 nd = nd - 1 c0 = c[-i] - (c1*(nd - 1))/nd c1 = tmp + (c1*x*(2*nd - 1))/nd return c0 + c1*x
def legval2d(x, y, c): """ Evaluate a 2-D Legendre series at points (x, y).
This function returns the values:
.. math:: p(x,y) = \\sum_{i,j} c_{i,j} * L_i(x) * L_j(y)
The parameters `x` and `y` are converted to arrays only if they are tuples or a lists, otherwise they are treated as a scalars and they must have the same shape after conversion. In either case, either `x` and `y` or their elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` is a 1-D array a one is implicitly appended to its shape to make it 2-D. The shape of the result will be c.shape[2:] + x.shape.
Parameters ---------- x, y : array_like, compatible objects The two dimensional series is evaluated at the points `(x, y)`, where `x` and `y` must have the same shape. If `x` or `y` is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and if it isn't an ndarray it is treated as a scalar. c : array_like Array of coefficients ordered so that the coefficient of the term of multi-degree i,j is contained in ``c[i,j]``. If `c` has dimension greater than two the remaining indices enumerate multiple sets of coefficients.
Returns ------- values : ndarray, compatible object The values of the two dimensional Legendre series at points formed from pairs of corresponding values from `x` and `y`.
See Also -------- legval, leggrid2d, legval3d, leggrid3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._valnd(legval, c, x, y)
def leggrid2d(x, y, c): """ Evaluate a 2-D Legendre series on the Cartesian product of x and y.
This function returns the values:
.. math:: p(a,b) = \\sum_{i,j} c_{i,j} * L_i(a) * L_j(b)
where the points `(a, b)` consist of all pairs formed by taking `a` from `x` and `b` from `y`. The resulting points form a grid with `x` in the first dimension and `y` in the second.
The parameters `x` and `y` are converted to arrays only if they are tuples or a lists, otherwise they are treated as a scalars. In either case, either `x` and `y` or their elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` has fewer than two dimensions, ones are implicitly appended to its shape to make it 2-D. The shape of the result will be c.shape[2:] + x.shape + y.shape.
Parameters ---------- x, y : array_like, compatible objects The two dimensional series is evaluated at the points in the Cartesian product of `x` and `y`. If `x` or `y` is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and, if it isn't an ndarray, it is treated as a scalar. c : array_like Array of coefficients ordered so that the coefficient of the term of multi-degree i,j is contained in `c[i,j]`. If `c` has dimension greater than two the remaining indices enumerate multiple sets of coefficients.
Returns ------- values : ndarray, compatible object The values of the two dimensional Chebyshev series at points in the Cartesian product of `x` and `y`.
See Also -------- legval, legval2d, legval3d, leggrid3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._gridnd(legval, c, x, y)
def legval3d(x, y, z, c): """ Evaluate a 3-D Legendre series at points (x, y, z).
This function returns the values:
.. math:: p(x,y,z) = \\sum_{i,j,k} c_{i,j,k} * L_i(x) * L_j(y) * L_k(z)
The parameters `x`, `y`, and `z` are converted to arrays only if they are tuples or a lists, otherwise they are treated as a scalars and they must have the same shape after conversion. In either case, either `x`, `y`, and `z` or their elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` has fewer than 3 dimensions, ones are implicitly appended to its shape to make it 3-D. The shape of the result will be c.shape[3:] + x.shape.
Parameters ---------- x, y, z : array_like, compatible object The three dimensional series is evaluated at the points `(x, y, z)`, where `x`, `y`, and `z` must have the same shape. If any of `x`, `y`, or `z` is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and if it isn't an ndarray it is treated as a scalar. c : array_like Array of coefficients ordered so that the coefficient of the term of multi-degree i,j,k is contained in ``c[i,j,k]``. If `c` has dimension greater than 3 the remaining indices enumerate multiple sets of coefficients.
Returns ------- values : ndarray, compatible object The values of the multidimensional polynomial on points formed with triples of corresponding values from `x`, `y`, and `z`.
See Also -------- legval, legval2d, leggrid2d, leggrid3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._valnd(legval, c, x, y, z)
def leggrid3d(x, y, z, c): """ Evaluate a 3-D Legendre series on the Cartesian product of x, y, and z.
This function returns the values:
.. math:: p(a,b,c) = \\sum_{i,j,k} c_{i,j,k} * L_i(a) * L_j(b) * L_k(c)
where the points `(a, b, c)` consist of all triples formed by taking `a` from `x`, `b` from `y`, and `c` from `z`. The resulting points form a grid with `x` in the first dimension, `y` in the second, and `z` in the third.
The parameters `x`, `y`, and `z` are converted to arrays only if they are tuples or a lists, otherwise they are treated as a scalars. In either case, either `x`, `y`, and `z` or their elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` has fewer than three dimensions, ones are implicitly appended to its shape to make it 3-D. The shape of the result will be c.shape[3:] + x.shape + y.shape + z.shape.
Parameters ---------- x, y, z : array_like, compatible objects The three dimensional series is evaluated at the points in the Cartesian product of `x`, `y`, and `z`. If `x`,`y`, or `z` is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and, if it isn't an ndarray, it is treated as a scalar. c : array_like Array of coefficients ordered so that the coefficients for terms of degree i,j are contained in ``c[i,j]``. If `c` has dimension greater than two the remaining indices enumerate multiple sets of coefficients.
Returns ------- values : ndarray, compatible object The values of the two dimensional polynomial at points in the Cartesian product of `x` and `y`.
See Also -------- legval, legval2d, leggrid2d, legval3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._gridnd(legval, c, x, y, z)
def legvander(x, deg): """Pseudo-Vandermonde matrix of given degree.
Returns the pseudo-Vandermonde matrix of degree `deg` and sample points `x`. The pseudo-Vandermonde matrix is defined by
.. math:: V[..., i] = L_i(x)
where `0 <= i <= deg`. The leading indices of `V` index the elements of `x` and the last index is the degree of the Legendre polynomial.
If `c` is a 1-D array of coefficients of length `n + 1` and `V` is the array ``V = legvander(x, n)``, then ``np.dot(V, c)`` and ``legval(x, c)`` are the same up to roundoff. This equivalence is useful both for least squares fitting and for the evaluation of a large number of Legendre series of the same degree and sample points.
Parameters ---------- x : array_like Array of points. The dtype is converted to float64 or complex128 depending on whether any of the elements are complex. If `x` is scalar it is converted to a 1-D array. deg : int Degree of the resulting matrix.
Returns ------- vander : ndarray The pseudo-Vandermonde matrix. The shape of the returned matrix is ``x.shape + (deg + 1,)``, where The last index is the degree of the corresponding Legendre polynomial. The dtype will be the same as the converted `x`.
""" ideg = pu._deprecate_as_int(deg, "deg") if ideg < 0: raise ValueError("deg must be non-negative")
x = np.array(x, copy=False, ndmin=1) + 0.0 dims = (ideg + 1,) + x.shape dtyp = x.dtype v = np.empty(dims, dtype=dtyp) # Use forward recursion to generate the entries. This is not as accurate # as reverse recursion in this application but it is more efficient. v[0] = x*0 + 1 if ideg > 0: v[1] = x for i in range(2, ideg + 1): v[i] = (v[i-1]*x*(2*i - 1) - v[i-2]*(i - 1))/i return np.moveaxis(v, 0, -1)
def legvander2d(x, y, deg): """Pseudo-Vandermonde matrix of given degrees.
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample points `(x, y)`. The pseudo-Vandermonde matrix is defined by
.. math:: V[..., (deg[1] + 1)*i + j] = L_i(x) * L_j(y),
where `0 <= i <= deg[0]` and `0 <= j <= deg[1]`. The leading indices of `V` index the points `(x, y)` and the last index encodes the degrees of the Legendre polynomials.
If ``V = legvander2d(x, y, [xdeg, ydeg])``, then the columns of `V` correspond to the elements of a 2-D coefficient array `c` of shape (xdeg + 1, ydeg + 1) in the order
.. math:: c_{00}, c_{01}, c_{02} ... , c_{10}, c_{11}, c_{12} ...
and ``np.dot(V, c.flat)`` and ``legval2d(x, y, c)`` will be the same up to roundoff. This equivalence is useful both for least squares fitting and for the evaluation of a large number of 2-D Legendre series of the same degrees and sample points.
Parameters ---------- x, y : array_like Arrays of point coordinates, all of the same shape. The dtypes will be converted to either float64 or complex128 depending on whether any of the elements are complex. Scalars are converted to 1-D arrays. deg : list of ints List of maximum degrees of the form [x_deg, y_deg].
Returns ------- vander2d : ndarray The shape of the returned matrix is ``x.shape + (order,)``, where :math:`order = (deg[0]+1)*(deg[1]+1)`. The dtype will be the same as the converted `x` and `y`.
See Also -------- legvander, legvander3d, legval2d, legval3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._vander_nd_flat((legvander, legvander), (x, y), deg)
def legvander3d(x, y, z, deg): """Pseudo-Vandermonde matrix of given degrees.
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample points `(x, y, z)`. If `l, m, n` are the given degrees in `x, y, z`, then The pseudo-Vandermonde matrix is defined by
.. math:: V[..., (m+1)(n+1)i + (n+1)j + k] = L_i(x)*L_j(y)*L_k(z),
where `0 <= i <= l`, `0 <= j <= m`, and `0 <= j <= n`. The leading indices of `V` index the points `(x, y, z)` and the last index encodes the degrees of the Legendre polynomials.
If ``V = legvander3d(x, y, z, [xdeg, ydeg, zdeg])``, then the columns of `V` correspond to the elements of a 3-D coefficient array `c` of shape (xdeg + 1, ydeg + 1, zdeg + 1) in the order
.. math:: c_{000}, c_{001}, c_{002},... , c_{010}, c_{011}, c_{012},...
and ``np.dot(V, c.flat)`` and ``legval3d(x, y, z, c)`` will be the same up to roundoff. This equivalence is useful both for least squares fitting and for the evaluation of a large number of 3-D Legendre series of the same degrees and sample points.
Parameters ---------- x, y, z : array_like Arrays of point coordinates, all of the same shape. The dtypes will be converted to either float64 or complex128 depending on whether any of the elements are complex. Scalars are converted to 1-D arrays. deg : list of ints List of maximum degrees of the form [x_deg, y_deg, z_deg].
Returns ------- vander3d : ndarray The shape of the returned matrix is ``x.shape + (order,)``, where :math:`order = (deg[0]+1)*(deg[1]+1)*(deg[2]+1)`. The dtype will be the same as the converted `x`, `y`, and `z`.
See Also -------- legvander, legvander3d, legval2d, legval3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._vander_nd_flat((legvander, legvander, legvander), (x, y, z), deg)
def legfit(x, y, deg, rcond=None, full=False, w=None): """ Least squares fit of Legendre series to data.
Return the coefficients of a Legendre series of degree `deg` that is the least squares fit to the data values `y` given at points `x`. If `y` is 1-D the returned coefficients will also be 1-D. If `y` is 2-D multiple fits are done, one for each column of `y`, and the resulting coefficients are stored in the corresponding columns of a 2-D return. The fitted polynomial(s) are in the form
.. math:: p(x) = c_0 + c_1 * L_1(x) + ... + c_n * L_n(x),
where `n` is `deg`.
Parameters ---------- x : array_like, shape (M,) x-coordinates of the M sample points ``(x[i], y[i])``. y : array_like, shape (M,) or (M, K) y-coordinates of the sample points. Several data sets of sample points sharing the same x-coordinates can be fitted at once by passing in a 2D-array that contains one dataset per column. deg : int or 1-D array_like Degree(s) of the fitting polynomials. If `deg` is a single integer all terms up to and including the `deg`'th term are included in the fit. For NumPy versions >= 1.11.0 a list of integers specifying the degrees of the terms to include may be used instead. rcond : float, optional Relative condition number of the fit. Singular values smaller than this relative to the largest singular value will be ignored. The default value is len(x)*eps, where eps is the relative precision of the float type, about 2e-16 in most cases. full : bool, optional Switch determining nature of return value. When it is False (the default) just the coefficients are returned, when True diagnostic information from the singular value decomposition is also returned. w : array_like, shape (`M`,), optional Weights. If not None, the weight ``w[i]`` applies to the unsquared residual ``y[i] - y_hat[i]`` at ``x[i]``. Ideally the weights are chosen so that the errors of the products ``w[i]*y[i]`` all have the same variance. When using inverse-variance weighting, use ``w[i] = 1/sigma(y[i])``. The default value is None.
.. versionadded:: 1.5.0
Returns ------- coef : ndarray, shape (M,) or (M, K) Legendre coefficients ordered from low to high. If `y` was 2-D, the coefficients for the data in column k of `y` are in column `k`. If `deg` is specified as a list, coefficients for terms not included in the fit are set equal to zero in the returned `coef`.
[residuals, rank, singular_values, rcond] : list These values are only returned if ``full == True``
- residuals -- sum of squared residuals of the least squares fit - rank -- the numerical rank of the scaled Vandermonde matrix - singular_values -- singular values of the scaled Vandermonde matrix - rcond -- value of `rcond`.
For more details, see `numpy.linalg.lstsq`.
Warns ----- RankWarning The rank of the coefficient matrix in the least-squares fit is deficient. The warning is only raised if ``full == False``. The warnings can be turned off by
>>> import warnings >>> warnings.simplefilter('ignore', np.RankWarning)
See Also -------- numpy.polynomial.polynomial.polyfit numpy.polynomial.chebyshev.chebfit numpy.polynomial.laguerre.lagfit numpy.polynomial.hermite.hermfit numpy.polynomial.hermite_e.hermefit legval : Evaluates a Legendre series. legvander : Vandermonde matrix of Legendre series. legweight : Legendre weight function (= 1). numpy.linalg.lstsq : Computes a least-squares fit from the matrix. scipy.interpolate.UnivariateSpline : Computes spline fits.
Notes ----- The solution is the coefficients of the Legendre series `p` that minimizes the sum of the weighted squared errors
.. math:: E = \\sum_j w_j^2 * |y_j - p(x_j)|^2,
where :math:`w_j` are the weights. This problem is solved by setting up as the (typically) overdetermined matrix equation
.. math:: V(x) * c = w * y,
where `V` is the weighted pseudo Vandermonde matrix of `x`, `c` are the coefficients to be solved for, `w` are the weights, and `y` are the observed values. This equation is then solved using the singular value decomposition of `V`.
If some of the singular values of `V` are so small that they are neglected, then a `RankWarning` will be issued. This means that the coefficient values may be poorly determined. Using a lower order fit will usually get rid of the warning. The `rcond` parameter can also be set to a value smaller than its default, but the resulting fit may be spurious and have large contributions from roundoff error.
Fits using Legendre series are usually better conditioned than fits using power series, but much can depend on the distribution of the sample points and the smoothness of the data. If the quality of the fit is inadequate splines may be a good alternative.
References ---------- .. [1] Wikipedia, "Curve fitting", https://en.wikipedia.org/wiki/Curve_fitting
Examples --------
""" return pu._fit(legvander, x, y, deg, rcond, full, w)
def legcompanion(c): """Return the scaled companion matrix of c.
The basis polynomials are scaled so that the companion matrix is symmetric when `c` is an Legendre basis polynomial. This provides better eigenvalue estimates than the unscaled case and for basis polynomials the eigenvalues are guaranteed to be real if `numpy.linalg.eigvalsh` is used to obtain them.
Parameters ---------- c : array_like 1-D array of Legendre series coefficients ordered from low to high degree.
Returns ------- mat : ndarray Scaled companion matrix of dimensions (deg, deg).
Notes -----
.. versionadded:: 1.7.0
""" # c is a trimmed copy [c] = pu.as_series([c]) if len(c) < 2: raise ValueError('Series must have maximum degree of at least 1.') if len(c) == 2: return np.array([[-c[0]/c[1]]])
n = len(c) - 1 mat = np.zeros((n, n), dtype=c.dtype) scl = 1./np.sqrt(2*np.arange(n) + 1) top = mat.reshape(-1)[1::n+1] bot = mat.reshape(-1)[n::n+1] top[...] = np.arange(1, n)*scl[:n-1]*scl[1:n] bot[...] = top mat[:, -1] -= (c[:-1]/c[-1])*(scl/scl[-1])*(n/(2*n - 1)) return mat
def legroots(c): """ Compute the roots of a Legendre series.
Return the roots (a.k.a. "zeros") of the polynomial
.. math:: p(x) = \\sum_i c[i] * L_i(x).
Parameters ---------- c : 1-D array_like 1-D array of coefficients.
Returns ------- out : ndarray Array of the roots of the series. If all the roots are real, then `out` is also real, otherwise it is complex.
See Also -------- numpy.polynomial.polynomial.polyroots numpy.polynomial.chebyshev.chebroots numpy.polynomial.laguerre.lagroots numpy.polynomial.hermite.hermroots numpy.polynomial.hermite_e.hermeroots
Notes ----- The root estimates are obtained as the eigenvalues of the companion matrix, Roots far from the origin of the complex plane may have large errors due to the numerical instability of the series for such values. Roots with multiplicity greater than 1 will also show larger errors as the value of the series near such points is relatively insensitive to errors in the roots. Isolated roots near the origin can be improved by a few iterations of Newton's method.
The Legendre series basis polynomials aren't powers of ``x`` so the results of this function may seem unintuitive.
Examples -------- >>> import numpy.polynomial.legendre as leg >>> leg.legroots((1, 2, 3, 4)) # 4L_3 + 3L_2 + 2L_1 + 1L_0, all real roots array([-0.85099543, -0.11407192, 0.51506735]) # may vary
""" # c is a trimmed copy [c] = pu.as_series([c]) if len(c) < 2: return np.array([], dtype=c.dtype) if len(c) == 2: return np.array([-c[0]/c[1]])
# rotated companion matrix reduces error m = legcompanion(c)[::-1,::-1] r = la.eigvals(m) r.sort() return r
def leggauss(deg): """ Gauss-Legendre quadrature.
Computes the sample points and weights for Gauss-Legendre quadrature. These sample points and weights will correctly integrate polynomials of degree :math:`2*deg - 1` or less over the interval :math:`[-1, 1]` with the weight function :math:`f(x) = 1`.
Parameters ---------- deg : int Number of sample points and weights. It must be >= 1.
Returns ------- x : ndarray 1-D ndarray containing the sample points. y : ndarray 1-D ndarray containing the weights.
Notes -----
.. versionadded:: 1.7.0
The results have only been tested up to degree 100, higher degrees may be problematic. The weights are determined by using the fact that
.. math:: w_k = c / (L'_n(x_k) * L_{n-1}(x_k))
where :math:`c` is a constant independent of :math:`k` and :math:`x_k` is the k'th root of :math:`L_n`, and then scaling the results to get the right value when integrating 1.
""" ideg = pu._deprecate_as_int(deg, "deg") if ideg <= 0: raise ValueError("deg must be a positive integer")
# first approximation of roots. We use the fact that the companion # matrix is symmetric in this case in order to obtain better zeros. c = np.array([0]*deg + [1]) m = legcompanion(c) x = la.eigvalsh(m)
# improve roots by one application of Newton dy = legval(x, c) df = legval(x, legder(c)) x -= dy/df
# compute the weights. We scale the factor to avoid possible numerical # overflow. fm = legval(x, c[1:]) fm /= np.abs(fm).max() df /= np.abs(df).max() w = 1/(fm * df)
# for Legendre we can also symmetrize w = (w + w[::-1])/2 x = (x - x[::-1])/2
# scale w to get the right value w *= 2. / w.sum()
return x, w
def legweight(x): """ Weight function of the Legendre polynomials.
The weight function is :math:`1` and the interval of integration is :math:`[-1, 1]`. The Legendre polynomials are orthogonal, but not normalized, with respect to this weight function.
Parameters ---------- x : array_like Values at which the weight function will be computed.
Returns ------- w : ndarray The weight function at `x`.
Notes -----
.. versionadded:: 1.7.0
""" w = x*0.0 + 1.0 return w
## Legendre series class#
class Legendre(ABCPolyBase): """A Legendre series class.
The Legendre class provides the standard Python numerical methods '+', '-', '*', '//', '%', 'divmod', '**', and '()' as well as the attributes and methods listed in the `ABCPolyBase` documentation.
Parameters ---------- coef : array_like Legendre coefficients in order of increasing degree, i.e., ``(1, 2, 3)`` gives ``1*P_0(x) + 2*P_1(x) + 3*P_2(x)``. domain : (2,) array_like, optional Domain to use. The interval ``[domain[0], domain[1]]`` is mapped to the interval ``[window[0], window[1]]`` by shifting and scaling. The default value is [-1, 1]. window : (2,) array_like, optional Window, see `domain` for its use. The default value is [-1, 1].
.. versionadded:: 1.6.0 symbol : str, optional Symbol used to represent the independent variable in string representations of the polynomial expression, e.g. for printing. The symbol must be a valid Python identifier. Default value is 'x'.
.. versionadded:: 1.24
""" # Virtual Functions _add = staticmethod(legadd) _sub = staticmethod(legsub) _mul = staticmethod(legmul) _div = staticmethod(legdiv) _pow = staticmethod(legpow) _val = staticmethod(legval) _int = staticmethod(legint) _der = staticmethod(legder) _fit = staticmethod(legfit) _line = staticmethod(legline) _roots = staticmethod(legroots) _fromroots = staticmethod(legfromroots)
# Virtual properties domain = np.array(legdomain) window = np.array(legdomain) basis_name = 'P'12345678910111213141516171819202122232425262728293031323334353637383940414243444546from typing import Any
from numpy import ndarray, dtype, int_from numpy.polynomial._polybase import ABCPolyBasefrom numpy.polynomial.polyutils import trimcoef
__all__: list[str]
legtrim = trimcoef
def poly2leg(pol): ...def leg2poly(c): ...
legdomain: ndarray[Any, dtype[int_]]legzero: ndarray[Any, dtype[int_]]legone: ndarray[Any, dtype[int_]]legx: ndarray[Any, dtype[int_]]
def legline(off, scl): ...def legfromroots(roots): ...def legadd(c1, c2): ...def legsub(c1, c2): ...def legmulx(c): ...def legmul(c1, c2): ...def legdiv(c1, c2): ...def legpow(c, pow, maxpower=...): ...def legder(c, m=..., scl=..., axis=...): ...def legint(c, m=..., k = ..., lbnd=..., scl=..., axis=...): ...def legval(x, c, tensor=...): ...def legval2d(x, y, c): ...def leggrid2d(x, y, c): ...def legval3d(x, y, z, c): ...def leggrid3d(x, y, z, c): ...def legvander(x, deg): ...def legvander2d(x, y, deg): ...def legvander3d(x, y, z, deg): ...def legfit(x, y, deg, rcond=..., full=..., w=...): ...def legcompanion(c): ...def legroots(c): ...def leggauss(deg): ...def legweight(x): ...
class Legendre(ABCPolyBase): domain: Any window: Any basis_name: Any123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542"""=================================================Power Series (:mod:`numpy.polynomial.polynomial`)=================================================
This module provides a number of objects (mostly functions) useful fordealing with polynomials, including a `Polynomial` class thatencapsulates the usual arithmetic operations. (General informationon how this module represents and works with polynomial objects is inthe docstring for its "parent" sub-package, `numpy.polynomial`).
Classes-------.. autosummary:: :toctree: generated/
Polynomial
Constants---------.. autosummary:: :toctree: generated/
polydomain polyzero polyone polyx
Arithmetic----------.. autosummary:: :toctree: generated/
polyadd polysub polymulx polymul polydiv polypow polyval polyval2d polyval3d polygrid2d polygrid3d
Calculus--------.. autosummary:: :toctree: generated/
polyder polyint
Misc Functions--------------.. autosummary:: :toctree: generated/
polyfromroots polyroots polyvalfromroots polyvander polyvander2d polyvander3d polycompanion polyfit polytrim polyline
See Also--------`numpy.polynomial`
"""__all__ = [ 'polyzero', 'polyone', 'polyx', 'polydomain', 'polyline', 'polyadd', 'polysub', 'polymulx', 'polymul', 'polydiv', 'polypow', 'polyval', 'polyvalfromroots', 'polyder', 'polyint', 'polyfromroots', 'polyvander', 'polyfit', 'polytrim', 'polyroots', 'Polynomial', 'polyval2d', 'polyval3d', 'polygrid2d', 'polygrid3d', 'polyvander2d', 'polyvander3d']
import numpy as npimport numpy.linalg as lafrom numpy.core.multiarray import normalize_axis_index
from . import polyutils as pufrom ._polybase import ABCPolyBase
polytrim = pu.trimcoef
## These are constant arrays are of integer type so as to be compatible# with the widest range of other types, such as Decimal.#
# Polynomial default domain.polydomain = np.array([-1, 1])
# Polynomial coefficients representing zero.polyzero = np.array([0])
# Polynomial coefficients representing one.polyone = np.array([1])
# Polynomial coefficients representing the identity x.polyx = np.array([0, 1])
## Polynomial series functions#
def polyline(off, scl): """ Returns an array representing a linear polynomial.
Parameters ---------- off, scl : scalars The "y-intercept" and "slope" of the line, respectively.
Returns ------- y : ndarray This module's representation of the linear polynomial ``off + scl*x``.
See Also -------- numpy.polynomial.chebyshev.chebline numpy.polynomial.legendre.legline numpy.polynomial.laguerre.lagline numpy.polynomial.hermite.hermline numpy.polynomial.hermite_e.hermeline
Examples -------- >>> from numpy.polynomial import polynomial as P >>> P.polyline(1,-1) array([ 1, -1]) >>> P.polyval(1, P.polyline(1,-1)) # should be 0 0.0
""" if scl != 0: return np.array([off, scl]) else: return np.array([off])
def polyfromroots(roots): """ Generate a monic polynomial with given roots.
Return the coefficients of the polynomial
.. math:: p(x) = (x - r_0) * (x - r_1) * ... * (x - r_n),
where the ``r_n`` are the roots specified in `roots`. If a zero has multiplicity n, then it must appear in `roots` n times. For instance, if 2 is a root of multiplicity three and 3 is a root of multiplicity 2, then `roots` looks something like [2, 2, 2, 3, 3]. The roots can appear in any order.
If the returned coefficients are `c`, then
.. math:: p(x) = c_0 + c_1 * x + ... + x^n
The coefficient of the last term is 1 for monic polynomials in this form.
Parameters ---------- roots : array_like Sequence containing the roots.
Returns ------- out : ndarray 1-D array of the polynomial's coefficients If all the roots are real, then `out` is also real, otherwise it is complex. (see Examples below).
See Also -------- numpy.polynomial.chebyshev.chebfromroots numpy.polynomial.legendre.legfromroots numpy.polynomial.laguerre.lagfromroots numpy.polynomial.hermite.hermfromroots numpy.polynomial.hermite_e.hermefromroots
Notes ----- The coefficients are determined by multiplying together linear factors of the form ``(x - r_i)``, i.e.
.. math:: p(x) = (x - r_0) (x - r_1) ... (x - r_n)
where ``n == len(roots) - 1``; note that this implies that ``1`` is always returned for :math:`a_n`.
Examples -------- >>> from numpy.polynomial import polynomial as P >>> P.polyfromroots((-1,0,1)) # x(x - 1)(x + 1) = x^3 - x array([ 0., -1., 0., 1.]) >>> j = complex(0,1) >>> P.polyfromroots((-j,j)) # complex returned, though values are real array([1.+0.j, 0.+0.j, 1.+0.j])
""" return pu._fromroots(polyline, polymul, roots)
def polyadd(c1, c2): """ Add one polynomial to another.
Returns the sum of two polynomials `c1` + `c2`. The arguments are sequences of coefficients from lowest order term to highest, i.e., [1,2,3] represents the polynomial ``1 + 2*x + 3*x**2``.
Parameters ---------- c1, c2 : array_like 1-D arrays of polynomial coefficients ordered from low to high.
Returns ------- out : ndarray The coefficient array representing their sum.
See Also -------- polysub, polymulx, polymul, polydiv, polypow
Examples -------- >>> from numpy.polynomial import polynomial as P >>> c1 = (1,2,3) >>> c2 = (3,2,1) >>> sum = P.polyadd(c1,c2); sum array([4., 4., 4.]) >>> P.polyval(2, sum) # 4 + 4(2) + 4(2**2) 28.0
""" return pu._add(c1, c2)
def polysub(c1, c2): """ Subtract one polynomial from another.
Returns the difference of two polynomials `c1` - `c2`. The arguments are sequences of coefficients from lowest order term to highest, i.e., [1,2,3] represents the polynomial ``1 + 2*x + 3*x**2``.
Parameters ---------- c1, c2 : array_like 1-D arrays of polynomial coefficients ordered from low to high.
Returns ------- out : ndarray Of coefficients representing their difference.
See Also -------- polyadd, polymulx, polymul, polydiv, polypow
Examples -------- >>> from numpy.polynomial import polynomial as P >>> c1 = (1,2,3) >>> c2 = (3,2,1) >>> P.polysub(c1,c2) array([-2., 0., 2.]) >>> P.polysub(c2,c1) # -P.polysub(c1,c2) array([ 2., 0., -2.])
""" return pu._sub(c1, c2)
def polymulx(c): """Multiply a polynomial by x.
Multiply the polynomial `c` by x, where x is the independent variable.
Parameters ---------- c : array_like 1-D array of polynomial coefficients ordered from low to high.
Returns ------- out : ndarray Array representing the result of the multiplication.
See Also -------- polyadd, polysub, polymul, polydiv, polypow
Notes -----
.. versionadded:: 1.5.0
""" # c is a trimmed copy [c] = pu.as_series([c]) # The zero series needs special treatment if len(c) == 1 and c[0] == 0: return c
prd = np.empty(len(c) + 1, dtype=c.dtype) prd[0] = c[0]*0 prd[1:] = c return prd
def polymul(c1, c2): """ Multiply one polynomial by another.
Returns the product of two polynomials `c1` * `c2`. The arguments are sequences of coefficients, from lowest order term to highest, e.g., [1,2,3] represents the polynomial ``1 + 2*x + 3*x**2.``
Parameters ---------- c1, c2 : array_like 1-D arrays of coefficients representing a polynomial, relative to the "standard" basis, and ordered from lowest order term to highest.
Returns ------- out : ndarray Of the coefficients of their product.
See Also -------- polyadd, polysub, polymulx, polydiv, polypow
Examples -------- >>> from numpy.polynomial import polynomial as P >>> c1 = (1,2,3) >>> c2 = (3,2,1) >>> P.polymul(c1,c2) array([ 3., 8., 14., 8., 3.])
""" # c1, c2 are trimmed copies [c1, c2] = pu.as_series([c1, c2]) ret = np.convolve(c1, c2) return pu.trimseq(ret)
def polydiv(c1, c2): """ Divide one polynomial by another.
Returns the quotient-with-remainder of two polynomials `c1` / `c2`. The arguments are sequences of coefficients, from lowest order term to highest, e.g., [1,2,3] represents ``1 + 2*x + 3*x**2``.
Parameters ---------- c1, c2 : array_like 1-D arrays of polynomial coefficients ordered from low to high.
Returns ------- [quo, rem] : ndarrays Of coefficient series representing the quotient and remainder.
See Also -------- polyadd, polysub, polymulx, polymul, polypow
Examples -------- >>> from numpy.polynomial import polynomial as P >>> c1 = (1,2,3) >>> c2 = (3,2,1) >>> P.polydiv(c1,c2) (array([3.]), array([-8., -4.])) >>> P.polydiv(c2,c1) (array([ 0.33333333]), array([ 2.66666667, 1.33333333])) # may vary
""" # c1, c2 are trimmed copies [c1, c2] = pu.as_series([c1, c2]) if c2[-1] == 0: raise ZeroDivisionError()
# note: this is more efficient than `pu._div(polymul, c1, c2)` lc1 = len(c1) lc2 = len(c2) if lc1 < lc2: return c1[:1]*0, c1 elif lc2 == 1: return c1/c2[-1], c1[:1]*0 else: dlen = lc1 - lc2 scl = c2[-1] c2 = c2[:-1]/scl i = dlen j = lc1 - 1 while i >= 0: c1[i:j] -= c2*c1[j] i -= 1 j -= 1 return c1[j+1:]/scl, pu.trimseq(c1[:j+1])
def polypow(c, pow, maxpower=None): """Raise a polynomial to a power.
Returns the polynomial `c` raised to the power `pow`. The argument `c` is a sequence of coefficients ordered from low to high. i.e., [1,2,3] is the series ``1 + 2*x + 3*x**2.``
Parameters ---------- c : array_like 1-D array of array of series coefficients ordered from low to high degree. pow : integer Power to which the series will be raised maxpower : integer, optional Maximum power allowed. This is mainly to limit growth of the series to unmanageable size. Default is 16
Returns ------- coef : ndarray Power series of power.
See Also -------- polyadd, polysub, polymulx, polymul, polydiv
Examples -------- >>> from numpy.polynomial import polynomial as P >>> P.polypow([1,2,3], 2) array([ 1., 4., 10., 12., 9.])
""" # note: this is more efficient than `pu._pow(polymul, c1, c2)`, as it # avoids calling `as_series` repeatedly return pu._pow(np.convolve, c, pow, maxpower)
def polyder(c, m=1, scl=1, axis=0): """ Differentiate a polynomial.
Returns the polynomial coefficients `c` differentiated `m` times along `axis`. At each iteration the result is multiplied by `scl` (the scaling factor is for use in a linear change of variable). The argument `c` is an array of coefficients from low to high degree along each axis, e.g., [1,2,3] represents the polynomial ``1 + 2*x + 3*x**2`` while [[1,2],[1,2]] represents ``1 + 1*x + 2*y + 2*x*y`` if axis=0 is ``x`` and axis=1 is ``y``.
Parameters ---------- c : array_like Array of polynomial coefficients. If c is multidimensional the different axis correspond to different variables with the degree in each axis given by the corresponding index. m : int, optional Number of derivatives taken, must be non-negative. (Default: 1) scl : scalar, optional Each differentiation is multiplied by `scl`. The end result is multiplication by ``scl**m``. This is for use in a linear change of variable. (Default: 1) axis : int, optional Axis over which the derivative is taken. (Default: 0).
.. versionadded:: 1.7.0
Returns ------- der : ndarray Polynomial coefficients of the derivative.
See Also -------- polyint
Examples -------- >>> from numpy.polynomial import polynomial as P >>> c = (1,2,3,4) # 1 + 2x + 3x**2 + 4x**3 >>> P.polyder(c) # (d/dx)(c) = 2 + 6x + 12x**2 array([ 2., 6., 12.]) >>> P.polyder(c,3) # (d**3/dx**3)(c) = 24 array([24.]) >>> P.polyder(c,scl=-1) # (d/d(-x))(c) = -2 - 6x - 12x**2 array([ -2., -6., -12.]) >>> P.polyder(c,2,-1) # (d**2/d(-x)**2)(c) = 6 + 24x array([ 6., 24.])
""" c = np.array(c, ndmin=1, copy=True) if c.dtype.char in '?bBhHiIlLqQpP': # astype fails with NA c = c + 0.0 cdt = c.dtype cnt = pu._deprecate_as_int(m, "the order of derivation") iaxis = pu._deprecate_as_int(axis, "the axis") if cnt < 0: raise ValueError("The order of derivation must be non-negative") iaxis = normalize_axis_index(iaxis, c.ndim)
if cnt == 0: return c
c = np.moveaxis(c, iaxis, 0) n = len(c) if cnt >= n: c = c[:1]*0 else: for i in range(cnt): n = n - 1 c *= scl der = np.empty((n,) + c.shape[1:], dtype=cdt) for j in range(n, 0, -1): der[j - 1] = j*c[j] c = der c = np.moveaxis(c, 0, iaxis) return c
def polyint(c, m=1, k=[], lbnd=0, scl=1, axis=0): """ Integrate a polynomial.
Returns the polynomial coefficients `c` integrated `m` times from `lbnd` along `axis`. At each iteration the resulting series is **multiplied** by `scl` and an integration constant, `k`, is added. The scaling factor is for use in a linear change of variable. ("Buyer beware": note that, depending on what one is doing, one may want `scl` to be the reciprocal of what one might expect; for more information, see the Notes section below.) The argument `c` is an array of coefficients, from low to high degree along each axis, e.g., [1,2,3] represents the polynomial ``1 + 2*x + 3*x**2`` while [[1,2],[1,2]] represents ``1 + 1*x + 2*y + 2*x*y`` if axis=0 is ``x`` and axis=1 is ``y``.
Parameters ---------- c : array_like 1-D array of polynomial coefficients, ordered from low to high. m : int, optional Order of integration, must be positive. (Default: 1) k : {[], list, scalar}, optional Integration constant(s). The value of the first integral at zero is the first value in the list, the value of the second integral at zero is the second value, etc. If ``k == []`` (the default), all constants are set to zero. If ``m == 1``, a single scalar can be given instead of a list. lbnd : scalar, optional The lower bound of the integral. (Default: 0) scl : scalar, optional Following each integration the result is *multiplied* by `scl` before the integration constant is added. (Default: 1) axis : int, optional Axis over which the integral is taken. (Default: 0).
.. versionadded:: 1.7.0
Returns ------- S : ndarray Coefficient array of the integral.
Raises ------ ValueError If ``m < 1``, ``len(k) > m``, ``np.ndim(lbnd) != 0``, or ``np.ndim(scl) != 0``.
See Also -------- polyder
Notes ----- Note that the result of each integration is *multiplied* by `scl`. Why is this important to note? Say one is making a linear change of variable :math:`u = ax + b` in an integral relative to `x`. Then :math:`dx = du/a`, so one will need to set `scl` equal to :math:`1/a` - perhaps not what one would have first thought.
Examples -------- >>> from numpy.polynomial import polynomial as P >>> c = (1,2,3) >>> P.polyint(c) # should return array([0, 1, 1, 1]) array([0., 1., 1., 1.]) >>> P.polyint(c,3) # should return array([0, 0, 0, 1/6, 1/12, 1/20]) array([ 0. , 0. , 0. , 0.16666667, 0.08333333, # may vary 0.05 ]) >>> P.polyint(c,k=3) # should return array([3, 1, 1, 1]) array([3., 1., 1., 1.]) >>> P.polyint(c,lbnd=-2) # should return array([6, 1, 1, 1]) array([6., 1., 1., 1.]) >>> P.polyint(c,scl=-2) # should return array([0, -2, -2, -2]) array([ 0., -2., -2., -2.])
""" c = np.array(c, ndmin=1, copy=True) if c.dtype.char in '?bBhHiIlLqQpP': # astype doesn't preserve mask attribute. c = c + 0.0 cdt = c.dtype if not np.iterable(k): k = [k] cnt = pu._deprecate_as_int(m, "the order of integration") iaxis = pu._deprecate_as_int(axis, "the axis") if cnt < 0: raise ValueError("The order of integration must be non-negative") if len(k) > cnt: raise ValueError("Too many integration constants") if np.ndim(lbnd) != 0: raise ValueError("lbnd must be a scalar.") if np.ndim(scl) != 0: raise ValueError("scl must be a scalar.") iaxis = normalize_axis_index(iaxis, c.ndim)
if cnt == 0: return c
k = list(k) + [0]*(cnt - len(k)) c = np.moveaxis(c, iaxis, 0) for i in range(cnt): n = len(c) c *= scl if n == 1 and np.all(c[0] == 0): c[0] += k[i] else: tmp = np.empty((n + 1,) + c.shape[1:], dtype=cdt) tmp[0] = c[0]*0 tmp[1] = c[0] for j in range(1, n): tmp[j + 1] = c[j]/(j + 1) tmp[0] += k[i] - polyval(lbnd, tmp) c = tmp c = np.moveaxis(c, 0, iaxis) return c
def polyval(x, c, tensor=True): """ Evaluate a polynomial at points x.
If `c` is of length `n + 1`, this function returns the value
.. math:: p(x) = c_0 + c_1 * x + ... + c_n * x^n
The parameter `x` is converted to an array only if it is a tuple or a list, otherwise it is treated as a scalar. In either case, either `x` or its elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` is a 1-D array, then `p(x)` will have the same shape as `x`. If `c` is multidimensional, then the shape of the result depends on the value of `tensor`. If `tensor` is true the shape will be c.shape[1:] + x.shape. If `tensor` is false the shape will be c.shape[1:]. Note that scalars have shape (,).
Trailing zeros in the coefficients will be used in the evaluation, so they should be avoided if efficiency is a concern.
Parameters ---------- x : array_like, compatible object If `x` is a list or tuple, it is converted to an ndarray, otherwise it is left unchanged and treated as a scalar. In either case, `x` or its elements must support addition and multiplication with with themselves and with the elements of `c`. c : array_like Array of coefficients ordered so that the coefficients for terms of degree n are contained in c[n]. If `c` is multidimensional the remaining indices enumerate multiple polynomials. In the two dimensional case the coefficients may be thought of as stored in the columns of `c`. tensor : boolean, optional If True, the shape of the coefficient array is extended with ones on the right, one for each dimension of `x`. Scalars have dimension 0 for this action. The result is that every column of coefficients in `c` is evaluated for every element of `x`. If False, `x` is broadcast over the columns of `c` for the evaluation. This keyword is useful when `c` is multidimensional. The default value is True.
.. versionadded:: 1.7.0
Returns ------- values : ndarray, compatible object The shape of the returned array is described above.
See Also -------- polyval2d, polygrid2d, polyval3d, polygrid3d
Notes ----- The evaluation uses Horner's method.
Examples -------- >>> from numpy.polynomial.polynomial import polyval >>> polyval(1, [1,2,3]) 6.0 >>> a = np.arange(4).reshape(2,2) >>> a array([[0, 1], [2, 3]]) >>> polyval(a, [1,2,3]) array([[ 1., 6.], [17., 34.]]) >>> coef = np.arange(4).reshape(2,2) # multidimensional coefficients >>> coef array([[0, 1], [2, 3]]) >>> polyval([1,2], coef, tensor=True) array([[2., 4.], [4., 7.]]) >>> polyval([1,2], coef, tensor=False) array([2., 7.])
""" c = np.array(c, ndmin=1, copy=False) if c.dtype.char in '?bBhHiIlLqQpP': # astype fails with NA c = c + 0.0 if isinstance(x, (tuple, list)): x = np.asarray(x) if isinstance(x, np.ndarray) and tensor: c = c.reshape(c.shape + (1,)*x.ndim)
c0 = c[-1] + x*0 for i in range(2, len(c) + 1): c0 = c[-i] + c0*x return c0
def polyvalfromroots(x, r, tensor=True): """ Evaluate a polynomial specified by its roots at points x.
If `r` is of length `N`, this function returns the value
.. math:: p(x) = \\prod_{n=1}^{N} (x - r_n)
The parameter `x` is converted to an array only if it is a tuple or a list, otherwise it is treated as a scalar. In either case, either `x` or its elements must support multiplication and addition both with themselves and with the elements of `r`.
If `r` is a 1-D array, then `p(x)` will have the same shape as `x`. If `r` is multidimensional, then the shape of the result depends on the value of `tensor`. If `tensor` is ``True`` the shape will be r.shape[1:] + x.shape; that is, each polynomial is evaluated at every value of `x`. If `tensor` is ``False``, the shape will be r.shape[1:]; that is, each polynomial is evaluated only for the corresponding broadcast value of `x`. Note that scalars have shape (,).
.. versionadded:: 1.12
Parameters ---------- x : array_like, compatible object If `x` is a list or tuple, it is converted to an ndarray, otherwise it is left unchanged and treated as a scalar. In either case, `x` or its elements must support addition and multiplication with with themselves and with the elements of `r`. r : array_like Array of roots. If `r` is multidimensional the first index is the root index, while the remaining indices enumerate multiple polynomials. For instance, in the two dimensional case the roots of each polynomial may be thought of as stored in the columns of `r`. tensor : boolean, optional If True, the shape of the roots array is extended with ones on the right, one for each dimension of `x`. Scalars have dimension 0 for this action. The result is that every column of coefficients in `r` is evaluated for every element of `x`. If False, `x` is broadcast over the columns of `r` for the evaluation. This keyword is useful when `r` is multidimensional. The default value is True.
Returns ------- values : ndarray, compatible object The shape of the returned array is described above.
See Also -------- polyroots, polyfromroots, polyval
Examples -------- >>> from numpy.polynomial.polynomial import polyvalfromroots >>> polyvalfromroots(1, [1,2,3]) 0.0 >>> a = np.arange(4).reshape(2,2) >>> a array([[0, 1], [2, 3]]) >>> polyvalfromroots(a, [-1, 0, 1]) array([[-0., 0.], [ 6., 24.]]) >>> r = np.arange(-2, 2).reshape(2,2) # multidimensional coefficients >>> r # each column of r defines one polynomial array([[-2, -1], [ 0, 1]]) >>> b = [-2, 1] >>> polyvalfromroots(b, r, tensor=True) array([[-0., 3.], [ 3., 0.]]) >>> polyvalfromroots(b, r, tensor=False) array([-0., 0.]) """ r = np.array(r, ndmin=1, copy=False) if r.dtype.char in '?bBhHiIlLqQpP': r = r.astype(np.double) if isinstance(x, (tuple, list)): x = np.asarray(x) if isinstance(x, np.ndarray): if tensor: r = r.reshape(r.shape + (1,)*x.ndim) elif x.ndim >= r.ndim: raise ValueError("x.ndim must be < r.ndim when tensor == False") return np.prod(x - r, axis=0)
def polyval2d(x, y, c): """ Evaluate a 2-D polynomial at points (x, y).
This function returns the value
.. math:: p(x,y) = \\sum_{i,j} c_{i,j} * x^i * y^j
The parameters `x` and `y` are converted to arrays only if they are tuples or a lists, otherwise they are treated as a scalars and they must have the same shape after conversion. In either case, either `x` and `y` or their elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` has fewer than two dimensions, ones are implicitly appended to its shape to make it 2-D. The shape of the result will be c.shape[2:] + x.shape.
Parameters ---------- x, y : array_like, compatible objects The two dimensional series is evaluated at the points `(x, y)`, where `x` and `y` must have the same shape. If `x` or `y` is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and, if it isn't an ndarray, it is treated as a scalar. c : array_like Array of coefficients ordered so that the coefficient of the term of multi-degree i,j is contained in `c[i,j]`. If `c` has dimension greater than two the remaining indices enumerate multiple sets of coefficients.
Returns ------- values : ndarray, compatible object The values of the two dimensional polynomial at points formed with pairs of corresponding values from `x` and `y`.
See Also -------- polyval, polygrid2d, polyval3d, polygrid3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._valnd(polyval, c, x, y)
def polygrid2d(x, y, c): """ Evaluate a 2-D polynomial on the Cartesian product of x and y.
This function returns the values:
.. math:: p(a,b) = \\sum_{i,j} c_{i,j} * a^i * b^j
where the points `(a, b)` consist of all pairs formed by taking `a` from `x` and `b` from `y`. The resulting points form a grid with `x` in the first dimension and `y` in the second.
The parameters `x` and `y` are converted to arrays only if they are tuples or a lists, otherwise they are treated as a scalars. In either case, either `x` and `y` or their elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` has fewer than two dimensions, ones are implicitly appended to its shape to make it 2-D. The shape of the result will be c.shape[2:] + x.shape + y.shape.
Parameters ---------- x, y : array_like, compatible objects The two dimensional series is evaluated at the points in the Cartesian product of `x` and `y`. If `x` or `y` is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and, if it isn't an ndarray, it is treated as a scalar. c : array_like Array of coefficients ordered so that the coefficients for terms of degree i,j are contained in ``c[i,j]``. If `c` has dimension greater than two the remaining indices enumerate multiple sets of coefficients.
Returns ------- values : ndarray, compatible object The values of the two dimensional polynomial at points in the Cartesian product of `x` and `y`.
See Also -------- polyval, polyval2d, polyval3d, polygrid3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._gridnd(polyval, c, x, y)
def polyval3d(x, y, z, c): """ Evaluate a 3-D polynomial at points (x, y, z).
This function returns the values:
.. math:: p(x,y,z) = \\sum_{i,j,k} c_{i,j,k} * x^i * y^j * z^k
The parameters `x`, `y`, and `z` are converted to arrays only if they are tuples or a lists, otherwise they are treated as a scalars and they must have the same shape after conversion. In either case, either `x`, `y`, and `z` or their elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` has fewer than 3 dimensions, ones are implicitly appended to its shape to make it 3-D. The shape of the result will be c.shape[3:] + x.shape.
Parameters ---------- x, y, z : array_like, compatible object The three dimensional series is evaluated at the points `(x, y, z)`, where `x`, `y`, and `z` must have the same shape. If any of `x`, `y`, or `z` is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and if it isn't an ndarray it is treated as a scalar. c : array_like Array of coefficients ordered so that the coefficient of the term of multi-degree i,j,k is contained in ``c[i,j,k]``. If `c` has dimension greater than 3 the remaining indices enumerate multiple sets of coefficients.
Returns ------- values : ndarray, compatible object The values of the multidimensional polynomial on points formed with triples of corresponding values from `x`, `y`, and `z`.
See Also -------- polyval, polyval2d, polygrid2d, polygrid3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._valnd(polyval, c, x, y, z)
def polygrid3d(x, y, z, c): """ Evaluate a 3-D polynomial on the Cartesian product of x, y and z.
This function returns the values:
.. math:: p(a,b,c) = \\sum_{i,j,k} c_{i,j,k} * a^i * b^j * c^k
where the points `(a, b, c)` consist of all triples formed by taking `a` from `x`, `b` from `y`, and `c` from `z`. The resulting points form a grid with `x` in the first dimension, `y` in the second, and `z` in the third.
The parameters `x`, `y`, and `z` are converted to arrays only if they are tuples or a lists, otherwise they are treated as a scalars. In either case, either `x`, `y`, and `z` or their elements must support multiplication and addition both with themselves and with the elements of `c`.
If `c` has fewer than three dimensions, ones are implicitly appended to its shape to make it 3-D. The shape of the result will be c.shape[3:] + x.shape + y.shape + z.shape.
Parameters ---------- x, y, z : array_like, compatible objects The three dimensional series is evaluated at the points in the Cartesian product of `x`, `y`, and `z`. If `x`,`y`, or `z` is a list or tuple, it is first converted to an ndarray, otherwise it is left unchanged and, if it isn't an ndarray, it is treated as a scalar. c : array_like Array of coefficients ordered so that the coefficients for terms of degree i,j are contained in ``c[i,j]``. If `c` has dimension greater than two the remaining indices enumerate multiple sets of coefficients.
Returns ------- values : ndarray, compatible object The values of the two dimensional polynomial at points in the Cartesian product of `x` and `y`.
See Also -------- polyval, polyval2d, polygrid2d, polyval3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._gridnd(polyval, c, x, y, z)
def polyvander(x, deg): """Vandermonde matrix of given degree.
Returns the Vandermonde matrix of degree `deg` and sample points `x`. The Vandermonde matrix is defined by
.. math:: V[..., i] = x^i,
where `0 <= i <= deg`. The leading indices of `V` index the elements of `x` and the last index is the power of `x`.
If `c` is a 1-D array of coefficients of length `n + 1` and `V` is the matrix ``V = polyvander(x, n)``, then ``np.dot(V, c)`` and ``polyval(x, c)`` are the same up to roundoff. This equivalence is useful both for least squares fitting and for the evaluation of a large number of polynomials of the same degree and sample points.
Parameters ---------- x : array_like Array of points. The dtype is converted to float64 or complex128 depending on whether any of the elements are complex. If `x` is scalar it is converted to a 1-D array. deg : int Degree of the resulting matrix.
Returns ------- vander : ndarray. The Vandermonde matrix. The shape of the returned matrix is ``x.shape + (deg + 1,)``, where the last index is the power of `x`. The dtype will be the same as the converted `x`.
See Also -------- polyvander2d, polyvander3d
""" ideg = pu._deprecate_as_int(deg, "deg") if ideg < 0: raise ValueError("deg must be non-negative")
x = np.array(x, copy=False, ndmin=1) + 0.0 dims = (ideg + 1,) + x.shape dtyp = x.dtype v = np.empty(dims, dtype=dtyp) v[0] = x*0 + 1 if ideg > 0: v[1] = x for i in range(2, ideg + 1): v[i] = v[i-1]*x return np.moveaxis(v, 0, -1)
def polyvander2d(x, y, deg): """Pseudo-Vandermonde matrix of given degrees.
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample points `(x, y)`. The pseudo-Vandermonde matrix is defined by
.. math:: V[..., (deg[1] + 1)*i + j] = x^i * y^j,
where `0 <= i <= deg[0]` and `0 <= j <= deg[1]`. The leading indices of `V` index the points `(x, y)` and the last index encodes the powers of `x` and `y`.
If ``V = polyvander2d(x, y, [xdeg, ydeg])``, then the columns of `V` correspond to the elements of a 2-D coefficient array `c` of shape (xdeg + 1, ydeg + 1) in the order
.. math:: c_{00}, c_{01}, c_{02} ... , c_{10}, c_{11}, c_{12} ...
and ``np.dot(V, c.flat)`` and ``polyval2d(x, y, c)`` will be the same up to roundoff. This equivalence is useful both for least squares fitting and for the evaluation of a large number of 2-D polynomials of the same degrees and sample points.
Parameters ---------- x, y : array_like Arrays of point coordinates, all of the same shape. The dtypes will be converted to either float64 or complex128 depending on whether any of the elements are complex. Scalars are converted to 1-D arrays. deg : list of ints List of maximum degrees of the form [x_deg, y_deg].
Returns ------- vander2d : ndarray The shape of the returned matrix is ``x.shape + (order,)``, where :math:`order = (deg[0]+1)*(deg([1]+1)`. The dtype will be the same as the converted `x` and `y`.
See Also -------- polyvander, polyvander3d, polyval2d, polyval3d
""" return pu._vander_nd_flat((polyvander, polyvander), (x, y), deg)
def polyvander3d(x, y, z, deg): """Pseudo-Vandermonde matrix of given degrees.
Returns the pseudo-Vandermonde matrix of degrees `deg` and sample points `(x, y, z)`. If `l, m, n` are the given degrees in `x, y, z`, then The pseudo-Vandermonde matrix is defined by
.. math:: V[..., (m+1)(n+1)i + (n+1)j + k] = x^i * y^j * z^k,
where `0 <= i <= l`, `0 <= j <= m`, and `0 <= j <= n`. The leading indices of `V` index the points `(x, y, z)` and the last index encodes the powers of `x`, `y`, and `z`.
If ``V = polyvander3d(x, y, z, [xdeg, ydeg, zdeg])``, then the columns of `V` correspond to the elements of a 3-D coefficient array `c` of shape (xdeg + 1, ydeg + 1, zdeg + 1) in the order
.. math:: c_{000}, c_{001}, c_{002},... , c_{010}, c_{011}, c_{012},...
and ``np.dot(V, c.flat)`` and ``polyval3d(x, y, z, c)`` will be the same up to roundoff. This equivalence is useful both for least squares fitting and for the evaluation of a large number of 3-D polynomials of the same degrees and sample points.
Parameters ---------- x, y, z : array_like Arrays of point coordinates, all of the same shape. The dtypes will be converted to either float64 or complex128 depending on whether any of the elements are complex. Scalars are converted to 1-D arrays. deg : list of ints List of maximum degrees of the form [x_deg, y_deg, z_deg].
Returns ------- vander3d : ndarray The shape of the returned matrix is ``x.shape + (order,)``, where :math:`order = (deg[0]+1)*(deg([1]+1)*(deg[2]+1)`. The dtype will be the same as the converted `x`, `y`, and `z`.
See Also -------- polyvander, polyvander3d, polyval2d, polyval3d
Notes -----
.. versionadded:: 1.7.0
""" return pu._vander_nd_flat((polyvander, polyvander, polyvander), (x, y, z), deg)
def polyfit(x, y, deg, rcond=None, full=False, w=None): """ Least-squares fit of a polynomial to data.
Return the coefficients of a polynomial of degree `deg` that is the least squares fit to the data values `y` given at points `x`. If `y` is 1-D the returned coefficients will also be 1-D. If `y` is 2-D multiple fits are done, one for each column of `y`, and the resulting coefficients are stored in the corresponding columns of a 2-D return. The fitted polynomial(s) are in the form
.. math:: p(x) = c_0 + c_1 * x + ... + c_n * x^n,
where `n` is `deg`.
Parameters ---------- x : array_like, shape (`M`,) x-coordinates of the `M` sample (data) points ``(x[i], y[i])``. y : array_like, shape (`M`,) or (`M`, `K`) y-coordinates of the sample points. Several sets of sample points sharing the same x-coordinates can be (independently) fit with one call to `polyfit` by passing in for `y` a 2-D array that contains one data set per column. deg : int or 1-D array_like Degree(s) of the fitting polynomials. If `deg` is a single integer all terms up to and including the `deg`'th term are included in the fit. For NumPy versions >= 1.11.0 a list of integers specifying the degrees of the terms to include may be used instead. rcond : float, optional Relative condition number of the fit. Singular values smaller than `rcond`, relative to the largest singular value, will be ignored. The default value is ``len(x)*eps``, where `eps` is the relative precision of the platform's float type, about 2e-16 in most cases. full : bool, optional Switch determining the nature of the return value. When ``False`` (the default) just the coefficients are returned; when ``True``, diagnostic information from the singular value decomposition (used to solve the fit's matrix equation) is also returned. w : array_like, shape (`M`,), optional Weights. If not None, the weight ``w[i]`` applies to the unsquared residual ``y[i] - y_hat[i]`` at ``x[i]``. Ideally the weights are chosen so that the errors of the products ``w[i]*y[i]`` all have the same variance. When using inverse-variance weighting, use ``w[i] = 1/sigma(y[i])``. The default value is None.
.. versionadded:: 1.5.0
Returns ------- coef : ndarray, shape (`deg` + 1,) or (`deg` + 1, `K`) Polynomial coefficients ordered from low to high. If `y` was 2-D, the coefficients in column `k` of `coef` represent the polynomial fit to the data in `y`'s `k`-th column.
[residuals, rank, singular_values, rcond] : list These values are only returned if ``full == True``
- residuals -- sum of squared residuals of the least squares fit - rank -- the numerical rank of the scaled Vandermonde matrix - singular_values -- singular values of the scaled Vandermonde matrix - rcond -- value of `rcond`.
For more details, see `numpy.linalg.lstsq`.
Raises ------ RankWarning Raised if the matrix in the least-squares fit is rank deficient. The warning is only raised if ``full == False``. The warnings can be turned off by:
>>> import warnings >>> warnings.simplefilter('ignore', np.RankWarning)
See Also -------- numpy.polynomial.chebyshev.chebfit numpy.polynomial.legendre.legfit numpy.polynomial.laguerre.lagfit numpy.polynomial.hermite.hermfit numpy.polynomial.hermite_e.hermefit polyval : Evaluates a polynomial. polyvander : Vandermonde matrix for powers. numpy.linalg.lstsq : Computes a least-squares fit from the matrix. scipy.interpolate.UnivariateSpline : Computes spline fits.
Notes ----- The solution is the coefficients of the polynomial `p` that minimizes the sum of the weighted squared errors
.. math:: E = \\sum_j w_j^2 * |y_j - p(x_j)|^2,
where the :math:`w_j` are the weights. This problem is solved by setting up the (typically) over-determined matrix equation:
.. math:: V(x) * c = w * y,
where `V` is the weighted pseudo Vandermonde matrix of `x`, `c` are the coefficients to be solved for, `w` are the weights, and `y` are the observed values. This equation is then solved using the singular value decomposition of `V`.
If some of the singular values of `V` are so small that they are neglected (and `full` == ``False``), a `RankWarning` will be raised. This means that the coefficient values may be poorly determined. Fitting to a lower order polynomial will usually get rid of the warning (but may not be what you want, of course; if you have independent reason(s) for choosing the degree which isn't working, you may have to: a) reconsider those reasons, and/or b) reconsider the quality of your data). The `rcond` parameter can also be set to a value smaller than its default, but the resulting fit may be spurious and have large contributions from roundoff error.
Polynomial fits using double precision tend to "fail" at about (polynomial) degree 20. Fits using Chebyshev or Legendre series are generally better conditioned, but much can still depend on the distribution of the sample points and the smoothness of the data. If the quality of the fit is inadequate, splines may be a good alternative.
Examples -------- >>> np.random.seed(123) >>> from numpy.polynomial import polynomial as P >>> x = np.linspace(-1,1,51) # x "data": [-1, -0.96, ..., 0.96, 1] >>> y = x**3 - x + np.random.randn(len(x)) # x^3 - x + Gaussian noise >>> c, stats = P.polyfit(x,y,3,full=True) >>> np.random.seed(123) >>> c # c[0], c[2] should be approx. 0, c[1] approx. -1, c[3] approx. 1 array([ 0.01909725, -1.30598256, -0.00577963, 1.02644286]) # may vary >>> stats # note the large SSR, explaining the rather poor results [array([ 38.06116253]), 4, array([ 1.38446749, 1.32119158, 0.50443316, # may vary 0.28853036]), 1.1324274851176597e-014]
Same thing without the added noise
>>> y = x**3 - x >>> c, stats = P.polyfit(x,y,3,full=True) >>> c # c[0], c[2] should be "very close to 0", c[1] ~= -1, c[3] ~= 1 array([-6.36925336e-18, -1.00000000e+00, -4.08053781e-16, 1.00000000e+00]) >>> stats # note the minuscule SSR [array([ 7.46346754e-31]), 4, array([ 1.38446749, 1.32119158, # may vary 0.50443316, 0.28853036]), 1.1324274851176597e-014]
""" return pu._fit(polyvander, x, y, deg, rcond, full, w)
def polycompanion(c): """ Return the companion matrix of c.
The companion matrix for power series cannot be made symmetric by scaling the basis, so this function differs from those for the orthogonal polynomials.
Parameters ---------- c : array_like 1-D array of polynomial coefficients ordered from low to high degree.
Returns ------- mat : ndarray Companion matrix of dimensions (deg, deg).
Notes -----
.. versionadded:: 1.7.0
""" # c is a trimmed copy [c] = pu.as_series([c]) if len(c) < 2: raise ValueError('Series must have maximum degree of at least 1.') if len(c) == 2: return np.array([[-c[0]/c[1]]])
n = len(c) - 1 mat = np.zeros((n, n), dtype=c.dtype) bot = mat.reshape(-1)[n::n+1] bot[...] = 1 mat[:, -1] -= c[:-1]/c[-1] return mat
def polyroots(c): """ Compute the roots of a polynomial.
Return the roots (a.k.a. "zeros") of the polynomial
.. math:: p(x) = \\sum_i c[i] * x^i.
Parameters ---------- c : 1-D array_like 1-D array of polynomial coefficients.
Returns ------- out : ndarray Array of the roots of the polynomial. If all the roots are real, then `out` is also real, otherwise it is complex.
See Also -------- numpy.polynomial.chebyshev.chebroots numpy.polynomial.legendre.legroots numpy.polynomial.laguerre.lagroots numpy.polynomial.hermite.hermroots numpy.polynomial.hermite_e.hermeroots
Notes ----- The root estimates are obtained as the eigenvalues of the companion matrix, Roots far from the origin of the complex plane may have large errors due to the numerical instability of the power series for such values. Roots with multiplicity greater than 1 will also show larger errors as the value of the series near such points is relatively insensitive to errors in the roots. Isolated roots near the origin can be improved by a few iterations of Newton's method.
Examples -------- >>> import numpy.polynomial.polynomial as poly >>> poly.polyroots(poly.polyfromroots((-1,0,1))) array([-1., 0., 1.]) >>> poly.polyroots(poly.polyfromroots((-1,0,1))).dtype dtype('float64') >>> j = complex(0,1) >>> poly.polyroots(poly.polyfromroots((-j,0,j))) array([ 0.00000000e+00+0.j, 0.00000000e+00+1.j, 2.77555756e-17-1.j]) # may vary
""" # c is a trimmed copy [c] = pu.as_series([c]) if len(c) < 2: return np.array([], dtype=c.dtype) if len(c) == 2: return np.array([-c[0]/c[1]])
# rotated companion matrix reduces error m = polycompanion(c)[::-1,::-1] r = la.eigvals(m) r.sort() return r
## polynomial class#
class Polynomial(ABCPolyBase): """A power series class.
The Polynomial class provides the standard Python numerical methods '+', '-', '*', '//', '%', 'divmod', '**', and '()' as well as the attributes and methods listed in the `ABCPolyBase` documentation.
Parameters ---------- coef : array_like Polynomial coefficients in order of increasing degree, i.e., ``(1, 2, 3)`` give ``1 + 2*x + 3*x**2``. domain : (2,) array_like, optional Domain to use. The interval ``[domain[0], domain[1]]`` is mapped to the interval ``[window[0], window[1]]`` by shifting and scaling. The default value is [-1, 1]. window : (2,) array_like, optional Window, see `domain` for its use. The default value is [-1, 1].
.. versionadded:: 1.6.0 symbol : str, optional Symbol used to represent the independent variable in string representations of the polynomial expression, e.g. for printing. The symbol must be a valid Python identifier. Default value is 'x'.
.. versionadded:: 1.24
""" # Virtual Functions _add = staticmethod(polyadd) _sub = staticmethod(polysub) _mul = staticmethod(polymul) _div = staticmethod(polydiv) _pow = staticmethod(polypow) _val = staticmethod(polyval) _int = staticmethod(polyint) _der = staticmethod(polyder) _fit = staticmethod(polyfit) _line = staticmethod(polyline) _roots = staticmethod(polyroots) _fromroots = staticmethod(polyfromroots)
# Virtual properties domain = np.array(polydomain) window = np.array(polydomain) basis_name = None
@classmethod def _str_term_unicode(cls, i, arg_str): if i == '1': return f"·{arg_str}" else: return f"·{arg_str}{i.translate(cls._superscript_mapping)}"
@staticmethod def _str_term_ascii(i, arg_str): if i == '1': return f" {arg_str}" else: return f" {arg_str}**{i}"
@staticmethod def _repr_latex_term(i, arg_str, needs_parens): if needs_parens: arg_str = rf"\left({arg_str}\right)" if i == 0: return '1' elif i == 1: return arg_str else: return f"{arg_str}^{{{i}}}"1234567891011121314151617181920212223242526272829303132333435363738394041from typing import Any
from numpy import ndarray, dtype, int_from numpy.polynomial._polybase import ABCPolyBasefrom numpy.polynomial.polyutils import trimcoef
__all__: list[str]
polytrim = trimcoef
polydomain: ndarray[Any, dtype[int_]]polyzero: ndarray[Any, dtype[int_]]polyone: ndarray[Any, dtype[int_]]polyx: ndarray[Any, dtype[int_]]
def polyline(off, scl): ...def polyfromroots(roots): ...def polyadd(c1, c2): ...def polysub(c1, c2): ...def polymulx(c): ...def polymul(c1, c2): ...def polydiv(c1, c2): ...def polypow(c, pow, maxpower=...): ...def polyder(c, m=..., scl=..., axis=...): ...def polyint(c, m=..., k=..., lbnd=..., scl=..., axis=...): ...def polyval(x, c, tensor=...): ...def polyvalfromroots(x, r, tensor=...): ...def polyval2d(x, y, c): ...def polygrid2d(x, y, c): ...def polyval3d(x, y, z, c): ...def polygrid3d(x, y, z, c): ...def polyvander(x, deg): ...def polyvander2d(x, y, deg): ...def polyvander3d(x, y, z, deg): ...def polyfit(x, y, deg, rcond=..., full=..., w=...): ...def polyroots(c): ...
class Polynomial(ABCPolyBase): domain: Any window: Any basis_name: Any123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789"""Utility classes and functions for the polynomial modules.
This module provides: error and warning objects; a polynomial base class;and some routines used in both the `polynomial` and `chebyshev` modules.
Warning objects---------------
.. autosummary:: :toctree: generated/
RankWarning raised in least-squares fit for rank-deficient matrix.
Functions---------
.. autosummary:: :toctree: generated/
as_series convert list of array_likes into 1-D arrays of common type. trimseq remove trailing zeros. trimcoef remove small trailing coefficients. getdomain return the domain appropriate for a given set of abscissae. mapdomain maps points between domains. mapparms parameters of the linear map between domains.
"""import operatorimport functoolsimport warnings
import numpy as np
from numpy.core.multiarray import dragon4_positional, dragon4_scientificfrom numpy.core.umath import absolute
__all__ = [ 'RankWarning', 'as_series', 'trimseq', 'trimcoef', 'getdomain', 'mapdomain', 'mapparms', 'format_float']
## Warnings and Exceptions#
class RankWarning(UserWarning): """Issued by chebfit when the design matrix is rank deficient.""" pass
## Helper functions to convert inputs to 1-D arrays#def trimseq(seq): """Remove small Poly series coefficients.
Parameters ---------- seq : sequence Sequence of Poly series coefficients. This routine fails for empty sequences.
Returns ------- series : sequence Subsequence with trailing zeros removed. If the resulting sequence would be empty, return the first element. The returned sequence may or may not be a view.
Notes ----- Do not lose the type info if the sequence contains unknown objects.
""" if len(seq) == 0: return seq else: for i in range(len(seq) - 1, -1, -1): if seq[i] != 0: break return seq[:i+1]
def as_series(alist, trim=True): """ Return argument as a list of 1-d arrays.
The returned list contains array(s) of dtype double, complex double, or object. A 1-d argument of shape ``(N,)`` is parsed into ``N`` arrays of size one; a 2-d argument of shape ``(M,N)`` is parsed into ``M`` arrays of size ``N`` (i.e., is "parsed by row"); and a higher dimensional array raises a Value Error if it is not first reshaped into either a 1-d or 2-d array.
Parameters ---------- alist : array_like A 1- or 2-d array_like trim : boolean, optional When True, trailing zeros are removed from the inputs. When False, the inputs are passed through intact.
Returns ------- [a1, a2,...] : list of 1-D arrays A copy of the input data as a list of 1-d arrays.
Raises ------ ValueError Raised when `as_series` cannot convert its input to 1-d arrays, or at least one of the resulting arrays is empty.
Examples -------- >>> from numpy.polynomial import polyutils as pu >>> a = np.arange(4) >>> pu.as_series(a) [array([0.]), array([1.]), array([2.]), array([3.])] >>> b = np.arange(6).reshape((2,3)) >>> pu.as_series(b) [array([0., 1., 2.]), array([3., 4., 5.])]
>>> pu.as_series((1, np.arange(3), np.arange(2, dtype=np.float16))) [array([1.]), array([0., 1., 2.]), array([0., 1.])]
>>> pu.as_series([2, [1.1, 0.]]) [array([2.]), array([1.1])]
>>> pu.as_series([2, [1.1, 0.]], trim=False) [array([2.]), array([1.1, 0. ])]
""" arrays = [np.array(a, ndmin=1, copy=False) for a in alist] if min([a.size for a in arrays]) == 0: raise ValueError("Coefficient array is empty") if any(a.ndim != 1 for a in arrays): raise ValueError("Coefficient array is not 1-d") if trim: arrays = [trimseq(a) for a in arrays]
if any(a.dtype == np.dtype(object) for a in arrays): ret = [] for a in arrays: if a.dtype != np.dtype(object): tmp = np.empty(len(a), dtype=np.dtype(object)) tmp[:] = a[:] ret.append(tmp) else: ret.append(a.copy()) else: try: dtype = np.common_type(*arrays) except Exception as e: raise ValueError("Coefficient arrays have no common type") from e ret = [np.array(a, copy=True, dtype=dtype) for a in arrays] return ret
def trimcoef(c, tol=0): """ Remove "small" "trailing" coefficients from a polynomial.
"Small" means "small in absolute value" and is controlled by the parameter `tol`; "trailing" means highest order coefficient(s), e.g., in ``[0, 1, 1, 0, 0]`` (which represents ``0 + x + x**2 + 0*x**3 + 0*x**4``) both the 3-rd and 4-th order coefficients would be "trimmed."
Parameters ---------- c : array_like 1-d array of coefficients, ordered from lowest order to highest. tol : number, optional Trailing (i.e., highest order) elements with absolute value less than or equal to `tol` (default value is zero) are removed.
Returns ------- trimmed : ndarray 1-d array with trailing zeros removed. If the resulting series would be empty, a series containing a single zero is returned.
Raises ------ ValueError If `tol` < 0
See Also -------- trimseq
Examples -------- >>> from numpy.polynomial import polyutils as pu >>> pu.trimcoef((0,0,3,0,5,0,0)) array([0., 0., 3., 0., 5.]) >>> pu.trimcoef((0,0,1e-3,0,1e-5,0,0),1e-3) # item == tol is trimmed array([0.]) >>> i = complex(0,1) # works for complex >>> pu.trimcoef((3e-4,1e-3*(1-i),5e-4,2e-5*(1+i)), 1e-3) array([0.0003+0.j , 0.001 -0.001j])
""" if tol < 0: raise ValueError("tol must be non-negative")
[c] = as_series([c]) [ind] = np.nonzero(np.abs(c) > tol) if len(ind) == 0: return c[:1]*0 else: return c[:ind[-1] + 1].copy()
def getdomain(x): """ Return a domain suitable for given abscissae.
Find a domain suitable for a polynomial or Chebyshev series defined at the values supplied.
Parameters ---------- x : array_like 1-d array of abscissae whose domain will be determined.
Returns ------- domain : ndarray 1-d array containing two values. If the inputs are complex, then the two returned points are the lower left and upper right corners of the smallest rectangle (aligned with the axes) in the complex plane containing the points `x`. If the inputs are real, then the two points are the ends of the smallest interval containing the points `x`.
See Also -------- mapparms, mapdomain
Examples -------- >>> from numpy.polynomial import polyutils as pu >>> points = np.arange(4)**2 - 5; points array([-5, -4, -1, 4]) >>> pu.getdomain(points) array([-5., 4.]) >>> c = np.exp(complex(0,1)*np.pi*np.arange(12)/6) # unit circle >>> pu.getdomain(c) array([-1.-1.j, 1.+1.j])
""" [x] = as_series([x], trim=False) if x.dtype.char in np.typecodes['Complex']: rmin, rmax = x.real.min(), x.real.max() imin, imax = x.imag.min(), x.imag.max() return np.array((complex(rmin, imin), complex(rmax, imax))) else: return np.array((x.min(), x.max()))
def mapparms(old, new): """ Linear map parameters between domains.
Return the parameters of the linear map ``offset + scale*x`` that maps `old` to `new` such that ``old[i] -> new[i]``, ``i = 0, 1``.
Parameters ---------- old, new : array_like Domains. Each domain must (successfully) convert to a 1-d array containing precisely two values.
Returns ------- offset, scale : scalars The map ``L(x) = offset + scale*x`` maps the first domain to the second.
See Also -------- getdomain, mapdomain
Notes ----- Also works for complex numbers, and thus can be used to calculate the parameters required to map any line in the complex plane to any other line therein.
Examples -------- >>> from numpy.polynomial import polyutils as pu >>> pu.mapparms((-1,1),(-1,1)) (0.0, 1.0) >>> pu.mapparms((1,-1),(-1,1)) (-0.0, -1.0) >>> i = complex(0,1) >>> pu.mapparms((-i,-1),(1,i)) ((1+1j), (1-0j))
""" oldlen = old[1] - old[0] newlen = new[1] - new[0] off = (old[1]*new[0] - old[0]*new[1])/oldlen scl = newlen/oldlen return off, scl
def mapdomain(x, old, new): """ Apply linear map to input points.
The linear map ``offset + scale*x`` that maps the domain `old` to the domain `new` is applied to the points `x`.
Parameters ---------- x : array_like Points to be mapped. If `x` is a subtype of ndarray the subtype will be preserved. old, new : array_like The two domains that determine the map. Each must (successfully) convert to 1-d arrays containing precisely two values.
Returns ------- x_out : ndarray Array of points of the same shape as `x`, after application of the linear map between the two domains.
See Also -------- getdomain, mapparms
Notes ----- Effectively, this implements:
.. math:: x\\_out = new[0] + m(x - old[0])
where
.. math:: m = \\frac{new[1]-new[0]}{old[1]-old[0]}
Examples -------- >>> from numpy.polynomial import polyutils as pu >>> old_domain = (-1,1) >>> new_domain = (0,2*np.pi) >>> x = np.linspace(-1,1,6); x array([-1. , -0.6, -0.2, 0.2, 0.6, 1. ]) >>> x_out = pu.mapdomain(x, old_domain, new_domain); x_out array([ 0. , 1.25663706, 2.51327412, 3.76991118, 5.02654825, # may vary 6.28318531]) >>> x - pu.mapdomain(x_out, new_domain, old_domain) array([0., 0., 0., 0., 0., 0.])
Also works for complex numbers (and thus can be used to map any line in the complex plane to any other line therein).
>>> i = complex(0,1) >>> old = (-1 - i, 1 + i) >>> new = (-1 + i, 1 - i) >>> z = np.linspace(old[0], old[1], 6); z array([-1. -1.j , -0.6-0.6j, -0.2-0.2j, 0.2+0.2j, 0.6+0.6j, 1. +1.j ]) >>> new_z = pu.mapdomain(z, old, new); new_z array([-1.0+1.j , -0.6+0.6j, -0.2+0.2j, 0.2-0.2j, 0.6-0.6j, 1.0-1.j ]) # may vary
""" x = np.asanyarray(x) off, scl = mapparms(old, new) return off + scl*x
def _nth_slice(i, ndim): sl = [np.newaxis] * ndim sl[i] = slice(None) return tuple(sl)
def _vander_nd(vander_fs, points, degrees): r""" A generalization of the Vandermonde matrix for N dimensions
The result is built by combining the results of 1d Vandermonde matrices,
.. math:: W[i_0, \ldots, i_M, j_0, \ldots, j_N] = \prod_{k=0}^N{V_k(x_k)[i_0, \ldots, i_M, j_k]}
where
.. math:: N &= \texttt{len(points)} = \texttt{len(degrees)} = \texttt{len(vander\_fs)} \\ M &= \texttt{points[k].ndim} \\ V_k &= \texttt{vander\_fs[k]} \\ x_k &= \texttt{points[k]} \\ 0 \le j_k &\le \texttt{degrees[k]}
Expanding the one-dimensional :math:`V_k` functions gives:
.. math:: W[i_0, \ldots, i_M, j_0, \ldots, j_N] = \prod_{k=0}^N{B_{k, j_k}(x_k[i_0, \ldots, i_M])}
where :math:`B_{k,m}` is the m'th basis of the polynomial construction used along dimension :math:`k`. For a regular polynomial, :math:`B_{k, m}(x) = P_m(x) = x^m`.
Parameters ---------- vander_fs : Sequence[function(array_like, int) -> ndarray] The 1d vander function to use for each axis, such as ``polyvander`` points : Sequence[array_like] Arrays of point coordinates, all of the same shape. The dtypes will be converted to either float64 or complex128 depending on whether any of the elements are complex. Scalars are converted to 1-D arrays. This must be the same length as `vander_fs`. degrees : Sequence[int] The maximum degree (inclusive) to use for each axis. This must be the same length as `vander_fs`.
Returns ------- vander_nd : ndarray An array of shape ``points[0].shape + tuple(d + 1 for d in degrees)``. """ n_dims = len(vander_fs) if n_dims != len(points): raise ValueError( f"Expected {n_dims} dimensions of sample points, got {len(points)}") if n_dims != len(degrees): raise ValueError( f"Expected {n_dims} dimensions of degrees, got {len(degrees)}") if n_dims == 0: raise ValueError("Unable to guess a dtype or shape when no points are given")
# convert to the same shape and type points = tuple(np.array(tuple(points), copy=False) + 0.0)
# produce the vandermonde matrix for each dimension, placing the last # axis of each in an independent trailing axis of the output vander_arrays = ( vander_fs[i](points[i], degrees[i])[(...,) + _nth_slice(i, n_dims)] for i in range(n_dims) )
# we checked this wasn't empty already, so no `initial` needed return functools.reduce(operator.mul, vander_arrays)
def _vander_nd_flat(vander_fs, points, degrees): """ Like `_vander_nd`, but flattens the last ``len(degrees)`` axes into a single axis
Used to implement the public ``<type>vander<n>d`` functions. """ v = _vander_nd(vander_fs, points, degrees) return v.reshape(v.shape[:-len(degrees)] + (-1,))
def _fromroots(line_f, mul_f, roots): """ Helper function used to implement the ``<type>fromroots`` functions.
Parameters ---------- line_f : function(float, float) -> ndarray The ``<type>line`` function, such as ``polyline`` mul_f : function(array_like, array_like) -> ndarray The ``<type>mul`` function, such as ``polymul`` roots See the ``<type>fromroots`` functions for more detail """ if len(roots) == 0: return np.ones(1) else: [roots] = as_series([roots], trim=False) roots.sort() p = [line_f(-r, 1) for r in roots] n = len(p) while n > 1: m, r = divmod(n, 2) tmp = [mul_f(p[i], p[i+m]) for i in range(m)] if r: tmp[0] = mul_f(tmp[0], p[-1]) p = tmp n = m return p[0]
def _valnd(val_f, c, *args): """ Helper function used to implement the ``<type>val<n>d`` functions.
Parameters ---------- val_f : function(array_like, array_like, tensor: bool) -> array_like The ``<type>val`` function, such as ``polyval`` c, args See the ``<type>val<n>d`` functions for more detail """ args = [np.asanyarray(a) for a in args] shape0 = args[0].shape if not all((a.shape == shape0 for a in args[1:])): if len(args) == 3: raise ValueError('x, y, z are incompatible') elif len(args) == 2: raise ValueError('x, y are incompatible') else: raise ValueError('ordinates are incompatible') it = iter(args) x0 = next(it)
# use tensor on only the first c = val_f(x0, c) for xi in it: c = val_f(xi, c, tensor=False) return c
def _gridnd(val_f, c, *args): """ Helper function used to implement the ``<type>grid<n>d`` functions.
Parameters ---------- val_f : function(array_like, array_like, tensor: bool) -> array_like The ``<type>val`` function, such as ``polyval`` c, args See the ``<type>grid<n>d`` functions for more detail """ for xi in args: c = val_f(xi, c) return c
def _div(mul_f, c1, c2): """ Helper function used to implement the ``<type>div`` functions.
Implementation uses repeated subtraction of c2 multiplied by the nth basis. For some polynomial types, a more efficient approach may be possible.
Parameters ---------- mul_f : function(array_like, array_like) -> array_like The ``<type>mul`` function, such as ``polymul`` c1, c2 See the ``<type>div`` functions for more detail """ # c1, c2 are trimmed copies [c1, c2] = as_series([c1, c2]) if c2[-1] == 0: raise ZeroDivisionError()
lc1 = len(c1) lc2 = len(c2) if lc1 < lc2: return c1[:1]*0, c1 elif lc2 == 1: return c1/c2[-1], c1[:1]*0 else: quo = np.empty(lc1 - lc2 + 1, dtype=c1.dtype) rem = c1 for i in range(lc1 - lc2, - 1, -1): p = mul_f([0]*i + [1], c2) q = rem[-1]/p[-1] rem = rem[:-1] - q*p[:-1] quo[i] = q return quo, trimseq(rem)
def _add(c1, c2): """ Helper function used to implement the ``<type>add`` functions. """ # c1, c2 are trimmed copies [c1, c2] = as_series([c1, c2]) if len(c1) > len(c2): c1[:c2.size] += c2 ret = c1 else: c2[:c1.size] += c1 ret = c2 return trimseq(ret)
def _sub(c1, c2): """ Helper function used to implement the ``<type>sub`` functions. """ # c1, c2 are trimmed copies [c1, c2] = as_series([c1, c2]) if len(c1) > len(c2): c1[:c2.size] -= c2 ret = c1 else: c2 = -c2 c2[:c1.size] += c1 ret = c2 return trimseq(ret)
def _fit(vander_f, x, y, deg, rcond=None, full=False, w=None): """ Helper function used to implement the ``<type>fit`` functions.
Parameters ---------- vander_f : function(array_like, int) -> ndarray The 1d vander function, such as ``polyvander`` c1, c2 See the ``<type>fit`` functions for more detail """ x = np.asarray(x) + 0.0 y = np.asarray(y) + 0.0 deg = np.asarray(deg)
# check arguments. if deg.ndim > 1 or deg.dtype.kind not in 'iu' or deg.size == 0: raise TypeError("deg must be an int or non-empty 1-D array of int") if deg.min() < 0: raise ValueError("expected deg >= 0") if x.ndim != 1: raise TypeError("expected 1D vector for x") if x.size == 0: raise TypeError("expected non-empty vector for x") if y.ndim < 1 or y.ndim > 2: raise TypeError("expected 1D or 2D array for y") if len(x) != len(y): raise TypeError("expected x and y to have same length")
if deg.ndim == 0: lmax = deg order = lmax + 1 van = vander_f(x, lmax) else: deg = np.sort(deg) lmax = deg[-1] order = len(deg) van = vander_f(x, lmax)[:, deg]
# set up the least squares matrices in transposed form lhs = van.T rhs = y.T if w is not None: w = np.asarray(w) + 0.0 if w.ndim != 1: raise TypeError("expected 1D vector for w") if len(x) != len(w): raise TypeError("expected x and w to have same length") # apply weights. Don't use inplace operations as they # can cause problems with NA. lhs = lhs * w rhs = rhs * w
# set rcond if rcond is None: rcond = len(x)*np.finfo(x.dtype).eps
# Determine the norms of the design matrix columns. if issubclass(lhs.dtype.type, np.complexfloating): scl = np.sqrt((np.square(lhs.real) + np.square(lhs.imag)).sum(1)) else: scl = np.sqrt(np.square(lhs).sum(1)) scl[scl == 0] = 1
# Solve the least squares problem. c, resids, rank, s = np.linalg.lstsq(lhs.T/scl, rhs.T, rcond) c = (c.T/scl).T
# Expand c to include non-fitted coefficients which are set to zero if deg.ndim > 0: if c.ndim == 2: cc = np.zeros((lmax+1, c.shape[1]), dtype=c.dtype) else: cc = np.zeros(lmax+1, dtype=c.dtype) cc[deg] = c c = cc
# warn on rank reduction if rank != order and not full: msg = "The fit may be poorly conditioned" warnings.warn(msg, RankWarning, stacklevel=2)
if full: return c, [resids, rank, s, rcond] else: return c
def _pow(mul_f, c, pow, maxpower): """ Helper function used to implement the ``<type>pow`` functions.
Parameters ---------- mul_f : function(array_like, array_like) -> ndarray The ``<type>mul`` function, such as ``polymul`` c : array_like 1-D array of array of series coefficients pow, maxpower See the ``<type>pow`` functions for more detail """ # c is a trimmed copy [c] = as_series([c]) power = int(pow) if power != pow or power < 0: raise ValueError("Power must be a non-negative integer.") elif maxpower is not None and power > maxpower: raise ValueError("Power is too large") elif power == 0: return np.array([1], dtype=c.dtype) elif power == 1: return c else: # This can be made more efficient by using powers of two # in the usual way. prd = c for i in range(2, power + 1): prd = mul_f(prd, c) return prd
def _deprecate_as_int(x, desc): """ Like `operator.index`, but emits a deprecation warning when passed a float
Parameters ---------- x : int-like, or float with integral value Value to interpret as an integer desc : str description to include in any error message
Raises ------ TypeError : if x is a non-integral float or non-numeric DeprecationWarning : if x is an integral float """ try: return operator.index(x) except TypeError as e: # Numpy 1.17.0, 2019-03-11 try: ix = int(x) except TypeError: pass else: if ix == x: warnings.warn( f"In future, this will raise TypeError, as {desc} will " "need to be an integer not just an integral float.", DeprecationWarning, stacklevel=3 ) return ix
raise TypeError(f"{desc} must be an integer") from e
def format_float(x, parens=False): if not np.issubdtype(type(x), np.floating): return str(x)
opts = np.get_printoptions()
if np.isnan(x): return opts['nanstr'] elif np.isinf(x): return opts['infstr']
exp_format = False if x != 0: a = absolute(x) if a >= 1.e8 or a < 10**min(0, -(opts['precision']-1)//2): exp_format = True
trim, unique = '0', True if opts['floatmode'] == 'fixed': trim, unique = 'k', False
if exp_format: s = dragon4_scientific(x, precision=opts['precision'], unique=unique, trim=trim, sign=opts['sign'] == '+') if parens: s = '(' + s + ')' else: s = dragon4_positional(x, precision=opts['precision'], fractional=True, unique=unique, trim=trim, sign=opts['sign'] == '+') return s1234567891011__all__: list[str]
class RankWarning(UserWarning): ...
def trimseq(seq): ...def as_series(alist, trim=...): ...def trimcoef(c, tol=...): ...def getdomain(x): ...def mapparms(old, new): ...def mapdomain(x, old, new): ...def format_float(x, parens=...): ...12345678910def configuration(parent_package='',top_path=None): from numpy.distutils.misc_util import Configuration config = Configuration('polynomial', parent_package, top_path) config.add_subpackage('tests') config.add_data_files('*.pyi') return config
if __name__ == '__main__': from numpy.distutils.core import setup setup(configuration=configuration)lib/python3.12/site-packages/cloudpickle/__init__.pyAdded+18
lib/python3.12/site-packages/cloudpickle/__pycache__/__init__.cpython-312.pycAdded
lib/python3.12/site-packages/cloudpickle/__pycache__/cloudpickle.cpython-312.pycAdded
lib/python3.12/site-packages/cloudpickle/__pycache__/cloudpickle_fast.cpython-312.pycAdded
lib/python3.12/site-packages/cloudpickle/cloudpickle.pyAdded+1552
lib/python3.12/site-packages/cloudpickle/cloudpickle_fast.pyAdded+14
lib/python3.12/site-packages/ninja-1.13.0.dist-info/INSTALLERAdded+1
lib/python3.12/site-packages/ninja-1.13.0.dist-info/METADATAAdded+102
lib/python3.12/site-packages/ninja-1.13.0.dist-info/RECORDAdded+18
lib/python3.12/site-packages/ninja-1.13.0.dist-info/WHEELAdded+6
lib/python3.12/site-packages/ninja-1.13.0.dist-info/licenses/AUTHORS.rstAdded+5
lib/python3.12/site-packages/ninja-1.13.0.dist-info/licenses/LICENSE_Apache_20Added+191
lib/python3.12/site-packages/numpy/__config__.pyAdded+162
lib/python3.12/site-packages/numpy/__init__.cython-30.pxdAdded+1050
lib/python3.12/site-packages/numpy/__init__.pxdAdded+1015
lib/python3.12/site-packages/numpy/__init__.pyAdded+461
lib/python3.12/site-packages/numpy/__init__.pyiAdded
lib/python3.12/site-packages/numpy/_distributor_init.pyAdded+15
lib/python3.12/site-packages/numpy/_globals.pyAdded+95
lib/python3.12/site-packages/numpy/_pytesttester.pyAdded+207
lib/python3.12/site-packages/numpy/_pytesttester.pyiAdded+18
lib/python3.12/site-packages/numpy/conftest.pyAdded+138
lib/python3.12/site-packages/numpy/ctypeslib.pyAdded+545
lib/python3.12/site-packages/numpy/ctypeslib.pyiAdded+251
lib/python3.12/site-packages/numpy/dtypes.pyAdded+77
lib/python3.12/site-packages/numpy/dtypes.pyiAdded+43
lib/python3.12/site-packages/numpy/exceptions.pyAdded+231
lib/python3.12/site-packages/numpy/exceptions.pyiAdded+18
lib/python3.12/site-packages/numpy/matlib.pyAdded+378
lib/python3.12/site-packages/numpy/polynomial/__init__.pyAdded+185
lib/python3.12/site-packages/numpy/polynomial/__init__.pyiAdded+22
lib/python3.12/site-packages/numpy/polynomial/_polybase.pyAdded+1206
lib/python3.12/site-packages/numpy/polynomial/_polybase.pyiAdded+71
lib/python3.12/site-packages/numpy/polynomial/chebyshev.pyAdded+2082
lib/python3.12/site-packages/numpy/polynomial/chebyshev.pyiAdded+51
lib/python3.12/site-packages/numpy/polynomial/hermite.pyAdded+1703
lib/python3.12/site-packages/numpy/polynomial/hermite.pyiAdded+46
lib/python3.12/site-packages/numpy/polynomial/hermite_e.pyAdded+1695
lib/python3.12/site-packages/numpy/polynomial/hermite_e.pyiAdded+46
lib/python3.12/site-packages/numpy/polynomial/laguerre.pyAdded+1651
lib/python3.12/site-packages/numpy/polynomial/laguerre.pyiAdded+46
lib/python3.12/site-packages/numpy/polynomial/legendre.pyAdded+1664
lib/python3.12/site-packages/numpy/polynomial/legendre.pyiAdded+46
lib/python3.12/site-packages/numpy/polynomial/polynomial.pyAdded+1542
lib/python3.12/site-packages/numpy/polynomial/polynomial.pyiAdded+41
lib/python3.12/site-packages/numpy/polynomial/polyutils.pyAdded+789
lib/python3.12/site-packages/numpy/polynomial/polyutils.pyiAdded+11
lib/python3.12/site-packages/numpy/polynomial/setup.pyAdded+10
lib/python3.12/site-packages/numpy/polynomial/tests/__init__.pyAdded
lib/python3.12/site-packages/numpy/polynomial/tests/__pycache__/test_classes.cpython-312.pycAdded