Reaktoro  v2.11.0
A unified framework for modeling chemically reactive systems
MathUtils.hpp
1 // Reaktoro is a unified framework for modeling chemically reactive systems.
2 //
3 // Copyright © 2014-2024 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 // Reaktoro includes
21 #include <Reaktoro/Common/Index.hpp>
22 #include <Reaktoro/Common/Real.hpp>
23 #include <Reaktoro/Common/Matrix.hpp>
24 
25 namespace Reaktoro {
26 
31 
36 
42 
48 
53 
60 auto rationalize(double x, unsigned maxden) -> std::tuple<long, long>;
61 
66 auto cleanRationalNumbers(double* vals, long size, long maxden = 6) -> void;
67 
71 auto cleanRationalNumbers(MatrixXdRef A, long maxden = 6) -> void;
72 
74 auto dot3p(VectorXdConstRef x, VectorXdConstRef y, double s) -> double;
75 
78 
80 template<typename T, typename U>
81 auto largestRelativeDifference(Eigen::ArrayBase<T> const& actual, Eigen::ArrayBase<U> const& expected) -> double
82 {
83  return ((actual - expected)/expected).abs().maxCoeff();
84 }
85 
87 template<typename T, typename U>
88 auto largestRelativeDifferenceLogScale(Eigen::ArrayBase<T> const& actual, Eigen::ArrayBase<U> const& expected) -> double
89 {
90  return largestRelativeDifference(actual.log(), expected.log());
91 }
92 
93 } // namespace Reaktoro
The namespace containing all components of the Reaktoro library.
Definition: Algorithms.hpp:29
auto cleanRationalNumbers(double *vals, long size, long maxden=6) -> void
Clean an array that is known to have rational numbers from round-off errors.
auto largestRelativeDifference(Eigen::ArrayBase< T > const &actual, Eigen::ArrayBase< U > const &expected) -> double
Return the largest relative difference between two arrays actual and expected.
Definition: MathUtils.hpp:81
auto linearlyIndependentCols(MatrixXdConstRef A) -> Indices
Determine the set of linearly independent columns in a matrix using a column pivoting QR algorithm.
auto largestRelativeDifferenceLogScale(Eigen::ArrayBase< T > const &actual, Eigen::ArrayBase< U > const &expected) -> double
Return the largest relative difference between two arrays actual and expected in log scale.
Definition: MathUtils.hpp:88
auto rationalize(double x, unsigned maxden) -> std::tuple< long, long >
Calculates the rational number that approximates a given real number.
Eigen::MatrixXd MatrixXd
Convenient alias to Eigen type.
Definition: Matrix.hpp:137
Eigen::Ref< const VectorXd > VectorXdConstRef
Convenient alias to Eigen type.
Definition: Matrix.hpp:76
Eigen::VectorXd VectorXd
Convenient alias to Eigen type.
Definition: Matrix.hpp:74
auto inverseShermanMorrison(MatrixXdConstRef invA, VectorXdConstRef D) -> MatrixXd
Calculate the inverse of A + D where inv(A) is already known and D is a diagonal matrix.
auto linearlyIndependentRows(MatrixXdConstRef A) -> Indices
Determine the set of linearly independent rows in a matrix.
auto residual3p(MatrixXdConstRef A, VectorXdConstRef x, VectorXdConstRef b) -> VectorXd
Return the residual of the equation A*x - b with triple-precision.
auto dot3p(VectorXdConstRef x, VectorXdConstRef y, double s) -> double
Return the dot product s + dot(x, y) of two vectors with triple-precision.
std::vector< Index > Indices
Define a type that represents a collection of indices.
Definition: Index.hpp:29
Eigen::Ref< const MatrixXd > MatrixXdConstRef
Convenient alias to Eigen type.
Definition: Matrix.hpp:139
Eigen::Ref< MatrixXd > MatrixXdRef
Convenient alias to Eigen type.
Definition: Matrix.hpp:138