Add Spectra.

This commit is contained in:
sunwen
2023-06-02 10:49:02 +08:00
parent ea3496372f
commit c4387f0cf6
50 changed files with 10101 additions and 0 deletions

View File

@@ -0,0 +1,158 @@
// Copyright (C) 2018-2022 Yixuan Qiu <yixuan.qiu@cos.name>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
#ifndef SPECTRA_ARNOLDI_OP_H
#define SPECTRA_ARNOLDI_OP_H
#include <Eigen/Core>
#include <cmath> // std::sqrt
namespace Spectra {
///
/// \ingroup Internals
/// @{
///
///
/// \defgroup Operators Operators
///
/// Different types of operators.
///
///
/// \ingroup Operators
///
/// Operators used in the Arnoldi factorization.
///
template <typename Scalar, typename OpType, typename BOpType>
class ArnoldiOp
{
private:
using Index = Eigen::Index;
using Vector = Eigen::Matrix<Scalar, Eigen::Dynamic, 1>;
const OpType& m_op;
const BOpType& m_Bop;
mutable Vector m_cache;
public:
ArnoldiOp(const OpType& op, const BOpType& Bop) :
m_op(op), m_Bop(Bop), m_cache(op.rows())
{}
// Move constructor
ArnoldiOp(ArnoldiOp&& other) :
m_op(other.m_op), m_Bop(other.m_Bop)
{
// We emulate the move constructor for Vector using Vector::swap()
m_cache.swap(other.m_cache);
}
inline Index rows() const { return m_op.rows(); }
// In generalized eigenvalue problem Ax=lambda*Bx, define the inner product to be <x, y> = x'By.
// For regular eigenvalue problems, it is the usual inner product <x, y> = x'y
// Compute <x, y> = x'By
// x and y are two vectors
template <typename Arg1, typename Arg2>
Scalar inner_product(const Arg1& x, const Arg2& y) const
{
m_Bop.perform_op(y.data(), m_cache.data());
return x.dot(m_cache);
}
// Compute res = <X, y> = X'By
// X is a matrix, y is a vector, res is a vector
template <typename Arg1, typename Arg2>
void trans_product(const Arg1& x, const Arg2& y, Eigen::Ref<Vector> res) const
{
m_Bop.perform_op(y.data(), m_cache.data());
res.noalias() = x.transpose() * m_cache;
}
// B-norm of a vector, ||x||_B = sqrt(x'Bx)
template <typename Arg>
Scalar norm(const Arg& x) const
{
using std::sqrt;
return sqrt(inner_product<Arg, Arg>(x, x));
}
// The "A" operator to generate the Krylov subspace
inline void perform_op(const Scalar* x_in, Scalar* y_out) const
{
m_op.perform_op(x_in, y_out);
}
};
///
/// \ingroup Operators
///
/// Placeholder for the B-operator when \f$B = I\f$.
///
class IdentityBOp
{};
///
/// \ingroup Operators
///
/// Partial specialization for the case \f$B = I\f$.
///
template <typename Scalar, typename OpType>
class ArnoldiOp<Scalar, OpType, IdentityBOp>
{
private:
using Index = Eigen::Index;
using Vector = Eigen::Matrix<Scalar, Eigen::Dynamic, 1>;
const OpType& m_op;
public:
ArnoldiOp(const OpType& op, const IdentityBOp& /*Bop*/) :
m_op(op)
{}
inline Index rows() const { return m_op.rows(); }
// Compute <x, y> = x'y
// x and y are two vectors
template <typename Arg1, typename Arg2>
Scalar inner_product(const Arg1& x, const Arg2& y) const
{
return x.dot(y);
}
// Compute res = <X, y> = X'y
// X is a matrix, y is a vector, res is a vector
template <typename Arg1, typename Arg2>
void trans_product(const Arg1& x, const Arg2& y, Eigen::Ref<Vector> res) const
{
res.noalias() = x.transpose() * y;
}
// B-norm of a vector. For regular eigenvalue problems it is simply the L2 norm
template <typename Arg>
Scalar norm(const Arg& x) const
{
return x.norm();
}
// The "A" operator to generate the Krylov subspace
inline void perform_op(const Scalar* x_in, Scalar* y_out) const
{
m_op.perform_op(x_in, y_out);
}
};
///
/// @}
///
} // namespace Spectra
#endif // SPECTRA_ARNOLDI_OP_H

View File

