Skip to main content

Piece-wise interpolation and lazy evaluation in cython

Project description

bpf4

wheels

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')

1

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()  

2

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bpf4-1.13.0.tar.gz (100.9 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

bpf4-1.13.0-cp313-cp313-win_amd64.whl (456.0 kB view details)

Uploaded CPython 3.13Windows x86-64

bpf4-1.13.0-cp313-cp313-win32.whl (401.5 kB view details)

Uploaded CPython 3.13Windows x86

bpf4-1.13.0-cp312-cp312-win_amd64.whl (456.5 kB view details)

Uploaded CPython 3.12Windows x86-64

bpf4-1.13.0-cp312-cp312-win32.whl (400.6 kB view details)

Uploaded CPython 3.12Windows x86

bpf4-1.13.0-cp311-cp311-win_amd64.whl (460.9 kB view details)

Uploaded CPython 3.11Windows x86-64

bpf4-1.13.0-cp311-cp311-win32.whl (406.9 kB view details)

Uploaded CPython 3.11Windows x86

bpf4-1.13.0-cp310-cp310-win_amd64.whl (455.4 kB view details)

Uploaded CPython 3.10Windows x86-64

bpf4-1.13.0-cp310-cp310-win32.whl (406.1 kB view details)

Uploaded CPython 3.10Windows x86

bpf4-1.13.0-cp39-cp39-win_amd64.whl (455.6 kB view details)

Uploaded CPython 3.9Windows x86-64

bpf4-1.13.0-cp39-cp39-win32.whl (406.1 kB view details)

Uploaded CPython 3.9Windows x86

File details

Details for the file bpf4-1.13.0.tar.gz.

File metadata

  • Download URL: bpf4-1.13.0.tar.gz
  • Upload date:
  • Size: 100.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for bpf4-1.13.0.tar.gz
Algorithm Hash digest
SHA256 54652858bbbf5e6fce86dc54082fa0f043c33d6b4ebd5fb3bdb252ab21dbe3ab
MD5 66ecca29b5529dc42cd69e3a7d668aad
BLAKE2b-256 3ec3f3943ada4df73514670517a96115fbea260a23aa2e41ec9d5d868a7a4f81

See more details on using hashes here.

File details

Details for the file bpf4-1.13.0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: bpf4-1.13.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 456.0 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.13

File hashes

Hashes for bpf4-1.13.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 27c2838b824be97d0ce34e434cab6f9af4410f8c1d9ab2f3184b0f7151c855f7
MD5 dc397553ed05eb788f78e4e3894ff461
BLAKE2b-256 2bffce2cd5298633d97facf642ed8d418d3d846b7b9b414e260ff57ac8c23192

See more details on using hashes here.

File details

Details for the file bpf4-1.13.0-cp313-cp313-win32.whl.

File metadata

  • Download URL: bpf4-1.13.0-cp313-cp313-win32.whl
  • Upload date:
  • Size: 401.5 kB
  • Tags: CPython 3.13, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.13

File hashes

Hashes for bpf4-1.13.0-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 979e9d2c6a28938a8cc8a6722f891a6c1c65a530657f4c6b541b5f972b7a9419
MD5 2c122e550c8212f0662720b4c46dd64b
BLAKE2b-256 1a4df40507ffd34886d74d502c675f6e8c51a55b95927378e603d8a269e1aea5

See more details on using hashes here.

File details

Details for the file bpf4-1.13.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: bpf4-1.13.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 456.5 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.13

File hashes

Hashes for bpf4-1.13.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 9985ecd4abd9289832ffd13e4f63122b56d5d67aa47b37aaaa0883b14dee3742
MD5 5d1283dba214a3ef789102edc52ee2a4
BLAKE2b-256 cfbedb3ac404abf2f4124a25cc1210f3d7de9588f00b3fabbb4c4d44ddc8f9db

See more details on using hashes here.

File details

Details for the file bpf4-1.13.0-cp312-cp312-win32.whl.

File metadata

  • Download URL: bpf4-1.13.0-cp312-cp312-win32.whl
  • Upload date:
  • Size: 400.6 kB
  • Tags: CPython 3.12, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.13

File hashes

Hashes for bpf4-1.13.0-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 301dbd79d35f5a2cdc493839078d7e65f86d41cf7187fe431d372225e1ee532f
MD5 da26cd25a38ec756bfabe534d08c2983
BLAKE2b-256 2d93a5c0d76b7adff4c34bf11a63eb77e8f571d18387e99ec2f3fafbb01e1ce1

See more details on using hashes here.

File details

Details for the file bpf4-1.13.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: bpf4-1.13.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 460.9 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.13

File hashes

Hashes for bpf4-1.13.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 fe067b759f7729f7a24c312ab6e1eb297aeee32ed0f88576aca05a4d44e72d31
MD5 b05eba32b4a4212c27cab8208c5bc16d
BLAKE2b-256 b517f201296c4e0839481e350a4d948c070ea907745e3fa50d30c7555a5d22a9

See more details on using hashes here.

File details

Details for the file bpf4-1.13.0-cp311-cp311-win32.whl.

File metadata

  • Download URL: bpf4-1.13.0-cp311-cp311-win32.whl
  • Upload date:
  • Size: 406.9 kB
  • Tags: CPython 3.11, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.13

File hashes

Hashes for bpf4-1.13.0-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 0a6c4709725e5a0adb54a88613ff599b1d4b6105614f1f7cf26f36b6a38d94dd
MD5 d206f66050a561879ddb6c941baf8713
BLAKE2b-256 9475c6119f15e579934d8f4b233b75b665d1cf18c21f29488afe26fe497bb4b6

See more details on using hashes here.

File details

Details for the file bpf4-1.13.0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: bpf4-1.13.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 455.4 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.13

File hashes

Hashes for bpf4-1.13.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 e9dce9db5f0eda0e3a95e87c06148e206edd24b992f48364e974cd6713585b2f
MD5 e921ddb084e24d989d3114c2a36415c4
BLAKE2b-256 d78e1f2a26bb30b4c64e535e3ef9fff66e99c5416b3fc90378ad816bc2839705

See more details on using hashes here.

File details

Details for the file bpf4-1.13.0-cp310-cp310-win32.whl.

File metadata

  • Download URL: bpf4-1.13.0-cp310-cp310-win32.whl
  • Upload date:
  • Size: 406.1 kB
  • Tags: CPython 3.10, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.13

File hashes

Hashes for bpf4-1.13.0-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 8bb8774bad8bb474253e849d51fc5fbecf57962852c483bc2abccf007a9c232f
MD5 5e01870bea88fcf0e13954bd212e2837
BLAKE2b-256 0ee02daca6b066bab31607470486de1770fc6593eba9a71400acc181dd5356cc

See more details on using hashes here.

File details

Details for the file bpf4-1.13.0-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: bpf4-1.13.0-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 455.6 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.13

File hashes

Hashes for bpf4-1.13.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 a8ca25832d307baaaa6f3964aa4a76b1e89b1c80cce06642c165ef6b602e6f21
MD5 2084d0fd1a8e18673d6aeebbe26d93bf
BLAKE2b-256 2edd3ff1bd8ba367fbe6c7f719185de5a49ea350783cf5c6bafbd4aa2e552b09

See more details on using hashes here.

File details

Details for the file bpf4-1.13.0-cp39-cp39-win32.whl.

File metadata

  • Download URL: bpf4-1.13.0-cp39-cp39-win32.whl
  • Upload date:
  • Size: 406.1 kB
  • Tags: CPython 3.9, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.13

File hashes

Hashes for bpf4-1.13.0-cp39-cp39-win32.whl
Algorithm Hash digest
SHA256 bde26442400e2e2a350028fede6dd2c1ea0d8ee2c233b61959eeb98d0a7e235c
MD5 4e395b8ebd7e571150417afa5aa2fc39
BLAKE2b-256 023e3074d598e13e63a34195fe12a82f240727c067c4fc33e14a01758addd191

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page