-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sympy expression with arbitrary variables/parameters #349
Comments
Hi @kevinxxq, thanks for your question! I'm not sure I understand your problem tough. The symfit model dict can have any number of elements in it, e.g. x_1, x_2, y_1, y_2 = variables('x_1, x_2, y_1, y_2')
y0, a_1, a_2, b_1, b_2 = parameters('y0, a_1, a_2, b_1, b_2')
model_dict = {
y_1: y0 + a_1 * exp(- b_1 * x_1),
y_2: y0 + a_2 * exp(- b_2 * x_2),
} such as the example taken from here: https://symfit.readthedocs.io/en/stable/fitting_types.html#fitting-multiple-datasets. Maybe that helps? If not, maybe you can reformulate the question? I've had a look at the stackoverflow question but that deals with automatically generating the model, is that your problem? |
Thank you for so quick reply. Not true. I don't know the left part of below declarification: The variables come from the caller when the function is called,the signature of my function is something as below.
Thank you. |
It's a bit unclear to me what you're exactly asking, and your code snippet also does not add clarity. The list_of_variables = variables('x, y, z')
x, y, z = list_of_variables # This is optional! You can also use list_of_variables[0] to get x, etc. |
@pckroon Thank you for involving. Yes, I understand that the variables are tuple unpacking. The problem is that I don't know the symbols for a sympy expression. The way it would be possible or I expect is:
Thank you. |
It's still not clear to me what you need/want. If you need the symbol names to pass the relevant data to Fit you can use dictionary unpacking. Otherwise I really need you to phrase your question more carefully: What do you want to achieve, what have you tried, what functionality are you missing. |
Do you know the number of variables and/or parameters? from symfit import variables, parameters
from functools import reduce
from operator import add
num_terms = 5
var_names = [f'x_{i}' for i in range(num_terms)]
par_names = [f'a_{i}' for i in range(num_terms)]
var_tup = variables(', '.join(var_names))
par_tup = parameters(', '.join(par_names))
expression = reduce(add, [a*v**i for i, (v, a) in enumerate(zip(var_tup, par_tup))])
expression
You can then do fitting by passing a dict with |
Thank you ALL, sorry that is me to consider it too simplistic or idealized: I just want to wrap the code in python to serve other clients who don't know anything about python. So I expect:
It may be impossible now, I will try to split it to serveral different functions. Thank you again. |
Ok. You'll need a way to turn your string expression into a sympy expression with the appropriate If you have a string of variables as you feed them to It's still not clear to me where you get stuck, we won't write your adapter function for you. |
Hi, @tBuLi , thank you for the package.
Just as https://stackoverflow.com/questions/55580926/how-to-extend-the-number-of-symfit-parameters-arbitrarily-in-python, I would like to wrap this package for api:
model: (dict of) sympy expression(s). e.g; a * x+ b
variables: x,y
parameters: a,b
Here is your example:
Now it turns to a different arbitrary way: a,b may be c,d, x,y may be x1,y1
I will pass a list of varibles and parameters such as "a, b, c, d" and "x, y, z" and a related sympy expression.
How can I handle it?
Thank you very much!
The text was updated successfully, but these errors were encountered: