diff options
Diffstat (limited to 'numpy/linalg')
-rw-r--r-- | numpy/linalg/__init__.pyi | 23 | ||||
-rwxr-xr-x | numpy/linalg/lapack_lite/make_lite.py | 18 | ||||
-rw-r--r-- | numpy/linalg/linalg.py | 23 | ||||
-rw-r--r-- | numpy/linalg/setup.py | 1 | ||||
-rw-r--r-- | numpy/linalg/tests/test_build.py | 4 | ||||
-rw-r--r-- | numpy/linalg/tests/test_linalg.py | 6 | ||||
-rw-r--r-- | numpy/linalg/umath_linalg.c.src | 2 |
7 files changed, 50 insertions, 27 deletions
diff --git a/numpy/linalg/__init__.pyi b/numpy/linalg/__init__.pyi new file mode 100644 index 000000000..ffb05bb81 --- /dev/null +++ b/numpy/linalg/__init__.pyi @@ -0,0 +1,23 @@ +from typing import Any + +matrix_power: Any +solve: Any +tensorsolve: Any +tensorinv: Any +inv: Any +cholesky: Any +eigvals: Any +eigvalsh: Any +pinv: Any +slogdet: Any +det: Any +svd: Any +eig: Any +eigh: Any +lstsq: Any +norm: Any +qr: Any +cond: Any +matrix_rank: Any +LinAlgError: Any +multi_dot: Any diff --git a/numpy/linalg/lapack_lite/make_lite.py b/numpy/linalg/lapack_lite/make_lite.py index 23921acf4..cf15b2541 100755 --- a/numpy/linalg/lapack_lite/make_lite.py +++ b/numpy/linalg/lapack_lite/make_lite.py @@ -81,7 +81,7 @@ class FortranRoutine: return self._dependencies def __repr__(self): - return "FortranRoutine({!r}, filename={!r})".format(self.name, self.filename) + return f'FortranRoutine({self.name!r}, filename={self.filename!r})' class UnknownFortranRoutine(FortranRoutine): """Wrapper for a Fortran routine for which the corresponding file @@ -193,7 +193,7 @@ class LapackLibrary(FortranLibrary): def printRoutineNames(desc, routines): print(desc) for r in routines: - print('\t%s' % r.name) + print(f'\t{r.name}') def getLapackRoutines(wrapped_routines, ignores, lapack_dir): blas_src_dir = os.path.join(lapack_dir, 'BLAS', 'SRC') @@ -243,7 +243,7 @@ def dumpRoutineNames(library, output_dir): with open(filename, 'w') as fo: for r in routines: deps = r.dependencies() - fo.write('%s: %s\n' % (r.name, ' '.join(deps))) + fo.write(f"{r.name}: {' '.join(deps)}\n") def concatenateRoutines(routines, output_file): with open(output_file, 'w') as output_fo: @@ -316,13 +316,13 @@ def create_name_header(output_dir): # Rename BLAS/LAPACK symbols for name in sorted(symbols): - f.write("#define %s_ BLAS_FUNC(%s)\n" % (name, name)) + f.write(f'#define {name}_ BLAS_FUNC({name})\n') # Rename also symbols that f2c exports itself f.write("\n" "/* Symbols exported by f2c.c */\n") for name in sorted(f2c_symbols): - f.write("#define %s numpy_lapack_lite_%s\n" % (name, name)) + f.write(f'#define {name} numpy_lapack_lite_{name}\n') def main(): if len(sys.argv) != 3: @@ -348,9 +348,9 @@ def main(): dumpRoutineNames(library, output_dir) for typename in types: - fortran_file = os.path.join(output_dir, 'f2c_%s.f' % typename) + fortran_file = os.path.join(output_dir, f'f2c_{typename}.f') c_file = fortran_file[:-2] + '.c' - print('creating %s ...' % c_file) + print(f'creating {c_file} ...') routines = library.allRoutinesByType(typename) concatenateRoutines(routines, fortran_file) @@ -358,11 +358,11 @@ def main(): patch_file = os.path.basename(fortran_file) + '.patch' if os.path.exists(patch_file): subprocess.check_call(['patch', '-u', fortran_file, patch_file]) - print("Patched {}".format(fortran_file)) + print(f'Patched {fortran_file}') try: runF2C(fortran_file, output_dir) except F2CError: - print('f2c failed on %s' % fortran_file) + print(f'f2c failed on {fortran_file}') break scrubF2CSource(c_file) diff --git a/numpy/linalg/linalg.py b/numpy/linalg/linalg.py index 513ea8219..7775e4c32 100644 --- a/numpy/linalg/linalg.py +++ b/numpy/linalg/linalg.py @@ -176,10 +176,9 @@ def _to_native_byte_order(*arrays): def _fastCopyAndTranspose(type, *arrays): cast_arrays = () for a in arrays: - if a.dtype.type is type: - cast_arrays = cast_arrays + (_fastCT(a),) - else: - cast_arrays = cast_arrays + (_fastCT(a.astype(type)),) + if a.dtype.type is not type: + a = a.astype(type) + cast_arrays = cast_arrays + (_fastCT(a),) if len(cast_arrays) == 1: return cast_arrays[0] else: @@ -362,13 +361,13 @@ def solve(a, b): Examples -------- - Solve the system of equations ``3 * x0 + x1 = 9`` and ``x0 + 2 * x1 = 8``: + Solve the system of equations ``x0 + 2 * x1 = 1`` and ``3 * x0 + 5 * x1 = 2``: - >>> a = np.array([[3,1], [1,2]]) - >>> b = np.array([9,8]) + >>> a = np.array([[1, 2], [3, 5]]) + >>> b = np.array([1, 2]) >>> x = np.linalg.solve(a, b) >>> x - array([2., 3.]) + array([-1., 1.]) Check that the solution is correct: @@ -902,7 +901,7 @@ def qr(a, mode='reduced'): warnings.warn(msg, DeprecationWarning, stacklevel=3) mode = 'economic' else: - raise ValueError("Unrecognized mode '%s'" % mode) + raise ValueError(f"Unrecognized mode '{mode}'") a, wrap = _makearray(a) _assert_2d(a) @@ -2207,8 +2206,8 @@ def lstsq(a, b, rcond="warn"): Least-squares solution. If `b` is two-dimensional, the solutions are in the `K` columns of `x`. residuals : {(1,), (K,), (0,)} ndarray - Sums of residuals; squared Euclidean 2-norm for each column in - ``b - a*x``. + Sums of squared residuals: Squared Euclidean 2-norm for each column in + ``b - a @ x``. If the rank of `a` is < N or M <= N, this is an empty array. If `b` is 1-dimensional, this is a (1,) shape array. Otherwise the shape is (K,). @@ -2559,7 +2558,7 @@ def norm(x, ord=None, axis=None, keepdims=False): # special case for speedup s = (x.conj() * x).real return sqrt(add.reduce(s, axis=axis, keepdims=keepdims)) - # None of the str-type keywords for ord ('fro', 'nuc') + # None of the str-type keywords for ord ('fro', 'nuc') # are valid for vectors elif isinstance(ord, str): raise ValueError(f"Invalid norm order '{ord}' for vectors") diff --git a/numpy/linalg/setup.py b/numpy/linalg/setup.py index bb070ed9d..5c9f2a4cb 100644 --- a/numpy/linalg/setup.py +++ b/numpy/linalg/setup.py @@ -80,6 +80,7 @@ def configuration(parent_package='', top_path=None): extra_info=lapack_info, libraries=['npymath'], ) + config.add_data_files('*.pyi') return config if __name__ == '__main__': diff --git a/numpy/linalg/tests/test_build.py b/numpy/linalg/tests/test_build.py index cbf3089bc..4859226d9 100644 --- a/numpy/linalg/tests/test_build.py +++ b/numpy/linalg/tests/test_build.py @@ -16,13 +16,13 @@ class FindDependenciesLdd: p = Popen(self.cmd, stdout=PIPE, stderr=PIPE) stdout, stderr = p.communicate() except OSError: - raise RuntimeError("command %s cannot be run" % self.cmd) + raise RuntimeError(f'command {self.cmd} cannot be run') def get_dependencies(self, lfile): p = Popen(self.cmd + [lfile], stdout=PIPE, stderr=PIPE) stdout, stderr = p.communicate() if not (p.returncode == 0): - raise RuntimeError("failed dependencies check for %s" % lfile) + raise RuntimeError(f'failed dependencies check for {lfile}') return stdout diff --git a/numpy/linalg/tests/test_linalg.py b/numpy/linalg/tests/test_linalg.py index 3f3bf9f70..21fab58e1 100644 --- a/numpy/linalg/tests/test_linalg.py +++ b/numpy/linalg/tests/test_linalg.py @@ -85,7 +85,7 @@ class LinalgCase: do(self.a, self.b, tags=self.tags) def __repr__(self): - return "<LinalgCase: %s>" % (self.name,) + return f'<LinalgCase: {self.name}>' def apply_tag(tag, cases): @@ -349,7 +349,7 @@ class LinalgTestCase: try: case.check(self.do) except Exception: - msg = "In test case: %r\n\n" % case + msg = f'In test case: {case!r}\n\n' msg += traceback.format_exc() raise AssertionError(msg) @@ -1732,7 +1732,7 @@ class TestCholesky: b = np.matmul(c, c.transpose(t).conj()) assert_allclose(b, a, - err_msg="{} {}\n{}\n{}".format(shape, dtype, a, c), + err_msg=f'{shape} {dtype}\n{a}\n{c}', atol=500 * a.shape[0] * np.finfo(dtype).eps) def test_0_size(self): diff --git a/numpy/linalg/umath_linalg.c.src b/numpy/linalg/umath_linalg.c.src index 59647c67d..1807aadcf 100644 --- a/numpy/linalg/umath_linalg.c.src +++ b/numpy/linalg/umath_linalg.c.src @@ -3665,7 +3665,7 @@ PyObject *PyInit__umath_linalg(void) return NULL; } - version = PyString_FromString(umath_linalg_version_string); + version = PyUnicode_FromString(umath_linalg_version_string); if (version == NULL) { return NULL; } |