Combining logical operators - boolean

I have an expression of the form
A or A and B
Can we represent it more concisely by representing the expression some other way?

As stated the expression may be slightly ambiguous. It can be interpereted in two ways:
(A or A) and B
Obviously A or A is logically equivalent to A, so in this case the entire statement is simply equivalent to A and B
More likely, this is intended to be read as
A or (A and B)
Let's write a truth table for this
A B | A or (A and B) | result
-----------------------------
0 0 | 0 or (0 and 0) | 0
0 1 | 0 or (0 and 1) | 0
1 0 | 1 or (1 and 0) | 1
1 1 | 1 or (1 and 1) | 1
Now you can pretty clearly see, in this case the statement is equivalent to A alone.

Related

two-variable output truth table for two-variable input

I have 2-bit variable that is needed to be converted in another one. I made such a table
i1 i2 | o1 o2
0 0 | x x
0 1 | 0 1
1 0 | 1 0
1 1 | 0 1
But I cannot figure out how to do it except something like
(o1(i1,i2)&0b01 << 1) | (o2(i1,i2) & 0b01)

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

Extending adjacency matrix neighbours

I have an adjacency matrix. For example, the following,
+---+-------------------------------+
| | 1 2 3 4 5 |
+---+-------------------------------+
| 1 | 0 1 0 0 0 |
| 2 | 1 0 0 0 1 |
| 3 | 0 0 0 1 0 |
| 4 | 0 0 1 0 1 |
| 5 | 0 1 0 1 0 |
+---+-------------------------------+
how can we extract the following adjacency matrix, without for loops, where for each element (row or column) the neighbors of the already existed neighbors were added? For example, the element 3 has neighbor the element 4 so in the new adjacency matrix the element 3 will have neighbors the elements 4 and 5.
+---+-------------------------------+
| | 1 2 3 4 5 |
+---+-------------------------------+
| 1 | 0 1 0 0 1 |
| 2 | 1 0 0 1 1 |
| 3 | 0 0 0 1 1 |
| 4 | 0 1 1 0 1 |
| 5 | 1 1 1 1 0 |
+---+-------------------------------+
Best regards,
Thoth.
If A is your adjacency matrix, then the matrix you want is A2, where:
A2 = (A+A^2) > 0
This is because the square of an adjacency matrix has components s_ij, where s_ij is the number of paths of length two between i and j. (In fact (A^n)_ij is the number of paths from i to j of length n).
Therefore, if you add A (which contains all pairs joined by a path of length 1) to A^2 which contains all pairs linked by a path of length two, you'll get the number of paths of length 1 or 2. (And we only care if this is positive for this case). Paths of length two are paths to the neighbours of neighbours.
You might want to set the diagonal back to zero though

Scala for-comprehension if filtering too much?

I have the following program: (Scala 2.9.2, Java6)
object Forcomp {
def main(args: Array[String]): Unit = {
val xs = List(-1, 0, 1)
val xss = for (a <- xs; b <- xs if a != 0 && b != 0) yield (a,b)
println(xss)
}
}
It produces this output: List((-1,-1), (-1,1), (1,-1), (1,1))
I would have expected it to only filter out values where a and b are both 0 – not all values where either a or b are 0.
I can get the behaviour I want by changing the if-clause to this: if (a,b) != (0,0) – however, should I really have to? Is this a bug or is this intentional behaviour? I, for one, was surprised by this.
The truth table for the filter you have is this:
a==0 | b==0 | (a!=0 && b!=0)
--------------------------------
0 | 0 | 0
0 | 1 | 0
1 | 0 | 0
1 | 1 | 1
whereas the behaviour you say you want is:
a==0 | b==0 | !(a==0 && b==0)
--------------------------------
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 1

Can all boolean expressions be written sequentially?

