Ask Your Question
2

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

asked 2023-07-28 11:27:46 +0100

lijr07 gravatar image

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 flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-07-29 16:40:39 +0100

Emmanuel Charpentier gravatar image

updated 2023-07-29 22:35:03 +0100

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,

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2023-07-28 11:27:46 +0100

Seen: 312 times

Last updated: Jul 29 '23