To be clear I am looking for an equation to go from
$$Ax^2 + Bx + C = 0$$
To
$$(Dx + E)(Fx + G) = 0$$
And I need it to be able to be done in a computer as it will be going in my app.
Thanks in Advance!
To be clear I am looking for an equation to go from
$$Ax^2 + Bx + C = 0$$
To
$$(Dx + E)(Fx + G) = 0$$
And I need it to be able to be done in a computer as it will be going in my app.
Thanks in Advance!
Using the naive quadratic formula has poor numeric stability properties (although it always works fine on problems you are given in 9th grade algebra). When either $A$ or $C$ is is small, then one of the roots will involve subtraction of two nearly-equal quantities, with a loss of accuracy.
The correct way to compute the roots is to first compute $$M = -\frac12\left( B+ \mbox{sign of }(B) \sqrt{B^2-4AC} \right)$$
and then the equation factors into $$A \left(x-\frac{M}{A}\right)\left(x-\frac{C}{M}\right) = 0 $$ See Numeric Recipes.
A straightforward way is to divide by $A$ and solve that equation with the quadratic formula. Then if your roots of that equation are $r,s$ then the factorization is
$$(x-r)(x-s)=0.$$
If you're only interested in working with equations whose roots are real and rational, then your equation will have $r=r_1/r_2$ and $s = s_1/s_2$ where $r_1, r_2, s_1, s_2$ are integers, $r_2, s_2 \neq 0$.
Then in this special case just multiply by $r_2, s_2$:
$$(r_2 x - r_1)(s_2 x - s_1) = 0.$$
You can just use the quadratic formula: $$ \left( x - \left(\frac{-B + \sqrt{B^2 - 4AC}}{2A}\right)\right)\left( x - \left(\frac{-B - \sqrt{B^2 - 4AC}}{2A}\right)\right)=0 $$
jagy@phobeusjunior:~$ ./factor 30 97 55
( 2 x + 5 ) ( 15 x + 11 )
jagy@phobeusjunior:~$ ./factor 1 2 1
( 1 x + 1 ) ( 1 x + 1 )
jagy@phobeusjunior:~$ ./factor 4096 100000 390625
( 32 x + 625 ) ( 128 x + 625 )
jagy@phobeusjunior:~$ ./factor 4096 100000 390623
non-square discriminant
jagy@phobeusjunior:~$ ./factor 4096 100000 3906237777
negative discriminant
jagy@phobeusjunior:~$
===============================================
SUMMARY: with $$ \color{blue}{b^2 - 4ac = \delta^2,} $$ take $$ \color{blue}{ a_1 = \gcd \left( a, \frac{(b + \delta)}{2} \right) ; \; \; \; \; \; a_2 = \frac{a}{a_1},} $$ then $$ \color{blue}{ a x^2 + b x y + c y^2 = \; \left(a_2x+ \left( \frac{b + \delta}{2a_1} \right) y \right) \; \; \left(a_1x+ \left( \frac{b - \delta}{2a_2} \right) y \right) \; } $$ in integers. If you do not want the variable $y,$ set $$ y=1. $$
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <strstream>
#include <list>
#include <set>
#include <math.h>
#include <iomanip>
#include <string>
#include <algorithm>
#include <iterator>
#include <gmp.h>
#include <gmpxx.h>
using namespace std;
// g++ -o factor factor.cc -lgmp -lgmpxx
mpz_class mp_Sqrt( mpz_class w)
{
if (w <= 0) return 0;
else if (w < 2) return 1;
else
{
mpz_class squirt;
mpz_sqrt( squirt.get_mpz_t(), w.get_mpz_t() );
return squirt ;
} // else w >= 2;
}
mpz_class mp_GCD( mpz_class i, mpz_class j )
{
if ( i < 0 ) i *= -1 ; // should work within scope
if ( j < 0 ) j *= -1 ;
if ( i == 0) return j; // gives gcd(0,0) = 0, practical choice
if ( j == 0) return i;
if( i < j ) return mp_GCD( i , j % i ) ;
else return mp_GCD( j , i % j ) ;
}
int main(int argc, char *argv[])
{
if ( argc != 4) cout << "Usage: ./factor A B C " << endl;
else {
int aa,bb,cc;
aa = atoi(argv[1]);
bb = atoi(argv[2]);
cc = atoi(argv[3]);
mpz_class a,b,c;
a = aa; b = bb; c = cc;
mpz_class discr = b * b - 4 * a * c;
if ( discr < 0 ) cout << " negative discriminant " << endl;
else{
mpz_class d = mp_Sqrt(discr);
if( d * d != discr) cout << " non-square discriminant " << endl;
else {
mpz_class a1, a2;
a1 = mp_GCD( a, (b + d)/2 );
a2 = a / a1;
cout << "( " << a2 << " x + " << (b + d)/(2 * a1) << " ) ( " << a1 << " x + " << (b - d)/(2 * a2) << " ) " << endl;
} // else square
} // else disc >= 0
} // else argc
return 0 ;
}
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Quantity under radical sign should be a perfect square.
Standard quadratic equation factorization is :
$$ \left( 2Ax +B + \sqrt{B^2 - 4AC}\right) \left( 2Ax +B - \sqrt{B^2 - 4AC}\right) =0 $$
The first part is the derivative and the second part, discriminant root.
Also, will this example help?
$$ m^2x^2+(m^2+n^2)x+n^2=0 $$
factors to
$$ (x+1) (m^2 x +n^2) = 0. $$