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

Simplify function of Sum and Product on simplify #22278

Merged
merged 1 commit into from Aug 28, 2022

Conversation

oscargus
Copy link
Contributor

@oscargus oscargus commented Oct 14, 2021

References to other Issues or PRs

Relates to #22260 (also see #22264 which applies simplification as part of doit which also makes sense)
Closes #14219 (just adding test, fixed elsewhere)

Brief description of what is fixed or changed

The function in the Sum and Product is simplified when calling simplify.

Also, product_simplify is improved a bit, honoring simplify keyword arguments and using sift.

Other comments

Release Notes

  • concrete
    • The function of Sum and Product is now simplified when calling simplify.

@sympy-bot
Copy link

sympy-bot commented Oct 14, 2021

Hi, I am the SymPy bot (v167). 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:

  • concrete
    • The function of Sum and Product is now simplified when calling simplify. (#22278 by @oscargus)

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

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. -->
Relates to #22260 (also see  #22264 which applies simplification as part of `doit` which also makes sense)
Closes #14219 (just adding test, fixed elsewhere)

#### Brief description of what is fixed or changed
The function in the `Sum` and `Product` is simplified when calling `simplify`.

Also, `product_simplify` is improved a bit, honoring simplify keyword arguments and using `sift`.

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

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 -->
* concrete
   * The function of `Sum` and `Product` is now simplified when calling `simplify`. 
<!-- END RELEASE NOTES -->

Update

The release notes on the wiki have been updated.

@oscargus
Copy link
Contributor Author

The error is caused by factoring out an expression that should not be factored out...

image

Note that the "constant" in front of the right summation is dependent on Y. For some reason this is now factored out.

@oscargus oscargus marked this pull request as draft October 15, 2021 09:03
@oscargus
Copy link
Contributor Author

Seems like the problem causing the failed test was fixed elsewhere.

@oscargus oscargus marked this pull request as ready for review December 31, 2021 18:47
@github-actions
Copy link

github-actions bot commented Dec 31, 2021

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)

       before           after         ratio
     [41d90958]       [a3826118]
-         996±1μs          622±1μs     0.63  solve.TimeSparseSystem.time_linear_eq_to_matrix(10)
-     2.85±0.01ms         1.15±0ms     0.40  solve.TimeSparseSystem.time_linear_eq_to_matrix(20)
-     5.68±0.01ms         1.69±0ms     0.30  solve.TimeSparseSystem.time_linear_eq_to_matrix(30)

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

for term in prod_terms:
f = term.function.simplify(**kwargs)
if f.is_zero:
return S.Zero
Copy link
Contributor

Choose a reason for hiding this comment

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

Thee probably needs to be some checking here:

In [4]: p = Product(Product(sin(t)**2+cos(t)**2-1, (k, 0, 1))*Product(1/k, (k, 0, 1)), (n, 0, 1))

In [5]: p
Out[5]: 
       1                                               
─┬───────────┬─                                        
 │           │     1         1                         
 │           │  ─┬────┬─   ─┬──┬─                      
 │           │   │    │  1  │  │     2         2       
 │           │   │    │  ─⋅ │  │  sin (t) + cos (t) - 1
 │           │   │    │  k  │  │                       
 │           │   │    │    k = 0                       
 │           │   k = 0                                 
 │           │                                         
     n = 0                                             

In [6]: simplify(p)
Out[6]: 0

In [7]: p.doit()
Out[7]: 
                           42         2zoo⋅⎝sin (t) + cos (t) - 1In [8]: p.doit().simplify()
Out[8]: nan

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In which way should they evaluate? But of course it is possible to skip the early exit.

Copy link
Contributor

Choose a reason for hiding this comment

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

The most important thing is that we don't get a definite-looking result for a case like this that is really undefined.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I came back to this now and it turns out that the problem is actually not in this implementation (although return 0 is not a good idea).

In current master:

In [3]: p = Product(sin(t)**2+cos(t)**2-1, (k, 0, 1))*Product(1/k, (k, 0, 1))

In [4]: p.simplify()
Out[4]: 0

In [5]: p.doit()
Out[5]: zoo*(sin(t)**2 + cos(t)**2 - 1)**2

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Which seems to come from:

In [3]: p = 0*Product(1/k, (k, 0, 1))

In [4]: p
Out[4]: 0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this comes from:

sympy/sympy/core/mul.py

Lines 684 to 691 in a382611

elif coeff.is_zero:
# we know for sure the result will be 0 except the multiplicand
# is infinity or a matrix
if any(isinstance(c, MatrixExpr) for c in nc_part):
return [coeff], nc_part, order_symbols
if any(c.is_finite == False for c in c_part):
return [S.NaN], [], order_symbols
return [coeff], [], order_symbols

where especially is_finite == False actually should be is_finite != True, but that will also give some other interesting effects, like 0*x not evaluating to 0 with default assumptions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nah, that should rather be something like:

            # we know for sure the result will be 0 except the multiplicand
            # is infinity or a matrix
            if any(isinstance(c, MatrixExpr) for c in nc_part):
                return [coeff], nc_part, order_symbols
            if any(c.is_finite == False for c in c_part):
                return [S.NaN], [], order_symbols
            possibly_not_finite = [c for c in c_part if c.is_finite == None]
            return [coeff], possibly_not_finite, order_symbols

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changing this leads to that the code breaks hard. Not only test failures, but test errors. Will update once everything is ran.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previous was wrong. Correct is probably:

            # we know for sure the result will be 0 except the multiplicand
            # is infinity or a matrix
            if any(isinstance(c, MatrixExpr) for c in nc_part):
                return [coeff], nc_part, order_symbols
            if any(c.is_finite == False for c in c_part):
                return [S.NaN], [], order_symbols
            c_part = [c for c in c_part if c.is_finite == None]
            if not c_part:
                return [coeff], [], order_symbols

But quite a lot of failures and simplify still evaluates incorrectly to 0.

Copy link
Contributor

Choose a reason for hiding this comment

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

See also #17224

@oscargus oscargus force-pushed the sumproductsimplify branch 5 times, most recently from b1d743b to 2021776 Compare August 28, 2022 15:25
@oscarbenjamin
Copy link
Contributor

Looks good.

@oscarbenjamin oscarbenjamin merged commit 0cb6343 into sympy:master Aug 28, 2022
@oscargus oscargus deleted the sumproductsimplify branch August 29, 2022 06:51
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.

Sum((diag(0, 2, -3))**n, (n, 0, 3)).doit() causes an error
3 participants