Skip to content

Commit

Permalink
Bettered some pybind11 examples
Browse files Browse the repository at this point in the history
  • Loading branch information
lformaggia committed Aug 13, 2024
1 parent 11c6189 commit 69158f7
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 58 deletions.
2 changes: 2 additions & 0 deletions Examples/src/pybind11/OneDMesh/pyOneDMesh.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
import OneDMesh as odm
Expand All @@ -7,6 +8,7 @@ def h(x):
return 1.05+np.sin(x)
gen=odm.VariableSize(d,h,100)
m2=odm.Mesh1D(gen) # max 100 elements non-uniformly distributed
print(m2)
m1v=np.zeros([m1.numNodes(),1])
m2v=0.1*np.ones([m2.numNodes(),1])

Expand Down
57 changes: 31 additions & 26 deletions Examples/src/pybind11/basicOptim/pyBasicOptim.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
#include "pybind11/pybind11.h"
#include "basicOptimization.hpp" // Include the header file where the functions are defined
#include "pybind11/functional.h"
#include "pybind11/pybind11.h"
#include "pybind11/stl.h"
#include "basicOptimization.hpp" // Include the header file where the functions are defined


namespace py = pybind11;
using namespace apsc;
/*
I need to explicitely instantiate the templates for the functions I want to export to python.
I need to explicitely instantiate the templates for the functions I want to
export to python.
*/
using funct=const std::function<double (double)> &;
template std::tuple<double, bool> apsc::golden_search(funct f, double a, double b, double tol,unsigned maxIter);
template std::array<double,2> apsc::Brent_glomin(double a, double b, double x0, double m, double e, double t,funct f);
template std::array<double,2> apsc::Brent_local_min(double a, double b, double tol,funct f);
template std::tuple<double, double, int> apsc::bracketIntervalMinimum(funct f, double x1, double h,unsigned maxIter);


