I use lambdify quite a bit, on rather large expressions. Basically, it always works like this for me:
import sympy as sm x1, x2, …, xn = sm.symbols(‚x1, x2, ….., xn‘) …. … expr = some expression of generally with me: sm.sin, sm.cos, sm.exp, sm.sqrt, sm.Heaviside, etc.. This expression may have 50,000 terms, may be an (axb) matrix, whatever. expr_lam = sm.lambdify([x1, x2, …,xn], expr) Now I can evaluate expr_lam(…) like I would evaluate any numpy function. I have no idea, what expr_lam looks like, I would not know how to look at it. I assume, it converts sm.sin(..) to np.sin(…), etc This is how it works for me. As I do not really understand your points, like ‚dynamically created‘, ‚parse and subs‘, this may be of not help at all for you. Peter On Thu 18. Aug 2022 at 09:21 Zohreh Karimzadeh <z.karimza...@gmail.com> wrote: > Before run I import sp.sqrt or sp.exp but after run they get disappeared. > My expression is big and dynamically created and not possible to parse and > subs np.exp or sp.exp. > > Zohreh Karimzadeh > > Contact me on > +989102116325 > and at > z.karimza...@gmail.com > 🌧️🌍🌱 > > > On Thu, 18 Aug 2022, 01:17 Aaron Meurer, <asmeu...@gmail.com> wrote: > >> Your expression uses "sqrt" but you haven't imported it from anywhere, >> since you only did "import sympy as sp". You need to use sp.sqrt. >> >> Aaron Meurer >> >> On Wed, Aug 17, 2022 at 11:02 AM Zohreh Karimzadeh < >> z.karimza...@gmail.com> wrote: >> >>> Here is my code: >>> >>> import matplotlib.pyplot as plt >>> import numpy as np >>> import sympy as sp >>> import pandas as pd >>> #exp_NaCl path: F:\Zohreh\MainZohreh\postdoc-field\CSU\Duplicat_Pure >>> df = >>> pd.read_excel(r'F:\Zohreh\MainZohreh\postdoc-field\CSU\Duplicat_Pure\data.xlsx', >>> sheet_name='NaCl_exp') >>> XNa = df['XNa'] >>> XCl = df['XCl'] >>> Xwater = df['Xwater'] >>> Y = df['gama_x'] >>> L=['WwaterNaCl', 'UwaterNaCl', 'VwaterNaCl', 'XCl', 'XNa', 'Xwater', >>> 'BNaCl'] >>> for j in range(len(L)): >>> locals()[L[j]] = sp.symbols(L[j]) >>> expr = -0.0118343195266272*BNaCl*XCl*XNa*(-2*(9.19238815542512*sqrt(XNa) + >>> 9.19238815542512*sqrt(XCl + XNa) + 1)*exp(-9.19238815542512*sqrt(XNa) - >>> 9.19238815542512*sqrt(XCl + XNa)) + 2)/((XCl + XNa)*(sqrt(XNa) + sqrt(XCl + >>> XNa))**2) + 0.00591715976331361*BNaCl*XCl*(-2*(9.19238815542512*sqrt(XNa) + >>> 9.19238815542512*sqrt(XCl + XNa) + 1)*exp(-9.19238815542512*sqrt(XNa) - >>> 9.19238815542512*sqrt(XCl + XNa)) + 2)/(sqrt(XNa) + sqrt(XCl + XNa))**2 + >>> 0.00591715976331361*BNaCl*XNa*(-2*(9.19238815542512*sqrt(XNa) + >>> 9.19238815542512*sqrt(XCl + XNa) + 1)*exp(-9.19238815542512*sqrt(XNa) - >>> 9.19238815542512*sqrt(XCl + XNa)) + 2)/(sqrt(XNa) + sqrt(XCl + XNa))**2 - >>> 1.0*Cl*WwaterNaCl*Xwater*(0.5*XCl + 0.5*XNa + 0.5)/XCl - >>> 0.5*Cl*WwaterNaCl/XCl - 4.0*UwaterNaCl*XCl*XNa*Xwater + >>> 2.0*UwaterNaCl*XCl*Xwater + 2.0*UwaterNaCl*XNa*Xwater - 4.0*UwaterNaCl*XNa >>> - 6.0*VwaterNaCl*XCl*XNa*Xwater**2 - 4.0*VwaterNaCl*XCl*Xwater**2 + >>> 2.0*VwaterNaCl*XNa*Xwater**2 - 1.0*WwaterNaCl*Xwater*(0.5*XCl + 0.5*XNa + >>> 0.5) + 2.0*WwaterNaCl*Xwater - 0.5*WwaterNaCl - >>> 1.45739430799067*(0.707106781186548*sqrt(XNa) + 0.707106781186548*sqrt(XCl >>> + XNa))*(-XCl - XNa + 1)/(9.19238815542512*sqrt(XNa) + >>> 9.19238815542512*sqrt(XCl + XNa) + 1) - >>> 1.45739430799067*(0.707106781186548*sqrt(XNa) + 0.707106781186548*sqrt(XCl >>> + XNa))*(-1.4142135623731*sqrt(XNa) - 1.4142135623731*sqrt(XCl + XNa) + >>> 1)/(9.19238815542512*sqrt(XNa) + 9.19238815542512*sqrt(XCl + XNa) + 1) - >>> 0.448429017843282*log(9.19238815542512*sqrt(XNa) + >>> 9.19238815542512*sqrt(XCl + XNa) + 1) >>> model_func = sp.lambdify(L, expr ) >>> >>> def f(param): >>> BNaCl = param[0] >>> UwaterNaCl = param[1] >>> VwaterNaCl = param[2] >>> WwaterNaCl = param[3] >>> Y_est = model_func >>> return np.sum((np.log(Y) - Y_est)**2) >>> >>> >>> bnds = [(1, np.inf), (0, 1), (0, 1), (-1, np.inf)] >>> x0 = (1, 0.01, 0.98, 1) >>> con = {"type": "eq", "fun": c} >>> >>> result = minimize(f, x0, bounds=bnds) >>> >>> print(result.fun) >>> print(result.message) >>> print(result.x[0], result.x[1], result.x[2], result.x[3]) >>> >>> while I got : >>> NameError: name 'sqrt' is not defined >>> >>> Zohreh Karimzadeh >>> *https://www.researchgate.net/profile/Zohreh-Karimzadeh* >>> <https://www.researchgate.net/profile/Zohreh-Karimzadeh> >>> Skype Name 49a52224a8b6b38b >>> Twitter Account @zohrehkarimzad1 >>> z.karimza...@gmail.com >>> +989102116325 >>> ((((((((((((((((Value Water))))))))))))))) >>> >>> >>> On Wed, Aug 17, 2022 at 7:46 PM Peter Stahlecker < >>> peter.stahlec...@gmail.com> wrote: >>> >>>> I use lambdify(....) a lot, but always like this: >>>> >>>> x = sympy.symbols('x') >>>> expr = symy.S(10.) * sympy.sqrt(x) >>>> expr_lam = sympy.lambdify([x], expr) >>>> >>>> a = expr_lam(10.) >>>> >>>> This seems to work for me. >>>> >>>> On Wed 17. Aug 2022 at 20:38, Zohreh Karimzadeh <z.karimza...@gmail.com> >>>> wrote: >>>> >>>>> Dear sympy group >>>>> Thanks for your sympy. >>>>> >>>>> I am working on a code, after creating my big expression using sympy >>>>> it includes sqrt. >>>>> >>>>> I need to lambdify my expression to make it consistent with numpy and >>>>> other suffs. >>>>> >>>>> expr =10 * sp.sqrt(sp.symbols('x')) >>>>> >>>>> model_func = sp.lambdify('x', expr) >>>>> >>>>> But I found my expression after lambdifying becomes somethings like >>>>> this: >>>>> >>>>> 10*sqrt(x) >>>>> >>>>> while I need : >>>>> >>>>> 10*numpy.sqrt(x) >>>>> >>>>> Could possibly let me know how get sqrt to work with numpy? >>>>> >>>>> Regards, >>>>> >>>>> Zohreh >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> -- >>>>> You received this message because you are subscribed to the Google >>>>> Groups "sympy" group. >>>>> To unsubscribe from this group and stop receiving emails from it, send >>>>> an email to sympy+unsubscr...@googlegroups.com. >>>>> To view this discussion on the web visit >>>>> https://groups.google.com/d/msgid/sympy/1f0b313f-31c5-402e-991e-142a556016f4n%40googlegroups.com >>>>> <https://groups.google.com/d/msgid/sympy/1f0b313f-31c5-402e-991e-142a556016f4n%40googlegroups.com?utm_medium=email&utm_source=footer> >>>>> . >>>>> >>>> -- >>>> Best regards, >>>> >>>> Peter Stahlecker >>>> >>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "sympy" group. >>>> To unsubscribe from this group and stop receiving emails from it, send >>>> an email to sympy+unsubscr...@googlegroups.com. >>>> To view this discussion on the web visit >>>> https://groups.google.com/d/msgid/sympy/CABKqA0ZoGwsadsk4SWCbJVMbCDwXcO_gNGumJH00GAeEFod7Cw%40mail.gmail.com >>>> <https://groups.google.com/d/msgid/sympy/CABKqA0ZoGwsadsk4SWCbJVMbCDwXcO_gNGumJH00GAeEFod7Cw%40mail.gmail.com?utm_medium=email&utm_source=footer> >>>> . >>>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "sympy" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to sympy+unsubscr...@googlegroups.com. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/sympy/CA%2B1XYLPRvXZ6jiJbUS_xpWNKqMuUH7Kt5evue%2BwKEwDMvGekBQ%40mail.gmail.com >>> <https://groups.google.com/d/msgid/sympy/CA%2B1XYLPRvXZ6jiJbUS_xpWNKqMuUH7Kt5evue%2BwKEwDMvGekBQ%40mail.gmail.com?utm_medium=email&utm_source=footer> >>> . >> >> >>> -- >> You received this message because you are subscribed to the Google Groups >> "sympy" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to sympy+unsubscr...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/sympy/CAKgW%3D6JfUmU7Uu%2BSrcA1STxVvWWm7bGWE%3Dit8CTchksTC0Qk7g%40mail.gmail.com >> <https://groups.google.com/d/msgid/sympy/CAKgW%3D6JfUmU7Uu%2BSrcA1STxVvWWm7bGWE%3Dit8CTchksTC0Qk7g%40mail.gmail.com?utm_medium=email&utm_source=footer> >> . >> > -- > You received this message because you are subscribed to the Google Groups > "sympy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sympy+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sympy/CA%2B1XYLPiCR%3DS2Fac3FZtjMpspqB7BRKtYEi45BVWPjkizVbNvw%40mail.gmail.com > <https://groups.google.com/d/msgid/sympy/CA%2B1XYLPiCR%3DS2Fac3FZtjMpspqB7BRKtYEi45BVWPjkizVbNvw%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > -- Best regards, Peter Stahlecker -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CABKqA0b%3DF0akMH4oyg5%2By9dGvgrf_vvVJTnVhVduMP1f%2Bp1pFw%40mail.gmail.com.