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+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/1f0b313f-31c5-402e-991e-142a556016f4n%40googlegroups.com.
--
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])
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CABKqA0ZoGwsadsk4SWCbJVMbCDwXcO_gNGumJH00GAeEFod7Cw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAKgW%3D6KsfT-W-Q1A7y1ppTP6xgF4Fucwxm7wZ0Y8WQX7hsMD4Q%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CA%2B1XYLPRvXZ6jiJbUS_xpWNKqMuUH7Kt5evue%2BwKEwDMvGekBQ%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAKgW%3D6JfUmU7Uu%2BSrcA1STxVvWWm7bGWE%3Dit8CTchksTC0Qk7g%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CA%2B1XYLPiCR%3DS2Fac3FZtjMpspqB7BRKtYEi45BVWPjkizVbNvw%40mail.gmail.com.
import numpy as np
from scipy.optimize import minimize, curve_fit
from lmfit import Model, Parameters
L = np.array([0.299, 0.295, 0.290, 0.284, 0.279, 0.273, 0.268, 0.262, 0.256, 0.250])
K = np.array([2.954, 3.056, 3.119, 3.163, 3.215, 3.274, 3.351, 3.410, 3.446, 3.416])
VA = np.array([0.919, 0.727, 0.928, 0.629, 0.656, 0.854, 0.955, 0.981, 0.908, 0.794])
def f(param):
gamma = param[0]
alpha = param[1]
beta = param[2]
eta = param[3]
VA_est = gamma - (1 / eta) * np.log(alpha * L ** -eta + beta * K ** -eta)
return np.sum((np.log(VA) - VA_est) ** 2)
bnds = [(1, np.inf), (0, 1), (0, 1), (-1, np.inf)]
x0 = (1, 0.01, 0.98, 1)
result = minimize(f, x0, bounds=bnds)
print(result.message)
print(result.x[0], result.x[1], result.x[2], result.x[3])
import numpy as np
import sympy as sp
from scipy.optimize import minimize, curve_fit
from lmfit import Model, Parameters
L = np.array([0.299, 0.295, 0.290, 0.284, 0.279, 0.273, 0.268, 0.262, 0.256, 0.250])
K = np.array([2.954, 3.056, 3.119, 3.163, 3.215, 3.274, 3.351, 3.410, 3.446, 3.416])
VA = np.array([0.919, 0.727, 0.928, 0.629, 0.656, 0.854, 0.955, 0.981, 0.908, 0.794])
def f(param):
gamma, alpha, beta, eta = sp.symbols('gamma, alpha, beta, eta')
gamma = param[0]
alpha = param[1]
beta = param[2]
eta = param[3]
Vi_est = gamma - (1 / eta) * sp.log(alpha * L ** -eta + beta * K ** -eta)
Vlam_est = sp.lambdify((gamma, alpha, beta, eta), Vi_est)
return np.sum((np.log(VA) - Vlam_est) ** 2)
bnds = [(1, np.inf), (0, 1), (0, 1), (-1, np.inf)]
x0 = (1, 0.01, 0.98, 1)
result = minimize(f, x0, bounds=bnds)
print(result.message)
print(result.x[0], result.x[1], result.x[2], result.x[3])
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CABKqA0b%3DF0akMH4oyg5%2By9dGvgrf_vvVJTnVhVduMP1f%2Bp1pFw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CA%2B1XYLMK-fgpxc71GYzue5gJvd%3Dfj2sV6Dvhj8zrmVpPhiVk%2Bw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CABKqA0aBsD2WhpTuQqzZGUK1pfkUyH4q3Om9DdBQOpoaaO4rqQ%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CA%2B1XYLNiQNVa_hg25e-_f8xs%2B2w88p7JC4ntneBrqO4YFajTgA%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CABKqA0YedBPPLH73J6ScxkgNWN9_NCR-YvOcd7vpGX4SsGZj0g%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CA%2B1XYLOkAu3fHK8Fm1eRoO8CQxkN70mnO%3DM7iNqeRg3bt44yZQ%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CABKqA0aeQ%3D47NmCUei%3DvtXwFyjs4dw%3DfZ%3DjQGqpJvsk68BRYHA%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CA%2B1XYLMg7bq4X3iedc0u_9nwiKHXrFd8GV1%3DdUt9n255dcwJxw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CABKqA0a6gewUiwcUHfk%3DR2fQVkwGgkefwYN0ziJoAesjKaabRg%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/5d28146e-d335-4eb9-9d88-6d77b31c18fcn%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CA%2B1XYLMAxxjDSQ19gr1fkr-AfNF_DcMgcsR3DcyvSg_cktcDqA%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/0df4cadb-4fcb-4502-9017-2fdfea527f17n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CA%2B1XYLMK-fgpxc71GYzue5gJvd%3Dfj2sV6Dvhj8zrmVpPhiVk%2Bw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAKgW%3D6%2BBQo_nWKJtbxPmi40V0Y6OgAaT78jSNSWKnwW8L3qmZQ%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CA%2B1XYLNg4ScH5bK%3DeW7%3DVL_2%2BWdWT4kf5ud41LR5hh%2BJkCJR2g%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CABKqA0aP_Rx9rDQ-%2BumFq_9W8C4LDTRMGrFif_5GZ_dRTpA2VA%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CA%2B1XYLOmpYg0iHjow4D5hvp_%3DFSfmKEgaWBiDFoweEX4Vo4jxw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CABKqA0YJ7gvfrAKV4%2B%2BWU%3Dd_MP_%2BUMDAeRh434KdFesS%3DxiAvA%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CA%2B1XYLP8YcB6puF4Tg0BVOUKv8wCHnE1o113AeKXoMZnJ-NfpA%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CABKqA0ZZCaRbYjVVhEHs1mdnVtrbXr8kYR8Vk8_ZQ81YV%2Btg_Q%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CA%2B1XYLOtBq8Bk17-QVN-z2CvYPQhFQOqJyzHpM3eXXGQz2uc1A%40mail.gmail.com.
Most welcome!Like I said, passing args = (L, K , VA) to your function F may take a little 'playing around'.
--
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+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/0d41fe02-9bba-41e8-83d0-5971f646475dn%40googlegroups.com.
--
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+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAHVvXxQJUuZ4tVnECPyWE%3DLd03hhhUB5mqv1bjHcjSNf9WP22Q%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CA%2B1XYLOMV_i3VJXwwVN7JWnuJ9Wo6ZPhULmC1SOubAfFLt-DBQ%40mail.gmail.com.

