Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tau_sampling->fit(gtau, dim) implementation #103

Merged
merged 2 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 106 additions & 92 deletions include/sparseir/sampling.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,44 @@ struct WorkSize
Eigen::Index dimensions() const { return cols; }
};

template <int N>
Eigen::array<int, N> getperm(int src, int dst)
{
Eigen::array<int, N> perm;
if (src == dst) {
for (int i = 0; i < N; ++i) {
perm[i] = i;
}
return perm;
}

int pos = 0;
for (int i = 0; i < N; ++i) {
if (i == dst) {
perm[i] = src;
} else {
// src の位置をスキップ
if (pos == src)
++pos;
perm[i] = pos;
++pos;
}
}
return perm;
}

// movedim: テンソル arr の次元 src を次元 dst
// に移動する(他の次元の順序はそのまま)
template <typename T, int N>
Eigen::Tensor<T, N> movedim(const Eigen::Tensor<T, N> &arr, int src, int dst)
{
if (src == dst) {
return arr;
}
auto perm = getperm<N>(src, dst);
return arr.shuffle(perm);
}

// Forward declarations
template <typename T, int N>
Eigen::MatrixX<T>& ldiv_noalloc(
Expand Down Expand Up @@ -64,111 +102,85 @@ Eigen::MatrixX<T>& ldiv_noalloc_inplace(
const Eigen::JacobiSVD<Eigen::MatrixXd>& A,
const Eigen::MatrixX<T>& B,
Eigen::VectorX<T>& workarr){
/*
function rdiv_noalloc!(Y::AbstractMatrix, A::AbstractMatrix, B::SVD,
workarr) # Setup work space worksize = (size(A, 1), size(B.U, 2)) worklength =
prod(worksize) length(workarr) ≥ worklength ||
throw(DimensionMismatch("size(workarr)=$(size(workarr)), min
worksize=$worklength")) workarr_view = reshape(view(workarr, 1:worklength),
worksize)

# Note: conj creates a temporary matrix
mul!(workarr_view, A, conj(B.U))
workarr_view ./= reshape(B.S, 1, :)
return mul!(Y, workarr_view, conj(B.Vt))
end
*/
WorkSize worksize(A.matrixU().cols(), B.cols());
Eigen::Index worklength = worksize.prod();
size_t worklength = A.matrixU().cols() * B.cols();

if (workarr.size() < worklength) {
throw std::runtime_error("Work array is too small");
throw std::runtime_error("Dimension mismatch: workarr.size()=" + std::to_string(workarr.size()) + ", min worksize=" + std::to_string(worklength));
}
Eigen::Map<Eigen::MatrixX<T>> workarr_view(workarr.data(), worksize.rows, worksize.cols);
Eigen::Map<Eigen::MatrixX<T>> workarr_view(workarr.data(), A.matrixU().cols(), B.cols());
workarr_view = A.matrixU().transpose() * B;
workarr_view.array() /= A.singularValues().array();
for (int i = 0; i < workarr_view.rows(); ++i) {
for (int j = 0; j < workarr_view.cols(); ++j) {
workarr_view(i, j) /= A.singularValues()(i);
}
}

std::cout << "workarr_view.rows(): " << workarr_view.rows() << std::endl;
std::cout << "workarr_view.cols(): " << workarr_view.cols() << std::endl;
std::cout << "A.matrixV().rows(): " << A.matrixV().rows() << std::endl;
std::cout << "A.matrixV().cols(): " << A.matrixV().cols() << std::endl;
Y = A.matrixV() * workarr_view;
return Y;
}

template <typename T, int N>
Eigen::MatrixX<T>& rdiv_noalloc_inplace(
Eigen::MatrixX<T>& Y,
const Eigen::JacobiSVD<Eigen::MatrixXd>& A,
const Eigen::MatrixX<T>& B,
const Eigen::MatrixX<T>& A,
const Eigen::JacobiSVD<Eigen::MatrixXd>& B,
Eigen::VectorX<T>& workarr){
/*
function ldiv_noalloc!(Y::AbstractMatrix, A::SplitSVD, B::AbstractMatrix,
workarr) # Setup work space worksize = (size(A.UrealT, 1), size(B, 2))
worklength = prod(worksize)
length(workarr) ≥ worklength ||
throw(DimensionMismatch("size(workarr)=$(size(workarr)), min
function rdiv_noalloc!(Y::AbstractMatrix, A::AbstractMatrix, B::SVD,
workarr) # Setup work space worksize = (size(A, 1), size(B.U, 2)) worklength =
prod(worksize) length(workarr) ≥ worklength ||
throw(DimensionMismatch("size(workarr)=$(size(workarr)), min
worksize=$worklength")) workarr_view = reshape(view(workarr, 1:worklength),
worksize)

mul!(workarr_view, A.UrealT, real(B))
mul!(workarr_view, A.UimagT, imag(B), true, true)
workarr_view ./= A.S
return mul!(Y, A.V, workarr_view)
# Note: conj creates a temporary matrix
mul!(workarr_view, A, conj(B.U))
workarr_view ./= reshape(B.S, 1, :)
return mul!(Y, workarr_view, conj(B.Vt))
end
*/
WorkSize worksize(A.matrixU().cols(), B.cols());
Eigen::Index worklength = worksize.prod();
*/

size_t worklength = A.rows() * B.matrixU().cols();
if (workarr.size() < worklength) {
throw std::runtime_error("Work array is too small");
}
Eigen::Map<Eigen::MatrixX<T>> workarr_view(workarr.data(), worksize.rows, worksize.cols);
workarr_view = A.matrixU().transpose() * B;
workarr_view.array() /= A.singularValues().array();
Y = A.matrixV() * workarr_view;
return Y;
}

template <int N>
Eigen::array<int, N> getperm(int src, int dst) {
Eigen::array<int, N> perm;
if (src == dst) {
for (int i = 0; i < N; ++i) {
perm[i] = i;
}
return perm;
}

int pos = 0;
for (int i = 0; i < N; ++i) {
if (i == dst) {
perm[i] = src;
} else {
// src の位置をスキップ
if (pos == src)
++pos;
perm[i] = pos;
++pos;
Eigen::Map<Eigen::MatrixX<T>> workarr_view(workarr.data(), A.rows(), B.matrixU().cols());
workarr_view = A * B.matrixU().conjugate();
for (int i = 0; i < workarr_view.rows(); ++i) {
for (int j = 0; j < workarr_view.cols(); ++j) {
workarr_view(i, j) /= B.singularValues()(j);
}
}
return perm;
Y = workarr_view * B.matrixV().conjugate();
return Y;
}


// Helper function to calculate buffer size for tensors
template<typename T, int N>
std::vector<Eigen::Index> calculate_buffer_size(
Eigen::VectorX<Eigen::Index> calculate_buffer_size(
const Eigen::Tensor<T, N>& al,
const Eigen::MatrixXd& matrix,
int dim)
{
std::vector<Eigen::Index> buffer_size;
buffer_size.reserve(N);
Eigen::VectorX<Eigen::Index> buffer_size(N);

// Add dimensions up to dim
for (int i = 0; i < dim; ++i) {
buffer_size.push_back(al.dimension(i));
buffer_size(i) = al.dimension(i);
}

// Add matrix dimension
buffer_size.push_back(matrix.rows());
buffer_size(dim) = matrix.rows();

// Add remaining dimensions
for (int i = dim + 1; i < N; ++i) {
buffer_size.push_back(al.dimension(i));
buffer_size(i) = al.dimension(i);
}

return buffer_size;
Expand Down Expand Up @@ -203,41 +215,45 @@ inline Eigen::Tensor<T, N>& div_noalloc_inplace(
if (dim < 0 || dim >= N) {
throw std::domain_error("Dimension must be in [0, N).");
}

std::cout << "dim: " << dim << std::endl;
if (dim == 0) {
// Create a temporary matrix to hold the non-const data
Eigen::MatrixX<T> temp_arr = Eigen::Map<const Eigen::MatrixX<T>>(
Eigen::MatrixX<T> flatarr = Eigen::Map<const Eigen::MatrixX<T>>(
arr.data(), arr.dimension(0), arr.size() / arr.dimension(0));
Eigen::MatrixX<T> temp_buffer(buffer.dimension(0), buffer.size() / buffer.dimension(0));

ldiv_noalloc_inplace<T, N>(temp_buffer, svd, temp_arr, workarr);
Eigen::MatrixX<T> flatbuffer(buffer.dimension(0), buffer.size() / buffer.dimension(0));
std::cout << "Calling ldiv_noalloc_inplace" << std::endl;
ldiv_noalloc_inplace<T, N>(flatbuffer, svd, flatarr, workarr);

// Copy back to buffer tensor
std::copy(temp_buffer.data(), temp_buffer.data() + temp_buffer.size(), buffer.data());
std::copy(flatbuffer.data(), flatbuffer.data() + flatbuffer.size(), buffer.data());
return buffer;
}
else if (dim != N - 1) {
auto perm = getperm<N>(dim, 0);
Eigen::Tensor<T, N> arr_perm = arr.shuffle(perm).eval();
Eigen::Tensor<T, N> buffer_perm = buffer.shuffle(perm).eval();
Eigen::Tensor<T, N> arr_perm = movedim(arr, dim, 0);
Eigen::Tensor<T, N> buffer_perm = movedim(buffer, dim, 0);

Eigen::MatrixX<T> flatarr = Eigen::Map<const Eigen::MatrixX<T>>(
arr_perm.data(), arr_perm.dimension(0), arr_perm.size() / arr_perm.dimension(0));
Eigen::MatrixX<T> flatbuffer(buffer_perm.dimension(0), buffer_perm.size() / buffer_perm.dimension(0));

// Remove the extra argument '0'
div_noalloc_inplace<T, N>(buffer_perm, svd, arr_perm, workarr, 0);
ldiv_noalloc_inplace<T, N>(flatbuffer, svd, flatarr, workarr);

auto inv_perm = getperm<N>(0, dim);
buffer = buffer_perm.shuffle(inv_perm).eval();
return buffer;
}
else {
// Create temporary matrices for the last dimension case
Eigen::MatrixX<T> temp_arr = Eigen::Map<const Eigen::MatrixX<T>>(
Eigen::MatrixX<T> flatarr = Eigen::Map<const Eigen::MatrixX<T>>(
arr.data(), arr.size() / arr.dimension(N-1), arr.dimension(N-1));
Eigen::MatrixX<T> temp_buffer(buffer.size() / buffer.dimension(N-1), buffer.dimension(N-1));
Eigen::MatrixX<T> flatbuffer(buffer.size() / buffer.dimension(N-1), buffer.dimension(N-1));

rdiv_noalloc_inplace<T, N>(temp_buffer, svd, temp_arr, workarr);
rdiv_noalloc_inplace<T, N>(flatbuffer, flatarr, svd, workarr);

// Copy back to buffer tensor
std::copy(temp_buffer.data(), temp_buffer.data() + temp_buffer.size(), buffer.data());
std::copy(flatbuffer.data(), flatbuffer.data() + flatbuffer.size(), buffer.data());
return buffer;
}
}
Expand Down Expand Up @@ -338,18 +354,6 @@ Eigen::Tensor<T, N>& matop_along_dim(
return buffer;
}

// movedim: テンソル arr の次元 src を次元 dst
// に移動する(他の次元の順序はそのまま)
template <typename T, int N>
Eigen::Tensor<T, N> movedim(const Eigen::Tensor<T, N> &arr, int src, int dst)
{
if (src == dst) {
return arr;
}
auto perm = getperm<N>(src, dst);
return arr.shuffle(perm);
}

template <typename S>
class AbstractSampling {
public:
Expand All @@ -375,6 +379,7 @@ class AbstractSampling {

// Calculate buffer dimensions using the new tensor version
auto buffer_dims = calculate_buffer_size(al, get_matrix(), dim);
std::cout << "buffer_dims: " << buffer_dims << std::endl;

// Convert vector to array for tensor construction
Eigen::array<Eigen::Index, N> dims;
Expand All @@ -391,7 +396,7 @@ class AbstractSampling {
size_t workarrlength(const Eigen::Tensor<T, N> &ax, int dim) const
{
auto svd = get_matrix_svd();
return svd.singularValues().size() * (ax.dimension(dim) / ax.dimension(0));
return svd.singularValues().size() * (ax.size() / ax.dimension(dim));
}

// Fit values at sampling points to basis coefficients
Expand All @@ -409,7 +414,16 @@ class AbstractSampling {
Eigen::array<Eigen::Index, N> dims;
std::copy(buffer_dims.begin(), buffer_dims.end(), dims.begin());
Eigen::Tensor<T, N> buffer(dims);
// std::cout << "buffer.dimensions(): " << buffer.dimensions() << std::endl;
auto svd = get_matrix_svd();
// std::cout << "svd.matrixU().cols(): " << svd.matrixU().cols() << std::endl;
// std::cout << "svd.matrixV().rows(): " << svd.matrixV().rows() << std::endl;
// std::cout << "svd.singularValues().size(): " << svd.singularValues().size() << std::endl;
// std::cout << "ax.dimension(dim): " << ax.dimension(dim) << std::endl;

if (workarr.size() < workarrlength(ax, dim)) {
throw std::runtime_error("Work array is too small");
}
div_noalloc_inplace<T, N>(buffer, svd, ax, workarr, dim);
return buffer;
}
Expand Down
4 changes: 2 additions & 2 deletions test/sampling.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ TEST_CASE("Sampling Tests") {
REQUIRE(gtau.dimension(1) == gl.dimension(1));
REQUIRE(gtau.dimension(2) == gl.dimension(2));
REQUIRE(gtau.dimension(3) == gl.dimension(3));
//Eigen::Tensor<ComplexF64, 4> gl_from_tau = tau_sampling->fit(gtau, dim);
//REQUIRE(gl_from_tau.isApprox(originalgl, 1e-10));
Eigen::Tensor<ComplexF64, 4> gl_from_tau = tau_sampling->fit(gtau, dim);
//REQUIRE(sparseir::tensorIsApprox(gl_from_tau, originalgl, 1e-10));
}

// Test evaluate and fit
Expand Down