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

adding assoc_legendre: legenp in mpmath_translation in lambdify.py #26117

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

harshkasat
Copy link
Contributor

@harshkasat harshkasat commented Jan 24, 2024

References to other Issues or PRs

Partial fix for: #15795

Brief description of what is fixed or changed

Other comments

Release Notes

  • core
    • Added "assoc_legendre" ("legenp") support in lambdify.py mpmath translation. Included a required test case for robust functionality.

@sympy-bot
Copy link

sympy-bot commented Jan 24, 2024

Hi, I am the SymPy bot. I'm here to help you write a release notes entry. Please read the guide on how to write release notes.

Your release notes are in good order.

Here is what the release notes will look like:

  • core
    • Added "assoc_legendre" ("legenp") support in lambdify.py mpmath translation. Included a required test case for robust functionality. (#26117 by @harshkasat)

This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.13.

Click here to see the pull request description that was parsed.
<!-- Your title above should be a short description of what
was changed. Do not include the issue number in the title. -->

#### References to other Issues or PRs
<!-- If this pull request fixes an issue, write "Fixes #NNNN" in that exact
format, e.g. "Fixes #1234" (see
https://tinyurl.com/auto-closing for more information). Also, please
write a comment on that issue linking back to this pull request once it is
open. -->

Partial fix for: #15795 

#### Brief description of what is fixed or changed


#### Other comments


#### Release Notes

<!-- Write the release notes for this release below between the BEGIN and END
statements. The basic format is a bulleted list with the name of the subpackage
and the release note for this PR. For example:

* solvers
  * Added a new solver for logarithmic equations.

* functions
  * Fixed a bug with log of integers. Formerly, `log(-x)` incorrectly gave `-log(x)`.

* physics.units
  * Corrected a semantical error in the conversion between volt and statvolt which
    reported the volt as being larger than the statvolt.

or if no release note(s) should be included use:

NO ENTRY

See https://github.com/sympy/sympy/wiki/Writing-Release-Notes for more
information on how to write release notes. The bot will check your release
notes automatically to see if they are formatted correctly. -->

<!-- BEGIN RELEASE NOTES -->
* core
  * Added "assoc_legendre" ("legenp") support in lambdify.py mpmath translation. Included a required test case for robust functionality.
<!-- END RELEASE NOTES -->

@oscarbenjamin
Copy link
Collaborator

Note my comment here #15795 (comment)

Anyone looking to open such a PR should check that the definition SymPy's assoc_legendre matches with mpmath's legenp. I have only done a simple test:

The documented definitions should be compared and more values (including negative, non-real etc) should be checked.

@harshkasat
Copy link
Contributor Author

  • The definition of Sympy and mpmath are mostly likely same.
  • The different name use in
    • scipy :- scipy.special.legendre
    • numpy:- numpy.polynomial.legendre.Legendre

Copy link

github-actions bot commented Feb 4, 2024

Benchmark results from GitHub Actions

Lower numbers are good, higher numbers are bad. A ratio less than 1
means a speed up and greater than 1 means a slowdown. Green lines
beginning with + are slowdowns (the PR is slower then master or
master is slower than the previous release). Red lines beginning
with - are speedups.

Significantly changed benchmark results (PR vs master)

Significantly changed benchmark results (master vs previous release)

| Change   | Before [a00718ba]    | After [8824e3ad]    |   Ratio | Benchmark (Parameter)                                                |
|----------|----------------------|---------------------|---------|----------------------------------------------------------------------|
| -        | 70.3±1ms             | 45.3±1ms            |    0.64 | integrate.TimeIntegrationRisch02.time_doit(10)                       |
| -        | 68.2±0.7ms           | 43.7±0.5ms          |    0.64 | integrate.TimeIntegrationRisch02.time_doit_risch(10)                 |
| +        | 18.4±0.1μs           | 30.4±0.1μs          |    1.65 | integrate.TimeIntegrationRisch03.time_doit(1)                        |
| -        | 5.42±0.06ms          | 2.86±0.03ms         |    0.53 | logic.LogicSuite.time_load_file                                      |
| -        | 73.0±1ms             | 29.3±0.1ms          |    0.4  | polys.TimeGCD_GaussInt.time_op(1, 'dense')                           |
| -        | 25.9±0.2ms           | 16.9±0.1ms          |    0.65 | polys.TimeGCD_GaussInt.time_op(1, 'expr')                            |
| -        | 74.2±0.5ms           | 29.0±0.2ms          |    0.39 | polys.TimeGCD_GaussInt.time_op(1, 'sparse')                          |
| -        | 255±1ms              | 126±0.4ms           |    0.49 | polys.TimeGCD_GaussInt.time_op(2, 'dense')                           |
| -        | 256±1ms              | 126±1ms             |    0.49 | polys.TimeGCD_GaussInt.time_op(2, 'sparse')                          |
| -        | 660±3ms              | 372±2ms             |    0.56 | polys.TimeGCD_GaussInt.time_op(3, 'dense')                           |
| -        | 655±6ms              | 371±0.9ms           |    0.57 | polys.TimeGCD_GaussInt.time_op(3, 'sparse')                          |
| -        | 500±10μs             | 286±1μs             |    0.57 | polys.TimeGCD_LinearDenseQuadraticGCD.time_op(1, 'dense')            |
| -        | 1.78±0.01ms          | 1.04±0.01ms         |    0.58 | polys.TimeGCD_LinearDenseQuadraticGCD.time_op(2, 'dense')            |
| -        | 5.84±0.03ms          | 3.05±0.03ms         |    0.52 | polys.TimeGCD_LinearDenseQuadraticGCD.time_op(3, 'dense')            |
| -        | 445±4μs              | 232±2μs             |    0.52 | polys.TimeGCD_QuadraticNonMonicGCD.time_op(1, 'dense')               |
| -        | 1.46±0.01ms          | 672±8μs             |    0.46 | polys.TimeGCD_QuadraticNonMonicGCD.time_op(2, 'dense')               |
| -        | 4.87±0.01ms          | 1.65±0.01ms         |    0.34 | polys.TimeGCD_QuadraticNonMonicGCD.time_op(3, 'dense')               |
| -        | 376±2μs              | 206±0.9μs           |    0.55 | polys.TimeGCD_SparseGCDHighDegree.time_op(1, 'dense')                |
| -        | 2.45±0.01ms          | 1.24±0.01ms         |    0.51 | polys.TimeGCD_SparseGCDHighDegree.time_op(3, 'dense')                |
| -        | 10.0±0.05ms          | 4.33±0.04ms         |    0.43 | polys.TimeGCD_SparseGCDHighDegree.time_op(5, 'dense')                |
| -        | 360±6μs              | 168±1μs             |    0.47 | polys.TimeGCD_SparseNonMonicQuadratic.time_op(1, 'dense')            |
| -        | 2.49±0.01ms          | 888±10μs            |    0.36 | polys.TimeGCD_SparseNonMonicQuadratic.time_op(3, 'dense')            |
| -        | 9.49±0.04ms          | 2.67±0.01ms         |    0.28 | polys.TimeGCD_SparseNonMonicQuadratic.time_op(5, 'dense')            |
| -        | 1.03±0.01ms          | 428±3μs             |    0.41 | polys.TimePREM_LinearDenseQuadraticGCD.time_op(3, 'dense')           |
| -        | 1.76±0.03ms          | 499±1μs             |    0.28 | polys.TimePREM_LinearDenseQuadraticGCD.time_op(3, 'sparse')          |
| -        | 6.05±0.2ms           | 1.79±0.02ms         |    0.3  | polys.TimePREM_LinearDenseQuadraticGCD.time_op(5, 'dense')           |
| -        | 8.49±0.07ms          | 1.49±0ms            |    0.18 | polys.TimePREM_LinearDenseQuadraticGCD.time_op(5, 'sparse')          |
| -        | 285±1μs              | 64.5±0.6μs          |    0.23 | polys.TimePREM_QuadraticNonMonicGCD.time_op(1, 'sparse')             |
| -        | 3.46±0.03ms          | 394±2μs             |    0.11 | polys.TimePREM_QuadraticNonMonicGCD.time_op(3, 'dense')              |
| -        | 4.05±0.01ms          | 281±2μs             |    0.07 | polys.TimePREM_QuadraticNonMonicGCD.time_op(3, 'sparse')             |
| -        | 7.02±0.08ms          | 1.26±0ms            |    0.18 | polys.TimePREM_QuadraticNonMonicGCD.time_op(5, 'dense')              |
| -        | 8.66±0.05ms          | 832±9μs             |    0.1  | polys.TimePREM_QuadraticNonMonicGCD.time_op(5, 'sparse')             |
| -        | 5.00±0.02ms          | 2.99±0.02ms         |    0.6  | polys.TimeSUBRESULTANTS_LinearDenseQuadraticGCD.time_op(2, 'sparse') |
| -        | 12.3±0.2ms           | 6.47±0.02ms         |    0.53 | polys.TimeSUBRESULTANTS_LinearDenseQuadraticGCD.time_op(3, 'dense')  |
| -        | 22.3±0.1ms           | 9.04±0.03ms         |    0.4  | polys.TimeSUBRESULTANTS_LinearDenseQuadraticGCD.time_op(3, 'sparse') |
| -        | 5.20±0.02ms          | 873±4μs             |    0.17 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(1, 'sparse')    |
| -        | 12.8±0.1ms           | 7.07±0.04ms         |    0.55 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(2, 'sparse')    |
| -        | 104±0.5ms            | 25.6±0.1ms          |    0.25 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(3, 'dense')     |
| -        | 166±0.6ms            | 53.7±0.2ms          |    0.32 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(3, 'sparse')    |
| -        | 174±0.9μs            | 113±0.8μs           |    0.65 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(1, 'dense')      |
| -        | 358±0.5μs            | 215±1μs             |    0.6  | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(1, 'sparse')     |
| -        | 4.35±0.08ms          | 861±3μs             |    0.2  | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(3, 'dense')      |
| -        | 5.36±0.03ms          | 382±3μs             |    0.07 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(3, 'sparse')     |
| -        | 20.2±0.09ms          | 2.75±0.01ms         |    0.14 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(5, 'dense')      |
| -        | 23.0±0.5ms           | 625±1μs             |    0.03 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(5, 'sparse')     |
| -        | 480±5μs              | 135±1μs             |    0.28 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(1, 'sparse') |
| -        | 4.75±0.01ms          | 619±1μs             |    0.13 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(3, 'dense')  |
| -        | 5.29±0.04ms          | 139±0.7μs           |    0.03 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(3, 'sparse') |
| -        | 13.2±0.08ms          | 1.29±0ms            |    0.1  | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(5, 'dense')  |
| -        | 13.7±0.08ms          | 142±0.4μs           |    0.01 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(5, 'sparse') |
| -        | 133±1μs              | 75.6±0.6μs          |    0.57 | solve.TimeMatrixOperations.time_rref(3, 0)                           |
| -        | 247±0.3μs            | 89.3±0.4μs          |    0.36 | solve.TimeMatrixOperations.time_rref(4, 0)                           |
| -        | 23.9±0.1ms           | 10.2±0.04ms         |    0.43 | solve.TimeSolveLinSys189x49.time_solve_lin_sys                       |
| -        | 28.5±0.3ms           | 15.5±0.2ms          |    0.54 | solve.TimeSparseSystem.time_linsolve_Aaug(20)                        |
| -        | 55.1±0.4ms           | 25.1±0.2ms          |    0.46 | solve.TimeSparseSystem.time_linsolve_Aaug(30)                        |
| -        | 28.1±0.2ms           | 15.3±0.07ms         |    0.54 | solve.TimeSparseSystem.time_linsolve_Ab(20)                          |
| -        | 54.5±0.2ms           | 24.7±0.1ms          |    0.45 | solve.TimeSparseSystem.time_linsolve_Ab(30)                          |

Full benchmark results can be found as artifacts in GitHub Actions
(click on checks at the top of the PR).

@oscarbenjamin
Copy link
Collaborator

  • The definition of Sympy and mpmath are mostly likely same.

What does "most likely" mean?

It is fine if you do not know how to check this but then just say so clearly. If you haven't confirmed this then someone else will need to or otherwise this cannot be merged.

@harshkasat
Copy link
Contributor Author

harshkasat commented Feb 5, 2024

Dear @oscarbenjamin, please accept my sincere apologies for my previous behaviour. In the future, I will ensure that my messages are clear, concise, and respectful at all times. I understand that my previous conduct was unbecoming of the open-source community.

About associate legendre from sympy and mpmath:

Sympy:-

  • The assoc_legendre function calculates the associated Legendre polynomial of degree n and order m for a given variable x. It returns a polynomial /numerical expression.

Mpmath:-

  • The mpmath.legenp function calculates the (associated) Legendre function of the first kind of degree n and order m for a given complex number z. It returns a numerical value.

  • In summary while there may be variations in the specific details of the implementation, the underlying mathematical concept of associated Legendre functions is consistent between SymPy and mpmath.

About associate legendre name from other library are :

Scipy:-

  • scipy.special.legendre(n, monic=False)
from scipy.special import legendre
legendre(3)
poly1d([ 2.5,  0. , -1.5,  0. ])

Mpmath:-

  • mpmath.legenp(n, m, z, type=2)
from mpmath import legenp
legenp(2, 0, 10)

Numpy

  • numpy.polynomial.legendre.Legendre(coef, domain=None, window=None, symbol='x')

  • If I have missed anything, please update me. And if I have made any mistakes, please let me know. Sorry for Incovince.

P.S

  • Based on the observation mentioned above, I have concluded that mpmath.legenp and sympy.functions.special.polynomials.assoc_legendre have the same definition.

@harshkasat
Copy link
Contributor Author

I tried to add a test case with a pull request, but I keep getting merge conflicts. I even pulled master into my branch, but I still got merge conflicts. Can you please guide me?

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

Successfully merging this pull request may close these issues.

None yet

3 participants