-
-
Notifications
You must be signed in to change notification settings - Fork 10
Review request -- Mathieu fcns #60
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
base: main
Are you sure you want to change the base?
Conversation
I'm afraid I can't help with C++ expertise, but I wonder whether you could open a draft PR to SciPy which shows the tests running (albeit with failures)? No rush, just thought it might be worth it whilst you are waiting for review here and have some momentum. Let me know if you would like help with making the necessary changes to the SciPy submodule etc. |
Thank you for your suggestion, @lucascolley. I will do that. Meanwhile, I see the CI thing is running on my PR and I have build failures. I fixed one -- I was using the suffix 'd' when declaring double literals which turns out to be non-standard. I fixed that one and pushed to my forked repo. Not sure if CI runs continuously but I will continue fixing any bugs it finds. Stuart |
It is a GitHub limitation that since this is your first PR, CI runs require approval from someone with write access on the repo. |
OK, I just submitted a PR on the main Scipy branch. The pull request includes:
I also submitted a new PR on the xsf repo to pick up a bugfix I implemented after looking at some of my last CI test failures: I had suffixed some double literals using 'd', like this: 2.0d. This is apparently not standard C++ so made some of the compilers barf. My mistake -- I fixed it and issued a new PR to you. Sorry for the multiple PRs -- I'm new to the Scipy dev chain and so will fumble around a little bit as I try to get this change into the codebase. Stuart |
Thanks @brorson. I plan to spend some time in the next few weeks to improve testing, and we can add some test cases based on references from https://github.com/mathieuandspheroidalwavefunctions computed with quad precision. In the meantime, your |
The usual workaround is to open a separate PR fixing a typo or other such trivial thing somewhere in the repo; once that's merged, CI will run here. |
Thank you, @steppi. Regarding the tests in main.c, sure I can move them to a file called "test_mathieu_fcns.cpp" and move them to xsf/tests/miscellaneous_tests/. Give me a day or two. Once I do that I will check the mods into my GitHub repo and issue another PR so you know they're there. |
@steppi, per your comments I moved the tests from main.c to a file xsf/tests/scipy_special_tests/test_mathieu.cpp in my local directory. But now I am looking at the other cpp files in that dir. It's clear that I need to do some homework and understand the formatting of the test files and how they are processed and then executed. Meanwhile, can I ask you to point me to some test files which are close to my use-case? My near-term goal is to just have a file which is picked up by the CI thing, runs some functions which test against golden values hard-coded into the file, then sets the right variables to tell the CI thing about passes/failures. Is there a particular file or files I can look at as an example? Thanks, |
Thanks. I should have some time tomorrow to write a quick template for how the test file could be written and post it here |
main.c to test_mathieu.cpp in the tests dir. I ran clang-format on all .h files.
Thank you. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks pretty impressive!
I'm by no means an expert on the underlying maths or C++, but I left some (superficial) comments anyway; I hope you don't mind. I wasn't able to look at everything though, given the size and all.
One thing general pattern I've seen, is that there's quite a bit of code duplication. For reviewers that's not a lot of fun. So if you want to help speed up the review process, we'd appreciate it if you could try do something about the duplication. And there's no need to go wild with macro's; a couple of helper functions here and there could already help a lot.
I've also seen a several malloc
and free
calls. But since we're in C++ land, I don't think we need those. There's even the fancy std::mdspan
you could use for vector- and matrix stuff, since we bundle a backport of it in kokkos/mdspan.hpp
, which I imagine could be very useful here.
* | ||
* Even parity characteristic values a. | ||
* | ||
* @param m Eigenvalue order. Must be positive integer less than 500. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should probably be an unsigned short or something then. I don't think that backwards compatibility is much of a concern here, but I might be wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Judging by the if ((m < 0) || (m != floor(m)))
, it looks like m
is allowed to be zero:
* @param m Eigenvalue order. Must be positive integer less than 500. | |
* @param m Eigenvalue order. Must be non-negative integer less than 500. |
template <typename T> | ||
T cem_cva(T m, T q) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since it internally casts to- and from double
, I don't think that there's any advantage to having it generic like this
include/xsf/mathieu/besseljyd.h
Outdated
//================================================================== | ||
double besselj(int k, double z) { | ||
// This is just a thin wrapper around the Bessel impl in the | ||
// std library. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// std library. | |
// xsf library. |
* | ||
*/ | ||
|
||
#define SQRT2 1.414213562373095 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a M_SQRT2
in config.h
you could use instead
/*----------------------------------------------- | ||
This creates the recurrence relation matrix for | ||
the even-even Mathieu fcns (ce_2n). | ||
Inputs: | ||
N = matrix size (related to max order desired). | ||
q = shape parameter. | ||
Output: | ||
A = recurrence matrix (must be calloc'ed in caller). | ||
Return: | ||
return code = 0 if OK. | ||
-------------------------------------------------*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: I think it would a bit easier to read if it would be a bit wider, so that there aren't as many linebreaks needed.
include/xsf/mathieu/mathieu_coeffs.h
Outdated
// Bail out if m is not even. | ||
if (m % 2 != 0) | ||
return -1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should probably fail loudly
include/xsf/mathieu/mathieu_coeffs.h
Outdated
return -1; | ||
|
||
// Allocate recursion matrix | ||
double *A = (double *)calloc(N * N, sizeof(double)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's probably some smart C++ thingy that could be used that doesn't require the manual free
ing. A std::vector
perhaps.
*a = std::numeric_limits<double>::quiet_NaN(); | ||
return SF_ERROR_DOMAIN; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's usually better to fail loudly in case of invalid input
include/xsf/mathieu/mathieu_fcns.h
Outdated
* This is part of the Mathieu function suite -- a reimplementation | ||
* of the Mathieu functions for Scipy. This file holds the function | ||
* implementations themselves. The prototype was written in Matlab | ||
* and validated. This is a translation from Matlab to C. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* and validated. This is a translation from Matlab to C. | |
* and validated. This is a translation from Matlab to C++. |
😉
namespace mathieu { | ||
|
||
//================================================================== | ||
int mathieu_ce(int m, double q, double v, double *ce, double *ced) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
xsf uses pass-by-reference in case of multiple scalar outputs
Thank you for the suggestions, @jorenham . I will go down the list and fix up my code based on your comments. Regarding code duplication, yes I thought of using macros for the repeated stuff but since the repeated stuff is multi-line I figured any macro would be multi-line and hard to manage. However, I'll think about how to use macros again based on your comments, and may also see if I can stick some of the stuff into a separate inline fcn. I will want to comment heavily any macro or fcn to make clear what the calc is trying to do. Regarding malloc() & free(), yeah, I am more of a C programmer than a C++ programmer. Does Scipy allow use of smart pointers? I see auto_ptr has been removed from C++ ... is there a preferred alternative? (I will browse the codebase to see what is used, but it's always easier to ask ...) Regarding failing loudly, do you mean throw an exception? I may be wrong, but since the xsf stuff supplies return codes I thought that was the preferred way to deal with errors. Please correct me if wrong. Thx, |
Regarding code structure, mathieu.h living in the xsf directory provides the interface to the rest of Scipy. It is just a wrapper around my impls. My impls live in the mathieu subdirectory. Passing error codes within my stuff I have handled by just using integer flags, but I should fix that to conform to Scipy's error codes. Error codes passed back to Scipy from the interface fcns in mathieu.h are currently done using Scipy/xsf's predefined codes in error.h. I made a guess about what the different errors in the enum mean ... I hope I was close enough to be correct. |
Regarding status of this project, I am currently bogged down with teaching and other responsibilities but hope to get back to this in a couple of weeks. Besides some clean-up per your suggestions (and my own list of things to clean up) I need to follow @rgommers suggestion about how to create a pull request which makes changes to two repos -- the main Scipy repo (where the Meson build config lives) and the xsf repo. I need to declare some dependencies in the build config stuff. That's waiting for me to do. |
The cases I've seen could be replaced with a
Anything user-facing that receives incorrect input returns NaN, generally speaking. |
This is a request to try the new Mathieu fcn impl. This new fcn impl has been discussed in #47 . I have updated the docstrings and other comment in this checkin, but the functionality has otherwise remained the same as that shown in the bug report.
I have run these fcns against the Scipy test suite using "spin test" and the test suite runs to completion. The Mathieu fcns fail, but that is expected since this is a new impl and the input arg is in radians, not degrees.
Please review and comment. My goal here is to request a review and find out what I need to change.
Stuart