To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CABKqA0YTOzyZkjhnQmKQKQh-Mrc_H_TcmD9FrXObaTadkfLpkQ%40mail.gmail.com.
term_vnca = 0
for k in anion:
for j in cation:
for i in neutral:
term_vnca += term_vncX - 4 * 3 * sp.symbols(f'X{str(k)}') * sp.symbols('X{0}'.format(str(i))) \
** 2 * sp.symbols(f'V{str(i)}{str(j)}{str(k)}')
lnfXs = term_wnca + term_unca + term_vnca + termx_w_solvent1 + termx_w_solvent2
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/17d62c5c-11d8-4b1b-9a44-ae67c8021832n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/837a3351-3aa3-4e1e-98d6-b34f3185d5b2n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CA%2B1XYLMEq2OLN0npw8RHfghS7m6vgewkrpshqw8gBBU%2BfUAnoQ%40mail.gmail.com.
cation = {'Na': 1,'Cl':2}
anion = {'Cl': 1}
ionic_dic = dict()
for d in (cation, anion):
ionic_dic.update(d)
def ionicstr():
Ionic = 0
sqrt_I = 0
for k1 in ionic_dic:
Ionic += 0.5 * (('X'+str(k1)) * (ionic_dic[k1] ** 2))
sqrt_I += (Ionic) ** 0.5
print(Ionic, sqrt_I)
return Ionic, sqrt_I
def term():
for i in cation:
A ='X' + str(i) + str(ionicstr()[0])
print(A)
return A
term()
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/54b70a36-3ee5-4d14-b30a-813b8fbf31fcn%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/6eae7b43-e685-4892-a943-8605ad04c2efn%40googlegroups.com.

You received this message because you are subscribed to a topic in the Google Groups "sympy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sympy/CAGn_agpz6M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sympy+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/a9bd0c6b-75f7-4711-9e02-19c06648c86dn%40googlegroups.com.
import numpy as np
import sympy as sp
from scipy.optimize import minimize, curve_fit
L_vals = np.array([0.299, 0.295, 0.290, 0.284, 0.279, 0.273, 0.268, 0.262, 0.256, 0.250])
K_vals = np.array([2.954, 3.056, 3.119, 3.163, 3.215, 3.274, 3.351, 3.410, 3.446, 3.416])
VA_vals = np.array([0.919, 0.727, 0.928, 0.629, 0.656, 0.854, 0.955, 0.981, 0.908, 0.794])
gamma, alpha, beta, eta, L, K, VA = sp.symbols('gamma, alpha, beta, eta, L, K, VA')
Vi_est = gamma - (1 / eta) * sp.log(alpha * L ** -eta + beta * K ** -eta)
# Note that the first entry in the args tuple is a 4 tuple
# That is needed to unpack the arguments from an array
f_Vi_est = sp.lambdify(((gamma, alpha, beta, eta), L, K, VA), Vi_est)
def f(param):
Vi_est_vals = f_Vi_est(param, L_vals, K_vals, VA_vals)
return np.sum((np.log(VA_vals) - Vi_est_vals) ** 2)
bnds = [(1, np.inf), (0, 1), (0, 1), (-1, np.inf)]
x0 = (1, 0.01, 0.98, 1)
result = minimize(f, x0, bounds=bnds)
print(result.message)
print(result.x[0], result.x[1], result.x[2], result.x[3])
f_Vi_est = sp.lambdify(((gamma, alpha, beta, eta), L, K, VA), Vi_est)
Vi_est_vals = f_Vi_est(param, L_vals, K_vals, VA_vals)
--
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+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/0d41fe02-9bba-41e8-83d0-5971f646475dn%40googlegroups.com.