@@ -0,0 +1,95 @@
// Copyright (C) 2020-2022 Yixuan Qiu <yixuan.qiu@cos.name>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
#ifndef SPECTRA_SYM_GEIGS_BUCKLING_OP_H
#define SPECTRA_SYM_GEIGS_BUCKLING_OP_H
#include <Eigen/Core>
#include "../SymShiftInvert.h"
#include "../SparseSymMatProd.h"
namespace Spectra {
///
/// \ingroup Operators
///
/// This class defines the matrix operation for generalized eigen solver in the
/// buckling mode. It computes \f$y=(K-\sigma K_G)^{-1}Kx\f$ for any
/// vector \f$x\f$, where \f$K\f$ is positive definite, \f$K_G\f$ is symmetric,
/// and \f$\sigma\f$ is a real shift.
/// This class is intended for internal use.
///
template <typename OpType = SymShiftInvert<double>,
typename BOpType = SparseSymMatProd<double>>
class SymGEigsBucklingOp
{
public:
using Scalar = typename OpType::Scalar;
private:
using Index = Eigen::Index;
using Vector = Eigen::Matrix<Scalar, Eigen::Dynamic, 1>;
OpType& m_op;
const BOpType& m_Bop;
mutable Vector m_cache; // temporary working space
public:
///
/// Constructor to create the matrix operation object.
///
/// \param op The \f$(K-\sigma K_G)^{-1}\f$ matrix operation object.
/// \param Bop The \f$K\f$ matrix operation object.
///
SymGEigsBucklingOp(OpType& op, const BOpType& Bop) :
m_op(op), m_Bop(Bop), m_cache(op.rows())
{}
///
/// Move constructor.
///
SymGEigsBucklingOp(SymGEigsBucklingOp&& other) :
m_op(other.m_op), m_Bop(other.m_Bop)
{
// We emulate the move constructor for Vector using Vector::swap()
m_cache.swap(other.m_cache);
}
///
/// Return the number of rows of the underlying matrix.
///
Index rows() const { return m_op.rows(); }
///
/// Return the number of columns of the underlying matrix.
///
Index cols() const { return m_op.rows(); }
///
/// Set the real shift \f$\sigma\f$.
///
void set_shift(const Scalar& sigma)
{
m_op.set_shift(sigma);
}
///
/// Perform the matrix operation \f$y=(K-\sigma K_G)^{-1}Kx\f$.
///
/// \param x_in Pointer to the \f$x\f$ vector.
/// \param y_out Pointer to the \f$y\f$ vector.
///
// y_out = inv(K - sigma * K_G) * K * x_in
void perform_op(const Scalar* x_in, Scalar* y_out) const
{
m_Bop.perform_op(x_in, m_cache.data());
m_op.perform_op(m_cache.data(), y_out);
}
};
} // namespace Spectra
#endif // SPECTRA_SYM_GEIGS_BUCKLING_OP_H

View File

@@ -0,0 +1,105 @@
// Copyright (C) 2020-2022 Yixuan Qiu <yixuan.qiu@cos.name>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
#ifndef SPECTRA_SYM_GEIGS_CAYLEY_OP_H
#define SPECTRA_SYM_GEIGS_CAYLEY_OP_H
#include <Eigen/Core>
#include "../SymShiftInvert.h"
#include "../SparseSymMatProd.h"
namespace Spectra {
///
/// \ingroup Operators
///
/// This class defines the matrix operation for generalized eigen solver in the
/// Cayley mode. It computes \f$y=(A-\sigma B)^{-1}(A+\sigma B)x\f$ for any
/// vector \f$x\f$, where \f$A\f$ is a symmetric matrix, \f$B\f$ is positive definite,
/// and \f$\sigma\f$ is a real shift.
/// This class is intended for internal use.
///
template <typename OpType = SymShiftInvert<double>,
typename BOpType = SparseSymMatProd<double>>
class SymGEigsCayleyOp
{
public:
using Scalar = typename OpType::Scalar;
private:
using Index = Eigen::Index;
using Vector = Eigen::Matrix<Scalar, Eigen::Dynamic, 1>;
using MapConstVec = Eigen::Map<const Vector>;
using MapVec = Eigen::Map<Vector>;
OpType& m_op;
const BOpType& m_Bop;
mutable Vector m_cache; // temporary working space
Scalar m_sigma;
public:
///
/// Constructor to create the matrix operation object.
///
/// \param op The \f$(A-\sigma B)^{-1}\f$ matrix operation object.
/// \param Bop The \f$B\f$ matrix operation object.
///
SymGEigsCayleyOp(OpType& op, const BOpType& Bop) :
m_op(op), m_Bop(Bop), m_cache(op.rows())
{}
///
/// Move constructor.
///
SymGEigsCayleyOp(SymGEigsCayleyOp&& other) :
m_op(other.m_op), m_Bop(other.m_Bop), m_sigma(other.m_sigma)
{
// We emulate the move constructor for Vector using Vector::swap()
m_cache.swap(other.m_cache);
}
///
/// Return the number of rows of the underlying matrix.
///
Index rows() const { return m_op.rows(); }
///
/// Return the number of columns of the underlying matrix.
///
Index cols() const { return m_op.rows(); }
///
/// Set the real shift \f$\sigma\f$.
///
void set_shift(const Scalar& sigma)
{
m_op.set_shift(sigma);
m_sigma = sigma;
}
///
/// Perform the matrix operation \f$y=(A-\sigma B)^{-1}(A+\sigma B)x\f$.
///
/// \param x_in Pointer to the \f$x\f$ vector.
/// \param y_out Pointer to the \f$y\f$ vector.
///
// y_out = inv(A - sigma * B) * (A + sigma * B) * x_in
void perform_op(const Scalar* x_in, Scalar* y_out) const
{
// inv(A - sigma * B) * (A + sigma * B) * x
// = inv(A - sigma * B) * (A - sigma * B + 2 * sigma * B) * x
// = x + 2 * sigma * inv(A - sigma * B) * B * x
m_Bop.perform_op(x_in, m_cache.data());
m_op.perform_op(m_cache.data(), y_out);
MapConstVec x(x_in, this->rows());
MapVec y(y_out, this->rows());
y.noalias() = x + (Scalar(2) * m_sigma) * y;
}
};
} // namespace Spectra
#endif // SPECTRA_SYM_GEIGS_CAYLEY_OP_H

