Skip to content

Conversation

@MialLewis
Copy link
Contributor

@MialLewis MialLewis commented Nov 13, 2025

Description of work

There were several points in the code where EigenMatrix/EigenVector objects were converted into gsl objects to use in gsl functions. This PR removes these cases, swapping out the gsl functions for eigen implementations.

Have enabled the system test which ref file I changed on windows and Mac, since the tests pass locally for me on Mac.

Here's a windows test: https://builds.mantidproject.org/job/build_branch/1446/

To test:

  • Unit tests
  • Try out the relevant fitting functions if you want?

Reviewer

Your comments will be used as part of the gatekeeper process. Comment clearly on what you have checked and tested during your review. Provide an audit trail for any changes requested.

As per the review guidelines:

  • Is the code of an acceptable quality? (Code standards/GUI standards)
  • Has a thorough functional test been performed? Do the changes handle unexpected input/situations?
  • Are appropriately scoped unit and/or system tests provided?
  • Do the release notes conform to the guidelines and describe the changes appropriately?
  • Has the relevant (user and developer) documentation been added/updated?
  • If the PR author isn’t in the mantid-developers or mantid-contributors teams, add a review comment rerun ci to authorize/rerun the CI

Gatekeeper

As per the gatekeeping guidelines:

  • Has a thorough first line review been conducted, including functional testing?
  • At a high-level, is the code quality sufficient?
  • Are the base, milestone and labels correct?

@MialLewis MialLewis added the ISIS: Core Issue and pull requests at ISIS that relate to Core features and functions label Nov 13, 2025
@MialLewis MialLewis moved this from Todo to In Progress in ISIS core workstream v6.15.0 Nov 13, 2025
@MialLewis MialLewis marked this pull request as ready for review November 13, 2025 21:27
@sf1919 sf1919 moved this from In Progress to Waiting for Review in ISIS core workstream v6.15.0 Nov 14, 2025
@MialLewis MialLewis moved this from Waiting for Review to In Progress in ISIS core workstream v6.15.0 Nov 18, 2025
@sf1919 sf1919 added this to the Release 6.15 milestone Nov 18, 2025
Copy link
Contributor

@rboston628 rboston628 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This simplifies a lot. I would just like better docs for the new method.

Comment on lines 218 to 239
// Replicates equivalent gsl function
Eigen::MatrixXd covar_from_jacobian(const map_type &J, double epsrel, const map_type &r) {
const Eigen::Index m = J.rows(), n = J.cols();
const int dof = std::max<int>(1, static_cast<int>(m - n)); // avoid div by 0 for safety
const double s2 = r.squaredNorm() / dof; // residual variance

Eigen::JacobiSVD<Eigen::MatrixXd> svd(J, Eigen::ComputeThinU | Eigen::ComputeThinV);
const Eigen::VectorXd &s = svd.singularValues(); // length = min(m,n)
const Eigen::MatrixXd &V = svd.matrixV(); // n x n (thin: n x r)

// GSL-style relative threshold on singular values
const double smax = s.size() ? s.array().maxCoeff() : 0.0;
const double tol = epsrel * smax;

// Build diag(1/s_i^2) with cutoff
Eigen::VectorXd inv_s2 = s.unaryExpr([&](double si) { return (si > tol) ? 1.0 / (si * si) : 0.0; });

// Covariance = s2 * V * diag(1/s_i^2) * V^T
Eigen::MatrixXd C = V * inv_s2.asDiagonal() * V.transpose();
C *= s2;
return C;
}
Copy link
Contributor

@rboston628 rboston628 Nov 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the GSL documentation there is no entry for gsl_multifit_covar. I did find some some documentation here, but this is a much simpler method than you have. It seems like you're following steps in GSL's overview of linear least-squares fitting.

Could you add a link to that section, and then add clarifying comments for s, s2, smax, V, etc., and also how they relate tot he math operations there?

Copy link
Contributor Author

@MialLewis MialLewis Nov 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, I've moved this to draft as I wasn't able to get this working 100% as gsl_multifit_covar did. I'm planning to pick this back up later in the sprint once our "Must" issues are out of the way.

I was adding tests as I realised this!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @rboston628, I've reworked the new function to match up with the docs (with the help of AI, as this is a bit beyond my limited liner algebra ability). I've gone through it and added comments to document; I think it's correct.

If you could take another look it'd be much appreciated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PG3StripPeaksDiff

The difference between reference files are very minimal as per the figure above; I'm going to adjust the results for this one.

@MialLewis MialLewis marked this pull request as draft November 24, 2025 17:19
@MialLewis MialLewis force-pushed the 0_remove_eigen_to_gsl_conversions branch from 25a5191 to f9d4c88 Compare January 7, 2026 12:49
@MialLewis MialLewis marked this pull request as ready for review January 7, 2026 16:55
@MialLewis MialLewis requested a review from rboston628 January 7, 2026 16:56
@MialLewis MialLewis moved this from In Progress to In Review in ISIS core workstream v6.15.0 Jan 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ISIS: Core Issue and pull requests at ISIS that relate to Core features and functions

Projects

Status: In Review

Development

Successfully merging this pull request may close these issues.

4 participants