From 69158f74ebe57edb1c0d4acd0133d5091ee13ae7 Mon Sep 17 00:00:00 2001 From: formaggia Date: Tue, 13 Aug 2024 17:16:50 +0200 Subject: [PATCH] Bettered some pybind11 examples --- Examples/src/pybind11/OneDMesh/pyOneDMesh.py | 2 + .../src/pybind11/basicOptim/pyBasicOptim.cpp | 57 ++++++------ .../src/pybind11/basicZeroFun/pyZeroFun.cpp | 90 ++++++++++++------- Examples/src/pybind11/basicZeroFun/test.py | 1 + 4 files changed, 92 insertions(+), 58 deletions(-) diff --git a/Examples/src/pybind11/OneDMesh/pyOneDMesh.py b/Examples/src/pybind11/OneDMesh/pyOneDMesh.py index 9f3413f57..d52922d1d 100755 --- a/Examples/src/pybind11/OneDMesh/pyOneDMesh.py +++ b/Examples/src/pybind11/OneDMesh/pyOneDMesh.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python import numpy as np import matplotlib.pyplot as plt import OneDMesh as odm @@ -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]) diff --git a/Examples/src/pybind11/basicOptim/pyBasicOptim.cpp b/Examples/src/pybind11/basicOptim/pyBasicOptim.cpp index 2e7f01e77..c3a75df0e 100644 --- a/Examples/src/pybind11/basicOptim/pyBasicOptim.cpp +++ b/Examples/src/pybind11/basicOptim/pyBasicOptim.cpp @@ -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 &; -template std::tuple apsc::golden_search(funct f, double a, double b, double tol,unsigned maxIter); -template std::array apsc::Brent_glomin(double a, double b, double x0, double m, double e, double t,funct f); -template std::array apsc::Brent_local_min(double a, double b, double tol,funct f); -template std::tuple apsc::bracketIntervalMinimum(funct f, double x1, double h,unsigned maxIter); - - -PYBIND11_MODULE(basicOptim, m) { - m.def("golden_search", &golden_search,py::arg("f"),py::arg("a"),py::arg("b"),py::arg("tol"),py::arg("maxIter"), +using funct = const std::function &; +template std::tuple +apsc::golden_search(funct f, double a, double b, double tol, unsigned maxIter); +template std::array apsc::Brent_glomin(double a, double b, double x0, + double m, double e, double t, + funct f); +template std::array apsc::Brent_local_min(double a, double b, + double tol, funct f); +template std::tuple +apsc::bracketIntervalMinimum(funct f, double x1, double h, unsigned maxIter); + +PYBIND11_MODULE(basicOptim, m) +{ + m.def("golden_search", &golden_search), + py::arg("f"),py::arg("a"),py::arg("b"),py::arg("tol"),py::arg("maxIter"), R"pbdoc( Golden search function. @@ -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, 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, 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. @@ -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, py::arg("a"),py::arg("b"),py::arg("tol"),py::arg("f"), - R"pbdoc( + + m.def("Brent_local_min", &Brent_local_min, 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. @@ -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, py::arg("f"),py::arg("x1"),py::arg("h"),py::arg("maxIter"), - R"pbdoc( + + m.def("bracketIntervalMinimum", &bracketIntervalMinimum, + 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. @@ -89,7 +98,3 @@ PYBIND11_MODULE(basicOptim, m) { )pbdoc"); } - - - - diff --git a/Examples/src/pybind11/basicZeroFun/pyZeroFun.cpp b/Examples/src/pybind11/basicZeroFun/pyZeroFun.cpp index e498d056c..cb438913e 100644 --- a/Examples/src/pybind11/basicZeroFun/pyZeroFun.cpp +++ b/Examples/src/pybind11/basicZeroFun/pyZeroFun.cpp @@ -1,29 +1,40 @@ -#include -#include "pybind11/pybind11.h" -#include "pybind11/functional.h" #include "basicZeroFun.hpp" +#include "pybind11/functional.h" +#include "pybind11/pybind11.h" +#include // you need to instantiate the templates -using funct=const std::function &; +using funct = const std::function &; +using functl = const std::function &; -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 apsc::secant(funct f, double a, double b, double tol, double tola,unsigned int maxit); -template std::tuple apsc::Newton(funct f, funct df, double a, double tol, double tola,unsigned int maxit); -template std::tuple apsc::bracketInterval(funct f, double x1, double h,unsigned int maxIter); -template std::tuple 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 apsc::secant(funct f, double a, double b, + double tol, double tola, + unsigned int maxit); +template std::tuple apsc::Newton(funct f, funct df, double a, + double tol, double tola, + unsigned int maxit); +template std::tuple +apsc::bracketInterval(funct f, double x1, double h, unsigned int maxIter); +template std::tuple +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", ®ulaFalsi, 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", ®ulaFalsi, 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. @@ -34,9 +45,10 @@ PYBIND11_MODULE(zeroFun, m) { :return: The zero of the function. )pbdoc"); - // Bind the bisection function - m.def("bisection", &bisection, py::arg("f"), py::arg("a"), py::arg("b"), py::arg("tol"), - R"pbdoc( + // Bind the bisection function + m.def("bisectionld", &bisection, 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. @@ -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, 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, 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, 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. @@ -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, 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, 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. @@ -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, py::arg("f"), py::arg("x1"), py::arg("h"), py::arg("maxIter"), - R"pbdoc( + // Bind the bracketInterval function + m.def("bracketInterval", &bracketInterval, 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. @@ -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, 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, 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. @@ -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"; }; - diff --git a/Examples/src/pybind11/basicZeroFun/test.py b/Examples/src/pybind11/basicZeroFun/test.py index 95a237941..b112b3280 100644 --- a/Examples/src/pybind11/basicZeroFun/test.py +++ b/Examples/src/pybind11/basicZeroFun/test.py @@ -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))