CNF Simplification Algorithm - boolean-logic

Given that a boolean expression is in conjunctive normal form: is there a "simple" algorithm to simplify it while keeping it in CNF?
In particular, what property of the following expression causes this simplifiation?
(~a+b+c)(a+~b+c)(a+~c)
simplifies to ...
(~a+b+c)(a+~b)(a+~c)

The Karnaugh map of your example is:
To get a simplified DNF, '1' cells are grouped to get a cover with the minimum number of minterms.
Similarly, one can group the '0' cells to get an inverse cover with the minimum number of terms.
The inverse map:
The literals of the resulting terms have to be inverted to arrive at the desired minimum CNF
(a + ~b) (a + ~c) (~a + b + c)
The procedure makes use of the fact that the inverse of a minterm is a maxterm (commonly called CNF clause) with inverted literals.

Related

Plot power series gnuplot

I'd like to know how to plot power series (whose variable is x), but I don't even know where to start with.
I know it might not be possible plot infinite series, but it'd do as well plotting the sum of the first n terms.
Gnuplot has a sum function, which can be used inside the using statement to sum up several columns or terms. Together with the special file name + you can implement power series.
Consider the exponention function, which has a power series
\sum_{n=0}^\infty x^n/n!
So, we define a term as
term(x, n) = x**n/n!
Now we can plot the power series up to the n=5 term with
set xrange [0:4]
term(x, n) = x**n/n!
set samples 20
plot '+' using 1:(sum [n=0:5] term($1, n))
To plot the results when using 2 to 7 terms and compare it with the actual exp function, use
term(x, n) = x**n/n!
set xrange [-2:2]
set samples 41
set key left
plot exp(x), for [i=1:6] '+' using 1:(sum[t=0:i] term($1, t)) title sprintf('%d terms', i)
The easiest way that I can think of is to generate a file that has a column of x-values and a column of f(x) values, then just plot the table like you would any other data. A power series is continuous, so you can just connect the dots and have a fairly accurate representation (provided your dots are close enough together). Also, when evaluating f(x), you just sum up the first N terms (where N is big enough). Big enough means that the sum of the rest of the terms is smaller than whatever error you allow. (*If you want 3 good digits, then N needs to be large enough that the remaining sum is smaller than .001.)
You can pull out a calc II textbook to determine how to bound the error on the tail of the sum. A lot of calc classes briefly cover it, but students tend to feel like the error estimates are pointless (I know because I've taught the course a few times.) As an example, if you have an alternating series (whose terms are decreasing in absolute value), then the absolute value of the first term you omit (don't sum) is an upperbound on your error.
*This statement is not 100% true, it is slightly over simplified, but is correct for most practical purposes.

Compute real roots of a quadratic equation in Pascal

