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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

bpf4-1.13.1-cp313-cp313-win_amd64.whl (461.3 kB view details)

Uploaded CPython 3.13Windows x86-64

bpf4-1.13.1-cp313-cp313-win32.whl (407.1 kB view details)

Uploaded CPython 3.13Windows x86

bpf4-1.13.1-cp312-cp312-win_amd64.whl (461.7 kB view details)

Uploaded CPython 3.12Windows x86-64

bpf4-1.13.1-cp312-cp312-win32.whl (406.7 kB view details)

Uploaded CPython 3.12Windows x86

bpf4-1.13.1-cp311-cp311-win_amd64.whl (466.2 kB view details)

Uploaded CPython 3.11Windows x86-64

bpf4-1.13.1-cp311-cp311-win32.whl (412.7 kB view details)

Uploaded CPython 3.11Windows x86

bpf4-1.13.1-cp310-cp310-win_amd64.whl (461.1 kB view details)

Uploaded CPython 3.10Windows x86-64

bpf4-1.13.1-cp310-cp310-win32.whl (411.3 kB view details)

Uploaded CPython 3.10Windows x86

bpf4-1.13.1-cp39-cp39-win_amd64.whl (461.3 kB view details)

Uploaded CPython 3.9Windows x86-64

bpf4-1.13.1-cp39-cp39-win32.whl (411.4 kB view details)

Uploaded CPython 3.9Windows x86

File details

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

File metadata

  • Download URL: bpf4-1.13.1-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 461.3 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.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 7f1cb53647f64c8ce1bd8b941a967b36068d5c4b52d9da76907522e44ab77e39
MD5 1a985b7659eac1dab89e5a5fc4b6178d
BLAKE2b-256 df1661d41ff16e28f1c207113d6293e2eef1b131c56d790b2be947f6b7c2e51b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bpf4-1.13.1-cp313-cp313-win32.whl
  • Upload date:
  • Size: 407.1 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.1-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 852d3752ee52403093b0a42365ede2503991559f3a24078656fe46adb1fcd3d0
MD5 c772726dcb2ece7b3d97d8de029f4086
BLAKE2b-256 819bbe47ccc55411f25790e8085dcb77b77a803e3dd999e22f450a68fa796020

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bpf4-1.13.1-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 461.7 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.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 e6362ba54a52bb6c3b2e660ae9c34030427b7eaee8bf05984850c49744f172ca
MD5 e5ea597fb24f48fcff6b65eab221a45c
BLAKE2b-256 051284410f496f2ab1e1983959223af1f232e391896a684ee321d34b2e8edd23

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bpf4-1.13.1-cp312-cp312-win32.whl
  • Upload date:
  • Size: 406.7 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.1-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 266e805c577e42f28219f16d364f8db08a5fba7f04274ceeb9dd27194a89c413
MD5 806178b2c5db0c2d8a724cca44a491d7
BLAKE2b-256 4d738a7cc31f309193cf9dc8091a0c49fd827c28a94a3095891943de492f060d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bpf4-1.13.1-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 466.2 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.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 0b1e38072219a874468f3ab41ed0d6d69e25bfbc0e1abb9e252abaf9ff458d20
MD5 c7e53d8730eded34893712b0492fff7c
BLAKE2b-256 61385434d6be176c9538681785358c7d95609f7a354d53d83ad6c9e0816c9ea1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bpf4-1.13.1-cp311-cp311-win32.whl
  • Upload date:
  • Size: 412.7 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.1-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 9f2ab074feb1fc5c183db1a91bc21e25ac30f0be940a1477fb1b3ed98bcbe719
MD5 ace7d35660502d78516096d00e4a8cb1
BLAKE2b-256 5841177d58664e3190470ee9a9674d33bf81849e89235999bafd83bcda4f1d34

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bpf4-1.13.1-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 461.1 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.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 bfd2cc21787b54012161cd5fab922ad931e09282f52ed1c46d2b900590ec523f
MD5 916c82cffffd7592431a2e734670caee
BLAKE2b-256 ccd754b7f5517248f3bacbd133674f514bc4ca1a9a0f2af4c3e141771db33fc7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bpf4-1.13.1-cp310-cp310-win32.whl
  • Upload date:
  • Size: 411.3 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.1-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 c38fe66c4e59c234b0f3da83afddb3e90852b9ff0d14a69eb6f8351b3b1a5c02
MD5 a120a25eb4f716ea380ddbb02f8b34f7
BLAKE2b-256 8a8abe09d2a6161c10e702cb6fdf62b30ee84e7f520c296c9a37675c90655db6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bpf4-1.13.1-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 461.3 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.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 09202f68676b0eca7bcaefb09879adff6e80bcfa0111ac2e5c129a05b840e52a
MD5 b6fd728fd60c5a6f018dcce8cecf2484
BLAKE2b-256 59969957e5cfeec618adfefb0f91a7310820905ac37f3119734fec82fc61180e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bpf4-1.13.1-cp39-cp39-win32.whl
  • Upload date:
  • Size: 411.4 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.1-cp39-cp39-win32.whl
Algorithm Hash digest
SHA256 cedba6d955bc6aed108792a8196e67fa50c696d6d1ca68402a259e8957614575
MD5 7b9f5ab19fffdedf175e4bd68732a4ab
BLAKE2b-256 799bcf3d4ad89995d98a6b3064e4db6e169d88d6b2b5a1d4131a28658b8b5e50

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