Skip to content

Use linear_eq_to_matrix when forming the Lagrange equations where appropriate#29830

Open
moorepants wants to merge 30 commits into
sympy:masterfrom
moorepants:lagrange-efficiency
Open

Use linear_eq_to_matrix when forming the Lagrange equations where appropriate#29830
moorepants wants to merge 30 commits into
sympy:masterfrom
moorepants:lagrange-efficiency

Conversation

@moorepants

@moorepants moorepants commented Jun 4, 2026

Copy link
Copy Markdown
Member

References to other Issues or PRs

#29815 should be merged before this PR

Brief description of what is fixed or changed

  • Switched two instances of .jacobian to linear_eq_to_matrix in forming Lagranges equations.
  • Removed one unnecessary call to .jacobian (due to product rule).
  • Improved the docstrings of the equations of motion attributes by adding equation explanations and using common terminology.
  • I also discovered a likely bug that negatives the Lagrange multipliers. I'll leave this for now and address that in another PR.

Other comments

AI Generation Disclosure

NO AI USE

Release Notes

  • physics.mechanics
    • Improved the computational efficiency of forming the equations of motion in LagrangesMethod.
  • polys
    • Improve computational efficiency of zero terms in _lin_eq2dict and thus linear_eq_to_matrix.

moorepants added 27 commits May 30, 2026 21:55
… be more uniform and added some new abstract base class attributes.
…anesMethod and JointsMethod. Adds new nonholonomic_constraints parameter to KanesMethod.
…e appropriate and improve the docstrings of the returned equations of motion.
@sympy-bot

sympy-bot commented Jun 4, 2026

Copy link
Copy Markdown

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:

  • physics.mechanics

    • Improved the computational efficiency of forming the equations of motion in LagrangesMethod. (#29830 by @moorepants)
  • polys

    • Improve computational efficiency of zero terms in _lin_eq2dict and thus linear_eq_to_matrix. (#29830 by @moorepants)

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

Click here to see the pull request description that was parsed.
<!-- DO NOT DELETE OR REPLACE THIS TEMPLATE or the PR will be closed.

Read our Policy on AI Generated Code and Communication at
https://docs.sympy.org/dev/contributing/ai-generated-code-policy.html.

As required in the policy do not use AI-generated text to complete the PR
description below or the PR will be closed. Follow the instructions in the
template below and keep all section headings or the PR will be closed.

The PR 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

#29815 should be merged before this PR

<!-- If there is an issue related to this PR, include a link to the issue here.
It is important not to waste reviewer's time by skipping this section.

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).

If this does not completely fix the issue, then write "See #NNNN" or "partially
fixes #NNNN", e.g. "See #1234" or "partially fixes #1234". -->


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

- Switched two instances of .jacobian to linear_eq_to_matrix in forming Lagranges equations.
- Removed one unnecessary call to .jacobian (due to product rule).
- Improved the docstrings of the equations of motion attributes by adding equation explanations and using common terminology.
- I also discovered a likely bug that negatives the Lagrange multipliers. I'll leave this for now and address that in another PR.

#### Other comments


#### AI Generation Disclosure

<!-- If this pull request includes AI-generated code or text, please disclose
the tool used and specify which lines were generated. Disclosure is not
required for minor assistive tasks, such as spell-checking or code reviewing,
in primarily human-authored work. Otherwise, write "NO AI USE" in the text area
below.

DO NOT just delete this AI section of the PR template and do not leave this
blank, or the PR will be closed. If you write "NO AI USE" and the code looks
like it was generated by AI then the PR will be closed. -->

NO AI USE

#### 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 -->

* physics.mechanics
  * Improved the computational efficiency of forming the equations of motion in `LagrangesMethod`.
* polys
  * Improve computational efficiency of zero terms in _lin_eq2dict and thus linear_eq_to_matrix.

<!-- END RELEASE NOTES -->

@sympy-bot

sympy-bot commented Jun 4, 2026

Copy link
Copy Markdown

🟠

Hi, I am the SymPy bot. I've noticed that some of your commits add or delete files. Since this is sometimes done unintentionally, I wanted to alert you about it.

This is an experimental feature of SymPy Bot. If you have any feedback on it, please comment at sympy/sympy-bot#75.

The following commits add new files:

  • bcc9113:
    • doc/src/modules/physics/mechanics/api/method.rst

If these files were added/deleted on purpose, you can ignore this message.

@oscarbenjamin

Copy link
Copy Markdown
Collaborator

I don't know if you have done any timings but note the diff I suggested here to speed up linear_eq_to_matrix: #29705 (comment).

@moorepants

Copy link
Copy Markdown
Member Author

Thanks for the reminder. I've included it here.

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.

3 participants