tags:

views:

524

answers:

6

How can I implement XOR using basic mathematical operators like +,-,*,/

Update: Actually, I need to track change in two matrix having Boolean values. This can be done using XORing each value with corresponding value in other matrix. But, Lp_Solve library doesn't support XOR operation. Also, it accepts only linear equations.

+7  A: 

(a-b)*(a-b) works, there must be a much better way though.

1,1 = 0
0,1 = 1
1,0 = 1
0,0 = 0 
glebm
I thought about this. This will not be a linear equation. Can you think of something in linear form?
Mayur
@Mayur Look at the shape the points (0,0,0), (1,0,1), (0,1,1), (1,1,0) make in a 3-D space. That's the graph of Xor. There is no linear function of `x` and `y` that passes through all these points, because they are not on a same plane.
Pascal Cuoq
+6  A: 

The simplest expression I can come up with is: a != b.

(Previous best effort was (a + b) == 1)

Paul R
Duh. (nothing more to say)
Konrad Rudolph
@Paul: often, an expression such as `a != b` isn’t possible in those solvers (didn’t check with this particular one). Therefore, your previous best answer is probably still the best, if equations (instead of just inequations) can be entered.
Konrad Rudolph
@Konrad: thanks, yes, I wasn't sure what operators were permissible so I left the previous suggestion as a possible alternative. It would help if the OP had specified exactly which operators are available...
Paul R
+3  A: 

Can you do something like:

(a + b) % 2
Alexander Vakrilov
A: 

Exclusive-OR is a linear function, but the definition of 'linear' with regards to a boolean function is not the same as with a polynomial function. You will have to look through the documentation for your lp_solve library to see if it is capable of handling linear boolean functions. From what I have read, I don't suspect that it can.

Edit: After looking further into the simplex algorithm that lp_solve uses, I'm fairly certain that you can't do what you are trying to do.

bta
A: 

abs(A+B-1). if it doesn't do abs, then (A+B-1)*(A+B-1) should do it.

Jason
(1,1) ---|1+1-1|//(1+1-1)^2---> 1, it should be 0. this wont work.
nlucaroni
+2  A: 

Weellllllllllll........

It's not quite as simple as that.

To model a XOR (let's call it X), we start with the logic.

X = (A & !B) | (!A & B)

In math, the above can be written as:

X = A*(1-B) + B*(1-A)

But the above expression is nonlinear (due to the bilinear terms -- to preserve linearity, we are not allowed to multiply variables with each other).

But! Because we are allowed to use constraints, we can rewrite the above expression in linear form.

First, we expand the terms:

X = A*(1-B) + B*(1-A) = A + B - 2*A*B

Now we need to take care of the A*B term (which essentially means A & B). Let a variable H represent the logical condition A & B. We can now write the AND condition as follows: (see cited reference PDF below)

H <= A
H <= B
H >= A + B - 1
H >= 0

Linear XOR Formulation

Finally, let's put everything together. This is your XOR formulation, using only linear constraints.

X = A + B - 2*H
H <= A
H <= B
H >= A + B - 1
H >= 0

I know it looks complicated (for a simple operation like a XOR). There may be a more compact formulation.

But in general, writing logical conditions in a linear programming context is complicated because one is usually severely restricted in the operations one can perform -- in order to avoid destroying the theoretical properties of the problem.

Reference

See here for a list of standard integer formulations for representing logic linearly. http://brblog.typepad.com/files/mipformref-1.pdf


Edit:

Explanation on how the H constraints model the "AND" logical condition. Essentially, in an LP, we pose inequality constraints that have to be satisfied at the solution point -- what we're doing here is playing a trick to "squeeze" H to the right value. For instance, given the tuple (A,B) = (0,0), the constraints for H would be:

H <= 0
H <= 0
H >= -1
H >= 0

In the above case, the only value H can take is 0, because H belongs in the interval [0,0]. Hence we get (A,B) = (0,0) => H = 0.

Let's try another example, (A,B) = (1,1).

H <= 1
H <= 1
H >= 1
H >= 0

From the above, you will immediately see that 1 <= H <= 1 implies that H = 1. We get (A,B) = (1,1) => H = 1.

And so on. You'll see that the H constraints model the "AND" condition exactly.

Gilead
+1 for `X = A*(1-B) + B*(1-A)`; it's more efficient than `X = (a-b)(a-b)`, but I'm confused about how Mayur can use the `H AND condition` Interesting read though!
JohnB
Thanks! I've added a mathematical explanation above.Essentially, one would add the H constraints to the problem, and LPsolve will account for them when solving the problem. This is more of a problem in mathematics than in computer science. If this optimization problem were to be solved as a Nonlinear Program (NLP) instead of a Linear Program (LP), the X = A*(1-B) + B*(1-A) would have worked just fine. It's when one has to come up with a linear formulation that some mathematical acrobatics is required.
Gilead