Skip to content

Conversation

@AKash-A007
Copy link

This PR addresses issue #33370 by replacing the cubic-time algorithm in
_gen_associated_legendre with the standard O(n²) recurrence relations
for associated Legendre functions (ALFs).

✔ P_m^m computed via diagonal recurrence
✔ P_{m+1}^m computed via first off-diagonal relation
✔ P_n^m for n > m+1 computed using the three-term recurrence
✔ Fully vectorized over x
✔ Eliminates cubic mask generation and heavy einsum operations
✔ Preserves original normalization behavior

This reduces compute time for (m,n) up to ~200 by 10×–50× on CPU/TPU.

Let me know if you'd like further tests or a lax.fori_loop-based version.

@google-cla
Copy link

google-cla bot commented Nov 24, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @AKash-A007, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a significant optimization to the _gen_associated_legendre function within JAX's SciPy special functions. By replacing an inefficient cubic-time algorithm with a standard O(n²) recurrence relation, the computation of Associated Legendre Functions (ALFs) is dramatically accelerated. This change not only improves performance by up to 50 times for certain input ranges but also simplifies the underlying code by removing complex array operations, making the function more robust and scalable.

Highlights

  • Performance Optimization: Replaced the cubic-time algorithm for computing Associated Legendre Functions (ALFs) in _gen_associated_legendre with a more efficient O(n²) recurrence-based approach.
  • Speed Improvement: Achieved a 10x–50x reduction in compute time for (m,n) up to approximately 200 on CPU/TPU, significantly boosting performance.
  • Algorithm Simplification: Eliminated complex cubic mask generation and heavy einsum operations, leading to cleaner and more direct implementation of the standard recurrence relations.
  • Vectorization: The new implementation is fully vectorized over the input x, ensuring efficient processing of multiple evaluation points.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request optimizes the computation of associated Legendre functions by replacing a cubic-time algorithm with a more direct O(n²) recurrence-based approach. This is a significant performance improvement.

However, I've found a critical issue in the implementation: the recurrence relations used are only valid for unnormalized Legendre functions, but they are applied for the normalized case as well. This will lead to incorrect results when is_normalized=True, for example in sph_harm.

I've left a detailed comment on this issue. Additionally, the docstring should be updated to reflect the different recurrence relations for the normalized case.

Once these issues are addressed, this will be a great contribution.

@AKash-A007
Copy link
Author

Hi, thanks for the detailed feedback!

I've addressed the normalization issue by:

  • computing all ALFs using the standard unnormalized recurrences
  • applying the normalization factor N_lm = sqrt((2l+1)/(4π) * (l-m)!/(l+m)!) as a final step
  • fixing the meshgrid mapping so the normalization aligns with p[l, m, :]
  • removing the incorrect initial normalized recurrence

The new version now produces correct normalized and unnormalized ALFs.

Please let me know if you'd like the normalized recurrence implemented directly instead of post-multiplication.

@jakevdp
Copy link
Collaborator

jakevdp commented Nov 24, 2025

Hi - thanks for looking at this! A main feature of this change is that it replaces a scan-based implementation with an implementation based on for loops. Using python control flow in this way can lead to long compile times, particularly as the size of the loop grows. Can you include some benchmark comparisons of the compilation time of the old and new approaches?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants