GQCP
Loading...
Searching...
No Matches
NewtonStepUpdate.hpp
Go to the documentation of this file.
1// This file is part of GQCG-GQCP.
2//
3// Copyright (C) 2017-2020 the GQCG developers
4//
5// GQCG-GQCP is free software: you can redistribute it and/or modify
6// it under the terms of the GNU Lesser General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// GQCG-GQCP is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU Lesser General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public License
16// along with GQCG-GQCP. If not, see <http://www.gnu.org/licenses/>.
17
18#pragma once
19
20
23#include "Mathematical/Optimization/LinearEquation/LinearEquationEnvironment.hpp"
24#include "Mathematical/Optimization/LinearEquation/LinearEquationSolver.hpp"
27
28#include <type_traits>
29
30
31namespace GQCP {
32namespace NonLinearEquation {
33
34
42template <typename _Scalar, typename _Environment, typename LinearSolver = decltype(LinearEquationSolver<_Scalar>::ColPivHouseholderQR())>
44 public Step<_Environment> {
45
46public:
47 using Scalar = _Scalar;
48 using Environment = _Environment;
49 static_assert(std::is_same<Scalar, typename Environment::Scalar>::value, "The scalar type must match that of the environment");
50 static_assert(std::is_base_of<NonLinearEquationEnvironment<Scalar>, Environment>::value, "The environment type must derive from NonLinearEquationEnvironment.");
51
52
53private:
54 LinearSolver linear_solver;
55
56
57public:
58 /*
59 * CONSTRUCTORS
60 */
61
65 NewtonStepUpdate(const LinearSolver& linear_solver = LinearEquationSolver<Scalar>::ColPivHouseholderQR()) :
66 linear_solver {linear_solver} {}
67
68
69 /*
70 * PUBLIC OVERRIDDEN METHODS
71 */
72
76 std::string description() const override {
77 return "Calculate a new iteration of the variables and add them to the environment.";
78 }
79
80
86 void execute(Environment& environment) override {
87
88 const auto& x = environment.variables.back();
89 const auto& f = environment.f;
90 const auto& J = environment.J;
91
92 // Calculate f(x) and J(x), i.e. the values of the vector field and its Jacobian at the given x
93 VectorX<Scalar> f_vector = f(x);
94 SquareMatrix<Scalar> J_matrix = J(x);
95
96 // Solve [J dx = -f].
97 auto linear_environment = LinearEquationEnvironment<Scalar>(J_matrix, -f_vector);
98 this->linear_solver.perform(linear_environment);
99
100 const auto dx = linear_environment.x;
101 environment.variables.push_back(x + dx);
102 }
103};
104
105
106} // namespace NonLinearEquation
107} // namespace GQCP
Definition: Matrix.hpp:47
Definition: NewtonStepUpdate.hpp:44
_Environment Environment
Definition: NewtonStepUpdate.hpp:48
std::string description() const override
Definition: NewtonStepUpdate.hpp:76
void execute(Environment &environment) override
Definition: NewtonStepUpdate.hpp:86
_Scalar Scalar
Definition: NewtonStepUpdate.hpp:47
NewtonStepUpdate(const LinearSolver &linear_solver=LinearEquationSolver< Scalar >::ColPivHouseholderQR())
Definition: NewtonStepUpdate.hpp:65
Definition: SquareMatrix.hpp:39
Definition: Step.hpp:37
Definition: BaseOneElectronIntegralBuffer.hpp:25
@ x
Definition: CartesianDirection.hpp:28