Algebra.js
function(symbol, v, raw) {
            if(!core.Utils.isSymbol(v))
                v = _.parse(v);
            var stop = function(msg) {
                msg = msg || 'Stopping';
                throw new core.exceptions.ValueLimitExceededError(msg);
            };
            //if not CP then nothing to do
            if(!symbol.isPoly()) 
                stop('Must be a polynomial!');
            //declare vars
            var deg, a, b, c, d, e, coeffs, sign, br, sym, sqrt_a;

            br = core.Utils.inBrackets;
            //make a copy
            symbol = symbol.clone();
            deg = core.Algebra.degree(symbol, v); //get the degree of polynomial
            //must be in form ax^2 +/- bx +/- c
            if(!deg.equals(2))
                stop('Cannot complete square for degree '+deg);
            //get the coeffs
            coeffs = core.Algebra.coeffs(symbol, v);
            a = coeffs[2];
            //store the sign
            sign = coeffs[1].sign(); 
            //divide the linear term by two and square it
            b = _.divide(coeffs[1], new Symbol(2));
            //add the difference to the constant
            c = _.pow(b.clone(), new Symbol(2));
            if(raw)
                return [a, b, d];
            sqrt_a = math.sqrt(a);
            e = _.divide(math.sqrt(c), sqrt_a.clone());
            //calculate d which is the constant
            d = _.subtract(coeffs[0], _.pow(e.clone(), new Symbol(2)));
            //compute the square part
            sym = _.parse(br(sqrt_a.clone()+'*'+v+(sign < 0 ? '-' : '+')+e));
            return {
                a: sym,
                c: d,
                f: _.add(_.pow(sym.clone(), new Symbol(2)), d.clone())
            };
        }
function(symbol, v, raw) {
            if(!core.Utils.isSymbol(v))
                v = _.parse(v);
            var stop = function(msg) {
                msg = msg || 'Stopping';
                throw new core.exceptions.ValueLimitExceededError(msg);
            };
            //if not CP then nothing to do
            if(!symbol.isPoly()) 
                stop('Must be a polynomial!');
            //declare vars
            var deg, a, b, c, d, e, coeffs, sign, br, sym, sqrt_a;

            br = core.Utils.inBrackets;
            //make a copy
            symbol = symbol.clone();
            deg = core.Algebra.degree(symbol, v); //get the degree of polynomial
            //must be in form ax^2 +/- bx +/- c
            if(!deg.equals(2))
                stop('Cannot complete square for degree '+deg);
            //get the coeffs
            coeffs = core.Algebra.coeffs(symbol, v);
            a = coeffs[2];
            //store the sign
            sign = coeffs[1].sign(); 
            //divide the linear term by two and square it
            b = _.divide(coeffs[1], new Symbol(2));
            //add the difference to the constant
            c = _.pow(b.clone(), new Symbol(2));
            if(raw)
                return [a, b, d];
            sqrt_a = math.sqrt(a);
            e = _.divide(math.sqrt(c), sqrt_a.clone());
            //calculate d which is the constant
            d = _.subtract(coeffs[0], _.pow(e.clone(), new Symbol(2)));
            //compute the square part
            sym = _.parse(br(sqrt_a.clone()+'*'+v+(sign < 0 ? '-' : '+')+e));
            return {
                a: sym,
                c: d,
                f: _.add(_.pow(sym.clone(), new Symbol(2)), d.clone())
            };
        }
function ( symbol , v , raw ) { if ( ! core . Utils . isSymbol ( v ) ) v = _ . parse ( v ) ; var stop = function ( msg ) { msg = msg || 'Stopping' ; throw new core . exceptions . ValueLimitExceededError ( msg ) ; } ; //if not CP then nothing to do
 if ( ! symbol . isPoly ( ) ) stop ( 'Must be a polynomial!' ) ; //declare vars
 var deg , a , b , c , d , e , coeffs , sign , br , sym , sqrt_a ; br = core . Utils . inBrackets ; //make a copy
 symbol = symbol . clone ( ) ; deg = core . Algebra . degree ( symbol , v ) ; //get the degree of polynomial
 //must be in form ax^2 +/- bx +/- c
 if ( ! deg . equals ( 2 ) ) stop ( 'Cannot complete square for degree ' + deg ) ; //get the coeffs
 coeffs = core . Algebra . coeffs ( symbol , v ) ; a = coeffs [ 2 ] ; //store the sign
 sign = coeffs [ 1 ] . sign ( ) ; //divide the linear term by two and square it
 b = _ . divide ( coeffs [ 1 ] , new Symbol ( 2 ) ) ; //add the difference to the constant
 c = _ . pow ( b . clone ( ) , new Symbol ( 2 ) ) ; if ( raw ) return [ a , b , d ] ; sqrt_a = math . sqrt ( a ) ; e = _ . divide ( math . sqrt ( c ) , sqrt_a . clone ( ) ) ; //calculate d which is the constant
 d = _ . subtract ( coeffs [ 0 ] , _ . pow ( e . clone ( ) , new Symbol ( 2 ) ) ) ; //compute the square part
 sym = _ . parse ( br ( sqrt_a . clone ( ) + '*' + v + ( sign < 0 ? '-' : '+' ) + e ) ) ; return { a : sym , c : d , f : _ . add ( _ . pow ( sym . clone ( ) , new Symbol ( 2 ) ) , d . clone ( ) ) } ; }
Attempts to complete the square of a polynomial
@param {type} symbol
@param {type} v
@param {type} raw
@throws {Error}
@returns {Object|Symbol[]}
Attempts to complete the square of a polynomial