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.5-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b7c54144ff616c9ca746ddaa539b85e9a301f7c262eec6230898c1e4571af35 |
|
MD5 | 1d15d1372b2edc9695e4c3b67b99477c |
|
BLAKE2b-256 | 34350328394128f52e0032b6ca83b6aebdeb942adb189e6a48ced8964aff8a2e |
Hashes for bpf4-1.4.5-cp310-cp310-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 58179eb1866c6e5307acc8a2e5c01c4078694428f89dba311b4be31c1ae16b80 |
|
MD5 | be1264b329a136352625076264af191c |
|
BLAKE2b-256 | 445fc9270609a3e412d1bdbb7844080012f69f9f38deacce044f234ccb504dcb |
Hashes for bpf4-1.4.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ede9580e5bc77958e88a6afef7f952d418b7566145ed2c9dafb0965bfbd1e843 |
|
MD5 | eb77184e91f51db2c0fbc4c7d9db58ad |
|
BLAKE2b-256 | 9bc4e256121df71d4b9b9ff623ed42158e41fea66c07b209dcd5b2887236e8fd |
Hashes for bpf4-1.4.5-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6bda2a5e97824d2d04fd14e6ebd2522dd273eef502844780396ab1a999cabdb2 |
|
MD5 | 791f271f7748758333144058c126dda7 |
|
BLAKE2b-256 | 25b321babef11dadcf06441d2990b5f4c6b9a012669380a0c81eeda21a0c3d6e |
Hashes for bpf4-1.4.5-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c3912b9cdd7e9b773b39c8b2bc9646f2c9c9dee2a2832dc656ab2f572337c260 |
|
MD5 | 298b5bc97a75b3fd4477749ebcacf95a |
|
BLAKE2b-256 | c2d7b5dfd65bd6dc02f7b6c13c2e8f1707f3ab9b3def177385a53f5ae2264301 |
Hashes for bpf4-1.4.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 30ab7b0f196d30d516e92233d106167c7a6d724f6028305517c2c668287e32e3 |
|
MD5 | 88bf9ccc56af983a1e952a5066288b39 |
|
BLAKE2b-256 | 3c1dbc887fcc70b913cbc7b61f27f6abdcc22490eab1b7ea05979f3c5e37b3ab |
Hashes for bpf4-1.4.5-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3f9e1dc1386f701a30001a8b48bc38d5fc71608e1d56ac226dd2a58d20ed4671 |
|
MD5 | 111a068a7c2b5c4b28a4a280e1ad1e54 |
|
BLAKE2b-256 | b65af29159c6a9ca1fa3c80ad5c5ca8f12ca925bce070c3a3efce73919584022 |
Hashes for bpf4-1.4.5-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c1e8173740cdb428b192f289dabebbdedbc32f2a709c9521170097cfba60a21 |
|
MD5 | 63eca1782be23a9ae89c48a79afb82d0 |
|
BLAKE2b-256 | 50f02a0dbd640cfcb7b1252327043cdfae36419ac9329c39f04864f59faf5e65 |
Hashes for bpf4-1.4.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 026946f5e7c30cc71b6e932cbe83c1f3725129753094868d06247b07974f8a15 |
|
MD5 | 3dbdef2582f6fab82027043ebb7fdb9a |
|
BLAKE2b-256 | 76ff6d4f46ab9b2e2250cfedc457ac3371110c747ce97f0df6151aebd543cefd |
Hashes for bpf4-1.4.5-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5563b539dc8db76d751c846e48ba58ef365c90467f83c76d69988a1258c41e12 |
|
MD5 | c6da55e0881edd770f2ba92ba6aa196c |
|
BLAKE2b-256 | 34fd5c0a6714c755428003532529a7cbeb10b668fadd251bf1e6694860fa0ec5 |