Is there a way to simplify AND/OR logical operators?
Let's say I have the following rule:
a AND (b AND/OR c)
Using the logical operators I will do as follow:
a & b & c | a & (b|c)
Can I do it so that I have something like a & (b &| c)?
Let's break it down into two parts.
a & b & c | a & (b|c) can be TRUE if either a & b & c (Part 1) is TRUE OR a & (b|c) (Part2) is TRUE.
which means if all of a, b and c are TRUE (Part 1) OR
a is TRUE and either of b or c is TRUE. (Part 2)
but if a and b is TRUE Part 2 is already TRUE so we don't really need c and part 1 then.
Hence, this ends up only with part 2 which is :
a & (b|c)
Related
I need to simplify this expression. I know the answer must be (Not A or Not C) but I keep getting C or (Not A and C)
Tried this in Lua:
local a = false
local b = true
local c = true
local f = (not b and not c) or (b and not c) or (not a and c)
local f_= not c or (not a and c)
print(f, f_)
Output: false, false
I also tried all the possibilities with all three variables and both 'f' and 'f_' remained identical.
F = (Not B and Not C) or (B and Not C) or (Not A and C)
-> (Not B and Not C) or (B and Not C) == Not C
F = Not C or (Not A and C)
I like using Karnaugh maps for boolean simplification:
https://en.wikipedia.org/wiki/Karnaugh_map
For your example, we build a 2D truth table:
Then fill in the terms from your question, they all get 'or'd together:
Then you find the smallest number of squares/rectangles that cover the needed parts. The squares and rectangles must have powers of two as a dimension, so 2x2 is ok, 1x4 etc, but not 3x2 for example. These are called 'minterms' and the bigger the square, the simpler the boolean expression they represent. In the example below, the minterm for 'not C' wraps off one end of the map and on to the other, but is still considered a 2x2 square.
You can also do it by covering the unused space with 'maxterms', and then invert it again to get the original expression:
The results of 'not A or not C' and 'not (A and C)' are equivalent by De Morgan's laws. (https://en.wikipedia.org/wiki/De_Morgan%27s_laws)
(Not B and Not C) or (B and Not C) or (Not A and C)
|
| Distributive Law
V
((Not B or B) and Not C) or (Not A and C)
|
| Complement Law
V
Not C or (Not A and C)
|
| Absorption Law
V
Not C or Not A
I am trying to formulate a constrain for my math model. the constrain goal is:
if A = 1 and B = 1 then C <= D
otherwise (A or B or both are 0) there is no constraint.
A and B are binary variables. C and D are integer numbers.
so far I was able to come up with this equation:
M(A - 1) - (B - 1) + C <= D (M is a big big number)
this formulation does not hold when A = 1 and B = 0
You could do this in two steps, first introduce a variable X representing logical and of A and B.
X >= A + B - 1
X <= A
X <= B
Then use X to express the inequality:
C - M(1-X) <= D
It appears that the logical NOT operator ! has non-intuitive order of operations in arithemtic:
set.seed(42)
a <- sample(100, 3)
b <- sample(100, 3)
c <- sample(100, 3)
l <- (1:3) <= 2
a * !l - b * !l + c
# 0 0 29
# same expression, but with parentheses for explicit grouping order of operations
(a * !l) - (b * !l) + c
# 74 14 43
There must be something I do not understand about the ! operator in relation to * or conversion from logical to numeric?
Note that in R, the negation operator ! will apply to entire expression to the right of the operator until it gets to the end or encounters an expression with a lower precedence. It does not just negate the most immediate term. Recall also that 0 is treated as FALSE and any other number is TRUE. So observe
!0
# [1] TRUE
!5
# [1] FALSE
!5-5
# [1] TRUE
!5-3-2
# [1] TRUE
(!5)-3-2
# [1] -5
So you see in the case of !5-3-2 the negation isn't happening until after the 5-3-2 is evaluated. Without the parenthesis, the negation is the very last thing that happens.
So when you write
a * !l - b * !l + c
that's the same as
a * !(l - (b * !(l + c)))
Because all the operations have to happen to the right of the negation before the negation can occur.
If you want to negate just the l terms, you can do
a * (!l) - b * (!l) + c
This is a function of the operator precedence in R (see the ?Syntax help page for details). It's once of the last operators to be evaluated in the given expression.
Note that & and | have a lower precedence than ! so when you do
!a | !b & !c
that's the same as
(!a) | ((!b) & (!c))
so this roughly would be what you expect if you just stick to logical operators. It just gets a bit odd perhaps when you combine logical and arithmetic operators.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
R: subset() logical-and operator for chaining conditions should be & not &&
What is the difference between short (&,|) and long (&&, ||) forms of AND, OR logical operators in R?
For example:
x==0 & y==1
x==0 && y==1
x==0 | y==1
x==0 || y==1
I always use the short forms in my code. Does it have any handicaps?
& and | - are element-wise and can be used with vector operations, whereas, || and && always generate single TRUE or FALSE
theck the difference:
> x <- 1:5
> y <- 5:1
> (x > 2) & (y < 3)
[1] FALSE FALSE FALSE TRUE TRUE
> (x > 2) && (y < 3) # here operaand && takes only 1'st elements from logical
# vectors (x>2) and (y<3)
> FALSE
So, && and || are commonly used in if (condition) state_1 else state_2 statements, as
dealing with vectors of length 1
a + b = c
c - a = b
Ok, now
a & b = c
c ?? a = b
which operator replace "??" ?
Thanks
There is no such operator, because it will be ill-defined if it exists:
so, let a = 0, c = 0
we have
a & 0 = c
a & 1 = c
then, we should have
c ?? a = 0 and c ?? a = 1
, but an operator/function cannot return two values given the same input/parameters.
It's impossible. This is because a & b is a lossy transformation.
You don't know whether any dropped 1 bits were part of a or b.
You can't.
0 && 0 == 0
1 && 0 == 0
To reverse this, you'd need an operator that gives back both 0 and 1.
(But if b == 1 you do of course know that a == c.)
No operator can replace "??" !
Only relation (logical relation !) b = (c=a)x2 + (not c)
can replace ""??".
J.Kawecki: Logical Relations. Warsaw,2019 Druk24h.pl