How to shorten the boolean function - boolean-operations

I have the following function:
f(x) = (x2 + x1x3x5)(x4 + x3x5x6)(x5 + x6)
How can I make the expression like:
f(x) = x1x2x3 + x2x3x4 + ...
out of this? Is there any method?
I'm not sure if SO is the right place to post this...I guess it's not, but still, I found the tag and around 100 posts with it, so here I am :P

As far as I remember the boolean algebra you can just multiply them as ordinary numbers.
So (x2 + x1x3x5)(x5 + x6) will be x2x5 + x2x6 + x1x3x5x5 + x1x3x5x6. But again as far as I remember this applied only to "AND" not to "OR"

Well this looks more like a math question.
But if I understand what you want correctly it would look like this
x2x4x5+x2x4x6+x2x3x5x6x5+x2x3x5x6x6+x1x3x5x4 ext.
basically
(a1+a2)a3=a1a3+a2a3
(a1+a2)(a3+a4)=a1a3+a1a4+a2a3+a3a4

Here you go:
(x2x4x5 + x1x3x5)(x4x6) + (x2x5 + x1x3x5)(x3x5x6)
(x2x4x5x6 + x1x3x4x5x6) + (x2x5 + x1x3x5)(x3x5x6)
x2x4x5x6 + x1x3x4x5x6 + x2x3x5x6 + x1x3x5x6
x2x4x5x6 + x1x3(x4x5x6 + x5x6) + x2x3x5x6
x2x5x6(x4+1) + x1x3(x4x5x6 + x5x6)
x2x4x5x6 + x1x3(x5x6(x4+1))
x2x4x5x6 + x1x3(x4x5x6)
x2x4x5x6 + x1x3x4x5x6
(x2+x1x3)x4x5x6
I probably made a mistake somewhere, so you should test it first.

Your original:
f(x) = (x2 + x1x3x5)(x4 + x3x5x6)(x5 + x6)
Now using some simple maths:
f(x) = (x2x4 + x2x3x5x6 + x1x3x5x4 + x1x3x5x3x5x6)(x5 + x6)
f(x) = x2x4x5 + x2x3x5x6x5 + x1x3x5x4x5 + x1x3x5x3x5x6x5 + x2x4x6 + x2x3x5x6x6 + x1x3x5x4x6 + x1x3x5x3x5x6x6
Simplifying gets us the answer (though not necessarily shorter),
f(x) = x2x4x5 + x2x3x5x6 + x1x3x5x4x5 + x1x3x5x6 + x2x4x6 + x2x3x5x6 + x1x3x5x4x6 + x1x3x5x6

Related

How could it be true that T(n) = θ(1) + 2T(n/2) + θ(n) + θ(1) = 2T(n/2) + θ(n)?

I have a question about Time Complexity of this pseudo code.
enter image description here
as following,
T(n) = θ(1) + 2T(n/2) + θ(n) + θ(1)
= 2T(n/2) + θ(n)
Can you explain me why these two 'θ(1)' are ignored in second equation?
Is it because meaningless comparing to 'θ(n)'?
They are constant and not going to change based on the value of n

Subtract in SQL Server code returning NULL

