On 10/29/10 5:59 PM, Oscar Gerardo Lazo Arjona wrote:
I want to propose the following changes to the output format of find_fit
and solve:

for find_fit the current output format is a list of equations:

sage: data = [(i, 1.2 * sin(0.5*i-0.2) + 0.1 * normalvariate(0, 1)) for
i in xsrange(0, 4*pi, 0.2)]
sage: var('a, b, c, x')
sage: model(x) = a * sin(b * x - c)
sage: find_fit(data, model)

[a == 1.2204167610363676, b == 0.50171964598627838, c
==0.22401763827376933]

or a dictionary:

sage: find_fit(data,model,solution_dict=True)

{c: 0.22401763827376933, b: 0.50171964598627838, a: 1.2204167610363676}

I'd like to get an expression where the values found for the parameters
are put in the model given to find_fit:

sage: find_fit(data, model)
1.2204167610363676*sin(0.50171964598627838 *x -0.22401763827376933 )


Why not make solution_dict=True the default, since then you could say:

fit=find_fit(data,model)
model.subs(fit)

to get the model with those parameters?

In fact, it would be great if this worked too:

fit=find_fit(data,model)
model(**fit)

but (even if solution_dict=True is the default) that complains that the keys in the dictionary are not strings.

However, we could make this work:

fit=find_fit(data,model)
model(fit)

but it require changes to the __call__ method to allow a dictionary input that would do the same as .subs() would do if handed a dictionary.


For solve the current output format depends on the input:

sage: var('x y')
sage: eq1=x+4==0
sage: eq2=x^2+4*x+2==0
sage: sys1=x+y==1
sage: sys2=x-2*y==-2
sage: solve(eq1,x)
[x == -4]
A list containing solutions (even when there is only one solution).

sage: solve(eq2,x)
[x == -sqrt(2) - 2, x == sqrt(2) - 2]
A list containing solutions when there is more than one solution

solve([sys1,sys2],x,y)
[[x == 0, y == 1]]
A list containing a list containing solutions (this is the worst)

The format I would like to see is:

sage: solve(eq1,x)
x == -4
A single equation

sage: solve(eq2,x)
[x == -sqrt(2) - 2, x == sqrt(2) - 2]
This one is fine

solve([sys1,sys2],x,y)
[x == 0, y == 1]
Just a list of solutions. It is harder to work with the solutions if
they are inside another list.

What if there are two different solutions, like (x=0, y=0) and (x=0,y=1)?

Personally, I think returning an equation is okay for situations where I am just checking something, and don't plan on using the solution again. Returning a list of equations is horrible for actually doing anything with the results, which is my intent in the vast majority of situations. Having to always append the not-very-obvious "solution_dict=True" to use solve in the common case is not very user-friendly. If we're changing the output of solve, I'd propose returning either a single dictionary (a single solution) or a list of dictionaries (multiple solutions). Solve seems like it would be much more useful that way.

Thanks,

Jason

--
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to