Skip to main content

Polynomials as a numpy datatype

Project description

circleci codecov pypi readthedocs

Numpoly is a generic library for creating, manipulating polynomial arrays.

Many numerical analysis, prominent in for example uncertainty quantification, uses polynomial approximations as proxy for real models to do analysis on. These models are often solutions to non-linear problems discretized with high mesh. As such, the corresponding polynomial approximation consist of high number of dimensions and large multi-dimensional polynomial coefficients.

numpoly is a subclass of numpy.ndarray implemented to represent polynomials as array element. As such is fast and scales very well with the size of the coefficients. It is also compatible with most numpy functions, where that makes sense, making the interface fairly intuitive. Some of the interface is also inspired by the sympy interface.

Installation

Installation should be straight forward:

pip install numpoly

And you should be ready to go.

Example usage

Constructing polynomial is typically done using one of the available constructors:

>>> poly1 = numpoly.monomial(("x", "y"), start=0, stop=3)
>>> print(poly1)
[1 y x x*y x**2 y**2 y**3 x*y**2 x**2*y x**3]

It is also possible to construct your own from symbols:

>>> x, y = numpoly.symbols("x y")
>>> poly2 = numpoly.polynomial([1, x**2-1, x*y, y**2-1])
>>> print(poly2)
[1 -1+x**2 x*y -1+y**2]

Or in combination with other numpy objects:

>>> poly3 = x**numpy.arange(4)-y**numpy.arange(3, -1, -1)
>>> print(poly3)
[1-y**3 -y**2+x -y+x**2 -1+x**3]

The polynomials can be evaluated as needed:

>>> print(poly1(1, 2))
[1 2 1 2 1 4 8 4 2 1]
>>> print(poly2(x=[1, 2]))
[[1 1]
 [0 3]
 [y 2*y]
 [-1+y**2 -1+y**2]]
>>> print(poly1(x=y, y=2*x))
[1 2*x y 2*x*y y**2 4*x**2 8*x**3 4*x**2*y 2*x*y**2 y**3]

The polynomials also support many numpy operations:

>>> print(numpy.reshape(poly2, (2, 2)))
[[1 -1+x**2]
 [x*y -1+y**2]]
>>> print(poly1[::3].astype(float))
[1.0 x*y y**3 x**3]
>>> print(numpy.sum(poly1.reshape(2, 5), 0))
[1+y**2 y+y**3 x+x*y**2 x*y+x**2*y x**2+x**3]

There are also several polynomial specific operators:

>>> print(numpoly.diff(poly3, y))
[-3*y**2 -2*y -1 0]
>>> print(numpoly.gradient(poly3))
[[0 1 2*x 3*x**2]
 [-3*y**2 -2*y -1 0]]

Development

Development is done using Poetry manager. Inside the repository directory, install and create a virtual enviroment with:

poetry install

To run tests, run:

poentry run pytest numpoly test --doctest-modules

Questions & Troubleshooting

For any problems and questions you might have related to numpoly, please feel free to file an issue.

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

numpoly-0.0.16.tar.gz (35.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

numpoly-0.0.16-py2.py3-none-any.whl (65.7 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: numpoly-0.0.16.tar.gz
  • Upload date:
  • Size: 35.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.7.3 Linux/4.15.0-1043-aws

File hashes

Hashes for numpoly-0.0.16.tar.gz
Algorithm Hash digest
SHA256 002ae2f8f6c0664e40d73652f9c6546228795c7f42e7d667473ff675f77b5f05
MD5 376b04df06934fed684048812317c134
BLAKE2b-256 b8683b3d7dc7451c6860b20b1a69337a99d448e0e76af8be97c5e2a3f75d6895

See more details on using hashes here.

File details

Details for the file numpoly-0.0.16-py2.py3-none-any.whl.

File metadata

  • Download URL: numpoly-0.0.16-py2.py3-none-any.whl
  • Upload date:
  • Size: 65.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.7.3 Linux/4.15.0-1043-aws

File hashes

Hashes for numpoly-0.0.16-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c219ea0dea87df88bcc016a0cf410ed378747819fe7dc79f73b7be89bed391cd
MD5 9a22cfb958dd97e9a22c5a1da315e67b
BLAKE2b-256 9a84dd6132c3fe9cf7f891eb70f46cff0212d511afd90b8e0720b6ae4e7334b2

See more details on using hashes here.

Supported by

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