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.4-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f8cf5ebc85d60a39103522cc0082a41ce493248849fbaab5f796846cdba6e47 |
|
MD5 | affbf3d88cc8eeee91054c2ae01fcd40 |
|
BLAKE2b-256 | d5e73e86979e6f5cc43d3a38a4b2088131e0dc05d8e3a9a88df72f793d2b5dc2 |
Hashes for bpf4-1.4.4-cp310-cp310-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d662e4b82637ff377f326f6ee9ab4411a28aa64f347de9b6fc57094e85e618d7 |
|
MD5 | 15a9e271d7b8b947ba9522e035a3fb4f |
|
BLAKE2b-256 | 48867efa0b2545ec49eb0214e120908dab6cd373b93df35e750048d05f223c43 |
Hashes for bpf4-1.4.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b3233b5f240b9922d8faa2a30bb272bd30540162d4e74d32528fd882ebaa472e |
|
MD5 | 12ac9a3eabadd0256eb85d6bc98cce9e |
|
BLAKE2b-256 | 8f9a71fec0d2e9533ee58be79ec96ae248c7a732bda3c9e27caf165af2f6adc1 |
Hashes for bpf4-1.4.4-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 13a26bc49edb5905be4228b67752fb76aff87acae2db030619920a03f16e3879 |
|
MD5 | 85b18ebd8d60af6f64fd8eba8372d8df |
|
BLAKE2b-256 | 96877d66cfaa4f2ecbc8859621c0a3265ffd289566c097be6ed75d22c392efe2 |
Hashes for bpf4-1.4.4-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1bfdb4e500504b759ea255e635292f77950cc9c31e0405f6c79b34e5b3892348 |
|
MD5 | 61294565459246588577592a15e1a344 |
|
BLAKE2b-256 | cfbe7d9a597851f70ce50e21699f24594bd593fe976168df4891999973910df7 |
Hashes for bpf4-1.4.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f0e66b4deab2861ab95c66f8c677279ab5ad965f918ae6ed372eeb564c7ac2fa |
|
MD5 | ecea14334a51644177afd533b8c5f118 |
|
BLAKE2b-256 | f2d6ad60a78c3f622581e182aab178531f97e546eaf1d6ad15da47adfd1e2c35 |
Hashes for bpf4-1.4.4-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0c9be4c48dec2be10649d4bb311ec1c07b68a36407fb9195014685141989194f |
|
MD5 | 5a01bea4832fbb91f02450cae89d9010 |
|
BLAKE2b-256 | 42508bdeaeef5c9b277544a8d30fa72777fdca6ec07a211508b5bdb483fdfa75 |
Hashes for bpf4-1.4.4-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 109b5d072d56e1b9e8a1e5b7541ab932fca706b5b48ed971523e3b33b2993721 |
|
MD5 | 2ac622b029cb821ba00ebc0a6024ac7f |
|
BLAKE2b-256 | 351c672145a382eae350e3faa14b88bb21ab271113c4d0091d690a546dbdad63 |
Hashes for bpf4-1.4.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48d053deffc246fa5d4f942f996cd354659abeae921920a76cf5a01387b57c0e |
|
MD5 | c143d4f98c7b5060237af916b82ccc3e |
|
BLAKE2b-256 | bfec0486e626291a1cc736966a5f8669958f5872cccf2992d4f1c0f0cac3d6f4 |
Hashes for bpf4-1.4.4-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40da5b8ec121f67e2bfa5fc09307ceb160854f288a4a89461b7bd681fd9c1f12 |
|
MD5 | 3646e3b0ff0cac3472d7b3604434f22f |
|
BLAKE2b-256 | 6622f887516523600372a308dc6e2cf150c27166230f99d218aeb03d92a371df |