View File

@@ -0,0 +1,87 @@
// Copyright (C) 2016-2022 Yixuan Qiu <yixuan.qiu@cos.name>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
#ifndef SPECTRA_SYM_GEIGS_CHOLESKY_OP_H
#define SPECTRA_SYM_GEIGS_CHOLESKY_OP_H
#include <Eigen/Core>
#include "../DenseSymMatProd.h"
#include "../DenseCholesky.h"
namespace Spectra {
///
/// \ingroup Operators
///
/// This class defines the matrix operation for generalized eigen solver in the
/// Cholesky decomposition mode. It calculates \f$y=L^{-1}A(L')^{-1}x\f$ for any
/// vector \f$x\f$, where \f$L\f$ is the Cholesky decomposition of \f$B\f$.
/// This class is intended for internal use.
///
template <typename OpType = DenseSymMatProd<double>,
typename BOpType = DenseCholesky<double>>
class SymGEigsCholeskyOp
{
public:
using Scalar = typename OpType::Scalar;
private:
using Index = Eigen::Index;
using Vector = Eigen::Matrix<Scalar, Eigen::Dynamic, 1>;
const OpType& m_op;
const BOpType& m_Bop;
mutable Vector m_cache; // temporary working space
public:
///
/// Constructor to create the matrix operation object.
///
/// \param op The \f$A\f$ matrix operation object.
/// \param Bop The \f$B\f$ matrix operation object.
///
SymGEigsCholeskyOp(const OpType& op, const BOpType& Bop) :
m_op(op), m_Bop(Bop), m_cache(op.rows())
{}
///
/// Move constructor.
///
SymGEigsCholeskyOp(SymGEigsCholeskyOp&& other) :
m_op(other.m_op), m_Bop(other.m_Bop)
{
// We emulate the move constructor for Vector using Vector::swap()
m_cache.swap(other.m_cache);
}
///
/// Return the number of rows of the underlying matrix.
///
Index rows() const { return m_Bop.rows(); }
///
/// Return the number of columns of the underlying matrix.
///
Index cols() const { return m_Bop.rows(); }
///
/// Perform the matrix operation \f$y=L^{-1}A(L')^{-1}x\f$.
///
/// \param x_in Pointer to the \f$x\f$ vector.
/// \param y_out Pointer to the \f$y\f$ vector.
///
// y_out = inv(L) * A * inv(L') * x_in
void perform_op(const Scalar* x_in, Scalar* y_out) const
{
m_Bop.upper_triangular_solve(x_in, y_out);
m_op.perform_op(y_out, m_cache.data());
m_Bop.lower_triangular_solve(m_cache.data(), y_out);
}
};
} // namespace Spectra
#endif // SPECTRA_SYM_GEIGS_CHOLESKY_OP_H

View File