I'm having problems in this SQL server code, even using the ISNULL or COALESCE functions it keeps returning NULL, could someone tell me how to solve it?
CASE WHEN TIPO_X = 'F'
AND TIPO_CALCULOX IN ('N','S','D')
AND TRPR_COD_X IN (7,13,15,17) THEN CASE WHEN EVEN_COD_X IN (623,70623,947,70947,1871,71871,2697,72697,2871,72871,30623,30947,31871,22697,22871,20623,20947,21871) THEN CONVERT(int, (SELECT SUM(VLR_X)
FROM GMS_RELATORIO_GERAL
WHERE CHAPA_X = MATRÍCULA_DO_SEGURADO
AND EVEN_COD_X IN (623 + 7062 + 947 + 70947 + 1871 + 71871 + 2697 + 72697 + 2871 + 72871 + 30623 + 30947 + 31871)
AND MONTH(DT_COMP_X) = #MES)) - CONVERT(int,(SELECT SUM(VLR_X)
FROM GMS_RELATORIO_GERAL
WHERE CHAPA_X = MATRÍCULA_DO_SEGURADO
AND EVEN_COD_X IN (22697 + 22871 + 20623 + 20947 + 21871)
AND MONTH(DT_COMP_X) = #MES))
END
ELSE ''
END AS RENUMERAÇÃO_DA_CONTRIBUIÇÃO
Somehow I doubt this part of the code is as intended:
AND EVEN_COD_X IN (623 + 7062 + 947 + 70947 + 1871 + 71871 + 2697 + 72697 + 2871 + 72871 + 30623 + 30947 + 31871)
It's equivalent to this:
AND EVEN_COD_X IN (397898)
You probably want commas there instead.
Among your three EVEN_COD_X IN clauses, the last two are wrong : values should be separated with a comma (,).
CASE WHEN EVEN_COD_X IN (623 + 7062) -- It should be (623, 7062)
A good practice is :
to simplify your code (use CTE or temporary tables instead of doing everything in one query) ;
to use indentation (mistakes like this will become more visible) ;
for debugging, simplify it even more until it works.

How to convert sympy expression which include integral and derivative to numpy

It may be normal to convert sympy expression to numpy, But my equation has Integral, Derivation on the output of the sympy. I tried lambdify but it gives me "name 'Integral' is not defined" Now, I get stuck, any help will be appreciated.
def plpwfun(w,b):
plpw=-pi**2*(-w*exp(b + w)/(exp(b + w) + 1)**2 + (2*b + w)*exp(b + w)/(exp(b + w) + 1) - log(exp(b) + 1) + log(exp(b + w) + 1) + 1/(exp(b + w) + 1) - 1/(exp(b) + 1)) + 0.5*Integral(2*((-re(w**2*exp(2*b + 2*w*x)/(exp(b + w*x) + 1)**2) + re(w**2*exp(b + w*x)/(exp(b + w*x) + 1)))*(-Derivative(re(w**2*exp(2*b + 2*w*x)/(exp(b + w*x) + 1)**2), w) + Derivative(re(w**2*exp(b + w*x)/(exp(b + w*x) + 1)), w)) + (-im(w**2*exp(2*b + 2*w*x)/(exp(b + w*x) + 1)**2) + im(w**2*exp(b + w*x)/(exp(b + w*x) + 1)))*(-Derivative(im(w**2*exp(2*b + 2*w*x)/(exp(b + w*x) + 1)**2), w) + Derivative(im(w**2*exp(b + w*x)/(exp(b + w*x) + 1)), w)))*Abs(w**2*exp(b + w*x)/(exp(b + w*x) + 1) - w**2*exp(2*b + 2*w*x)/(exp(b + w*x) + 1)**2)*sign(w**2*exp(b + w*x)/(exp(b + w*x) + 1) - w**2*exp(2*b + 2*w*x)/(exp(b + w*x) + 1)**2)/(w**2*exp(b + w*x)/(exp(b + w*x) + 1) - w**2*exp(2*b + 2*w*x)/(exp(b + w*x) + 1)**2), (x, 0, 1))
return plpw
print(plpwfun(0,0))
I use the sympy output to be my gradient function(plpw,pwpb), but I can't convert them to numpy and calculate. Is there any ways I could solve it?
Can you try with the newly released sympy 1.7?
pip install sympy --upgrade
Confirm by running
import sympy
print(sympy.__version__)
It includes a change to support Integral with lambdify: https://github.com/sympy/sympy/pull/20134
You'll probably want to have scipy installed as well (so the function generated by lambdify will use scipy's quad or nquad otherwise mpmath will be required to run the generated function as a fallback).
For example this will work in 1.7
from sympy import *
x,y = symbols("x y")
i = Integral(x, (x, 0, y))
f = lambdify([y], i)
print(f(3)) # 4.5
If this doesn't work, could you can edit your question to show self contained code with the actual sympy Integral you're trying to lambdify?

sql Update column with sum other columns data

I want to update with sum full datatable data row wise but how?
See what want I do:
This code only for shown sum data with row-wise but its not working with NULL value.
select
sum(tc6671 + tf6671 + pc6671 + pf6671 + tc8572 + tf8572 + pc8572 + pf8572 +
tc6672 + tf6672 + tc6673 + tf6673 + pc6673 + pf6673 + pc6674 + pf6674 + tc5852 + tf5852 +
tc5853 + tf5853) as Test
from
cmt_7th;
But I want to update my specific column with all sum data.
I was tried this code but failed:
update cmt_7th
set roll = sum(tc6671+tf6671+pc6671+pf6671+tc8572+tf8572+pc8572+pf8572+
tc6672+tf6672+tc6673+tf6673+pc6673+pf6673+
pc6674+pf6674+tc5852+tf5852+tc5853+tf5853);
And I was tried also by this code:
UPDATE c
SET c.total = c.tc6671+c.tf6671+c.pc6671+c.pf6671+c.tc8572+c.tf8572+c.pc8572+
c.pf8572+c.tc6672+c.tf6672+c.tc6673+c.tf6673+c.pc6673+c.pf6673+
c.pc6674+c.pf6674+c.tc5852+c.tf5852+c.tc5853+c.tf5853
FROM cmt_7th c
Instead of tc6671+tf6671, use coalesce to handle nulls.
Like coalesce(tc6671,0)+coalesce(tf6671,0)+...
So your update statement will be something like
UPDATE c
SET c.total = coalesce(c.tc6671,0) + coalesce(c.tf6671,0) + coalesce(c.pc6671,0)
+ coalesce(c.pf6671,0) + coalesce(c.tc8572,0) + coalesce(c.tf8572,0)
+ coalesce(c.pc8572,0) + coalesce(c.pf8572,0) + coalesce(c.tc6672,0)
+ coalesce(c.tf6672,0) + coalesce(c.tc6673,0) + coalesce(c.tf6673,0)
+ coalesce(c.pc6673,0) + coalesce(c.pf6673,0) + coalesce(c.pc6674,0)
+ coalesce(c.pf6674,0) + coalesce(c.tc5852,0) + coalesce(c.tf5852,0)
+ coalesce(c.tc5853,0) + coalesce(c.tf5853,0)
FROM cmt_7th c
But if you use select sum(coalesce(c.tc6671,0) ... then you will get only 1 row and column with sum of sum of all columns.
See what I mean here
http://rextester.com/LUIPOW59089

Rounding in sql server 2008 R2

I have this query
(case when a.item_no >= '77000' and a.item_no <='77099' then
(op_drill + op_machine + op_ssd + op_freight + op_paint + ((actual_tooling * 1.2)))
else (op_drill + op_machine + op_ssd + ((op_drill + op_machine + op_ssd) *.27) + ((op_drill + op_machine + op_ssd + op_freight + (op_curforg *1.25) + ((op_drill + op_machine + op_ssd)*.27)) * .075)+
op_freight + (op_curforg *1.25) + op_paint)
end) as new_cost,
it returns 10.1201575
In vb.net I can run this statement
dblNewCost = Math.Round(dblNewCost, 2, MidpointRounding.AwayFromZero)
which returns 10.13
Is there a way to duplicate this is sql server. I tried round(num,2) but that didn't showup correctly
I think you have some other issue. SQL Servers ROUND() function works just like VB's. There is no way using Math.Round(10.1201575, 2, MidpointRounding.AwayFromZero) would return 10.13. Compare the number that is being rounded on both side to make sure you are comparing apples-to-apples.