How to convert sympy expression which include integral and derivative to numpy - 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?

Related

ORDER BY euclidean distance and also return the distance?

I have the following table structure for my table called storage_faceencoding.
id: UUID
feature_1: Float
feature_2: Float
feature_3: Float
...
feature_128: Float
I want to query against those 128 feature columns and return the id along with the distance value.
I'm querying against those 128 feature columns like so:
SELECT * from storage_faceencoding ORDER BY sqrt(power(feature_1 - -0.054835572838783264, 2) + power(feature_2 - 0.034169211983680725, 2) + power(feature_3 - 0.0789899230003357, 2) + power(feature_4 - -0.12306179106235504, 2) + power(feature_5 - -0.14243610203266144, 2) + power(feature_6 - 0.0015480183064937592, 2) + power(feature_7 - -0.03730906546115875, 2) + power(feature_8 - -0.04611199349164963, 2) + power(feature_9 - 0.17417974770069122, 2) + power(feature_10 - -0.07324142754077911, 2) + power(feature_11 - 0.2784380316734314, 2) + power(feature_12 - -0.05557965114712715, 2) + power(feature_13 - -0.28433677554130554, 2) + power(feature_14 - 0.003816460259258747, 2) + power(feature_15 - -0.004081043414771557, 2) + power(feature_16 - 0.14893895387649536, 2) + power(feature_17 - -0.12637391686439514, 2) + power(feature_18 - -0.0776168555021286, 2) + power(feature_19 - -0.07115891575813293, 2) + power(feature_20 - 0.013110934756696224, 2) + power(feature_21 - -0.03728703036904335, 2) + power(feature_22 - 0.0312555767595768, 2) + power(feature_23 - -0.013231765478849411, 2) + power(feature_24 - 0.08345625549554825, 2) + power(feature_25 - -0.14501842856407166, 2) + power(feature_26 - -0.3354545533657074, 2) + power(feature_27 - -0.06517831236124039, 2) + power(feature_28 - -0.16320215165615082, 2) + power(feature_29 - -0.029647808521986008, 2) + power(feature_30 - -0.026274608448147774, 2) + power(feature_31 - 0.008217697963118553, 2) + power(feature_32 - 0.09989964962005615, 2) + power(feature_33 - -0.10960874706506729, 2) + power(feature_34 - -0.04241098091006279, 2) + power(feature_35 - 0.03659277781844139, 2) + power(feature_36 - 0.05843937397003174, 2) + power(feature_37 - -0.1882062405347824, 2) + power(feature_38 - -0.1472000777721405, 2) + power(feature_39 - 0.1806768774986267, 2) + power(feature_40 - -0.047331131994724274, 2) + power(feature_41 - -0.1587458848953247, 2) + power(feature_42 - -0.03949886932969093, 2) + power(feature_43 - 0.025885790586471558, 2) + power(feature_44 - 0.23678790032863617, 2) + power(feature_45 - 0.10318232327699661, 2) + power(feature_46 - -0.02302492968738079, 2) + power(feature_47 - 0.1084280014038086, 2) + power(feature_48 - -0.047027621418237686, 2) + power(feature_49 - 0.07425221800804138, 2) + power(feature_50 - -0.2947121560573578, 2) + power(feature_51 - 0.0031417328864336014, 2) + power(feature_52 - 0.121400848031044, 2) + power(feature_53 - 0.07688573747873306, 2) + power(feature_54 - 0.09685630351305008, 2) + power(feature_55 - 0.10917495936155319, 2) + power(feature_56 - -0.1276204138994217, 2) + power(feature_57 - 0.08362210541963577, 2) + power(feature_58 - 0.16328635811805725, 2) + power(feature_59 - -0.25213390588760376, 2) + power(feature_60 - 0.05785408616065979, 2) + power(feature_61 - 0.018559012562036514, 2) + power(feature_62 - -0.01683107763528824, 2) + power(feature_63 - -0.01742824725806713, 2) + power(feature_64 - -0.1193951666355133, 2) + power(feature_65 - 0.2622459828853607, 2) + power(feature_66 - 0.12121666967868805, 2) + power(feature_67 - -0.1520567685365677, 2) + power(feature_68 - -0.1122896820306778, 2) + power(feature_69 - 0.14081937074661255, 2) + power(feature_70 - -0.14206485450267792, 2) + power(feature_71 - -0.10112465173006058, 2) + power(feature_72 - 0.07290071249008179, 2) + power(feature_73 - -0.16641031205654144, 2) + power(feature_74 - -0.21917763352394104, 2) + power(feature_75 - -0.25251486897468567, 2) + power(feature_76 - -0.0048762112855911255, 2) + power(feature_77 - 0.37720248103141785, 2) + power(feature_78 - 0.1548663228750229, 2) + power(feature_79 - -0.12398210912942886, 2) + power(feature_80 - 0.041234225034713745, 2) + power(feature_81 - -0.06544441729784012, 2) + power(feature_82 - -0.09197349101305008, 2) + power(feature_83 - -0.021058330312371254, 2) + power(feature_84 - 0.039639901369810104, 2) + power(feature_85 - -0.0023813657462596893, 2) + power(feature_86 - -0.07752023637294769, 2) + power(feature_87 - -0.03788585215806961, 2) + power(feature_88 - 0.01571604609489441, 2) + power(feature_89 - 0.22586794197559357, 2) + power(feature_90 - -0.08962416648864746, 2) + power(feature_91 - 0.027185270562767982, 2) + power(feature_92 - 0.21015749871730804, 2) + power(feature_93 - 0.014571629464626312, 2) + power(feature_94 - -0.03427625447511673, 2) + power(feature_95 - -0.031976763159036636, 2) + power(feature_96 - -0.02412014827132225, 2) + power(feature_97 - -0.0572514683008194, 2) + power(feature_98 - 0.025131937116384506, 2) + power(feature_99 - -0.055663101375103, 2) + power(feature_100 - 0.06940365582704544, 2) + power(feature_101 - -0.008616405539214611, 2) + power(feature_102 - -0.08647502958774567, 2) + power(feature_103 - -0.002743437420576811, 2) + power(feature_104 - 0.06195132061839104, 2) + power(feature_105 - -0.21810059249401093, 2) + power(feature_106 - 0.18355928361415863, 2) + power(feature_107 - 0.028437575325369835, 2) + power(feature_108 - -0.02596919797360897, 2) + power(feature_109 - 0.03891913592815399, 2) + power(feature_110 - 0.017703840509057045, 2) + power(feature_111 - -0.14867563545703888, 2) + power(feature_112 - -0.035795167088508606, 2) + power(feature_113 - 0.21177658438682556, 2) + power(feature_114 - -0.3009713888168335, 2) + power(feature_115 - 0.20549386739730835, 2) + power(feature_116 - 0.15210166573524475, 2) + power(feature_117 - 0.1311473250389099, 2) + power(feature_118 - 0.1054188460111618, 2) + power(feature_119 - 0.017464492470026016, 2) + power(feature_120 - -0.011423288844525814, 2) + power(feature_121 - -0.0072870198637247086, 2) + power(feature_122 - -0.021593499928712845, 2) + power(feature_123 - -0.21365071833133698, 2) + power(feature_124 - -0.09368597716093063, 2) + power(feature_125 - 0.06633836776018143, 2) + power(feature_126 - -0.05478530377149582, 2) + power(feature_127 - -0.009351606480777264, 2) + power(feature_128 - 0.04693271964788437, 2) ) LIMIT 5
This works and orders the results by the euclidean distance however I also want the distance value returned (i.e. SELECT it).
How would I go about getting that distance value?
Thanks in advance.
You can use an alias to show that distance on the result and then order by that distance:
SELECT *, (sqrt(power(feature_1 - -0.054835572838783264, 2) + power(feature_2 - 0.034169211983680725, 2) + power(feature_3 - 0.0789899230003357, 2) + power(feature_4 - -0.12306179106235504, 2) + power(feature_5 - -0.14243610203266144, 2) + power(feature_6 - 0.0015480183064937592, 2) + power(feature_7 - -0.03730906546115875, 2) + power(feature_8 - -0.04611199349164963, 2) + power(feature_9 - 0.17417974770069122, 2) + power(feature_10 - -0.07324142754077911, 2) + power(feature_11 - 0.2784380316734314, 2) + power(feature_12 - -0.05557965114712715, 2) + power(feature_13 - -0.28433677554130554, 2) + power(feature_14 - 0.003816460259258747, 2) + power(feature_15 - -0.004081043414771557, 2) + power(feature_16 - 0.14893895387649536, 2) + power(feature_17 - -0.12637391686439514, 2) + power(feature_18 - -0.0776168555021286, 2) + power(feature_19 - -0.07115891575813293, 2) + power(feature_20 - 0.013110934756696224, 2) + power(feature_21 - -0.03728703036904335, 2) + power(feature_22 - 0.0312555767595768, 2) + power(feature_23 - -0.013231765478849411, 2) + power(feature_24 - 0.08345625549554825, 2) + power(feature_25 - -0.14501842856407166, 2) + power(feature_26 - -0.3354545533657074, 2) + power(feature_27 - -0.06517831236124039, 2) + power(feature_28 - -0.16320215165615082, 2) + power(feature_29 - -0.029647808521986008, 2) + power(feature_30 - -0.026274608448147774, 2) + power(feature_31 - 0.008217697963118553, 2) + power(feature_32 - 0.09989964962005615, 2) + power(feature_33 - -0.10960874706506729, 2) + power(feature_34 - -0.04241098091006279, 2) + power(feature_35 - 0.03659277781844139, 2) + power(feature_36 - 0.05843937397003174, 2) + power(feature_37 - -0.1882062405347824, 2) + power(feature_38 - -0.1472000777721405, 2) + power(feature_39 - 0.1806768774986267, 2) + power(feature_40 - -0.047331131994724274, 2) + power(feature_41 - -0.1587458848953247, 2) + power(feature_42 - -0.03949886932969093, 2) + power(feature_43 - 0.025885790586471558, 2) + power(feature_44 - 0.23678790032863617, 2) + power(feature_45 - 0.10318232327699661, 2) + power(feature_46 - -0.02302492968738079, 2) + power(feature_47 - 0.1084280014038086, 2) + power(feature_48 - -0.047027621418237686, 2) + power(feature_49 - 0.07425221800804138, 2) + power(feature_50 - -0.2947121560573578, 2) + power(feature_51 - 0.0031417328864336014, 2) + power(feature_52 - 0.121400848031044, 2) + power(feature_53 - 0.07688573747873306, 2) + power(feature_54 - 0.09685630351305008, 2) + power(feature_55 - 0.10917495936155319, 2) + power(feature_56 - -0.1276204138994217, 2) + power(feature_57 - 0.08362210541963577, 2) + power(feature_58 - 0.16328635811805725, 2) + power(feature_59 - -0.25213390588760376, 2) + power(feature_60 - 0.05785408616065979, 2) + power(feature_61 - 0.018559012562036514, 2) + power(feature_62 - -0.01683107763528824, 2) + power(feature_63 - -0.01742824725806713, 2) + power(feature_64 - -0.1193951666355133, 2) + power(feature_65 - 0.2622459828853607, 2) + power(feature_66 - 0.12121666967868805, 2) + power(feature_67 - -0.1520567685365677, 2) + power(feature_68 - -0.1122896820306778, 2) + power(feature_69 - 0.14081937074661255, 2) + power(feature_70 - -0.14206485450267792, 2) + power(feature_71 - -0.10112465173006058, 2) + power(feature_72 - 0.07290071249008179, 2) + power(feature_73 - -0.16641031205654144, 2) + power(feature_74 - -0.21917763352394104, 2) + power(feature_75 - -0.25251486897468567, 2) + power(feature_76 - -0.0048762112855911255, 2) + power(feature_77 - 0.37720248103141785, 2) + power(feature_78 - 0.1548663228750229, 2) + power(feature_79 - -0.12398210912942886, 2) + power(feature_80 - 0.041234225034713745, 2) + power(feature_81 - -0.06544441729784012, 2) + power(feature_82 - -0.09197349101305008, 2) + power(feature_83 - -0.021058330312371254, 2) + power(feature_84 - 0.039639901369810104, 2) + power(feature_85 - -0.0023813657462596893, 2) + power(feature_86 - -0.07752023637294769, 2) + power(feature_87 - -0.03788585215806961, 2) + power(feature_88 - 0.01571604609489441, 2) + power(feature_89 - 0.22586794197559357, 2) + power(feature_90 - -0.08962416648864746, 2) + power(feature_91 - 0.027185270562767982, 2) + power(feature_92 - 0.21015749871730804, 2) + power(feature_93 - 0.014571629464626312, 2) + power(feature_94 - -0.03427625447511673, 2) + power(feature_95 - -0.031976763159036636, 2) + power(feature_96 - -0.02412014827132225, 2) + power(feature_97 - -0.0572514683008194, 2) + power(feature_98 - 0.025131937116384506, 2) + power(feature_99 - -0.055663101375103, 2) + power(feature_100 - 0.06940365582704544, 2) + power(feature_101 - -0.008616405539214611, 2) + power(feature_102 - -0.08647502958774567, 2) + power(feature_103 - -0.002743437420576811, 2) + power(feature_104 - 0.06195132061839104, 2) + power(feature_105 - -0.21810059249401093, 2) + power(feature_106 - 0.18355928361415863, 2) + power(feature_107 - 0.028437575325369835, 2) + power(feature_108 - -0.02596919797360897, 2) + power(feature_109 - 0.03891913592815399, 2) + power(feature_110 - 0.017703840509057045, 2) + power(feature_111 - -0.14867563545703888, 2) + power(feature_112 - -0.035795167088508606, 2) + power(feature_113 - 0.21177658438682556, 2) + power(feature_114 - -0.3009713888168335, 2) + power(feature_115 - 0.20549386739730835, 2) + power(feature_116 - 0.15210166573524475, 2) + power(feature_117 - 0.1311473250389099, 2) + power(feature_118 - 0.1054188460111618, 2) + power(feature_119 - 0.017464492470026016, 2) + power(feature_120 - -0.011423288844525814, 2) + power(feature_121 - -0.0072870198637247086, 2) + power(feature_122 - -0.021593499928712845, 2) + power(feature_123 - -0.21365071833133698, 2) + power(feature_124 - -0.09368597716093063, 2) + power(feature_125 - 0.06633836776018143, 2) + power(feature_126 - -0.05478530377149582, 2) + power(feature_127 - -0.009351606480777264, 2) + power(feature_128 - 0.04693271964788437, 2) )) as distance
from storage_faceencoding ORDER BY distance LIMIT 5

