Skip to main content

SciPy fixes and extensions

Project description

scipyx

PyPi Version PyPI pyversions GitHub stars PyPi downloads

gh-actions codecov LGTM Code style: black

SciPy is large library used everywhere in scientific computing. That's why breaking backwards-compatibility comes as a significant cost and is almost always avoided, even if the API of some methods is arguably lacking. This package provides drop-in wrappers "fixing" those.

npx does the same for NumPy.

If you have a fix for a SciPy method that can't go upstream for some reason, feel free to PR here.

Krylov methods

import numpy as np
import scipy.sparse
import scipyx as spx

# create tridiagonal (-1, 2, -1) matrix
n = 100
data = -np.ones((3, n))
data[1] = 2.0
A = scipy.sparse.spdiags(data, [-1, 0, 1], n, n)
A = A.tocsr()
b = np.ones(n)


sol, info = spx.cg(A, b, tol=1.0e-10)
sol, info = spx.minres(A, b, tol=1.0e-10)
sol, info = spx.gmres(A, b, tol=1.0e-10)
sol, info = spx.bicg(A, b, tol=1.0e-10)
sol, info = spx.bicgstab(A, b, tol=1.0e-10)
sol, info = spx.cgs(A, b, tol=1.0e-10)
sol, info = spx.qmr(A, b, tol=1.0e-10)

sol is the solution of the linear system A @ x = b (or None if no convergence), and info contains some useful data, e.g., info.resnorms. The solution sol and all callback x have the shape of x0/b. The methods are wrappers around SciPy's iterative solvers.

Relevant issues:

Optimization

import scipyx as spx


def f(x):
    return (x ** 2 - 2) ** 2


x0 = 1.5
out = spx.minimize(f, x0)
print(out.x)

x0 = -3.2
x, _ = spx.leastsq(f, x0)
print(x)

In scipyx, all intermediate values x and the result from a minimization out.x will have the same shape as x0. (In SciPy, they always have shape (n,), no matter the input vector.)

Relevant issues:

Root-finding

import scipyx as spx


def f(x):
    return x ** 2 - 2


a, b = spx.bisect(f, 0.0, 5.0, tol=1.0e-12)
a, b = spx.regula_falsi(f, 0.0, 5.0, tol=1.0e-12)

scipyx provides some basic nonlinear root-findings algorithms: bisection and regula falsi. They're not as fast-converging as other methods, but are very robust and work with almost any function.

Rolling Lagrange interpolation

import numpy as np
import scipyx as spx


x = np.linspace(0.0, 1.0, 11)
y = np.sin(7.0 * x)

poly = spx.interp_rolling_lagrange(x, y, order=3)

Given an array of coordinates x and an array of values y, you can use scipyx to compute a piecewise polynomial Lagrange approximation. The order + 1 closest coordinates x/y are considered for each interval.

Order 0 Order 1 Order 2

License

This software is published under the BSD-3-Clause license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

scipyx-0.0.16.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

scipyx-0.0.16-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file scipyx-0.0.16.tar.gz.

File metadata

  • Download URL: scipyx-0.0.16.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6

File hashes

Hashes for scipyx-0.0.16.tar.gz
Algorithm Hash digest
SHA256 4ac641379c228444e171aef2cdff6cb1533a4fed63f922922489d640c484b5f9
MD5 0dceda0689a570afe18cddfcf48f00b8
BLAKE2b-256 514427a72e7643de4890c6945bf5d66441ddd7233bff75b3aadf5fbf9677f57f

See more details on using hashes here.

File details

Details for the file scipyx-0.0.16-py3-none-any.whl.

File metadata

  • Download URL: scipyx-0.0.16-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6

File hashes

Hashes for scipyx-0.0.16-py3-none-any.whl
Algorithm Hash digest
SHA256 f15890addfb4ece81d5d95fce929cf9bc8d7ed1d42bcc890b1abd4e28a10e022
MD5 d74fc2136adc0852ee6505734c35336e
BLAKE2b-256 b54323c4b04cd054299dd4235955c70f56d49857576f1bf7f7f160216c9bda66

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page