I have the following logic:
if(!(A || B)) {}
How can this be simplified and how can this simplification be visualized?
A | B
-----
0 0
0 1 -
1 0 |- this is A OR B
1 1 -
A | B
-----
0 0 - This is !(A OR B) ?
0 1
1 0
1 1
The simplification !(A || B) <=> !A && !B (which is one of
De Morgan's laws, as noted by #JamesChoi) is best visualised
by observing that the truth value that accrues to the major
truth-functor in each expression is the same for all possible
distributions of truth values to the variables:
A | B | !(A || B) | !A && !B
---|-----|------------|----------
T | T | F(T T T) | FT F FT
T | F | F(T T F) | FT F TF
F | T | F(F T T) | TF F FT
F | F | T(F F F) | TF T TF
---------------------------------
^ ^
This shows that the expressions are truth-functionally equivalent. It is an
application of the truth-table method of propositional calculus.
The truth table for && is:
A | B | A && B
---|-----|-------
T | T | T T T
T | F | T F F
F | T | F F T
F | F | F F F
and the truth-table for || (inclusive-or) is:
A | B | A || B
---|-----|-------
T | T | T T T
T | F | T T F
F | T | F T T
F | F | F F F
The truth-table for ! must be self-evident.
Related
I have a boolean expression that looks like this
(!B and !C) or (B and !D) or (A and !C)
And I need to convert it so that it only has and operations. So I came up with this result
(B and C) and (!B and D) and (!A and C)
Is this correct or am I doing something wrong? I just want to make it sure.
I also know that
A or B
Is equilevant to
!(!A and !B)
If you apply your last expression to replace OR by AND, you can rewrite your Boolean expression to
!(!(!B and !C) and !(B and !D) and !(A and !C))
But the Boolean expression can be simplified to
!C or (B and !D)
Karnaugh map:
cd
00 01 11 10
+---+---+---+---+
00 | 1 | 1 | 0 | 0 |
+---+---+---+---+
01 | 1 | 1 | 0 | 1 |
ab +---+---+---+---+
11 | 1 | 1 | 0 | 1 |
+---+---+---+---+
10 | 1 | 1 | 0 | 0 |
+---+---+---+---+
This can be expressed as
!(C and !(B and !D))
The two expressions are not equivalent. The second one is the dual of the first one. The expression is already in the minimal Sum-of-Product form.
Suppose I have a query combining AND and OR conditions without parenthesis:
SELECT * FROM tbl1
WHERE a = 1 AND b = 2 OR c = 3;
How does PostgreSQL evaluate these conditions? Like (a = 1 AND b = 2) OR c = 3 or a = 1 AND (b = 2 OR c = 3). I couldn't find it anywhere in the documentation.
Note: I'm not purposefully writing an ambiguous query like this. I'm building a tool where the user could potentially create a query like that.
Note 2: If it makes any difference, I'm using PostgreSQL 9.6 in one instance and 11 in another.
AND is stronger than OR, so:
a AND b OR c == (a AND b) OR c
demo:db<>fiddle
a | b | c | a AND b OR c | (a AND b) OR c | a AND (b OR c)
:- | :- | :- | :----------- | :------------- | :-------
f | f | f | f | f | f
f | f | t | t | t | f
f | t | f | f | f | f
f | t | t | t | t | f
t | f | f | f | f | f
t | f | t | t | t | t
t | t | f | t | t | t
t | t | t | t | t | t
That, of course, means in your case:
a = 1 AND b = 2 OR c = 3 == (a = 1 AND b = 2) OR c = 3
This question already has an answer here:
Flattening Rows in Spark
(1 answer)
Closed 5 years ago.
I have a dataframe in spark like below and I want to convert all the column in different rows with respect to first column id.
+----------------------------------+
| id code1 code2 code3 code4 code5 |
+----------------------------------+
| 1 A B C D E |
| 1 M N O P Q |
| 1 P Q R S T |
| 2 P A C D F |
| 2 S D F R G |
+----------------------------------+
I want the output like below format
+-------------+
| id code |
+-------------+
| 1 A |
| 1 B |
| 1 C |
| 1 D |
| 1 E |
| 1 M |
| 1 N |
| 1 O |
| 1 P |
| 1 Q |
| 1 P |
| 1 Q |
| 1 R |
| 1 S |
| 1 T |
| 2 P |
| 2 A |
| 2 C |
| 2 D |
| 2 F |
| 2 S |
| 2 D |
| 2 F |
| 2 R |
| 2 G |
+-------------+
Can anyone please help me here how I will get the above output with spark and scala.
using array, explode and drop functions should have you the desired output as
df.withColumn("code", explode(array("code1", "code2", "code3", "code4", "code5")))
.drop("code1", "code2", "code3", "code4", "code5")
OR
as defined by undefined_variable, you can just use select
df.select($"id", explode(array("code1", "code2", "code3", "code4", "code5")).as("code"))
df.select(col("id"),explode(concat_ws(",",Seq(col(code1),col("code2"),col("code3"),col("code4"),col("code5")))))
Basically idea is first concat all required columns and then explode it
How does one automatically create a continuous chart over time based on data that is only available as each year?
For example, most data comes in the form of the following:
j | f | m | a | m | j | j | a | s | o | n | d |
year 1 x y
year 2 z
year 3
However, in order to create a chart than spans multiple years, I need the data transposed and combined as below:
year 1 | j x
| f y
| m
| a
| m
| j
| j
| a
| s
| o
| n
| d
year 2 | j
| f
| m z
| a
| m
| j
| j
| a
| s
| o
| n
| d
year 3 | j
| f
| m
| a
| m
| j
| j
| a
| s
| o
| n
| d
Is there a simple way to do this with pivot tables or something else?
Assuming your data is in Sheet1. In Sheet2 A2 put this:
=TRANSPOSE(SPLIT(ArrayFormula(JOIN(" , , , , , , , , , , , ,",FILTER(Sheet1!A2:A,Sheet1!A2:A <> ""))),","))
In Sheet2 B2 put:
=ArrayFormula(transpose(split(rept(join(",",transpose(Sheet1!B1:N1)),countA(Sheet1!A2:A)),",")))
These will expand if years are added.
To get the data, this will work, however, a query will need to be added for each additional year:
=transpose({query(Sheet1!B2:M2,"select *"),query(Sheet1!B3:M3,"select *"),query(Sheet1!B4:M4,"select *")})
I want to check wheather a column and a row is the same like this:
| | A | B | C |
-----------------
| A | X | 0 | 0 |
| B | 0 | X | 0 |
| C | 0 | 0 | X |
If I use the following formula:
#TBLFM: #<<$<<..#>$> = if ($1==#1,X,0)
then I get the following:
| | A | B | C |
-----------------
| A | X | A = B ? X : 0 | A = C ? X : 0 |
| B | B = A ? X : 0 | X | B = C ? X : 0 |
| C | C = A ? X : 0 | C = B ? X : 0 | X |
Any ideas whats going wrong?
Your formula is comparing symbols, so A==A is always true. But the result for A==B is the whole symbolic expression.
Adding quotes to your row/column headers treats them as strings:
| | A | B | C |
|---+---+---+---|
| A | X | 0 | 0 |
| B | 0 | X | 0 |
| C | 0 | 0 | X |
#+TBLFM: #<<$<<..#>$> = if ("$1"=="#1",X,0)