VBA - Bypass Application.evaulate limit

So I got an string that contains numeric values that I want to calculate into a sum. I tried to use Application.evaluate but I noticed that there is a limit on the amount of characters I can use in the calculation. Is there a way to bypass this or fix it so I can calculate it? I'm thinking of writing my own "calculator" but it feels like a bit to much and there must be an easier way to do it?
0 /(2^16) + 3 /(2^16) + 53 /(2^16) + 139573 /(2^16) + 1844259
/(2^16) + 4005892 /(2^16) + 7546905 /(2^16) + 14629148 /(2^16) +
15078164 /(2^16) + 7477520 /(2^16) + 3158785/(2^16) + 1258795/(2^16)
+ 538914/(2^16) + 396079/(2^16) + 12305/(2^16) + 1555/(2^16) + 258/(2^16) + 8/(2^16) + 4/(2^16)
Another example:
1215 /( 7168 + 5922 + 5390 + 7034 + 10410 + 10494 +
12113 + 6607 + 448 + 67 + 2996 + 4103 + 15581 + 18432 +
7299 + 1215 + 177 + 11 + 3 + 0 )*100
Could you stick it in a cell and read it back?
Sub test()
Dim s As String
s = "0 /(2^16) + 3 /(2^16) + 53 /(2^16) + 139573 /(2^16) + 1844259 /(2^16) + 4005892 /(2^16) + 7546905 /(2^16) + 14629148 /(2^16) + 15078164 /(2^16) + 7477520 /(2^16) + 3158785/(2^16) + 1258795/(2^16) + 538914/(2^16) + 396079/(2^16) + 12305/(2^16) + 1555/(2^16) + 258/(2^16) + 8/(2^16) + 4/(2^16)"
Sheet1.Range("A1").Formula = "=" & s
Debug.Print Sheet1.Range("A1").Value
Sheet1.Range("A1").ClearContents
End Sub
Given that addition is commutative, can you not calculate half your expression (up to 3158785/(2^16)), assign the result to a variable, then calculate the rest of the expression and add that to the previous result?

Replacement (Hyperlinks.Add Anchor)

Is there such a command/method in VBA that operated the same way as
Hyperlinks.Add Anchor , but worked with variables , not with ranges ?
book1.ActiveSheet.Hyperlinks.Add Anchor:=Cells(34 + (x - 1), (26 + (iLoop * 21)) + (y - 1)), Address:=data(x, y), TextToDisplay:=vata(x, y)
That instead of
Cells(34 + (x - 1), (26 + (iLoop * 21)) + (y - 1))
there was a variable .

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.

How to shorten the boolean function

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