@@ -0,0 +1,84 @@
// Copyright (C) 2017-2022 Yixuan Qiu <yixuan.qiu@cos.name>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
#ifndef SPECTRA_SYM_GEIGS_REG_INV_OP_H
#define SPECTRA_SYM_GEIGS_REG_INV_OP_H
#include <Eigen/Core>
#include "../SparseSymMatProd.h"
#include "../SparseRegularInverse.h"
namespace Spectra {
///
/// \ingroup Operators
///
/// This class defines the matrix operation for generalized eigen solver in the
/// regular inverse mode. This class is intended for internal use.
///
template <typename OpType = SparseSymMatProd<double>,
typename BOpType = SparseRegularInverse<double>>
class SymGEigsRegInvOp
{
public:
using Scalar = typename OpType::Scalar;
private:
using Index = Eigen::Index;
using Vector = Eigen::Matrix<Scalar, Eigen::Dynamic, 1>;
const OpType& m_op;
const BOpType& m_Bop;
mutable Vector m_cache; // temporary working space
public:
///
/// Constructor to create the matrix operation object.
///
/// \param op The \f$A\f$ matrix operation object.
/// \param Bop The \f$B\f$ matrix operation object.
///
SymGEigsRegInvOp(const OpType& op, const BOpType& Bop) :
m_op(op), m_Bop(Bop), m_cache(op.rows())
{}
///
/// Move constructor.
///
SymGEigsRegInvOp(SymGEigsRegInvOp&& other) :
m_op(other.m_op), m_Bop(other.m_Bop)
{
// We emulate the move constructor for Vector using Vector::swap()
m_cache.swap(other.m_cache);
}
///
/// Return the number of rows of the underlying matrix.
///
Index rows() const { return m_Bop.rows(); }
///
/// Return the number of columns of the underlying matrix.
///
Index cols() const { return m_Bop.rows(); }
///
/// Perform the matrix operation \f$y=B^{-1}Ax\f$.
///
/// \param x_in Pointer to the \f$x\f$ vector.
/// \param y_out Pointer to the \f$y\f$ vector.
///
// y_out = inv(B) * A * x_in
void perform_op(const Scalar* x_in, Scalar* y_out) const
{
m_op.perform_op(x_in, m_cache.data());
m_Bop.solve(m_cache.data(), y_out);
}
};
} // namespace Spectra
#endif // SPECTRA_SYM_GEIGS_REG_INV_OP_H

View File

@@ -0,0 +1,95 @@
// Copyright (C) 2020-2022 Yixuan Qiu <yixuan.qiu@cos.name>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
#ifndef SPECTRA_SYM_GEIGS_SHIFT_INVERT_OP_H
#define SPECTRA_SYM_GEIGS_SHIFT_INVERT_OP_H
#include <Eigen/Core>
#include "../SymShiftInvert.h"
#include "../SparseSymMatProd.h"
namespace Spectra {
///
/// \ingroup Operators
///
/// This class defines the matrix operation for generalized eigen solver in the
/// shift-and-invert mode. It computes \f$y=(A-\sigma B)^{-1}Bx\f$ for any
/// vector \f$x\f$, where \f$A\f$ is a symmetric matrix, \f$B\f$ is positive definite,
/// and \f$\sigma\f$ is a real shift.
/// This class is intended for internal use.
///
template <typename OpType = SymShiftInvert<double>,
typename BOpType = SparseSymMatProd<double>>
class SymGEigsShiftInvertOp
{
public:
using Scalar = typename OpType::Scalar;
private:
using Index = Eigen::Index;
using Vector = Eigen::Matrix<Scalar, Eigen::Dynamic, 1>;
OpType& m_op;
const BOpType& m_Bop;
mutable Vector m_cache; // temporary working space
public:
///
/// Constructor to create the matrix operation object.
///
/// \param op The \f$(A-\sigma B)^{-1}\f$ matrix operation object.
/// \param Bop The \f$B\f$ matrix operation object.
///
SymGEigsShiftInvertOp(OpType& op, const BOpType& Bop) :
m_op(op), m_Bop(Bop), m_cache(op.rows())
{}
///
/// Move constructor.
///
SymGEigsShiftInvertOp(SymGEigsShiftInvertOp&& other) :
m_op(other.m_op), m_Bop(other.m_Bop)
{
// We emulate the move constructor for Vector using Vector::swap()
m_cache.swap(other.m_cache);
}
///
/// Return the number of rows of the underlying matrix.
///
Index rows() const { return m_op.rows(); }
///
/// Return the number of columns of the underlying matrix.
///
Index cols() const { return m_op.rows(); }
///
/// Set the real shift \f$\sigma\f$.
///
void set_shift(const Scalar& sigma)
{
m_op.set_shift(sigma);
}
///
/// Perform the matrix operation \f$y=(A-\sigma B)^{-1}Bx\f$.
///
/// \param x_in Pointer to the \f$x\f$ vector.
/// \param y_out Pointer to the \f$y\f$ vector.
///
// y_out = inv(A - sigma * B) * B * x_in
void perform_op(const Scalar* x_in, Scalar* y_out) const
{
m_Bop.perform_op(x_in, m_cache.data());
m_op.perform_op(m_cache.data(), y_out);
}
};
} // namespace Spectra
#endif // SPECTRA_SYM_GEIGS_SHIFT_INVERT_OP_H