Piece-wise interpolation and lazy evaluation in cython
Project description
BPF4
About
bpf4 is a python library to operate with curves in 2D space.
Curves can be defined via breakpoints (break-point functions, hence the name) or using functions. Moreover, curves can be used to build other curves. bpf4 can be used to perform curve fitting, data analysis, plotting, etc. Its core is programmed in cython for efficiency.
Installation
pip install --upgrade bpf4
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
- univariate splie
- pchip (hermite)
- cosine
- exponential
- logarithmic
- etc.
With the exception of curve-fitting bpfs (splines), interpolation types can be mixed, so that each segment has a different interpolation. Following from the example above:
c = (a + b).sin().abs()
# plot only the range (1.5, 4)
c[1.5:4].plot()
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 / Integration
from bpf4 import *
a = spline((0, 0), (1, 5), (2, 3), (5, 10))
deriv = a.derivative()
integr = a.integrated()
import matplotlib.pyplot as plt
fig, axs = plt.subplots(3, 1, sharex=True, figsize=(16, 8), tight_layout=True)
a.plot(axes=axs[0], show=False)
deriv.plot(axes=axs[1], show=False)
integr.plot(axes=axs[2])
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 Distributions
Built Distributions
Hashes for bpf4-1.11.2-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c79955d50e1a887112d33fdf2aec268686d2b2b64087429ed69f7bc8dc191f2 |
|
MD5 | 2a5d5554c2b2bc02d0880ee8e38959a8 |
|
BLAKE2b-256 | 9b91ab5d9d7e4b9b0d4a575cad98037d40b0515f6523b6132467c953a4b4f020 |
Hashes for bpf4-1.11.2-cp312-cp312-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 996be20c49a3c1ecce199f2a5861316f923d88dcee2d3b2c26ed07aa5f68d67b |
|
MD5 | 470077717d9e4a7951c3a1366c94b0c1 |
|
BLAKE2b-256 | 06191998b35f6faf1021f3189b5bc2e2cbc57007845180865458f188e2f2a846 |
Hashes for bpf4-1.11.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6397f734ac667cc4f1c0cc3424d5cdee5f513ae1f92160eecde6b6232bb02d70 |
|
MD5 | 0e9a1a4bb3fdc32ce18e1404f2b209b3 |
|
BLAKE2b-256 | 33b1867813daa1c2c08a157f68dddd44f3d8fdbbfcba07e5b548f500f8068ddd |
Hashes for bpf4-1.11.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4632984d773291b786e5557eade8c90e4bc2a92745a474bca7d1db7c5ec7f8c9 |
|
MD5 | 8eb816c3a19a19e2e74e997c7de5c9a9 |
|
BLAKE2b-256 | a4a8d7140fdf25ce962b52111acc9194b77aa977da8a196b72f26d6c3b29f25f |
Hashes for bpf4-1.11.2-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc7704c7fc1a4186d07839efb1ec8a9ab930ebb78160db0af0575df6fac0fc4b |
|
MD5 | 0ca9388e221876f27e4730e2b0ae223e |
|
BLAKE2b-256 | 8732af79159c907f50c2870782e7b378f3237a3dcd2d516b4fa247e6f66a95f6 |
Hashes for bpf4-1.11.2-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5bf57f262324a546799962aaf5234d302e230971ff0ee1c4d569faba6712fad0 |
|
MD5 | 6628f8754ad5dcb912b97bd0787b28b4 |
|
BLAKE2b-256 | aac6b82e414a38f067ae1b2e4238a59f1e644f808da581ef1ba127a465675478 |
Hashes for bpf4-1.11.2-cp311-cp311-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b34d18d9f9e350d91ab4ccc3dbbf30cbb7b973afe126794477b3fe50915978f |
|
MD5 | 8e573f3c889620fc31ae80281d92a05e |
|
BLAKE2b-256 | d43c3aadae4d826b2035553a6c47e9843d37050ffa14bd12b1a95ad5998f1a5b |
Hashes for bpf4-1.11.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d77ef83ddf17082501376f6a75d778a1a448a226044d8ab11d55394e857c731 |
|
MD5 | 0b0bc3d7d9234558fe6c51f4bab798f1 |
|
BLAKE2b-256 | c6c896cf82f3fb613b3354dbd2eb3d7aefc8c20dac5880ad23f74a37b871042e |
Hashes for bpf4-1.11.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 73d5609433e3d69fc7859b690a8af8b9bb901bc63311c6b58eaa92877284108a |
|
MD5 | 18a424a44daeb24bb5bc08277b6cba1d |
|
BLAKE2b-256 | 64aff7c91f7dfd97d5ed4794ce9bb2f997ff602a2b1238401115e4c3c44b7109 |
Hashes for bpf4-1.11.2-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d1fe9275b729b4142f5b7616f307449dcd5177f0ae70ee7c4cadfe514a836b18 |
|
MD5 | 85f1918d09985c49978bff8ec6940712 |
|
BLAKE2b-256 | 4dfa9ee9c6de40a783617a5bf8c38557401ead15fa3c78fbad2c189b4fba39ef |
Hashes for bpf4-1.11.2-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 93c31631f60800fbfed7cef8f4a90d8d02407b89367b1758a7c14f32dd99cb40 |
|
MD5 | 78c26c629f8c7d2debb767efb4eaaf97 |
|
BLAKE2b-256 | 0cdd98cb59540c4405418f3cb6e92cf1322e0bbab3fdc31dcfc55a500d5e6c43 |
Hashes for bpf4-1.11.2-cp310-cp310-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ad65cf328a026633367da6dd5fffcf7a5be0718b198fad33296ff31c6b86a15 |
|
MD5 | ff998adbda42fafdb7d018890177a203 |
|
BLAKE2b-256 | 363ce434dc3bfb705fce39be4d96129455506c0501cc43c06d959836aab31bdb |
Hashes for bpf4-1.11.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 53ba25d784eeebfb9b68c0d8a642e9e3d1896da196f70dc472d82dfece7456cd |
|
MD5 | 04d45bdd5789a542b49a93b9f59b917b |
|
BLAKE2b-256 | 34755113def685feb246e31c216a21551144afbe560bd5ac03dcee302b15df79 |
Hashes for bpf4-1.11.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c4950c6ba470c7f008e62c9a7f353e0918e9c310131855014e1401ce46760011 |
|
MD5 | 42588ddd7edeb36cc6eece5c0f672191 |
|
BLAKE2b-256 | 23499d444e02eb80064efd809292327b108789fbb91fc5f573c3ac165dfe05c8 |
Hashes for bpf4-1.11.2-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3d284ccdba6f6028235b4f7660730d26816dab65e3d5d45ae86f666962485a1b |
|
MD5 | e940aae00ce7ebaa1b02abd108f608ec |
|
BLAKE2b-256 | 961953eea00858ad1e3dd66ae31dec8cef060ed929ed2a0d264ff7551958cb5e |
Hashes for bpf4-1.11.2-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6f42c0819475e642bdedcf80c68ddac5b28f2030f34027b37e21b8b540369742 |
|
MD5 | e2e23ceebf79bf32352bb3ba9424b96e |
|
BLAKE2b-256 | 1bac7a5a5e2125fdd6cf3526f64219ca571dd1ff2b854b9eb85bce8d2a7e6f9c |
Hashes for bpf4-1.11.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd1567a61b9ca87dffae8c8b1e17c8b3b62a3a358ffb30badbca079b9546054f |
|
MD5 | cec2dc6bf851b341737ded718f7a93f8 |
|
BLAKE2b-256 | e7f1e06b4c4cff184af2e88389147bb6df26630e0b5d05c64a4704252da16385 |
Hashes for bpf4-1.11.2-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 18881f36ec278ab0887939746d55853231bd3639159626f5ff9d7c6c0c9bfc36 |
|
MD5 | b2485f5733b8c4a1bcc821f28949c6b7 |
|
BLAKE2b-256 | cecaa5e54f96caa834ba8dabc0bc318c886c8a5a98dd2d07e3671fb9150dde76 |
Hashes for bpf4-1.11.2-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0fd4898cd45ccd12d3e7daaeb717bd8156470e0deafcea21be2987b9dbfbf154 |
|
MD5 | f460fe8671dc8ef5c50ee12a8dafe364 |
|
BLAKE2b-256 | 5af9cddb8e0d8289e6a20f9648f3007838cec5d4097162544270cdd56ca3540c |