Simplification with karnaugh maps - karnaugh-map

I have 2 boolean equations, and I have to simplify them with karnaugh-maps, but I have a little problem understanding the procedure:
First, I got this one: out = (~a * b * ~c * d) + (~a * b * c * d). This reduces to ~a * b * d. The bit changes at c, so c is irrelevant, no problem.
Now the problem: b * ~a + a * b * ~c this reduces to ~a * b+b * ~c.
Here we have a bit change at c again, but ~c isn't irrelevant. Why?

Because there are two separate minterms, ~AB and B~C. Your first example only comprises of one minterm; your assumption about relevance only applies to individual minterms, not to the set of all minterms.

Related

PostGIS Intersection of multiple tables

In my postGIS DB, I need to create a new layer from the intersection of multiple polygons layers, while maintaining all fields (or Columns) from all layers (or tables) ==> in the output table, I need all columns for the 3 input tables
I believe it has to include ST_Intersects, but I am unable to find the correct syntax. Can you help me designing the SQL command line, knowing the following generic table names:
- TableA (with the columns: GeomA (geometry), ColumnA1, ColumnA2)
- TableB (with the columns: GeomB (geometry), ColumnB1)
- TableC (with the columns: GeomC (geometry), ColumnC1)
All geometry fields from TableA,TableB and TableC are in the same projection.
Thanks
For clarity, since "interaction with multiple polygons layers" is a bit vague, it could mean:
you want to find all polygons from A that intersect with a polygon from B and a polygon from C
or A with B, B with C
or A with B, A with C and B with C
For simplicity let us assume the first scenario, and I presume the others will be pretty easy to deduce:
select A.*, B.*, C.*
from A, B, C
where st_intersects(A.geomA, B.geomB) = true
and st_intersects(A.geomA, C.geomC) = true
[EDIT] Not just finding the rows, but if the intersection itself is important we can do the following (in the simple case of two geometries intersecting)
select A.*, B.*, st_intersection(A.geomA, B.geomB) as geomAB
from A, B
where st_intersects(A.geomA, B.geomB) = true
I simplified the case because if A intersects with B, and A intersects with C, it is not even sure those two intersections intersect again and have a result.
If we suppost A intersects with B, B with C and C with A then we should have a intersection imho. So that would look like:
select A.*, B.*, C.*, st_intersection(A.geomA, st_intersection(B.geomB, C.geomC)) as geomABC
from A, B, C
where st_intersects(A.geomA, B.geomB) = true
and st_intersects(A.geomA, C.geomC) = true
and st_intersects(B.geomB, C.geomC) = true

SQL - WHERE (X, Y) IN (A, B)

I have some kind of blockage currently.
My theoretic query looks something like this:
SELECT * FROM Table WHERE X in (a, b, c) AND Y IN (d, e, f)
So basically, I want all rows having multiple columns match, meaning:
X, Y
1, 2
3, 4
5, 6
7, 8,
9, 10
If I want to get all rows where (X=1, Y=2) or (X=5, Y=6), so X and Y are correlated, how would I do that?
(MS SQL2005+)
Why not something simple like the following?
WHERE (X = 1 AND Y = 2) OR (X = 5 AND Y = 6) ...
Or, if you're looking for rows (based on your example) where Y should be X + 1, then:
WHERE Y = X + 1
If you have thousands of OR clauses like the above, then I would suggest you populate a criterion table ahead of time, and rewrite your query as a join. Suppose you have such a table Criteria(X, Y) then your query becomes much simpler:
SELECT Table.*
FROM Table
INNER JOIN Criteria ON Table.X = Criteria.X AND Table.Y = Criteria.Y
Don't forget to add an index / foreign keys as necessary to the new table.
If for some reason you prefer to not create a table ahead of time, you can use a temporary table or table variable and populate it within your procedure.
If X and Y are in a table then a JOIN would be cleanest:
SELECT * FROM Table t
INNER JOIN XandY xy
WHERE tX = xy.X AND t.Y = xy.Y
If there not in a table I would strongly suggest putting them in one. IN only works with single-value sets and there's no way to line up results using multiple IN clauses.

How do I add a column, preserving the existing columns, without listing them all?

I want to add a new column to an alias, preserving all the existing ones.
A = foreach A generate
A.id as id,
A.date as date,
A.foo as foo,
A.bar as bar,
A.foo / A.bar as foobar;
Can I do that without listing all of them explicitly?
Yes, let's say you have an alias like:
A: {num1:int, num2:int}
and you want to calculate the sum while keeping num1 and num2. You can do this like:
B = FOREACH A GENERATE *, num1 + num2 AS num3:int ;
DESCRIBE B;
B: {num1:int, num2:int, num3:int}
Used like this, the * operator generates all fields.

SQLite3 query takes a lot of time, how would you do it?

I have a sqlite3 DB table of around 250 000 rows, my code is written in python. I need to filter it in very specific wat, and it takes much too long time.
Table is as follows:
self.cur.execute("""create table DetectedVehicles(IdD INTEGER PRIMARY KEY,
CLCode INT,
DetectionTime INT,
PlateNo VARCHAR)""")
It's a table for automatic plate number recognition result filtering.
And I need to filter it to get (native sql-like statements :) ):
Get rows from table DetectedVehicles where vehicles were observed at
CLCode="X" before they were observed at CLCode="Y".
(implicite: they were observed at both of them)
So I need to get list of detectedvehicles, that crossed specific CLCodes in proper sequence, i.e. Y before X.
I managed to create something that is working, but it takes about 10seconds for the query. Is there a faster way?
The code goes here:
self.cur.execute('select distinct PlateNo from DetectedVehicles where CLCode=? intersect select PlateNo from DetectedVehicles where CLCode=?',(CountLocationNo[0],CountLocationNo[1]))
PlatesTab=list(self.cur)
Results=[]
for Plate in PlatesTab:
PlateQ1='select * from DetectedVehicles where PlateNo in (?) and ((select DetectionTime from DetectedVehicles where CLCode = ? and PlateNo in (?) ) < (select DetectionTime from DetectedVehicles where CLCode = ? and PlateNo in (?)))'
R=list(self.cur.execute(PlateQ1,(Plate,CountLocationNo[0],Plate,CountLocationNo[1],Plate)))
if R:
TimesOD=self.curST2.execute('select DetectionTime from DetectedVehicles where PlateNo in (?) and (CLCode= ? or CLCode=?)',(Plate,CountLocationNo[0],CountLocationNo[1])).fetchall()
if TimesOD:
TravelTimes.append(TimesOD[1][0]-TimesOD[0][0])
DetectionTimes.append(TimesOD[0][0])
for i in R:
Results.append(i[0])
Results=tuple(Results)
QueryCL=' intersect select * from DetectedVehicles where IDd in ' + str(Results)
Thanks in advance
You can do it all in a single query.
select
dv1.PlateNo, dvPoint1.DetectionTime, dvPoint2.DetectionTime
from
DetectedVehicles dvPoint1
inner join DetectedVehicles dvPoint2
on dvPoint1.PlateNo = dvPoint2.PlateNo
and dvPoint1.CLCode = ? and dvPoint2.CLCode = ?
and dvPoint1.DetectionTime < dvPoint2.DetectionTime
You will want an index on (PlateNo, DetectionTime, CLCode), or (CLCode, PlateNo). Try them both to see which is faster. PlateNo on it's own may do.
Try:
select distinct x.*
from DetectedVehicles x
join DetectedVehicles y
on x.PlateNo = y.PlateNo and
x.DetectionTime < y.DetectionTime
where x.CLCode=? and y.CLCode=?
or:
select x.*
from DetectedVehicles x
where exists
(select 1
from DetectedVehicles y
where x.PlateNo = y.PlateNo and
x.DetectionTime < y.DetectionTime and
x.CLCode=? and y.CLCode=?)
I would normally expect the latter query to execute more quickly, but it would be worth running both to check.
Thank You guys for this feedback.
I post it as an answer, and present time results:
1. fastest total (query 1.80s, fetchall 0.20s, total: 2s)
select distinct x.*
from DetectedVehicles x
join DetectedVehicles y
on x.PlateNo = y.PlateNo and
x.DetectionTime < y.DetectionTime
where x.CLCode=? and y.CLCode=?
2. (query 1.83s, fetchall 0.19s, total: 2.02s)
select
dvPoint1.PlateNo, dvPoint1.DetectionTime, dvPoint2.DetectionTime
from
DetectedVehicles dvPoint1
inner join DetectedVehicles dvPoint2
on dvPoint1.PlateNo = dvPoint2.PlateNo
and dvPoint1.CLCode = ? and dvPoint2.CLCode = ?
and dvPoint1.DetectionTime < dvPoint2.DetectionTime
3. (query 1.82s, fetchall 1.09s, total: 2.91s)
select x.*
from DetectedVehicles x
where exists
(select 1
from DetectedVehicles y
where x.PlateNo = y.PlateNo and
x.DetectionTime < y.DetectionTime and
x.CLCode=? and y.CLCode=?)
So thanks #Mark Bannister for Your answer, I'm going to accept it.
However one issue remains:
cur.fetchall() takes enourmously long time.. and I need to get the results, how should I do it ? (For just a 100 of rows it takes around 2 minutes for each of your solutions).
Solved issue: download new sqlite.dll to your python/dlls folder ... don't ask me why: Join with Pythons sqlite module is slower than doing it manually

SQL. Calculation correlations of asset classes

I have a database with 101 simulations for, lets say, 5 different asset classes returns.
I need to write a query that will calculate the respective correlations between each of the 5 classes. Table will look something like this:
AssetClass_ID | Simulation | AssetClass_Value
Any ideas? I am struggling to get even close.
(Depending on difficulty I may end up having to tell the end user to just download all the simulations and do the stats using inbuilt EXCEL functions, but I am unlikely to be popular for doing so)
Ok, with some google and some work I came up with:
SELECT
AssetID_1, AssetID_2,
((psum - (sum1 * sum2 / n)) / sqrt((sum1sq - sum1*sum1 / n) * (sum2sq - sum2*sum2 / n))) AS [Correlation Coefficient],
n
FROM
(SELECT
n1.AssetClass_ID AS AssetID_1,
n2.AssetClass_ID AS AssetID_2,
SUM(n1.RunResults_Value) AS sum1,
SUM(n2.RunResults_Value) AS sum2,
SUM(n1.RunResults_Value * n1.RunResults_Value) AS sum1sq,
SUM(n2.RunResults_Value * n2.RunResults_Value) AS sum2sq,
SUM(n1.RunResults_Value * n2.RunResults_Value) AS psum,
COUNT(*) AS n
FROM
dbo.tbl_RunResults AS n1
LEFT JOIN dbo.tbl_RunResults AS n2 ON n1.Simulation_ID = n2.Simulation_ID
WHERE
n1.AssetClass_ID < n2.AssetClass_ID AND
n1.series_ID = 2332 AND
n2.series_ID = 2332
GROUP BY
n1.AssetClass_ID, n2.AssetClass_ID) AS step1
ORDER BY
AssetID_1
Answers match Excel inbuilt functions so far, so good.