Variable Isolation
Hi all, Is there a ready-to-use method to isolate a variable in an expression? At least for first and second degree polynomials in at most two variables. David E. Narvaez
Hi! On 12/12/2012 05:27 PM, David Narvaez wrote:
Is there a ready-to-use method to isolate a variable in an expression? At least for first and second degree polynomials in at most two variables.
I don't know what you mean by "isolate". Maybe the the collect and coeff methods are what you are looking for. -richy. -- Richard B. Kreckel
On Thu, Dec 13, 2012 at 3:04 AM, Richard B. Kreckel <kreckel@ginac.de> wrote:
I don't know what you mean by "isolate". Maybe the the collect and coeff methods are what you are looking for.
I mean something like isolate("x + y = 1", x) -> "x = 1 - y" isolate("(y+2)^2+x * (y+2)+1=0", yplus2) -> "y+2 = (-x + sqrt(x^2-4))/2" David E. Narváez
On 12/13/2012 01:30 PM, David Narvaez wrote:
On Thu, Dec 13, 2012 at 3:04 AM, Richard B. Kreckel<kreckel@ginac.de> wrote:
I don't know what you mean by "isolate". Maybe the the collect and coeff methods are what you are looking for.
I mean something like
isolate("x + y = 1", x) -> "x = 1 - y" isolate("(y+2)^2+x * (y+2)+1=0", yplus2) -> "y+2 = (-x + sqrt(x^2-4))/2"
For the linear case, check out lsolve. -richy. -- Richard B. Kreckel
On Thu, Dec 13, 2012 at 3:06 PM, Richard B. Kreckel <kreckel@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
participants (2)
-
David Narvaez
-
Richard B. Kreckel