Combine two boolean equations to separate a boolean variables from others - boolean

I have 2 programs: X and Y.
X has two bitmaps A and C.
X calls Y.
Y has a bitMap B.
The code I need to execute in Y is
if AB == B && BC == C
//do some action Z
I want to combine on A and C using boolean operations such that I can pass a single bitMap to Y. This single bitmap can then do any boolean operation with B to return true / false and accordingly I can decide to do action Z.

There is no way to simplify by combining A and C.
Here is our truth table:
B A C Z
0 0 0 1
0 0 1 0
0 1 0 1
0 1 1 0
1 0 0 0
1 0 1 0
1 1 0 1
1 1 1 1
We can see from the truth table that when B = 0, we have Z = not C; when B = 1, we have Z = A.
Suppose for the sake of contradiction that we have a one-bit function Y = f(A, C) that "summarizes" A and C. We use B to choose whether the summary equals 'not C' versus 'A'. But this is clearly impossible, because a single bit cannot preserve enough information to be able to extract the value 'not C' and also the value 'A'.

Related

Boolean Simplification - Q=A.B.(~B+C)+B.C+B

I've been struggling with boolean simplification in class, and took it to practice some more at home. I found a list of questions, but they don't have any answers or workings. This one I'm stuck on, if you could answer clearly showing each step I'd much appreciate:
Q=A.B.(~B+C)+B.C+B
I tried looking for a calculator to give me the answer and then to work out how to get to that, but I'm lost
(I'm new to this)
Edit: ~B = NOT B
I've never done this, so I'm using this site to help me.
A.B.(B' + C) = A.(B.B' + B.C) = A.(0 + B.C) = A.(B.C)
So the expression is now A.(B.C) + B.C + B.
Not sure about this, but I'm guessing A.(B.C) + (B.C) = (A + 1).(B.C).
This equals A.(B.C).
So the expression is now A.(B.C) + B.
As A.(B + C) = B.(A.C), the expression is now B.(A.C) + B, which equals (B + 1).(A.C) = B.(A.C).
NOTE: This isn't complete yet, so please avoid downvoting as I'm not finished yet (posted this to help the OP understand the first part).
Let's be lazy and use sympy, a Python library for symbolic computation.
>>> from sympy import *
>>> from sympy.logic import simplify_logic
>>> a, b, c = symbols('a, b, c')
>>> expr = a & b & (~b | c) | b & c | b # A.B.(~B+C)+B.C+B
>>> simplify_logic(expr)
b
There are two ways to go about such a formula:
Applying simplifications,
Brute force
Let's look at brute force first. The following is a dense truth table (for a better looking table, look at Wα), enumerating all possible value for a, b and c, alongside the values of the expression.
a b c -- a & b & (~b | c) | b & c | b = Q
0 0 0 0 0 10 1 0 0 0 0 0 = 0
0 0 1 0 0 10 1 1 0 0 1 0 = 0
0 1 0 0 1 01 0 0 1 0 0 1 = 1
0 1 1 0 1 01 1 1 1 1 1 1 = 1
1 0 0 1 0 10 1 0 0 0 0 0 = 0
1 0 1 1 0 10 1 1 0 0 1 0 = 0
1 1 0 1 1 01 1 0 1 0 0 1 = 1
1 1 1 1 1 01 1 1 1 1 1 1 = 1
You can also think of the expression as a tree, which will depend on the precedence rules (e.g. usually AND binds stronger than OR, see also this question on math.se).
So the expression:
a & b & (~b | c) | b & c | b
is a disjunction of three terms:
a & b & (~b | c)
b & c
b
You can try to reason about the individual terms, knowing that only one has to be true (as this is a disjunction).
The last two will be true, if and only if b is true. For the first, this a bit harder to see, but if you look closely: you have now a conjunction (terms concatenated by AND): All of them must be true, for the whole expression to be true, so a and b must be true. Especially b must be true.
In summary: For the whole expression to be true, in all three top-level cases, b must be true (and it will be false, if b is false). So it simplifies to just b.
Explore more on Wolfram Alpha:
https://www.wolframalpha.com/input/?i=a+%26+b+%26+(~b+%7C+c)+%7C+b+%26+c+%7C+b
A.B.(~B+C) + B.C + B = A.B.~B + A.B.C + B.C + B ; Distribution
= A.B.C + B.C + B ; Because B.~B = 0
= B.C + B ; Because A.B.C <= B.C
= B ; Because B.C <= B

Matlab how to find boundary submatrix

let a and b are sequence of k consecutive natural numbers a = (a_i) and b = (b_i). If A an n x n matrix, then A[a,b] (a nontrivial submatrix of A which is obtained from eliminating elements that doesnt belong in rows a and columns b) is called boundary submatrix if
i) a_1 = 1 or a_1 > 1 and A[(a_1 - 1),b] = 0
or
ii) b_1 = 1 or b_1 > 1 and A[a,(b_1 - 1)] = 0
(yes the matrix usually got some zero entries)
what is the easiest code to understand for this case?
I tried to make and array for the sequence using columnk but it seems difficult.
Example for matrix
B =
1 1 1 0
1 2 1 1
0 1 2 2
0 0 3 2
if we pick a = 2,3 and b = 3,4 we get
B(a,b) =
1 1
2 2
and the example of its boundary submatrices of B would be its every principal submatrix, and B([2,3],[2,3]) since B([2,3],1)=0