PYBIND11_MODULE(basicOptim, m) {
m.def("golden_search", &golden_search<funct>,py::arg("f"),py::arg("a"),py::arg("b"),py::arg("tol"),py::arg("maxIter"),
using funct = const std::function<double(double)> &;
template std::tuple<double, bool>
apsc::golden_search(funct f, double a, double b, double tol, unsigned maxIter);
template std::array<double, 2> apsc::Brent_glomin(double a, double b, double x0,
double m, double e, double t,
funct f);
template std::array<double, 2> apsc::Brent_local_min(double a, double b,
double tol, funct f);
template std::tuple<double, double, int>
apsc::bracketIntervalMinimum(funct f, double x1, double h, unsigned maxIter);

PYBIND11_MODULE(basicOptim, m)
{
m.def("golden_search", &golden_search<funct>),
py::arg("f"),py::arg("a"),py::arg("b"),py::arg("tol"),py::arg("maxIter"),
R"pbdoc(
Golden search function.
Expand All @@ -34,9 +40,10 @@ PYBIND11_MODULE(basicOptim, m) {
tuple: A tuple containing the minimum value and a boolean indicating if the optimization converged.
)pbdoc");

m.def("Brent_glomin", &Brent_glomin<funct>, py::arg("a"),py::arg("b"),py::arg("x0"),py::arg("m"),py::arg("e"),py::arg("t"),py::arg("f"),
R"pbdoc(

m.def("Brent_glomin", &Brent_glomin<funct>, py::arg("a"), py::arg("b"),
py::arg("x0"), py::arg("m"), py::arg("e"), py::arg("t"), py::arg("f"),
R"pbdoc(
Brent glomin function.
This function performs Brent's method for global optimization to find the minimum of a given function.
Expand All @@ -54,9 +61,10 @@ PYBIND11_MODULE(basicOptim, m) {
array: An array containing the minimum value and the number of iterations performed.
)pbdoc");

m.def("Brent_local_min", &Brent_local_min<funct>, py::arg("a"),py::arg("b"),py::arg("tol"),py::arg("f"),
R"pbdoc(

m.def("Brent_local_min", &Brent_local_min<funct>, py::arg("a"),
py::arg("b"), py::arg("tol"), py::arg("f"),
R"pbdoc(
Brent local min function.
This function performs Brent's method for local optimization to find the minimum of a given function.
Expand All @@ -71,9 +79,10 @@ PYBIND11_MODULE(basicOptim, m) {
array: An array containing the minimum value and the number of iterations performed.
)pbdoc");

m.def("bracketIntervalMinimum", &bracketIntervalMinimum<funct>, py::arg("f"),py::arg("x1"),py::arg("h"),py::arg("maxIter"),
R"pbdoc(

m.def("bracketIntervalMinimum", &bracketIntervalMinimum<funct>,
py::arg("f"), py::arg("x1"), py::arg("h"), py::arg("maxIter"),
R"pbdoc(
Bracket interval minimum function.
This function finds an interval containing the minimum of a given function.
Expand All @@ -89,7 +98,3 @@ PYBIND11_MODULE(basicOptim, m) {
)pbdoc");
}




90 changes: 58 additions & 32 deletions Examples/src/pybind11/basicZeroFun/pyZeroFun.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
#include <functional>
#include "pybind11/pybind11.h"
#include "pybind11/functional.h"
#include "basicZeroFun.hpp"
#include "pybind11/functional.h"
#include "pybind11/pybind11.h"
#include <functional>

// you need to instantiate the templates
using funct=const std::function<double (double)> &;
using funct = const std::function<double(double)> &;
using functl = const std::function<long double(long double)> &;

template double apsc::regulaFalsi(funct f, double a, double b, double tol, double tola);
template double apsc::regulaFalsi(funct f, double a, double b, double tol,
double tola);
template double apsc::bisection(funct f, double a, double b, double tol);
template std::tuple<double, bool> apsc::secant(funct f, double a, double b, double tol, double tola,unsigned int maxit);
template std::tuple<double, bool> apsc::Newton(funct f, funct df, double a, double tol, double tola,unsigned int maxit);
template std::tuple<double, double, bool> apsc::bracketInterval(funct f, double x1, double h,unsigned int maxIter);
template std::tuple<double, bool> apsc::brent_search(funct f, double a, double b, double tol,unsigned int maxit);
template double apsc::bisection(functl f, double a, double b, double tol);
template std::tuple<double, bool> apsc::secant(funct f, double a, double b,
double tol, double tola,
unsigned int maxit);
template std::tuple<double, bool> apsc::Newton(funct f, funct df, double a,
double tol, double tola,
unsigned int maxit);
template std::tuple<double, double, bool>
apsc::bracketInterval(funct f, double x1, double h, unsigned int maxIter);
template std::tuple<double, bool>
apsc::brent_search(funct f, double a, double b, double tol, unsigned int maxit);

/*!
python binder.
Yiu can make it better by adding descriptors
*/
PYBIND11_MODULE(zeroFun, m) {
using namespace apsc;
namespace py=pybind11;
// Bind the regulaFalsi function
m.def("regulaFalsi", &regulaFalsi<funct>, py::arg("f"), py::arg("a"), py::arg("b"), py::arg("tol"), py::arg("tola"),
R"pbdoc(
PYBIND11_MODULE(zeroFun, m)
{
using namespace apsc;
namespace py = pybind11;
// Bind the regulaFalsi function
m.def("regulaFalsi", &regulaFalsi<funct>, py::arg("f"), py::arg("a"),
py::arg("b"), py::arg("tol"), py::arg("tola"),
R"pbdoc(
Find the zero of a function using the regula falsi method.
:param f: The function to find the zero of.
Expand All @@ -34,9 +45,10 @@ PYBIND11_MODULE(zeroFun, m) {
:return: The zero of the function.
)pbdoc");

// Bind the bisection function
m.def("bisection", &bisection<funct>, py::arg("f"), py::arg("a"), py::arg("b"), py::arg("tol"),
R"pbdoc(
// Bind the bisection function
m.def("bisectionld", &bisection<functl>, py::arg("f"), py::arg("a"),
py::arg("b"), py::arg("tol"),
R"pbdoc(
Find the zero of a function using the bisection method.
:param f: The function to find the zero of.
Expand All @@ -45,10 +57,22 @@ PYBIND11_MODULE(zeroFun, m) {
:param tol: The tolerance for the zero.
:return: The zero of the function.
)pbdoc");
m.def("bisection", &bisection<funct>, py::arg("f"), py::arg("a"),
py::arg("b"), py::arg("tol"),
R"pbdoc(
Find the zero of a function using the bisection method.
// Bind the secant function
m.def("secant", &secant<funct>, py::arg("f"), py::arg("a"), py::arg("b"), py::arg("tol"), py::arg("tola"), py::arg("maxit"),
R"pbdoc(
:param f: The function to find the zero of.
:param a: The lower bound of the interval.
:param b: The upper bound of the interval.
:param tol: The tolerance for the zero.
:return: The zero of the function.
)pbdoc");

// Bind the secant function
m.def("secant", &secant<funct>, py::arg("f"), py::arg("a"), py::arg("b"),
py::arg("tol"), py::arg("tola"), py::arg("maxit"),
R"pbdoc(
Find the zero of a function using the secant method.
:param f: The function to find the zero of.
Expand All @@ -60,9 +84,10 @@ PYBIND11_MODULE(zeroFun, m) {
:return: A tuple containing the zero of the function and a boolean indicating convergence.
)pbdoc");

// Bind the Newton function
m.def("Newton", &Newton<funct, funct>, py::arg("f"), py::arg("df"), py::arg("a"), py::arg("tol"), py::arg("tola"), py::arg("maxit"),
R"pbdoc(
// Bind the Newton function
m.def("Newton", &Newton<funct, funct>, py::arg("f"), py::arg("df"),
py::arg("a"), py::arg("tol"), py::arg("tola"), py::arg("maxit"),
R"pbdoc(
Find the zero of a function using the Newton method.
:param f: The function to find the zero of.
Expand All @@ -74,9 +99,10 @@ PYBIND11_MODULE(zeroFun, m) {
:return: A tuple containing the zero of the function, a boolean indicating convergence, and the number of iterations.
)pbdoc");

// Bind the bracketInterval function
m.def("bracketInterval", &bracketInterval<funct>, py::arg("f"), py::arg("x1"), py::arg("h"), py::arg("maxIter"),
R"pbdoc(
// Bind the bracketInterval function
m.def("bracketInterval", &bracketInterval<funct>, py::arg("f"), py::arg("x1"),
py::arg("h"), py::arg("maxIter"),
R"pbdoc(
Find an interval that brackets a zero of a function.
:param f: The function to find the zero of.
Expand All @@ -86,9 +112,10 @@ PYBIND11_MODULE(zeroFun, m) {
:return: A tuple containing the lower and upper bounds of the bracketed interval, and a boolean indicating convergence.
)pbdoc");

// Bind the brent_search function
m.def("brent_search", &brent_search<funct>, py::arg("f"), py::arg("a"), py::arg("b"), py::arg("tol"), py::arg("maxit"),
R"pbdoc(
// Bind the brent_search function
m.def("brent_search", &brent_search<funct>, py::arg("f"), py::arg("a"),
py::arg("b"), py::arg("tol"), py::arg("maxit"),
R"pbdoc(
Find the zero of a function using Brent's method.
:param f: The function to find the zero of.
Expand All @@ -99,6 +126,5 @@ PYBIND11_MODULE(zeroFun, m) {
:return: A tuple containing the zero of the function and a boolean indicating convergence.
)pbdoc");

m.doc() = "A set of utilities to find the zero of a scalar function";
m.doc() = "A set of utilities to find the zero of a scalar function";
};

1 change: 1 addition & 0 deletions Examples/src/pybind11/basicZeroFun/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def dfun(x) -> float:


print("zero by regula falsi", zero.regulaFalsi(fun,-0.3,2.,1.e-6,1.e-8))
print("zero by bisectionld", zero.bisectionld(fun,-0.3,2.,1.e-6))
print("zero by secant", zero.secant(fun,-0.3,2.,1.e-6,1.e-8,100))
print("zero by Newton", zero.Newton(fun,dfun,0.,1.e-8,1.e-12,100))
print("A bracket of the zero", zero.bracketInterval(fun,0.,1.e-3,100))

0 comments on commit 69158f7

Please sign in to comment.