I'm working with some tools, and the only way it can determine if a particular transaction is successful is if it passes various checks. However, it is limited in the way that it can only do one check at a time, and it must be sequential. Everything must be computed from left to right.
For example,
A || C && D
It will be computed with A || C first, and then the result will be AND'ed with D.
It gets tougher with parenthesis. I am unable to compute an expression like this, since B || C would need to be compututed first. I cannot work with any order of operations;
A && ( B || C)
I think I've worked this down to this sequential boolean expression,
C || B && A
Where C || B is computed first, then that result is AND'd with A
Can all boolean expressions be successfully worked into a sequential boolean expression? (Like the example I have)
The answer is no:
Consider A || B && C || D which has the truth table:
A | B | C | D |
0 | 0 | 0 | 0 | 0
0 | 0 | 0 | 1 | 0
0 | 0 | 1 | 0 | 0
0 | 0 | 1 | 1 | 0
0 | 1 | 0 | 0 | 0
0 | 1 | 0 | 1 | 1
0 | 1 | 1 | 0 | 1
0 | 1 | 1 | 1 | 1
1 | 0 | 0 | 0 | 0
1 | 0 | 0 | 1 | 1
1 | 0 | 1 | 0 | 1
1 | 0 | 1 | 1 | 1
1 | 1 | 0 | 0 | 0
1 | 1 | 0 | 1 | 1
1 | 1 | 1 | 0 | 1
1 | 1 | 1 | 1 | 1
If it were possible to evaluate sequentially there would have to be a last expression which would be one of two cases:
Case 1:
X || Y such that Y is one of A,B,C,D and X is any sequential boolean expression.
Now, since there is no variable in A,B,C,D where the entire expression is true whenever that variable is true, none of:
X || A
X || B
X || C
X || D
can possibly be the last operation in the expression (for any X).
Case 2:
X && Y: such that Y is one of A,B,C,D and X is any sequential boolean expression.
Now, since there is no variable in A,B,C,D where the entire expression is false whenever that variable is false, none of:
X && A
X && B
X && C
X && D
can possibly be the last operation in the expression (for any X).
Therefore you cannot write (A || B) && (C || D) in this way.
The reason you are able to do this for some expressions, like: A && ( B || C) becoming C || B && A is because that expression can be built recursively out of expressions which have one of the two properties above:
IE.
The truth table for A && ( B || C) is:
A | B | C |
0 | 0 | 0 | 0
0 | 0 | 1 | 0
0 | 1 | 0 | 0
0 | 1 | 1 | 0
1 | 0 | 0 | 0
1 | 0 | 1 | 1
1 | 1 | 0 | 1
1 | 1 | 1 | 1
Which we can quickly see has the property that it is false whenever A is 0. So Our expression Could be X && A.
Then we take A out of the truth table and look at only the rows where A is 1 is the original:
B | C
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 1
Which has the property that it is True whenever B is 1 (or C, we can pick here). So we can write the expression as
X || B and the entire expression becomes X || B && A
Then we reduce the table again to the portion where B was 0 and we get:
C
0 | 0
1 | 1
X is just C. So the final expression is C || B && A
This is a problem of rewriting an expression so that no parentheses occur on the right. Logical AND (∧) and OR (∨) are both commutative:
A ∧ B = B ∧ A
A ∨ B = B ∨ A
So you can rewrite any expression of the form “X a (Y)” as “(Y) a X”:
A ∧ (B ∧ C) = (A ∧ B) ∧ C
A ∧ (B ∨ C) = (B ∨ C) ∧ A
A ∨ (B ∧ C) = (B ∧ C) ∨ A
A ∨ (B ∨ C) = (B ∨ C) ∨ A
They are also distributive, by the following laws:
(A ∧ B) ∨ (A ∧ C)
= A ∧ (B ∨ C)
= (B ∨ C) ∧ A
(A ∨ B) ∧ (A ∨ C)
= A ∨ (B ∧ C)
= (B ∧ C) ∨ A
So many Boolean expressions can be rewritten without parentheses on the right. But, as a counter­example, there is no way to rewrite an expression such as (A ∧ B) ∨ (C ∧ D) if A ≠ C, because of the lack of common factors.
Can't you just do this:
(( A || C ) && D)
and for your second example:
((( A && C ) || B ) && A )
Would that work for you?
Hope that helps...
You'll hit problems if you need to do something like (A || B) && (C || D) unless you can store the intermediate values for later use.
If you're allowed to construct more than one chain and try them all until either one of them passes or they all fail (so each chain is effectively ORed with the next) then I should think you can handle any combination. The example above would become (where each line is a separate query):
(A && C) ||
(A && D) ||
(B && C) ||
(B && D)
However, for a very complex check this could easily get out of hand!