Looking for matrices in matlab

My problem is as follows:
I have the followings matrices:
0 1
1 1
or
1 1 1
1 1 1
0 1 0
or
1 1 1 0
1 1 1 1
0 1 1 0
And I want to get the following matrices:
0 1
2 1
or
1 1 1
1 1 1
0 2 0
or
1 1 1 0
1 1 1 2
0 3 3 0
What I want is to get the submatrices (or col-vector, or row-vector in case that submatrices are not possible) as large as possible.
I am going to explain clearly:
If the input is the third matrix:
1 1 1 0
1 1 1 1
0 1 1 0
I want to group elements vertically or horizontally, making these submatrices as large as possible. The largest submatrix in this example is:
x x x 0
x x x 1
0 1 1 0
Another possible submatrix, that is also largest is:
1 x x 0
1 x x 1
0 x x 0
Both represented by x.
Then, there are three elements with 1, yet. So I want to group again. Getting the largest submatrices (or sub-vector) again. At that point, depending on the previous move, we will get:
x x x 0
x x x 1
0 y y 0
or
y x x 0
y x x 1
0 x x 0
Represented by y.
Now, we have another element, that has not being grouped, and now we make another group (represented by z):
y x x 0
y x x z
0 x x 0
If now we choose another input, we have the following steps:
0 1
1 1
We have two sub-vector, so we have two possible solutions:
0 1
x x
or
0 x
1 x
And then, depending on the solution chosen, we have the following solution:
0 y
x x
or
0 x
y x
Grouping the other element alone.
Finally, in the second case, we only have one possible solution:
1 1 1
1 1 1
0 1 0
Getting the largest submatrix we have this solution:
x x x
x x x
0 1 0
And then, group the last element alone:
x x x
x x x
0 y 0
Thank you in advance.
It won't be fast, but brute forcing your way through would work (for small problems). Pseudo code like:
A = [1, 0; 1, 1];
B = zeros(size(A));
bCount = 1;
while any(A ~= 0)
globalmax = 0;
for i,j = 1 : sizeOfMatrix
localmax = 0;
for ii,jj = i,j : sizeOfMatrix
if ((ii-i+1) * (jj-j+1) > localmax ... &&
&& all(A(i:ii, j:jj) ~= 0))
localmax = (ii-i+1) * (jj-j+1);
localmaxPoints = [i, ii; j, jj];
end
end
if (localmax > globalmax)
globalmax = localmax;
globalmaxPoints = localmaxPoints;
end
end
A[globalmaxPoints] = 0;
B[globalmaxPoints] = bCount;
bCount = bCount+1;
end
Note this code won't work directly, but it should be easy to fix it.
Obviously the approach it is suitable only for small matrices - too slow for anything large. There are some small optimizations which are trivial, but won't change much.
You require something better to find optimum for a huge matrix. Unless you have some properties in these matrices you could use (for example zeros are only along the edges), you will have to use optimization techniques. You might not get the most optimal solution, but it will be a very good one.

