Skip to main content

Python CFFI bindings for Pijavski C++ function to calculate minimum of a given function.

Project description

Pijavski

This is an example of how to use CFFI to call a Pijavski function written in C++ that optimises a test function and returns the minimum.

Installation

To install simply type:

$ pip install pijavski

Usage

To test, open a python console and import the package. The function pijavski.get_minimum, with arguments lip, xl, xu, precision and maxiter, prints res, x0, f, prec, maxit as result.

>>> import pijavski
>>> pijavski.get_minimum()
0 -5323.428786928975 1.2546522006214123e-09 3.5218863499790176 65533

Defining custom functions to optimise

The function to optimise needs to be declared as a callback function for CFFI so that the Pijavski program can process it. The user can implement up to 20 functions following the conditions below:

  • The function definition needs to be preceeded by @ffi.def_extern().

  • The function name must be fun[1-20] (suffix between 1 and 20) as this is how the callback function is defined in the CFFI builder.

  • When writing the function, arguments f and x need to be declared as if they were pointers using the bracket notation f[] and x[].

  • Use numpy math functions.

Example:

>>> import numpy as np
>>> from pijavski import get_minimum, ffi, lib
>>> # Simple declaration of f = -cos^2(x) as callback function.
>>> @ffi.def_extern()
... def fun1(f, x):
...     f[0] = (-1)*np.cos(x[0])**2
>>> # Simple declaration of f = sin^2(x) as callback function.
>>> @ffi.def_extern()
... def fun2(f, x):
...     f[0] = np.sin(x[0])**2
>>> # Call get_minimum for fun1
>>> get_minimum(func=lib.fun1, xl=-100, xu=100)
0 -1.0 4.6838846e-317 4.6838846e-317 1 
>>> # Call get_minimum for fun2
>>> get_minimum(func=lib.fun2, xl=-100, xu=100)
0 -1.0 4.6838846e-317 4.6838846e-317 1 

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

pijavski-0.0.6.tar.gz (267.1 kB view details)

Uploaded Source

File details

Details for the file pijavski-0.0.6.tar.gz.

File metadata

  • Download URL: pijavski-0.0.6.tar.gz
  • Upload date:
  • Size: 267.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.5

File hashes

Hashes for pijavski-0.0.6.tar.gz
Algorithm Hash digest
SHA256 cc8f6f053d5ff40bb2221d98c4cc6a7fd8f0baa49d6232914c5a7ca2a90a94f0
MD5 37d5b1eb5d4c0b44fb1d3a520e9048eb
BLAKE2b-256 88488db79c67ae8ec82218673fed7aaf14ce97540e2de88c56d7b79771870983

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