Zohreh, I cannot tell you what is your best choice until you tell me what you are doing after you build the expression. Is it 1 or 2, as I asked before?
1) You ultimately pass a string expression to lmfit or some other fitting package that is parsed into a numpy expression for fitting or something similar. 2)You are making your own model function that returns a number every time it is called by some other package (fitting?). Please explain exactly how you wish to use the expression you are developing and, if you are using it with another tool, which one? Regards, Jonathan On Friday, August 26, 2022 at 9:24:32 PM UTC-5 z.kari...@gmail.com wrote: > Dear Jonathan > Thank you for your reply. > About using strings instead of sympy good idea, It is necessary to > know that there are bunch of functions that are inserted in the main > model as one of them : > > anion = {'Cl': 1} > cation = {'Na': 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 * (sp.symbols('X{0}'.format(str(k1))) * > (ionic_dic[k1] ** 2)) > sqrt_I += sp.sqrt(Ionic) > print(Ionic, sqrt_I) > return Ionic, sqrt_I > AS can be seen while all of these kinds of functions must be evaluated > or substituted in the main model. > Is it still possible to use strings instead of sympy?? > Regards, > Zohreh > > On Fri, Aug 26, 2022 at 6:04 PM gu...@uwosh.edu <gu...@uwosh.edu> wrote: > > > > Zohreh, > > > > It is still unclear what you are actually doing. I can think of two > possibilities (are either of them correct?): > > > > 1) You ultimately pass a string expression to lmfit or some other > fitting package that is parsed into a numpy expression for fitting or > something similar. So I am suggesting that you remove the intermediate step > of creating a sympy expression to make things more efficient. You can > generate the string expression dynamically in essentially the same way you > are currently generating the sympy expression, but avoid having the > intermediate step of using sympy to generate the expression and then using > it to generate a string expression that is parsed again. > > > > 2) Alternatively, you may be making your own model function (I believe > for lmfit). In that case you need to generate a function that does a > calculation. I see no reason it has to have human readable names for the > parameters in the actual calculation. However, you could connect them with > human readable names through a dictionary or something like that. > > > > If you want a string, your code is almost there. As long as the elements > of cation and anion are strings. Just replace things like > sp.symbols('X{0}'.format(str(i))) with 'X_'+str(i). But maybe that is not > what you are doing. > > > > Jonathan > > > > On Friday, August 26, 2022 at 5:03:00 AM UTC-5 z.kari...@gmail.com > wrote: > >> > >> Jonathan has recommended to use string instead of sympy approche to > >> derive your model and -parameters. > >> But I stated that my model is driven dynamically. It means, as an > >> example for one term in model I have: > >> > >> term_unca = 0 > >> for k in anion: > >> for j in cation: > >> for i in neutral: > >> term_unca += term_uncX - 2 * > >> sp.symbols('X{0}'.format(str(i))) * sp.symbols(f'X{str(j)}') * \ > >> sp.symbols(f'X{str(k)}') * ((cation[j] + > >> anion[k]) ** 2 / (cation[j] * anion[k])) \ > >> * sp.symbols(f'U{str(i)}{str(j)}{str(k)}') > >> This way all the parameters should be created for each run of fitting. > >> > >> > >> Zohreh Karimzadeh > >> https://www.researchgate.net/profile/Zohreh-Karimzadeh > >> Skype Name 49a52224a8b6b38b > >> Twitter Account @zohrehkarimzad1 > >> z.kari...@gmail.com > >> +989102116325 <+98%20910%20211%206325> > >> ((((((((((((((((Value Water))))))))))))))) > >> > >> Zohreh Karimzadeh > >> https://www.researchgate.net/profile/Zohreh-Karimzadeh > >> Skype Name 49a52224a8b6b38b > >> Twitter Account @zohrehkarimzad1 > >> z.kari...@gmail.com > >> +989102116325 <+98%20910%20211%206325> > >> ((((((((((((((((Value Water))))))))))))))) > >> > >> > >> On Fri, Aug 26, 2022 at 7:58 AM Peter Stahlecker > >> <peter.st...@gmail.com> wrote: > >> > > >> > Dear Zohreh, > >> > > >> > I think, you should describe precisely what you want, rather than > referring to previous correspondence. > >> > Someone out there (likely not me) might have a solution to what you > need - but cannot be bothered to read through you previous correspondence. > >> > > >> > 1. > >> > Parameter names should be made in a dynamic way. > >> > what exactly do you mean by this? > >> > Better give an example of what you would like to do. > >> > > >> > 2. > >> > how using for loop in string can be simultaneously handled. > >> > I have no idea, what your question is - and I am afraid, not many > people understand. > >> > Better give an example of what you want to do. > >> > > >> > Peter > >> > > >> > On Fri 26. Aug 2022 at 04:42 Zohreh Karimzadeh <z.kari...@gmail.com> > wrote: > >> >> > >> >> As can be seen in the former script, parameter names should be made > in a dynamic way. > >> >> I am wondering how using for loop in string can be simultaneously > handled. > >> >> Regards, > >> >> Zohreh Karimzadeh > >> >> https://www.researchgate.net/profile/Zohreh-Karimzadeh > >> >> Skype Name 49a52224a8b6b38b > >> >> Twitter Account @zohrehkarimzad1 > >> >> z.kari...@gmail.com > >> >> +989102116325 <+98%20910%20211%206325> > >> >> ((((((((((((((((Value Water))))))))))))))) > >> >> > >> >> > >> >> On Wed, Aug 24, 2022 at 9:32 PM gu...@uwosh.edu <gu...@uwosh.edu> > wrote: > >> >>> > >> >>> Zohreh, > >> >>> > >> >>> The code snippets you have provided do not suggest you are doing > anything that requires sympy. So, I am confused. It looks like you need a > function that generates a string of the summations in your expression, when > passed species. I think there is a fixed number of parameters for each > species. You are probably better off building the string for your > expressions without involving sympy. I am thinking something like this to > define part of your fit function: > >> >>> > >> >>> > >> >>> def term(species_name): > >> >>> > return(species_name+'_param1*conc_'+species_name+'+'+species_name+'_param2*np.sqrt(conc_'+species_name+')') > >> >>> Then > >> >>> term('Cl') > >> >>> returns > >> >>> 'Cl_param1*conc_Cl+Cl_param2*np.sqrt(conc_Cl)' > >> >>> > >> >>> Then build your overall expression from these pieces. Then do a fit > with initial guesses for all the parameters. > >> >>> > >> >>> That's how I would do it unless I am manipulating the expressions > using sympy first. > >> >>> > >> >>> Jonathan > >> >>> On Wednesday, August 24, 2022 at 11:11:58 AM UTC-5 > z.kari...@gmail.com wrote: > >> >>>> > >> >>>> I should extract the required model for each system of chemicals > and do a least squre for derived model and parameters to find the > parameters for each chemical systems. > >> >>>> > >> >>>> > >> >>>> Zohreh Karimzadeh > >> >>>> > >> >>>> Contact me on > >> >>>> +989102116325 <+98%20910%20211%206325> > >> >>>> and at > >> >>>> z.kari...@gmail.com > >> >>>> 🌧️🌍🌱 > >> >>>> > >> >>>> On Wed, 24 Aug 2022, 19:33 gu...@uwosh.edu, <gu...@uwosh.edu> > wrote: > >> >>>>> > >> >>>>> Zohreh, > >> >>>>> > >> >>>>> I pulled the paper. I see no reason you need to use SymPy to do > the numerical fitting. I would approach this by inputting the species in a > dictionary or list and then use that to call functions that generate the > computed value for each term/sum. Are you trying to keep track of the > complete symbolic expression or do a numerical calculation? > >> >>>>> > >> >>>>> If you are using chemical symbols or reactions, are you taking > advantage of ChemPy? > >> >>>>> > >> >>>>> Jonathan > >> >>>>> On Tuesday, August 23, 2022 at 12:01:02 AM UTC-5 > z.kari...@gmail.com wrote: > >> >>>>>> > >> >>>>>> As following, I should generate each parameters dynamically (for > 20-30 terms): > >> >>>>>> > >> >>>>>> 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 > >> >>>>>> > >> >>>>>> Zohreh Karimzadeh > >> >>>>>> https://www.researchgate.net/profile/Zohreh-Karimzadeh > >> >>>>>> Skype Name 49a52224a8b6b38b > >> >>>>>> Twitter Account @zohrehkarimzad1 > >> >>>>>> z.kari...@gmail.com > >> >>>>>> +989102116325 <+98%20910%20211%206325> > >> >>>>>> ((((((((((((((((Value Water))))))))))))))) > >> >>>>>> > >> >>>>>> > >> >>>>>> On Tue, Aug 23, 2022 at 9:26 AM Zohreh Karimzadeh < > z.kari...@gmail.com> wrote: > >> >>>>>>> > >> >>>>>>> Not these model and parameters, My model is very complicated > wit large number of parameters: > >> >>>>>>> > >> >>>>>>> As a very simple example I raise this question. > >> >>>>>>> Zohreh Karimzadeh > >> >>>>>>> > >> >>>>>>> Contact me on > >> >>>>>>> +989102116325 <+98%20910%20211%206325> > >> >>>>>>> and at > >> >>>>>>> z.kari...@gmail.com > >> >>>>>>> 🌧️🌍🌱 > >> >>>>>>> > >> >>>>>>> > >> >>>>>>> On Tue, 23 Aug 2022, 05:16 Peter Stahlecker, < > peter.st...@gmail.com> wrote: > >> >>>>>>>> > >> >>>>>>>> Dear Zohreh, > >> >>>>>>>> > >> >>>>>>>> Just for my understanding: > >> >>>>>>>> 1. > >> >>>>>>>> You params are alpha, betta ,gamma, eta (?) > >> >>>>>>>> 2. > >> >>>>>>>> What do you mean by generating them dynamically? > >> >>>>>>>> > >> >>>>>>>> Peter > >> >>>>>>>> > >> >>>>>>>> On Tue 23. Aug 2022 at 02:44 Zohreh Karimzadeh < > z.kari...@gmail.com> wrote: > >> >>>>>>>>> > >> >>>>>>>>> Dear Oscar > >> >>>>>>>>> BIG help ! > >> >>>>>>>>> Here I seriously need to use sympy to generate my params > dynamically. > >> >>>>>>>>> All Bests > >> >>>>>>>>> Zohreh Karimzadeh > >> >>>>>>>>> https://www.researchgate.net/profile/Zohreh-Karimzadeh > >> >>>>>>>>> Skype Name 49a52224a8b6b38b > >> >>>>>>>>> Twitter Account @zohrehkarimzad1 > >> >>>>>>>>> z.kari...@gmail.com > >> >>>>>>>>> +989102116325 <+98%20910%20211%206325> > >> >>>>>>>>> ((((((((((((((((Value Water))))))))))))))) > >> >>>>>>>>> > >> >>>>>>>>> > >> >>>>>>>>> On Mon, Aug 22, 2022 at 7:42 PM Oscar Benjamin < > oscar.j....@gmail.com> wrote: > >> >>>>>>>>>> > >> >>>>>>>>>> On Mon, 22 Aug 2022 at 15:36, Peter Stahlecker > >> >>>>>>>>>> <peter.st...@gmail.com> wrote: > >> >>>>>>>>>> > > >> >>>>>>>>>> > Dear Oscar, > >> >>>>>>>>>> > > >> >>>>>>>>>> > Thanks for your hint about these parameters!. > >> >>>>>>>>>> > Probably dumb question of mine: > >> >>>>>>>>>> > Could one not define f_Vi_est directly as > >> >>>>>>>>>> > > >> >>>>>>>>>> > def_Vi_est(gamma, alfa, beta, eta, L, K, VA): > >> >>>>>>>>>> > Vi_est = gamma - (1 / eta)….. > >> >>>>>>>>>> > return np.sum(…..) > >> >>>>>>>>>> > > >> >>>>>>>>>> > without any ‚lambdification‘? > >> >>>>>>>>>> > >> >>>>>>>>>> Yes. In an earlier post the OP showed that they had working > code like > >> >>>>>>>>>> that but wanted to know how to do it using SymPy and > lambdify. I > >> >>>>>>>>>> presume there is a reason for wanting to use SymPy there > (perhaps to > >> >>>>>>>>>> accomplish something slightly different from the exact code > shown). > >> >>>>>>>>>> It's also possible that there isn't any actual reason to use > SymPy for > >> >>>>>>>>>> this at all though. > >> >>>>>>>>>> > >> >>>>>>>>>> -- > >> >>>>>>>>>> Oscar > >> >>>>>>>>>> > >> >>>>>>>>>> -- > >> >>>>>>>>>> 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 > . > >> >>>>>>>>> > >> >>>>>>>>> -- > >> >>>>>>>>> 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/CA%2B1XYLOMV_i3VJXwwVN7JWnuJ9Wo6ZPhULmC1SOubAfFLt-DBQ%40mail.gmail.com > . > >> >>>>>>>> > >> >>>>>>>> -- > >> >>>>>>>> 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+un...@googlegroups.com. > >> >>>>>>>> To view this discussion on the web visit > https://groups.google.com/d/msgid/sympy/CABKqA0YTOzyZkjhnQmKQKQh-Mrc_H_TcmD9FrXObaTadkfLpkQ%40mail.gmail.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/17d62c5c-11d8-4b1b-9a44-ae67c8021832n%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/837a3351-3aa3-4e1e-98d6-b34f3185d5b2n%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/CA%2B1XYLMEq2OLN0npw8RHfghS7m6vgewkrpshqw8gBBU%2BfUAnoQ%40mail.gmail.com > . > >> > > >> > -- > >> > 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+un...@googlegroups.com. > >> > To view this discussion on the web visit > https://groups.google.com/d/msgid/sympy/CABKqA0b261cO3ixRjsp_YPU0Yk1ahRVgnEytcOkLrQUW-eHdwQ%40mail.gmail.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/ec94798a-fcdc-4df8-b80e-2642f5a60ec5n%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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/54b70a36-3ee5-4d14-b30a-813b8fbf31fcn%40googlegroups.com.