Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/clone/wp-conte.../plugins/ninja-fo.../includes/Librarie.../EOS
File: Parser.php
<?php
[0] Fix | Delete
[1] Fix | Delete
/*
[2] Fix | Delete
* MODIFICATIONS
[3] Fix | Delete
* - Removed `\` for php5.2 support (does not support namespaces).
[4] Fix | Delete
* - Renamed with prefix to avoid naming collisions.
[5] Fix | Delete
* - Updated references to the EOS Stack class to reflect name changes.
[6] Fix | Delete
*/
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* Equation Operating System Classes.
[10] Fix | Delete
*
[11] Fix | Delete
* This class was created for the safe parsing of mathematical equations
[12] Fix | Delete
* in PHP. There is a need for a way to successfully parse equations
[13] Fix | Delete
* in PHP that do NOT require the use of `eval`. `eval` at its core
[14] Fix | Delete
* opens the system using it to so many security vulnerabilities it is oft
[15] Fix | Delete
* suggested /never/ to use it, and for good reason. This class set will
[16] Fix | Delete
* successfully take an equation, parse it, and provide solutions to the
[17] Fix | Delete
* developer. It is a safe way to evaluate expressions without putting
[18] Fix | Delete
* the system at risk.
[19] Fix | Delete
*
[20] Fix | Delete
* 2015/07
[21] Fix | Delete
* - Added all real number factorial support
[22] Fix | Delete
* - Added gamma function to class
[23] Fix | Delete
*
[24] Fix | Delete
* 2014/08
[25] Fix | Delete
* - Added scientific notation support
[26] Fix | Delete
* - Added basic factorial support
[27] Fix | Delete
*
[28] Fix | Delete
* 2013/06 UPDATE:
[29] Fix | Delete
* - Added 'abs' (absolute value) support per tjbaron's update.
[30] Fix | Delete
*
[31] Fix | Delete
* 2013/04 UPDATE:
[32] Fix | Delete
* - Moved to native class functions for PHP5
[33] Fix | Delete
* - Removed deprecated `eregi` calls to `preg_match`
[34] Fix | Delete
* - Updated to PHPDoc comment syntax
[35] Fix | Delete
* - Added Exception throwing instead of silent exits
[36] Fix | Delete
* - Added additional variable prefix of '$', '&' is still allowed as well
[37] Fix | Delete
* - Fixed small implied multiplication problem
[38] Fix | Delete
*
[39] Fix | Delete
* @author Jon Lawrence <jlawrence11@gmail.com>
[40] Fix | Delete
* @copyright Copyright 2005-2015, Jon Lawrence
[41] Fix | Delete
* @license http://opensource.org/licenses/LGPL-2.1 LGPL 2.1 License
[42] Fix | Delete
* @package EOS
[43] Fix | Delete
* @version 2.2.1
[44] Fix | Delete
*/
[45] Fix | Delete
[46] Fix | Delete
require_once 'Stack.php';
[47] Fix | Delete
[48] Fix | Delete
/**
[49] Fix | Delete
* Equation Operating System (EOS) Parser
[50] Fix | Delete
*
[51] Fix | Delete
* An EOS that can safely parse equations from unknown sources returning
[52] Fix | Delete
* the calculated value of it. Can also handle solving equations with
[53] Fix | Delete
* variables, if the variables are defined (useful for the Graph creation
[54] Fix | Delete
* that the second and extended class in this file provides. {@see eqGraph})
[55] Fix | Delete
* This class was created for PHP4 in 2005, updated to fully PHP5 in 2013.
[56] Fix | Delete
*
[57] Fix | Delete
* @author Jon Lawrence <jlawrence11@gmail.com>
[58] Fix | Delete
* @copyright Copyright �2005-2013, Jon Lawrence
[59] Fix | Delete
* @license http://opensource.org/licenses/LGPL-2.1 LGPL 2.1 License
[60] Fix | Delete
* @package Math
[61] Fix | Delete
* @subpackage EOS
[62] Fix | Delete
* @version 2.2.1
[63] Fix | Delete
*/
[64] Fix | Delete
class NF_EOS_Parser {
[65] Fix | Delete
[66] Fix | Delete
/**
[67] Fix | Delete
* No matching Open/Close pair
[68] Fix | Delete
*/
[69] Fix | Delete
const E_NO_SET = 5500;
[70] Fix | Delete
[71] Fix | Delete
/**
[72] Fix | Delete
* Division by 0
[73] Fix | Delete
*/
[74] Fix | Delete
const E_DIV_ZERO = 5501;
[75] Fix | Delete
[76] Fix | Delete
/**
[77] Fix | Delete
* No Equation
[78] Fix | Delete
*/
[79] Fix | Delete
const E_NO_EQ = 5502;
[80] Fix | Delete
[81] Fix | Delete
/**
[82] Fix | Delete
* No variable replacement available
[83] Fix | Delete
*/
[84] Fix | Delete
const E_NO_VAR = 5503;
[85] Fix | Delete
[86] Fix | Delete
/**
[87] Fix | Delete
* Not a number
[88] Fix | Delete
*/
[89] Fix | Delete
const E_NAN = 5504;
[90] Fix | Delete
[91] Fix | Delete
/**
[92] Fix | Delete
* @var bool Activate Debug output.
[93] Fix | Delete
* @see __construct()
[94] Fix | Delete
* @see solveIF()
[95] Fix | Delete
*/
[96] Fix | Delete
public static $debug = FALSE;
[97] Fix | Delete
[98] Fix | Delete
/**#@+
[99] Fix | Delete
*Private variables
[100] Fix | Delete
*/
[101] Fix | Delete
private $postFix;
[102] Fix | Delete
private $inFix;
[103] Fix | Delete
/**#@-*/
[104] Fix | Delete
/**#@+
[105] Fix | Delete
* Protected variables
[106] Fix | Delete
*/
[107] Fix | Delete
//What are opening and closing selectors
[108] Fix | Delete
protected $SEP = array('open' => array('(', '['), 'close' => array(')', ']'));
[109] Fix | Delete
//Top presedence following operator - not in use
[110] Fix | Delete
protected $SGL = array('!');
[111] Fix | Delete
//Order of operations arrays follow
[112] Fix | Delete
protected $ST = array('^', '!');
[113] Fix | Delete
protected $ST1 = array('/', '*', '%');
[114] Fix | Delete
protected $ST2 = array('+', '-');
[115] Fix | Delete
//Allowed functions
[116] Fix | Delete
protected $FNC = array('sin', 'cos', 'tan', 'csc', 'sec', 'cot', 'abs', 'log', 'log10', 'sqrt');
[117] Fix | Delete
/**#@-*/
[118] Fix | Delete
/**
[119] Fix | Delete
* Construct method
[120] Fix | Delete
*
[121] Fix | Delete
* Will initiate the class. If variable given, will assign to
[122] Fix | Delete
* internal variable to solve with this::solveIF() without needing
[123] Fix | Delete
* additional input. Initializing with a variable is not suggested.
[124] Fix | Delete
*
[125] Fix | Delete
* @see Parser::solveIF()
[126] Fix | Delete
* @param String $inFix Standard format equation
[127] Fix | Delete
*/
[128] Fix | Delete
public function __construct($inFix = null) {
[129] Fix | Delete
if(defined('DEBUG') && DEBUG) {
[130] Fix | Delete
self::$debug = true;
[131] Fix | Delete
}
[132] Fix | Delete
$this->inFix = (isset($inFix)) ? $inFix : null;
[133] Fix | Delete
$this->postFix = array();
[134] Fix | Delete
}
[135] Fix | Delete
[136] Fix | Delete
/**
[137] Fix | Delete
* Check Infix for opening closing pair matches.
[138] Fix | Delete
*
[139] Fix | Delete
* This function is meant to solely check to make sure every opening
[140] Fix | Delete
* statement has a matching closing one, and throws an exception if
[141] Fix | Delete
* it doesn't.
[142] Fix | Delete
*
[143] Fix | Delete
* @param String $infix Equation to check
[144] Fix | Delete
* @throws Exception if malformed.
[145] Fix | Delete
* @return Bool true if passes - throws an exception if not.
[146] Fix | Delete
*/
[147] Fix | Delete
private function checkInfix($infix) {
[148] Fix | Delete
if(trim($infix) == "") {
[149] Fix | Delete
throw new Exception("No Equation given", NF_EOS_Parser::E_NO_EQ);
[150] Fix | Delete
}
[151] Fix | Delete
//Make sure we have the same number of '(' as we do ')'
[152] Fix | Delete
// and the same # of '[' as we do ']'
[153] Fix | Delete
if(substr_count($infix, '(') != substr_count($infix, ')')) {
[154] Fix | Delete
throw new Exception("Mismatched parenthesis in '{$infix}'", NF_EOS_Parser::E_NO_SET);
[155] Fix | Delete
} elseif(substr_count($infix, '[') != substr_count($infix, ']')) {
[156] Fix | Delete
throw new Exception("Mismatched brackets in '{$infix}'", NF_EOS_Parser::E_NO_SET);
[157] Fix | Delete
}
[158] Fix | Delete
$this->inFix = $infix;
[159] Fix | Delete
return true;
[160] Fix | Delete
}
[161] Fix | Delete
[162] Fix | Delete
/**
[163] Fix | Delete
* Infix to Postfix
[164] Fix | Delete
*
[165] Fix | Delete
* Converts an infix (standard) equation to postfix (RPN) notation.
[166] Fix | Delete
* Sets the internal variable $this->postFix for the Parser::solvePF()
[167] Fix | Delete
* function to use.
[168] Fix | Delete
*
[169] Fix | Delete
* @link http://en.wikipedia.org/wiki/Infix_notation Infix Notation
[170] Fix | Delete
* @link http://en.wikipedia.org/wiki/Reverse_Polish_notation Reverse Polish Notation
[171] Fix | Delete
* @param String $infix A standard notation equation
[172] Fix | Delete
* @throws Exception When parenthesis are mismatched.
[173] Fix | Delete
* @return Array Fully formed RPN Stack
[174] Fix | Delete
*/
[175] Fix | Delete
public function in2post($infix = null) {
[176] Fix | Delete
// if an equation was not passed, use the one that was passed in the constructor
[177] Fix | Delete
$infix = (isset($infix)) ? $infix : $this->inFix;
[178] Fix | Delete
[179] Fix | Delete
//check to make sure 'valid' equation
[180] Fix | Delete
$this->checkInfix($infix);
[181] Fix | Delete
$pf = array();
[182] Fix | Delete
$ops = new NF_EOS_Stack();
[183] Fix | Delete
//$vars = new Stack();
[184] Fix | Delete
[185] Fix | Delete
// remove all white-space
[186] Fix | Delete
$infix = preg_replace("/\s/", "", $infix);
[187] Fix | Delete
[188] Fix | Delete
// Create postfix array index
[189] Fix | Delete
$pfIndex = 0;
[190] Fix | Delete
[191] Fix | Delete
//what was the last character? (useful for decerning between a sign for negation and subtraction)
[192] Fix | Delete
$lChar = '';
[193] Fix | Delete
[194] Fix | Delete
//loop through all the characters and start doing stuff ^^
[195] Fix | Delete
for($i=0;$i<strlen($infix);$i++) {
[196] Fix | Delete
// pull out 1 character from the string
[197] Fix | Delete
$chr = substr($infix, $i, 1);
[198] Fix | Delete
[199] Fix | Delete
// if the character is numerical
[200] Fix | Delete
if(preg_match('/[0-9.]/i', $chr)) {
[201] Fix | Delete
// if the previous character was not a '-' or a number
[202] Fix | Delete
if((!preg_match('/[0-9.]/i', $lChar) && ($lChar != "")) && (isset($pf[$pfIndex]) && ($pf[$pfIndex]!="-")))
[203] Fix | Delete
$pfIndex++; // increase the index so as not to overlap anything
[204] Fix | Delete
// Add the number character to the array
[205] Fix | Delete
if(isset($pf[$pfIndex])) {
[206] Fix | Delete
$pf[$pfIndex] .= $chr;
[207] Fix | Delete
} else {
[208] Fix | Delete
$pf[$pfIndex] = $chr;
[209] Fix | Delete
}
[210] Fix | Delete
[211] Fix | Delete
}
[212] Fix | Delete
// If the character opens a set e.g. '(' or '['
[213] Fix | Delete
elseif(in_array($chr, $this->SEP['open'])) {
[214] Fix | Delete
// if the last character was a number, place an assumed '*' on the stack
[215] Fix | Delete
if(preg_match('/[0-9.]/i', $lChar))
[216] Fix | Delete
$ops->push('*');
[217] Fix | Delete
[218] Fix | Delete
$ops->push($chr);
[219] Fix | Delete
}
[220] Fix | Delete
// if the character closes a set e.g. ')' or ']'
[221] Fix | Delete
elseif(in_array($chr, $this->SEP['close'])) {
[222] Fix | Delete
// find what set it was i.e. matches ')' with '(' or ']' with '['
[223] Fix | Delete
$key = array_search($chr, $this->SEP['close']);
[224] Fix | Delete
// while the operator on the stack isn't the matching pair...pop it off
[225] Fix | Delete
while($ops->peek() != $this->SEP['open'][$key]) {
[226] Fix | Delete
$nchr = $ops->pop();
[227] Fix | Delete
if($nchr)
[228] Fix | Delete
$pf[++$pfIndex] = $nchr;
[229] Fix | Delete
else {
[230] Fix | Delete
throw new Exception("Error while searching for '". $this->SEP['open'][$key] ."' in '{$infix}'.", NF_EOS_Parser::E_NO_SET);
[231] Fix | Delete
}
[232] Fix | Delete
}
[233] Fix | Delete
$ops->pop();
[234] Fix | Delete
}
[235] Fix | Delete
// If a special operator that has precedence over everything else
[236] Fix | Delete
elseif(in_array($chr, $this->ST)) {
[237] Fix | Delete
while(in_array($ops->peek(), $this->ST))
[238] Fix | Delete
$pf[++$pfIndex] = $ops->pop();
[239] Fix | Delete
$ops->push($chr);
[240] Fix | Delete
$pfIndex++;
[241] Fix | Delete
}
[242] Fix | Delete
// Any other operator other than '+' and '-'
[243] Fix | Delete
elseif(in_array($chr, $this->ST1)) {
[244] Fix | Delete
while(in_array($ops->peek(), $this->ST1) || in_array($ops->peek(), $this->ST))
[245] Fix | Delete
$pf[++$pfIndex] = $ops->pop();
[246] Fix | Delete
[247] Fix | Delete
$ops->push($chr);
[248] Fix | Delete
$pfIndex++;
[249] Fix | Delete
}
[250] Fix | Delete
// if a '+' or '-'
[251] Fix | Delete
elseif(in_array($chr, $this->ST2)) {
[252] Fix | Delete
// if it is a '-' and the character before it was an operator or nothingness (e.g. it negates a number)
[253] Fix | Delete
if((in_array($lChar, array_merge($this->ST1, $this->ST2, $this->ST, $this->SEP['open'])) || $lChar=="") && $chr=="-") {
[254] Fix | Delete
// increase the index because there is no reason that it shouldn't..
[255] Fix | Delete
$pfIndex++;
[256] Fix | Delete
$pf[$pfIndex] = $chr;
[257] Fix | Delete
}
[258] Fix | Delete
// Otherwise it will function like a normal operator
[259] Fix | Delete
else {
[260] Fix | Delete
while(in_array($ops->peek(), array_merge($this->ST1, $this->ST2, $this->ST)))
[261] Fix | Delete
$pf[++$pfIndex] = $ops->pop();
[262] Fix | Delete
$ops->push($chr);
[263] Fix | Delete
$pfIndex++;
[264] Fix | Delete
}
[265] Fix | Delete
}
[266] Fix | Delete
// make sure we record this character to be referred to by the next one
[267] Fix | Delete
$lChar = $chr;
[268] Fix | Delete
}
[269] Fix | Delete
// if there is anything on the stack after we are done...add it to the back of the RPN array
[270] Fix | Delete
while(($tmp = $ops->pop()) !== false)
[271] Fix | Delete
$pf[++$pfIndex] = $tmp;
[272] Fix | Delete
[273] Fix | Delete
// re-index the array at 0
[274] Fix | Delete
$pf = array_values($pf);
[275] Fix | Delete
[276] Fix | Delete
// set the private variable for later use if needed
[277] Fix | Delete
$this->postFix = $pf;
[278] Fix | Delete
[279] Fix | Delete
// return the RPN array in case developer wants to use it fro some insane reason (bug testing ;]
[280] Fix | Delete
return $pf;
[281] Fix | Delete
} //end function in2post
[282] Fix | Delete
[283] Fix | Delete
/**
[284] Fix | Delete
* Solve Postfix (RPN)
[285] Fix | Delete
*
[286] Fix | Delete
* This function will solve a RPN array. Default action is to solve
[287] Fix | Delete
* the RPN array stored in the class from Parser::in2post(), can take
[288] Fix | Delete
* an array input to solve as well, though default action is preferred.
[289] Fix | Delete
*
[290] Fix | Delete
* @link http://en.wikipedia.org/wiki/Reverse_Polish_notation Postix Notation
[291] Fix | Delete
* @param Array $pfArray RPN formatted array. Optional.
[292] Fix | Delete
* @throws Exception On division by zero.
[293] Fix | Delete
* @return Float Result of the operation.
[294] Fix | Delete
*/
[295] Fix | Delete
public function solvePF($pfArray = null) {
[296] Fix | Delete
// if no RPN array is passed - use the one stored in the private var
[297] Fix | Delete
$pf = (!is_array($pfArray)) ? $this->postFix : $pfArray;
[298] Fix | Delete
[299] Fix | Delete
// create our temporary function variables
[300] Fix | Delete
$temp = array();
[301] Fix | Delete
//$tot = 0;
[302] Fix | Delete
$hold = 0;
[303] Fix | Delete
[304] Fix | Delete
// Loop through each number/operator
[305] Fix | Delete
for($i=0;$i<count($pf); $i++) {
[306] Fix | Delete
// If the string isn't an operator, add it to the temp var as a holding place
[307] Fix | Delete
if(!in_array($pf[$i], array_merge($this->ST, $this->ST1, $this->ST2))) {
[308] Fix | Delete
$temp[$hold++] = $pf[$i];
[309] Fix | Delete
}
[310] Fix | Delete
// ...Otherwise perform the operator on the last two numbers
[311] Fix | Delete
else {
[312] Fix | Delete
switch ($pf[$i]) {
[313] Fix | Delete
case '+':
[314] Fix | Delete
$temp[$hold-2] = $temp[$hold-2] + $temp[$hold-1];
[315] Fix | Delete
break;
[316] Fix | Delete
case '-':
[317] Fix | Delete
$temp[$hold-2] = $temp[$hold-2] - $temp[$hold-1];
[318] Fix | Delete
break;
[319] Fix | Delete
case '*':
[320] Fix | Delete
$temp[$hold-2] = $temp[$hold-2] * $temp[$hold-1];
[321] Fix | Delete
break;
[322] Fix | Delete
case '/':
[323] Fix | Delete
if($temp[$hold-1] == 0) {
[324] Fix | Delete
throw new Exception("Division by 0 on: '{$temp[$hold-2]} / {$temp[$hold-1]}' in {$this->inFix}", NF_EOS_Parser::E_DIV_ZERO);
[325] Fix | Delete
}
[326] Fix | Delete
$temp[$hold-2] = $temp[$hold-2] / $temp[$hold-1];
[327] Fix | Delete
break;
[328] Fix | Delete
case '^':
[329] Fix | Delete
$temp[$hold-2] = pow($temp[$hold-2], $temp[$hold-1]);
[330] Fix | Delete
break;
[331] Fix | Delete
case '!':
[332] Fix | Delete
$temp[$hold-1] = $this->factorial($temp[$hold-1]);
[333] Fix | Delete
$hold++;
[334] Fix | Delete
break;
[335] Fix | Delete
case '%':
[336] Fix | Delete
if($temp[$hold-1] == 0) {
[337] Fix | Delete
throw new Exception("Division by 0 on: '{$temp[$hold-2]} % {$temp[$hold-1]}' in {$this->inFix}", NF_EOS_Parser::E_DIV_ZERO);
[338] Fix | Delete
}
[339] Fix | Delete
$temp[$hold-2] = bcmod($temp[$hold-2], $temp[$hold-1]);
[340] Fix | Delete
break;
[341] Fix | Delete
}
[342] Fix | Delete
// Decrease the hold var to one above where the last number is
[343] Fix | Delete
$hold = $hold-1;
[344] Fix | Delete
}
[345] Fix | Delete
}
[346] Fix | Delete
// return the last number in the array
[347] Fix | Delete
return $temp[$hold-1];
[348] Fix | Delete
[349] Fix | Delete
} //end function solvePF
[350] Fix | Delete
[351] Fix | Delete
public function solve($equation, $values = null) {
[352] Fix | Delete
if(is_array($equation)) {
[353] Fix | Delete
return $this->solvePF($equation);
[354] Fix | Delete
} else {
[355] Fix | Delete
return $this->solveIF($equation, $values);
[356] Fix | Delete
}
[357] Fix | Delete
}
[358] Fix | Delete
[359] Fix | Delete
/**
[360] Fix | Delete
* Solve Infix (Standard) Notation Equation
[361] Fix | Delete
*
[362] Fix | Delete
* Will take a standard equation with optional variables and solve it. Variables
[363] Fix | Delete
* must begin with '&' or '$'
[364] Fix | Delete
* The variable array must be in the format of 'variable' => value. If
[365] Fix | Delete
* variable array is scalar (ie 5), all variables will be replaced with it.
[366] Fix | Delete
*
[367] Fix | Delete
* @param String $infix Standard Equation to solve
[368] Fix | Delete
* @param String|Array $vArray Variable replacement
[369] Fix | Delete
* @throws Exception On division by zero and on NaN and lack of variable replacement.
[370] Fix | Delete
* @return Float Solved equation
[371] Fix | Delete
*/
[372] Fix | Delete
function solveIF($infix, $vArray = null) {
[373] Fix | Delete
$infix = ($infix != "") ? $infix : $this->inFix;
[374] Fix | Delete
//Check to make sure a 'valid' expression
[375] Fix | Delete
$this->checkInfix($infix);
[376] Fix | Delete
[377] Fix | Delete
//$ops = new Stack();
[378] Fix | Delete
//$vars = new Stack();
[379] Fix | Delete
$hand = null;
[380] Fix | Delete
[381] Fix | Delete
//remove all white-space
[382] Fix | Delete
$infix = preg_replace("/\s/", "", $infix);
[383] Fix | Delete
if(NF_EOS_Parser::$debug) {
[384] Fix | Delete
$hand=fopen("eq.txt","a");
[385] Fix | Delete
}
[386] Fix | Delete
[387] Fix | Delete
//replace scientific notation with normal notation (2e-9 to 2*10^-9)
[388] Fix | Delete
$infix = preg_replace('/([\d])([eE])(-?\d)/', '$1*10^$3', $infix);
[389] Fix | Delete
[390] Fix | Delete
if(NF_EOS_Parser::$debug) {
[391] Fix | Delete
fwrite($hand, "$infix\n");
[392] Fix | Delete
}
[393] Fix | Delete
[394] Fix | Delete
// Finds all the 'functions' within the equation and calculates them
[395] Fix | Delete
// NOTE - when using function, only 1 set of parenthesis will be found, instead use brackets for sets within functions!!
[396] Fix | Delete
//while((preg_match("/(". implode("|", $this->FNC) . ")\(([^\)\(]*(\([^\)]*\)[^\(\)]*)*[^\)\(]*)\)/", $infix, $match)) != 0) {
[397] Fix | Delete
//Nested parenthesis are now a go!
[398] Fix | Delete
while((preg_match("/(". implode("|", $this->FNC) . ")\(((?:[^()]|\((?2)\))*+)\)/", $infix, $match)) != 0) {
[399] Fix | Delete
$func = $this->solveIF($match[2], $vArray);
[400] Fix | Delete
switch($match[1]) {
[401] Fix | Delete
case "cos":
[402] Fix | Delete
$ans = cos($func);
[403] Fix | Delete
break;
[404] Fix | Delete
case "sin":
[405] Fix | Delete
$ans = sin($func);
[406] Fix | Delete
break;
[407] Fix | Delete
case "tan":
[408] Fix | Delete
$ans = tan($func);
[409] Fix | Delete
break;
[410] Fix | Delete
case "sec":
[411] Fix | Delete
$tmp = cos($func);
[412] Fix | Delete
if($tmp == 0) {
[413] Fix | Delete
throw new Exception("Division by 0 on: 'sec({$func}) = 1/cos({$func})' in {$this->inFix}", NF_EOS_Parser::E_DIV_ZERO);
[414] Fix | Delete
}
[415] Fix | Delete
$ans = 1/$tmp;
[416] Fix | Delete
break;
[417] Fix | Delete
case "csc":
[418] Fix | Delete
$tmp = sin($func);
[419] Fix | Delete
if($tmp == 0) {
[420] Fix | Delete
throw new Exception("Division by 0 on: 'csc({$func}) = 1/sin({$func})' in {$this->inFix}", NF_EOS_Parser::E_DIV_ZERO);
[421] Fix | Delete
}
[422] Fix | Delete
$ans = 1/$tmp;
[423] Fix | Delete
break;
[424] Fix | Delete
case "cot":
[425] Fix | Delete
$tmp = tan($func);
[426] Fix | Delete
if($tmp == 0) {
[427] Fix | Delete
throw new Exception("Division by 0 on: 'cot({$func}) = 1/tan({$func})' in {$this->inFix}", NF_EOS_Parser::E_DIV_ZERO);
[428] Fix | Delete
}
[429] Fix | Delete
$ans = 1/$tmp;
[430] Fix | Delete
break;
[431] Fix | Delete
case "abs":
[432] Fix | Delete
$ans = abs($func);
[433] Fix | Delete
break;
[434] Fix | Delete
case "log":
[435] Fix | Delete
$ans = log($func);
[436] Fix | Delete
if(is_nan($ans) || is_infinite($ans)) {
[437] Fix | Delete
throw new Exception("Result of 'log({$func}) = {$ans}' is either infinite or a non-number in {$this->inFix}", NF_EOS_Parser::E_NAN);
[438] Fix | Delete
}
[439] Fix | Delete
break;
[440] Fix | Delete
case "log10":
[441] Fix | Delete
$ans = log10($func);
[442] Fix | Delete
if(is_nan($ans) || is_infinite($ans)) {
[443] Fix | Delete
throw new Exception("Result of 'log10({$func}) = {$ans}' is either infinite or a non-number in {$this->inFix}", NF_EOS_Parser::E_NAN);
[444] Fix | Delete
}
[445] Fix | Delete
break;
[446] Fix | Delete
case "sqrt":
[447] Fix | Delete
if($func < 0) {
[448] Fix | Delete
throw new Exception("Result of 'sqrt({$func}) = i. We can't handle imaginary numbers", NF_EOS_Parser::E_NAN);
[449] Fix | Delete
}
[450] Fix | Delete
$ans = sqrt($func);
[451] Fix | Delete
break;
[452] Fix | Delete
default:
[453] Fix | Delete
$ans = 0;
[454] Fix | Delete
break;
[455] Fix | Delete
}
[456] Fix | Delete
$infix = str_replace($match[0], "({$ans})", $infix);
[457] Fix | Delete
}
[458] Fix | Delete
[459] Fix | Delete
$infix = preg_replace('/[$&]/', "", $infix);
[460] Fix | Delete
//Find all the variables that were passed and replaces them
[461] Fix | Delete
while((preg_match('/([^a-zA-Z]){0,1}([a-zA-Z]+)([^a-zA-Z]){0,1}/', $infix, $match)) != 0) {
[462] Fix | Delete
[463] Fix | Delete
[464] Fix | Delete
//remove notices by defining if undefined.
[465] Fix | Delete
if(!isset($match[3])) {
[466] Fix | Delete
$match[3] = "";
[467] Fix | Delete
}
[468] Fix | Delete
[469] Fix | Delete
if(NF_EOS_Parser::$debug)
[470] Fix | Delete
fwrite($hand, "{$match[1]} || {$match[3]}\n");
[471] Fix | Delete
// Ensure that the variable has an operator or something of that sort in front and back - if it doesn't, add an implied '*'
[472] Fix | Delete
if((!in_array($match[1], array_merge($this->ST, $this->ST1, $this->ST2, $this->SEP['open'])) && $match[1] != "") || is_numeric($match[1])) //$this->SEP['close'] removed
[473] Fix | Delete
$front = "*";
[474] Fix | Delete
else
[475] Fix | Delete
$front = "";
[476] Fix | Delete
[477] Fix | Delete
if((!in_array($match[3], array_merge($this->ST, $this->ST1, $this->ST2, $this->SEP['close'])) && $match[3] != "") || is_numeric($match[3])) //$this->SEP['open'] removed
[478] Fix | Delete
$back = "*";
[479] Fix | Delete
else
[480] Fix | Delete
$back = "";
[481] Fix | Delete
[482] Fix | Delete
//Make sure that the variable does have a replacement
[483] Fix | Delete
//First check for pi and e variables that wll automagically be replaced
[484] Fix | Delete
if(in_array(strtolower($match[2]), array('pi', 'e'))) {
[485] Fix | Delete
$t = (strtolower($match[2])=='pi') ? pi() : exp(1);
[486] Fix | Delete
$infix = str_replace($match[0], $match[1] . $front. $t. $back . $match[3], $infix);
[487] Fix | Delete
} elseif(!isset($vArray[$match[2]]) && (!is_array($vArray != "") && !is_numeric($vArray) && 0 !== $vArray)) {
[488] Fix | Delete
throw new Exception("Variable replacement does not exist for '". substr($match[0], 1, 1). $match[2] ."' in {$this->inFix}", NF_EOS_Parser::E_NO_VAR);
[489] Fix | Delete
} elseif(!isset($vArray[$match[2]]) && (!is_array($vArray != "") && is_numeric($vArray))) {
[490] Fix | Delete
$infix = str_replace($match[0], $match[1] . $front. $vArray. $back . $match[3], $infix);
[491] Fix | Delete
} elseif(isset($vArray[$match[2]])) {
[492] Fix | Delete
$infix = str_replace($match[0], $match[1] . $front. $vArray[$match[2]]. $back . $match[3], $infix);
[493] Fix | Delete
}
[494] Fix | Delete
}
[495] Fix | Delete
[496] Fix | Delete
if(NF_EOS_Parser::$debug)
[497] Fix | Delete
fclose($hand);
[498] Fix | Delete
return $this->solvePF($this->in2post($infix));
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function