2D matrix of matrices

I have two diagonal matrices. I am trying to build a larger block diagonal matrix from them. For example if I had this:
D = diag(zeros(3,1)+1)
D =
1 0 0
0 1 0
0 0 1
and...
E = diag(zeros(2,1)+2, -1) + diag(zeros(2,1)+2, +1) + diag(zeros(3,1)+4)
E =
4 2 0
2 4 2
0 2 4
I have an equation that says A*U = X
Where A is
[E D 0
D E D
0 D E]
This is for 3x3. 5x5 would look like this:
A =
[E D 0 0 0
D E D 0 0
0 D E D 0
0 0 D E D
0 0 0 D E]
A would be another diagonal matrix consisting of these matrices. I need to produce a 40x40 and it would take a VERY LONG TIME to do manually, of course.
How can I define that? I haven't figured out how to use blkdiag to construct.
I solved this on my own manually because I could never find a Matlab function to help me.
for n = 1:Distance_Resolution
A(((n-1)*Distance_Resolution +1):n*Distance_Resolution, ((n-1)*Distance_Resolution +1):n*Distance_Resolution) = A1;
if n == Distance_Resolution
else
A((n*Distance_Resolution+1):(n+1)*(Distance_Resolution), ((n-1)*Distance_Resolution+1:n*Distance_Resolution)) = A2;
A((n-1)*Distance_Resolution+1:n*Distance_Resolution, (n*Distance_Resolution+1):(n+1)*(Distance_Resolution)) = A2;
end
end
This will produce a block matrix that has the above specified demands and is of length Distance_Resolution x Distance_Resolution x Distance_Resolution. I defined A1 and A2 through help from above poster (Fo is just a constant here):
vector = zeros(Distance_Resolution,1) - Fo;
A2 = diag(vector);
A1 = toeplitz([1+4*Fo, -Fo, zeros(1,Distance_Resolution-2)]);
This is a workable code snippet, but I am still looking for a smarter way to code it.

matlab - creating a single dimensional array from two dimensional parts

hi i have the following situation
h = [0,1,1,1;
0,0,0,0;
1,1,1,1];
i'll check incoming values which can range between 0 and rowsize of h, i.e. in this case 2,. so my options are 0,1,2.
now i want to create a single dimensional array (let's name it j) as follows
whenever the incoming value is 0
j = [0,1,1,1]
next time if the incoming value is 1
then j = [0,1,1,1,0,0,0,0]
and so on... how is it possible to achieve this in matlab? thanks!
Matlab, as you know, indexes from 1 so you will need to add 1 to the index 0,1,2 to get the row identifier for h. So if the input is 'index'
j = h(index+1,:)
Then, for the next index
j = [j h(index+1,:)]
and so on.
Try this (with x as your vector of incoming values):
j = reshape(h(x+1,:).',1,[]);
The above uses x+1 as an index to select copies of the rows, then transposes and reshapes the result into a single row vector. Here's a test:
>> h = [0 1 1 1; 0 0 0 0; 1 1 1 1];
>> x = [0 0 0];
>> j = reshape(h(x+1,:).',1,[])
j =
0 1 1 1 0 1 1 1 0 1 1 1
If the incoming value is x, you could do something like:
g = h.'
j = g(1:(x+1)*size(h,2))