Now it is possible to calculate for example this:
In [451]: SolveFractionIeq((2*x-1)/(x-1), (2*x-1)/x, x)
Out[451]: [[1/2, 1], (-∞, 0]]
... which is almost correct if we omit the closedness of intervals
(somebody fixes?).
Note that the aforementioned UnboundLocalError in inequalities.py
prevents calculation of simpler problems with constants:
In [452]: SolveFractionIeq((2*x-1)/(x-1), 1, x)
The inequality solver for fractions follows. Sorry that I have
recently no time to learn how to effectively contribute to the sympy's
git trees.
---------------
def SetsIntersect(*sets):
"""
Inputs intervals (or sets consisting of intervals) and calculates the
intersection of these.
>>> SetsIntersect([Interval(1,2),Interval(3,4)], Interval(1.5,3.5))
[[1.5, 2], [3, 3.5]]
"""
if len(sets)<2: return list(sets)
lset = sets[0]
if type(lset) not in (tuple,list): lset=[lset]
for rset in sets[1:]:
if type(rset) not in (tuple,list): rset=[rset]
newlset = []
for lint in lset:
for rint in rset:
inters = lint.intersect(rint)
if inters.measure>0:
newlset.append(inters)
lset = newlset
return lset
def SolveFractionIeq(left,right,x):
"""
Returns a set of intervals, when the inequality holds. Works with
fractions, too.
>>> SolveFractionIeq((2*x-1)/(x-1), (2*x-1)/x, x)
[[1/2, 1], (-∞, 0]]
>>> SolveFractionIeq((2*x-1)/(x-1), 1, x)
"""
if type(left) in (mul.Mul, power.Pow): ## numerator/denominator
separation
l1, l2=left.as_numer_denom()
else:
l1, l2=(left,1)
if type(right) in (mul.Mul, power.Pow):
r1, r2=right.as_numer_denom()
else:
r1, r2=(right,1)
pos_pos = SetsIntersect( ## Where both denominators positive
solve([Le(l1*r2,l2*r1), Assume(x,Q.real)],x,relational=False),
solve([Le(0,l2), Assume(x,Q.real)],x,relational=False),
solve([Le(0,r2), Assume(x,Q.real)],x,relational=False))
neg_neg = SetsIntersect( ## Where both denominators positive
solve([Le(l1*r2,l2*r1), Assume(x,Q.real)],x,relational=False),
solve([Le(l2,0), Assume(x,Q.real)],x,relational=False),
solve([Le(r2,0), Assume(x,Q.real)],x,relational=False))
neg_pos = SetsIntersect( ## Where left denom negative, right
positive
solve([Le(l2*r1,l1*r2), Assume(x,Q.real)],x,relational=False),
solve([Le(l2,0), Assume(x,Q.real)],x,relational=False),
solve([Le(0,r2), Assume(x,Q.real)],x,relational=False))
pos_neg = SetsIntersect( ## Where right denom negative, left
positive
solve([Le(l2*r1,l1*r2), Assume(x,Q.real)],x,relational=False),
solve([Le(0,l2), Assume(x,Q.real)],x,relational=False),
solve([Le(r2,0), Assume(x,Q.real)],x,relational=False))
return pos_pos+neg_neg+neg_pos+pos_neg
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sympy?hl=en.