problem in solving inequations

95 views
Skip to first unread message

bruno....@gmail.com

unread,
Dec 2, 2014, 9:44:33 AM12/2/14
to sy...@googlegroups.com

  Hi all,

  I am new to sympy (and to symbolic computations...) and I meet a problem
  for solving (possibly difficult) inequalities depending upon on real symbol

(I have used k = symbols('k',real=True,positive=True)).

 
  First I build a set of (5) inequalities, stored in a list L and I want to find
  for which subset (of real numbers) they are all true. Substition for a given value
 of the symbol k works, for instance :

for elem in L:

print(elem.subs(k,Rational(9,4)))


outputs five True. But solving this way :


res = solve([k>2, k <= 3, L[0], L[1], L[2], L[3], L[4]], k)


leads to the following error:


Traceback (most recent call last):

File "<stdin>", line 1, in <module>

File "/usr/lib/python2.7/site-packages/sympy/solvers/solvers.py", line 691, in solve

return reduce_inequalities(f, symbols=symbols)

File "/usr/lib/python2.7/site-packages/sympy/solvers/inequalities.py", line 513, in reduce_inequalities

poly_reduced.append(reduce_rational_inequalities([exprs], gen))

File "/usr/lib/python2.7/site-packages/sympy/solvers/inequalities.py", line 255, in reduce_rational_inequalities

result = solution.as_relational(gen)

File "/usr/lib/python2.7/site-packages/sympy/sets/sets.py", line 1212, in as_relational

return Or(*[set.as_relational(symbol) for set in self.args])

AttributeError: 'Complement' object has no attribute 'as_relational'

 

From these messages, at first glance, I guessed for something wrong with my relationals L but

each element of L seems to have a good type:

>>> type(L[1])

<class 'sympy.core.relational.StrictGreaterThan'>


the same than what returns: type(k>2). So I am wondering if I have made

some mistake or if this is due to some limitations as my relations are quite complicated :

>>> L[1]

4*(-k**6 + 2*k**4 + 4*k**2 + 12)/(k*(-k**8 + 2*k**6 + 4*k**4 + 16*k**2 - 8)) > (k**8 - 4*k**6 + 16)/(k*(k**8 - 2*k**6 - 4*k**4 - 16*k**2 + 8))


Thanks for any help or advice
Bruno




Chris Smith

unread,
Dec 2, 2014, 11:30:38 AM12/2/14
to sy...@googlegroups.com
Could you give this a try in my (smichr) improve_relational branch? I saw that error there, too, and a correction I made avoided the error.

/c

bruno....@gmail.com

unread,
Dec 2, 2014, 1:25:34 PM12/2/14
to sy...@googlegroups.com


On Tuesday, December 2, 2014 5:30:38 PM UTC+1, Chris Smith wrote:
Could you give this a try in my (smichr) improve_relational branch? I saw that error there, too, and a correction I made avoided the error.



  Thanks but did you mean I should try your own git repository sympy version
 or maybe a part of it ?  How I can do that ? (yesterday I 've dowloaded
 the last official tgz sympy archive, should I install the git version then "merge"
 with yours or something like that ?).

    Bruno


Chris Smith

unread,
Dec 2, 2014, 4:14:33 PM12/2/14
to sy...@googlegroups.com
If you did something like

git remote add smichr git://github.com/smicbr/sympy.git
git pull smichr
git checkout smichr/improve_relational

that would put you in my branch and allow you to test your expression.

bruno....@gmail.com

unread,
Dec 3, 2014, 4:22:43 AM12/3/14
to sy...@googlegroups.com

  Hi Chris, thanks for helping me. I've just installed git on my machine. Then i've tried to
 download the sympy git version with :

git clone git://github.com/sympy/sympy.git

 seems that all is OK. But when I tried (from the same directory
 I 've launched the first git command)  :



git remote add smichr git://github.com/smicbr/sympy.git


 it looks like  git://github.com/smicbr is not existant :


git remote add smichr git://github.com/smicbr/sympy.git
fatal: Not a git repository (or any parent up to mount point /home)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).

 If I enter this same command (git remote add smichr git://github.com/smicbr/sympy.git)
 from the sympy directory (created by my first git clone) nothing happens (no error message)
 then when I enter :

git pull smichr
fatal: remote error:
  Repository not found.

  Did I miss something ?

Bruno

Harsh Gupta

unread,
Dec 3, 2014, 5:47:20 AM12/3/14
to sy...@googlegroups.com
> Did I miss something ?
Nope, There is a typo in above instructions, replace "smicbr" with "smichr".
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/92fefac3-24ac-4647-9085-09cc2a9800e3%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Harsh

bruno....@gmail.com

unread,
Dec 3, 2014, 9:34:06 AM12/3/14
to sy...@googlegroups.com

   OK Thanks. Still having some problems with the git instructions
  suggested above (seems that I should learn git a little) I finally
 do this :

    git clone git://github.com/smichr/sympy.git

  Hoping it contains the last Chris modifications (maybe not ?).
 Then I 've installed this new sympy as the tgz one's.

  I see some differences : one of my 5 relations is now already detected
 as True for all k real positive. So I  remove it and try to solve for the 4 others.
 I get :
 

>>> res = solve([k>2, k <= 3, R[0], R[1], R[2], R[3]],k)

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

File "/usr/lib/python2.7/site-packages/sympy/solvers/solvers.py", line 674, in solve

symbols=symbols)

File "/usr/lib/python2.7/site-packages/sympy/solvers/inequalities.py", line 434, in reduce_inequalities

poly_reduced.append(reduce_rational_inequalities([exprs], gen, assume))

