Reaktoro
A unified framework for modeling chemically reactive systems
Roots.hpp
1 // Reaktoro is a unified framework for modeling chemically reactive systems.
2 //
3 // Copyright (C) 2014-2018 Allan Leal
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library 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 GNU
13 // 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 this library. If not, see <http://www.gnu.org/licenses/>.
17 
18 #pragma once
19 
20 // C++ includes
21 #include <complex>
22 #include <functional>
23 #include <tuple>
24 
25 // Reaktoro includes
26 #include <Reaktoro/Math/Matrix.hpp>
27 
28 namespace Reaktoro {
29 
31 using CubicRoots = std::tuple<std::complex<double>, std::complex<double>, std::complex<double>>;
32 
34 using SquareRoots = std::tuple<double, double>;
35 
50 auto cardano(double a, double b, double c, double d) -> CubicRoots;
51 
57 auto newton(const std::function<std::tuple<double,double>(double)>& f,
58  double x0, double epsilon, unsigned maxiter) -> double;
59 
65 auto newton(const std::function<void(VectorConstRef, VectorRef, MatrixRef)>& f,
66  VectorConstRef x0, double epsilon, unsigned maxiter) -> Vector;
67 
71 auto realRoots(const CubicRoots& roots)->std::vector<double>;
72 
73 } // namespace Reaktoro
Eigen::Ref< Eigen::MatrixXd > MatrixRef
Alias to Eigen type Ref<MatrixXd>.
Definition: Matrix.hpp:43
std::tuple< double, double > SquareRoots
Define a type that describes the roots of a square equation.
Definition: Roots.hpp:34
The namespace containing all components of the Reaktoro library.
Definition: ChemicalScalar.hpp:24
std::tuple< std::complex< double >, std::complex< double >, std::complex< double > > CubicRoots
Define a type that describes the roots of a cubic equation.
Definition: Roots.hpp:31
auto realRoots(const CubicRoots &roots) -> std::vector< double >
Return all real roots of a group of roots.
Definition: Roots.cpp:120
auto cardano(double a, double b, double c, double d) -> CubicRoots
Calculate the roots of a cubic equation using Cardano's method.
Definition: Roots.cpp:28
Eigen::Ref< Eigen::VectorXd > VectorRef
< Alias to Eigen type VectorXd.
Definition: Matrix.hpp:29
Eigen::Ref< const Eigen::VectorXd > VectorConstRef
< Alias to Eigen type Ref<VectorXd>.
Definition: Matrix.hpp:31
auto newton(const std::function< std::tuple< double, double >(double)> &f, double x0, double epsilon, unsigned maxiter) -> double
Calculate the root of a non-linear function using Newton's method.
Definition: Roots.cpp:76