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

sets - fixes imageset does not work over range #21794

Closed
wants to merge 2 commits into from

Conversation

praneethratna
Copy link
Contributor

References to other Issues or PRs

Brief description of what is fixed or changed

Fixes #18400 by changing code in sympy/sets/handlers/functions.py and also added a test case function test_issue_18400() in sympy/sets/tests/test_sets.py

Other comments

Release Notes

NO ENTRY

@sympy-bot
Copy link

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

  • No release notes entry will be added for this pull request.
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. -->


#### Brief description of what is fixed or changed
Fixes #18400 by changing code in sympy/sets/handlers/functions.py and also added a test case function test_issue_18400() in sympy/sets/tests/test_sets.py
#### 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 -->
NO ENTRY
<!-- END RELEASE NOTES -->

@praneethratna praneethratna changed the title sets - added a try and except block in _set_function() in functions.py and also added a test case function in test_sets.py sets - fixes imagenet does not work over range Jul 28, 2021
@github-actions
Copy link

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
     [ed9a550f]       [12b7787c]
     <sympy-1.8^0>                 
-         8.83±0s          4.25±0s     0.48  integrate.TimeIntegrationRisch02.time_doit(100)
-         8.76±0s          4.24±0s     0.48  integrate.TimeIntegrationRisch02.time_doit_risch(100)
+      73.5±0.1μs      2.57±0.01ms    35.00  matrices.TimeDiagonalEigenvals.time_eigenvals
-     5.10±0.01ms         2.81±0ms     0.55  solve.TimeRationalSystem.time_linsolve(10)
-        1.03±0ms          653±1μs     0.64  solve.TimeRationalSystem.time_linsolve(5)
-        1.21±0ms          795±2μs     0.66  solve.TimeSparseSystem.time_linsolve_eqs(10)
-     2.27±0.01ms         1.44±0ms     0.64  solve.TimeSparseSystem.time_linsolve_eqs(20)
-        3.33±0ms         2.12±0ms     0.64  solve.TimeSparseSystem.time_linsolve_eqs(30)

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

Comment on lines +151 to +154
try:
n = self.size
except ValueError:
n = None
Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer an explicit check rather than catching an exception.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@oscarbenjamin i mean like I've tried an explicit check ie something like
if not self.size.is_Integer:
return
but it kinda seems to return the initial error of the problem ie the ValueError('invalid method for symbolic range'),
what could be a possible solution to this?

Copy link
Member

Choose a reason for hiding this comment

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

Perhaps Range could be modified to return None instead of an error -- but I'm not sure what the implications of that would be. Capturing the ValueError for a single line of code that queries a property that raises a ValueError if the size is unknown doesn't bother me so much, however. (But I'm the one that suggested it.)

Copy link
Contributor

Choose a reason for hiding this comment

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

There should be a way to get the symbolic size. It could be a different attribute like r.length or something but the size of Range(n) is n and it should be possible to get that in some way without an exception.

Copy link
Member

Choose a reason for hiding this comment

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

Only a few symbolic assumptions could actually give you a size (like even+prime=2). And it is worse with Range(x,y,z). Symbolic ranges are largely for display purposes only; is it worth the work to return a Piecewise that would be true for symbolic arguments? And that should only be returned if all args are declared as integers. It's not a trivial matter; I doubt it is of any significant use; I would prefer to just let the ValueError signal that it's not available (as here) and let the code work around it. Would returning None be better than raising an error? Or would NotImplemented('on purpose') be better?

Copy link
Contributor

Choose a reason for hiding this comment

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

If we are going to have a Range with symbolic start/end then its size can be symbolic as well. I can see the value in having an attribute that gives the size when it is known as an explicit integer and raises otherwise (as .size does). There also is a need for an attribute that simply gives the symbolic size as an expression e.g. Range(n).length -> n even if n is a symbol or some other expression.

Many builtin operations can raise ValueError so ValueError should not be explicitly raised and caught within the SymPy codebase because it can mask bugs. If there is ever a need to catch the exception then a different exception class should be raised in the first place.

It is more robust not to catch exceptions at all though. There should be an interface that makes it possible to check whether an operation should succeed without needing to catch an exception: then any exception that does get raised can bubble up and can rightly be considered a bug.

Copy link
Member

@smichr smichr Jul 29, 2021

Choose a reason for hiding this comment

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

This is a test of the Piecewise logic for the step size (assuming 0 for cases that would raise an error):

from itertools import combinations, permutations
for i in combinations(range(-3,4),3):
    for i in permutations(i):
        start,stop,step=i
        if all([int(start)==start,int(stop)==stop,int(step)==step,(stop-start)*step>0]):
            n = int((stop - start)/step) + (1 if (stop-start)%step else 0)
        else:
            n=0
        try:
            m=len(range(*i))
        except:
            m=0
        assert n == m

And here is the Piecewise which checks that all args are integers or at most 1 of them is infinite, that they are compatible with the direction of the step:

Piecewise((floor((stop - start)/step) + Piecewise((1,Ne(Mod(stop-start,step),0)),
(0,True)),And(Eq(floor(start),start),Eq(floor(stop),stop),Eq(floor(step),step),(stop - 
start)*step>0,Ne(step+1,step),Or(Ne(start+1,start),Ne(stop+1,stop)))),(0,True))

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay, that's very complicated. That makes me wonder if symbolic range was a good idea. We mainly use it because we want to represent sets of integers but it would be better to have something simpler for that e.g.: #17856 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

We supported it for the purpose of code generation. We could also compute the size for something like Range(i, i + k*j, k) as j but I think for simplicity we just steered clear of trying to support it without a well defined need.

@smichr smichr changed the title sets - fixes imagenet does not work over range sets - fixes imageset does not work over range Jul 29, 2021
@oscargus
Copy link
Contributor

oscargus commented Aug 25, 2021

Closing this as the alternative fix in #21932 has been merged.

@oscargus oscargus closed this Aug 25, 2021
@praneethratna praneethratna deleted the 18400_imageset branch August 25, 2021 08:32
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.

imageset over Range does not work
5 participants