nerdamer icon indicating copy to clipboard operation
nerdamer copied to clipboard

Solving Boolean formulas and piecewise expressions

Open jarble opened this issue 4 years ago • 8 comments

It seems that Nerdamer is not yet able to solve equations involving piecewise expressions or boolean formulas, but I see a relatively easy way to solve this problem. It is possible to solve some boolean formulas by rewriting them as equations, but I'm not sure if Nerdamer can do this yet.

For example, (a=2 or a=3) and (b=4 or b=a) could be written as (a-2)*(b-3) = 0,(b-4)*(b-a) = 0. This method only works when the expression is written in conjunctive normal form, so the expressions would need to be converted into this form in order to solve them.

Is Nerdamer currently able to solve Boolean formulas like this one?

jarble avatar Aug 12 '19 17:08 jarble

@jarble, unfortunately not. I'd like to consider this as something to add so if you'd like we can keep this discussion going. The timeframe of course depend on its complexity.

jiggzson avatar Aug 12 '19 20:08 jiggzson

@jiggzson It turns out that some Boolean expressions can be easily converted into a format that Nerdamer is able to solve. For example:

(a = 0) or (b = 0) or (c = 0) can be written as a*b*c = 0.

To solve more complicated Boolean expressions, you could convert the expression into conjunctive normal form, and then solve it as a system of equations.

This wouldn't handle formulas that include logical negation, but at least it would solve formulas involving disjunctions and conjunctions.

But it's easier to solve expressions that contain only Boolean variables, since they can be directly converted to polynomials.

jarble avatar Nov 15 '21 20:11 jarble

@jarble , how do you want to represent the solution (in case if there are many "points") ?

Yaffle avatar Nov 16 '21 11:11 Yaffle

@Yaffle The formula would be converted to a system of equations, and then solved using solveEquations. For example: (a = 1) and ((b = 2+a) or (b = 3+a)) could be solved as solveEquations([a-1, (b-(2+a))*(b-(3+a))]).

jarble avatar Nov 16 '21 17:11 jarble

@jarble , ok

nerdamer.solveEquations(['a+b=0']);

it is interesting, this thing throws an exeption right now. I have my own library, where I am also solving systems of equations, but may be it is better to add something to nerdamer.

Can and and or operators be added to nerdamer? Or is it better to write unitility function in javascript so the javascript API can be used for this?

Yaffle avatar Nov 16 '21 17:11 Yaffle

@Yaffle solveEquations seems to have a few bugs. I also see an error message when I use it to solve a single equation, like a+1=0. But it also shows a ParseError if the system does not have a distinct solution.

jarble avatar Nov 16 '21 17:11 jarble

@jarble , it is more simple if you have a single solution

Yaffle avatar Nov 16 '21 17:11 Yaffle

For example: (a = 1) and ((b = 2+a) or (b = 3+a)) could be solved as solveEquations([a-1, (b-(2+a))*(b-(3+a))]).

@jarble, I think this example conveys your approach the best. I do believe that @Yaffle is correct that the and and the or operator would need to implemented for this.

@Yaffle solveEquations seems to have a few bugs. I also see an error message when I use it to solve a single equation, like a+1=0.

This should have been fixed on the dev branch.

jiggzson avatar Nov 16 '21 19:11 jiggzson