[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
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

fix(core): restrict the 2-arg Mul to -1 #27200

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

oscarbenjamin
Copy link
Collaborator
@oscarbenjamin oscarbenjamin commented Oct 29, 2024

Since 3e835b5 from gh-716 there is the following behaviour:

  >>> 2*(x + y)
  2*x + 2*y

Specifically if there is a Mul with 2 args and one arg is a Rational and the other an Add then the Rational is distributed into the Add. More generally if there is a Mul and the arguments collapse to a number and an Add e.g. Mul(2, 3.0, x + y) then the coefficient is distributed into the Add.

There are various problems with this behaviour such as the fact that it breaks associativity (a*b)*c != a*(b*c). Previous discussions have agreed that this should be removed (e.g. gh-4596). There have been previous attempts to remove this e.g. gh-17242 but it is not easy because so much code now depends on it.

This commit attempts a more limited change in which the 2-arg Mul is mostly removed but kept for one important special case: if we have Mul(-1, Add(...)) then the -1 will be distributed into the Add. This special case is important because it arises when subtracting to Adds e.g.:

  >>> x + y - (x + y)
  0

It could perhaps be possible to special case negation and subtraction somehow so that negating the terms of an Add happens specifically in subtraction rather than more generally in a Mul but here we just preserve the 2-arg behaviour for -1 specifically as a more limited change to existing behaviour. Keeping this particular special case of the 2-arg Mul dramatically reduces the number of test failures seen in the test suite compared to removing automatic distribution altogether.

References to other Issues or PRs

Brief description of what is fixed or changed

Other comments

Release Notes

  • core
    • BREAKING CHANGE: Multiplying a Rational and an Add no longer distributes the Rational into the Add e.g. 2*(x + y) remains as 2*(x + y) rather than distributing as 2*x + 2*y. Distribution is now limited to the special case in which the multiplier is -1 so -(x + y) still transforms to -x - y but any other coefficient will not be distributed.

@sympy-bot
Copy link
sympy-bot commented Oct 29, 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
    • BREAKING CHANGE: Multiplying a Rational and an Add no longer distributes the Rational into the Add e.g. 2*(x + y) remains as 2*(x + y) rather than distributing as 2*x + 2*y. Distribution is now limited to the special case in which the multiplier is -1 so -(x + y) still transforms to -x - y but any other coefficient will not be distributed. (#27200 by @oscarbenjamin)

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.
Since 3e835b5 from gh-716 there is the following behaviour:
```
  >>> 2*(x + y)
  2*x + 2*y
```
Specifically if there is a Mul with 2 args and one arg is a Rational and the other an Add then the Rational is distributed into the Add. More generally if there is a Mul and the arguments collapse to a number and an Add e.g. `Mul(2, 3.0, x + y)` then the coefficient is distributed into the Add.

There are various problems with this behaviour such as the fact that it breaks associativity `(a*b)*c != a*(b*c)`. Previous discussions have agreed that this should be removed (e.g. gh-4596). There have been previous attempts to remove this e.g. gh-17242 but it is not easy because so much code now depends on it.

This commit attempts a more limited change in which the 2-arg Mul is mostly removed but kept for one important special case: if we have `Mul(-1, Add(...))` then the -1 will be distributed into the Add. This special case is important because it arises when subtracting to Adds e.g.:
```
  >>> x + y - (x + y)
  0
```
It could perhaps be possible to special case negation and subtraction somehow so that negating the terms of an Add happens specifically in subtraction rather than more generally in a Mul but here we just preserve the 2-arg behaviour for -1 specifically as a more limited change to existing behaviour. Keeping this particular special case of the 2-arg Mul dramatically reduces the number of test failures seen in the test suite compared to removing automatic distribution altogether.

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


#### 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
   * BREAKING CHANGE: Multiplying a Rational and an Add no longer distributes the Rational into the Add e.g. `2*(x + y)` remains as `2*(x + y)` rather than distributing as `2*x + 2*y`. Distribution is now limited to the special case in which the multiplier is `-1` so `-(x + y)` still transforms to `-x - y` but any other coefficient will not be distributed.
<!-- END RELEASE NOTES -->

Since 3e835b5 from sympygh-716 there is the following behaviour:

  >>> 2*(x + y)
  2*x + 2*y

Specifically if there is a Mul with 2 args and one arg is a Rational
and the other an Add then the Rational is distributed into the Add.
More generally if there is a Mul and the arguments collapse to a number
and an Add e.g. Mul(2, 3.0, x + y) then the coefficient is distributed
into the Add.

There are various problems with this behaviour such as the fact that it
breaks associativity (a*b)*c != a*(b*c). There have been previous
attempts to remove this e.g. sympygh-17242 but it is not easy because so much
code now depends on it.

This commit attempts a more limited change in which the 2-arg Mul is
mostly removed but kept for one important special case: if we have
Mul(-1, Add(...)) then the -1 will be distributed into the Add. This
special case is important because it arises when subtracting to Adds
e.g.:

  >>> x + y - (x + y)
  0

It could perhaps be possible to special case negation and subtraction
somehow so that negating the terms of an Add happens specifically in
subtraction rather than more generally in a Mul but here we just
preserve the 2-arg behaviour for -1 specifically as a more limited
change to existing behaviour. Keeping this particular special case of
the 2-arg Mul dramatically reduces the number of test failures seen in
the test suite comared to removing automatic distribution altogether.
@oscarbenjamin
Copy link
Collaborator Author

a more limited change in which the 2-arg Mul is mostly removed but kept for one important special case: if we have Mul(-1, Add(...)) then the -1 will be distributed into the Add.

Interestingly Maxima has this behaviour:

(%i6) 2*(x + y);
(%o6)                              2 (y + x)
(%i7) -1*(x + y);
(%o7)                              (- y) - x
(%i8) -2*(x + y)/2;
(%o8)                              (- y) - x
(%i9) -1*(x + y)*(a + b);
(%o9)                          - (b + a) (y + x)
(%i10) (-1*(x + y))*(a + b);
(%o10)                        (b + a) ((- y) - x)
(%i11) -(x + y)*(a + b);
(%o11)                        (b + a) ((- y) - x)

It seems that there is a special case for -1 in a 2-arg Mul.

SymEngine does not do this at all e.g.:

In [1]: import symengine

In [2]: x, y = symengine.symbols('x, y')

In [3]: x + y - (x + y)
Out[3]: x + y - (x + y)

It is hard to say what is actually best here but from running parts of the test suite and studying the failures there are far bigger and deeper failures that come from matching the SymEngine behaviour than from matching the Maxima behaviour here. When restricting distribution to -1 most of the test failures just look like tests that would need to be updated because the form of the output has changed. If -1 also does not distribute then it seems to break all kinds of things leading to RecursionError and other deeper failures where basic code just doesn't function any more without significant changes.

@oscarbenjamin
Copy link
Collaborator Author

When restricting distribution to -1 most of the test failures just look like tests that would need to be updated because the form of the output has changed.

I think in so far as possible though it would be better not to update the tests but to try to find where in the code the automatic distribution was happening and add it back in explicitly so that the output comes in the same form.

@oscarbenjamin
Copy link
Collaborator Author

Looks like trigsimp doesn't work:

In [11]: trigsimp(cos(x)**2 - sin(x)**2)
Out[11]: 
         2   
1 - 2sin (x)

On master it is:

In [1]: trigsimp(cos(x)**2 - sin(x)**2)
Out[1]: cos(2x)

Somewhere in trigsimp there must be an assumption that something distributes in order to make this simplification work.

@oscarbenjamin oscarbenjamin marked this pull request as draft October 29, 2024 12:13
@oscarbenjamin
Copy link
Collaborator Author

This fails in the PR:

In [1]: cot(pi/24).radsimp() # master
Out[1]: √2 +3 + 2 +6

In [1]: cot(pi/24).radsimp() # PR
Out[1]: 
         2               
(√2 +6) ⋅(-2 + 1 +6)
─────────────────────────
            4  

@oscarbenjamin
Copy link
Collaborator Author

It seems powsimp needs a bit of help:

In [1]: powsimp(4**k*4**(-k - 1), combine='exp') # master
Out[1]: 1/4

In [1]: powsimp(4**k*4**(-k - 1), combine='exp') # PR
Out[1]: 
 2k + 2⋅(-k - 1)
2   

@oscarbenjamin
Copy link
Collaborator Author

Mod needs some help:

In [1]: (12*x + 18*y) % (3*x) # master
Out[1]: 3⋅(6y mod x)

In [1]: (12*x + 18*y) % (3*x)  # PR
Out[1]: 3⋅(2⋅(2x + 3y) mod x)

@oscarbenjamin
Copy link
Collaborator Author
oscarbenjamin commented Oct 30, 2024

This should probably distribute:

In [2]: sqrt(-3 - 4*I)  # master
Out[2]: 1 - 2

In [1]: sqrt(-3 - 4*I) # PR
Out[1]: 2⋅(1/2 - )

I think that the intention here is to have a canonical form of Gaussian rational if possible so it should be expanded.

@oscarbenjamin
Copy link
Collaborator Author
oscarbenjamin commented Oct 30, 2024

Also this difference:

In [1]: sqrt((3 + 4*I)/4) # master
Out[1]: 
    
1 +2

In [1]: sqrt((3 + 4*I)/4) # PR
Out[1]: 
2 + 
─────
  2

This one is handled in Pow._eval_expand_power_base when called from Mul._eval_power. I'm not sure what is best here.

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

Successfully merging this pull request may close these issues.

2 participants