Peace-wise interpolation and lazy evaluation in cython
Project description
BPF4
About
bpf4 is a library for curve fitting and break-point functions in python. It is mainly programmed in Cython for efficiency.
Documentation
The documentation is hosted at https://bpf4.readthedocs.io
Example
Find the intersection between two curves
from bpf4 import bpf # this imports the api
a = bpf.spline((0, 0), (1, 5), (2, 3), (5, 10)) # each point (x, y)
b = bpf.expon((0, -10), (2,15), (5, 3), exp=3)
a.plot() # uses matplotlib
b.plot()
zeros = (a - b).zeros()
import pylab
pylab.plot(zeros, a.map(zeros), 'o')
Features
Many interpolation types besides linear:
- spline
- half-cosine
- exponential
- fibonacci
- exponantial half-cosine
- pchip
- logarithmic
- etc.
Interpolation types can be mixed, so that each segment has a different interpolation (with the exception of spline interpolation)
Curves can be combined non-destructively. Following from the example above.
c = (a + b).sin().abs()
c[1.5:4].plot() # plot only the range (1.5, 4)
Syntax support for shifting, scaling and slicing a bpf
a >> 2 # a shifted to the right
(a * 5) ^ 2 # scale the x coord by 2, scale the y coord by 5
a[2:2.5] # slice only a portion of the bpf
a[::0.01] # sample the bpf with an interval of 0.01
- Derivation and Integration:
c.derivative().plot()
orc.integrated().integrated().plot()
- Numerical integration:
c.integrate_between(2, 4)
Installation
pip install --upgrade bpf4
To install from source:
git clone https://github.com/gesellkammer/bpf4.git
cd bpf4
pip install .
Mathematical operations
Max / Min
a = linear(0, 0, 1, 0.5, 2, 0)
b = expon(0, 0, 2, 1, exp=3)
a.plot(show=False, color="red", linewidth=4, alpha=0.3)
b.plot(show=False, color="blue", linewidth=4, alpha=0.3)
core.Max((a, b)).plot(color="black", linewidth=4, alpha=0.8, linestyle='dotted')
a = linear(0, 0, 1, 0.5, 2, 0)
b = expon(0, 0, 2, 1, exp=3)
a.plot(show=False, color="red", linewidth=4, alpha=0.3)
b.plot(show=False, color="blue", linewidth=4, alpha=0.3)
core.Min((a, b)).plot(color="black", linewidth=4, alpha=0.8, linestyle='dotted')
+, -, *, /
a = linear(0, 0, 1, 0.5, 2, 0)
b = expon(0, 0, 2, 1, exp=3)
a.plot(show=False, color="red", linewidth=4, alpha=0.3)
b.plot(show=False, color="blue", linewidth=4, alpha=0.3)
(a*b).plot(color="black", linewidth=4, alpha=0.8, linestyle='dotted')
a = linear(0, 0, 1, 0.5, 2, 0)
b = expon(0, 0, 2, 1, exp=3)
a.plot(show=False, color="red", linewidth=4, alpha=0.3)
b.plot(show=False, color="blue", linewidth=4, alpha=0.3)
(a**b).plot(color="black", linewidth=4, alpha=0.8, linestyle='dotted')
a = linear(0, 0, 1, 0.5, 2, 0)
b = expon(0, 0, 2, 1, exp=3)
a.plot(show=False, color="red", linewidth=4, alpha=0.3)
b.plot(show=False, color="blue", linewidth=4, alpha=0.3)
((a+b)/2).plot(color="black", linewidth=4, alpha=0.8, linestyle='dotted')
Building functions
A bpf can be used to build complex formulas
Fresnel's Integral: ( S(x) = \int_0^x {sin(t^2)} dt )
t = slope(1)
f = (t**2).sin()[0:10:0.001].integrated()
f.plot()
Polar plots
Any kind of matplotlib plot can be used. For example, polar plots are possible
by creating an axes with polar=True
Cardiod: (\rho = 1 + sin(-\theta) )
from math import *
theta = slope(1, bounds=(0, 2*pi))
r = 1 + (-theta).sin()
ax = plt.axes(polar=True)
ax.set_rticks([0.5, 1, 1.5, 2]); ax.set_rlabel_position(38)
r.plot(axes=ax)
Flower 5: (\rho = 3 + cos(5 * \theta) )
theta = core.Slope(1, bounds=(0, 2*pi))
r = 3 + (5*theta).cos()
ax = plt.axes(polar=True)
r.plot(axes=ax)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for bpf4-1.4.3-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 785cd33235e10301801f0ebe27387a6bd300d523da1cfbe91ce566b134d51692 |
|
MD5 | 8b3173489cdff6a7b360320f972b8ea4 |
|
BLAKE2b-256 | 125319e4bb65c18cfc0cdb758963edbaca343f2955d0f218d662c083fd69db50 |
Hashes for bpf4-1.4.3-cp310-cp310-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b0b5b079633aca383970f3c1c7b01a577017e2c56b28f7d2d2ff4f8c4e21b7f |
|
MD5 | 4e00d3ce963b8d8d10b5e17b5cfd5169 |
|
BLAKE2b-256 | 5397362b3c0ffbbedcdedce66ff76968525f2b55cfb3887104d159940ee1f962 |
Hashes for bpf4-1.4.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9616b7dc47c385fa00676f50bf2811c5819ecb35fef7ffc0ba266e2f851f5c2c |
|
MD5 | 4b05e8ea93778f2648195cde7258a87a |
|
BLAKE2b-256 | f421c239f9e01cf1833b7abdea111f0e10ac4443f5534b905535fa2df552c2ee |
Hashes for bpf4-1.4.3-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a15678d59a5503e5b89bf6eb96807c0253a7c1a178fab2eae8b9ebd6cc73d935 |
|
MD5 | 1b9d7b8a99317e610eadd945db5c879b |
|
BLAKE2b-256 | 6c259e31de5720a21ffd95083025e05da687cb968f63e06f859f552b936dfede |
Hashes for bpf4-1.4.3-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d3dc80bacd4f91b8913dcf21245cffe84271b0dce39e908f2812646bfca8cee |
|
MD5 | d72a1a5f7cde96f345b6227ec06667c9 |
|
BLAKE2b-256 | 971225e59ff10cfa4def9dd7889832870a3aade0c43f92e2b5525e6c2aa1c080 |
Hashes for bpf4-1.4.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 10b68cd7fc1ceedfe60caaf7821b4dcee66fdacd103e8576626fbb41bdccfe4e |
|
MD5 | 7a7522e2e29fa335f4bfd92a182080ce |
|
BLAKE2b-256 | 324342cf68f41c6e4837d78fd25fe09f9f708fe0f73b92c270f4943b0c0aeb25 |
Hashes for bpf4-1.4.3-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ea569b8145a0ac58036a1bffadab403ec1dc94ccc3fa56b386e27df47a4c674 |
|
MD5 | d8f15f944b3a88cbe6be2d4d97d4f9b7 |
|
BLAKE2b-256 | c1aed661464ea45c4b253ae05869f128313a6aa207370d9144fe9912506cc9d9 |
Hashes for bpf4-1.4.3-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 654558d5eace6f09726a0c0718d3dc01cf25e48678e5fff1835de2c79b9fff74 |
|
MD5 | afff6158f4cb4cdf734aba9ace011df2 |
|
BLAKE2b-256 | 027edb0ee08e52e5375d711b127a9d6e824309471df820c14fc763c0ad0a8ac1 |
Hashes for bpf4-1.4.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1bd9db6ebe8b912aa34327a460c0df0902b035dffd8e6f413b026a69ff5da8ce |
|
MD5 | 4ca1c817234473314b2a343ec8e76ff6 |
|
BLAKE2b-256 | 9df960f3b2f80311ae26a19b72a1e8346c39a2a51081168046598727cd4a69b1 |
Hashes for bpf4-1.4.3-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 38eef3582f313f00cfdea9ef6db4e4688b6539d0123af0158e6ba9278410e83a |
|
MD5 | 6e8a7b9771f7ab3653f67fa6f7bf9b03 |
|
BLAKE2b-256 | 9dc06dce9e302de85d7e428667b0897af0b264da1d7e51ff81554040dd585023 |