the following code is ok when expression is passed as :

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])

but when the expression is passed as the following way:

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])


I face difficulty:
*********************************************
Traceback (most recent call last):
  File
"C:\Users\Zohreh\AppData\Roaming\Python\Python310\site-packages\sympy\core\cache.py",
line 70, in wrapper
    retval = cfunc(*args, **kwargs)
TypeError: unhashable type: 'numpy.ndarray'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File
"C:\Users\Zohreh\AppData\Roaming\Python\Python310\site-packages\sympy\core\cache.py",
line 70, in wrapper
    retval = cfunc(*args, **kwargs)
TypeError: unhashable type: 'numpy.ndarray'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File
"F:\Zohreh\MainZohreh\postdoc-field\CSU\pythonProject\fit_test_2.py", line
26, in <module>
    result = minimize(f, x0, bounds=bnds)
  File
"C:\Users\Zohreh\AppData\Roaming\Python\Python310\site-packages\scipy\optimize\_minimize.py",
line 692, in minimize
    res = _minimize_lbfgsb(fun, x0, args, jac, bounds,
  File
"C:\Users\Zohreh\AppData\Roaming\Python\Python310\site-packages\scipy\optimize\_lbfgsb_py.py",
line 308, in _minimize_lbfgsb
    sf = _prepare_scalar_function(fun, x0, jac=jac, args=args, epsilon=eps,
  File
"C:\Users\Zohreh\AppData\Roaming\Python\Python310\site-packages\scipy\optimize\_optimize.py",
line 263, in _prepare_scalar_function
    sf = ScalarFunction(fun, x0, args, grad, hess,
  File
"C:\Users\Zohreh\AppData\Roaming\Python\Python310\site-packages\scipy\optimize\_differentiable_functions.py",
line 158, in __init__
    self._update_fun()
  File
"C:\Users\Zohreh\AppData\Roaming\Python\Python310\site-packages\scipy\optimize\_differentiable_functions.py",
line 251, in _update_fun
    self._update_fun_impl()
  File
"C:\Users\Zohreh\AppData\Roaming\Python\Python310\site-packages\scipy\optimize\_differentiable_functions.py",
line 155, in update_fun
    self.f = fun_wrapped(self.x)
  File
"C:\Users\Zohreh\AppData\Roaming\Python\Python310\site-packages\scipy\optimize\_differentiable_functions.py",
line 137, in fun_wrapped
    fx = fun(np.copy(x), *args)
  File
"F:\Zohreh\MainZohreh\postdoc-field\CSU\pythonProject\fit_test_2.py", line
17, in f
    Vi_est = gamma - (1 / eta) * sp.log(alpha * L ** -eta + beta * K **
-eta)
  File
"C:\Users\Zohreh\AppData\Roaming\Python\Python310\site-packages\sympy\core\cache.py",
line 74, in wrapper
    retval = func(*args, **kwargs)
  File
"C:\Users\Zohreh\AppData\Roaming\Python\Python310\site-packages\sympy\core\function.py",
line 476, in __new__
    result = super().__new__(cls, *args, **options)
  File
"C:\Users\Zohreh\AppData\Roaming\Python\Python310\site-packages\sympy\core\cache.py",
line 74, in wrapper
    retval = func(*args, **kwargs)
  File
"C:\Users\Zohreh\AppData\Roaming\Python\Python310\site-packages\sympy\core\function.py",
line 288, in __new__
    evaluated = cls.eval(*args)
  File
"C:\Users\Zohreh\AppData\Roaming\Python\Python310\site-packages\sympy\functions\elementary\exponential.py",
line 718, in eval
    coeff = arg.as_coefficient(I)
AttributeError: 'ImmutableDenseNDimArray' object has no attribute
'as_coefficient'








Zohreh Karimzadeh
https://www.researchgate.net/profile/Zohreh-Karimzadeh
Skype Name 49a52224a8b6b38b
Twitter Account @zohrehkarimzad1
z.karimza...@gmail.com
+989102116325
((((((((((((((((Value Water)))))))))))))))

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 Thu, Aug 18, 2022 at 10:42 AM Peter Stahlecker <
peter.stahlec...@gmail.com> wrote:

> 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
> <https://groups.google.com/d/msgid/sympy/CABKqA0b%3DF0akMH4oyg5%2By9dGvgrf_vvVJTnVhVduMP1f%2Bp1pFw%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%2B1XYLMK-fgpxc71GYzue5gJvd%3Dfj2sV6Dvhj8zrmVpPhiVk%2Bw%40mail.gmail.com.

Reply via email to