Reaktoro
A unified framework for modeling chemically reactive systems
Matrix.hxx
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 namespace Reaktoro {
21 
22 inline auto zeros(Index rows) -> decltype(Vector::Zero(rows))
23 {
24  return Vector::Zero(rows);
25 }
26 
27 inline auto ones(Index rows) -> decltype(Vector::Ones(rows))
28 {
29  return Vector::Ones(rows);
30 }
31 
32 inline auto constants(Index rows, double val) -> decltype(Vector::Constant(rows, val))
33 {
34  return Vector::Constant(rows, val);
35 }
36 
37 inline auto random(Index rows) -> decltype(Vector::Random(rows))
38 {
39  return Vector::Random(rows);
40 }
41 
42 inline auto linspace(Index rows, double start, double stop) -> decltype(Vector::LinSpaced(rows, start, stop))
43 {
44  return Vector::LinSpaced(rows, start, stop);
45 }
46 
47 inline auto unit(Index rows, Index i) -> decltype(Vector::Unit(rows, i))
48 {
49  return Vector::Unit(rows, i);
50 }
51 
52 inline auto unitcol(Index rows, Index i) -> decltype(unit(rows, i))
53 {
54  return unit(rows, i);
55 }
56 
57 inline auto unitrow(Index cols, Index i) -> decltype(RowVector::Unit(cols, i))
58 {
59  return RowVector::Unit(cols, i);
60 }
61 
62 inline auto zeros(Index rows, Index cols) -> decltype(Matrix::Zero(rows, cols))
63 {
64  return Matrix::Zero(rows, cols);
65 }
66 
67 inline auto ones(Index rows, Index cols) -> decltype(Matrix::Ones(rows, cols))
68 {
69  return Matrix::Ones(rows, cols);
70 }
71 
72 inline auto random(Index rows, Index cols) -> decltype(Matrix::Random(rows, cols))
73 {
74  return Matrix::Random(rows, cols);
75 }
76 
77 inline auto identity(Index rows, Index cols) -> decltype(Matrix::Identity(rows, cols))
78 {
79  return Matrix::Identity(rows, cols);
80 }
81 
82 template<typename Derived>
83 auto rows(Eigen::MatrixBase<Derived>& mat, Index start, Index num) -> decltype(mat.middleRows(start, num))
84 {
85  return mat.middleRows(start, num);
86 }
87 
88 template<typename Derived>
89 auto rows(const Eigen::MatrixBase<Derived>& mat, Index start, Index num) -> decltype(mat.middleRows(start, num))
90 {
91  return mat.middleRows(start, num);
92 }
93 
94 template<typename Derived, typename Indices>
95 auto rows(Eigen::MatrixBase<Derived>& mat, const Indices& irows) -> decltype(mat(irows, Eigen::all))
96 {
97  return mat(irows, Eigen::all);
98 }
99 
100 template<typename Derived, typename Indices>
101 auto rows(const Eigen::MatrixBase<Derived>& mat, const Indices& irows) -> decltype(mat(irows, Eigen::all))
102 {
103  return mat(irows, Eigen::all);
104 }
105 
106 template<typename Derived>
107 auto cols(Eigen::MatrixBase<Derived>& mat, Index start, Index num) -> decltype(mat.middleCols(start, num))
108 {
109  return mat.middleCols(start, num);
110 }
111 
112 template<typename Derived>
113 auto cols(const Eigen::MatrixBase<Derived>& mat, Index start, Index num) -> decltype(mat.middleCols(start, num))
114 {
115  return mat.middleCols(start, num);
116 }
117 
118 template<typename Derived, typename Indices>
119 auto cols(Eigen::MatrixBase<Derived>& mat, const Indices& icols) -> decltype(mat(Eigen::all, icols))
120 {
121  return mat(Eigen::all, icols);
122 }
123 
124 template<typename Derived, typename Indices>
125 auto cols(const Eigen::MatrixBase<Derived>& mat, const Indices& icols) -> decltype(mat(Eigen::all, icols))
126 {
127  return mat(Eigen::all, icols);
128 }
129 
130 template<typename Derived>
131 auto segment(Eigen::MatrixBase<Derived>& vec, Index irow, Index nrows) -> decltype(vec.segment(irow, nrows))
132 {
133  return vec.segment(irow, nrows);
134 }
135 
136 template<typename Derived>
137 auto segment(const Eigen::MatrixBase<Derived>& vec, Index irow, Index nrows) -> decltype(vec.segment(irow, nrows))
138 {
139  return vec.segment(irow, nrows);
140 }
141 
142 template<typename Derived>
143 auto block(Eigen::MatrixBase<Derived>& mat, Index irow, Index icol, Index nrows, Index ncols) -> decltype(mat.block(irow, icol, nrows, ncols))
144 {
145  return mat.block(irow, icol, nrows, ncols);
146 }
147 
148 template<typename Derived>
149 auto block(const Eigen::MatrixBase<Derived>& mat, Index irow, Index icol, Index nrows, Index ncols) -> decltype(mat.block(irow, icol, nrows, ncols))
150 {
151  return mat.block(irow, icol, nrows, ncols);
152 }
153 
154 template<typename Derived, typename Indices>
155 auto submatrix(Eigen::MatrixBase<Derived>& mat, const Indices& irows, const Indices& icols) -> decltype(mat(irows, icols))
156 {
157  return mat(irows, icols);
158 }
159 
160 template<typename Derived, typename Indices>
161 auto submatrix(const Eigen::MatrixBase<Derived>& mat, const Indices& irows, const Indices& icols) -> decltype(mat(irows, icols))
162 {
163  return mat(irows, icols);
164 }
165 
166 template<typename Derived>
167 auto tr(Eigen::MatrixBase<Derived>& mat) -> decltype(mat.transpose())
168 {
169  return mat.transpose();
170 }
171 
172 template<typename Derived>
173 auto tr(const Eigen::MatrixBase<Derived>& mat) -> decltype(mat.transpose())
174 {
175  return mat.transpose();
176 }
177 
178 template<typename Derived>
179 auto inv(const Eigen::MatrixBase<Derived>& mat) -> decltype(mat.cwiseInverse())
180 {
181  return mat.cwiseInverse();
182 }
183 
184 template<typename Derived>
185 auto diag(const Eigen::MatrixBase<Derived>& vec) -> decltype(vec.asDiagonal())
186 {
187  return vec.asDiagonal();
188 }
189 
190 template<typename Derived>
191 auto diagonal(Eigen::MatrixBase<Derived>& mat) -> decltype(mat.diagonal())
192 {
193  return mat.diagonal();
194 }
195 
196 template<typename Derived>
197 auto diagonal(const Eigen::MatrixBase<Derived>& mat) -> decltype(mat.diagonal())
198 {
199  return mat.diagonal();
200 }
201 
202 template<int p, typename Derived>
203 auto norm(const Eigen::MatrixBase<Derived>& mat) -> double
204 {
205  return mat.template lpNorm<p>();
206 }
207 
208 template<typename Derived>
209 auto norm(const Eigen::MatrixBase<Derived>& mat) -> double
210 {
211  return mat.norm();
212 }
213 
214 template<typename Derived>
215 auto norminf(const Eigen::MatrixBase<Derived>& mat) -> double
216 {
217  return mat.template lpNorm<Eigen::Infinity>();
218 }
219 
220 template<typename Derived>
221 auto sum(const Eigen::DenseBase<Derived>& mat) -> typename Derived::Scalar
222 {
223  return mat.sum();
224 }
225 
226 template<typename DerivedLHS, typename DerivedRHS>
227 auto dot(const Eigen::MatrixBase<DerivedLHS>& lhs, const Eigen::MatrixBase<DerivedRHS>& rhs) -> decltype(lhs.dot(rhs))
228 {
229  return lhs.dot(rhs);
230 }
231 
232 template<typename Derived>
233 auto min(const Eigen::MatrixBase<Derived>& mat) -> decltype(mat.minCoeff())
234 {
235  return mat.minCoeff();
236 }
237 
238 template<typename DerivedLHS, typename DerivedRHS>
239 auto min(const Eigen::MatrixBase<DerivedLHS>& lhs, const Eigen::MatrixBase<DerivedRHS>& rhs) -> decltype(lhs.cwiseMin(rhs))
240 {
241  return lhs.cwiseMin(rhs);
242 }
243 
244 template<typename Derived>
245 auto max(const Eigen::MatrixBase<Derived>& mat) -> decltype(mat.maxCoeff())
246 {
247  return mat.maxCoeff();
248 }
249 
250 template<typename DerivedLHS, typename DerivedRHS>
251 auto max(const Eigen::MatrixBase<DerivedLHS>& lhs, const Eigen::MatrixBase<DerivedRHS>& rhs) -> decltype(lhs.cwiseMax(rhs))
252 {
253  return lhs.cwiseMax(rhs);
254 }
255 
256 template<typename DerivedLHS, typename DerivedRHS>
257 auto operator%(const Eigen::MatrixBase<DerivedLHS>& lhs, const Eigen::MatrixBase<DerivedRHS>& rhs) -> decltype(lhs.cwiseProduct(rhs))
258 {
259  return lhs.cwiseProduct(rhs);
260 }
261 
262 template<typename DerivedLHS, typename DerivedRHS>
263 auto operator/(const Eigen::MatrixBase<DerivedLHS>& lhs, const Eigen::MatrixBase<DerivedRHS>& rhs) -> decltype(lhs.cwiseQuotient(rhs))
264 {
265  return lhs.cwiseQuotient(rhs);
266 }
267 
268 template<typename Derived>
269 auto operator/(const typename Derived::Scalar& scalar, const Eigen::MatrixBase<Derived>& mat) -> decltype(scalar*mat.cwiseInverse())
270 {
271  return scalar*mat.cwiseInverse();
272 }
273 
274 template<typename Derived>
275 auto operator+(const typename Derived::Scalar& scalar, const Eigen::MatrixBase<Derived>& mat) -> decltype((scalar + mat.array()).matrix())
276 {
277  return (scalar + mat.array()).matrix();
278 }
279 
280 template<typename Derived>
281 auto operator+(const Eigen::MatrixBase<Derived>& mat, const typename Derived::Scalar& scalar) -> decltype((scalar + mat.array()).matrix())
282 {
283  return (scalar + mat.array()).matrix();
284 }
285 
286 template<typename Derived>
287 auto operator-(const typename Derived::Scalar& scalar, const Eigen::MatrixBase<Derived>& mat) -> decltype((scalar - mat.array()).matrix())
288 {
289  return (scalar - mat.array()).matrix();
290 }
291 
292 template<typename Derived>
293 auto operator-(const Eigen::MatrixBase<Derived>& mat, const typename Derived::Scalar& scalar) -> decltype((mat.array() - scalar).matrix())
294 {
295  return (mat.array() - scalar).matrix();
296 }
297 
298 template<typename Derived>
299 auto abs(const Eigen::MatrixBase<Derived>& mat) -> decltype(mat.cwiseAbs())
300 {
301  return mat.cwiseAbs();
302 }
303 
304 template<typename Derived>
305 auto sqrt(const Eigen::MatrixBase<Derived>& mat) -> decltype(mat.cwiseSqrt())
306 {
307  return mat.cwiseSqrt();
308 }
309 
310 template<typename Derived>
311 auto pow(const Eigen::MatrixBase<Derived>& mat, double power) -> decltype(mat.array().pow(power).matrix())
312 {
313  return mat.array().pow(power).matrix();
314 }
315 
316 template<typename Derived>
317 auto exp(const Eigen::MatrixBase<Derived>& mat) -> decltype(mat.array().exp().matrix())
318 {
319  return mat.array().exp().matrix();
320 }
321 
322 template<typename Derived>
323 auto log(const Eigen::MatrixBase<Derived>& mat) -> decltype(mat.array().log().matrix())
324 {
325  return mat.array().log().matrix();
326 }
327 
328 template<typename Derived>
329 auto log10(const Eigen::MatrixBase<Derived>& mat) -> decltype(mat.array().log10().matrix())
330 {
331  return mat.array().log10().matrix();
332 }
333 
334 } // namespace Reaktoro
auto random(Index rows) -> decltype(Vector::Random(rows))
Return an expression of a vector with random entries.
Definition: Matrix.hxx:37
auto inv(const Eigen::MatrixBase< Derived > &mat) -> decltype(mat.cwiseInverse())
Return the component-wise inverse of the matrix.
Definition: Matrix.hxx:179
auto norminf(const Eigen::MatrixBase< Derived > &mat) -> double
Return the L-inf norm of a matrix.
Definition: Matrix.hxx:215
auto submatrix(Eigen::MatrixBase< Derived > &mat, const Indices &irows, const Indices &icols) -> decltype(mat(irows, icols))
Return a view of some rows and columns of a matrix.
Definition: Matrix.hxx:155
auto norm(const Eigen::MatrixBase< Derived > &mat) -> double
Return the Lp norm of a matrix.
Definition: Matrix.hxx:203
auto linspace(Index rows, double start, double stop) -> decltype(Vector::LinSpaced(rows, start, stop))
Return a linearly spaced vector.
Definition: Matrix.hxx:42
The namespace containing all components of the Reaktoro library.
Definition: ChemicalScalar.hpp:24
auto identity(Index rows, Index cols) -> decltype(Matrix::Identity(rows, cols))
Return an expression of an identity matrix.
Definition: Matrix.hxx:77
auto segment(Eigen::MatrixBase< Derived > &vec, Index irow, Index nrows) -> decltype(vec.segment(irow, nrows))
Return a view of some rows and columns of a matrix.
Definition: Matrix.hxx:131
auto zeros(const ChemicalVectorBase< V, T, P, N > &v) -> ChemicalVectorBase< decltype(zeros(0)), decltype(zeros(0)), decltype(zeros(0)), decltype(zeros(0, 0))>
Return a ChemicalVectorBase expression representing zeros with same dimension of given vector.
Definition: ChemicalVector.hpp:361
auto rows(ChemicalVectorBase< V, T, P, N > &vec, Index irow, Index nrows) -> ChemicalVectorBase< decltype(rows(vec.val, irow, nrows)), decltype(rows(vec.ddT, irow, nrows)), decltype(rows(vec.ddP, irow, nrows)), decltype(rows(vec.ddn, irow, nrows))>
Return a reference of a sequence of rows of this ChemicalVectorBase instance.
Definition: ChemicalVector.hpp:680
auto cols(Eigen::MatrixBase< Derived > &mat, Index start, Index num) -> decltype(mat.middleCols(start, num))
Return a view of a sequence of columns of a matrix.
Definition: Matrix.hxx:107
std::size_t Index
Define a type that represents an index.
Definition: Index.hpp:26
auto diagonal(Eigen::MatrixBase< Derived > &mat) -> decltype(mat.diagonal())
Return a vector representation of the diagonal of a matrix.
Definition: Matrix.hxx:191
std::vector< Index > Indices
Define a type that represents a collection of indices.
Definition: Index.hpp:29
auto ones(const ChemicalVectorBase< V, T, P, N > &v) -> ChemicalVectorBase< decltype(ones(0)), decltype(zeros(0)), decltype(zeros(0)), decltype(zeros(0, 0))>
Return a ChemicalVectorBase expression representing ones with same dimension of given vector.
Definition: ChemicalVector.hpp:369
auto tr(Eigen::MatrixBase< Derived > &mat) -> decltype(mat.transpose())
Return the transpose of the matrix.
Definition: Matrix.hxx:167
auto block(Eigen::MatrixBase< Derived > &mat, Index irow, Index icol, Index nrows, Index ncols) -> decltype(mat.block(irow, icol, nrows, ncols))
Return a view of some rows and columns of a matrix.
Definition: Matrix.hxx:143
auto diag(const Eigen::MatrixBase< Derived > &vec) -> decltype(vec.asDiagonal())
Return a diagonal matrix representation of a vector.
Definition: Matrix.hxx:185
auto unit(Index rows, Index i) -> decltype(Vector::Unit(rows, i))
Return an expression of a unit vector.
Definition: Matrix.hxx:47