[GiNaC-list] Variable Isolation
David Narvaez
david.narvaez at computer.org
Mon Dec 17 05:02:57 CET 2012
On Thu, Dec 13, 2012 at 3:06 PM, Richard B. Kreckel <kreckel at ginac.de> wrote:
> For the linear case, check out lsolve.
Right, silly me. For the quadratic case, I ended up drafting this function
GiNaC::lst qsolve(const GiNaC::ex & quadratic_equation, const
GiNaC::symbol & solve_for)
{
GiNaC::lst solutions;
assert(quadratic_equation.nops() == 2);
GiNaC::ex lhs = *quadratic_equation.begin();
GiNaC::ex rhs = *(quadratic_equation.begin() + 1);
GiNaC::ex solvable_ex = (lhs - rhs).expand();
assert(solvable_ex.degree(solve_for) == 2);
GiNaC::ex a = solvable_ex.coeff(solve_for, 2);
GiNaC::ex b = solvable_ex.coeff(solve_for, 1);
GiNaC::ex c = solvable_ex.coeff(solve_for, 0);
solutions.append((-b + GiNaC::sqrt(GiNaC::pow(b, 2) - 4 * a * c)) / 2);
solutions.append((-b - GiNaC::sqrt(GiNaC::pow(b, 2) - 4 * a * c)) / 2);
return solutions;
}
Makes sense? How can it be improved?
David E. Narvaez
More information about the GiNaC-list
mailing list