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.2-cp313-cp313-win_amd64.whl (461.7 kB view details)

Uploaded CPython 3.13Windows x86-64

bpf4-1.13.2-cp313-cp313-win32.whl (407.6 kB view details)

Uploaded CPython 3.13Windows x86

bpf4-1.13.2-cp312-cp312-win_amd64.whl (462.1 kB view details)

Uploaded CPython 3.12Windows x86-64

bpf4-1.13.2-cp312-cp312-win32.whl (407.3 kB view details)

Uploaded CPython 3.12Windows x86

bpf4-1.13.2-cp311-cp311-win_amd64.whl (466.7 kB view details)

Uploaded CPython 3.11Windows x86-64

bpf4-1.13.2-cp311-cp311-win32.whl (413.2 kB view details)

Uploaded CPython 3.11Windows x86

bpf4-1.13.2-cp310-cp310-win_amd64.whl (461.5 kB view details)

Uploaded CPython 3.10Windows x86-64

bpf4-1.13.2-cp310-cp310-win32.whl (411.9 kB view details)

Uploaded CPython 3.10Windows x86

bpf4-1.13.2-cp39-cp39-win_amd64.whl (461.7 kB view details)

Uploaded CPython 3.9Windows x86-64

bpf4-1.13.2-cp39-cp39-win32.whl (412.1 kB view details)

Uploaded CPython 3.9Windows x86

File details

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

File metadata

  • Download URL: bpf4-1.13.2-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 461.7 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.2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 d67730018616e9025bd1d0cfe076bd2d9cf1febc400aeb412dd07fa437da7594
MD5 8742eeac5ea790fb50f0625efcacd7ac
BLAKE2b-256 a10b7f490a87f1af8d496944b397b191474accdeaf5a7737c17aeece50dd8e23

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bpf4-1.13.2-cp313-cp313-win32.whl
  • Upload date:
  • Size: 407.6 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.2-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 61821f8d32263457a30753b6cbbce705769f0656523c58ad67bb47e9129d9d5a
MD5 f2e0d3b90d426275b0002a5124e29e6c
BLAKE2b-256 19c5d1336766df192388b99e692c751c7ba18a35388f62bf17541c5c4175bbd1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bpf4-1.13.2-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 462.1 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.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 b60e53bd0a9e6ba7b35e7bd823a08ee11c2b3d6e7bfab8e6a8d865b9e57c42d3
MD5 38c083931677c6c4e13ecba4ac576f66
BLAKE2b-256 6299baf6e0aee2cde080def7286be6b47fad083c018d8289cf43f0240bbf8b74

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bpf4-1.13.2-cp312-cp312-win32.whl
  • Upload date:
  • Size: 407.3 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.2-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 0790e6a2a7a7992141f9435bf4fb78566e9c8d224fedbad2d9b7fb5dce2de06d
MD5 57c8d6b9bf7b152a20804d16ddd8b584
BLAKE2b-256 3b9cc81ef6d724d34d8b0b3f82ebf3c9507dab5def369db3b45c1d0d6ff76724

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bpf4-1.13.2-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 466.7 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.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 8016cb193c571607516fbfc64c8db66debbee70ea9af527d06106edfe77e892c
MD5 e92b4c638980d1e2d522f0adfa5d96c2
BLAKE2b-256 b0e14526b56af9dc0b7738aad4cf4e154e9a0836acc5100b6db98abf9e85175a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bpf4-1.13.2-cp311-cp311-win32.whl
  • Upload date:
  • Size: 413.2 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.2-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 f7ae3e66822bda69e18799e1d16d53d16442f351c3f455a5a833ecc61d933804
MD5 ee9c348b4acf6351089b500276e1065c
BLAKE2b-256 4dbe7dd27a7b50e98cbae7329497a89c91dd1cc2b8e7b92e803dd21f832a3e7e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bpf4-1.13.2-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 461.5 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.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 9baf838f055b0b07cf980b1c4b7b4f0506b5b6db19d0b59f68afd091b9983a6f
MD5 587bfe31f7bc63f3708c456acf879572
BLAKE2b-256 2f2417dd7e22e28841f2b62f318f950a83229bcb619ce0b8ef56cac1903dd987

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bpf4-1.13.2-cp310-cp310-win32.whl
  • Upload date:
  • Size: 411.9 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.2-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 d19026f314a9c59a2bf7b5a379ffddfb2327c44f715a8371821acce81b48981e
MD5 f5e872992afe6d64c94ea9337eeb774c
BLAKE2b-256 f28712b2bcc7237609d7758cbb099a1dd9f2cf5ac4c28ed38d74c9f21f5e7a0e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bpf4-1.13.2-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 461.7 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.2-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 78e11a65735728c0c3e9d1450f5c6cd4371c07000564e7725bfaf8a38e3dba69
MD5 57335d0ee93f029549da112e152bc812
BLAKE2b-256 931ef01396db7c85c744e40ba58265b33dec61778a08a031c77935c93f9db851

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bpf4-1.13.2-cp39-cp39-win32.whl
  • Upload date:
  • Size: 412.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.2-cp39-cp39-win32.whl
Algorithm Hash digest
SHA256 aa444fadda796391a8b2d9068271454c6987f58fb4a43eb274ffd6fbaa2ca5d2
MD5 ca69d08142ecc517e8b7babddc1bbf99
BLAKE2b-256 e22a8c0ebfc963ff609f245ad26b89451e72d4817dca03c8a7a5b4e41bd19ee4

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