I am trying to solve this problem :
(Write a program to compute the real roots of a quadratic equation (ax2 + bx + c = 0). The roots can be calculated using the following formulae:
x1 = (-b + sqrt(b2 - 4ac))/2a
and
x2 = (-b - sqrt(b2 - 4ac))/2a
I wrote the following code, but its not correct:
program week7_lab2_a1;
var a,b,c,i:integer;
x,x1,x2:real;
begin
write('Enter the value of a :');
readln(a);
write('Enter the value of b :');
readln(b);
write('Enter the value of c :');
readln(c);
if (sqr(b)-4*a*c)>=0 then
begin
if ((a>0) and (b>0)) then
begin
x1:=(-1*b+sqrt(sqr(b)-4*a*c))/2*a;
x2:=(-1*b-sqrt(sqr(b)-4*a*c))/2*a;
writeln('x1=',x1:0:2);
writeln('x2=',x2:0:2);
end
else
if ((a=0) and (b=0)) then
write('The is no solution')
else
if ((a=0) and (b<>0)) then
begin
x:=-1*c/b;
write('The only root :',x:0:2);
end;
end
else
if (sqr(b)-4*a*c)<0 then
write('The is no real root');
readln;
end.
do you know why?
and taking a=-6,b=7,c=8 .. can you desk-check it after writing the pesudocode?
You have an operator precedence error here:
x1:=(-1*b+sqrt(sqr(b)-4*a*c))/2*a;
x2:=(-1*b-sqrt(sqr(b)-4*a*c))/2*a;
See at the end, the 2 * a doesn't do what you think it does. It does divide the expression by 2, but then multiplies it by a, because of precedence rules. This is what you want:
x1:=(-1*b+sqrt(sqr(b)-4*a*c))/(2*a);
x2:=(-1*b-sqrt(sqr(b)-4*a*c))/(2*a);
In fact, this is because the expression is evaluated left-to-right wrt brackets and that multiplication and division have the same priority. So basically, once it's divided by 2, it says "I'm done with division, I will multiply what I have now with a as told".
As it doesn't really seem clear from the formula you were given, this is the quadratic formula:
As you can see you need to divide by 2a, so you must use brackets here to make it work properly, just as the correct text-only expression for this equation is x = (-b +- sqrt(b^2 - 4ac)) / (2a).
Otherwise the code looks fine, if somewhat convoluted (for instance, you could discard cases where (a = 0) and (b = 0) right after input, which would simplify the logic a bit later on). Did you really mean to exclude negative coefficients though, or just zero coefficients? You should check that.
Also be careful with floating-point equality comparison - it works fine with 0, but will usually not work with most constants, so use an epsilon instead if you need to check if one value is equal to another (like such: abs(a - b) < 1e-6)
Completely agree with what Thomas said in his answer. Just want to add some optimization marks:
You check the discriminant value in if-statement, and then use it again:
if (sqr(b)-4*a*c)>=0 then
...
x1:=(-1*b+sqrt(sqr(b)-4*a*c))/2*a;
x2:=(-1*b-sqrt(sqr(b)-4*a*c))/2*a;
This not quite efficient - instead of evaluating discriminant value at once you compute it multiple times. You should first compute discriminant value and store it into some variable:
D := sqr(b)-4*a*c;
and after that you can use your evaluated value in all expressions, like this:
if (D >= 0) then
...
x1:=(-b+sqrt(D)/(2*a);
x2:=(-b-sqrt(D)/(2*a);
and so on.
Also, I wouldn't write -1*b... Instead of this just use -b or 0-b in worst case, but not multiplication. Multiplication here is not needed.
EDIT:
One more note:
Your code:
if (sqr(b)-4*a*c)>=0 then
begin
...
end
else
if (sqr(b)-4*a*c)<0 then
write('The is no real root');
You here double check the if-condition. I simplify this:
if (a) then
begin ... end
else
if (not a)
...
Where you check for not a (in your code it corresponds to (sqr(b)-4*a*c)<0) - in this case condition can be only false (for a) and there is no need to double check it. You should just throw it out.

distributive property for product of maxterms

I am unsure how to use the Distributive property on the following function:
F = B'D + A'D + BD
I understand that F = xy + x'z would become (xy + x')(xy + z) but I'm not sure how to do this with three terms with two variables.
Also another small question:
I was wondering how to know what number a minterm is without having to consult (or memorise) the table of minterms.
For example how can I tell that xy'z' is m4?
When you're trying to use the distributive property there, what you're doing is converting minterms to maxterms. This is actually very related to your second question.
To tell that xy'z' is m4, think of function as binary where false is 0 and true is 1. xy'z' then becomes 100, binary for the decimal 4. That's really what a k-map/minterm table is doing for you to give a number.
Now an important extension of this: the number of possible combinations is 2^number of different variables. If you have 3 variables, there are 2^3 or 8 different combinations. That means you have min/maxterm possible numbers from 0-7. Here's the cool part: anything that isn't a minterm is a maxterm, and vice versa.
So, if you have variables x and y, and you have the expression xy', you can see that as 10, or m2. Because the numbers go from 0-3 with 2 variables, m2 implies M0, M1, and M3. Therefore, xy'=(x+y)(x+y')(x'+y').
In other words, the easiest way to do the distributive property in either direction is to note what minterm or maxterm you're dealing with, and just switch it to the other.
For more info/different wording.

Inner Functions With SML NJ

I am a complete newbie to sml and am having trouble with the syntax for inner functions. What I need to do is take a list of a list of ints, average each list, and return a list of reals. This is the psuedo-ish code I have so far.
fun listAvg [] = 0
else (sum (x) div size (x))
fun sum[] = 0
| sum(head::rest)= head + sum rest;
fun size [] = 0
| size(head::rest) = 1 + size rest;
listAvg([[1,3,6,8,9], [4,2,6,5,1], [9,5,9,7], [5,4], [3,6,4,8]]);
any advice would be greatly appreciated. Thanks!
Use a let, as in
fun listAvg [] = 0
| listAvg x =
let
fun sum[] = 0
| sum(head::tail)= head + sum tail;
fun size [] = 0
| size(head::tail) = 1 + size tail;
in
(sum x) div (size x)
end
You have to pass an int list to this function e.g.
listAvg [1, 2, 3, 4];
This is no change to your code, except to rearrange the order and putting the keywords let, in, and end. If this is not homework, I would recommend using a few built-in standard library functions in the List structure that could reduce this function to two lines, including the pattern match on the empty list.
EDIT:
There are two possible meanings to "average a list of a list of ints." The first is to average each list and then take the average of the averages, and the second is to join the lists together into one long list and take the average over the entire list. The two methods are equivalent (except for rounding errors) when all the lists of ints are of the same length, but as your example shows, they don't have to be the same length.
Since this is homework, I'm not going to give you the answer directly, but consider the following, which might be helpful:
If you're using the first interpretation of "average a list of a list of ints:" there is a built-in SML function that will let you apply another function to each element of a list, and get the resulting list. This might be helpful for getting the individual averages, which you can then combine together into an overall average.
If you're using the second interpretation: there is a built-in SML function (it looks like an operator, but many of the things that look like operators in SML are just infix functions) to join two lists together, and a built-in SML function to apply a function to elements going down the list, along with an accumulator value, to generate a single value. You might be able to use those two functions to create one long list of all the numbers, which you can then average.

Is there any boolean algebra expression that can not be put into 3SAT?

This seems to me pretty obvious, There is not but I might be leaving a special case.
As I see it 1SAT (only one literal per clause) and 2SAT can be easily transformed into 3SAT.
An any clause with more than 3 literas has been proven it can be transformed into 3SAT.
So maybe the question should be asked as:
Do all boolean algebra can be put into SAT? or
can we define boolean algebra with ony these operators? AND OR and NOT
No, there is not.
I will not give the full proof but here is the main idea: Write the given formula in a normal form i.e. conjunction of disjunctions. Use induction on the number of variables on an expression. Pick the longest subexpression with n+1 variables, introduce a new variable for some part of subexpression to leave an expression of n variables, add the constraints for the new variable to the formula, repeat the procedure as many times as needed to have a formula where the longest subexpression has n variables.