From 017c776cb7d1c734c3ede558c67ea530d5343528 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20Have=CC=81?= Date: Thu, 27 Oct 2022 15:33:40 +0200 Subject: [PATCH 01/11] Add explicit copy() function Should be protected in C++ to avoid default copy constructor call. --- .../src/_pylibkriging/Kriging_binding.cpp | 7 ++++ .../src/_pylibkriging/Kriging_binding.hpp | 7 ++++ .../_pylibkriging/NoiseKriging_binding.cpp | 8 +++++ .../_pylibkriging/NoiseKriging_binding.hpp | 7 ++++ .../_pylibkriging/NuggetKriging_binding.cpp | 10 +++++- .../_pylibkriging/NuggetKriging_binding.hpp | 7 ++++ .../Python/src/_pylibkriging/pylibkriging.cpp | 9 +++++ bindings/Python/tests/CMakeLists.txt | 1 + bindings/Python/tests/PyKriging_copy_test.py | 33 +++++++++++++++++++ src/lib/include/libKriging/Kriging.hpp | 2 ++ src/lib/include/libKriging/NoiseKriging.hpp | 2 ++ src/lib/include/libKriging/NuggetKriging.hpp | 2 ++ 12 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 bindings/Python/tests/PyKriging_copy_test.py diff --git a/bindings/Python/src/_pylibkriging/Kriging_binding.cpp b/bindings/Python/src/_pylibkriging/Kriging_binding.cpp index c588ffd0..043c47f5 100644 --- a/bindings/Python/src/_pylibkriging/Kriging_binding.cpp +++ b/bindings/Python/src/_pylibkriging/Kriging_binding.cpp @@ -46,6 +46,13 @@ PyKriging::PyKriging(const py::array_t& y, PyKriging::~PyKriging() {} +PyKriging::PyKriging(const PyKriging& other) : m_internal{std::make_unique(*other.m_internal)} {} + +PyKriging PyKriging::copy() const { + auto copy_internal = std::make_unique(*m_internal); + return PyKriging(std::move(copy_internal)); +} + void PyKriging::fit(const py::array_t& y, const py::array_t& X, const Trend::RegressionModel& regmodel, diff --git a/bindings/Python/src/_pylibkriging/Kriging_binding.hpp b/bindings/Python/src/_pylibkriging/Kriging_binding.hpp index f2c30e10..aa4feee0 100644 --- a/bindings/Python/src/_pylibkriging/Kriging_binding.hpp +++ b/bindings/Python/src/_pylibkriging/Kriging_binding.hpp @@ -13,6 +13,9 @@ namespace py = pybind11; class PyKriging { + private: + PyKriging(std::unique_ptr && internal) : m_internal(std::move(internal)) {} + public: PyKriging(const std::string& kernel); PyKriging(const py::array_t& y, @@ -32,6 +35,10 @@ class PyKriging { const std::string& objective, const py::dict& dict); ~PyKriging(); + + PyKriging(const PyKriging & other); + + [[nodiscard]] PyKriging copy() const; void fit(const py::array_t& y, const py::array_t& X, diff --git a/bindings/Python/src/_pylibkriging/NoiseKriging_binding.cpp b/bindings/Python/src/_pylibkriging/NoiseKriging_binding.cpp index 9e37a670..774cf991 100644 --- a/bindings/Python/src/_pylibkriging/NoiseKriging_binding.cpp +++ b/bindings/Python/src/_pylibkriging/NoiseKriging_binding.cpp @@ -52,6 +52,14 @@ PyNoiseKriging::PyNoiseKriging(const py::array_t& y, PyNoiseKriging::~PyNoiseKriging() {} +PyNoiseKriging::PyNoiseKriging(const PyNoiseKriging& other) + : m_internal{std::make_unique(*other.m_internal)} {} + +PyNoiseKriging PyNoiseKriging::copy() const { + auto copy_internal = std::make_unique(*m_internal); + return PyNoiseKriging(std::move(copy_internal)); +} + void PyNoiseKriging::fit(const py::array_t& y, const py::array_t& noise, const py::array_t& X, diff --git a/bindings/Python/src/_pylibkriging/NoiseKriging_binding.hpp b/bindings/Python/src/_pylibkriging/NoiseKriging_binding.hpp index c9fde0ef..a2ffcec9 100644 --- a/bindings/Python/src/_pylibkriging/NoiseKriging_binding.hpp +++ b/bindings/Python/src/_pylibkriging/NoiseKriging_binding.hpp @@ -13,6 +13,9 @@ namespace py = pybind11; class PyNoiseKriging { + private: + PyNoiseKriging(std::unique_ptr && internal) : m_internal(std::move(internal)) {} + public: PyNoiseKriging(const std::string& kernel); PyNoiseKriging(const py::array_t& y, @@ -35,6 +38,10 @@ class PyNoiseKriging { const py::dict& dict); ~PyNoiseKriging(); + PyNoiseKriging(const PyNoiseKriging & other); + + PyNoiseKriging copy() const; + void fit(const py::array_t& y, const py::array_t& noise, const py::array_t& X, diff --git a/bindings/Python/src/_pylibkriging/NuggetKriging_binding.cpp b/bindings/Python/src/_pylibkriging/NuggetKriging_binding.cpp index 0f6acb26..efa4d648 100644 --- a/bindings/Python/src/_pylibkriging/NuggetKriging_binding.cpp +++ b/bindings/Python/src/_pylibkriging/NuggetKriging_binding.cpp @@ -50,6 +50,14 @@ PyNuggetKriging::PyNuggetKriging(const py::array_t& y, PyNuggetKriging::~PyNuggetKriging() {} +PyNuggetKriging::PyNuggetKriging(const PyNuggetKriging& other) + : m_internal{std::make_unique(*other.m_internal)} {} + +PyNuggetKriging PyNuggetKriging::copy() const { + auto copy_internal = std::make_unique(*m_internal); + return PyNuggetKriging(std::move(copy_internal)); +} + void PyNuggetKriging::fit(const py::array_t& y, const py::array_t& X, const Trend::RegressionModel& regmodel, @@ -210,4 +218,4 @@ double PyNuggetKriging::nugget() { bool PyNuggetKriging::is_nugget_estim() { return m_internal->is_nugget_estim(); -} \ No newline at end of file +} diff --git a/bindings/Python/src/_pylibkriging/NuggetKriging_binding.hpp b/bindings/Python/src/_pylibkriging/NuggetKriging_binding.hpp index 07ee1094..e2a1c4e4 100644 --- a/bindings/Python/src/_pylibkriging/NuggetKriging_binding.hpp +++ b/bindings/Python/src/_pylibkriging/NuggetKriging_binding.hpp @@ -13,6 +13,9 @@ namespace py = pybind11; class PyNuggetKriging { + private: + PyNuggetKriging(std::unique_ptr && internal) : m_internal(std::move(internal)) {} + public: PyNuggetKriging(const std::string& kernel); PyNuggetKriging(const py::array_t& y, @@ -32,6 +35,10 @@ class PyNuggetKriging { const std::string& objective, const py::dict& dict); ~PyNuggetKriging(); + + PyNuggetKriging(const PyNuggetKriging & other); + + PyNuggetKriging copy() const; void fit(const py::array_t& y, const py::array_t& X, diff --git a/bindings/Python/src/_pylibkriging/pylibkriging.cpp b/bindings/Python/src/_pylibkriging/pylibkriging.cpp index 43d0d19b..b8ed37f4 100644 --- a/bindings/Python/src/_pylibkriging/pylibkriging.cpp +++ b/bindings/Python/src/_pylibkriging/pylibkriging.cpp @@ -122,6 +122,8 @@ PYBIND11_MODULE(_pylibkriging, m) { py::arg("optim") = default_optim, py::arg("objective") = default_objective, py::arg("parameters") = py::dict{}) + .def(py::init()) + .def("copy", &PyKriging::copy) .def("fit", &PyKriging::fit) .def("predict", &PyKriging::predict) .def("simulate", &PyKriging::simulate) @@ -183,6 +185,7 @@ PYBIND11_MODULE(_pylibkriging, m) { py::arg("optim") = default_optim, py::arg("objective") = default_objective, py::arg("parameters") = Kriging::Parameters{}) + .def("copy", &Kriging::copy) .def("predict", &Kriging::predict) .def("simulate", &Kriging::simulate) .def("update", &Kriging::update) @@ -239,6 +242,8 @@ PYBIND11_MODULE(_pylibkriging, m) { py::arg("optim") = default_optim, py::arg("objective") = default_objective, py::arg("parameters") = py::dict{}) + .def(py::init()) + .def("copy", &PyNuggetKriging::copy) .def("fit", &PyNuggetKriging::fit) .def("predict", &PyNuggetKriging::predict) .def("simulate", &PyNuggetKriging::simulate) @@ -292,6 +297,7 @@ PYBIND11_MODULE(_pylibkriging, m) { py::arg("optim") = default_optim, py::arg("objective") = default_objective, py::arg("parameters") = NuggetKriging::Parameters{}) + .def("copy", &NuggetKriging::copy) .def("fit", &NuggetKriging::fit, py::arg("y"), @@ -359,6 +365,8 @@ PYBIND11_MODULE(_pylibkriging, m) { py::arg("optim") = default_optim, py::arg("objective") = default_objective, py::arg("parameters") = py::dict{}) + .def(py::init()) + .def("copy", &PyNoiseKriging::copy) .def("fit", &PyNoiseKriging::fit) .def("predict", &PyNoiseKriging::predict) .def("simulate", &PyNoiseKriging::simulate) @@ -421,6 +429,7 @@ PYBIND11_MODULE(_pylibkriging, m) { py::arg("optim") = default_optim, py::arg("objective") = default_objective, py::arg("parameters") = NoiseKriging::Parameters{}) + .def("copy", &NoiseKriging::copy) .def("predict", &NoiseKriging::predict) .def("simulate", &NoiseKriging::simulate) .def("update", &NoiseKriging::update) diff --git a/bindings/Python/tests/CMakeLists.txt b/bindings/Python/tests/CMakeLists.txt index 3b5fb90d..2a8161f4 100644 --- a/bindings/Python/tests/CMakeLists.txt +++ b/bindings/Python/tests/CMakeLists.txt @@ -62,5 +62,6 @@ add_python_test(NAME WrappedPyLinearRegression FILENAME WrappedPyLinearRegressio add_python_test(NAME WrappedPyKrigingParametricTest FILENAME WrappedPyKriging_parametric_test.py) add_python_test(NAME PyLinearRegression FILENAME PyLinearRegression_test.py) add_python_test(NAME PyKrigingParametricTest FILENAME PyKriging_parametric_test.py) +add_python_test(NAME KrigingCopyTest FILENAME PyKriging_copy_test.py) add_python_test(NAME binding_consistency FILENAME binding_consistency_test.py) add_python_test(NAME KrigingDemo FILENAME pylibkriging_demo.py) diff --git a/bindings/Python/tests/PyKriging_copy_test.py b/bindings/Python/tests/PyKriging_copy_test.py new file mode 100644 index 00000000..78bf6520 --- /dev/null +++ b/bindings/Python/tests/PyKriging_copy_test.py @@ -0,0 +1,33 @@ +import pylibkriging as lk +import numpy as np +import pytest + + +def test_copied_wrapped_kriging_returns_same_result(): + X = [0.0, 0.2, 0.5, 0.8, 1.0] + f = lambda x: (1 - 1 / 2 * (np.sin(12 * x) / (1 + x) + 2 * np.cos(7 * x) * x ** 5 + 0.7)) + y = [f(xi) for xi in X] + + rl1 = lk.WrappedPyKriging(y, X, "gauss", parameters={'sigma2': 1, 'is_theta_estim': False}) + # rl1 = lk.WrappedPyNuggetKriging(y, X, "gauss") + # rl1 = lk.WrappedPyNoiseKriging(y, X, "gauss") + print(rl1.summary()) + + rl2 = rl1.copy() # true copy not reference copy + print(rl2.summary()) + + assert id(rl1) != id(rl2) # not same object reference + + x = np.arange(0, 1, 1 / 99) + + p1 = rl1.predict(x, True, False, False) + p1 = {"mean": p1[0], "stdev": p1[1], "cov": p1[2], "mean_deriv": p1[3], "stdev_deriv": p1[4]} + + p2 = rl2.predict(x, True, False, False) + p2 = {"mean": p2[0], "stdev": p2[1], "cov": p2[2], "mean_deriv": p2[3], "stdev_deriv": p2[4]} + + assert np.array_equal(p1["mean"], p2["mean"]) + assert np.array_equal(p1["stdev"], p2["stdev"]) + assert np.array_equal(p1["cov"], p2["cov"]) + assert np.array_equal(p1["mean_deriv"], p2["mean_deriv"]) + assert np.array_equal(p1["stdev_deriv"], p2["stdev_deriv"]) diff --git a/src/lib/include/libKriging/Kriging.hpp b/src/lib/include/libKriging/Kriging.hpp index 7e5cd553..7e40c014 100644 --- a/src/lib/include/libKriging/Kriging.hpp +++ b/src/lib/include/libKriging/Kriging.hpp @@ -113,6 +113,8 @@ class Kriging { const std::string& objective = "LL", const Parameters& parameters = {}); + LIBKRIGING_EXPORT Kriging copy() const { return *this; } + /** Fit the kriging object on (X,y): * @param y is n length column vector of output * @param X is n*d matrix of input diff --git a/src/lib/include/libKriging/NoiseKriging.hpp b/src/lib/include/libKriging/NoiseKriging.hpp index 19b6cfe1..1754699c 100644 --- a/src/lib/include/libKriging/NoiseKriging.hpp +++ b/src/lib/include/libKriging/NoiseKriging.hpp @@ -108,6 +108,8 @@ class NoiseKriging { const std::string& objective = "LL", const Parameters& parameters = Parameters{}); + LIBKRIGING_EXPORT NoiseKriging copy() const { return *this; } + /** Fit the kriging object on (X,y): * @param y is n length column vector of output * @param noise is n length column vector of output variances diff --git a/src/lib/include/libKriging/NuggetKriging.hpp b/src/lib/include/libKriging/NuggetKriging.hpp index 63085d07..c284eeff 100644 --- a/src/lib/include/libKriging/NuggetKriging.hpp +++ b/src/lib/include/libKriging/NuggetKriging.hpp @@ -117,6 +117,8 @@ class NuggetKriging { const std::string& objective = "LL", const Parameters& parameters = Parameters{}); + LIBKRIGING_EXPORT NuggetKriging copy() const { return *this; } + /** Fit the kriging object on (X,y): * @param y is n length column vector of output * @param X is n*d matrix of input From 6b11299930ec5d3a5ed07fa9cb7f2cb5b644ed01 Mon Sep 17 00:00:00 2001 From: yannrichet Date: Thu, 27 Oct 2022 16:07:17 +0200 Subject: [PATCH 02/11] impl. copy in R & octave wrappers --- bindings/Octave/Kriging.m | 4 +++ bindings/Octave/Kriging_binding.cpp | 10 +++++++ bindings/Octave/Kriging_binding.hpp | 1 + bindings/Octave/NoiseKriging.m | 4 +++ bindings/Octave/NoiseKriging_binding.cpp | 10 +++++++ bindings/Octave/NoiseKriging_binding.hpp | 1 + bindings/Octave/NuggetKriging.m | 6 +++- bindings/Octave/NuggetKriging_binding.cpp | 10 +++++++ bindings/Octave/NuggetKriging_binding.hpp | 1 + bindings/Octave/mLibKriging.cpp | 6 ++++ bindings/R/rlibkriging/R/KrigingClass.R | 28 +++++++++++++++++ bindings/R/rlibkriging/R/NoiseKrigingClass.R | 30 ++++++++++++++++++- bindings/R/rlibkriging/R/NuggetKrigingClass.R | 30 ++++++++++++++++++- bindings/R/rlibkriging/R/allGenerics.R | 15 ++++++++++ .../R/rlibkriging/src/kriging_binding.cpp | 13 ++++++++ .../rlibkriging/src/noisekriging_binding.cpp | 13 ++++++++ .../rlibkriging/src/nuggetkriging_binding.cpp | 13 ++++++++ 17 files changed, 192 insertions(+), 3 deletions(-) diff --git a/bindings/Octave/Kriging.m b/bindings/Octave/Kriging.m index 1219d91d..d116082f 100644 --- a/bindings/Octave/Kriging.m +++ b/bindings/Octave/Kriging.m @@ -9,6 +9,10 @@ % fprintf("New Kriging\n"); obj.ref = mLibKriging("Kriging::new", varargin{:}); end + + function varargout = copy(obj, varargin) + [varargout{1:nargout}] = mLibKriging("Kriging::copy", obj.ref, varargin{:}); + end function delete(obj, varargin) % disp(["ObjectRef = ", num2str(obj.ref)]) diff --git a/bindings/Octave/Kriging_binding.cpp b/bindings/Octave/Kriging_binding.cpp index cd01b6a7..43298a02 100644 --- a/bindings/Octave/Kriging_binding.cpp +++ b/bindings/Octave/Kriging_binding.cpp @@ -46,6 +46,16 @@ void build(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs) { output.set(0, km, "new object reference"); } +void copy(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs) { + MxMapper input{"Input", + nrhs, + const_cast(prhs), // NOLINT(cppcoreguidelines-pro-type-const-cast) + RequiresArg::Exactly{1}}; + MxMapper output{"Output", nlhs, plhs, RequiresArg::Exactly{1}}; + auto* km = input.getObjectFromRef(0, "Kriging reference"); + output.set(0,km->copy() "copied object reference"); +} + void destroy(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs) { MxMapper input{"Input", nrhs, diff --git a/bindings/Octave/Kriging_binding.hpp b/bindings/Octave/Kriging_binding.hpp index e3a9df06..c6a316bf 100644 --- a/bindings/Octave/Kriging_binding.hpp +++ b/bindings/Octave/Kriging_binding.hpp @@ -5,6 +5,7 @@ namespace KrigingBinding { void build(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs); +void copy(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs); void destroy(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs); void fit(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs); void predict(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs); diff --git a/bindings/Octave/NoiseKriging.m b/bindings/Octave/NoiseKriging.m index c70263b3..44de3315 100644 --- a/bindings/Octave/NoiseKriging.m +++ b/bindings/Octave/NoiseKriging.m @@ -10,6 +10,10 @@ obj.ref = mLibKriging("NoiseKriging::new", varargin{:}); end + function varargout = copy(obj, varargin) + [varargout{1:nargout}] = mLibKriging("NoiseKriging::copy", obj.ref, varargin{:}); + end + function delete(obj, varargin) % disp(["ObjectRef = ", num2str(obj.ref)]) % destroy the mex backend diff --git a/bindings/Octave/NoiseKriging_binding.cpp b/bindings/Octave/NoiseKriging_binding.cpp index 972358d1..d465d83f 100644 --- a/bindings/Octave/NoiseKriging_binding.cpp +++ b/bindings/Octave/NoiseKriging_binding.cpp @@ -46,6 +46,16 @@ void build(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs) { output.set(0, km, "new object reference"); } +void copy(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs) { + MxMapper input{"Input", + nrhs, + const_cast(prhs), // NOLINT(cppcoreguidelines-pro-type-const-cast) + RequiresArg::Exactly{1}}; + MxMapper output{"Output", nlhs, plhs, RequiresArg::Exactly{1}}; + auto* km = input.getObjectFromRef(0, "NoiseKriging reference"); + output.set(0,km->copy() "copied object reference"); +} + void destroy(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs) { MxMapper input{"Input", nrhs, diff --git a/bindings/Octave/NoiseKriging_binding.hpp b/bindings/Octave/NoiseKriging_binding.hpp index d273aa6b..ca624072 100644 --- a/bindings/Octave/NoiseKriging_binding.hpp +++ b/bindings/Octave/NoiseKriging_binding.hpp @@ -5,6 +5,7 @@ namespace NoiseKrigingBinding { void build(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs); +void copy(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs); void destroy(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs); void fit(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs); void predict(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs); diff --git a/bindings/Octave/NuggetKriging.m b/bindings/Octave/NuggetKriging.m index 482b8a50..1d2d3dff 100644 --- a/bindings/Octave/NuggetKriging.m +++ b/bindings/Octave/NuggetKriging.m @@ -9,7 +9,11 @@ % printf("New NuggetKriging\n"); obj.ref = mLibKriging("NuggetKriging::new", varargin{:}); end - + + function varargout = copy(obj, varargin) + [varargout{1:nargout}] = mLibKriging("NuggetKriging::copy", obj.ref, varargin{:}); + end + function delete(obj, varargin) % disp(["ObjectRef = ", num2str(obj.ref)]) % destroy the mex backend diff --git a/bindings/Octave/NuggetKriging_binding.cpp b/bindings/Octave/NuggetKriging_binding.cpp index 51a30fb7..50c01d34 100644 --- a/bindings/Octave/NuggetKriging_binding.cpp +++ b/bindings/Octave/NuggetKriging_binding.cpp @@ -47,6 +47,16 @@ void build(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs) { output.set(0, km, "new object reference"); } +void copy(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs) { + MxMapper input{"Input", + nrhs, + const_cast(prhs), // NOLINT(cppcoreguidelines-pro-type-const-cast) + RequiresArg::Exactly{1}}; + MxMapper output{"Output", nlhs, plhs, RequiresArg::Exactly{1}}; + auto* km = input.getObjectFromRef(0, "NuggetKriging reference"); + output.set(0,km->copy() "copied object reference"); +} + void destroy(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs) { MxMapper input{"Input", nrhs, diff --git a/bindings/Octave/NuggetKriging_binding.hpp b/bindings/Octave/NuggetKriging_binding.hpp index f6990b92..037cc6f7 100644 --- a/bindings/Octave/NuggetKriging_binding.hpp +++ b/bindings/Octave/NuggetKriging_binding.hpp @@ -5,6 +5,7 @@ namespace NuggetKrigingBinding { void build(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs); +void copy(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs); void destroy(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs); void fit(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs); void predict(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs); diff --git a/bindings/Octave/mLibKriging.cpp b/bindings/Octave/mLibKriging.cpp index 733b638e..3bc61fe1 100644 --- a/bindings/Octave/mLibKriging.cpp +++ b/bindings/Octave/mLibKriging.cpp @@ -73,6 +73,8 @@ void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) try case "Kriging::new"_hash: return KrigingBinding::build(nlhs, plhs, nrhs - 1, prhs + 1); + case "Kriging::copy"_hash: + return KrigingBinding::copy(nlhs, plhs, nrhs - 1, prhs + 1); case "Kriging::delete"_hash: return KrigingBinding::destroy(nlhs, plhs, nrhs - 1, prhs + 1); case "Kriging::fit"_hash: @@ -143,6 +145,8 @@ void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) try case "NuggetKriging::new"_hash: return NuggetKrigingBinding::build(nlhs, plhs, nrhs - 1, prhs + 1); + case "NuggetKriging::copy"_hash: + return NuggetKrigingBinding::copy(nlhs, plhs, nrhs - 1, prhs + 1); case "NuggetKriging::delete"_hash: return NuggetKrigingBinding::destroy(nlhs, plhs, nrhs - 1, prhs + 1); case "NuggetKriging::fit"_hash: @@ -213,6 +217,8 @@ void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) try case "NoiseKriging::new"_hash: return NoiseKrigingBinding::build(nlhs, plhs, nrhs - 1, prhs + 1); + case "NoiseKriging::copy"_hash: + return NoiseKrigingBinding::copy(nlhs, plhs, nrhs - 1, prhs + 1); case "NoiseKriging::delete"_hash: return NoiseKrigingBinding::destroy(nlhs, plhs, nrhs - 1, prhs + 1); case "NoiseKriging::fit"_hash: diff --git a/bindings/R/rlibkriging/R/KrigingClass.R b/bindings/R/rlibkriging/R/KrigingClass.R index 00a5368c..82b55d51 100644 --- a/bindings/R/rlibkriging/R/KrigingClass.R +++ b/bindings/R/rlibkriging/R/KrigingClass.R @@ -769,3 +769,31 @@ logMargPostFun.Kriging <- function(object, theta, grad = FALSE, ...) { logMargPost.Kriging <- function(object, ...) { return(kriging_logMargPost(object)) } + +## **************************************************************************** +#' Duplicate a Kriging Model +#' +#' @author Yann Richet \email{yann.richet@irsn.fr} +#' +#' @param object An S3 Kriging object. +#' @param ... Not used. +#' +#' @return The copy of object. +#' +#' @method copy Kriging +#' @export +#' @aliases copy,Kriging,Kriging-method +#' +#' @examples +#' f <- function(x) 1 - 1 / 2 * (sin(12 * x) / (1 + x) + 2 * cos(7 * x) * x^5 + 0.7) +#' set.seed(123) +#' X <- as.matrix(runif(10)) +#' y <- f(X) +#' +#' k <- Kriging(y, X, kernel = "matern3_2", objective="LMP") +#' print(k) +#' +#' print(copy(k)) +copy.Kriging <- function(object, ...) { + return(kriging_copy(object)) +} diff --git a/bindings/R/rlibkriging/R/NoiseKrigingClass.R b/bindings/R/rlibkriging/R/NoiseKrigingClass.R index 0d8ee75a..347b603f 100644 --- a/bindings/R/rlibkriging/R/NoiseKrigingClass.R +++ b/bindings/R/rlibkriging/R/NoiseKrigingClass.R @@ -105,7 +105,7 @@ NoiseKriging <- function(y, noise, X, kernel, } } # This will allow to access kriging data/props using `k$d()` - for (d in c('kernel','optim','objective','X','centerX','scaleX','y','noise','centerY','scaleY','regmodel','F','T','M','z','beta','is_beta_estim','theta','is_theta_estim','sigma2','is_sigma2_estim')) { + for (d in c('copy','kernel','optim','objective','X','centerX','scaleX','y','noise','centerY','scaleY','regmodel','F','T','M','z','beta','is_beta_estim','theta','is_theta_estim','sigma2','is_sigma2_estim')) { eval(parse(text=paste0( "nk$", d, " <- function() noisekriging_", d, "(nk)" ))) @@ -598,3 +598,31 @@ logLikelihoodFun.NoiseKriging <- function(object, theta_sigma2, logLikelihood.NoiseKriging <- function(object, ...) { return(noisekriging_logLikelihood(object)) } + +## **************************************************************************** +#' Duplicate a NoiseKriging Model +#' +#' @author Yann Richet \email{yann.richet@irsn.fr} +#' +#' @param object An S3 NoiseKriging object. +#' @param ... Not used. +#' +#' @return The copy of object. +#' +#' @method copy NoiseKriging +#' @export +#' @aliases copy,NoiseKriging,NoiseKriging-method +#' +#' @examples +#' f <- function(x) 1 - 1 / 2 * (sin(12 * x) / (1 + x) + 2 * cos(7 * x) * x^5 + 0.7) +#' set.seed(123) +#' X <- as.matrix(runif(10)) +#' y <- f(X) + X/10 * rnorm(nrow(X)) +#' +#' k <- NoiseKriging(y, (X/10)^2, X, kernel = "matern3_2", objective="LL") +#' print(k) +#' +#' print(copy(k)) +copy.NoiseKriging <- function(object, ...) { + return(noisekriging_copy(object)) +} diff --git a/bindings/R/rlibkriging/R/NuggetKrigingClass.R b/bindings/R/rlibkriging/R/NuggetKrigingClass.R index afa8e716..79bf695a 100644 --- a/bindings/R/rlibkriging/R/NuggetKrigingClass.R +++ b/bindings/R/rlibkriging/R/NuggetKrigingClass.R @@ -102,7 +102,7 @@ NuggetKriging <- function(y, X, kernel, } } # This will allow to access kriging data/props using `k$d()` - for (d in c('kernel','optim','objective','X','centerX','scaleX','y','centerY','scaleY','regmodel','F','T','M','z','beta','is_beta_estim','theta','is_theta_estim','sigma2','is_sigma2_estim','nugget','is_nugget_estim')) { + for (d in c('copy','kernel','optim','objective','X','centerX','scaleX','y','centerY','scaleY','regmodel','F','T','M','z','beta','is_beta_estim','theta','is_theta_estim','sigma2','is_sigma2_estim','nugget','is_nugget_estim')) { eval(parse(text=paste0( "nk$", d, " <- function() nuggetkriging_", d, "(nk)" ))) @@ -696,3 +696,31 @@ logMargPostFun.NuggetKriging <- function(object, theta_alpha, grad = FALSE, ...) logMargPost.NuggetKriging <- function(object, ...) { return(nuggetkriging_logMargPost(object)) } + +## **************************************************************************** +#' Duplicate a NuggetKriging Model +#' +#' @author Yann Richet \email{yann.richet@irsn.fr} +#' +#' @param object An S3 NuggetKriging object. +#' @param ... Not used. +#' +#' @return The copy of object. +#' +#' @method copy NuggetKriging +#' @export +#' @aliases copy,NuggetKriging,NuggetKriging-method +#' +#' @examples +#' f <- function(x) 1 - 1 / 2 * (sin(12 * x) / (1 + x) + 2 * cos(7 * x) * x^5 + 0.7) +#' set.seed(123) +#' X <- as.matrix(runif(10)) +#' y <- f(X) + 0.1 * rnorm(nrow(X)) +#' +#' k <- NuggetKriging(y, X, kernel = "matern3_2", objective="LMP") +#' print(k) +#' +#' print(copy(k)) +copy.NuggetKriging <- function(object, ...) { + return(nuggetkriging_copy(object)) +} \ No newline at end of file diff --git a/bindings/R/rlibkriging/R/allGenerics.R b/bindings/R/rlibkriging/R/allGenerics.R index 8d4d77f6..b08dff4e 100644 --- a/bindings/R/rlibkriging/R/allGenerics.R +++ b/bindings/R/rlibkriging/R/allGenerics.R @@ -135,3 +135,18 @@ logLikelihood <- function (object, ...) { logMargPost <- function (object, ...) { UseMethod("logMargPost") } + +## ***************************************************************************** +##' Duplicate a model given in +##' \code{object}. +##' +##' @title Duplicate object. +##' +##' @param object An object representing a fitted model. +##' @param ... Ignored. +##' +##' @return The copied object. +##' @export +copy <- function (object, ...) { + UseMethod("copy") +} diff --git a/bindings/R/rlibkriging/src/kriging_binding.cpp b/bindings/R/rlibkriging/src/kriging_binding.cpp index 80163fc5..1026ab15 100644 --- a/bindings/R/rlibkriging/src/kriging_binding.cpp +++ b/bindings/R/rlibkriging/src/kriging_binding.cpp @@ -104,6 +104,19 @@ Rcpp::List new_Kriging(arma::vec y, return obj; } +// [[Rcpp::export]] +Rcpp::List kriging_copy(Rcpp::List k) { + if (!k.inherits("Kriging")) + Rcpp::stop("Input must be a Kriging object."); + SEXP impl = k.attr("object"); + Rcpp::XPtr impl_ptr(impl); + + Rcpp::List obj; + obj.attr("object") = impl_ptr->copy(); + obj.attr("class") = "Kriging"; + return obj; +} + // [[Rcpp::export]] Rcpp::List kriging_model(Rcpp::List k) { if (!k.inherits("Kriging")) diff --git a/bindings/R/rlibkriging/src/noisekriging_binding.cpp b/bindings/R/rlibkriging/src/noisekriging_binding.cpp index 07099962..15cdcff8 100644 --- a/bindings/R/rlibkriging/src/noisekriging_binding.cpp +++ b/bindings/R/rlibkriging/src/noisekriging_binding.cpp @@ -106,6 +106,19 @@ Rcpp::List new_NoiseKriging(arma::vec y, return obj; } +// [[Rcpp::export]] +Rcpp::List noisekriging_copy(Rcpp::List k) { + if (!k.inherits("NoiseKriging")) + Rcpp::stop("Input must be a NoiseKriging object."); + SEXP impl = k.attr("object"); + Rcpp::XPtr impl_ptr(impl); + + Rcpp::List obj; + obj.attr("object") = impl_ptr->copy(); + obj.attr("class") = "NoiseKriging"; + return obj; +} + // [[Rcpp::export]] Rcpp::List noisekriging_model(Rcpp::List k) { if (!k.inherits("NoiseKriging")) diff --git a/bindings/R/rlibkriging/src/nuggetkriging_binding.cpp b/bindings/R/rlibkriging/src/nuggetkriging_binding.cpp index a6b27bbf..77ff6756 100644 --- a/bindings/R/rlibkriging/src/nuggetkriging_binding.cpp +++ b/bindings/R/rlibkriging/src/nuggetkriging_binding.cpp @@ -123,6 +123,19 @@ Rcpp::List new_NuggetKriging(arma::vec y, return obj; } +// [[Rcpp::export]] +Rcpp::List nuggetkriging_copy(Rcpp::List k) { + if (!k.inherits("NuggetKriging")) + Rcpp::stop("Input must be a NoiseKriging object."); + SEXP impl = k.attr("object"); + Rcpp::XPtr impl_ptr(impl); + + Rcpp::List obj; + obj.attr("object") = impl_ptr->copy(); + obj.attr("class") = "NuggetKriging"; + return obj; +} + // [[Rcpp::export]] Rcpp::List nuggetkriging_model(Rcpp::List k) { if (!k.inherits("NuggetKriging")) From 6f4e6978c98504f95e2b7fea1869d475a088d3dc Mon Sep 17 00:00:00 2001 From: Yann Richet Date: Thu, 27 Oct 2022 16:48:04 +0200 Subject: [PATCH 03/11] fixes... --- bindings/Octave/Kriging_binding.cpp | 2 +- bindings/Octave/NoiseKriging_binding.cpp | 2 +- bindings/Octave/NuggetKriging_binding.cpp | 2 +- bindings/R/rlibkriging/src/kriging_binding.cpp | 3 ++- bindings/R/rlibkriging/src/noisekriging_binding.cpp | 3 ++- bindings/R/rlibkriging/src/nuggetkriging_binding.cpp | 3 ++- 6 files changed, 9 insertions(+), 6 deletions(-) diff --git a/bindings/Octave/Kriging_binding.cpp b/bindings/Octave/Kriging_binding.cpp index 43298a02..c35a0e8e 100644 --- a/bindings/Octave/Kriging_binding.cpp +++ b/bindings/Octave/Kriging_binding.cpp @@ -53,7 +53,7 @@ void copy(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs) { RequiresArg::Exactly{1}}; MxMapper output{"Output", nlhs, plhs, RequiresArg::Exactly{1}}; auto* km = input.getObjectFromRef(0, "Kriging reference"); - output.set(0,km->copy() "copied object reference"); + output.set(0, km->copy(), "copied object reference"); } void destroy(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs) { diff --git a/bindings/Octave/NoiseKriging_binding.cpp b/bindings/Octave/NoiseKriging_binding.cpp index d465d83f..6a34407f 100644 --- a/bindings/Octave/NoiseKriging_binding.cpp +++ b/bindings/Octave/NoiseKriging_binding.cpp @@ -53,7 +53,7 @@ void copy(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs) { RequiresArg::Exactly{1}}; MxMapper output{"Output", nlhs, plhs, RequiresArg::Exactly{1}}; auto* km = input.getObjectFromRef(0, "NoiseKriging reference"); - output.set(0,km->copy() "copied object reference"); + output.set(0, km->copy(), "copied object reference"); } void destroy(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs) { diff --git a/bindings/Octave/NuggetKriging_binding.cpp b/bindings/Octave/NuggetKriging_binding.cpp index 50c01d34..18f03575 100644 --- a/bindings/Octave/NuggetKriging_binding.cpp +++ b/bindings/Octave/NuggetKriging_binding.cpp @@ -54,7 +54,7 @@ void copy(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs) { RequiresArg::Exactly{1}}; MxMapper output{"Output", nlhs, plhs, RequiresArg::Exactly{1}}; auto* km = input.getObjectFromRef(0, "NuggetKriging reference"); - output.set(0,km->copy() "copied object reference"); + output.set(0, km->copy(), "copied object reference"); } void destroy(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs) { diff --git a/bindings/R/rlibkriging/src/kriging_binding.cpp b/bindings/R/rlibkriging/src/kriging_binding.cpp index 1026ab15..0ff243d1 100644 --- a/bindings/R/rlibkriging/src/kriging_binding.cpp +++ b/bindings/R/rlibkriging/src/kriging_binding.cpp @@ -112,7 +112,8 @@ Rcpp::List kriging_copy(Rcpp::List k) { Rcpp::XPtr impl_ptr(impl); Rcpp::List obj; - obj.attr("object") = impl_ptr->copy(); + Rcpp::XPtr impl_copy(impl_ptr->copy()); + obj.attr("object") = impl_copy; obj.attr("class") = "Kriging"; return obj; } diff --git a/bindings/R/rlibkriging/src/noisekriging_binding.cpp b/bindings/R/rlibkriging/src/noisekriging_binding.cpp index 15cdcff8..b252c1ca 100644 --- a/bindings/R/rlibkriging/src/noisekriging_binding.cpp +++ b/bindings/R/rlibkriging/src/noisekriging_binding.cpp @@ -114,7 +114,8 @@ Rcpp::List noisekriging_copy(Rcpp::List k) { Rcpp::XPtr impl_ptr(impl); Rcpp::List obj; - obj.attr("object") = impl_ptr->copy(); + Rcpp::XPtr impl_copy(impl_ptr->copy()); + obj.attr("object") = impl_copy; obj.attr("class") = "NoiseKriging"; return obj; } diff --git a/bindings/R/rlibkriging/src/nuggetkriging_binding.cpp b/bindings/R/rlibkriging/src/nuggetkriging_binding.cpp index 77ff6756..324a50f4 100644 --- a/bindings/R/rlibkriging/src/nuggetkriging_binding.cpp +++ b/bindings/R/rlibkriging/src/nuggetkriging_binding.cpp @@ -131,7 +131,8 @@ Rcpp::List nuggetkriging_copy(Rcpp::List k) { Rcpp::XPtr impl_ptr(impl); Rcpp::List obj; - obj.attr("object") = impl_ptr->copy(); + Rcpp::XPtr impl_copy(impl_ptr->copy()); + obj.attr("object") = impl_copy; obj.attr("class") = "NuggetKriging"; return obj; } From 14765d4f5e97de1da004e4bdb6d03a66016f9c6b Mon Sep 17 00:00:00 2001 From: Yann Richet Date: Thu, 27 Oct 2022 18:08:35 +0200 Subject: [PATCH 04/11] ... --- bindings/Octave/Kriging_binding.cpp | 4 +++- bindings/Octave/NoiseKriging_binding.cpp | 4 +++- bindings/Octave/NuggetKriging_binding.cpp | 4 +++- bindings/R/rlibkriging/src/kriging_binding.cpp | 3 ++- bindings/R/rlibkriging/src/noisekriging_binding.cpp | 3 ++- bindings/R/rlibkriging/src/nuggetkriging_binding.cpp | 3 ++- 6 files changed, 15 insertions(+), 6 deletions(-) diff --git a/bindings/Octave/Kriging_binding.cpp b/bindings/Octave/Kriging_binding.cpp index c35a0e8e..7e9d8927 100644 --- a/bindings/Octave/Kriging_binding.cpp +++ b/bindings/Octave/Kriging_binding.cpp @@ -53,7 +53,9 @@ void copy(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs) { RequiresArg::Exactly{1}}; MxMapper output{"Output", nlhs, plhs, RequiresArg::Exactly{1}}; auto* km = input.getObjectFromRef(0, "Kriging reference"); - output.set(0, km->copy(), "copied object reference"); + auto km_copy = buildObject(); + *km_copy = km->copy(); + output.set(0, km_copy, "copied object reference"); } void destroy(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs) { diff --git a/bindings/Octave/NoiseKriging_binding.cpp b/bindings/Octave/NoiseKriging_binding.cpp index 6a34407f..b05187a9 100644 --- a/bindings/Octave/NoiseKriging_binding.cpp +++ b/bindings/Octave/NoiseKriging_binding.cpp @@ -53,7 +53,9 @@ void copy(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs) { RequiresArg::Exactly{1}}; MxMapper output{"Output", nlhs, plhs, RequiresArg::Exactly{1}}; auto* km = input.getObjectFromRef(0, "NoiseKriging reference"); - output.set(0, km->copy(), "copied object reference"); + auto km_copy = buildObject(); + *km_copy = km->copy(); + output.set(0, km_copy, "copied object reference"); } void destroy(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs) { diff --git a/bindings/Octave/NuggetKriging_binding.cpp b/bindings/Octave/NuggetKriging_binding.cpp index 18f03575..77e605a4 100644 --- a/bindings/Octave/NuggetKriging_binding.cpp +++ b/bindings/Octave/NuggetKriging_binding.cpp @@ -54,7 +54,9 @@ void copy(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs) { RequiresArg::Exactly{1}}; MxMapper output{"Output", nlhs, plhs, RequiresArg::Exactly{1}}; auto* km = input.getObjectFromRef(0, "NuggetKriging reference"); - output.set(0, km->copy(), "copied object reference"); + auto km_copy = buildObject(); + *km_copy = km->copy(); + output.set(0, km_copy, "copied object reference"); } void destroy(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs) { diff --git a/bindings/R/rlibkriging/src/kriging_binding.cpp b/bindings/R/rlibkriging/src/kriging_binding.cpp index 0ff243d1..3a7a5614 100644 --- a/bindings/R/rlibkriging/src/kriging_binding.cpp +++ b/bindings/R/rlibkriging/src/kriging_binding.cpp @@ -112,7 +112,8 @@ Rcpp::List kriging_copy(Rcpp::List k) { Rcpp::XPtr impl_ptr(impl); Rcpp::List obj; - Rcpp::XPtr impl_copy(impl_ptr->copy()); + Rcpp::XPtr impl_copy(new Kriging()); + *impl_copy = impl_ptr->copy(); obj.attr("object") = impl_copy; obj.attr("class") = "Kriging"; return obj; diff --git a/bindings/R/rlibkriging/src/noisekriging_binding.cpp b/bindings/R/rlibkriging/src/noisekriging_binding.cpp index b252c1ca..0d3aff9a 100644 --- a/bindings/R/rlibkriging/src/noisekriging_binding.cpp +++ b/bindings/R/rlibkriging/src/noisekriging_binding.cpp @@ -114,7 +114,8 @@ Rcpp::List noisekriging_copy(Rcpp::List k) { Rcpp::XPtr impl_ptr(impl); Rcpp::List obj; - Rcpp::XPtr impl_copy(impl_ptr->copy()); + Rcpp::XPtr impl_copy(new NoiseKriging()); + *impl_copy = impl_ptr->copy(); obj.attr("object") = impl_copy; obj.attr("class") = "NoiseKriging"; return obj; diff --git a/bindings/R/rlibkriging/src/nuggetkriging_binding.cpp b/bindings/R/rlibkriging/src/nuggetkriging_binding.cpp index 324a50f4..597e0e0f 100644 --- a/bindings/R/rlibkriging/src/nuggetkriging_binding.cpp +++ b/bindings/R/rlibkriging/src/nuggetkriging_binding.cpp @@ -131,7 +131,8 @@ Rcpp::List nuggetkriging_copy(Rcpp::List k) { Rcpp::XPtr impl_ptr(impl); Rcpp::List obj; - Rcpp::XPtr impl_copy(impl_ptr->copy()); + Rcpp::XPtr impl_copy(new NuggetKriging()); + *impl_copy = impl_ptr->copy(); obj.attr("object") = impl_copy; obj.attr("class") = "NuggetKriging"; return obj; From 4056ae927ff85488d99189e942a8b31aaba6d029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20Have=CC=81?= Date: Thu, 27 Oct 2022 19:11:18 +0200 Subject: [PATCH 05/11] Improve explicit copy for Kriging in C++, Python, Octave Automatic and buggy binding for Python has been removed --- bindings/Octave/Kriging_binding.cpp | 5 +- bindings/Octave/NoiseKriging_binding.cpp | 5 +- bindings/Octave/NuggetKriging_binding.cpp | 5 +- bindings/Octave/tools/ObjectAccessor.hpp | 2 +- .../src/_pylibkriging/Kriging_binding.cpp | 6 +- .../_pylibkriging/NoiseKriging_binding.cpp | 5 +- .../_pylibkriging/NuggetKriging_binding.cpp | 5 +- .../Python/src/_pylibkriging/pylibkriging.cpp | 186 ------------------ bindings/Python/tests/PyKriging_copy_test.py | 34 +++- src/lib/CMakeLists.txt | 3 +- src/lib/include/libKriging/Kriging.hpp | 8 +- src/lib/include/libKriging/NoiseKriging.hpp | 7 +- src/lib/include/libKriging/NuggetKriging.hpp | 10 +- .../utils/ExplicitCopySpecifier.hpp | 6 + 14 files changed, 71 insertions(+), 216 deletions(-) create mode 100644 src/lib/include/libKriging/utils/ExplicitCopySpecifier.hpp diff --git a/bindings/Octave/Kriging_binding.cpp b/bindings/Octave/Kriging_binding.cpp index 7e9d8927..523dc479 100644 --- a/bindings/Octave/Kriging_binding.cpp +++ b/bindings/Octave/Kriging_binding.cpp @@ -52,9 +52,8 @@ void copy(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs) { const_cast(prhs), // NOLINT(cppcoreguidelines-pro-type-const-cast) RequiresArg::Exactly{1}}; MxMapper output{"Output", nlhs, plhs, RequiresArg::Exactly{1}}; - auto* km = input.getObjectFromRef(0, "Kriging reference"); - auto km_copy = buildObject(); - *km_copy = km->copy(); + const auto* km = input.getObjectFromRef(0, "Kriging reference"); + auto km_copy = buildObject(*km, ExplicitCopySpecifier{}); output.set(0, km_copy, "copied object reference"); } diff --git a/bindings/Octave/NoiseKriging_binding.cpp b/bindings/Octave/NoiseKriging_binding.cpp index b05187a9..8e0adcdb 100644 --- a/bindings/Octave/NoiseKriging_binding.cpp +++ b/bindings/Octave/NoiseKriging_binding.cpp @@ -52,9 +52,8 @@ void copy(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs) { const_cast(prhs), // NOLINT(cppcoreguidelines-pro-type-const-cast) RequiresArg::Exactly{1}}; MxMapper output{"Output", nlhs, plhs, RequiresArg::Exactly{1}}; - auto* km = input.getObjectFromRef(0, "NoiseKriging reference"); - auto km_copy = buildObject(); - *km_copy = km->copy(); + const auto* km = input.getObjectFromRef(0, "NoiseKriging reference"); + auto km_copy = buildObject(*km, ExplicitCopySpecifier{}); output.set(0, km_copy, "copied object reference"); } diff --git a/bindings/Octave/NuggetKriging_binding.cpp b/bindings/Octave/NuggetKriging_binding.cpp index 77e605a4..69e1bd03 100644 --- a/bindings/Octave/NuggetKriging_binding.cpp +++ b/bindings/Octave/NuggetKriging_binding.cpp @@ -53,9 +53,8 @@ void copy(int nlhs, mxArray** plhs, int nrhs, const mxArray** prhs) { const_cast(prhs), // NOLINT(cppcoreguidelines-pro-type-const-cast) RequiresArg::Exactly{1}}; MxMapper output{"Output", nlhs, plhs, RequiresArg::Exactly{1}}; - auto* km = input.getObjectFromRef(0, "NuggetKriging reference"); - auto km_copy = buildObject(); - *km_copy = km->copy(); + const auto* km = input.getObjectFromRef(0, "NuggetKriging reference"); + auto km_copy = buildObject(*km, ExplicitCopySpecifier{}); output.set(0, km_copy, "copied object reference"); } diff --git a/bindings/Octave/tools/ObjectAccessor.hpp b/bindings/Octave/tools/ObjectAccessor.hpp index 535fa07e..ef6b608a 100644 --- a/bindings/Octave/tools/ObjectAccessor.hpp +++ b/bindings/Octave/tools/ObjectAccessor.hpp @@ -11,7 +11,7 @@ struct ObjectRef {}; struct EmptyObject {}; template -ObjectCollector::ref_t buildObject(Args... args) { +ObjectCollector::ref_t buildObject(Args &&... args) { return ObjectCollector::registerObject(new T{args...}); } diff --git a/bindings/Python/src/_pylibkriging/Kriging_binding.cpp b/bindings/Python/src/_pylibkriging/Kriging_binding.cpp index 043c47f5..82ba3163 100644 --- a/bindings/Python/src/_pylibkriging/Kriging_binding.cpp +++ b/bindings/Python/src/_pylibkriging/Kriging_binding.cpp @@ -46,11 +46,11 @@ PyKriging::PyKriging(const py::array_t& y, PyKriging::~PyKriging() {} -PyKriging::PyKriging(const PyKriging& other) : m_internal{std::make_unique(*other.m_internal)} {} +PyKriging::PyKriging(const PyKriging& other) + : m_internal{std::make_unique(*other.m_internal, ExplicitCopySpecifier{})} {} PyKriging PyKriging::copy() const { - auto copy_internal = std::make_unique(*m_internal); - return PyKriging(std::move(copy_internal)); + return PyKriging(*this); } void PyKriging::fit(const py::array_t& y, diff --git a/bindings/Python/src/_pylibkriging/NoiseKriging_binding.cpp b/bindings/Python/src/_pylibkriging/NoiseKriging_binding.cpp index 774cf991..86d89946 100644 --- a/bindings/Python/src/_pylibkriging/NoiseKriging_binding.cpp +++ b/bindings/Python/src/_pylibkriging/NoiseKriging_binding.cpp @@ -53,11 +53,10 @@ PyNoiseKriging::PyNoiseKriging(const py::array_t& y, PyNoiseKriging::~PyNoiseKriging() {} PyNoiseKriging::PyNoiseKriging(const PyNoiseKriging& other) - : m_internal{std::make_unique(*other.m_internal)} {} + : m_internal{std::make_unique(*other.m_internal, ExplicitCopySpecifier{})} {} PyNoiseKriging PyNoiseKriging::copy() const { - auto copy_internal = std::make_unique(*m_internal); - return PyNoiseKriging(std::move(copy_internal)); + return PyNoiseKriging(*this); } void PyNoiseKriging::fit(const py::array_t& y, diff --git a/bindings/Python/src/_pylibkriging/NuggetKriging_binding.cpp b/bindings/Python/src/_pylibkriging/NuggetKriging_binding.cpp index efa4d648..3b267840 100644 --- a/bindings/Python/src/_pylibkriging/NuggetKriging_binding.cpp +++ b/bindings/Python/src/_pylibkriging/NuggetKriging_binding.cpp @@ -51,11 +51,10 @@ PyNuggetKriging::PyNuggetKriging(const py::array_t& y, PyNuggetKriging::~PyNuggetKriging() {} PyNuggetKriging::PyNuggetKriging(const PyNuggetKriging& other) - : m_internal{std::make_unique(*other.m_internal)} {} + : m_internal{std::make_unique(*other.m_internal, ExplicitCopySpecifier{})} {} PyNuggetKriging PyNuggetKriging::copy() const { - auto copy_internal = std::make_unique(*m_internal); - return PyNuggetKriging(std::move(copy_internal)); + return PyNuggetKriging(*this); } void PyNuggetKriging::fit(const py::array_t& y, diff --git a/bindings/Python/src/_pylibkriging/pylibkriging.cpp b/bindings/Python/src/_pylibkriging/pylibkriging.cpp index b8ed37f4..5117fb80 100644 --- a/bindings/Python/src/_pylibkriging/pylibkriging.cpp +++ b/bindings/Python/src/_pylibkriging/pylibkriging.cpp @@ -157,68 +157,6 @@ PYBIND11_MODULE(_pylibkriging, m) { .def("sigma2", &PyKriging::sigma2) .def("is_sigma2_estim", &PyKriging::is_sigma2_estim); - // Automated mapper - py::class_(m, "PyKriging") - .def(py::init()) - .def(py::init(), - py::arg("y"), - py::arg("X"), - py::arg("kernel"), - py::arg("regmodel") = default_regmodel, - py::arg("normalize") = default_normalize, - py::arg("optim") = default_optim, - py::arg("objective") = default_objective, - py::arg("parameters") = Kriging::Parameters{}) - .def("fit", - &Kriging::fit, - py::arg("y"), - py::arg("X"), - py::arg("regmodel") = default_regmodel, - py::arg("normalize") = default_normalize, - py::arg("optim") = default_optim, - py::arg("objective") = default_objective, - py::arg("parameters") = Kriging::Parameters{}) - .def("copy", &Kriging::copy) - .def("predict", &Kriging::predict) - .def("simulate", &Kriging::simulate) - .def("update", &Kriging::update) - .def("summary", &Kriging::summary) - .def("leaveOneOutFun", &Kriging::leaveOneOutFun) - .def("logLikelihoodFun", &Kriging::logLikelihoodFun) - .def("logMargPostFun", &Kriging::logMargPostFun) - .def("leaveOneOut", &Kriging::leaveOneOut) - .def("logLikelihood", &Kriging::logLikelihood) - .def("logMargPost", &Kriging::logMargPost) - - .def("kernel", &Kriging::kernel) - .def("optim", &Kriging::optim) - .def("objective", &Kriging::objective) - .def("X", &Kriging::X) - .def("centerX", &Kriging::centerX) - .def("scaleX", &Kriging::scaleX) - .def("y", &Kriging::y) - .def("centerY", &Kriging::centerY) - .def("scaleY", &Kriging::scaleY) - .def("normalize", &Kriging::normalize) - .def("regmodel", &Kriging::regmodel) - .def("F", &Kriging::F) - .def("T", &Kriging::T) - .def("M", &Kriging::M) - .def("z", &Kriging::z) - .def("beta", &Kriging::beta) - .def("is_beta_estim", &Kriging::is_beta_estim) - .def("theta", &Kriging::theta) - .def("is_theta_estim", &Kriging::is_theta_estim) - .def("sigma2", &Kriging::sigma2) - .def("is_sigma2_estim", &Kriging::is_sigma2_estim); - // Quick and dirty manual wrapper (cf optional argument mapping) py::class_(m, "NuggetKrigingParameters").def(py::init<>()); @@ -278,68 +216,6 @@ PYBIND11_MODULE(_pylibkriging, m) { .def("nugget", &PyNuggetKriging::nugget) .def("is_nugget_estim", &PyNuggetKriging::is_nugget_estim); - // Automated mapper - py::class_(m, "PyNuggetKriging") - .def(py::init()) - .def(py::init(), - py::arg("y"), - py::arg("X"), - py::arg("kernel"), - py::arg("regmodel") = default_regmodel, - py::arg("normalize") = default_normalize, - py::arg("optim") = default_optim, - py::arg("objective") = default_objective, - py::arg("parameters") = NuggetKriging::Parameters{}) - .def("copy", &NuggetKriging::copy) - .def("fit", - &NuggetKriging::fit, - py::arg("y"), - py::arg("X"), - py::arg("regmodel") = default_regmodel, - py::arg("normalize") = default_normalize, - py::arg("optim") = default_optim, - py::arg("objective") = default_objective, - py::arg("parameters") = NuggetKriging::Parameters{}) - .def("predict", &NuggetKriging::predict) - .def("simulate", &NuggetKriging::simulate) - .def("update", &NuggetKriging::update) - .def("summary", &NuggetKriging::summary) - .def("logLikelihoodFun", &NuggetKriging::logLikelihoodFun) - .def("logMargPostFun", &NuggetKriging::logMargPostFun) - .def("logLikelihood", &NuggetKriging::logLikelihood) - .def("logMargPost", &NuggetKriging::logMargPost) - - .def("kernel", &NuggetKriging::kernel) - .def("optim", &NuggetKriging::optim) - .def("objective", &NuggetKriging::objective) - .def("X", &NuggetKriging::X) - .def("centerX", &NuggetKriging::centerX) - .def("scaleX", &NuggetKriging::scaleX) - .def("y", &NuggetKriging::y) - .def("centerY", &NuggetKriging::centerY) - .def("scaleY", &NuggetKriging::scaleY) - .def("normalize", &NuggetKriging::normalize) - .def("regmodel", &NuggetKriging::regmodel) - .def("F", &NuggetKriging::F) - .def("T", &NuggetKriging::T) - .def("M", &NuggetKriging::M) - .def("z", &NuggetKriging::z) - .def("beta", &NuggetKriging::beta) - .def("is_beta_estim", &NuggetKriging::is_beta_estim) - .def("theta", &NuggetKriging::theta) - .def("is_theta_estim", &NuggetKriging::is_theta_estim) - .def("sigma2", &NuggetKriging::sigma2) - .def("is_sigma2_estim", &NuggetKriging::is_sigma2_estim) - .def("nugget", &NuggetKriging::nugget) - .def("is_nugget_estim", &NuggetKriging::is_nugget_estim); - // Quick and dirty manual wrapper (cf optional argument mapping) py::class_(m, "NoiseKrigingParameters").def(py::init<>()); @@ -397,66 +273,4 @@ PYBIND11_MODULE(_pylibkriging, m) { .def("is_theta_estim", &PyNoiseKriging::is_theta_estim) .def("sigma2", &PyNoiseKriging::sigma2) .def("is_sigma2_estim", &PyNoiseKriging::is_sigma2_estim); - - // Automated mapper - py::class_(m, "PyNoiseKriging") - .def(py::init()) - .def(py::init(), - py::arg("y"), - py::arg("noise"), - py::arg("X"), - py::arg("kernel"), - py::arg("regmodel") = default_regmodel, - py::arg("normalize") = default_normalize, - py::arg("optim") = default_optim, - py::arg("objective") = default_objective, - py::arg("parameters") = NoiseKriging::Parameters{}) - .def("fit", - &NoiseKriging::fit, - py::arg("y"), - py::arg("noise"), - py::arg("X"), - py::arg("regmodel") = default_regmodel, - py::arg("normalize") = default_normalize, - py::arg("optim") = default_optim, - py::arg("objective") = default_objective, - py::arg("parameters") = NoiseKriging::Parameters{}) - .def("copy", &NoiseKriging::copy) - .def("predict", &NoiseKriging::predict) - .def("simulate", &NoiseKriging::simulate) - .def("update", &NoiseKriging::update) - .def("summary", &NoiseKriging::summary) - .def("logLikelihoodFun", &NoiseKriging::logLikelihoodFun) - .def("logLikelihood", &NoiseKriging::logLikelihood) - - .def("kernel", &NoiseKriging::kernel) - .def("optim", &NoiseKriging::optim) - .def("objective", &NoiseKriging::objective) - .def("X", &NoiseKriging::X) - .def("centerX", &NoiseKriging::centerX) - .def("scaleX", &NoiseKriging::scaleX) - .def("y", &NoiseKriging::y) - .def("centerY", &NoiseKriging::centerY) - .def("scaleY", &NoiseKriging::scaleY) - .def("noise", &NoiseKriging::noise) - .def("normalize", &NoiseKriging::normalize) - .def("regmodel", &NoiseKriging::regmodel) - .def("F", &NoiseKriging::F) - .def("T", &NoiseKriging::T) - .def("M", &NoiseKriging::M) - .def("z", &NoiseKriging::z) - .def("beta", &NoiseKriging::beta) - .def("is_beta_estim", &NoiseKriging::is_beta_estim) - .def("theta", &NoiseKriging::theta) - .def("is_theta_estim", &NoiseKriging::is_theta_estim) - .def("sigma2", &NoiseKriging::sigma2) - .def("is_sigma2_estim", &NoiseKriging::is_sigma2_estim); } \ No newline at end of file diff --git a/bindings/Python/tests/PyKriging_copy_test.py b/bindings/Python/tests/PyKriging_copy_test.py index 78bf6520..6b4347e1 100644 --- a/bindings/Python/tests/PyKriging_copy_test.py +++ b/bindings/Python/tests/PyKriging_copy_test.py @@ -3,17 +3,15 @@ import pytest -def test_copied_wrapped_kriging_returns_same_result(): +def test_copied_kriging_returns_same_result(): X = [0.0, 0.2, 0.5, 0.8, 1.0] f = lambda x: (1 - 1 / 2 * (np.sin(12 * x) / (1 + x) + 2 * np.cos(7 * x) * x ** 5 + 0.7)) y = [f(xi) for xi in X] - rl1 = lk.WrappedPyKriging(y, X, "gauss", parameters={'sigma2': 1, 'is_theta_estim': False}) - # rl1 = lk.WrappedPyNuggetKriging(y, X, "gauss") - # rl1 = lk.WrappedPyNoiseKriging(y, X, "gauss") + rl1 = lk.Kriging(y, X, "gauss", parameters={'sigma2': 1, 'is_theta_estim': False}) print(rl1.summary()) - rl2 = rl1.copy() # true copy not reference copy + rl2 = rl1.copy() # true copy not reference copy print(rl2.summary()) assert id(rl1) != id(rl2) # not same object reference @@ -31,3 +29,29 @@ def test_copied_wrapped_kriging_returns_same_result(): assert np.array_equal(p1["cov"], p2["cov"]) assert np.array_equal(p1["mean_deriv"], p2["mean_deriv"]) assert np.array_equal(p1["stdev_deriv"], p2["stdev_deriv"]) + + +def test_copied_and_changed_kriging_returns_different_result(): + X = [0.0, 0.2, 0.5, 0.8, 1.0] + f = lambda x: (1 - 1 / 2 * (np.sin(12 * x) / (1 + x) + 2 * np.cos(7 * x) * x ** 5 + 0.7)) + y = [f(xi) for xi in X] + + rl1 = lk.Kriging(y, X, "gauss", parameters={'sigma2': 1, 'is_theta_estim': False}) + print(rl1.summary()) + + rl2 = rl1.copy() # true copy not reference copy + print(rl2.summary()) + + assert id(rl1) != id(rl2) # not same object reference + + x = np.arange(0, 1, 1 / 99) + + p1 = rl1.predict(x, True, False, False) + p1 = {"mean": p1[0], "stdev": p1[1], "cov": p1[2], "mean_deriv": p1[3], "stdev_deriv": p1[4]} + + rl2.update([0.6], [f(0.6)]) + p2 = rl2.predict(x, True, False, False) + p2 = {"mean": p2[0], "stdev": p2[1], "cov": p2[2], "mean_deriv": p2[3], "stdev_deriv": p2[4]} + + assert not np.array_equal(p1["mean"], p2["mean"]) + assert not np.array_equal(p1["stdev"], p2["stdev"]) diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 24af2bf5..6037a5a5 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -21,7 +21,8 @@ add_library(Kriging CacheFunction.cpp include/libKriging/CacheFunction.hpp include/libKriging/utils/custom_hash_function.hpp include/libKriging/utils/cache_details.hpp include/libKriging/utils/LinearHashStorage.hpp include/libKriging/utils/data_from_arma_vec.hpp - include/libKriging/version.hpp ${VERSION_FILE}) + include/libKriging/utils/ExplicitCopySpecifier.hpp + include/libKriging/version.hpp ${VERSION_FILE} ) if (CXX_CLANG_TIDY) set_target_properties(Kriging diff --git a/src/lib/include/libKriging/Kriging.hpp b/src/lib/include/libKriging/Kriging.hpp index 7e40c014..1cca6aa3 100644 --- a/src/lib/include/libKriging/Kriging.hpp +++ b/src/lib/include/libKriging/Kriging.hpp @@ -7,6 +7,7 @@ #include "libKriging/utils/lk_armadillo.hpp" #include "libKriging/Trend.hpp" +#include "libKriging/utils/ExplicitCopySpecifier.hpp" #include "libKriging/libKriging_exports.h" @@ -26,6 +27,9 @@ struct KrigingParameters { * @ingroup Regression */ class Kriging { + Kriging() = delete; + Kriging(const Kriging& other) = default; // Should be specialized is non default copy constructor is required + public: using Parameters = KrigingParameters; @@ -113,8 +117,8 @@ class Kriging { const std::string& objective = "LL", const Parameters& parameters = {}); - LIBKRIGING_EXPORT Kriging copy() const { return *this; } - + LIBKRIGING_EXPORT Kriging(const Kriging& other, ExplicitCopySpecifier) : Kriging{other} {} + /** Fit the kriging object on (X,y): * @param y is n length column vector of output * @param X is n*d matrix of input diff --git a/src/lib/include/libKriging/NoiseKriging.hpp b/src/lib/include/libKriging/NoiseKriging.hpp index 1754699c..a24f6a45 100644 --- a/src/lib/include/libKriging/NoiseKriging.hpp +++ b/src/lib/include/libKriging/NoiseKriging.hpp @@ -7,6 +7,7 @@ #include "libKriging/utils/lk_armadillo.hpp" #include "libKriging/Trend.hpp" +#include "libKriging/utils/ExplicitCopySpecifier.hpp" #include "libKriging/libKriging_exports.h" @@ -26,6 +27,10 @@ struct NoiseKrigingParameters { * @ingroup Regression */ class NoiseKriging { + NoiseKriging() = delete; + NoiseKriging(const NoiseKriging& other) + = default; // Should be specialized is non default copy constructor is required + public: using Parameters = NoiseKrigingParameters; @@ -108,7 +113,7 @@ class NoiseKriging { const std::string& objective = "LL", const Parameters& parameters = Parameters{}); - LIBKRIGING_EXPORT NoiseKriging copy() const { return *this; } + LIBKRIGING_EXPORT NoiseKriging(const NoiseKriging& other, ExplicitCopySpecifier) : NoiseKriging{other} {} /** Fit the kriging object on (X,y): * @param y is n length column vector of output diff --git a/src/lib/include/libKriging/NuggetKriging.hpp b/src/lib/include/libKriging/NuggetKriging.hpp index c284eeff..d9fdc696 100644 --- a/src/lib/include/libKriging/NuggetKriging.hpp +++ b/src/lib/include/libKriging/NuggetKriging.hpp @@ -7,6 +7,7 @@ #include "libKriging/utils/lk_armadillo.hpp" #include "libKriging/Trend.hpp" +#include "libKriging/utils/ExplicitCopySpecifier.hpp" #include "libKriging/libKriging_exports.h" @@ -28,6 +29,11 @@ struct NuggetKrigingParameters { * @ingroup Regression */ class NuggetKriging { + private: + NuggetKriging() = delete; + NuggetKriging(const NuggetKriging& other) + = default; // Should be specialized is non default copy constructor is required + public: using Parameters = NuggetKrigingParameters; @@ -117,8 +123,8 @@ class NuggetKriging { const std::string& objective = "LL", const Parameters& parameters = Parameters{}); - LIBKRIGING_EXPORT NuggetKriging copy() const { return *this; } - + LIBKRIGING_EXPORT NuggetKriging(const NuggetKriging& other, ExplicitCopySpecifier) : NuggetKriging{other} {} + /** Fit the kriging object on (X,y): * @param y is n length column vector of output * @param X is n*d matrix of input diff --git a/src/lib/include/libKriging/utils/ExplicitCopySpecifier.hpp b/src/lib/include/libKriging/utils/ExplicitCopySpecifier.hpp new file mode 100644 index 00000000..29137bda --- /dev/null +++ b/src/lib/include/libKriging/utils/ExplicitCopySpecifier.hpp @@ -0,0 +1,6 @@ +#ifndef LIBKRIGING_SRC_LIB_INCLUDE_LIBKRIGING_UTILS_EXPLICITCOPYSPECIFIER_HPP +#define LIBKRIGING_SRC_LIB_INCLUDE_LIBKRIGING_UTILS_EXPLICITCOPYSPECIFIER_HPP + +struct ExplicitCopySpecifier {}; + +#endif // LIBKRIGING_SRC_LIB_INCLUDE_LIBKRIGING_UTILS_EXPLICITCOPYSPECIFIER_HPP From 78d16e422b04d862bf99b6841c7b7606c8d1e0c2 Mon Sep 17 00:00:00 2001 From: yannrichet Date: Fri, 28 Oct 2022 10:08:14 +0200 Subject: [PATCH 06/11] Fix _copy function implementation for R --- bindings/R/rlibkriging/src/kriging_binding.cpp | 3 +-- bindings/R/rlibkriging/src/noisekriging_binding.cpp | 3 +-- bindings/R/rlibkriging/src/nuggetkriging_binding.cpp | 3 +-- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/bindings/R/rlibkriging/src/kriging_binding.cpp b/bindings/R/rlibkriging/src/kriging_binding.cpp index 3a7a5614..e69a2ebb 100644 --- a/bindings/R/rlibkriging/src/kriging_binding.cpp +++ b/bindings/R/rlibkriging/src/kriging_binding.cpp @@ -112,8 +112,7 @@ Rcpp::List kriging_copy(Rcpp::List k) { Rcpp::XPtr impl_ptr(impl); Rcpp::List obj; - Rcpp::XPtr impl_copy(new Kriging()); - *impl_copy = impl_ptr->copy(); + Rcpp::XPtr impl_copy(new Kriging(*impl_ptr, ExplicitCopySpecifier{})); obj.attr("object") = impl_copy; obj.attr("class") = "Kriging"; return obj; diff --git a/bindings/R/rlibkriging/src/noisekriging_binding.cpp b/bindings/R/rlibkriging/src/noisekriging_binding.cpp index 0d3aff9a..be01b57f 100644 --- a/bindings/R/rlibkriging/src/noisekriging_binding.cpp +++ b/bindings/R/rlibkriging/src/noisekriging_binding.cpp @@ -114,8 +114,7 @@ Rcpp::List noisekriging_copy(Rcpp::List k) { Rcpp::XPtr impl_ptr(impl); Rcpp::List obj; - Rcpp::XPtr impl_copy(new NoiseKriging()); - *impl_copy = impl_ptr->copy(); + Rcpp::XPtr impl_copy(new NoiseKriging(*impl_ptr, ExplicitCopySpecifier{})); obj.attr("object") = impl_copy; obj.attr("class") = "NoiseKriging"; return obj; diff --git a/bindings/R/rlibkriging/src/nuggetkriging_binding.cpp b/bindings/R/rlibkriging/src/nuggetkriging_binding.cpp index 597e0e0f..e175c3ef 100644 --- a/bindings/R/rlibkriging/src/nuggetkriging_binding.cpp +++ b/bindings/R/rlibkriging/src/nuggetkriging_binding.cpp @@ -131,8 +131,7 @@ Rcpp::List nuggetkriging_copy(Rcpp::List k) { Rcpp::XPtr impl_ptr(impl); Rcpp::List obj; - Rcpp::XPtr impl_copy(new NuggetKriging()); - *impl_copy = impl_ptr->copy(); + Rcpp::XPtr impl_copy(new NuggetKriging(*impl_ptr, ExplicitCopySpecifier{})); obj.attr("object") = impl_copy; obj.attr("class") = "NuggetKriging"; return obj; From a5f40a2185005466b6ce468d3b388203e41c719e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20Have=CC=81?= Date: Mon, 7 Nov 2022 10:18:19 +0100 Subject: [PATCH 07/11] Move Kriging ExplicitCopy constructor into CPP --- src/lib/Kriging.cpp | 2 ++ src/lib/NoiseKriging.cpp | 2 ++ src/lib/NuggetKriging.cpp | 3 +++ src/lib/include/libKriging/Kriging.hpp | 4 ++-- src/lib/include/libKriging/NoiseKriging.hpp | 4 ++-- src/lib/include/libKriging/NuggetKriging.hpp | 2 +- 6 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/lib/Kriging.cpp b/src/lib/Kriging.cpp index df3becbb..309335aa 100644 --- a/src/lib/Kriging.cpp +++ b/src/lib/Kriging.cpp @@ -77,6 +77,8 @@ LIBKRIGING_EXPORT Kriging::Kriging(const arma::colvec& y, fit(y, X, regmodel, normalize, optim, objective, parameters); } +LIBKRIGING_EXPORT Kriging::Kriging(const Kriging& other, ExplicitCopySpecifier) : Kriging{other} {} + // arma::mat XtX(arma::mat &X) { // arma::mat XtX = arma::zeros(X.n_cols,X.n_cols); // for (arma::uword i = 0; i < X.n_cols; i++) { diff --git a/src/lib/NoiseKriging.cpp b/src/lib/NoiseKriging.cpp index 27a71652..b8576ca2 100644 --- a/src/lib/NoiseKriging.cpp +++ b/src/lib/NoiseKriging.cpp @@ -77,6 +77,8 @@ LIBKRIGING_EXPORT NoiseKriging::NoiseKriging(const arma::colvec& y, fit(y, X, noise, regmodel, normalize, optim, objective, parameters); } +LIBKRIGING_EXPORT NoiseKriging::NoiseKriging(const NoiseKriging& other, ExplicitCopySpecifier) : NoiseKriging{other} {} + // arma::mat XtX(arma::mat &X) { // arma::mat XtX = arma::zeros(X.n_cols,X.n_cols); // for (arma::uword i = 0; i < X.n_cols; i++) { diff --git a/src/lib/NuggetKriging.cpp b/src/lib/NuggetKriging.cpp index 5ff44dca..253cc3ba 100644 --- a/src/lib/NuggetKriging.cpp +++ b/src/lib/NuggetKriging.cpp @@ -76,6 +76,9 @@ LIBKRIGING_EXPORT NuggetKriging::NuggetKriging(const arma::colvec& y, fit(y, X, regmodel, normalize, optim, objective, parameters); } +LIBKRIGING_EXPORT NuggetKriging::NuggetKriging(const NuggetKriging& other, ExplicitCopySpecifier) + : NuggetKriging{other} {} + // arma::mat XtX(arma::mat &X) { // arma::mat XtX = arma::zeros(X.n_cols,X.n_cols); // for (arma::uword i = 0; i < X.n_cols; i++) { diff --git a/src/lib/include/libKriging/Kriging.hpp b/src/lib/include/libKriging/Kriging.hpp index 1cca6aa3..1bdfd29b 100644 --- a/src/lib/include/libKriging/Kriging.hpp +++ b/src/lib/include/libKriging/Kriging.hpp @@ -117,8 +117,8 @@ class Kriging { const std::string& objective = "LL", const Parameters& parameters = {}); - LIBKRIGING_EXPORT Kriging(const Kriging& other, ExplicitCopySpecifier) : Kriging{other} {} - + LIBKRIGING_EXPORT Kriging(const Kriging& other, ExplicitCopySpecifier); + /** Fit the kriging object on (X,y): * @param y is n length column vector of output * @param X is n*d matrix of input diff --git a/src/lib/include/libKriging/NoiseKriging.hpp b/src/lib/include/libKriging/NoiseKriging.hpp index a24f6a45..fb13f7e9 100644 --- a/src/lib/include/libKriging/NoiseKriging.hpp +++ b/src/lib/include/libKriging/NoiseKriging.hpp @@ -113,8 +113,8 @@ class NoiseKriging { const std::string& objective = "LL", const Parameters& parameters = Parameters{}); - LIBKRIGING_EXPORT NoiseKriging(const NoiseKriging& other, ExplicitCopySpecifier) : NoiseKriging{other} {} - + LIBKRIGING_EXPORT NoiseKriging(const NoiseKriging& other, ExplicitCopySpecifier); + /** Fit the kriging object on (X,y): * @param y is n length column vector of output * @param noise is n length column vector of output variances diff --git a/src/lib/include/libKriging/NuggetKriging.hpp b/src/lib/include/libKriging/NuggetKriging.hpp index d9fdc696..92f85806 100644 --- a/src/lib/include/libKriging/NuggetKriging.hpp +++ b/src/lib/include/libKriging/NuggetKriging.hpp @@ -123,7 +123,7 @@ class NuggetKriging { const std::string& objective = "LL", const Parameters& parameters = Parameters{}); - LIBKRIGING_EXPORT NuggetKriging(const NuggetKriging& other, ExplicitCopySpecifier) : NuggetKriging{other} {} + LIBKRIGING_EXPORT NuggetKriging(const NuggetKriging& other, ExplicitCopySpecifier); /** Fit the kriging object on (X,y): * @param y is n length column vector of output From a5ba49973eea753636944f8e76b886fb490b2b6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20Have=CC=81?= Date: Mon, 7 Nov 2022 10:47:06 +0100 Subject: [PATCH 08/11] Upgrade macos to 12 in GHA CI --- .github/workflows/main.yml | 6 +++--- .github/workflows/python-installation.yml | 2 +- .github/workflows/release.yml | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9b669976..5f9b2339 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -65,7 +65,7 @@ jobs: mode: Debug enable_python: on - name: "macOS Debug" - os: macOS-11 + os: macOS-12 mode: Debug enable_python: on - name: "Windows Debug" @@ -79,7 +79,7 @@ jobs: enable_octave: on force_update: on - name: "Octave macOS" - os: macOS-11 + os: macOS-12 mode: Release enable_python: off enable_octave: on @@ -101,7 +101,7 @@ jobs: mode: Release enable_R: on - name: "R macOS" - os: macOS-11 + os: macOS-12 mode: Release enable_R: on - name: "R Windows" diff --git a/.github/workflows/python-installation.yml b/.github/workflows/python-installation.yml index edd43b87..a3b7ef32 100644 --- a/.github/workflows/python-installation.yml +++ b/.github/workflows/python-installation.yml @@ -15,7 +15,7 @@ jobs: fail-fast: false matrix: python-version: ["3.6", "3.7", "3.8", "3.9", "3.10"] - os: [ubuntu-18.04, ubuntu-20.04, macos-11] + os: [ubuntu-18.04, ubuntu-20.04, macos-12] exclude: # not supported - os: ubuntu-20.04 python-version: 3.6 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 8ca7fd34..bb632913 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -104,7 +104,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ macOS-11 ] + os: [ macOS-12 ] python-version: ["3.6", "3.7", "3.8", "3.9", "3.10"] include: - os: ubuntu-18.04 @@ -188,7 +188,7 @@ jobs: os: ubuntu-20.04 force_update: on - name: "Octave macOS" - os: macOS-11 + os: macOS-12 - name: "Octave Windows" os: windows-2019 @@ -270,7 +270,7 @@ jobs: - name: "R Linux" os: ubuntu-20.04 - name: "R macOS" - os: macOS-11 + os: macOS-12 - name: "R Windows" os: windows-2019 From 166adbb780580d48a6e49e3258ff110e00ebef5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20Have=CC=81?= Date: Wed, 9 Nov 2022 19:36:04 +0100 Subject: [PATCH 09/11] Update pybind11 to 2.10.1 for Python 3.11 support --- dependencies/pybind11 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dependencies/pybind11 b/dependencies/pybind11 index ffa34686..80dc998e 160000 --- a/dependencies/pybind11 +++ b/dependencies/pybind11 @@ -1 +1 @@ -Subproject commit ffa346860b306c9bbfb341aed9c14c067751feb8 +Subproject commit 80dc998efced8ceb2be59756668a7e90e8bef917 From 75559a170a8d86b5a5efb10f462c6d796a559970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20Have=CC=81?= Date: Wed, 9 Nov 2022 20:30:17 +0100 Subject: [PATCH 10/11] Fix macos dynamic library search path --- .travis-ci/linux-macos/test.sh | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/.travis-ci/linux-macos/test.sh b/.travis-ci/linux-macos/test.sh index 67203416..0b172871 100755 --- a/.travis-ci/linux-macos/test.sh +++ b/.travis-ci/linux-macos/test.sh @@ -19,8 +19,17 @@ else # Cleanup compiled libs to check right path finding rm -fr src/lib + # add library directory search PATH for executables - export LD_LIBRARY_PATH=$PWD/installed/lib:${LD_LIBRARY_PATH} + case "$(uname -s)" in + Darwin) + export DYLD_LIBRARY_PATH=$PWD/installed/lib:${DYLD_LIBRARY_PATH} + ;; + + *) + export LD_LIBRARY_PATH=$PWD/installed/lib:${LD_LIBRARY_PATH} + ;; + esac ctest -C "${MODE}" ${CTEST_FLAGS} fi From cf07a80eb10649faa9ace323d1e161a8aaaf7fe9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20Have=CC=81?= Date: Wed, 9 Nov 2022 22:18:20 +0100 Subject: [PATCH 11/11] Fix format --- bindings/Octave/tools/ObjectAccessor.hpp | 2 +- bindings/Python/src/_pylibkriging/Kriging_binding.hpp | 10 +++++----- .../Python/src/_pylibkriging/NoiseKriging_binding.hpp | 6 +++--- .../Python/src/_pylibkriging/NuggetKriging_binding.hpp | 10 +++++----- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/bindings/Octave/tools/ObjectAccessor.hpp b/bindings/Octave/tools/ObjectAccessor.hpp index ef6b608a..8e43d242 100644 --- a/bindings/Octave/tools/ObjectAccessor.hpp +++ b/bindings/Octave/tools/ObjectAccessor.hpp @@ -11,7 +11,7 @@ struct ObjectRef {}; struct EmptyObject {}; template -ObjectCollector::ref_t buildObject(Args &&... args) { +ObjectCollector::ref_t buildObject(Args&&... args) { return ObjectCollector::registerObject(new T{args...}); } diff --git a/bindings/Python/src/_pylibkriging/Kriging_binding.hpp b/bindings/Python/src/_pylibkriging/Kriging_binding.hpp index aa4feee0..cf39e464 100644 --- a/bindings/Python/src/_pylibkriging/Kriging_binding.hpp +++ b/bindings/Python/src/_pylibkriging/Kriging_binding.hpp @@ -14,8 +14,8 @@ namespace py = pybind11; class PyKriging { private: - PyKriging(std::unique_ptr && internal) : m_internal(std::move(internal)) {} - + PyKriging(std::unique_ptr&& internal) : m_internal(std::move(internal)) {} + public: PyKriging(const std::string& kernel); PyKriging(const py::array_t& y, @@ -35,9 +35,9 @@ class PyKriging { const std::string& objective, const py::dict& dict); ~PyKriging(); - - PyKriging(const PyKriging & other); - + + PyKriging(const PyKriging& other); + [[nodiscard]] PyKriging copy() const; void fit(const py::array_t& y, diff --git a/bindings/Python/src/_pylibkriging/NoiseKriging_binding.hpp b/bindings/Python/src/_pylibkriging/NoiseKriging_binding.hpp index a2ffcec9..8b08dc74 100644 --- a/bindings/Python/src/_pylibkriging/NoiseKriging_binding.hpp +++ b/bindings/Python/src/_pylibkriging/NoiseKriging_binding.hpp @@ -14,7 +14,7 @@ namespace py = pybind11; class PyNoiseKriging { private: - PyNoiseKriging(std::unique_ptr && internal) : m_internal(std::move(internal)) {} + PyNoiseKriging(std::unique_ptr&& internal) : m_internal(std::move(internal)) {} public: PyNoiseKriging(const std::string& kernel); @@ -38,8 +38,8 @@ class PyNoiseKriging { const py::dict& dict); ~PyNoiseKriging(); - PyNoiseKriging(const PyNoiseKriging & other); - + PyNoiseKriging(const PyNoiseKriging& other); + PyNoiseKriging copy() const; void fit(const py::array_t& y, diff --git a/bindings/Python/src/_pylibkriging/NuggetKriging_binding.hpp b/bindings/Python/src/_pylibkriging/NuggetKriging_binding.hpp index e2a1c4e4..0e5ebb49 100644 --- a/bindings/Python/src/_pylibkriging/NuggetKriging_binding.hpp +++ b/bindings/Python/src/_pylibkriging/NuggetKriging_binding.hpp @@ -14,8 +14,8 @@ namespace py = pybind11; class PyNuggetKriging { private: - PyNuggetKriging(std::unique_ptr && internal) : m_internal(std::move(internal)) {} - + PyNuggetKriging(std::unique_ptr&& internal) : m_internal(std::move(internal)) {} + public: PyNuggetKriging(const std::string& kernel); PyNuggetKriging(const py::array_t& y, @@ -35,9 +35,9 @@ class PyNuggetKriging { const std::string& objective, const py::dict& dict); ~PyNuggetKriging(); - - PyNuggetKriging(const PyNuggetKriging & other); - + + PyNuggetKriging(const PyNuggetKriging& other); + PyNuggetKriging copy() const; void fit(const py::array_t& y,