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-2015 Allan Leal
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU 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 // This program 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 General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. 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 zeros(Index rows, Index cols) -> decltype(Matrix::Zero(rows, cols))
53 {
54  return Matrix::Zero(rows, cols);
55 }
56 
57 inline auto ones(Index rows, Index cols) -> decltype(Matrix::Ones(rows, cols))
58 {
59  return Matrix::Ones(rows, cols);
60 }
61 
62 inline auto random(Index rows, Index cols) -> decltype(Matrix::Random(rows, cols))
63 {
64  return Matrix::Random(rows, cols);
65 }
66 
67 inline auto identity(Index rows, Index cols) -> decltype(Matrix::Identity(rows, cols))
68 {
69  return Matrix::Identity(rows, cols);
70 }
71 
72 template<typename Derived>
73 auto rows(Eigen::MatrixBase<Derived>& mat, Index start, Index num) -> decltype(mat.middleRows(start, num))
74 {
75  return mat.middleRows(start, num);
76 }
77 
78 template<typename Derived>
79 auto rows(const Eigen::MatrixBase<Derived>& mat, Index start, Index num) -> decltype(mat.middleRows(start, num))
80 {
81  return mat.middleRows(start, num);
82 }
83 
84 template<typename Derived, typename Indices>
85 auto rows(Eigen::MatrixBase<Derived>& mat, const Indices& irows) -> Eigen::MatrixRowsView<Derived, Indices>
86 {
87  return {mat, irows};
88 }
89 
90 template<typename Derived, typename Indices>
91 auto rows(const Eigen::MatrixBase<Derived>& mat, const Indices& irows) -> Eigen::MatrixRowsViewConst<Derived, Indices>
92 {
93  return {mat, irows};
94 }
95 
96 template<typename Derived>
97 auto cols(Eigen::MatrixBase<Derived>& mat, Index start, Index num) -> decltype(mat.middleCols(start, num))
98 {
99  return mat.middleCols(start, num);
100 }
101 
102 template<typename Derived>
103 auto cols(const Eigen::MatrixBase<Derived>& mat, Index start, Index num) -> decltype(mat.middleCols(start, num))
104 {
105  return mat.middleCols(start, num);
106 }
107 
108 template<typename Derived, typename Indices>
109 auto cols(Eigen::MatrixBase<Derived>& mat, const Indices& icols) -> Eigen::MatrixColsView<Derived, Indices>
110 {
111  return {mat, icols};
112 }
113 
114 template<typename Derived, typename Indices>
115 auto cols(const Eigen::MatrixBase<Derived>& mat, const Indices& icols) -> Eigen::MatrixColsViewConst<Derived, Indices>
116 {
117  return {mat, icols};
118 }
119 
120 template<typename Derived>
121 auto segment(Eigen::MatrixBase<Derived>& vec, Index irow, Index nrows) -> decltype(vec.segment(irow, nrows))
122 {
123  return vec.segment(irow, nrows);
124 }
125 
126 template<typename Derived>
127 auto segment(const Eigen::MatrixBase<Derived>& vec, Index irow, Index nrows) -> decltype(vec.segment(irow, nrows))
128 {
129  return vec.segment(irow, nrows);
130 }
131 
132 template<typename Derived>
133 auto block(Eigen::MatrixBase<Derived>& mat, Index irow, Index icol, Index nrows, Index ncols) -> decltype(mat.block(irow, icol, nrows, ncols))
134 {
135  return mat.block(irow, icol, nrows, ncols);
136 }
137 
138 template<typename Derived>
139 auto block(const Eigen::MatrixBase<Derived>& mat, Index irow, Index icol, Index nrows, Index ncols) -> decltype(mat.block(irow, icol, nrows, ncols))
140 {
141  return mat.block(irow, icol, nrows, ncols);
142 }
143 
144 template<typename Derived, typename Indices>
145 auto submatrix(Eigen::MatrixBase<Derived>& mat, const Indices& irows, const Indices& icols) -> Eigen::MatrixSubView<Derived, Indices>
146 {
147  return {mat, irows, icols};
148 }
149 
150 template<typename Derived, typename Indices>
151 auto submatrix(const Eigen::MatrixBase<Derived>& mat, const Indices& irows, const Indices& icols) -> Eigen::MatrixSubViewConst<Derived, Indices>
152 {
153  return {mat, irows, icols};
154 }
155 
156 template<typename Derived>
157 auto tr(Eigen::MatrixBase<Derived>& mat) -> decltype(mat.transpose())
158 {
159  return mat.transpose();
160 }
161 
162 template<typename Derived>
163 auto tr(const Eigen::MatrixBase<Derived>& mat) -> decltype(mat.transpose())
164 {
165  return mat.transpose();
166 }
167 
168 template<typename Derived>
169 auto inv(const Eigen::MatrixBase<Derived>& mat) -> decltype(mat.cwiseInverse())
170 {
171  return mat.cwiseInverse();
172 }
173 
174 template<typename Derived>
175 auto diag(const Eigen::MatrixBase<Derived>& vec) -> decltype(vec.asDiagonal())
176 {
177  return vec.asDiagonal();
178 }
179 
180 template<typename Derived>
181 auto diagonal(Eigen::MatrixBase<Derived>& mat) -> decltype(mat.diagonal())
182 {
183  return mat.diagonal();
184 }
185 
186 template<typename Derived>
187 auto diagonal(const Eigen::MatrixBase<Derived>& mat) -> decltype(mat.diagonal())
188 {
189  return mat.diagonal();
190 }
191 
192 template<int p, typename Derived>
193 auto norm(const Eigen::MatrixBase<Derived>& mat) -> double
194 {
195  return mat.template lpNorm<p>();
196 }
197 
198 template<typename Derived>
199 auto norm(const Eigen::MatrixBase<Derived>& mat) -> double
200 {
201  return mat.norm();
202 }
203 
204 template<typename Derived>
205 auto norminf(const Eigen::MatrixBase<Derived>& mat) -> double
206 {
207  return mat.template lpNorm<Eigen::Infinity>();
208 }
209 
210 template<typename Derived>
211 auto sum(const Eigen::DenseBase<Derived>& mat) -> typename Derived::Scalar
212 {
213  return mat.sum();
214 }
215 
216 template<typename DerivedLHS, typename DerivedRHS>
217 auto dot(const Eigen::MatrixBase<DerivedLHS>& lhs, const Eigen::MatrixBase<DerivedRHS>& rhs) -> decltype(lhs.dot(rhs))
218 {
219  return lhs.dot(rhs);
220 }
221 
222 template<typename Derived>
223 auto min(const Eigen::MatrixBase<Derived>& mat) -> decltype(mat.minCoeff())
224 {
225  return mat.minCoeff();
226 }
227 
228 template<typename DerivedLHS, typename DerivedRHS>
229 auto min(const Eigen::MatrixBase<DerivedLHS>& lhs, const Eigen::MatrixBase<DerivedRHS>& rhs) -> decltype(lhs.cwiseMin(rhs))
230 {
231  return lhs.cwiseMin(rhs);
232 }
233 
234 template<typename Derived>
235 auto max(const Eigen::MatrixBase<Derived>& mat) -> decltype(mat.maxCoeff())
236 {
237  return mat.maxCoeff();
238 }
239 
240 template<typename DerivedLHS, typename DerivedRHS>
241 auto max(const Eigen::MatrixBase<DerivedLHS>& lhs, const Eigen::MatrixBase<DerivedRHS>& rhs) -> decltype(lhs.cwiseMax(rhs))
242 {
243  return lhs.cwiseMax(rhs);
244 }
245 
246 template<typename DerivedLHS, typename DerivedRHS>
247 auto operator%(const Eigen::MatrixBase<DerivedLHS>& lhs, const Eigen::MatrixBase<DerivedRHS>& rhs) -> decltype(lhs.cwiseProduct(rhs))
248 {
249  return lhs.cwiseProduct(rhs);
250 }
251 
252 template<typename DerivedLHS, typename DerivedRHS>
253 auto operator/(const Eigen::MatrixBase<DerivedLHS>& lhs, const Eigen::MatrixBase<DerivedRHS>& rhs) -> decltype(lhs.cwiseQuotient(rhs))
254 {
255  return lhs.cwiseQuotient(rhs);
256 }
257 
258 template<typename Derived>
259 auto operator/(const typename Derived::Scalar& scalar, const Eigen::MatrixBase<Derived>& mat) -> decltype(scalar*mat.cwiseInverse())
260 {
261  return scalar*mat.cwiseInverse();
262 }
263 
264 template<typename Derived>
265 auto operator+(const typename Derived::Scalar& scalar, const Eigen::MatrixBase<Derived>& mat) -> decltype((scalar + mat.array()).matrix())
266 {
267  return (scalar + mat.array()).matrix();
268 }
269 
270 template<typename Derived>
271 auto operator+(const Eigen::MatrixBase<Derived>& mat, const typename Derived::Scalar& scalar) -> decltype((scalar + mat.array()).matrix())
272 {
273  return (scalar + mat.array()).matrix();
274 }
275 
276 template<typename Derived>
277 auto operator-(const typename Derived::Scalar& scalar, const Eigen::MatrixBase<Derived>& mat) -> decltype((scalar - mat.array()).matrix())
278 {
279  return (scalar - mat.array()).matrix();
280 }
281 
282 template<typename Derived>
283 auto operator-(const Eigen::MatrixBase<Derived>& mat, const typename Derived::Scalar& scalar) -> decltype((mat.array() - scalar).matrix())
284 {
285  return (mat.array() - scalar).matrix();
286 }
287 
288 template<typename Derived>
289 auto abs(const Eigen::MatrixBase<Derived>& mat) -> decltype(mat.cwiseAbs())
290 {
291  return mat.cwiseAbs();
292 }
293 
294 template<typename Derived>
295 auto sqrt(const Eigen::MatrixBase<Derived>& mat) -> decltype(mat.cwiseSqrt())
296 {
297  return mat.cwiseSqrt();
298 }
299 
300 template<typename Derived>
301 auto pow(const Eigen::MatrixBase<Derived>& mat, double power) -> decltype(mat.array().pow(power).matrix())
302 {
303  return mat.array().pow(power).matrix();
304 }
305 
306 template<typename Derived>
307 auto exp(const Eigen::MatrixBase<Derived>& mat) -> decltype(mat.array().exp().matrix())
308 {
309  return mat.array().exp().matrix();
310 }
311 
312 template<typename Derived>
313 auto log(const Eigen::MatrixBase<Derived>& mat) -> decltype(mat.array().log().matrix())
314 {
315  return mat.array().log().matrix();
316 }
317 
318 template<typename Derived>
319 auto log10(const Eigen::MatrixBase<Derived>& mat) -> decltype(mat.array().log10().matrix())
320 {
321  return mat.array().log10().matrix();
322 }
323 
324 } // namespace Reaktoro
auto norminf(const Eigen::MatrixBase< Derived > &mat) -> double
Return the L-inf norm of a matrix.
Definition: Matrix.hxx:205
std::vector< Index > Indices
Define a type that represents a collection of indices.
Definition: Index.hpp:29
auto diag(const Eigen::MatrixBase< Derived > &vec) -> decltype(vec.asDiagonal())
Return a diagonal matrix representation of a vector.
Definition: Matrix.hxx:175
auto diagonal(Eigen::MatrixBase< Derived > &mat) -> decltype(mat.diagonal())
Return a vector representation of the diagonal of a matrix.
Definition: Matrix.hxx:181
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:618
auto inv(const Eigen::MatrixBase< Derived > &mat) -> decltype(mat.cwiseInverse())
Return the component-wise inverse of the matrix.
Definition: Matrix.hxx:169
auto linspace(Index rows, double start, double stop) -> decltype(Vector::LinSpaced(rows, start, stop))
Return a linearly spaced vector.
Definition: Matrix.hxx:42
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:133
auto identity(Index rows, Index cols) -> decltype(Matrix::Identity(rows, cols))
Return an expression of an identity matrix.
Definition: Matrix.hxx:67
auto unit(Index rows, Index i) -> decltype(Vector::Unit(rows, i))
Return an expression of a unit vector.
Definition: Matrix.hxx:47
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:121
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:97
auto ones(Index rows) -> decltype(Vector::Ones(rows))
Return an expression of a vector with entries equal to one.
Definition: Matrix.hxx:27
std::size_t Index
Define a type that represents an index.
Definition: Index.hpp:26
auto tr(Eigen::MatrixBase< Derived > &mat) -> decltype(mat.transpose())
Return the transpose of the matrix.
Definition: Matrix.hxx:157
The namespace containing all components of the Reaktoro library.
Definition: ChemicalScalar.hpp:24
auto norm(const Eigen::MatrixBase< Derived > &mat) -> double
Return the Lp norm of a matrix.
Definition: Matrix.hxx:193
auto submatrix(Eigen::MatrixBase< Derived > &mat, const Indices &irows, const Indices &icols) -> Eigen::MatrixSubView< Derived, Indices >
Return a view of some rows and columns of a matrix.
Definition: Matrix.hxx:145
auto random(Index rows) -> decltype(Vector::Random(rows))
Return an expression of a vector with random entries.
Definition: Matrix.hxx:37
auto zeros(Index rows) -> decltype(Vector::Zero(rows))
Return an expression of a zero vector.
Definition: Matrix.hxx:22