diff --git a/lib/function/algebra/rationalize.js b/lib/function/algebra/rationalize.js index c65ade236..b625a94ad 100644 --- a/lib/function/algebra/rationalize.js +++ b/lib/function/algebra/rationalize.js @@ -1,51 +1,9 @@ 'use strict'; -////////////////// Debug Code /////////////////////////// - -// Source: https://stackoverflow.com/questions/14172455/get-name-and-line-of-calling-function-in-node-js - -//Object.defineProperty(global, '__stack', { -//get: function() { -// var orig = Error.prepareStackTrace; -// Error.prepareStackTrace = function(_, stack) { -// return stack; -// }; -// var err = new Error; -// Error.captureStackTrace(err, arguments.callee); -// var stack = err.stack; -// Error.prepareStackTrace = orig; -// return stack; -// } -//}); -// -//Object.defineProperty(global, '__line', { -//get: function() { -// return __stack[2].getLineNumber(); -// } -//}); -// -//Object.defineProperty(global, '__function', { -//get: function() { -// return __stack[2].getFunctionName(); -// } -//}); -// -////////////////// Debug Code /////////////////////////// -// -//function retStr(expr) { //XXX -// return expr.toString({parenthesis: 'all'}).replace(/ /g,''); // st + "> " + -//} -// - -//var count=0 //XXX counter of calls that changes an expression -//var prev=""; //XXX previous expression before calls parse or simplify -//var glob=""; //XXX global string for print in the error - function factory (type, config, load, typed) { var simplify = load(require('./simplify')); - var simplifyCore = load(require('./simplify/simplifyCore')); //NNN - var simplifyConstant = load(require('./simplify/simplifyConstant')); //NNN - + var simplifyCore = load(require('./simplify/simplifyCore')); + var simplifyConstant = load(require('./simplify/simplifyConstant')); var ArgumentsError = require('../../error/ArgumentsError'); var parse = load(require('../../expression/function/parse')); var number = require('../../utils/number') @@ -100,20 +58,20 @@ function factory (type, config, load, typed) { */ var rationalize = typed('rationalize', { 'string': function (expr) { - return rationalize(parse(expr), {}, false); //XXX + return rationalize(parse(expr), {}, false); }, 'string, boolean': function (expr, detailed) { - return rationalize(parse(expr), {} , detailed); //XXX + return rationalize(parse(expr), {} , detailed); }, 'string, Object': function (expr, scope) { - return rationalize(parse(expr), scope, false); //XXX + return rationalize(parse(expr), scope, false); }, 'string, Object, boolean': function (expr, scope, detailed) { - return rationalize(parse(expr), scope, detailed); //XXX + return rationalize(parse(expr), scope, detailed); }, 'Node': function (expr) { @@ -143,7 +101,7 @@ function factory (type, config, load, typed) { var rules; var eDistrDiv = true - expr = simplify(expr, setRules.firstRules); // Apply the initial rules, including succ div rules //XXX + expr = simplify(expr, setRules.firstRules); // Apply the initial rules, including succ div rules s = expr.toString(); @@ -160,9 +118,9 @@ function factory (type, config, load, typed) { } if (redoInic) { // Apply first rules again without succ div rules (if there are changes) - expr = simplify(expr,setRules.firstRulesAgain); //XXX + expr = simplify(expr,setRules.firstRulesAgain); } - expr = simplify(expr,setRules.finalRules); // Aplly final rules //XXX + expr = simplify(expr,setRules.finalRules); // Aplly final rules } // NVars >= 1 @@ -171,9 +129,6 @@ function factory (type, config, load, typed) { if (nVars==1) { if (expr.type==='OperatorNode' && expr.op==='/') { // Separate numerator from denominator - //console.log(retStr('N',expr.args[0])) //XXX - //console.log(retStr('D',expr.args[1])) //XXX - // glob = retStr("N",expr.args[0]); expr.args[0] = polyToCanonical(expr.args[0],coefficients); expr.args[1] = polyToCanonical(expr.args[1]); if (detailed) { @@ -184,7 +139,7 @@ function factory (type, config, load, typed) { expr = polyToCanonical(expr,coefficients); if (detailed) { retRationalize.numerator = expr; - retRationalize.denominator = null // new ConstantNode(1); + retRationalize.denominator = null } } } else { @@ -192,11 +147,6 @@ function factory (type, config, load, typed) { retRationalize.denominator = null; } // nVars - // console.log(glob); //XXX - // count = 0; - // prev = "" - // glob = ""; - if (! detailed) return expr; retRationalize.coefficients = coefficients; retRationalize.variables = polyRet.variables; @@ -205,259 +155,6 @@ function factory (type, config, load, typed) { } // ^^^^^^^ end of rationalize ^^^^^^^^ }); // end of typed rationalize -//////////////////// Debug Code /////////////////////////// -// -// //--------------------- procMidTok ------------------------------ -// // break s in position p (next char). -// // return new position backwards without -// // break a number or identifier. -// //-------------------------------------------------------------- -// function procMidTok(s, pos) { -// if (pos>=s.length) return pos; -// -// var ch; // current character -// var pBack; -// var digs = "0123456789"; -// var lets = "abcdefghilklmnoprstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$"; -// -// var isDig; // Status of current character -// var isLet; -// var isExpo; -// var isSign; -// var isDot; -// -// var pBack = pos; // backward cursor -// var inic = true; -// var canNumber = true; -// var canId = true; -// -// var pastExpo = false; // Already occurs "e" or "E" -// var pastDot = false; // Already occurs "." -// var pastSign = false; // Already occurs "-" or "+" -// -// var succExpo = false; // Previous character backwards is +,-,digit -// var succSign = false; -// var succDot = true; -// -// var prevExpo = false; -// var prevSign = false; -// var prevNumber= false; -// var prevDot= false; -// -// var triggerSign=false; -// -// if (pos+1=0) { // loop -// -// ch = s[pBack]; // current character -// isDig = digs.indexOf(ch)!==-1; // is digit -// isLet = lets.indexOf(ch)!==-1; // is letter -// isDot = ch==="." && canNumber ; // is dot -// isExpo = ( ch==="e" || ch==="E") && canNumber ; // is expo -// isSign = ( ch==="+" || ch==="-" ) && canNumber ; // is sign -// -// if (prevSign) { // if previous is signal -// triggerSign = ! isExpo; // wait for "e" or it's the end. Get piece after sign -// canNumber = isExpo; -// } else if (prevExpo && ! isDig && ! isDot ) { // before "e" needs a digit or dot -// prevExpo = false; -// pastExpo = false -// triggerSign = pastSign // Is not a number, get token after sign te23 -// canNumber = ! triggerSign -// // There is a situation that don't trigger, but it is catched below. -// // Ex: d23232323e+23, the piece is 23. -// } -// -// // number: digit, signal, dot (just once), expo (just once) -// if (canNumber) canNumber = isDig || isDot || isExpo || isSign; -// -// // id: letter, digit -// if (canId) canId = isLet || isDig -// -// // expo just before "+","-",digit. Never two "e" in number. Nuver before "." -// if (isExpo && canNumber) -// canNumber = succExpo && ! pastDot && ! pastExpo; -// -// // Signal just before digit/dot and ends before "e" or "." -// if (isSign && canNumber) -// canNumber = succSign && ! pastExpo && ! pastDot; -// -// // Dot not after letter,dot or signal. Never 2 dots. -// if (isDot && canNumber) { -// canNumber = succDot && ! pastDot; -// } -// -// // Invalid character in an identifier or decimal number -// if (! canNumber && ! canId ) { -// if (triggerSign || (pastSign && ! canNumber)) // Sign not immediately after "NUMBER e" (exponential notation) -// pBack = pSave; -// else -// pBack = Math.min(pBack+1,pos); // index of first character from id or number without the sign -// return pBack -// } -// -// pastSign = isSign || pastSign -// pastExpo = isExpo || pastExpo; -// pastDot = isDot || pastDot; -// -// succExpo = isDig || isSign; -// succSign = isDig || isDot; -// succDot = ! isLet && ! isSign && ! isDot -// -// prevNumber = canNumber; -// -// prevExpo = isExpo; // Previous character is expo? -// prevDot = isDot; -// prevSign = isSign; -// -// if (isDig || isDot) pSave = pBack; // Save last digit or dot position -// pBack -- -// } // while -// -// return 0; -// } -// -// -// //--------------------- breakStr ------------------------------ -// // break string "s" in pieces of length "len" -// // if type empty no handling -// // if type="#" includes "// " in each line (javascript comment) -// // if type = '"' is character that needs occurs even times -// // break line in the middle of a quoted string -// //-------------------------------------------------------------- -// function breakStr(s, len, type="") { -// var isComm = (type==="#"); // Is comment? -// var piece; // piece of string -// var n = s.length; // total length -// var x = ""; // exit -// var pNew; // changed index -// var ok; -// if (type!=="") -// var patt = new RegExp(type,"g"); -// -// if (n < len) return (isComm ? "// ":"") + s + "\n"; -// var pos = 0; -// while (pos <= n-1) { -// piece = (isComm ? "// ": "") + s.slice(pos,pos+len) -// -// pos += len; -// pNew = pos; -// ok = isComm; -// -// if (! isComm && (type!=="")) { // Has quota delimiter -// var k = (piece.match(patt) || []).length; // count characters -// if (k % 2 ===1) { // Odd number of quotes? -// pNew = s.lastIndexOf(type,pos-1)-1; // Search next even quote and skip back -// ok = true; -// } // odd quote -// } // search even quote -// -// if (! isComm && ! ok) // Can be in the middle of token (id/number) -// var pNew = procMidTok(s,pos); -// -// if (pos !== pNew) { -// piece = piece.slice(0,len-(pos-pNew)); -// pos = pNew; -// } -// x += piece + "\n"; // If nothing special -// } // while -// return x -// } -// -// -// // ----------------------- funcCommon ---------------------- -// // Common piece for "simplifyDeb" and "parseDeb" functions -// // ---------------------------------------------------------- -// function funcCommon(func,expr,rules,lin) { -// var actual; -// var len = 70; -// count += 1; -// if (prev === "") { -// prev = expr; -// glob += breakStr(count + "> Initial Expression: " + expr + " before " + func.name +" call(" + lin + ")",len,"#") ; -// glob += breakStr("var expr = " + expr + ";",len); // Expressão inicial -// } else { -// actual = retStr(expr); -// if (prev === actual) -// glob += "// " + count + "> No expression change before " + func.name +" call(" + lin + ")\n" ; -// else -// glob += breakStr(count + "> Expression in " + func.name +" call [" + prev + "] changes to " + actual,len,"#"); -// } -// -// strRules = JSON.stringify(rules); -// if (func.name==="simplify") { // Simplify -// var strRules; -// if (Array.isArray(rules)) { // Simplify call with rules -// var erro=false; -// if (typeof(rules[0])==="function") { // Simplify call include old rules. -// erro = (typeof(rules[3]) !=="function"); -// if (erro) glob += "// " + count + '> Error in rules. Function expected in index 3\n'; -// if (! erro) { -// erro = (rules[0].name !== "simplifyCore"); -// if (erro) glob += "// " + count + '> Error in rules. simplifyCore function expected in index 0\n'; -// } -// if (! erro) { -// erro = (rules[3].name !== "simplifyConstant"); -// if (erro) glob += "// " + count + '> Error in rules. simplifyConstant function expected in index 14\n'; -// } -// if (! erro) { -// strRules = strRules.replace("[null,","[simplifyCore,"); -// strRules = strRules.replace(",null,",",simplifyConstant,"); -// } -// } -// glob += breakStr("var rules = " + strRules + ";",len,'"'); // rule assignment in generated source -// } -// glob += "expr = simplify(expr,rules);\n"; // simplify call in generated source -// var node = func(expr,rules); // real execution -// } // end of simplify part -// -// else { // parse part -// glob += "expr = parse(expr);\n" // parse call in generated source -// var node = func(expr); // real execution -// } // end of parse part -// -// prev = retStr(node); -// glob += breakStr(count + "> Expression in " + func.name +" call (" + lin + ") changes to " + prev,len,"#"); -// glob += "// ==========================\n"; -// return node; -// } -// -// // ----------------------- simplifyDeb ---------------------- -// // "simplify" encapsulated for debug action -// // ---------------------------------------------------------- -// -// function simplifyDeb(expr,rules) { ///XXX -// var lin = __line; -// node = funcCommon(simplify,expr,rules,lin); -// return node; -// } -// -// -// // ----------------------- parseDeb ---------------------- -// // "parse" encapsulated for debug action -// // ---------------------------------------------------------- -// function parseDeb(expr) { ///XXX -// var lin=__line; -// node = funcCommon(parse,expr,undefined,lin); -// return node; -// } - -////////////////// End of Debug Code /////////////////////////// - - /** * Function to simplify an expression using an optional scope and * return it if the expression is a polynomial expression, i.e. @@ -480,7 +177,7 @@ function factory (type, config, load, typed) { function polynomial (expr, scope, extended) { var variables = []; - var node = simplify(expr,scope); // Resolves any variables and functions with all defined parameters //XXX + var node = simplify(expr,scope); // Resolves any variables and functions with all defined parameters extended = !! extended var oper = '+-*' + (extended ? '/' : ''); @@ -551,16 +248,6 @@ function factory (type, config, load, typed) { * @return {array} rule set to rationalize an polynomial expression */ function rulesRationalize() { -// var sCore; // simplifyCore function -// var sConstant; // simplifyConstant function - -// simplify.rules.forEach(function(elem) { // simplify.rules - Standard simplify rules -// if (elem.name) { -// if (elem.name==="simplifyCore") sCore = elem; -// if (elem.name==="simplifyConstant") sConstant = elem; -// } -// }); - var oldRules = [simplifyCore, // sCore {l:"n+n",r:"2*n"}, {l:"n+-n",r:"0"}, @@ -831,13 +518,12 @@ function factory (type, config, load, typed) { // -,+,* : children of +,- if ((node.fn==='subtract' || node.fn==='add' || node.fn==='multiply') && noPai.fn!=='add' && noPai.fn!=='subtract' ) - throw new ArgumentsError('Invalid ' + node.op + ' placing'); //XXX Append : [' + glob + ']') + throw new ArgumentsError('Invalid ' + node.op + ' placing'); // -,+ : first child -// if ((node.fn==='subtract' || node.fn==='add' || //TTT -// node.fn==='unaryMinus' ) && o.noFil!==0 ) -// throw new ArgumentsError('Invalid ' + node.op + ' placing') - + if ((node.fn==='subtract' || node.fn==='add' || + node.fn==='unaryMinus' ) && o.noFil!==0 ) + throw new ArgumentsError('Invalid ' + node.op + ' placing') } // Has parent // Firers: ^,* Old: ^,&,-(unary): firers