variable with binary values?

192 views
Skip to first unread message

Kevin Hunter

unread,
Jan 5, 2012, 2:40:23 AM1/5/12
to sy...@googlegroups.com
Hullo Sympy Group,

In my code, I'd like to be able to specify symbols with a binary type, something akin to:

>>> b = symbols('b', binary=True)

This would let b assume only values from the set {0, 1}.  I see the Boolean class from which the Symbol class derives, but I believe that's for boolean operations.  The symbols function appears to accept the binary keyword, but it appears to silently accept /any/ keyword.  What I'm currently doing for my uses is keeping track of this via an external object, but I'm hoping I can get the Sympy core to take note of it's status.

Is there a way to specify a variable as binary?

Thanks,

Kevin

Aaron Meurer

unread,
Jan 6, 2012, 6:59:01 AM1/6/12
to sy...@googlegroups.com
So you want to assume that b is a number modulo 2. To do this, you
can use the new Mod() object that was recently added. Note that this
was added after the latest release, so you'll have to use the git
version if you want to use it.

You define b = Symbol('b', integer=True), and use Mod(b, 2) everywhere
where you have b, and that will represent that it should be either 0
or 1. Note that this is still very new, and presently doesn't really
do much beyond evaluating with numbers. But more is planned, e.g.,
reduction of things like Mod(2*b, 2) to 0.

Another option that might work, depending on what you are doing, is to
use the new stats code, which is so new it hasn't even been merged yet
(see https://github.com/sympy/sympy/pull/902). There, you could do
something like b = Die(2, symbol='b'), and then b would represent a
random variable, with 50% probability of being 0 and 50% probability
of being 1. Again, though, it depends on what you are doing. With
this, you can easily compute things like expected values and
conditional probabilities, and (I believe), substitute in random
values.

So what exact things would you want to do with the object? I think the
best answer depends on what exactly you are wanting to do.

And by the way, Symbol() does indeed accept any "assumption". It
doesn't seem to do anything with them, though, other than put them in
.assumptions0 (it doesn't even automatically create .is_binary). I
think this is a bug, but it might be there for a reason. Does anyone
know if this is intentional? I created
http://code.google.com/p/sympy/issues/detail?id=2967 for this.

Aaron Meurer

> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/sympy/-/rrwCaYpeoyMJ.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to
> sympy+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sympy?hl=en.

Kevin Hunter

unread,
Jan 6, 2012, 1:07:52 PM1/6/12
to sy...@googlegroups.com
For this project, my end goal is to interface a series of Sympy equations that I've built to an optimization solver.  In the context of optimization, binary variables usually represent a decision, and in the context of solving, they represent a "branch" point.  Branch points make it expensive to find a solution because each branch is a basically a copy of the problem that must be individually solved, and most implementations keep /both/ sub problems in memory.  For large optimization problems (on the order of millions of variables and millions of equations), even a reduction of a single variable or equation can mean huge gains in solve-time.

I'm but human, and the equations I have written may have more room for symbolic simplification before I pass them to the solver; this is where I'm hoping Sympy can help.  I'll take a look at using the Mod() operation, perhaps in it's own pass of the system for each equation that has a binary variable.

If you're curious and don't already know about it, here's some "gentle reading" in regards to mathematical optimization.  Chapters 12 and specifically 13 would be of pertinence.


Thanks for the pointer to Mod().

Aaron Meurer

unread,
Jan 8, 2012, 4:49:10 AM1/8/12
to sy...@googlegroups.com
I see.

Here's something that might be useful to you then: in the git master,
we there is an option to simplify() that let's you use a custom
measure function. simplify() tries various types of simplifications,
and returns the expression that gives the smallest value according to
the measure function. By default, this is count_ops(), which gives
the number of operations in the expression. But you can easily create
a custom function that penalizes branch points (or anything). See the
docstring of simplify() for an example. The feature was added fairly
recently, and is intended for experimentation, so let us know how it
works for you if you decide to use it.

By the way, if all you want to do is keep track of the fact that the
Symbol is binary, and never really symbolically use this fact, it
would probably be better to just use some naming convention. As a
simple example, you could always make binary symbols start with the
letter b. You can easily check this (b.name.startswith('b')).
Depending on what your names are, you might choose something more
complicated, and use re to programmatically parse it out.

Mod is OK too, but it might clutter things up more, especially if you
have very large expressions. You also could just use Symbol('b',
binary=True). It won't create .is_binary, but you can easily check
.assumptions0 for it. Do note though that this last option is based
on internal APIs that might (will) change in the future.

Aaron Meurer

> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/sympy/-/WbtZDllULS0J.

Kevin Hunter

unread,
Jan 10, 2012, 10:53:08 PM1/10/12
to sy...@googlegroups.com
Thank you for your pointers.  I'll take them under advisement as I move forward with my project.

Nikolaus Sonnenschein

unread,
Nov 25, 2013, 10:42:49 AM11/25/13
to sy...@googlegroups.com
Hi Kevin,

Out of curiosity, did you get anywhere with 'interfacing' sympy equations with optimization solvers? I am currently working on a generic solver interface that uses sympy equations and variables for problem formulation. I wondered if I am reinventing the wheel.

Best,

Niko
Reply all
Reply to author
Forward
0 new messages