I can't figure out what the logical operator "not" (~/!), does in front of a parenthesis.
Here is the task:
I have a = 1, b = 2 and c = 2.
and this if statement:
if (a ~= b | a ~= c) &&~(a ~= b && a ~= c && b ~= c)
What does it mean having a logical operator "not" before the parenthesis (look at the bold ~)?
It plays the role of a logic-negation. It'll return the logical negation of (a ~= b && a ~= c && b ~= c). Meaning if (a ~= b && a ~= c && b ~= c) is true, then ~(a ~= b && a ~= c && b ~= c) is false. and vice versa.
not(A) is equivalent to ~A. The symbol ~ acts like the not(A) logical operator if A is a logical expression. The same symbol ~ placed before = acts like the ne(A) relational operator.
Matlab employs short-circuiting behavior when dealing with logical operators && and ||. Meaning that if we are evaluating e.g expr1 && expr2, then expr2 is not evaluated if expr1 is logical 0 (false). Same way happens when we evaluate e.g expr1 || expr2, expr2 is not evaluated if expr1 is logical 1 (true).
The symbols | and || perform different operations in a MATLAB application. The symbol | is an element-wise OR operator. The short-circuit OR operator is ||. meaning that if we have e.g expr1 || expr2, both expr1 and expr2 are evaluated. Same thing happens with the & element-wise AND operator.
Now, let's have a look at the expression you asked for:
if (a ~= b | a ~= c) &&~(a ~= b && a ~= c && b ~= c)
Knowing that: a = 1, b = 2 and c = 2.
expr1 = (a ~= b | a ~= c)
= (1 ~= 2 | 1 ~= 2)
= 1 (true)
expr1 is true, so Matlab proceeds to evaluate expr2:
expr2 = ~(a ~= b && a ~= c && b ~= c)
= ~(1 ~= 2 && a ~= c && b ~= c)
= ~(1 && 1 ~= 2 && b ~= c)
= ~(1 && 1 && 2 ~= 2)
= ~(1 && 1 && 0)
= ~(0)
= 1 (true)
So the final result of expr1 && expr2 is 1 (true).
Hope it's useful!
I'll try to complement the Academia's answer - the evaluation order is important and every logical operator has it's precedence. In Matlab the negation operator '~' not() has higher precedence than other logical operators. In this case the expression a ~= b && a ~= c && b ~= c will be evaluated before the negation operator as a whole because it was placed inside the parenthesis. This expression can be read as "all three variables have different values". By placing it inside the parenthesis and negating it, you get "not all three variables have different values". Without the parenthesis you'd get not(a) ~= b && a ~= c && b ~= c. Note also that comparison operators have higher precedence than &&.
Hope it helps.
Related
This is probably an easy answer, like no there are is no 'or' or 'and' operators in Minizinc, but I can't seem to find anything related to it on google.
In most programming languages you can do this:
boolean s = (a != 1 && b != 2);
But nowhere can I find a Minizinc version of this statement, like:
constraint a != 1 and b != 2;
Cheers
The disjunction (or) and conjunction (and) are written in MiniZinc as \/ and /\ respectively.
So your example should be written as
constraint a != 1 /\ b != 2;
See a little more here: https://www.minizinc.org/doc-2.5.5/en/modelling2.html?highlight=conjunction
Chanced upon this beautiful problem. Since I am new to Boolean expressions, it is looking quite difficult.
I guess parentheses can be used.
If one of A, B, C is true, A||B||C must be true. Using AND and NOT, it can be done but, how do we know which one has which value?
I tried using truth tables, but three variables were too much.
Any ideas on how to solve, or at least how to make it faster?
Learn De Morgan's laws. It's a little piece of a basic knowledge for a programmer.
They state, among others, that not(X or Y) = (not X) and (not Y).
If you negate both sides and then apply the formula twice—first to ((A or B) or C), treating the (A or B) subexpression as X, and then to (A or B) itself—you'll get the desired result:
A || B || C =
(A || B) || C =
!(!(A || B) && !C) =
!((!A || !B) && !C) =
!(!A && !B && !C)
DeMorgan's Law (one of them, anyway), normally applied to two variables, states that:
A or B == not (not A and not B)
But this works equally well for three (or more) variables:
A or B or C == not (not A and not B and not C)
This becomes obvious when you realise that A or B or C is true if any of them are true, the only way to get false if if all of them are false.
And, only if they're all false will not A and not B and not C give true (hence not(that) will give false). For confirmation, here's the table, where you'll see that the A or B or C and not(notA and notB and notC) columns give the same values:
A B C A or B or C notA notB notC not(notA and notB and notC)
----- ----------- -------------- ---------------------------
f f f f t t t f
f f t t t t f t
f t f t t f t t
f t t t t f f t
t f f t f t t t
t f t t f t f t
t t f t f f t t
t t t t f f f t
I'm just wondering if there's a difference between an if or a while statement if the condition is either (!a && !b) (let's call this statement 1) or !(a && b) (let's call this statement 2).
I was thinking about it, and we have four combinations of a and b possible, and I think that the condition would be different if a != b. I'm just hoping someone can check my logic.
If a and b are both true, then statements 1 and 2 are both false. If a and b are both false, then statements 1 and 2 are both true. ?However, if a is true and b is false, or the inverse, then statement 1 is false, but statement 2 is true. Is this correct?
You are actually asking about a fundamental law of Boolean Algebra: De Morgan's Laws are very useful to know when re-working or simplifying conditionals.
De Morgan's Laws
!(a && b) = (!a || !b)
!(a || b) = (!a && !b)
Your intuition that your statements 1 and 2 might not be equivalent is correct. Working through the four possibilities of a and b above shows that the actual equivalencies are those given by De Morgan's Laws.
Key: ∧ is logical AND (&&); ∨ is logical OR (||).
Image source: http://ndp.jct.ac.il/tutorials/mavomath/node15.html
!(a && b) = !a || !b this does not equal !a && !b. In the first case if a or b is false then the result is true. In the second case both a and b must be false for the result to be true.
So as you say they have different results when a != b.
I am trying to better understand boolean equivalence but this example has me a little stuck.
I am referring to this website: http://chortle.ccsu.edu/java5/Notes/chap40B/ch40B_9.html
It makes sense, but doesn't at the same time... it says that they are equivalent, but the true/false values don't add up/align in a way that makes them out to be equivalent as the table shows they are. Could someone explain this to me?
!(A && B) <-- first expression
(C || D) <-- second expression
The last columns refers to the equivalency of the two expressions, which yes, they are equivalent according to the table. However, I just don't get how the two expressions are equivalent. If A = F, B = F --> T, wouldn't C = F, D = F --> T as well?
A B C D
--------------------
F F T T T
F T T F T
T F F T T
T T F F F
You are confusing yourself when trying to reduce it from the actual expression to single letter variables. On referring the actual link, it would appear that the variables you use can be mapped to the original expressions as follows:
A = speed > 2000
B = memory > 512
C = speed <= 2000
D = memory <= 512
If you look at it, C equals !A and D equals !B. So the expression (C || D) is effectively !((!A) || (!B)). By De Morgan's Law, that is the same as !(A && B).
This table is explaining that !(A && B) is equivalent to !A || !B. The columns C and D appear to be defined as C = !A and D = !B. The last column is C || D
So A = F, B = F certainly implies !(A && B). In this case C = D = T, and so also C || D = T.
(E.G. In Perl) When either condition A or condition B have the same consequence
if (A){
# Consequence X
}elsif (B){
# Consequence X
}
we can write
if ( A || B ) {
# Consequence X
}
How about we have the following condition: Either when A and C are true, or B and C are true, consequence C follows.
This can be written very long:
if ( A && C){
# Consequence X
} elsif (B && C ){
# consequence X
}
My question is, is there any way to write this shorter?
Something like this:
if ( (A && C) || (B && C) )
is syntactically ok ???
Yes.
if ( A && C){
# Consequence X
} elsif (B && C ){
# consequence X
}
is the same as:
if ( (A && C) || (B && C) ){
#Consequence X
}
And this avoids evaluating C twice:
if ( (A || B) && C){
#Consequence X
}
BTW, this is more like a logical question, the logic here isn't limited to Perl.
Try the following:
if (C && (A || B)) {
}