-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
Implemented interval arithmetic. #2682
Conversation
hargup
commented
Dec 15, 2013
- Allow additions and multiplications and other basic operations with intervals
- Allow elementary functions to take intervals as arguments
- Add tests
- Add documentation
I would use a separate class from Interval. For instance, |
I would do it. But I think it end up being the same class |
Maybe what you really want is a separate class on top of Interval. What you are building isn't really an interval, but rather an arbitrary element of an interval. [1, 2] + [2, 4) doesn't make sense mathematically, but (arbitrary element of [1, 2]) + (arbitrary element of [2, 4)) does. Anyway, we should definitely keep them separate. Interval is a set, not an expression (in the Expr sense). We often use abuse of notation to treat intervals as such, but abuses of notation only serve to create confusing type systems (which isn't to say we shouldn't allow nice syntactic sugar; that's a separate point). |
Thanks, I got it now. I have created a separate class For some reasons the test for division of intervals are failing on my local machine, but it working fine on IPython. Can you help me with this?
|
in numbers.py, m.b.?
Yes, the problem is here:
Please, take look on the python documentation & other classes (e.g. numbers) on how to handle this. Or you can change 2 -> Integer(2). |
Thanks, I had defined |
Yes, there are three combinations to test, Python 2, Python 2 with |
On Sun, Dec 22, 2013 at 03:19:09PM -0800, Aaron Meurer wrote:
Why not add just: |
@skirpichev: I created a new file instead of adding |
@asmeurer, @skirpichev : You can start the review. I hope this time you won't have to write comments like "space after comma". |
from sympy.core.singleton import S | ||
|
||
|
||
class IV(Interval, Expr): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why Expr? (and not even AtomicExpr)
I don't think it's a good idea to introduce a new file. Logically, IV belongs to numbers.py or just to sets.py. And for @asmeurer.
No, it is. All we do - define some new fancy binary ops: Interval x Interval -> Interval.
And maybe the current Set class is wrong. Probably, it should be like builtin set, and not to overload add as union, for example. See this: Interval - is just a very good name to keep it. If we keep clean separate interfaces (set-like and number-like) - nothing is wrong if we will have an entity that we can treat as a set and as an expression. |
Mul(self.end, other.end))) | ||
|
||
elif Number.is_real: | ||
if other > S.Zero: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if other.is_positive
It doesn't seem to belong to numbers.py to me. |
Mul(self.end, 1/other.end))) | ||
|
||
elif other.is_real: | ||
if other > S.Zero: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
other.is_positive
and so on, as above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops, missed them.
This commit implemented interval arithmetic as class IV in sympy.core.sets Now sympy can: - add and subtract intervals with intervals and Real Numbers - Multiply and divide intervals with intervals and Real Numbers - exponentiate an interval to some positive integer power - substitute an expression with interval and evaluate the resulting interval
I removed the https://gist.github.com/hargup/8161971 |
On Sat, Dec 28, 2013 at 09:58:53AM -0800, Harsh Gupta wrote:
It was just a suggestion (and maybe it has been already |
@@ -222,6 +224,16 @@ def eval(cls, arg): | |||
return S.One | |||
elif arg.is_negative: | |||
return cls(-arg) | |||
elif isinstance(arg, IV): | |||
if arg.start > S.Zero: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
arg.start.is_positive
and so on...
Added code to handle IV as arguments in elementary functions. Following functions are now supported: - exponential: exp, log - trigonometric: sin, cos, tan, cot - hyperbolic: sinh, cosh
It's all about python compatibility. If Set is a set of sympy's objects - maybe the same (as for builtin set container) interface makes sense.
If we keep separate IV and Interval - you should choose a good name for the first. |
Can you suggest any? |
Sorry :( |
I agree we should keep the same interface, but we should also keep + for union. |
@@ -257,6 +257,12 @@ def test_sympy__core__function__WildFunction(): | |||
assert _test_args(WildFunction('f')) | |||
|
|||
|
|||
@XFAIL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does this fail?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fails because two parameters for IV
are left_open
and right_open
which are Python booleans, hence are not an instance of Basic
.
Classes like this really show the need for a good dispatching system. One should be able to write this without touching any other file. |
Sorry I'm late to the discussion. I think that this class should inherit from Also, I think it should contain a
How far can we generalize the notion of an expression that carries around a set? SymPy.stats does this to some extent. Each symbol carries around both a set and a probability distribution on that set. Expressions containing multiple random symbols are projections onto cartesian products of sets. This Set-Expr issue should be a much easier problem. It may be that Intervals are easier to work with and deserve special attention. Some sort of simplification scheme might be helpful here. |
+1 to Matthew's comments. |
I should also note that I'm totally in favor of work in this direction. I think that an |
For scalar functions on sets I think that the natural thing to do is to rely on
In this way we rely on a general computational piece of sets to do much of the work for us. I expect this to expose errors in ImageSet. That's good. This doesn't handle set-set interactions though. |
I set up a draft of my set expression idea in #2721 |
Thanks a lot your words are encouraging.
That will be definitely better. The need to implement operations on Interval arose to handle cases like |
I was not of aware of
I think |
Sorry, I should have suggested that you use
In [13]: ImageSet(Lambda(x, x**2), Interval(0, 2))
Out[13]:
⎧ 2 ⎫
⎨x | x ∊ [0, 2]⎬
⎩ ⎭
In [14]: imageset(Lambda(x, x**2), Interval(0, 2))
Out[14]: [0, 4] |
|
Good catch. Looks like |
I have implemented an initial fix for |
I think this PR is no longer needed when Matthew is working on |
While I do think that |
On Wed, Jan 01, 2014 at 08:07:43AM -0800, Harsh Gupta wrote:
@hargup, please take look on |
Limits no longer a subclass of `Interval` S.false removed from Basic call for Limits, docstring for relationals repetitive test removed and Pow approach changed for Limits comments for future work added `Limits` -> `AccumulationBounds` (alias `AccumBounds`) also code from sympy#2682 taken from Harsha's PR. fixes sympy#5299 sympy#9934 changes `_print_Limits` -> `_print_AccumulationBouds`