diff --git a/src/polysolve/Utils.hpp b/src/polysolve/Utils.hpp index fda3aa8..5f87f6c 100644 --- a/src/polysolve/Utils.hpp +++ b/src/polysolve/Utils.hpp @@ -61,48 +61,3 @@ namespace polysolve Eigen::SparseMatrix sparse_identity(int rows, int cols); } // namespace polysolve - -namespace nlohmann -{ - template - struct adl_serializer> - { - static void to_json(json &j, const Eigen::Matrix &matrix) - { - for (int row = 0; row < matrix.rows(); ++row) - { - json column = json::array(); - for (int col = 0; col < matrix.cols(); ++col) - { - column.push_back(matrix(row, col)); - } - j.push_back(column); - } - } - - static void from_json(const json &j, Eigen::Matrix &matrix) - { - using Scalar = typename Eigen::Matrix::Scalar; - assert(j.size() > 0); - assert(nrows >= j.size() || nrows == -1); - assert(j.at(0).is_number() || ncols == -1 || ncols >= j.at(0).size()); - - const int n_cols = j.at(0).is_number() ? 1 : j.at(0).size(); - if (nrows == -1 || ncols == -1) - matrix.setZero(j.size(), n_cols); - - for (std::size_t row = 0; row < j.size(); ++row) - { - const auto& jrow = j.at(row); - if (jrow.is_number()) - matrix(row) = jrow; - else - for (std::size_t col = 0; col < jrow.size(); ++col) - { - const auto& value = jrow.at(col); - matrix(row, col) = value.get(); - } - } - } - }; -} // namespace nlohmann \ No newline at end of file diff --git a/src/polysolve/nonlinear/BoxConstraintSolver.cpp b/src/polysolve/nonlinear/BoxConstraintSolver.cpp index d40c3f5..3dab2ac 100644 --- a/src/polysolve/nonlinear/BoxConstraintSolver.cpp +++ b/src/polysolve/nonlinear/BoxConstraintSolver.cpp @@ -7,6 +7,51 @@ #include +namespace nlohmann +{ + template + struct adl_serializer> + { + static void to_json(json &j, const Eigen::Matrix &matrix) + { + for (int row = 0; row < matrix.rows(); ++row) + { + json column = json::array(); + for (int col = 0; col < matrix.cols(); ++col) + { + column.push_back(matrix(row, col)); + } + j.push_back(column); + } + } + + static void from_json(const json &j, Eigen::Matrix &matrix) + { + using Scalar = typename Eigen::Matrix::Scalar; + assert(j.size() > 0); + assert(nrows >= j.size() || nrows == -1); + assert(j.at(0).is_number() || ncols == -1 || ncols >= j.at(0).size()); + + const int n_cols = j.at(0).is_number() ? 1 : j.at(0).size(); + if (nrows == -1 || ncols == -1) + matrix.setZero(j.size(), n_cols); + + for (std::size_t row = 0; row < j.size(); ++row) + { + const auto& jrow = j.at(row); + if (jrow.is_number()) + matrix(row) = jrow; + else + for (std::size_t col = 0; col < jrow.size(); ++col) + { + const auto& value = jrow.at(col); + matrix(row, col) = value.get(); + } + } + } + }; +} // namespace nlohmann + namespace polysolve::nonlinear {