// This file is part of GQCG-GQCP.
//
// Copyright (C) 2017-2020  the GQCG developers
//
// GQCG-GQCP is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GQCG-GQCP is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with GQCG-GQCP.  If not, see <http://www.gnu.org/licenses/>.

#include "Mathematical/Optimization/Eigenproblem/GeneralizedEigenproblemEnvironment.hpp"
#include "Utilities/complex.hpp"

#include <pybind11/pybind11.h>


namespace gqcpy {


// Provide some shortcuts for frequent namespaces.
namespace py = pybind11;
using namespace GQCP;


/**
 *  Add Python bindings for some APIs related to `GeneralizedEigenproblemEnvironment`.
 *
 *  @tparam Class               The type of the Pybind11 `class_` (generated by the compiler).
 *
 *  @param py_class             The Pybind11 `class_` that should obtain APIs related to `GeneralizedEigenproblemEnvironment`.
 */
template <typename Class>
void bindGeneralizedEigenproblemEnvironmentInterface(Class& py_class) {

    // The C++ type corresponding to the Python class.
    using Type = typename Class::type;
    using Scalar = typename Type::Scalar;


    // Define read/write properties.
    py_class

        // Bind properties with a custom setter (to allow for non-native Eigen types).
        .def_property(
            "A",
            [](const GeneralizedEigenproblemEnvironment<Scalar>& environment) {
                return environment.A;
            },
            [](GeneralizedEigenproblemEnvironment<Scalar>& environment, const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>& new_A) {
                environment.A = SquareMatrix<Scalar>(new_A);
            })

        .def_property(
            "eigenvalues",
            [](const GeneralizedEigenproblemEnvironment<Scalar>& environment) {
                return environment.eigenvalues;
            },
            [](GeneralizedEigenproblemEnvironment<Scalar>& environment, const Eigen::Matrix<double, Eigen::Dynamic, 1>& new_eigenvalues) {
                environment.eigenvalues = VectorX<double>(new_eigenvalues);
            })

        .def_property(
            "eigenvectors",
            [](const GeneralizedEigenproblemEnvironment<Scalar>& environment) {
                return environment.eigenvectors;
            },
            [](GeneralizedEigenproblemEnvironment<Scalar>& environment, const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>& new_eigenvectors) {
                environment.eigenvectors = MatrixX<Scalar>(new_eigenvectors);
            })

        .def_property(
            "S",
            [](const GeneralizedEigenproblemEnvironment<Scalar>& environment) {
                return environment.S;
            },
            [](GeneralizedEigenproblemEnvironment<Scalar>& environment, const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>& new_S) {
                environment.S = SquareMatrix<Scalar>(new_S);
            });
}


/**
 *  Register `GeneralizedEigenproblemEnvironment_d` and `GeneralizedEigenproblemEnvironment_cd` to the gqcpy module and expose parts of their C++ interfaces to Python.
 *
 *  @param module           The Pybind11 module in which the classes should be registered.
 */
void bindGeneralizedEigenproblemEnvironment(py::module& module) {

    // Define the Python class for `GeneralizedEigenproblemEnvironment_d`.
    py::class_<GeneralizedEigenproblemEnvironment<double>> py_GeneralizedEigenproblemEnvironment_d {module, "GeneralizedEigenproblemEnvironment_d", "A real-valued environment used to solve eigenvalue problems for a general eigenvalue problem."};
    bindGeneralizedEigenproblemEnvironmentInterface(py_GeneralizedEigenproblemEnvironment_d);

    // Define the Python class for `GeneralizedEigenproblemEnvironment_cd`.
    py::class_<GeneralizedEigenproblemEnvironment<complex>> py_GeneralizedEigenproblemEnvironment_cd {module, "GeneralizedEigenproblemEnvironment_cd", "A complex-valued environment used to solve eigenvalue problems for a general eigenvalue problem."};
    bindGeneralizedEigenproblemEnvironmentInterface(py_GeneralizedEigenproblemEnvironment_cd);
}


}  // namespace gqcpy
