sympy icon indicating copy to clipboard operation
sympy copied to clipboard

Introduce Option for Custom Jacobian Function in physics.mechanics Linearizer

Open ricdigi opened this issue 1 year ago • 5 comments
trafficstars

References to Other Issues or PRs

This PR is based on the modifications introduced in PR #26773.

Brief Description of What Is Fixed or Changed

This PR introduces an enhancement to the Linearizer class by allowing users to specify a custom function for Jacobian computation. Previously, the Jacobian was always computed using matrixbase's jacobian() method. With this update, users can now pass their own Jacobian function when calling the to_linearizer() method from Kane's or Lagrange's methods, providing greater flexibility and customization in the linearization process. By default the methods use the function _forward_jacobian().

Other Comments

I have doubts on the backward-compatibility as the jacobian() method is not anymore the default method used in the lineariser.

Release Notes

  • physics.mechanics
    • Added the ability to specify a custom Jacobian function in the Linearizer class.
    • Enhanced the to_linearizer() method to accept a custom Jacobian function when invoked from Kane's or Lagrange's methods.

ricdigi avatar Aug 12 '24 13:08 ricdigi

:white_check_mark:

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
    • Added the ability to specify a custom Jacobian function in the Linearizer class. (#26952 by @brocksam and @ricdigi)

    • Enhanced the to_linearizer() method to accept a custom Jacobian function when invoked from Kane's or Lagrange's methods. (#26952 by @brocksam and @ricdigi)

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

Click here to see the pull request description that was parsed.
#### References to Other Issues or PRs
This PR is based on the modifications introduced in PR #26773. 

#### Brief Description of What Is Fixed or Changed

This PR introduces an enhancement to the `Linearizer` class by allowing users to specify a custom function for Jacobian computation. Previously, the Jacobian was always computed using matrixbase's jacobian() method. With this update, users can now pass their own Jacobian function when calling the `to_linearizer()` method from Kane's or Lagrange's methods, providing greater flexibility and customization in the linearization process. By default the methods use the function _forward_jacobian().

#### Other Comments

I have doubts on the backward-compatibility as the jacobian() method is not anymore the default method used in the lineariser.

#### Release Notes

<!-- BEGIN RELEASE NOTES -->
* physics.mechanics
  * Added the ability to specify a custom Jacobian function in the `Linearizer` class.
  * Enhanced the `to_linearizer()` method to accept a custom Jacobian function when invoked from Kane's or Lagrange's methods.
<!-- END RELEASE NOTES -->

sympy-bot avatar Aug 12 '24 13:08 sympy-bot

🟠

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 https://github.com/sympy/sympy-bot/issues/75.

The following commits add new files:

  • bcd7778ff208d4eddce97c5b2d9dad63899401c8:
    • sympy/simplify/cse_diff.py
  • f72e7c6b39df5675f8c6aef41286f9fb770b394d:
    • sympy/simplify/tests/test_cse_diff.py

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

sympy-bot avatar Aug 12 '24 13:08 sympy-bot

Considerations about bicycle_example.rst

As can be observed from the automatic tests, there is a discrepancy between the expected values and the obtained values for the bicycle example in doc/src/tutorials/physics/mechanics/.

The expected values used in the test are taken from the paper Meijaard 2007.

To understand whether the discrepancy observed when running the test using the new forward_jacobian function inside the linearizer represents a problem, I summarize the findings in the following table:

Source v Complex Eigenvalue (Real) Complex Eigenvalue (Imaginary) Real Eigenvalue 1 Real Eigenvalue 2
norm_jac 1 3.52696170990069 -0.807740275199311 -3.13423125066578 -7.11008014637441
meijaard 1 3.52696170990070 -0.80774027519930 -3.13423125066578 -7.11008014637442
new_jac 1 3.52696170990069 -0.807740275199311 -3.13423125066578 -7.11008014637441
norm_jac 2 2.68234517512745 -1.68066296590676 -3.07158645641514 -8.67387984831737
meijaard 2 2.68234517512745 -1.68066296590675 -3.07158645641514 -8.67387984831735
norm_jac 3 1.70675605663973 -2.31582447384324 -2.63366137253665 -10.3510146724592
meijaard 3 1.70675605663975 -2.31582447384325 -2.63366137253667 -10.35101467245920
norm_jac 4 0.413253315211239 -3.07910818603205 -1.42944427361326 -12.1586142657644
meijaard 4 0.41325331521125 -3.07910818603206 -1.42944427361326 -12.15861426576447
new_jac 4 0.413253315211238 -3.07910818603205 -1.42944427361326 -12.1586142657644
norm_jac 5 -0.775341882195845 -4.46486771378823 -0.322866429004087 -14.0783896927982
meijaard 5 -0.77534188219585 -4.46486771378823 -0.32286642900409 -14.07838969279822
new_jac 5 -0.775341882195846 -4.46486771378823 -0.322866429004085 -14.0783896927982

In the table the current expected values are identified by: norm_jac, the values from the paper are identified as: meijaard, and the values that I obtained are identified by: new_jac.

There is also another discrepancy, regarding values of matrix A:

  • The first two rows are identical in both matrices.
  • In the third row, the discrepancy is in the first element:
    • Expected: 9.48977444677355
    • Got: 9.48977444677356
  • The fourth row is identical in both matrices.

Note: I am not considering the obtained discrepancies between very low values (1e-64) becasue I don’t think they are relevant

It seems that by truncating just after the 13th decimal digit all the results should match. What are your thought on this? @tjstienstra @moorepants

ricdigi avatar Aug 19 '24 16:08 ricdigi

You can merge in master into this branch to remove all the commits from the first PR.

moorepants avatar Aug 27 '24 14:08 moorepants

Make a unit test that shows the new options work in the three locations.

moorepants avatar Aug 27 '24 14:08 moorepants