Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, to correct a few things that are just blatently wrong in your question:</p> <ul> <li><p><code>solve</code> solves for algebraic expressions. <code>solve(expr, x)</code> solves the equation expr = 0 for <code>x</code>. </p></li> <li><p><code>solve(x | y = False)</code> and so on are invalid syntax. You cannot use <code>=</code> to mean equality in Python. See <a href="http://docs.sympy.org/latest/tutorial/gotchas.html#equals-signs" rel="nofollow noreferrer">http://docs.sympy.org/latest/tutorial/gotchas.html#equals-signs</a> (and I recommend reading the rest of that tutorial as well). </p></li> <li><p>As I mentioned in the answer to <a href="https://stackoverflow.com/questions/19703284/boolean-operation-with-symbol-in-sympy">another</a> question, <code>Symbol('y', bool=True)</code> does nothing. <code>Symbol('x', something=True)</code> sets the <code>is_something</code> assumption on <code>x</code>, but <code>bool</code> is not a recognized assumption by any part of SymPy. Just use regular <code>Symbol('x')</code> for boolean expressions.</p></li> </ul> <p>As some commenters noted, what you want is <code>satisfiable</code>, which implements a SAT solver. <code>satisfiable(expr)</code> tells you if <code>expr</code> is satisfiable, that is, if there are values for the variables in <code>expr</code> that make it true. If it is satisfiable, it returns a mapping of such values (called a "model"). If no such mapping exists, i.e., <code>expr</code> is a contradiction, it returns <code>False</code>. </p> <p>Therefore, <code>satisfiable(expr)</code> is the same as solving for <code>expr = True</code>. If you want to solve for <code>expr = False</code>, you should use <code>satisfiable(~expr)</code> (<code>~</code> in SymPy means not). </p> <pre><code>In [5]: satisfiable(x &amp; y) Out[5]: {x: True, y: True} In [6]: satisfiable(~(x | y)) Out[6]: {x: False, y: False} In [7]: satisfiable(x &amp; y &amp; z) Out[7]: {x: True, y: True, z: True} </code></pre> <p>Finally, note that <code>satisfiable</code> only returns one solution, because in general this is all you want, whereas finding all the solutions in general is extremely expensive, as there could be as many as <code>2**n</code> of them, where <code>n</code> is the number of variables in your expression. </p> <p>If however, you want to find all of them, the usual trick is to append your original expression with <code>~E</code>, where <code>E</code> is the conjunction of the previous solution. So for example, </p> <pre><code>In [8]: satisfiable(x ^ y) Out[8]: {x: True, y: False} In [9]: satisfiable((x ^ y) &amp; ~(x &amp; ~y)) Out[9]: {x: False, y: True} </code></pre> <p>The <code>&amp; ~(x &amp; ~y)</code> means that you don't want a solution where <code>x</code> is true and <code>y</code> is false (think of <code>&amp;</code> as adding extra conditions on your solution). Iterating this way, you can generate all solutions. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload