# How to find the number of parameters in the result of solve function?

I would like to find a way to obtain the number of parameters in the result of solve function. For example, in the following result of solve:

[{c0: -r77, c1: r77, c2: -r75 - r76, c3: 0, c4: -r77, c5: r76, c6: r75}]


the parameters are r75, r76, r77. So the number is 3. How to obtain this number automatically?

I think we can check each c[i] to see if it is a non-zero monomial. Then the number is the number of these non-zero monomials. But I don't know which function could check if a result in solve is a monomial.

Thank you very much.

edit retag close merge delete

Sort by ยป oldest newest most voted

Without some sample problem code, it is not easy to come up with sample solution code. So let's pontify...

You can extract the variables of a symbolic expression with the method .variables() ; this returns a tuple, which you can turn in a set thanks to the function set. Its difference() method allows you to substract the original parameters (present in the suystem before solution) ; what remains are the new parameters introduced by the solver.

What remains to do is to map such a function to the elements of the solution(s) and compute the union of the result.

Made-up example for an ordinary differential equation :

sage: Ovars=var("t, a, b, c")
sage: f=function("f")
sage: de=a*f(t).diff(t,2)+b*f(t).diff(t)+c==0
sage: with assuming(b!=0):Sol=desolve(de, f(t), ivar=t) ; Sol
_K2*e^(-b*t/a) + _K1 - (b*c*t - a*c)/b^2
sage: len(Params:=set(Sol.variables())-set(Ovars))
2
sage: Params
{_K2, _K1}


Extrapolate for multiple-elements solution(s) and/or multiple solutions, and season to taste. Serve cool...

HTH,

more