I have expressions which look like this:
"0.56 * ((i1 == 5) & (i2 == 2)) + 0.88 * ((i1 == 4) & (i2 == 3)) +
0.33 * ((i1 == 2) & (i2 ==4)) + 0.25 * (~b1 & b2)"
where i* are integer variables, b* are booleans.
At some point in my program I know the values for half the variables
(i1, b1, ...), and I would like to simplify the expression by
partially evaluating it.
eg if i1 is 5 and b1 is False, I would like to get:
"0.56 * (i2 == 2) + 0.25 * b2"
I was hoping sympy would help my in this, however I stumbled on two problems:
- the "==" is not supported by Sympy. From what I read, it seems to be
on purpose and I should use "Eq(expr1, expr2)" instead... Ok, fine,
but given that the expressions I get are written using ==, is there an
easy way to transform "==" to Eq()? I was hoping to avoid writing a
parser for those expressions... "sympify" does not help.
- while the boolean simplification work as I was hoping (in the
example above, sympy gets the "0.25 * b2" part right), the equalities
are not:
In [31]: x = sympy.Symbol('x')
In [32]: (0.5 * sympy.Eq(x, 5)).subs({'x': 5})
Out[32]: 0.5*(5 == 5)
I was hoping for just "0.5".
What's worse for me is that I was unable to find a way to evaluate
that expression. evalf() and the others I tried don't help. Is there a
way to do this?
Thanks in advance,
--
Gaëtan de Menten
> Hi all,
>
> I have expressions which look like this:
> "0.56 * ((i1 == 5) & (i2 == 2)) + 0.88 * ((i1 == 4) & (i2 == 3)) +
> 0.33 * ((i1 == 2) & (i2 ==4)) + 0.25 * (~b1 & b2)"
> where i* are integer variables, b* are booleans.
>
> At some point in my program I know the values for half the variables
> (i1, b1, ...), and I would like to simplify the expression by
> partially evaluating it.
> eg if i1 is 5 and b1 is False, I would like to get:
> "0.56 * (i2 == 2) + 0.25 * b2"
>
> I was hoping sympy would help my in this, however I stumbled on two problems:
>
> - the "==" is not supported by Sympy. From what I read, it seems to be
> on purpose and I should use "Eq(expr1, expr2)" instead... Ok, fine,
> but given that the expressions I get are written using ==, is there an
> easy way to transform "==" to Eq()? I was hoping to avoid writing a
> parser for those expressions... "sympify" does not help.
If you have something that can parse a find and replace regular expression, like sed or some text editors, you could just do find: \((.+) == (.+)\) replace: Eq\(\1, \2\) (you will need to use extended regular expressions, or else swap the '(' and '\(' ). That's how I would do it anyway.
>
> - while the boolean simplification work as I was hoping (in the
> example above, sympy gets the "0.25 * b2" part right), the equalities
> are not:
>
> In [31]: x = sympy.Symbol('x')
>
> In [32]: (0.5 * sympy.Eq(x, 5)).subs({'x': 5})
> Out[32]: 0.5*(5 == 5)
>
> I was hoping for just "0.5".
>
It works in SymPy 0.6.7:
In [3]: (0.5 * sympy.Eq(x, 5)).subs({'x': 5})
Out[3]: 0.500000000000000
but not in older versions. So maybe you just need to upgrade.
If things still do not work, you might try the bleeding edge, which is the git master. Just install git and type
git clone git://github.com/sympy/sympy.git
in a terminal in some directory where you want sympy (I am assuming you are using Linux or Mac OS X, for Windows, use msysgit). Then do
cd sympy
./bin/isympy
and try from there. You can do
git pull
to update the bleeding edge (see git tutorials online for more information).
Aaron Meurer