KIDS  ver-0.0.1
KIDS : Kernel Integrated Dynamics Simulator
Loading...
Searching...
No Matches
la_utils.h
Go to the documentation of this file.
1
15#ifndef LA_Utils_H
16#define LA_Utils_H
17#include "array_utils.h"
18#include "types.h"
19
20/*==============================================
21= la_utils configuration =
22==============================================*/
23
24
25#define ARRAY_USE_EIGEN
26// #define EIGEN_USE_MKL
27
28/*===== End of la_utils configuration ======*/
29
30
31/*===========================================================
32= realize interface of linear algebra =
33===========================================================*/
34
35/*---------- MKL interface to linear algebra ----------*/
36
37#ifdef ARRAY_USE_MKL
38
39extern lapack_int LAPACKE_dsyev(int matrix_layout, char jobz, char uplo, lapack_int n, double* a, lapack_int lda,
40 double* w);
41
42extern lapack_int LAPACKE_zheev(int matrix_layout, char jobz, char uplo, lapack_int n, lapack_complex_double* a,
43 lapack_int lda, double* w);
44
45extern lapack_int LAPACKE_zgeev(int matrix_layout, char jobvl, char jobvr, lapack_int n, lapack_complex_double* a,
46 lapack_int lda, lapack_complex_double* w, lapack_complex_double* vl, lapack_int ldvl,
47 lapack_complex_double* vr, lapack_int ldvr);
48
49void EigenSolve(num_real* E, num_real* T, num_real* A, const int& N);
50
51void EigenSolve(num_real* E, num_complex* T, num_complex* A, const int& N);
52
53void EigenSolve_zgeev(num_complex* E, num_complex* Tl, num_complex* Tr, num_complex* A, const int& N);
54
55/*---------- Eigen interface to linear algebra ----------*/
56
57#elif defined(ARRAY_USE_EIGEN)
58
59#include "../thirdpart/Eigen/Dense"
60#include "../thirdpart/Eigen/QR"
61
62#define EigMajor Eigen::RowMajor
63
64template <class T>
65using EigVX = Eigen::Matrix<T, Eigen::Dynamic, 1, EigMajor>;
66
67template <class T>
68using EigMX = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, EigMajor>;
69
70template <class T>
71using EigAX = Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic, EigMajor>;
72
79typedef Eigen::Map<EigVXr> MapVXr;
80typedef Eigen::Map<EigVXc> MapVXc;
81typedef Eigen::Map<EigMXr> MapMXr;
82typedef Eigen::Map<EigMXc> MapMXc;
83typedef Eigen::Map<EigAXr> MapAXr;
84typedef Eigen::Map<EigAXc> MapAXc;
85
86void LinearSolve(num_real* x, num_real* A, num_real* b, const int& N);
87
88void EigenSolve(num_real* E, num_real* T, num_real* A, const int& N);
89
90void EigenSolve(num_real* E, num_complex* T, num_complex* A, const int& N);
91
92void EigenSolve(num_complex* E, num_complex* T, num_complex* A, const int& N);
93
94void PseudoInverse(num_real* A, num_real* invA, const int& N, num_real e = 1E-5);
95
96
100#define DEFINE_POINTER(T, name) \
101 public: \
102 EigMX<T>& ref_##name() { return name##_eigen_container; } \
103 T* name; \
104 EigMX<T> name##_eigen_container;
105
106#define DEFINE_POINTER_PROTECTED(T, name) \
107 public: \
108 EigMX<T>& ref_##name() { return name##_eigen_container; } \
109 \
110 protected: \
111 T* name; \
112 EigMX<T> name##_eigen_container;
113
117#define ALLOCATE_PTR_TO_VECTOR(name, size) \
118 name##_eigen_container.resize(size, 1); \
119 name = name##_eigen_container.data();
120
124#define ALLOCATE_PTR_TO_MATRIX(name, size1, size2) \
125 name##_eigen_container.resize(size1, size2); \
126 name = name##_eigen_container.data();
127
128
129/*****************************************
130 An brief introduction to Eigen library
131------------------------------------------
132
133// Eigen // Matlab // comments
134x.size() // length(x) // vector size
135C.rows() // size(C,1) // number of rows
136C.cols() // size(C,2) // number of columns
137x(i) // x(i+1) // Matlab is 1-based
138C(i, j) // C(i+1,j+1) //
139
140A.resize(4, 4); // Runtime error if assertions are on.
141B.resize(4, 9); // Runtime error if assertions are on.
142A.resize(3, 3); // Ok; size didn't change.
143B.resize(3, 9); // Ok; only dynamic cols changed.
144
145A << 1, 2, 3, // Initialize A. The elements can also be
146 4, 5, 6, // matrices, which are stacked along cols
147 7, 8, 9; // and then the rows are stacked.
148B << A, A, A; // B is three horizontally stacked A's.
149A.fill(10); // Fill A with all 10's.
150
151
152// Eigen // Matlab
153MatrixXd::Identity(rows,cols) // eye(rows,cols)
154C.setIdentity(rows,cols) // C = eye(rows,cols)
155MatrixXd::Zero(rows,cols) // zeros(rows,cols)
156C.setZero(rows,cols) // C = ones(rows,cols)
157MatrixXd::Ones(rows,cols) // ones(rows,cols)
158C.setOnes(rows,cols) // C = ones(rows,cols)
159MatrixXd::Random(rows,cols) // rand(rows,cols)*2-1
160C.setRandom(rows,cols) // C = rand(rows,cols)*2-1
161VectorXd::LinSpaced(size,low,high) // linspace(low,high,size)'
162v.setLinSpaced(size,low,high) // v = linspace(low,high,size)'
163
164
165// Matrix slicing and blocks. All expressions listed here are read/write.
166// Templated size versions are faster. Note that Matlab is 1-based (a size N
167// vector is x(1)...x(N)).
168// Eigen // Matlab
169x.head(n) // x(1:n)
170x.head<n>() // x(1:n)
171x.tail(n) // x(end - n + 1: end)
172x.tail<n>() // x(end - n + 1: end)
173x.segment(i, n) // x(i+1 : i+n)
174x.segment<n>(i) // x(i+1 : i+n)
175P.block(i, j, rows, cols) // P(i+1 : i+rows, j+1 : j+cols)
176P.block<rows, cols>(i, j) // P(i+1 : i+rows, j+1 : j+cols)
177P.row(i) // P(i+1, :)
178P.col(j) // P(:, j+1)
179P.leftCols<cols>() // P(:, 1:cols)
180P.leftCols(cols) // P(:, 1:cols)
181P.middleCols<cols>(j) // P(:, j+1:j+cols)
182P.middleCols(j, cols) // P(:, j+1:j+cols)
183P.rightCols<cols>() // P(:, end-cols+1:end)
184P.rightCols(cols) // P(:, end-cols+1:end)
185P.topRows<rows>() // P(1:rows, :)
186P.topRows(rows) // P(1:rows, :)
187P.middleRows<rows>(i) // P(i+1:i+rows, :)
188P.middleRows(i, rows) // P(i+1:i+rows, :)
189P.bottomRows<rows>() // P(end-rows+1:end, :)
190P.bottomRows(rows) // P(end-rows+1:end, :)
191P.topLeftCorner(rows, cols) // P(1:rows, 1:cols)
192P.topRightCorner(rows, cols) // P(1:rows, end-cols+1:end)
193P.bottomLeftCorner(rows, cols) // P(end-rows+1:end, 1:cols)
194P.bottomRightCorner(rows, cols) // P(end-rows+1:end, end-cols+1:end)
195P.topLeftCorner<rows,cols>() // P(1:rows, 1:cols)
196P.topRightCorner<rows,cols>() // P(1:rows, end-cols+1:end)
197P.bottomLeftCorner<rows,cols>() // P(end-rows+1:end, 1:cols)
198P.bottomRightCorner<rows,cols>() // P(end-rows+1:end, end-cols+1:end)
199
200
201
202
203// Of particular note is Eigen's swap function which is highly optimized.
204// Eigen // Matlab
205R.row(i) = P.col(j); // R(i, :) = P(:, i)
206R.col(j1).swap(mat1.col(j2)); // R(:, [j1 j2]) = R(:, [j2, j1])
207
208// Views, transpose, etc; all read-write except for .adjoint().
209// Eigen // Matlab
210R.adjoint() // R'
211R.transpose() // R.' or conj(R')
212R.diagonal() // diag(R)
213x.asDiagonal() // diag(x)
214R.transpose().colwise().reverse(); // rot90(R)
215R.conjugate() // conj(R)
216
217// All the same as Matlab, but matlab doesn't have *= style operators.
218// Matrix-vector. Matrix-matrix. Matrix-scalar.
219y = M*x; R = P*Q; R = P*s;
220a = b*M; R = P - Q; R = s*P;
221a *= M; R = P + Q; R = P/s;
222 R *= Q; R = s*P;
223 R += Q; R *= s;
224 R -= Q; R /= s;
225
226
227// Vectorized operations on each element independently
228// Eigen // Matlab
229R = P.cwiseProduct(Q); // R = P .* Q
230R = P.array() * s.array();// R = P .* s
231R = P.cwiseQuotient(Q); // R = P ./ Q
232R = P.array() / Q.array();// R = P ./ Q
233R = P.array() + s.array();// R = P + s
234R = P.array() - s.array();// R = P - s
235R.array() += s; // R = R + s
236R.array() -= s; // R = R - s
237R.array() < Q.array(); // R < Q
238R.array() <= Q.array(); // R <= Q
239R.cwiseInverse(); // 1 ./ P
240R.array().inverse(); // 1 ./ P
241R.array().sin() // sin(P)
242R.array().cos() // cos(P)
243R.array().pow(s) // P .^ s
244R.array().square() // P .^ 2
245R.array().cube() // P .^ 3
246R.cwiseSqrt() // sqrt(P)
247R.array().sqrt() // sqrt(P)
248R.array().exp() // exp(P)
249R.array().log() // log(P)
250R.cwiseMax(P) // max(R, P)
251R.array().max(P.array()) // max(R, P)
252R.cwiseMin(P) // min(R, P)
253R.array().min(P.array()) // min(R, P)
254R.cwiseAbs() //std::abs(P)
255R.array().abs() //std::abs(P)
256R.cwiseAbs2() //std::abs(P.^2)
257R.array().abs2() //std::abs(P.^2)
258(R.array() < s).select(P,Q); // (R < s ? P : Q)
259
260// Reductions.
261int r, c;
262// Eigen // Matlab
263R.minCoeff() // min(R(:))
264R.maxCoeff() // max(R(:))
265s = R.minCoeff(&r, &c) // [s, i] = min(R(:)); [r, c] = ind2sub(size(R), i);
266s = R.maxCoeff(&r, &c) // [s, i] = max(R(:)); [r, c] = ind2sub(size(R), i);
267R.sum() // sum(R(:))
268R.colwise().sum() // sum(R)
269R.rowwise().sum() // sum(R, 2) or sum(R')'
270R.prod() // prod(R(:))
271R.colwise().prod() // prod(R)
272R.rowwise().prod() // prod(R, 2) or prod(R')'
273R.trace() // trace(R)
274R.all() // all(R(:))
275R.colwise().all() // all(R)
276R.rowwise().all() // all(R, 2)
277R.any() // any(R(:))
278R.colwise().any() // any(R)
279R.rowwise().any() // any(R, 2)
280
281
282
283
284// Dot products, norms, etc.
285// Eigen // Matlab
286x.norm() // norm(x). Note that norm(R) doesn't work in Eigen.
287x.squaredNorm() // dot(x, x) Note the equivalence is not true for complex
288x.dot(y) // dot(x, y)
289x.cross(y) // cross(x, y) Requires #include <Eigen/Geometry>
290
292// Eigen // Matlab
293A.cast<num_real>(); // num_real(A)
294A.cast<float>(); // single(A)
295A.cast<int>(); // int32(A)
296A.real(); // real(A)
297A.imag(); // imag(A)
298// if the original type equals destination type, no work is done
299
300
301// Solve Ax = b. Result stored in x. Matlab: x = A \ b.
302x = A.ldlt().solve(b)); // A sym. p.s.d. #include <Eigen/Cholesky>
303x = A.llt() .solve(b)); // A sym. p.d. #include <Eigen/Cholesky>
304x = A.lu() .solve(b)); // Stable and fast. #include <Eigen/LU>
305x = A.qr() .solve(b)); // No pivoting. #include <Eigen/QR>
306x = A.svd() .solve(b)); // Stable, slowest. #include <Eigen/SVD>
307// .ldlt() -> .matrixL() and .matrixD()
308// .llt() -> .matrixL()
309// .lu() -> .matrixL() and .matrixU()
310// .qr() -> .matrixQ() and .matrixR()
311// .svd() -> .matrixU(), .singularValues(), and .matrixV()
312
313// Eigenvalue problems
314// Eigen // Matlab
315A.eigenvalues(); // eig(A);
316EigenSolver<Matrix3d> eig(A); // [vec val] = eig(A)
317eig.eigenvalues(); // diag(val)
318eig.eigenvectors(); // vec
319// For self-adjoint matrices use SelfAdjointEigenSolver<>
320
321
322// Use Map of Eigen, associate Map class with C++ pointer
323
324MapMd W(pW,1,N/F);
325std::cout << std::setiosflags(std::ios::scientific)
326 << std::setprecision(8) << W << std::endl;
327
328*****************************************/
329
330
331/*===== End of realize interface of linear algebra ======*/
332
333
334#endif // ARRAY_USE_MKL & ARRAY_USE_EIGEN
335
336#endif // LA_Utils_H
Eigen::Map< EigMXr > MapMXr
Definition la_utils.h:81
EigMX< num_real > EigAXr
Definition la_utils.h:77
EigVX< num_complex > EigVXc
Definition la_utils.h:74
EigMX< num_complex > EigMXc
Definition la_utils.h:76
Eigen::Map< EigMXc > MapMXc
Definition la_utils.h:82
Eigen::Map< EigAXr > MapAXr
Definition la_utils.h:83
EigMX< num_real > EigMXr
Definition la_utils.h:75
void EigenSolve(num_real *E, num_real *T, num_real *A, const int &N)
Eigen::Map< EigVXr > MapVXr
Definition la_utils.h:79
void PseudoInverse(num_real *A, num_real *invA, const int &N, num_real e=1E-5)
void LinearSolve(num_real *x, num_real *A, num_real *b, const int &N)
Eigen::Map< EigAXc > MapAXc
Definition la_utils.h:84
EigVX< num_real > EigVXr
Definition la_utils.h:73
EigMX< num_complex > EigAXc
Definition la_utils.h:78
Eigen::Map< EigVXc > MapVXc
Definition la_utils.h:80
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, EigMajor > EigMX
Definition linalg_tpl.h:48
Eigen::Matrix< T, Eigen::Dynamic, 1, EigMajor > EigVX
Definition linalg_tpl.h:45
Eigen::Array< T, Eigen::Dynamic, Eigen::Dynamic, EigMajor > EigAX
Definition linalg_tpl.h:51