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.5.0-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea9d676347b2d5681f20c33d08a8c6372f780bfe50bc3717d26bd5b27b803f10 |
|
MD5 | b0a545b0efc72946b4ec7553ef327740 |
|
BLAKE2b-256 | 7de9861238bf5e535bf97a19955680df0015ea2a9562af4fe80d7715a1059404 |
Hashes for bpf4-1.5.0-cp310-cp310-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4525d7a5ccbfaa9074ea7125d68663833b9e9332f967ee2b3f2009f61b44a49c |
|
MD5 | 4edeb2c710c83a566a219df91aa1bb0c |
|
BLAKE2b-256 | 9ac7ef844090581b70c84b56cdec0d58eb881985098c7aebfea74e281d0a8c29 |
Hashes for bpf4-1.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a38e40250bc919e39f7e0181b7a94f14eb571c6fadf85be4e235b88459186b1a |
|
MD5 | 3f82133f7fa4437339218e9c4f2b51e3 |
|
BLAKE2b-256 | 3430105ba23513638d9aa49a0130504011059fb9633b099da624deab7e1fd849 |
Hashes for bpf4-1.5.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 50636531d428e6926f7f6df61dc6bd0f795f5d62a929f9f416b2ccb90f66a195 |
|
MD5 | 465bb41ab8e31b1ecc3ac3bff8c3c817 |
|
BLAKE2b-256 | 4696a24962f5195f5b38e86e527f29e34f352846602b6904f705c42b83d1d635 |
Hashes for bpf4-1.5.0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7bb0a0d42cac6a2949cf8c6cc4bd9fa38b939fcbd319fa5caf8afb448fbf1ed1 |
|
MD5 | 1cb252bb7712d9857a86d7b40ab16336 |
|
BLAKE2b-256 | 72639fb1acb4a2b7942ea0e08ce0a47ad84d8c35540dbf6f157d129bb0171d60 |
Hashes for bpf4-1.5.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0d9bf7b572ab02a0391b80d7443fa91962f704a4dcd4cb0e9058f1b3bd0b8036 |
|
MD5 | 0638a8d7764fd633a93513405bddcd69 |
|
BLAKE2b-256 | 1fc1bd609ba1994e235f8bec8644d05b996e518a308514adb46648eea6e8e01e |
Hashes for bpf4-1.5.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 81aec1f9aded258f0ac0f5b17d6d84bb6eab9f70a48929d33b1b521ff378a9ab |
|
MD5 | c54f1954331b9a21194bf7ff9e9366a5 |
|
BLAKE2b-256 | 0b4d8d9ebe780cf19284dba7dd359344bc0f32b931dc5539a67eb83e65076b24 |
Hashes for bpf4-1.5.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed4d89f6ea2ec34e443b8aa1378f36dd5399c060dc7f7771a20178bdd6b5119d |
|
MD5 | 24027252d975e1c07a986cc9c4807d91 |
|
BLAKE2b-256 | be22515016684ed034be514aab40ee0e53f681f7789ae94ca93e7e2bc88400ad |
Hashes for bpf4-1.5.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 412a51e361f49e1cb8947472520f5e952e3dbd2fc0a3d229e8c585ac4d3c5b36 |
|
MD5 | 6da4925a292de9e9117db0595c520617 |
|
BLAKE2b-256 | ae42bc18eeb1716052e61c956917dc7828ea78af06da084f7f0ac6929a066e71 |
Hashes for bpf4-1.5.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb996446ed0a08eb4d0bdeeb8db069e384557eb5452029241cd95021f3bb831f |
|
MD5 | e045289ddbd3e54eaf76fe7e9a1f4661 |
|
BLAKE2b-256 | 666b49bd0980f1b1b4e955c9d2d5adc941f01f169d60f469982f63a422ef48e1 |