File "/usr/lib/python2.7/site-packages/sympy/solvers/inequalities.py", line 209, in reduce_rational_inequalities

solution = solve_rational_inequalities(eqs)

File "/usr/lib/python2.7/site-packages/sympy/solvers/inequalities.py", line 144, in solve_rational_inequalities

global_interval -= denom_interval

File "/usr/lib/python2.7/site-packages/sympy/core/sets.py", line 250, in __sub__

return self.intersect(other.complement)

File "/usr/lib/python2.7/site-packages/sympy/core/sets.py", line 133, in complement

return self._complement

File "/usr/lib/python2.7/site-packages/sympy/core/sets.py", line 1229, in _complement

% self)

ValueError: {RootOf(k**8 - 2*k**6 - 4*k**4 - 16*k**2 + 8, 0)}: Complement not defined for symbolic inputs


So the error message is slightly different... Thanks for any hint.


Bruno

Chris Smith

unread,
Dec 4, 2014, 1:23:28 AM12/4/14
to sy...@googlegroups.com
Could you do `print filldedent(R);print filldedent(L)` so I can see what the expressions are that you are trying to solve?


On Tuesday, December 2, 2014 8:44:33 AM UTC-6, bruno....@gmail.com wrote:

bruno....@gmail.com

unread,
Dec 4, 2014, 3:24:11 AM12/4/14
to sy...@googlegroups.com


On Thursday, December 4, 2014 7:23:28 AM UTC+1, Chris Smith wrote:
Could you do `print filldedent(R);print filldedent(L)` so I can see what the expressions are that you are trying to solve?



   Sorry my list L of 4 relationals was latter renamed R...  Here they are
  (btw I was not able to use filldedent, using "from sympy import *" so
   I used print(R) and reformat with my editor to have an element per line) :

  R = [(-k**8 + 8*k**6 - 8*k**4 - 16*k**2 - 64)/(k*(k**8 - 2*k**6 - 4*k**4 - 16*k**2 + 8)) > 0,
     (-k**8 + 8*k**6 - 8*k**4 - 16*k**2 - 64)/(k*(k**8 - 2*k**6 - 4*k**4 - 16*k**2 + 8)) > 0,
     (k**8 - 6*k**6 + 8*k**2 + 96)/(k*(k**8 - 2*k**6 - 4*k**4 - 16*k**2 + 8)) >= 0,
     (k**8 - 2*k**7 + 4*k**6 + 4*k**5 - 16*k**4 + 8*k**3 - 40*k**2 + 24*k - 64)/(k*(k**8 - 2*k**6 - 4*k**4 - 16*k**2 + 8)) >= 0]

  The symbol k is restricted to be real and in (2,3]. 

(k is introduced with k = symbols('k',real=True,positive=True)).


  In fact even if I try to solve for only the first relational I meet the same error message :

>>> res= solve([k>2, k <= 3, R[0]],k)

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

File "/usr/lib/python2.7/site-packages/sympy/solvers/solvers.py", line 674, in solve

symbols=symbols)

File "/usr/lib/python2.7/site-packages/sympy/solvers/inequalities.py", line 434, in reduce_inequalities

poly_reduced.append(reduce_rational_inequalities([exprs], gen, assume))

File "/usr/lib/python2.7/site-packages/sympy/solvers/inequalities.py", line 209, in reduce_rational_inequalities

solution = solve_rational_inequalities(eqs)

File "/usr/lib/python2.7/site-packages/sympy/solvers/inequalities.py", line 144, in solve_rational_inequalities

global_interval -= denom_interval

File "/usr/lib/python2.7/site-packages/sympy/core/sets.py", line 250, in __sub__

return self.intersect(other.complement)

File "/usr/lib/python2.7/site-packages/sympy/core/sets.py", line 133, in complement

return self._complement

File "/usr/lib/python2.7/site-packages/sympy/core/sets.py", line 1229, in _complement

% self)

ValueError: {RootOf(k**8 - 2*k**6 - 4*k**4 - 16*k**2 + 8, 0)}: Complement not defined for symbolic inputs


Bruno


bruno....@gmail.com

unread,
Dec 4, 2014, 5:28:20 AM12/4/14
to sy...@googlegroups.com

  Oups I just saw that my first and second relationals are the same. So I have to solve for only 3 relationals.

  I just went to try the wolfram alpha online calculator, I got :

   1/ for the first relational :

   solve((-k**8 + 8*k**6 - 8*k**4 - 16*k**2 - 64)/(k*(k**8 - 2*k**6 - 4*k**4 - 16*k**2 + 8)) > 0, k > 2, k <= 3)

    alpha gives:

2<k<sqrt(2/3 (2+(53-3 sqrt(201))^(1/3)+(53+3 sqrt(201))^(1/3)))

    2/ for the third one:

solve((k**8 - 6*k**6 + 8*k**2 + 96)/(k*(k**8 - 2*k**6 - 4*k**4 - 16*k**2 + 8)) >= 0, k > 2, k <= 3)

   alpha gives:

sqrt(2/3+1/3 (404-36 sqrt(109))^(1/3)+1/3 2^(2/3) (101+9 sqrt(109))^(1/3))<=k<=3

   3/ and for the fourth one:

 solve((k**8 - 2*k**7 + 4*k**6 + 4*k**5 - 16*k**4 + 8*k**3 - 40*k**2 + 24*k - 64)/(k*(k**8 - 2*k**6 - 4*k**4 - 16*k**2 + 8)) >= 0, k > 2, k <= 3)

   alpha gives:

2<k<=3


  Bruno
Reply all
Reply to author
Forward
0 new messages