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