Boolean Expression Evaluation For ExNor Gate - boolean

I know that an xnor expression can be broken up as follows:
X xnor Y = X'Y' + XY
But but I know that sume of the complement of the same combination (x+x') is 1 always therefore shouldn't xnor be always equal to 1?
I got confused in this question when I was trying to solve the following problem:
Z=X*Y where * denotes xnor. Evaluate Z*X.
My answer was 1, which according to the provided solution is incorrect the correct solution was given Y.
How? Thanks for the help.

Related

Returning indices of variables in a three-dimensional matrix satisfying a condition

I am trying to find the value of 3 variables that satisfy a condition, namely that their sum is less than or equal to 1. My approach is to use ndgrid to sweep over all combinations of the variables and define a matrix I that contains a 1 if the condition is satisfied. My code is below
ss=0.25;
[pp1,pp2,pp3] = ndgrid(0:ss:1,0:ss:1,0:ss:1);
I = pp1+pp2+pp3<=1
My question is, how do I generate a list of all valid variable combinations? I wish to have a 3 x n vector p that contains all n valid values for pp1,pp2, and pp3.
I've found a solution which I'll post as an answer. It is simply
p = [pp1(I) pp2(I) pp3(I)]
If anyone has a better solution I would appreciate any comments.

Non-element-wise vector division in Matlab returns a scalar! What is it?

I spent over an hour chasing a bug in my code which was leading to a precision error. It turned out that in one of my equations, I had forgotten to divide two vectors element-wise; I had written / instead of ./. Usually Matlab gives an error in these cases, e.g. if you try to multiply two vectors with * instead of .*. But in this case it's happily returning a scalar value! Is this supposed to happen, and does this value have any meaning?
For example,
x = 0 : 0.01 : 1;
y = x/exp(x);
sets y=0.3132.
Yes, this is supposed to happen. You used the matrix right division operator /, and in this particular case it found a scalar value of y that solved the following system of equations in a least-squares sense:
y*exp(x) = x;

Double sqrt solution in Matlab?

I would like to know how can I get both the positive and the negative solution from a sqrt in Matlab.
For example if I have:
sin(a) = sqrt(1-cos(a)^2);
The docs don't say anything specific about always only providing the positive square root but it does seem like a fair assumption in which case you can get the negative square pretty easily like this:
p = sqrt(1-cos(a)^2);
n = -sqrt(1-cos(a)^2);
btw assigning to sin(a) like that is going to create a variable called sin which will hide the sin function leading to many possible errors, so I would highly recommend choosing a different variable name.
MATLAB (and every other programming language that I know of) only returns the principal square root of x when calling sqrt(x) or equivalent.
How you'd write the square root of x mathematically, is
s = ±√x
which is just a shorthand for writing the whole solution set
s = {+√x -√x}
In MATLAB, you'd write it the same as this last case, but with slightly different syntax,
s = [+sqrt(x) -sqrt(x)]
which can be computed more efficiently if you "factor out" the sqrt:
s = sqrt(x) * [1 -1]
So, for your case,
s = sqrt(1-cos(a)^2) * [1 -1]
or, if you so desire,
s = sin(acos(a)) * [1 -1]
which is a tad slower, but perhaps more readable (and actually a bit more accurate as well).
Now of course, if you can somehow find the components whose quotient results in the value of your cosine, then you wouldn't have to deal with all this messy business of course....
sqrt does not solve equations, only gives numerical output. You will need to formulate your equation as you need it, and then you can use sqrt(...) -1*sqrt(...) to give your positive and negative outputs.

Can a SAT solver be used to find all solutions?

I wrote an answer to what I thought was a quite interesting question, but unfortunately the question was deleted by its author before I could post. I'm reposting a summary of the question and my answer here in case it might be of use to anyone else.
Suppose I have a SAT solver that, given a Boolean formula in conjunctive normal form, returns either a solution (a variable assignment that satisfies the formula) or the information that the problem is unsatisfiable.
Can I use this solver to find all the solutions?
There is definitely a way to use the SAT solver you described to find all the solutions of a SAT problem, although it may not be the most efficient way.
Just use the solver to find a solution to your original problem, add a clause that does nothing except rule out the solution you just found, use the solver to find a solution to the new problem, and so forth. Keep going until you get a problem that's unsatisfiable.
For example, suppose you want to satisfy (X or Y) and (X or Z). There are five solutions:
Four with X true, Y and Z arbitrary.
One with X false, Y and Z true.
So you run your solver, and let's say it gives you the solution (X, Y, Z) = (T, F, F). You can rule out this solution---and only this solution---with the constraint
not (X and (not Y) and (not Z))
This constraint can be rewritten as the clause
(not X) or Y or Z
So now you can run your solver on the new problem
(X or Y) and (X or Z) and ((not X) or Y or Z)
and so forth.
Like I said, this is a way to do what you want, but it probably isn't the most efficient way. When your SAT solver is looking for a solution, it learns a lot about the problem, but it doesn't return all that information to you---it just gives you the solution it found. When you run the solver again, it has to re-learn all the information that was thrown away.
Sure it can. When MiniSat[1] finds a solution
s SATISFIABLE
v 1 2 -3 0
(solution 1=True, 2=True, 3=False) then you have to put into the original CNF[2] a clause that bans this solution:
-1 -2 3 0
(which means, either 1 or 2 must be False or 3 must be True). Then you solve again. You do this until the solver returns UNSAT i.e. that there are no more solutions to the problem. You will insert one clause for each iteration, and each clause will have the same format as the solution except that it's all inverted and has a 0 at the end
It's much faster to do this using the C++ interface of MiniSat, as it can then save intermediate data and the iterations will be faster.
[1] http://minisat.se/
[2] http://fairmut3x.wordpress.com/2011/07/29/cnf-conjunctive-normal-form-dimacs-format-explained/

Understanding colon notation in MATLAB

So I'm completely new to MATLAB and I'm trying to understand colon notation within mathematical operations. So, in this book I found this statement:
w(1:5)=j(1:5) + k(1:5);
I do not understand what it really does. I know that w(1:5) is pretty much iterating through the w array from index 1 through 5, but in the statement above, shouldn't all indexes of w be equal to j(5) + k(5) in the end? Or am I completely wrong on how this works? It'd be awesome if someone posted the equivalent in Java to that up there. Thanks in advance :-)
I am pretty sure this means
"The first 5 elements of w shall be the first 5 elements of j + the first 5 elements of k" (I am not sure if matlab arrays start with 0 or 1 though)
So:
w1 = j1+k1
w2 = j2+k2
w3 = j3+k3
w4 = j4+k4
w5 = j5+k5
Think "Vector addition" here.
w(1:5)=j(1:5) + k(1:5);
is the same that:
for i=1:5
w(i)=j(i)+k(i);
end
MATLAB uses vectors and matrices, and is heavily optimized to handle operations on them efficiently.
The expression w(1:5) means a vector consisting of the first 5 elements of w; the expression you posted adds two 5 element vectors (the first 5 elements of j and k) and assigns the result to the first five elements of w.
I think your problem comes from the way how do you call this statement. It is not an iteration, but rather simple assignment. Now we only need to understand what was assigned to what.
I will assume j,k, w are all vectors 1 by N.
j(1:5) - means elements from 1 to 5 of the vector j
j(1:5) + k(1:5) - will result in elementwise sum of both operands
w(1:5) = ... - will assign the result again elementwise to w
Writing your code using colon notation makes it less verbose and more efficient. So it is highly recommended to do so. Also, colon notation is the basic and very powerful feature of MATLAB. Make sure you understand it well, before you move on. MATLAB is very well documented so you can read on this topic here.