-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Option to use Eigen versus Lapack to solve equations
On Windows system, it is often tedious to install Lapack/Atlas/Openblas packages so as to simulate PSFs. Configure the project: ``` meson setup -Duse_eigen=true -Darmadillo-code:lapack=none build-windows/ ``` In order to bypass LAPACK depdendencies. Download Eigen3 the header-only library. Implement the Bidiagonal divide and conquer SVD solver. Write a wrapper around the Eigen logic to avoid C++ namespace pollusion.
- Loading branch information
1 parent
7a0ed1e
commit 0e24abc
Showing
12 changed files
with
194 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ option('lapack', | |
'openblas', | ||
'lapack64', | ||
'lapack', | ||
'none', | ||
], | ||
value: 'openblas', | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,11 @@ | ||
option('install_examples', type: 'boolean', value: false) | ||
option('use_boost', type: 'boolean', value: false) | ||
option('use_boost', | ||
description: 'Compute bessel function of the first kind with Boost::Math', | ||
type: 'boolean', | ||
value: false, | ||
) | ||
option('use_eigen', | ||
description: 'Solve linear least squares with Eigen3 the matrix library', | ||
type: 'boolean', | ||
value: false, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#pragma once | ||
|
||
#include <armadillo> | ||
|
||
namespace microsc_psf { | ||
namespace internal { | ||
|
||
/** Solve min || A * x - b ||^2 for x , using Eigen3 solver. | ||
* | ||
* Typical ussage: simulate PSF on machines not having LAPACK support, e.g. | ||
* Windows on ARM64 CPU. | ||
* | ||
* Note: This may seem contrived to have two near-identical C++ libraries | ||
* serving the linear algebra code, but Armadillo has a nice Matlab-like syntax | ||
* that simplifies the code review process a lot. | ||
* | ||
* @tparam tranpose_b True if the signal b needs a Hermitian transpose ahead of | ||
* the least squares solver. | ||
*/ | ||
template <bool transpose_b> | ||
arma::cx_mat solveWithEigen(const arma::mat& A, const arma::cx_mat& b); | ||
} // namespace internal | ||
} // namespace microsc_psf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include "linsolver.h" | ||
|
||
#include <Eigen/Dense> | ||
#include <cassert> | ||
|
||
namespace microsc_psf { | ||
|
||
namespace internal { | ||
|
||
template <bool transpose_b> | ||
arma::cx_mat | ||
solveWithEigen(const arma::mat& A_buffer, const arma::cx_mat& b_buffer) { | ||
using arma::cx_double; | ||
using Eigen::ComputeThinU; | ||
using Eigen::ComputeThinV; | ||
using Eigen::Map; | ||
using Eigen::MatrixX; | ||
using Eigen::MatrixXd; | ||
|
||
const auto m = A_buffer.n_rows; | ||
const auto n = A_buffer.n_cols; | ||
const auto k = (transpose_b ? b_buffer.n_rows : b_buffer.n_cols); | ||
|
||
if constexpr (transpose_b) { | ||
assert(b_buffer.n_cols == m); | ||
} else { | ||
assert(b_buffer.n_rows == m); | ||
} | ||
|
||
// First, map to Eigen's primary data structure. | ||
const Map<const MatrixXd> A(A_buffer.memptr(), m, n); | ||
const Map<const MatrixX<cx_double>> b(b_buffer.memptr(), b_buffer.n_rows, b_buffer.n_cols); | ||
|
||
// Allocate the result buffer | ||
arma::cx_mat xopt_buffer(n, k); | ||
|
||
// Next, solve with Eigen's SVD solver | ||
Map<Eigen::MatrixX<cx_double>> xopt(xopt_buffer.memptr(), n, k); | ||
if constexpr (transpose_b) { | ||
// Eigen may have an efficient Hermitian transposed solver. Move the transpose step into | ||
// Eigen. | ||
xopt = A.bdcSvd(ComputeThinU | ComputeThinV).solve(b.transpose()); | ||
} else { | ||
xopt = A.bdcSvd(ComputeThinU | ComputeThinV).solve(b); | ||
} | ||
|
||
return xopt_buffer; | ||
} | ||
|
||
template arma::cx_mat solveWithEigen<true>(const arma::mat&, const arma::cx_mat&); | ||
template arma::cx_mat solveWithEigen<false>(const arma::mat&, const arma::cx_mat&); | ||
} // namespace internal | ||
} // namespace microsc_psf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters