How was this boolean expression further simplified? - language-agnostic

(ab+cd)(a'b'+c'd') = 1+ abc'd' + a'b'cd +1
so I'm stuck at
abc'd'+a'b'cd
but the final answer is
(a+b)(c+d)+(a'+b')(c'+d')
What am I missing?

It seems to me that those two expressions are complementary, i.e. the only two cases where (a+b)(c+d)+(a'+b')(c'+d') are false are abc'd' and a'b'cd.
Edit: Somewhere along the line I think you've lost a ' and you're actually looking for one of these:
((ab+cd)(a'b'+c'd'))'
(ab+cd)'+(a'b'+c'd')'
((ab)'(cd)')+((a'b')'(c'd')')
(a'+b')(c'+d')+(a+b)(c+d)
(a+b)(c+d)+(a'+b')(c'+d')
(ab+cd)(a'b'+c'd')
(a'b'+c'd')(ab+cd)
((a+b)'+(c+d)')((a'+b')'+(c'+d')')
((a+b)(c+d))'((a'+b')(c'+d'))'
((a+b)(c+d)+(a'+b')(c'+d'))'

you cannot prove that (ab+cd)(a'b'+c'd') = (a+b)(c+d)+(a'+b')(c'+d') because it is not true.
take a=b=1, c=d=0:
(ab+cd)(a'b'+c'd') = (1+0)(0+1) = 1
but
(a+b)(c+d)+(a'+b')(c'+d') = (1*0)+(0*1) = 0
(assuming x' is "not")

Related

Django: ORM/SQL query speed significantly decreased after adding additional BooleanField or (SQL tinyint) to Django Filter

Using MySQL Latest Django:
I have a vaguely complex Django query that works quite quickly--until I add an additional "AND" with a Boolean Field--
See Below:
queriedForms = queryFormtype.form_set.filter(is_public=True)
newQuery = queriedForms.filter(formrecordattributevalue__record_value__icontains=term['TVAL'], formrecordattributevalue__record_attribute_type__pk=rtypePK)
newQuery = newQuery.filter(flagged_for_deletion=False)
logger.info(newQuery.query)
term['count'] = newQuery.count()
If I either remove the initial "is_public=True" or the final "flagged_for_deletion=False)--it works incredibly fast. If I use both as filters, it increases the time for the count() function by something like 2000%
The different QuerySet.query outputs are below:
SELECT `maqluengine_form`.`id`, `maqluengine_form`.`form_name`, `maqluengine_form`.`form_number`, `maqluengine_form`.`form_geojson_string`, `maqluengine_form`.`hierarchy_parent_id`, `maqluengine_form`.`is_public`, `maqluengine_form`.`project_id`, `maqluengine_form`.`date_created`, `maqluengine_form`.`created_by_id`, `maqluengine_form`.`date_last_modified`, `maqluengine_form`.`modified_by_id`, `maqluengine_form`.`sort_index`, `maqluengine_form`.`form_type_id`, `maqluengine_form`.`flagged_for_deletion` FROM `maqluengine_form` INNER JOIN `maqluengine_formrecordattributevalue` ON (`maqluengine_form`.`id` = `maqluengine_formrecordattributevalue`.`form_parent_id`) WHERE (`maqluengine_form`.`form_type_id` = 319 AND `maqluengine_form`.`is_public` = True AND `maqluengine_formrecordattributevalue`.`record_value` LIKE %seal% AND `maqluengine_formrecordattributevalue`.`record_attribute_type_id` = 18510 AND `maqluengine_form`.`flagged_for_deletion` = False)
SELECT `maqluengine_form`.`id`, `maqluengine_form`.`form_name`, `maqluengine_form`.`form_number`, `maqluengine_form`.`form_geojson_string`, `maqluengine_form`.`hierarchy_parent_id`, `maqluengine_form`.`is_public`, `maqluengine_form`.`project_id`, `maqluengine_form`.`date_created`, `maqluengine_form`.`created_by_id`, `maqluengine_form`.`date_last_modified`, `maqluengine_form`.`modified_by_id`, `maqluengine_form`.`sort_index`, `maqluengine_form`.`form_type_id`, `maqluengine_form`.`flagged_for_deletion` FROM `maqluengine_form` INNER JOIN `maqluengine_formrecordattributevalue` ON (`maqluengine_form`.`id` = `maqluengine_formrecordattributevalue`.`form_parent_id`) WHERE (`maqluengine_form`.`form_type_id` = 319 AND `maqluengine_form`.`is_public` = True AND `maqluengine_formrecordattributevalue`.`record_value` LIKE %seal% AND `maqluengine_formrecordattributevalue`.`record_attribute_type_id` = 18510)
The first takes about 20/30 seconds to perform the count(), while the second with only 1 of the two BooleanField's takes less than a second to perform the count()
=======================================
EDIT=======================
Apologies: since the question isn't obvious enough--why is adding an additional AND with a BooleanField increasing the query time by +2000%? Is anyone able to assist in figuring out WHY that's occurring. Thanks.
EDIT=========================
Also discovered that using a exclude(is_public=False) rather than filter(is_public=True) has the same effect as the solution below. Does anyone happen to know why an exclude() works fine--whereas the filter() does not?
==============================
Solution I came up with after a night's rest:
--I keep the query as is(I need it for later because it continues getting chain filtered)
--I need the count() from this stage--which is taking substantially longer than it should with the additional BooleanField AND
--I take a temporary values list to perform a len() on instead:
queriedForms = queryFormtype.form_set.all()
newQuery = queriedForms.filter(formrecordattributevalue__record_value__icontains=term['TVAL'], formrecordattributevalue__record_attribute_type__pk=rtypePK)
newQuery = newQuery.filter(flagged_for_deletion=False)
tempQuery = newQuery.values_list('is_public',flat=True)
finalQuery = [entry for entry in tempQuery if entry != 'False'] #Remove any indices that contain "False"
term['count'] = len(finalQuery)
The following counts that use chained filters after use the same technique--it's significantly faster--if not as fast as removing one of the Booleans from the filters.

Using a variable in a SQLAlchemy filter

I want to use a variable like this :
myVariable = 0.5
myQuery.filter(myTable.column1 == myVariable*myTable.column2)
I have then no results when I apply all() to myQuery.
If I replace the variable with its value, it is OK.
queryBidOffer = session.query(BidOffer.id, BidOffer.price, BidOffer.qmin, BidOffer.qmax)
queryBidOffer = queryBidOffer.join(Equipment).filter(BidOffer.equipment==Equipment.id, Equipment.equipment_type.in_(['L','P','W','M']))
queryBidOffer_day = queryBidOffer.filter(BidOffer.day == day)
queryBidOffer_hour = queryBidOffer_day.filter(BidOffer.start_hour == timeSlice)
queryBidOffer_hour = queryBidOffer_hour.join(EquipmentDayHour, BidOffer.equipment == EquipmentDayHour.equipment)
queryBidOffer_hour = queryBidOffer_hour.filter(EquipmentDayHour.day == day)
queryBidOffer_hour = queryBidOffer_hour.filter(EquipmentDayHour.hour == timeSlice)
factor1 = 1.00 - 0.07
queryBidOffer_hour = queryBidOffer_hour.filter(BidOffer.equipment == EquipmentDayHour.equipment, factor1*func.abs(EquipmentDayHour.ref_prog) == 930)
The problem is with the two last lines (factor1).
In the last line, if I replace factor1 by its value, it is OK.
IEEE floating point numbers as used by Python, and many databases do not always work as your school math. The reason why 0.93 worked was that that was the bitwisely exact value that you had originally stored in database. But slight error appears in the subtraction.
See this for example:
>>> print "%0.64f" % (1.0 - 0.07)
0.9299999999999999378275106209912337362766265869140625000000000000
>>> print "%0.64f" % (0.93)
0.9300000000000000488498130835068877786397933959960937500000000000
Read more here How should I do floating point comparison and then for very in depth essay you could read What Every Computer Scientist Should Know About Floating-Point Arithmetic...
One solution could be to use decimal.Decimal in Python and Numeric in SQL.
I've just tested out something similar to the code you are describing, but get results whether I use a variable or a literal:
myVariable = 0.5
myQuery.filter(myTable.column1 == myVariable*myTable.column2).all()
vs.
myQuery.filter(myTable.column1 == 0.5*myTable.column2).all()
Can you post up the literal SQL that is being genarated? i.e. the results of
print myQuery
for both of those?

Lua - How do I use a function from another script?

I've been looking around and I have not been able to find anything that has worked for me. I'm starting to learn more Lua and to start off I'm making a simple calculator. I was able to get each individual operation onto separate programs, but when I try to combine them I just can't get it to work. My script as it is now is
require "io"
require "operations.lua"
do
print ("Please enter the first number in your problem.")
x = io.read()
print ("Please enter the second number in your problem.")
y = io.read()
print ("Please choose the operation you wish to perform.")
print ("Use 1 for addition, 2 for subtraction, 3 for multiplication, and 4 for division.")
op = io.read()
op = 1 then
function addition
op = 2 then
function subtraction
op = 3 then
function multiplication
op = 4 then
function division
print (answer)
io.read()
end
and my operations.lua script is
function addition
return answer = x+y
end
function subtraction
return answer = x-y
end
function multiplication
return answer = x*y
end
function division
return answer = x/y
end
I've tried using
if op = 1 then
answer = x+y
print(answer)
if op = 2 then
answer = x-y
print(answer)
and I did that completing each operation. But it doesn't work. I can't even get the error code that it's returning because it closes so fast. What should I do?
In your example, make these changes: You require operations.lua without the extension. Include parameters in your operations function definitions. Return the operation expression directly versus returning a statement like answer = x+y.
All together:
Code for operations.lua
function addition(x,y)
return x + y
end
--more functions go here...
function division(x,y)
return x / y
end
Code for your hosting Lua script:
require "operations"
result = addition(5,7)
print(result)
result = division(9,3)
print(result)
Once you get that working, try re-adding your io logic.
Keep in mind that as it's coded, your functions will be defined globally. To avoid polluting the global table, consider defining operations.lua as a module. Take a look at the lua-users.org Modules Tutorial.
The right if-then-else syntax:
if op==1 then
answer = a+b
elseif op==2 then
answer = a*b
end
print(answer)
After: please check the correct function-declaration syntax.
After: return answer=x+y is incorrect. If you want set answer's value, set without return. If you want return the sum, please use return x+y.
And I think you should check Programming in Lua.
First of all, learn to use the command line so you can see the errors (on Windows that would be cmd.exe).
Second, change the second line to require("operations"). The way you did it the interpreter expects a directory operations with an underlying script lua.lua.

Regexp for mysql to detect one '=' and not two '=='

I want to detect the following:
window.location.href = "http://newlocation.com";
but not
window.location.href == "http://newlocation.com";
the query is ran in mySQL like so:
select "blablabla" REGEXP "bla"
Can't seem to get my head around this one.
SELECT 'window.location.href="http://newlocation.com"' REGEXP "href=\""
This gives me 1 (TRUE)
That one also
SELECT 'window.location.href="http://newlocation.com"' REGEXP "window\.location\.href=\"http:\/\/newlocation\.com\""
It depends what you want to match really.
There are two ways:
SELECT 'window.location.href = "http://www.google.com/"' REGEXP '[[:<:]]=[[:>:]]';
OR
SELECT 'window.location.href = "http://www.google.com/"' REGEXP ' = ';
Please note that both the above assume that there will be a space before and after the equals sign. If there might be no spaces before or after equals, you can try:
SELECT 'window.location.href = "http://www.google.com/"' REGEXP '[^=]+=[^=]+';
I'm not sure about the last one but it should work.
Hope it helps!

access fields same but not equal?

i'm using the following code to compare two recordsets:
For i = 1 To (recordsetA.Fields.Count - 1)
If recordsetA.Fields(i).Value <> recordsetB.Fields(i).Value Then
stringFieldList = stringFieldList & ", " & recordsetA.Fields(i).Name
End If
Next i
However in the stringFieldList there are a couple of fields which have the same values (like 1339.5). Why?
Since it sounds like your dealing with double datatypes, the proper way for the test would be to set a limit, then test the absolute difference. You will also need to think about handling a null value.
Const epsilon as double = 0.00001
If Abs(recordsetA.Fields(i).Value - recordsetB.Fields(i).Value) < epsilon Then
'do stuff here
End If