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 Distributions
Built Distributions
Hashes for bpf4-1.4.2-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8abd824570c4739ecc4df82d0f8b8bbaeb9a047294cd21e83154c691cdee21a |
|
MD5 | 3f67176b4fe94f7df8d795f739c244a9 |
|
BLAKE2b-256 | 37b72a07ae1f402c7630a469fd5009ca0e7395f0932aa53cd0a3997feb4ba95e |
Hashes for bpf4-1.4.2-cp310-cp310-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e752fb4819feee38b6192d85b328ee2cd34f61373a10741079d12491c983fcd5 |
|
MD5 | a4738dffc63bc978ee35bc56688a039f |
|
BLAKE2b-256 | f7a862dd50e56efc82a7f7b0cec8ed375533a2851b5a08292dbb5ecb05e3c6ed |
Hashes for bpf4-1.4.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a046a5971d9e32c3f245e783fddd18ab85cdb2d012804028259d8bd65f548b22 |
|
MD5 | b8ffa08248d588cdf913695e20a942eb |
|
BLAKE2b-256 | 6240b22b2ed622a37be6fba9ccf2b12f253b07d20d90aa2423afe50000f0c62e |
Hashes for bpf4-1.4.2-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1a92ae67f05bf1ffe607c3737da81eeb16017eb198867e29c926b4d0ceb9b43 |
|
MD5 | b9fdb167263ae992338f85763d0a98ca |
|
BLAKE2b-256 | 4bcd4da5f5f8e0f1b0dca72b7cb8a4b3f5648352aa1e79ccf9c90c08d52145f3 |
Hashes for bpf4-1.4.2-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c209611e63a0a42c0b8ab8864e55fee86448407e81a6b3db922449c82763000 |
|
MD5 | 88d42ab7c222199ce4a2e0cc0cf7eab7 |
|
BLAKE2b-256 | 4b6129326f6ba04fdcfbb1037d595b44358ac5514f2741260bd0ef3329d93258 |
Hashes for bpf4-1.4.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 53740235c4f309df2d50d17086b7e5b51fa8b0bfc745b7bb93b005591e52d091 |
|
MD5 | a9c66bd9bc82449f6b803d5c1e583a6a |
|
BLAKE2b-256 | ce276c07873a01927c5b165e6de932f78e21659c7f25ba42343677fda84813c9 |
Hashes for bpf4-1.4.2-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a3de5527ac5ea515fe6ec6027dcb2bc98204262d6cf436b324e3ba86f4adabf8 |
|
MD5 | 1a1a3188607c5a8908cebbce282d9e43 |
|
BLAKE2b-256 | c96cdcba6d0ca711af598bfcdd962ed3d4c5b12469b2eb6f3ec7ce3413be1dcb |
Hashes for bpf4-1.4.2-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a529771afc270f985066bd111dc212ca5370bb376dc86d31a659e1de4ad78d7 |
|
MD5 | 72f64a6eeba280982fb0d87392f13a40 |
|
BLAKE2b-256 | 5711cee7955d7513bdb5d83df98949cce5109d63e9252799b85b8d5420f9b0f4 |
Hashes for bpf4-1.4.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd1053045eb972efae5d3c20e220d8f42a9c92ede25176b834debac4381b9bcc |
|
MD5 | f911c3958c585f56c28689ee8d1894cc |
|
BLAKE2b-256 | 52254170fcb38b2c46bc1f7e8100e580f00c4640660cba448cc5c92dac75f70b |
Hashes for bpf4-1.4.2-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7dbe5857b946760b0eef0bb2aeabac267a4491f1bbe43fb499c982cd2989cc02 |
|
MD5 | 0c67dba92b88723efbbeec674d1ce03e |
|
BLAKE2b-256 | 53fd773bba0ff11024357c4843339f39c2e0c127a10e0a362a8518b761235a81 |