From 1efa7e853314097455b07fb3d8c9981906fd16d5 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Sun, 10 May 2015 23:25:04 +0100 Subject: [PATCH] More PSR-2 adjustments --- Classes/PHPExcel/Calculation/LookupRef.php | 347 ++--- Classes/PHPExcel/Calculation/MathTrig.php | 503 ++++--- Classes/PHPExcel/Calculation/Statistical.php | 1408 ++++++++++-------- Classes/PHPExcel/Calculation/TextData.php | 299 ++-- Classes/PHPExcel/Calculation/Token/Stack.php | 30 +- 5 files changed, 1364 insertions(+), 1223 deletions(-) diff --git a/Classes/PHPExcel/Calculation/LookupRef.php b/Classes/PHPExcel/Calculation/LookupRef.php index 86208f0d..640f0c2e 100644 --- a/Classes/PHPExcel/Calculation/LookupRef.php +++ b/Classes/PHPExcel/Calculation/LookupRef.php @@ -1,6 +1,16 @@ '') { - if (strpos($sheetText,' ') !== False) { $sheetText = "'".$sheetText."'"; } + if (strpos($sheetText, ' ') !== false) { + $sheetText = "'".$sheetText."'"; + } $sheetText .='!'; } if ((!is_bool($referenceStyle)) || $referenceStyle) { $rowRelative = $columnRelative = '$'; $column = PHPExcel_Cell::stringFromColumnIndex($column-1); - if (($relativity == 2) || ($relativity == 4)) { $columnRelative = ''; } - if (($relativity == 3) || ($relativity == 4)) { $rowRelative = ''; } + if (($relativity == 2) || ($relativity == 4)) { + $columnRelative = ''; + } + if (($relativity == 3) || ($relativity == 4)) { + $rowRelative = ''; + } return $sheetText.$columnRelative.$column.$rowRelative.$row; } else { - if (($relativity == 2) || ($relativity == 4)) { $column = '['.$column.']'; } - if (($relativity == 3) || ($relativity == 4)) { $row = '['.$row.']'; } + if (($relativity == 2) || ($relativity == 4)) { + $column = '['.$column.']'; + } + if (($relativity == 3) || ($relativity == 4)) { + $row = '['.$row.']'; + } return $sheetText.'R'.$row.'C'.$column; } - } // function CELL_ADDRESS() + } /** @@ -109,33 +110,36 @@ class PHPExcel_Calculation_LookupRef { * @param cellAddress A reference to a range of cells for which you want the column numbers * @return integer or array of integer */ - public static function COLUMN($cellAddress=Null) { - if (is_null($cellAddress) || trim($cellAddress) === '') { return 0; } + public static function COLUMN($cellAddress = null) + { + if (is_null($cellAddress) || trim($cellAddress) === '') { + return 0; + } if (is_array($cellAddress)) { - foreach($cellAddress as $columnKey => $value) { - $columnKey = preg_replace('/[^a-z]/i','',$columnKey); + foreach ($cellAddress as $columnKey => $value) { + $columnKey = preg_replace('/[^a-z]/i', '', $columnKey); return (integer) PHPExcel_Cell::columnIndexFromString($columnKey); } } else { - if (strpos($cellAddress,'!') !== false) { - list($sheet,$cellAddress) = explode('!',$cellAddress); + if (strpos($cellAddress, '!') !== false) { + list($sheet, $cellAddress) = explode('!', $cellAddress); } - if (strpos($cellAddress,':') !== false) { - list($startAddress,$endAddress) = explode(':',$cellAddress); - $startAddress = preg_replace('/[^a-z]/i','',$startAddress); - $endAddress = preg_replace('/[^a-z]/i','',$endAddress); + if (strpos($cellAddress, ':') !== false) { + list($startAddress, $endAddress) = explode(':', $cellAddress); + $startAddress = preg_replace('/[^a-z]/i', '', $startAddress); + $endAddress = preg_replace('/[^a-z]/i', '', $endAddress); $returnValue = array(); do { $returnValue[] = (integer) PHPExcel_Cell::columnIndexFromString($startAddress); } while ($startAddress++ != $endAddress); return $returnValue; } else { - $cellAddress = preg_replace('/[^a-z]/i','',$cellAddress); + $cellAddress = preg_replace('/[^a-z]/i', '', $cellAddress); return (integer) PHPExcel_Cell::columnIndexFromString($cellAddress); } } - } // function COLUMN() + } /** @@ -149,7 +153,8 @@ class PHPExcel_Calculation_LookupRef { * @param cellAddress An array or array formula, or a reference to a range of cells for which you want the number of columns * @return integer The number of columns in cellAddress */ - public static function COLUMNS($cellAddress = null) { + public static function COLUMNS($cellAddress = null) + { if (is_null($cellAddress) || $cellAddress === '') { return 1; } elseif (!is_array($cellAddress)) { @@ -158,14 +163,14 @@ class PHPExcel_Calculation_LookupRef { reset($cellAddress); $isMatrix = (is_numeric(key($cellAddress))); - list($columns,$rows) = PHPExcel_Calculation::_getMatrixDimensions($cellAddress); + list($columns, $rows) = PHPExcel_Calculation::_getMatrixDimensions($cellAddress); if ($isMatrix) { return $rows; } else { return $columns; } - } // function COLUMNS() + } /** @@ -182,34 +187,37 @@ class PHPExcel_Calculation_LookupRef { * @param cellAddress A reference to a range of cells for which you want the row numbers * @return integer or array of integer */ - public static function ROW($cellAddress=Null) { - if (is_null($cellAddress) || trim($cellAddress) === '') { return 0; } + public static function ROW($cellAddress = null) + { + if (is_null($cellAddress) || trim($cellAddress) === '') { + return 0; + } if (is_array($cellAddress)) { - foreach($cellAddress as $columnKey => $rowValue) { - foreach($rowValue as $rowKey => $cellValue) { - return (integer) preg_replace('/[^0-9]/i','',$rowKey); + foreach ($cellAddress as $columnKey => $rowValue) { + foreach ($rowValue as $rowKey => $cellValue) { + return (integer) preg_replace('/[^0-9]/i', '', $rowKey); } } } else { - if (strpos($cellAddress,'!') !== false) { - list($sheet,$cellAddress) = explode('!',$cellAddress); + if (strpos($cellAddress, '!') !== false) { + list($sheet, $cellAddress) = explode('!', $cellAddress); } - if (strpos($cellAddress,':') !== false) { - list($startAddress,$endAddress) = explode(':',$cellAddress); - $startAddress = preg_replace('/[^0-9]/','',$startAddress); - $endAddress = preg_replace('/[^0-9]/','',$endAddress); + if (strpos($cellAddress, ':') !== false) { + list($startAddress, $endAddress) = explode(':', $cellAddress); + $startAddress = preg_replace('/[^0-9]/', '', $startAddress); + $endAddress = preg_replace('/[^0-9]/', '', $endAddress); $returnValue = array(); do { $returnValue[][] = (integer) $startAddress; } while ($startAddress++ != $endAddress); return $returnValue; } else { - list($cellAddress) = explode(':',$cellAddress); - return (integer) preg_replace('/[^0-9]/','',$cellAddress); + list($cellAddress) = explode(':', $cellAddress); + return (integer) preg_replace('/[^0-9]/', '', $cellAddress); } } - } // function ROW() + } /** @@ -223,7 +231,8 @@ class PHPExcel_Calculation_LookupRef { * @param cellAddress An array or array formula, or a reference to a range of cells for which you want the number of rows * @return integer The number of rows in cellAddress */ - public static function ROWS($cellAddress=Null) { + public static function ROWS($cellAddress = null) + { if (is_null($cellAddress) || $cellAddress === '') { return 1; } elseif (!is_array($cellAddress)) { @@ -232,14 +241,14 @@ class PHPExcel_Calculation_LookupRef { reset($cellAddress); $isMatrix = (is_numeric(key($cellAddress))); - list($columns,$rows) = PHPExcel_Calculation::_getMatrixDimensions($cellAddress); + list($columns, $rows) = PHPExcel_Calculation::_getMatrixDimensions($cellAddress); if ($isMatrix) { return $columns; } else { return $rows; } - } // function ROWS() + } /** @@ -255,12 +264,13 @@ class PHPExcel_Calculation_LookupRef { * @param PHPExcel_Cell $pCell The cell to set the hyperlink in * @return mixed The value of $displayName (or $linkURL if $displayName was blank) */ - public static function HYPERLINK($linkURL = '', $displayName = null, PHPExcel_Cell $pCell = null) { + public static function HYPERLINK($linkURL = '', $displayName = null, PHPExcel_Cell $pCell = null) + { $args = func_get_args(); $pCell = array_pop($args); - $linkURL = (is_null($linkURL)) ? '' : PHPExcel_Calculation_Functions::flattenSingleValue($linkURL); - $displayName = (is_null($displayName)) ? '' : PHPExcel_Calculation_Functions::flattenSingleValue($displayName); + $linkURL = (is_null($linkURL)) ? '' : PHPExcel_Calculation_Functions::flattenSingleValue($linkURL); + $displayName = (is_null($displayName)) ? '' : PHPExcel_Calculation_Functions::flattenSingleValue($displayName); if ((!is_object($pCell)) || (trim($linkURL) == '')) { return PHPExcel_Calculation_Functions::REF(); @@ -273,7 +283,7 @@ class PHPExcel_Calculation_LookupRef { $pCell->getHyperlink()->setUrl($linkURL); return $displayName; - } // function HYPERLINK() + } /** @@ -294,16 +304,17 @@ class PHPExcel_Calculation_LookupRef { * @todo Support for the optional a1 parameter introduced in Excel 2010 * */ - public static function INDIRECT($cellAddress = NULL, PHPExcel_Cell $pCell = NULL) { + public static function INDIRECT($cellAddress = null, PHPExcel_Cell $pCell = null) + { $cellAddress = PHPExcel_Calculation_Functions::flattenSingleValue($cellAddress); if (is_null($cellAddress) || $cellAddress === '') { return PHPExcel_Calculation_Functions::REF(); } $cellAddress1 = $cellAddress; - $cellAddress2 = NULL; - if (strpos($cellAddress,':') !== false) { - list($cellAddress1,$cellAddress2) = explode(':',$cellAddress); + $cellAddress2 = null; + if (strpos($cellAddress, ':') !== false) { + list($cellAddress1, $cellAddress2) = explode(':', $cellAddress); } if ((!preg_match('/^'.PHPExcel_Calculation::CALCULATION_REGEXP_CELLREF.'$/i', $cellAddress1, $matches)) || @@ -312,27 +323,27 @@ class PHPExcel_Calculation_LookupRef { return PHPExcel_Calculation_Functions::REF(); } - if (strpos($cellAddress,'!') !== FALSE) { - list($sheetName, $cellAddress) = explode('!',$cellAddress); + if (strpos($cellAddress, '!') !== false) { + list($sheetName, $cellAddress) = explode('!', $cellAddress); $sheetName = trim($sheetName, "'"); $pSheet = $pCell->getWorksheet()->getParent()->getSheetByName($sheetName); } else { $pSheet = $pCell->getWorksheet(); } - return PHPExcel_Calculation::getInstance()->extractNamedRange($cellAddress, $pSheet, FALSE); + return PHPExcel_Calculation::getInstance()->extractNamedRange($cellAddress, $pSheet, false); } - if (strpos($cellAddress,'!') !== FALSE) { - list($sheetName,$cellAddress) = explode('!',$cellAddress); + if (strpos($cellAddress, '!') !== false) { + list($sheetName, $cellAddress) = explode('!', $cellAddress); $sheetName = trim($sheetName, "'"); $pSheet = $pCell->getWorksheet()->getParent()->getSheetByName($sheetName); } else { $pSheet = $pCell->getWorksheet(); } - return PHPExcel_Calculation::getInstance()->extractCellRange($cellAddress, $pSheet, FALSE); - } // function INDIRECT() + return PHPExcel_Calculation::getInstance()->extractCellRange($cellAddress, $pSheet, false); + } /** @@ -360,12 +371,13 @@ class PHPExcel_Calculation_LookupRef { * @param width The width, in number of columns, that you want the returned reference to be. Width must be a positive number. * @return string A reference to a cell or range of cells */ - public static function OFFSET($cellAddress=Null,$rows=0,$columns=0,$height=null,$width=null) { - $rows = PHPExcel_Calculation_Functions::flattenSingleValue($rows); - $columns = PHPExcel_Calculation_Functions::flattenSingleValue($columns); - $height = PHPExcel_Calculation_Functions::flattenSingleValue($height); - $width = PHPExcel_Calculation_Functions::flattenSingleValue($width); - if ($cellAddress == Null) { + public static function OFFSET($cellAddress = null, $rows = 0, $columns = 0, $height = null, $width = null) + { + $rows = PHPExcel_Calculation_Functions::flattenSingleValue($rows); + $columns = PHPExcel_Calculation_Functions::flattenSingleValue($columns); + $height = PHPExcel_Calculation_Functions::flattenSingleValue($height); + $width = PHPExcel_Calculation_Functions::flattenSingleValue($width); + if ($cellAddress == null) { return 0; } @@ -375,18 +387,18 @@ class PHPExcel_Calculation_LookupRef { return PHPExcel_Calculation_Functions::REF(); } - $sheetName = NULL; - if (strpos($cellAddress,"!")) { - list($sheetName,$cellAddress) = explode("!",$cellAddress); + $sheetName = null; + if (strpos($cellAddress, "!")) { + list($sheetName, $cellAddress) = explode("!", $cellAddress); $sheetName = trim($sheetName, "'"); } - if (strpos($cellAddress,":")) { - list($startCell,$endCell) = explode(":",$cellAddress); + if (strpos($cellAddress, ":")) { + list($startCell, $endCell) = explode(":", $cellAddress); } else { $startCell = $endCell = $cellAddress; } - list($startCellColumn,$startCellRow) = PHPExcel_Cell::coordinateFromString($startCell); - list($endCellColumn,$endCellRow) = PHPExcel_Cell::coordinateFromString($endCell); + list($startCellColumn, $startCellRow) = PHPExcel_Cell::coordinateFromString($startCell); + list($endCellColumn, $endCellRow) = PHPExcel_Cell::coordinateFromString($endCell); $startCellRow += $rows; $startCellColumn = PHPExcel_Cell::columnIndexFromString($startCellColumn) - 1; @@ -419,14 +431,14 @@ class PHPExcel_Calculation_LookupRef { $cellAddress .= ':'.$endCellColumn.$endCellRow; } - if ($sheetName !== NULL) { + if ($sheetName !== null) { $pSheet = $pCell->getWorksheet()->getParent()->getSheetByName($sheetName); } else { $pSheet = $pCell->getWorksheet(); } - return PHPExcel_Calculation::getInstance()->extractCellRange($cellAddress, $pSheet, False); - } // function OFFSET() + return PHPExcel_Calculation::getInstance()->extractCellRange($cellAddress, $pSheet, false); + } /** @@ -447,12 +459,13 @@ class PHPExcel_Calculation_LookupRef { * text. * @return mixed The selected value */ - public static function CHOOSE() { + public static function CHOOSE() + { $chooseArgs = func_get_args(); $chosenEntry = PHPExcel_Calculation_Functions::flattenArray(array_shift($chooseArgs)); $entryCount = count($chooseArgs) - 1; - if(is_array($chosenEntry)) { + if (is_array($chosenEntry)) { $chosenEntry = array_shift($chosenEntry); } if ((is_numeric($chosenEntry)) && (!is_bool($chosenEntry))) { @@ -470,7 +483,7 @@ class PHPExcel_Calculation_LookupRef { } else { return $chooseArgs[$chosenEntry]; } - } // function CHOOSE() + } /** @@ -486,7 +499,8 @@ class PHPExcel_Calculation_LookupRef { * @param match_type The number -1, 0, or 1. -1 means above, 0 means exact match, 1 means below. If match_type is 1 or -1, the list has to be ordered. * @return integer The relative position of the found item */ - public static function MATCH($lookup_value, $lookup_array, $match_type=1) { + public static function MATCH($lookup_value, $lookup_array, $match_type = 1) + { $lookup_array = PHPExcel_Calculation_Functions::flattenArray($lookup_array); $lookup_value = PHPExcel_Calculation_Functions::flattenSingleValue($lookup_value); $match_type = (is_null($match_type)) ? 1 : (int) PHPExcel_Calculation_Functions::flattenSingleValue($match_type); @@ -510,7 +524,7 @@ class PHPExcel_Calculation_LookupRef { } // lookup_array should contain only number, text, or logical values, or empty (null) cells - foreach($lookup_array as $i => $lookupArrayValue) { + foreach ($lookup_array as $i => $lookupArrayValue) { // check the type of the value if ((!is_numeric($lookupArrayValue)) && (!is_string($lookupArrayValue)) && (!is_bool($lookupArrayValue)) && (!is_null($lookupArrayValue))) { @@ -521,7 +535,7 @@ class PHPExcel_Calculation_LookupRef { $lookup_array[$i] = strtolower($lookupArrayValue); } if ((is_null($lookupArrayValue)) && (($match_type == 1) || ($match_type == -1))) { - $lookup_array = array_slice($lookup_array,0,$i-1); + $lookup_array = array_slice($lookup_array, 0, $i-1); } } @@ -529,7 +543,7 @@ class PHPExcel_Calculation_LookupRef { if ($match_type == 1) { asort($lookup_array); $keySet = array_keys($lookup_array); - } elseif($match_type == -1) { + } elseif ($match_type == -1) { arsort($lookup_array); $keySet = array_keys($lookup_array); } @@ -537,42 +551,25 @@ class PHPExcel_Calculation_LookupRef { // ** // find the match // ** - // loop on the cells -// var_dump($lookup_array); -// echo '
'; - foreach($lookup_array as $i => $lookupArrayValue) { + foreach ($lookup_array as $i => $lookupArrayValue) { if (($match_type == 0) && ($lookupArrayValue == $lookup_value)) { // exact match return ++$i; } elseif (($match_type == -1) && ($lookupArrayValue <= $lookup_value)) { -// echo '$i = '.$i.' => '; -// var_dump($lookupArrayValue); -// echo '
'; -// echo 'Keyset = '; -// var_dump($keySet); -// echo '
'; - $i = array_search($i,$keySet); -// echo '$i='.$i.'
'; + $i = array_search($i, $keySet); // if match_type is -1 <=> find the smallest value that is greater than or equal to lookup_value - if ($i < 1){ - // 1st cell was allready smaller than the lookup_value + if ($i < 1) { + // 1st cell was already smaller than the lookup_value break; } else { // the previous cell was the match return $keySet[$i-1]+1; } } elseif (($match_type == 1) && ($lookupArrayValue >= $lookup_value)) { -// echo '$i = '.$i.' => '; -// var_dump($lookupArrayValue); -// echo '
'; -// echo 'Keyset = '; -// var_dump($keySet); -// echo '
'; - $i = array_search($i,$keySet); -// echo '$i='.$i.'
'; + $i = array_search($i, $keySet); // if match_type is 1 <=> find the largest value that is less than or equal to lookup_value - if ($i < 1){ - // 1st cell was allready bigger than the lookup_value + if ($i < 1) { + // 1st cell was already bigger than the lookup_value break; } else { // the previous cell was the match @@ -583,7 +580,7 @@ class PHPExcel_Calculation_LookupRef { // unsuccessful in finding a match, return #N/A error value return PHPExcel_Calculation_Functions::NA(); - } // function MATCH() + } /** @@ -599,8 +596,8 @@ class PHPExcel_Calculation_LookupRef { * @param column_num The column in array from which to return a value. If column_num is omitted, row_num is required. * @return mixed the value of a specified cell or array of cells */ - public static function INDEX($arrayValues,$rowNum = 0,$columnNum = 0) { - + public static function INDEX($arrayValues, $rowNum = 0, $columnNum = 0) + { if (($rowNum < 0) || ($columnNum < 0)) { return PHPExcel_Calculation_Functions::VALUE(); } @@ -620,7 +617,7 @@ class PHPExcel_Calculation_LookupRef { } $rowNum = $rowKeys[--$rowNum]; $returnArray = array(); - foreach($arrayValues as $arrayColumn) { + foreach ($arrayValues as $arrayColumn) { if (is_array($arrayColumn)) { if (isset($arrayColumn[$rowNum])) { $returnArray[] = $arrayColumn[$rowNum]; @@ -642,7 +639,7 @@ class PHPExcel_Calculation_LookupRef { $rowNum = $rowKeys[--$rowNum]; return $arrayValues[$rowNum][$columnNum]; - } // function INDEX() + } /** @@ -653,43 +650,48 @@ class PHPExcel_Calculation_LookupRef { * * Unlike the Excel TRANSPOSE function, which will only work on a single row or column, this function will transpose a full matrix. */ - public static function TRANSPOSE($matrixData) { + public static function TRANSPOSE($matrixData) + { $returnMatrix = array(); - if (!is_array($matrixData)) { $matrixData = array(array($matrixData)); } + if (!is_array($matrixData)) { + $matrixData = array(array($matrixData)); + } $column = 0; - foreach($matrixData as $matrixRow) { + foreach ($matrixData as $matrixRow) { $row = 0; - foreach($matrixRow as $matrixCell) { + foreach ($matrixRow as $matrixCell) { $returnMatrix[$row][$column] = $matrixCell; ++$row; } ++$column; } return $returnMatrix; - } // function TRANSPOSE() + } - private static function _vlookupSort($a,$b) { + private static function vlookupSort($a, $b) + { reset($a); $firstColumn = key($a); if (strtolower($a[$firstColumn]) == strtolower($b[$firstColumn])) { return 0; } return (strtolower($a[$firstColumn]) < strtolower($b[$firstColumn])) ? -1 : 1; - } // function _vlookupSort() + } /** - * VLOOKUP - * The VLOOKUP function searches for value in the left-most column of lookup_array and returns the value in the same row based on the index_number. - * @param lookup_value The value that you want to match in lookup_array - * @param lookup_array The range of cells being searched - * @param index_number The column number in table_array from which the matching value must be returned. The first column is 1. - * @param not_exact_match Determines if you are looking for an exact match based on lookup_value. - * @return mixed The value of the found cell - */ - public static function VLOOKUP($lookup_value, $lookup_array, $index_number, $not_exact_match=true) { + * VLOOKUP + * The VLOOKUP function searches for value in the left-most column of lookup_array and returns the value in the same row based on the index_number. + * @param lookup_value The value that you want to match in lookup_array + * @param lookup_array The range of cells being searched + * @param index_number The column number in table_array from which the matching value must be returned. The first column is 1. + * @param not_exact_match Determines if you are looking for an exact match based on lookup_value. + * @return mixed The value of the found cell + */ + public static function VLOOKUP($lookup_value, $lookup_array, $index_number, $not_exact_match = true) + { $lookup_value = PHPExcel_Calculation_Functions::flattenSingleValue($lookup_value); $index_number = PHPExcel_Calculation_Functions::flattenSingleValue($index_number); $not_exact_match = PHPExcel_Calculation_Functions::flattenSingleValue($not_exact_match); @@ -715,11 +717,11 @@ class PHPExcel_Calculation_LookupRef { } if (!$not_exact_match) { - uasort($lookup_array,array('self','_vlookupSort')); + uasort($lookup_array, array('self', 'vlookupSort')); } - $rowNumber = $rowValue = False; - foreach($lookup_array as $rowKey => $rowData) { + $rowNumber = $rowValue = false; + foreach ($lookup_array as $rowKey => $rowData) { if ((is_numeric($lookup_value) && is_numeric($rowData[$firstColumn]) && ($rowData[$firstColumn] > $lookup_value)) || (!is_numeric($lookup_value) && !is_numeric($rowData[$firstColumn]) && (strtolower($rowData[$firstColumn]) > strtolower($lookup_value)))) { break; @@ -739,19 +741,20 @@ class PHPExcel_Calculation_LookupRef { } return PHPExcel_Calculation_Functions::NA(); - } // function VLOOKUP() + } -/** - * HLOOKUP - * The HLOOKUP function searches for value in the top-most row of lookup_array and returns the value in the same column based on the index_number. - * @param lookup_value The value that you want to match in lookup_array - * @param lookup_array The range of cells being searched - * @param index_number The row number in table_array from which the matching value must be returned. The first row is 1. - * @param not_exact_match Determines if you are looking for an exact match based on lookup_value. - * @return mixed The value of the found cell - */ - public static function HLOOKUP($lookup_value, $lookup_array, $index_number, $not_exact_match=true) { + /** + * HLOOKUP + * The HLOOKUP function searches for value in the top-most row of lookup_array and returns the value in the same column based on the index_number. + * @param lookup_value The value that you want to match in lookup_array + * @param lookup_array The range of cells being searched + * @param index_number The row number in table_array from which the matching value must be returned. The first row is 1. + * @param not_exact_match Determines if you are looking for an exact match based on lookup_value. + * @return mixed The value of the found cell + */ + public static function HLOOKUP($lookup_value, $lookup_array, $index_number, $not_exact_match = true) + { $lookup_value = PHPExcel_Calculation_Functions::flattenSingleValue($lookup_value); $index_number = PHPExcel_Calculation_Functions::flattenSingleValue($index_number); $not_exact_match = PHPExcel_Calculation_Functions::flattenSingleValue($not_exact_match); @@ -781,8 +784,8 @@ class PHPExcel_Calculation_LookupRef { $firstRowH = asort($lookup_array[$firstColumn]); } - $rowNumber = $rowValue = False; - foreach($lookup_array[$firstColumn] as $rowKey => $rowData) { + $rowNumber = $rowValue = false; + foreach ($lookup_array[$firstColumn] as $rowKey => $rowData) { if ((is_numeric($lookup_value) && is_numeric($rowData) && ($rowData > $lookup_value)) || (!is_numeric($lookup_value) && !is_numeric($rowData) && (strtolower($rowData) > strtolower($lookup_value)))) { break; @@ -802,7 +805,7 @@ class PHPExcel_Calculation_LookupRef { } return PHPExcel_Calculation_Functions::NA(); - } // function HLOOKUP() + } /** @@ -813,8 +816,9 @@ class PHPExcel_Calculation_LookupRef { * @param result_vector The column from which the matching value must be returned * @return mixed The value of the found cell */ - public static function LOOKUP($lookup_value, $lookup_vector, $result_vector=null) { - $lookup_value = PHPExcel_Calculation_Functions::flattenSingleValue($lookup_value); + public static function LOOKUP($lookup_value, $lookup_vector, $result_vector = null) + { + $lookup_value = PHPExcel_Calculation_Functions::flattenSingleValue($lookup_value); if (!is_array($lookup_vector)) { return PHPExcel_Calculation_Functions::NA(); @@ -849,7 +853,7 @@ class PHPExcel_Calculation_LookupRef { $lookup_vector = array_shift($lookup_vector); } if ($lookupColumns != 2) { - foreach($lookup_vector as &$value) { + foreach ($lookup_vector as &$value) { if (is_array($value)) { $k = array_keys($value); $key1 = $key2 = array_shift($k); @@ -869,7 +873,6 @@ class PHPExcel_Calculation_LookupRef { unset($value); } - return self::VLOOKUP($lookup_value,$lookup_vector,2); - } // function LOOKUP() - -} // class PHPExcel_Calculation_LookupRef + return self::VLOOKUP($lookup_value, $lookup_vector, 2); + } +} diff --git a/Classes/PHPExcel/Calculation/MathTrig.php b/Classes/PHPExcel/Calculation/MathTrig.php index 3b3d64fe..df0c88b8 100644 --- a/Classes/PHPExcel/Calculation/MathTrig.php +++ b/Classes/PHPExcel/Calculation/MathTrig.php @@ -1,6 +1,16 @@ 1; --$i) { if (($value % $i) == 0) { - $factorArray = array_merge($factorArray,self::_factors($value / $i)); - $factorArray = array_merge($factorArray,self::_factors($i)); + $factorArray = array_merge($factorArray, self::factors($value / $i)); + $factorArray = array_merge($factorArray, self::factors($i)); if ($i <= sqrt($value)) { break; } @@ -67,12 +59,13 @@ class PHPExcel_Calculation_MathTrig { } else { return array((integer) $value); } - } // function _factors() + } - private static function _romanCut($num, $n) { + private static function romanCut($num, $n) + { return ($num - ($num % $n ) ) / $n; - } // function _romanCut() + } /** @@ -97,12 +90,13 @@ class PHPExcel_Calculation_MathTrig { * @param float $yCoordinate The y-coordinate of the point. * @return float The inverse tangent of the specified x- and y-coordinates. */ - public static function ATAN2($xCoordinate = NULL, $yCoordinate = NULL) { - $xCoordinate = PHPExcel_Calculation_Functions::flattenSingleValue($xCoordinate); - $yCoordinate = PHPExcel_Calculation_Functions::flattenSingleValue($yCoordinate); + public static function ATAN2($xCoordinate = null, $yCoordinate = null) + { + $xCoordinate = PHPExcel_Calculation_Functions::flattenSingleValue($xCoordinate); + $yCoordinate = PHPExcel_Calculation_Functions::flattenSingleValue($yCoordinate); - $xCoordinate = ($xCoordinate !== NULL) ? $xCoordinate : 0.0; - $yCoordinate = ($yCoordinate !== NULL) ? $yCoordinate : 0.0; + $xCoordinate = ($xCoordinate !== null) ? $xCoordinate : 0.0; + $yCoordinate = ($yCoordinate !== null) ? $yCoordinate : 0.0; if (((is_numeric($xCoordinate)) || (is_bool($xCoordinate))) && ((is_numeric($yCoordinate))) || (is_bool($yCoordinate))) { @@ -116,7 +110,7 @@ class PHPExcel_Calculation_MathTrig { return atan2($yCoordinate, $xCoordinate); } return PHPExcel_Calculation_Functions::VALUE(); - } // function ATAN2() + } /** @@ -136,13 +130,14 @@ class PHPExcel_Calculation_MathTrig { * @param float $significance The multiple to which you want to round. * @return float Rounded Number */ - public static function CEILING($number, $significance = NULL) { - $number = PHPExcel_Calculation_Functions::flattenSingleValue($number); - $significance = PHPExcel_Calculation_Functions::flattenSingleValue($significance); + public static function CEILING($number, $significance = null) + { + $number = PHPExcel_Calculation_Functions::flattenSingleValue($number); + $significance = PHPExcel_Calculation_Functions::flattenSingleValue($significance); if ((is_null($significance)) && (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC)) { - $significance = $number/abs($number); + $significance = $number / abs($number); } if ((is_numeric($number)) && (is_numeric($significance))) { @@ -155,7 +150,7 @@ class PHPExcel_Calculation_MathTrig { } } return PHPExcel_Calculation_Functions::VALUE(); - } // function CEILING() + } /** @@ -173,7 +168,8 @@ class PHPExcel_Calculation_MathTrig { * @param int $numInSet Number of objects in each combination * @return int Number of combinations */ - public static function COMBIN($numObjs, $numInSet) { + public static function COMBIN($numObjs, $numInSet) + { $numObjs = PHPExcel_Calculation_Functions::flattenSingleValue($numObjs); $numInSet = PHPExcel_Calculation_Functions::flattenSingleValue($numInSet); @@ -186,7 +182,7 @@ class PHPExcel_Calculation_MathTrig { return round(self::FACT($numObjs) / self::FACT($numObjs - $numInSet)) / self::FACT($numInSet); } return PHPExcel_Calculation_Functions::VALUE(); - } // function COMBIN() + } /** @@ -206,8 +202,9 @@ class PHPExcel_Calculation_MathTrig { * @param float $number Number to round * @return int Rounded Number */ - public static function EVEN($number) { - $number = PHPExcel_Calculation_Functions::flattenSingleValue($number); + public static function EVEN($number) + { + $number = PHPExcel_Calculation_Functions::flattenSingleValue($number); if (is_null($number)) { return 0; @@ -217,10 +214,10 @@ class PHPExcel_Calculation_MathTrig { if (is_numeric($number)) { $significance = 2 * self::SIGN($number); - return (int) self::CEILING($number,$significance); + return (int) self::CEILING($number, $significance); } return PHPExcel_Calculation_Functions::VALUE(); - } // function EVEN() + } /** @@ -237,7 +234,8 @@ class PHPExcel_Calculation_MathTrig { * @param float $factVal Factorial Value * @return int Factorial */ - public static function FACT($factVal) { + public static function FACT($factVal) + { $factVal = PHPExcel_Calculation_Functions::flattenSingleValue($factVal); if (is_numeric($factVal)) { @@ -258,7 +256,7 @@ class PHPExcel_Calculation_MathTrig { return $factorial ; } return PHPExcel_Calculation_Functions::VALUE(); - } // function FACT() + } /** @@ -274,7 +272,8 @@ class PHPExcel_Calculation_MathTrig { * @param float $factVal Factorial Value * @return int Double Factorial */ - public static function FACTDOUBLE($factVal) { + public static function FACTDOUBLE($factVal) + { $factLoop = PHPExcel_Calculation_Functions::flattenSingleValue($factVal); if (is_numeric($factLoop)) { @@ -290,7 +289,7 @@ class PHPExcel_Calculation_MathTrig { return $factorial ; } return PHPExcel_Calculation_Functions::VALUE(); - } // function FACTDOUBLE() + } /** @@ -307,11 +306,13 @@ class PHPExcel_Calculation_MathTrig { * @param float $significance Significance * @return float Rounded Number */ - public static function FLOOR($number, $significance = NULL) { + public static function FLOOR($number, $significance = null) + { $number = PHPExcel_Calculation_Functions::flattenSingleValue($number); $significance = PHPExcel_Calculation_Functions::flattenSingleValue($significance); - if ((is_null($significance)) && (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC)) { + if ((is_null($significance)) && + (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC)) { $significance = $number/abs($number); } @@ -325,10 +326,10 @@ class PHPExcel_Calculation_MathTrig { } else { return PHPExcel_Calculation_Functions::NaN(); } - } else + } return PHPExcel_Calculation_Functions::VALUE(); - } // function FLOOR() + } /** @@ -346,19 +347,20 @@ class PHPExcel_Calculation_MathTrig { * @param mixed $arg,... Data values * @return integer Greatest Common Divisor */ - public static function GCD() { + public static function GCD() + { $returnValue = 1; $allValuesFactors = array(); // Loop through arguments - foreach(PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $value) { + foreach (PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $value) { if (!is_numeric($value)) { return PHPExcel_Calculation_Functions::VALUE(); } elseif ($value == 0) { continue; - } elseif($value < 0) { + } elseif ($value < 0) { return PHPExcel_Calculation_Functions::NaN(); } - $myFactors = self::_factors($value); + $myFactors = self::factors($value); $myCountedFactors = array_count_values($myFactors); $allValuesFactors[] = $myCountedFactors; } @@ -368,16 +370,16 @@ class PHPExcel_Calculation_MathTrig { } $mergedArray = $allValuesFactors[0]; - for ($i=1;$i < $allValuesCount; ++$i) { - $mergedArray = array_intersect_key($mergedArray,$allValuesFactors[$i]); + for ($i=1; $i < $allValuesCount; ++$i) { + $mergedArray = array_intersect_key($mergedArray, $allValuesFactors[$i]); } $mergedArrayValues = count($mergedArray); if ($mergedArrayValues == 0) { return $returnValue; } elseif ($mergedArrayValues > 1) { - foreach($mergedArray as $mergedKey => $mergedValue) { - foreach($allValuesFactors as $highestPowerTest) { - foreach($highestPowerTest as $testKey => $testValue) { + foreach ($mergedArray as $mergedKey => $mergedValue) { + foreach ($allValuesFactors as $highestPowerTest) { + foreach ($highestPowerTest as $testKey => $testValue) { if (($testKey == $mergedKey) && ($testValue < $mergedValue)) { $mergedArray[$mergedKey] = $testValue; $mergedValue = $testValue; @@ -387,24 +389,24 @@ class PHPExcel_Calculation_MathTrig { } $returnValue = 1; - foreach($mergedArray as $key => $value) { - $returnValue *= pow($key,$value); + foreach ($mergedArray as $key => $value) { + $returnValue *= pow($key, $value); } return $returnValue; } else { $keys = array_keys($mergedArray); $key = $keys[0]; $value = $mergedArray[$key]; - foreach($allValuesFactors as $testValue) { - foreach($testValue as $mergedKey => $mergedValue) { + foreach ($allValuesFactors as $testValue) { + foreach ($testValue as $mergedKey => $mergedValue) { if (($mergedKey == $key) && ($mergedValue < $value)) { $value = $mergedValue; } } } - return pow($key,$value); + return pow($key, $value); } - } // function GCD() + } /** @@ -420,7 +422,8 @@ class PHPExcel_Calculation_MathTrig { * @param float $number Number to cast to an integer * @return integer Integer value */ - public static function INT($number) { + public static function INT($number) + { $number = PHPExcel_Calculation_Functions::flattenSingleValue($number); if (is_null($number)) { @@ -432,7 +435,7 @@ class PHPExcel_Calculation_MathTrig { return (int) floor($number); } return PHPExcel_Calculation_Functions::VALUE(); - } // function INT() + } /** @@ -451,11 +454,12 @@ class PHPExcel_Calculation_MathTrig { * @param mixed $arg,... Data values * @return int Lowest Common Multiplier */ - public static function LCM() { + public static function LCM() + { $returnValue = 1; $allPoweredFactors = array(); // Loop through arguments - foreach(PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $value) { + foreach (PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $value) { if (!is_numeric($value)) { return PHPExcel_Calculation_Functions::VALUE(); } @@ -464,14 +468,14 @@ class PHPExcel_Calculation_MathTrig { } elseif ($value < 0) { return PHPExcel_Calculation_Functions::NaN(); } - $myFactors = self::_factors(floor($value)); + $myFactors = self::factors(floor($value)); $myCountedFactors = array_count_values($myFactors); $myPoweredFactors = array(); - foreach($myCountedFactors as $myCountedFactor => $myCountedPower) { - $myPoweredFactors[$myCountedFactor] = pow($myCountedFactor,$myCountedPower); + foreach ($myCountedFactors as $myCountedFactor => $myCountedPower) { + $myPoweredFactors[$myCountedFactor] = pow($myCountedFactor, $myCountedPower); } - foreach($myPoweredFactors as $myPoweredValue => $myPoweredFactor) { - if (array_key_exists($myPoweredValue,$allPoweredFactors)) { + foreach ($myPoweredFactors as $myPoweredValue => $myPoweredFactor) { + if (array_key_exists($myPoweredValue, $allPoweredFactors)) { if ($allPoweredFactors[$myPoweredValue] < $myPoweredFactor) { $allPoweredFactors[$myPoweredValue] = $myPoweredFactor; } @@ -480,11 +484,11 @@ class PHPExcel_Calculation_MathTrig { } } } - foreach($allPoweredFactors as $allPoweredFactor) { + foreach ($allPoweredFactors as $allPoweredFactor) { $returnValue *= (integer) $allPoweredFactor; } return $returnValue; - } // function LCM() + } /** @@ -501,16 +505,19 @@ class PHPExcel_Calculation_MathTrig { * @param float $base The base of the logarithm. If base is omitted, it is assumed to be 10. * @return float */ - public static function LOG_BASE($number = NULL, $base = 10) { + public static function LOG_BASE($number = null, $base = 10) + { $number = PHPExcel_Calculation_Functions::flattenSingleValue($number); $base = (is_null($base)) ? 10 : (float) PHPExcel_Calculation_Functions::flattenSingleValue($base); - if ((!is_numeric($base)) || (!is_numeric($number))) + if ((!is_numeric($base)) || (!is_numeric($number))) { return PHPExcel_Calculation_Functions::VALUE(); - if (($base <= 0) || ($number <= 0)) + } + if (($base <= 0) || ($number <= 0)) { return PHPExcel_Calculation_Functions::NaN(); + } return log($number, $base); - } // function LOG_BASE() + } /** @@ -526,25 +533,34 @@ class PHPExcel_Calculation_MathTrig { * @param array $matrixValues A matrix of values * @return float */ - public static function MDETERM($matrixValues) { + public static function MDETERM($matrixValues) + { $matrixData = array(); - if (!is_array($matrixValues)) { $matrixValues = array(array($matrixValues)); } + if (!is_array($matrixValues)) { + $matrixValues = array(array($matrixValues)); + } $row = $maxColumn = 0; - foreach($matrixValues as $matrixRow) { - if (!is_array($matrixRow)) { $matrixRow = array($matrixRow); } + foreach ($matrixValues as $matrixRow) { + if (!is_array($matrixRow)) { + $matrixRow = array($matrixRow); + } $column = 0; - foreach($matrixRow as $matrixCell) { + foreach ($matrixRow as $matrixCell) { if ((is_string($matrixCell)) || ($matrixCell === null)) { return PHPExcel_Calculation_Functions::VALUE(); } $matrixData[$column][$row] = $matrixCell; ++$column; } - if ($column > $maxColumn) { $maxColumn = $column; } + if ($column > $maxColumn) { + $maxColumn = $column; + } ++$row; } - if ($row != $maxColumn) { return PHPExcel_Calculation_Functions::VALUE(); } + if ($row != $maxColumn) { + return PHPExcel_Calculation_Functions::VALUE(); + } try { $matrix = new PHPExcel_Shared_JAMA_Matrix($matrixData); @@ -552,7 +568,7 @@ class PHPExcel_Calculation_MathTrig { } catch (PHPExcel_Exception $ex) { return PHPExcel_Calculation_Functions::VALUE(); } - } // function MDETERM() + } /** @@ -568,25 +584,34 @@ class PHPExcel_Calculation_MathTrig { * @param array $matrixValues A matrix of values * @return array */ - public static function MINVERSE($matrixValues) { + public static function MINVERSE($matrixValues) + { $matrixData = array(); - if (!is_array($matrixValues)) { $matrixValues = array(array($matrixValues)); } + if (!is_array($matrixValues)) { + $matrixValues = array(array($matrixValues)); + } $row = $maxColumn = 0; - foreach($matrixValues as $matrixRow) { - if (!is_array($matrixRow)) { $matrixRow = array($matrixRow); } + foreach ($matrixValues as $matrixRow) { + if (!is_array($matrixRow)) { + $matrixRow = array($matrixRow); + } $column = 0; - foreach($matrixRow as $matrixCell) { + foreach ($matrixRow as $matrixCell) { if ((is_string($matrixCell)) || ($matrixCell === null)) { return PHPExcel_Calculation_Functions::VALUE(); } $matrixData[$column][$row] = $matrixCell; ++$column; } - if ($column > $maxColumn) { $maxColumn = $column; } + if ($column > $maxColumn) { + $maxColumn = $column; + } ++$row; } - if ($row != $maxColumn) { return PHPExcel_Calculation_Functions::VALUE(); } + if ($row != $maxColumn) { + return PHPExcel_Calculation_Functions::VALUE(); + } try { $matrix = new PHPExcel_Shared_JAMA_Matrix($matrixData); @@ -594,7 +619,7 @@ class PHPExcel_Calculation_MathTrig { } catch (PHPExcel_Exception $ex) { return PHPExcel_Calculation_Functions::VALUE(); } - } // function MINVERSE() + } /** @@ -604,17 +629,24 @@ class PHPExcel_Calculation_MathTrig { * @param array $matrixData2 A matrix of values * @return array */ - public static function MMULT($matrixData1,$matrixData2) { + public static function MMULT($matrixData1, $matrixData2) + { $matrixAData = $matrixBData = array(); - if (!is_array($matrixData1)) { $matrixData1 = array(array($matrixData1)); } - if (!is_array($matrixData2)) { $matrixData2 = array(array($matrixData2)); } + if (!is_array($matrixData1)) { + $matrixData1 = array(array($matrixData1)); + } + if (!is_array($matrixData2)) { + $matrixData2 = array(array($matrixData2)); + } try { $rowA = 0; - foreach($matrixData1 as $matrixRow) { - if (!is_array($matrixRow)) { $matrixRow = array($matrixRow); } + foreach ($matrixData1 as $matrixRow) { + if (!is_array($matrixRow)) { + $matrixRow = array($matrixRow); + } $columnA = 0; - foreach($matrixRow as $matrixCell) { + foreach ($matrixRow as $matrixCell) { if ((!is_numeric($matrixCell)) || ($matrixCell === null)) { return PHPExcel_Calculation_Functions::VALUE(); } @@ -625,10 +657,12 @@ class PHPExcel_Calculation_MathTrig { } $matrixA = new PHPExcel_Shared_JAMA_Matrix($matrixAData); $rowB = 0; - foreach($matrixData2 as $matrixRow) { - if (!is_array($matrixRow)) { $matrixRow = array($matrixRow); } + foreach ($matrixData2 as $matrixRow) { + if (!is_array($matrixRow)) { + $matrixRow = array($matrixRow); + } $columnB = 0; - foreach($matrixRow as $matrixCell) { + foreach ($matrixRow as $matrixCell) { if ((!is_numeric($matrixCell)) || ($matrixCell === null)) { return PHPExcel_Calculation_Functions::VALUE(); } @@ -648,7 +682,7 @@ class PHPExcel_Calculation_MathTrig { var_dump($ex->getMessage()); return PHPExcel_Calculation_Functions::VALUE(); } - } // function MMULT() + } /** @@ -658,20 +692,21 @@ class PHPExcel_Calculation_MathTrig { * @param int $b Divisor * @return int Remainder */ - public static function MOD($a = 1, $b = 1) { - $a = PHPExcel_Calculation_Functions::flattenSingleValue($a); - $b = PHPExcel_Calculation_Functions::flattenSingleValue($b); + public static function MOD($a = 1, $b = 1) + { + $a = PHPExcel_Calculation_Functions::flattenSingleValue($a); + $b = PHPExcel_Calculation_Functions::flattenSingleValue($b); if ($b == 0.0) { return PHPExcel_Calculation_Functions::DIV0(); } elseif (($a < 0.0) && ($b > 0.0)) { - return $b - fmod(abs($a),$b); + return $b - fmod(abs($a), $b); } elseif (($a > 0.0) && ($b < 0.0)) { - return $b + fmod($a,abs($b)); + return $b + fmod($a, abs($b)); } - return fmod($a,$b); - } // function MOD() + return fmod($a, $b); + } /** @@ -683,9 +718,10 @@ class PHPExcel_Calculation_MathTrig { * @param int $multiple Multiple to which you want to round $number * @return float Rounded Number */ - public static function MROUND($number,$multiple) { - $number = PHPExcel_Calculation_Functions::flattenSingleValue($number); - $multiple = PHPExcel_Calculation_Functions::flattenSingleValue($multiple); + public static function MROUND($number, $multiple) + { + $number = PHPExcel_Calculation_Functions::flattenSingleValue($number); + $multiple = PHPExcel_Calculation_Functions::flattenSingleValue($multiple); if ((is_numeric($number)) && (is_numeric($multiple))) { if ($multiple == 0) { @@ -698,7 +734,7 @@ class PHPExcel_Calculation_MathTrig { return PHPExcel_Calculation_Functions::NaN(); } return PHPExcel_Calculation_Functions::VALUE(); - } // function MROUND() + } /** @@ -709,7 +745,8 @@ class PHPExcel_Calculation_MathTrig { * @param array of mixed Data Series * @return float */ - public static function MULTINOMIAL() { + public static function MULTINOMIAL() + { $summer = 0; $divisor = 1; // Loop through arguments @@ -732,7 +769,7 @@ class PHPExcel_Calculation_MathTrig { return $summer / $divisor; } return 0; - } // function MULTINOMIAL() + } /** @@ -743,22 +780,21 @@ class PHPExcel_Calculation_MathTrig { * @param float $number Number to round * @return int Rounded Number */ - public static function ODD($number) { - $number = PHPExcel_Calculation_Functions::flattenSingleValue($number); + public static function ODD($number) + { + $number = PHPExcel_Calculation_Functions::flattenSingleValue($number); if (is_null($number)) { return 1; } elseif (is_bool($number)) { - $number = (int) $number; - } - - if (is_numeric($number)) { + return 1; + } elseif (is_numeric($number)) { $significance = self::SIGN($number); if ($significance == 0) { return 1; } - $result = self::CEILING($number,$significance); + $result = self::CEILING($number, $significance); if ($result == self::EVEN($result)) { $result += $significance; } @@ -766,7 +802,7 @@ class PHPExcel_Calculation_MathTrig { return (int) $result; } return PHPExcel_Calculation_Functions::VALUE(); - } // function ODD() + } /** @@ -778,7 +814,8 @@ class PHPExcel_Calculation_MathTrig { * @param float $y * @return float */ - public static function POWER($x = 0, $y = 2) { + public static function POWER($x = 0, $y = 2) + { $x = PHPExcel_Calculation_Functions::flattenSingleValue($x); $y = PHPExcel_Calculation_Functions::flattenSingleValue($y); @@ -792,7 +829,7 @@ class PHPExcel_Calculation_MathTrig { // Return $result = pow($x, $y); return (!is_nan($result) && !is_infinite($result)) ? $result : PHPExcel_Calculation_Functions::NaN(); - } // function POWER() + } /** @@ -808,7 +845,8 @@ class PHPExcel_Calculation_MathTrig { * @param mixed $arg,... Data values * @return float */ - public static function PRODUCT() { + public static function PRODUCT() + { // Return value $returnValue = null; @@ -829,7 +867,7 @@ class PHPExcel_Calculation_MathTrig { return 0; } return $returnValue; - } // function PRODUCT() + } /** @@ -846,7 +884,8 @@ class PHPExcel_Calculation_MathTrig { * @param mixed $arg,... Data values * @return float */ - public static function QUOTIENT() { + public static function QUOTIENT() + { // Return value $returnValue = null; @@ -868,7 +907,7 @@ class PHPExcel_Calculation_MathTrig { // Return return intval($returnValue); - } // function QUOTIENT() + } /** @@ -878,19 +917,21 @@ class PHPExcel_Calculation_MathTrig { * @param int $max Maximal value * @return int Random number */ - public static function RAND($min = 0, $max = 0) { - $min = PHPExcel_Calculation_Functions::flattenSingleValue($min); - $max = PHPExcel_Calculation_Functions::flattenSingleValue($max); + public static function RAND($min = 0, $max = 0) + { + $min = PHPExcel_Calculation_Functions::flattenSingleValue($min); + $max = PHPExcel_Calculation_Functions::flattenSingleValue($max); if ($min == 0 && $max == 0) { - return (mt_rand(0,10000000)) / 10000000; + return (mt_rand(0, 10000000)) / 10000000; } else { return mt_rand($min, $max); } - } // function RAND() + } - public static function ROMAN($aValue, $style=0) { + public static function ROMAN($aValue, $style = 0) + { $aValue = PHPExcel_Calculation_Functions::flattenSingleValue($aValue); $style = (is_null($style)) ? 0 : (integer) PHPExcel_Calculation_Functions::flattenSingleValue($style); if ((!is_numeric($aValue)) || ($aValue < 0) || ($aValue >= 4000)) { @@ -901,22 +942,25 @@ class PHPExcel_Calculation_MathTrig { return ''; } - $mill = Array('', 'M', 'MM', 'MMM', 'MMMM', 'MMMMM'); - $cent = Array('', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM'); - $tens = Array('', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC'); - $ones = Array('', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'); + $mill = array('', 'M', 'MM', 'MMM', 'MMMM', 'MMMMM'); + $cent = array('', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM'); + $tens = array('', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC'); + $ones = array('', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'); $roman = ''; while ($aValue > 5999) { $roman .= 'M'; $aValue -= 1000; } - $m = self::_romanCut($aValue, 1000); $aValue %= 1000; - $c = self::_romanCut($aValue, 100); $aValue %= 100; - $t = self::_romanCut($aValue, 10); $aValue %= 10; + $m = self::romanCut($aValue, 1000); + $aValue %= 1000; + $c = self::romanCut($aValue, 100); + $aValue %= 100; + $t = self::romanCut($aValue, 10); + $aValue %= 10; return $roman.$mill[$m].$cent[$c].$tens[$t].$ones[$aValue]; - } // function ROMAN() + } /** @@ -928,12 +972,13 @@ class PHPExcel_Calculation_MathTrig { * @param int $digits Number of digits to which you want to round $number * @return float Rounded Number */ - public static function ROUNDUP($number,$digits) { + public static function ROUNDUP($number, $digits) + { $number = PHPExcel_Calculation_Functions::flattenSingleValue($number); $digits = PHPExcel_Calculation_Functions::flattenSingleValue($digits); if ((is_numeric($number)) && (is_numeric($digits))) { - $significance = pow(10,(int) $digits); + $significance = pow(10, (int) $digits); if ($number < 0.0) { return floor($number * $significance) / $significance; } else { @@ -941,7 +986,7 @@ class PHPExcel_Calculation_MathTrig { } } return PHPExcel_Calculation_Functions::VALUE(); - } // function ROUNDUP() + } /** @@ -953,12 +998,13 @@ class PHPExcel_Calculation_MathTrig { * @param int $digits Number of digits to which you want to round $number * @return float Rounded Number */ - public static function ROUNDDOWN($number,$digits) { + public static function ROUNDDOWN($number, $digits) + { $number = PHPExcel_Calculation_Functions::flattenSingleValue($number); $digits = PHPExcel_Calculation_Functions::flattenSingleValue($digits); if ((is_numeric($number)) && (is_numeric($digits))) { - $significance = pow(10,(int) $digits); + $significance = pow(10, (int) $digits); if ($number < 0.0) { return ceil($number * $significance) / $significance; } else { @@ -966,7 +1012,7 @@ class PHPExcel_Calculation_MathTrig { } } return PHPExcel_Calculation_Functions::VALUE(); - } // function ROUNDDOWN() + } /** @@ -980,8 +1026,8 @@ class PHPExcel_Calculation_MathTrig { * @param array of mixed Data Series * @return float */ - public static function SERIESSUM() { - // Return value + public static function SERIESSUM() + { $returnValue = 0; // Loop through arguments @@ -994,19 +1040,18 @@ class PHPExcel_Calculation_MathTrig { if ((is_numeric($x)) && (is_numeric($n)) && (is_numeric($m))) { // Calculate $i = 0; - foreach($aArgs as $arg) { + foreach ($aArgs as $arg) { // Is it a numeric value? if ((is_numeric($arg)) && (!is_string($arg))) { - $returnValue += $arg * pow($x,$n + ($m * $i++)); + $returnValue += $arg * pow($x, $n + ($m * $i++)); } else { return PHPExcel_Calculation_Functions::VALUE(); } } - // Return return $returnValue; } return PHPExcel_Calculation_Functions::VALUE(); - } // function SERIESSUM() + } /** @@ -1018,11 +1063,13 @@ class PHPExcel_Calculation_MathTrig { * @param float $number Number to round * @return int sign value */ - public static function SIGN($number) { + public static function SIGN($number) + { $number = PHPExcel_Calculation_Functions::flattenSingleValue($number); - if (is_bool($number)) + if (is_bool($number)) { return (int) $number; + } if (is_numeric($number)) { if ($number == 0.0) { return 0; @@ -1030,7 +1077,7 @@ class PHPExcel_Calculation_MathTrig { return $number / abs($number); } return PHPExcel_Calculation_Functions::VALUE(); - } // function SIGN() + } /** @@ -1041,7 +1088,8 @@ class PHPExcel_Calculation_MathTrig { * @param float $number Number * @return float Square Root of Number * Pi */ - public static function SQRTPI($number) { + public static function SQRTPI($number) + { $number = PHPExcel_Calculation_Functions::flattenSingleValue($number); if (is_numeric($number)) { @@ -1051,7 +1099,7 @@ class PHPExcel_Calculation_MathTrig { return sqrt($number * M_PI) ; } return PHPExcel_Calculation_Functions::VALUE(); - } // function SQRTPI() + } /** @@ -1064,7 +1112,8 @@ class PHPExcel_Calculation_MathTrig { * @param array of mixed Data Series * @return float */ - public static function SUBTOTAL() { + public static function SUBTOTAL() + { $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args()); // Calculate @@ -1072,43 +1121,32 @@ class PHPExcel_Calculation_MathTrig { if ((is_numeric($subtotal)) && (!is_string($subtotal))) { switch($subtotal) { - case 1 : + case 1: return PHPExcel_Calculation_Statistical::AVERAGE($aArgs); - break; - case 2 : + case 2: return PHPExcel_Calculation_Statistical::COUNT($aArgs); - break; - case 3 : + case 3: return PHPExcel_Calculation_Statistical::COUNTA($aArgs); - break; - case 4 : + case 4: return PHPExcel_Calculation_Statistical::MAX($aArgs); - break; - case 5 : + case 5: return PHPExcel_Calculation_Statistical::MIN($aArgs); - break; - case 6 : + case 6: return self::PRODUCT($aArgs); - break; - case 7 : + case 7: return PHPExcel_Calculation_Statistical::STDEV($aArgs); - break; - case 8 : + case 8: return PHPExcel_Calculation_Statistical::STDEVP($aArgs); - break; - case 9 : + case 9: return self::SUM($aArgs); - break; - case 10 : + case 10: return PHPExcel_Calculation_Statistical::VARFunc($aArgs); - break; - case 11 : + case 11: return PHPExcel_Calculation_Statistical::VARP($aArgs); - break; } } return PHPExcel_Calculation_Functions::VALUE(); - } // function SUBTOTAL() + } /** @@ -1124,8 +1162,8 @@ class PHPExcel_Calculation_MathTrig { * @param mixed $arg,... Data values * @return float */ - public static function SUM() { - // Return value + public static function SUM() + { $returnValue = 0; // Loop through the arguments @@ -1136,9 +1174,8 @@ class PHPExcel_Calculation_MathTrig { } } - // Return return $returnValue; - } // function SUM() + } /** @@ -1155,8 +1192,8 @@ class PHPExcel_Calculation_MathTrig { * @param string $condition The criteria that defines which cells will be summed. * @return float */ - public static function SUMIF($aArgs,$condition,$sumArgs = array()) { - // Return value + public static function SUMIF($aArgs, $condition, $sumArgs = array()) + { $returnValue = 0; $aArgs = PHPExcel_Calculation_Functions::flattenArray($aArgs); @@ -1179,9 +1216,8 @@ class PHPExcel_Calculation_MathTrig { } } - // Return return $returnValue; - } // function SUMIF() + } /** @@ -1195,7 +1231,8 @@ class PHPExcel_Calculation_MathTrig { * @param mixed $arg,... Data values * @return float */ - public static function SUMPRODUCT() { + public static function SUMPRODUCT() + { $arrayList = func_get_args(); $wrkArray = PHPExcel_Calculation_Functions::flattenArray(array_shift($arrayList)); @@ -1207,7 +1244,7 @@ class PHPExcel_Calculation_MathTrig { } } - foreach($arrayList as $matrixData) { + foreach ($arrayList as $matrixData) { $array2 = PHPExcel_Calculation_Functions::flattenArray($matrixData); $count = count($array2); if ($wrkCellCount != $count) { @@ -1223,7 +1260,7 @@ class PHPExcel_Calculation_MathTrig { } return array_sum($wrkArray); - } // function SUMPRODUCT() + } /** @@ -1239,8 +1276,8 @@ class PHPExcel_Calculation_MathTrig { * @param mixed $arg,... Data values * @return float */ - public static function SUMSQ() { - // Return value + public static function SUMSQ() + { $returnValue = 0; // Loop through arguments @@ -1251,9 +1288,8 @@ class PHPExcel_Calculation_MathTrig { } } - // Return return $returnValue; - } // function SUMSQ() + } /** @@ -1263,16 +1299,11 @@ class PHPExcel_Calculation_MathTrig { * @param mixed[] $matrixData2 Matrix #2 * @return float */ - public static function SUMX2MY2($matrixData1,$matrixData2) { + public static function SUMX2MY2($matrixData1, $matrixData2) + { $array1 = PHPExcel_Calculation_Functions::flattenArray($matrixData1); $array2 = PHPExcel_Calculation_Functions::flattenArray($matrixData2); - $count1 = count($array1); - $count2 = count($array2); - if ($count1 < $count2) { - $count = $count1; - } else { - $count = $count2; - } + $count = min(count($array1), count($array2)); $result = 0; for ($i = 0; $i < $count; ++$i) { @@ -1283,7 +1314,7 @@ class PHPExcel_Calculation_MathTrig { } return $result; - } // function SUMX2MY2() + } /** @@ -1293,16 +1324,11 @@ class PHPExcel_Calculation_MathTrig { * @param mixed[] $matrixData2 Matrix #2 * @return float */ - public static function SUMX2PY2($matrixData1,$matrixData2) { + public static function SUMX2PY2($matrixData1, $matrixData2) + { $array1 = PHPExcel_Calculation_Functions::flattenArray($matrixData1); $array2 = PHPExcel_Calculation_Functions::flattenArray($matrixData2); - $count1 = count($array1); - $count2 = count($array2); - if ($count1 < $count2) { - $count = $count1; - } else { - $count = $count2; - } + $count = min(count($array1), count($array2)); $result = 0; for ($i = 0; $i < $count; ++$i) { @@ -1313,7 +1339,7 @@ class PHPExcel_Calculation_MathTrig { } return $result; - } // function SUMX2PY2() + } /** @@ -1323,16 +1349,11 @@ class PHPExcel_Calculation_MathTrig { * @param mixed[] $matrixData2 Matrix #2 * @return float */ - public static function SUMXMY2($matrixData1,$matrixData2) { + public static function SUMXMY2($matrixData1, $matrixData2) + { $array1 = PHPExcel_Calculation_Functions::flattenArray($matrixData1); $array2 = PHPExcel_Calculation_Functions::flattenArray($matrixData2); - $count1 = count($array1); - $count2 = count($array2); - if ($count1 < $count2) { - $count = $count1; - } else { - $count = $count2; - } + $count = min(count($array1), count($array2)); $result = 0; for ($i = 0; $i < $count; ++$i) { @@ -1343,7 +1364,7 @@ class PHPExcel_Calculation_MathTrig { } return $result; - } // function SUMXMY2() + } /** @@ -1355,22 +1376,24 @@ class PHPExcel_Calculation_MathTrig { * @param int $digits * @return float Truncated value */ - public static function TRUNC($value = 0, $digits = 0) { + public static function TRUNC($value = 0, $digits = 0) + { $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); $digits = PHPExcel_Calculation_Functions::flattenSingleValue($digits); // Validate parameters - if ((!is_numeric($value)) || (!is_numeric($digits))) + if ((!is_numeric($value)) || (!is_numeric($digits))) { return PHPExcel_Calculation_Functions::VALUE(); - $digits = floor($digits); + } + $digits = floor($digits); // Truncate $adjust = pow(10, $digits); - if (($digits > 0) && (rtrim(intval((abs($value) - abs(intval($value))) * $adjust),'0') < $adjust/10)) + if (($digits > 0) && (rtrim(intval((abs($value) - abs(intval($value))) * $adjust), '0') < $adjust/10)) { return $value; + } return (intval($value * $adjust)) / $adjust; - } // function TRUNC() - -} // class PHPExcel_Calculation_MathTrig + } +} diff --git a/Classes/PHPExcel/Calculation/Statistical.php b/Classes/PHPExcel/Calculation/Statistical.php index 64e568b0..ad523f67 100644 --- a/Classes/PHPExcel/Calculation/Statistical.php +++ b/Classes/PHPExcel/Calculation/Statistical.php @@ -1,30 +1,4 @@ $value) { + foreach ($array1 as $key => $value) { if ((is_bool($value)) || (is_string($value)) || (is_null($value))) { unset($array1[$key]); unset($array2[$key]); } } - foreach($array2 as $key => $value) { + foreach ($array2 as $key => $value) { if ((is_bool($value)) || (is_string($value)) || (is_null($value))) { unset($array1[$key]); unset($array2[$key]); @@ -83,8 +78,8 @@ class PHPExcel_Calculation_Statistical { $array1 = array_merge($array1); $array2 = array_merge($array2); - return True; - } // function _checkTrendArrays() + return true; + } /** @@ -96,13 +91,14 @@ class PHPExcel_Calculation_Statistical { * @param q require q>0 * @return 0 if p<=0, q<=0 or p+q>2.55E305 to avoid errors and over/underflow */ - private static function _beta($p, $q) { + private static function _beta($p, $q) + { if ($p <= 0.0 || $q <= 0.0 || ($p + $q) > LOG_GAMMA_X_MAX_VALUE) { return 0.0; } else { return exp(self::_logBeta($p, $q)); } - } // function _beta() + } /** @@ -117,7 +113,8 @@ class PHPExcel_Calculation_Statistical { * @param q require q>0 * @return 0 if x<0, p<=0, q<=0 or p+q>2.55E305 and 1 if x>1 to avoid errors and over/underflow */ - private static function _incompleteBeta($x, $p, $q) { + private static function _incompleteBeta($x, $p, $q) + { if ($x <= 0.0) { return 0.0; } elseif ($x >= 1.0) { @@ -131,13 +128,13 @@ class PHPExcel_Calculation_Statistical { } else { return 1.0 - ($beta_gam * self::_betaFraction(1 - $x, $q, $p) / $q); } - } // function _incompleteBeta() + } // Function cache for _logBeta function - private static $_logBetaCache_p = 0.0; - private static $_logBetaCache_q = 0.0; - private static $_logBetaCache_result = 0.0; + private static $logBetaCacheP = 0.0; + private static $logBetaCacheQ = 0.0; + private static $logBetaCacheResult = 0.0; /** * The natural logarithm of the beta function. @@ -147,18 +144,19 @@ class PHPExcel_Calculation_Statistical { * @return 0 if p<=0, q<=0 or p+q>2.55E305 to avoid errors and over/underflow * @author Jaco van Kooten */ - private static function _logBeta($p, $q) { - if ($p != self::$_logBetaCache_p || $q != self::$_logBetaCache_q) { - self::$_logBetaCache_p = $p; - self::$_logBetaCache_q = $q; + private static function _logBeta($p, $q) + { + if ($p != self::$logBetaCacheP || $q != self::$logBetaCacheQ) { + self::$logBetaCacheP = $p; + self::$logBetaCacheQ = $q; if (($p <= 0.0) || ($q <= 0.0) || (($p + $q) > LOG_GAMMA_X_MAX_VALUE)) { - self::$_logBetaCache_result = 0.0; + self::$logBetaCacheResult = 0.0; } else { - self::$_logBetaCache_result = self::_logGamma($p) + self::_logGamma($q) - self::_logGamma($p + $q); + self::$logBetaCacheResult = self::_logGamma($p) + self::_logGamma($q) - self::_logGamma($p + $q); } } - return self::$_logBetaCache_result; - } // function _logBeta() + return self::$logBetaCacheResult; + } /** @@ -166,7 +164,8 @@ class PHPExcel_Calculation_Statistical { * Based on an idea from Numerical Recipes (W.H. Press et al, 1992). * @author Jaco van Kooten */ - private static function _betaFraction($x, $p, $q) { + private static function _betaFraction($x, $p, $q) + { $c = 1.0; $sum_pq = $p + $q; $p_plus = $p + 1.0; @@ -179,7 +178,7 @@ class PHPExcel_Calculation_Statistical { $frac = $h; $m = 1; $delta = 0.0; - while ($m <= MAX_ITERATIONS && abs($delta-1.0) > PRECISION ) { + while ($m <= MAX_ITERATIONS && abs($delta-1.0) > PRECISION) { $m2 = 2 * $m; // even index for d $d = $m * ($q - $m) * $x / ( ($p_minus + $m2) * ($p + $m2)); @@ -209,7 +208,7 @@ class PHPExcel_Calculation_Statistical { ++$m; } return $frac; - } // function _betaFraction() + } /** @@ -256,153 +255,167 @@ class PHPExcel_Calculation_Statistical { */ // Function cache for logGamma - private static $_logGammaCache_result = 0.0; - private static $_logGammaCache_x = 0.0; + private static $logGammaCacheResult = 0.0; + private static $logGammaCacheX = 0.0; - private static function _logGamma($x) { + private static function _logGamma($x) + { // Log Gamma related constants static $lg_d1 = -0.5772156649015328605195174; static $lg_d2 = 0.4227843350984671393993777; static $lg_d4 = 1.791759469228055000094023; - static $lg_p1 = array( 4.945235359296727046734888, - 201.8112620856775083915565, - 2290.838373831346393026739, - 11319.67205903380828685045, - 28557.24635671635335736389, - 38484.96228443793359990269, - 26377.48787624195437963534, - 7225.813979700288197698961 ); - static $lg_p2 = array( 4.974607845568932035012064, - 542.4138599891070494101986, - 15506.93864978364947665077, - 184793.2904445632425417223, - 1088204.76946882876749847, - 3338152.967987029735917223, - 5106661.678927352456275255, - 3074109.054850539556250927 ); - static $lg_p4 = array( 14745.02166059939948905062, - 2426813.369486704502836312, - 121475557.4045093227939592, - 2663432449.630976949898078, - 29403789566.34553899906876, - 170266573776.5398868392998, - 492612579337.743088758812, - 560625185622.3951465078242 ); + static $lg_p1 = array( + 4.945235359296727046734888, + 201.8112620856775083915565, + 2290.838373831346393026739, + 11319.67205903380828685045, + 28557.24635671635335736389, + 38484.96228443793359990269, + 26377.48787624195437963534, + 7225.813979700288197698961 + ); + static $lg_p2 = array( + 4.974607845568932035012064, + 542.4138599891070494101986, + 15506.93864978364947665077, + 184793.2904445632425417223, + 1088204.76946882876749847, + 3338152.967987029735917223, + 5106661.678927352456275255, + 3074109.054850539556250927 + ); + static $lg_p4 = array( + 14745.02166059939948905062, + 2426813.369486704502836312, + 121475557.4045093227939592, + 2663432449.630976949898078, + 29403789566.34553899906876, + 170266573776.5398868392998, + 492612579337.743088758812, + 560625185622.3951465078242 + ); + static $lg_q1 = array( + 67.48212550303777196073036, + 1113.332393857199323513008, + 7738.757056935398733233834, + 27639.87074403340708898585, + 54993.10206226157329794414, + 61611.22180066002127833352, + 36351.27591501940507276287, + 8785.536302431013170870835 + ); + static $lg_q2 = array( + 183.0328399370592604055942, + 7765.049321445005871323047, + 133190.3827966074194402448, + 1136705.821321969608938755, + 5267964.117437946917577538, + 13467014.54311101692290052, + 17827365.30353274213975932, + 9533095.591844353613395747 + ); + static $lg_q4 = array( + 2690.530175870899333379843, + 639388.5654300092398984238, + 41355999.30241388052042842, + 1120872109.61614794137657, + 14886137286.78813811542398, + 101680358627.2438228077304, + 341747634550.7377132798597, + 446315818741.9713286462081 + ); + static $lg_c = array( + -0.001910444077728, + 8.4171387781295e-4, + -5.952379913043012e-4, + 7.93650793500350248e-4, + -0.002777777777777681622553, + 0.08333333333333333331554247, + 0.0057083835261 + ); - static $lg_q1 = array( 67.48212550303777196073036, - 1113.332393857199323513008, - 7738.757056935398733233834, - 27639.87074403340708898585, - 54993.10206226157329794414, - 61611.22180066002127833352, - 36351.27591501940507276287, - 8785.536302431013170870835 ); - static $lg_q2 = array( 183.0328399370592604055942, - 7765.049321445005871323047, - 133190.3827966074194402448, - 1136705.821321969608938755, - 5267964.117437946917577538, - 13467014.54311101692290052, - 17827365.30353274213975932, - 9533095.591844353613395747 ); - static $lg_q4 = array( 2690.530175870899333379843, - 639388.5654300092398984238, - 41355999.30241388052042842, - 1120872109.61614794137657, - 14886137286.78813811542398, - 101680358627.2438228077304, - 341747634550.7377132798597, - 446315818741.9713286462081 ); - - static $lg_c = array( -0.001910444077728, - 8.4171387781295e-4, - -5.952379913043012e-4, - 7.93650793500350248e-4, - -0.002777777777777681622553, - 0.08333333333333333331554247, - 0.0057083835261 ); - - // Rough estimate of the fourth root of logGamma_xBig - static $lg_frtbig = 2.25e76; - static $pnt68 = 0.6796875; + // Rough estimate of the fourth root of logGamma_xBig + static $lg_frtbig = 2.25e76; + static $pnt68 = 0.6796875; - if ($x == self::$_logGammaCache_x) { - return self::$_logGammaCache_result; - } - $y = $x; - if ($y > 0.0 && $y <= LOG_GAMMA_X_MAX_VALUE) { - if ($y <= EPS) { - $res = -log(y); - } elseif ($y <= 1.5) { - // --------------------- - // EPS .LT. X .LE. 1.5 - // --------------------- - if ($y < $pnt68) { - $corr = -log($y); - $xm1 = $y; - } else { - $corr = 0.0; - $xm1 = $y - 1.0; - } - if ($y <= 0.5 || $y >= $pnt68) { - $xden = 1.0; - $xnum = 0.0; - for ($i = 0; $i < 8; ++$i) { - $xnum = $xnum * $xm1 + $lg_p1[$i]; - $xden = $xden * $xm1 + $lg_q1[$i]; + if ($x == self::$logGammaCacheX) { + return self::$logGammaCacheResult; + } + $y = $x; + if ($y > 0.0 && $y <= LOG_GAMMA_X_MAX_VALUE) { + if ($y <= EPS) { + $res = -log(y); + } elseif ($y <= 1.5) { + // --------------------- + // EPS .LT. X .LE. 1.5 + // --------------------- + if ($y < $pnt68) { + $corr = -log($y); + $xm1 = $y; + } else { + $corr = 0.0; + $xm1 = $y - 1.0; } - $res = $corr + $xm1 * ($lg_d1 + $xm1 * ($xnum / $xden)); - } else { - $xm2 = $y - 1.0; + if ($y <= 0.5 || $y >= $pnt68) { + $xden = 1.0; + $xnum = 0.0; + for ($i = 0; $i < 8; ++$i) { + $xnum = $xnum * $xm1 + $lg_p1[$i]; + $xden = $xden * $xm1 + $lg_q1[$i]; + } + $res = $corr + $xm1 * ($lg_d1 + $xm1 * ($xnum / $xden)); + } else { + $xm2 = $y - 1.0; + $xden = 1.0; + $xnum = 0.0; + for ($i = 0; $i < 8; ++$i) { + $xnum = $xnum * $xm2 + $lg_p2[$i]; + $xden = $xden * $xm2 + $lg_q2[$i]; + } + $res = $corr + $xm2 * ($lg_d2 + $xm2 * ($xnum / $xden)); + } + } elseif ($y <= 4.0) { + // --------------------- + // 1.5 .LT. X .LE. 4.0 + // --------------------- + $xm2 = $y - 2.0; $xden = 1.0; $xnum = 0.0; for ($i = 0; $i < 8; ++$i) { $xnum = $xnum * $xm2 + $lg_p2[$i]; $xden = $xden * $xm2 + $lg_q2[$i]; } - $res = $corr + $xm2 * ($lg_d2 + $xm2 * ($xnum / $xden)); - } - } elseif ($y <= 4.0) { - // --------------------- - // 1.5 .LT. X .LE. 4.0 - // --------------------- - $xm2 = $y - 2.0; - $xden = 1.0; - $xnum = 0.0; - for ($i = 0; $i < 8; ++$i) { - $xnum = $xnum * $xm2 + $lg_p2[$i]; - $xden = $xden * $xm2 + $lg_q2[$i]; - } - $res = $xm2 * ($lg_d2 + $xm2 * ($xnum / $xden)); - } elseif ($y <= 12.0) { - // ---------------------- - // 4.0 .LT. X .LE. 12.0 - // ---------------------- - $xm4 = $y - 4.0; - $xden = -1.0; - $xnum = 0.0; - for ($i = 0; $i < 8; ++$i) { - $xnum = $xnum * $xm4 + $lg_p4[$i]; - $xden = $xden * $xm4 + $lg_q4[$i]; - } - $res = $lg_d4 + $xm4 * ($xnum / $xden); - } else { - // --------------------------------- - // Evaluate for argument .GE. 12.0 - // --------------------------------- - $res = 0.0; - if ($y <= $lg_frtbig) { - $res = $lg_c[6]; - $ysq = $y * $y; - for ($i = 0; $i < 6; ++$i) - $res = $res / $ysq + $lg_c[$i]; + $res = $xm2 * ($lg_d2 + $xm2 * ($xnum / $xden)); + } elseif ($y <= 12.0) { + // ---------------------- + // 4.0 .LT. X .LE. 12.0 + // ---------------------- + $xm4 = $y - 4.0; + $xden = -1.0; + $xnum = 0.0; + for ($i = 0; $i < 8; ++$i) { + $xnum = $xnum * $xm4 + $lg_p4[$i]; + $xden = $xden * $xm4 + $lg_q4[$i]; + } + $res = $lg_d4 + $xm4 * ($xnum / $xden); + } else { + // --------------------------------- + // Evaluate for argument .GE. 12.0 + // --------------------------------- + $res = 0.0; + if ($y <= $lg_frtbig) { + $res = $lg_c[6]; + $ysq = $y * $y; + for ($i = 0; $i < 6; ++$i) { + $res = $res / $ysq + $lg_c[$i]; + } + $res /= $y; + $corr = log($y); + $res = $res + log(SQRT2PI) - 0.5 * $corr; + $res += $y * ($corr - 1.0); } - $res /= $y; - $corr = log($y); - $res = $res + log(SQRT2PI) - 0.5 * $corr; - $res += $y * ($corr - 1.0); } } else { // -------------------------- @@ -413,16 +426,17 @@ class PHPExcel_Calculation_Statistical { // ------------------------------ // Final adjustments and return // ------------------------------ - self::$_logGammaCache_x = $x; - self::$_logGammaCache_result = $res; + self::$logGammaCacheX = $x; + self::$logGammaCacheResult = $res; return $res; - } // function _logGamma() + } // // Private implementation of the incomplete Gamma function // - private static function _incompleteGamma($a,$x) { + private static function _incompleteGamma($a, $x) + { static $max = 32; $summer = 0; for ($n=0; $n<=$max; ++$n) { @@ -430,37 +444,41 @@ class PHPExcel_Calculation_Statistical { for ($i=1; $i<=$n; ++$i) { $divisor *= ($a + $i); } - $summer += (pow($x,$n) / $divisor); + $summer += (pow($x, $n) / $divisor); } - return pow($x,$a) * exp(0-$x) * $summer; - } // function _incompleteGamma() + return pow($x, $a) * exp(0-$x) * $summer; + } // // Private implementation of the Gamma function // - private static function _gamma($data) { - if ($data == 0.0) return 0; + private static function _gamma($data) + { + if ($data == 0.0) { + return 0; + } static $p0 = 1.000000000190015; - static $p = array ( 1 => 76.18009172947146, - 2 => -86.50532032941677, - 3 => 24.01409824083091, - 4 => -1.231739572450155, - 5 => 1.208650973866179e-3, - 6 => -5.395239384953e-6 - ); + static $p = array( + 1 => 76.18009172947146, + 2 => -86.50532032941677, + 3 => 24.01409824083091, + 4 => -1.231739572450155, + 5 => 1.208650973866179e-3, + 6 => -5.395239384953e-6 + ); $y = $x = $data; $tmp = $x + 5.5; $tmp -= ($x + 0.5) * log($tmp); $summer = $p0; - for ($j=1;$j<=6;++$j) { + for ($j=1; $j<=6; ++$j) { $summer += ($p[$j] / ++$y); } return exp(0 - $tmp + log(SQRT2PI * $summer / $x)); - } // function _gamma() + } /*************************************************************************** @@ -471,7 +489,8 @@ class PHPExcel_Calculation_Statistical { * email : nickersonm@yahoo.com * ***************************************************************************/ - private static function _inverse_ncdf($p) { + private static function inverseNcdf($p) + { // Inverse ncdf approximation by Peter J. Acklam, implementation adapted to // PHP by Michael Nickerson, using Dr. Thomas Ziegler's C implementation as // a guide. http://home.online.no/~pjacklam/notes/invnorm/index.html @@ -485,34 +504,38 @@ class PHPExcel_Calculation_Statistical { // Input paramater is $p - probability - where 0 < p < 1. // Coefficients in rational approximations - static $a = array( 1 => -3.969683028665376e+01, - 2 => 2.209460984245205e+02, - 3 => -2.759285104469687e+02, - 4 => 1.383577518672690e+02, - 5 => -3.066479806614716e+01, - 6 => 2.506628277459239e+00 - ); + static $a = array( + 1 => -3.969683028665376e+01, + 2 => 2.209460984245205e+02, + 3 => -2.759285104469687e+02, + 4 => 1.383577518672690e+02, + 5 => -3.066479806614716e+01, + 6 => 2.506628277459239e+00 + ); - static $b = array( 1 => -5.447609879822406e+01, - 2 => 1.615858368580409e+02, - 3 => -1.556989798598866e+02, - 4 => 6.680131188771972e+01, - 5 => -1.328068155288572e+01 - ); + static $b = array( + 1 => -5.447609879822406e+01, + 2 => 1.615858368580409e+02, + 3 => -1.556989798598866e+02, + 4 => 6.680131188771972e+01, + 5 => -1.328068155288572e+01 + ); - static $c = array( 1 => -7.784894002430293e-03, - 2 => -3.223964580411365e-01, - 3 => -2.400758277161838e+00, - 4 => -2.549732539343734e+00, - 5 => 4.374664141464968e+00, - 6 => 2.938163982698783e+00 - ); + static $c = array( + 1 => -7.784894002430293e-03, + 2 => -3.223964580411365e-01, + 3 => -2.400758277161838e+00, + 4 => -2.549732539343734e+00, + 5 => 4.374664141464968e+00, + 6 => 2.938163982698783e+00 + ); - static $d = array( 1 => 7.784695709041462e-03, - 2 => 3.224671290700398e-01, - 3 => 2.445134137142996e+00, - 4 => 3.754408661907416e+00 - ); + static $d = array( + 1 => 7.784695709041462e-03, + 2 => 3.224671290700398e-01, + 3 => 2.445134137142996e+00, + 4 => 3.754408661907416e+00 + ); // Define lower and upper region break-points. $p_low = 0.02425; //Use lower region approx. below this @@ -537,10 +560,11 @@ class PHPExcel_Calculation_Statistical { } // If 0 < p < 1, return a null value return PHPExcel_Calculation_Functions::NULL(); - } // function _inverse_ncdf() + } - private static function _inverse_ncdf2($prob) { + private static function inverseNcdf2($prob) + { // Approximation of inverse standard normal CDF developed by // B. Moro, "The Full Monte," Risk 8(2), Feb 1995, 57-58. @@ -580,10 +604,11 @@ class PHPExcel_Calculation_Statistical { } } return $z; - } // function _inverse_ncdf2() + } // function inverseNcdf2() - private static function _inverse_ncdf3($p) { + private static function inverseNcdf3($p) + { // ALGORITHM AS241 APPL. STATIST. (1988) VOL. 37, NO. 3. // Produces the normal deviate Z corresponding to a given lower // tail area of P; Z is accurate to about 1 part in 10**16. @@ -662,10 +687,10 @@ class PHPExcel_Calculation_Statistical { } else { $R = 1 - $p; } - $R = pow(-log($R),2); + $R = pow(-log($R), 2); // computation for p not close to 0, 0.5 or 1. - If ($R <= $split2) { + if ($R <= $split2) { $R = $R - $const2; $z = ((((((($c7 * $R + $c6) * $R + $c5) * $R + $c4) * $R + $c3) * $R + $c2) * $R + $c1) * $R + $c0) / ((((((($d7 * $R + $d6) * $R + $d5) * $R + $d4) * $R + $d3) * $R + $d2) * $R + $d1) * $R + 1); @@ -680,7 +705,7 @@ class PHPExcel_Calculation_Statistical { } } return $z; - } // function _inverse_ncdf3() + } /** @@ -697,7 +722,8 @@ class PHPExcel_Calculation_Statistical { * @param mixed $arg,... Data values * @return float */ - public static function AVEDEV() { + public static function AVEDEV() + { $aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args()); // Return value @@ -729,7 +755,7 @@ class PHPExcel_Calculation_Statistical { return $returnValue / $aCount; } return PHPExcel_Calculation_Functions::NaN(); - } // function AVEDEV() + } /** @@ -745,7 +771,8 @@ class PHPExcel_Calculation_Statistical { * @param mixed $arg,... Data values * @return float */ - public static function AVERAGE() { + public static function AVERAGE() + { $returnValue = $aCount = 0; // Loop through arguments @@ -771,7 +798,7 @@ class PHPExcel_Calculation_Statistical { } else { return PHPExcel_Calculation_Functions::DIV0(); } - } // function AVERAGE() + } /** @@ -787,8 +814,8 @@ class PHPExcel_Calculation_Statistical { * @param mixed $arg,... Data values * @return float */ - public static function AVERAGEA() { - // Return value + public static function AVERAGEA() + { $returnValue = null; $aCount = 0; @@ -813,13 +840,12 @@ class PHPExcel_Calculation_Statistical { } } - // Return if ($aCount > 0) { return $returnValue / $aCount; } else { return PHPExcel_Calculation_Functions::DIV0(); } - } // function AVERAGEA() + } /** @@ -837,8 +863,8 @@ class PHPExcel_Calculation_Statistical { * @param mixed[] $averageArgs Data values * @return float */ - public static function AVERAGEIF($aArgs,$condition,$averageArgs = array()) { - // Return value + public static function AVERAGEIF($aArgs, $condition, $averageArgs = array()) + { $returnValue = 0; $aArgs = PHPExcel_Calculation_Functions::flattenArray($aArgs); @@ -850,7 +876,9 @@ class PHPExcel_Calculation_Statistical { // Loop through arguments $aCount = 0; foreach ($aArgs as $key => $arg) { - if (!is_numeric($arg)) { $arg = PHPExcel_Calculation::_wrapResult(strtoupper($arg)); } + if (!is_numeric($arg)) { + $arg = PHPExcel_Calculation::_wrapResult(strtoupper($arg)); + } $testCondition = '='.$arg.$condition; if (PHPExcel_Calculation::getInstance()->_calculateFormulaValue($testCondition)) { if ((is_null($returnValue)) || ($arg > $returnValue)) { @@ -860,13 +888,11 @@ class PHPExcel_Calculation_Statistical { } } - // Return if ($aCount > 0) { return $returnValue / $aCount; - } else { - return PHPExcel_Calculation_Functions::DIV0(); } - } // function AVERAGEIF() + return PHPExcel_Calculation_Functions::DIV0(); + } /** @@ -881,12 +907,13 @@ class PHPExcel_Calculation_Statistical { * @return float * */ - public static function BETADIST($value,$alpha,$beta,$rMin=0,$rMax=1) { - $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); - $alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha); - $beta = PHPExcel_Calculation_Functions::flattenSingleValue($beta); - $rMin = PHPExcel_Calculation_Functions::flattenSingleValue($rMin); - $rMax = PHPExcel_Calculation_Functions::flattenSingleValue($rMax); + public static function BETADIST($value, $alpha, $beta, $rMin = 0, $rMax = 1) + { + $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); + $alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha); + $beta = PHPExcel_Calculation_Functions::flattenSingleValue($beta); + $rMin = PHPExcel_Calculation_Functions::flattenSingleValue($rMin); + $rMax = PHPExcel_Calculation_Functions::flattenSingleValue($rMax); if ((is_numeric($value)) && (is_numeric($alpha)) && (is_numeric($beta)) && (is_numeric($rMin)) && (is_numeric($rMax))) { if (($value < $rMin) || ($value > $rMax) || ($alpha <= 0) || ($beta <= 0) || ($rMin == $rMax)) { @@ -899,10 +926,10 @@ class PHPExcel_Calculation_Statistical { } $value -= $rMin; $value /= ($rMax - $rMin); - return self::_incompleteBeta($value,$alpha,$beta); + return self::_incompleteBeta($value, $alpha, $beta); } return PHPExcel_Calculation_Functions::VALUE(); - } // function BETADIST() + } /** @@ -919,12 +946,13 @@ class PHPExcel_Calculation_Statistical { * @return float * */ - public static function BETAINV($probability,$alpha,$beta,$rMin=0,$rMax=1) { - $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability); - $alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha); - $beta = PHPExcel_Calculation_Functions::flattenSingleValue($beta); - $rMin = PHPExcel_Calculation_Functions::flattenSingleValue($rMin); - $rMax = PHPExcel_Calculation_Functions::flattenSingleValue($rMax); + public static function BETAINV($probability, $alpha, $beta, $rMin = 0, $rMax = 1) + { + $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability); + $alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha); + $beta = PHPExcel_Calculation_Functions::flattenSingleValue($beta); + $rMin = PHPExcel_Calculation_Functions::flattenSingleValue($rMin); + $rMax = PHPExcel_Calculation_Functions::flattenSingleValue($rMax); if ((is_numeric($probability)) && (is_numeric($alpha)) && (is_numeric($beta)) && (is_numeric($rMin)) && (is_numeric($rMax))) { if (($alpha <= 0) || ($beta <= 0) || ($rMin == $rMax) || ($probability <= 0) || ($probability > 1)) { @@ -953,10 +981,10 @@ class PHPExcel_Calculation_Statistical { if ($i == MAX_ITERATIONS) { return PHPExcel_Calculation_Functions::NA(); } - return round($rMin + $guess * ($rMax - $rMin),12); + return round($rMin + $guess * ($rMax - $rMin), 12); } return PHPExcel_Calculation_Functions::VALUE(); - } // function BETAINV() + } /** @@ -977,10 +1005,11 @@ class PHPExcel_Calculation_Statistical { * @todo Cumulative distribution function * */ - public static function BINOMDIST($value, $trials, $probability, $cumulative) { - $value = floor(PHPExcel_Calculation_Functions::flattenSingleValue($value)); - $trials = floor(PHPExcel_Calculation_Functions::flattenSingleValue($trials)); - $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability); + public static function BINOMDIST($value, $trials, $probability, $cumulative) + { + $value = floor(PHPExcel_Calculation_Functions::flattenSingleValue($value)); + $trials = floor(PHPExcel_Calculation_Functions::flattenSingleValue($trials)); + $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability); if ((is_numeric($value)) && (is_numeric($trials)) && (is_numeric($probability))) { if (($value < 0) || ($value > $trials)) { @@ -993,16 +1022,16 @@ class PHPExcel_Calculation_Statistical { if ($cumulative) { $summer = 0; for ($i = 0; $i <= $value; ++$i) { - $summer += PHPExcel_Calculation_MathTrig::COMBIN($trials,$i) * pow($probability,$i) * pow(1 - $probability,$trials - $i); + $summer += PHPExcel_Calculation_MathTrig::COMBIN($trials, $i) * pow($probability, $i) * pow(1 - $probability, $trials - $i); } return $summer; } else { - return PHPExcel_Calculation_MathTrig::COMBIN($trials,$value) * pow($probability,$value) * pow(1 - $probability,$trials - $value) ; + return PHPExcel_Calculation_MathTrig::COMBIN($trials, $value) * pow($probability, $value) * pow(1 - $probability, $trials - $value) ; } } } return PHPExcel_Calculation_Functions::VALUE(); - } // function BINOMDIST() + } /** @@ -1014,9 +1043,10 @@ class PHPExcel_Calculation_Statistical { * @param float $degrees degrees of freedom * @return float */ - public static function CHIDIST($value, $degrees) { - $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); - $degrees = floor(PHPExcel_Calculation_Functions::flattenSingleValue($degrees)); + public static function CHIDIST($value, $degrees) + { + $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); + $degrees = floor(PHPExcel_Calculation_Functions::flattenSingleValue($degrees)); if ((is_numeric($value)) && (is_numeric($degrees))) { if ($degrees < 1) { @@ -1028,10 +1058,10 @@ class PHPExcel_Calculation_Statistical { } return PHPExcel_Calculation_Functions::NaN(); } - return 1 - (self::_incompleteGamma($degrees/2,$value/2) / self::_gamma($degrees/2)); + return 1 - (self::_incompleteGamma($degrees/2, $value/2) / self::_gamma($degrees/2)); } return PHPExcel_Calculation_Functions::VALUE(); - } // function CHIDIST() + } /** @@ -1043,12 +1073,12 @@ class PHPExcel_Calculation_Statistical { * @param float $degrees degrees of freedom * @return float */ - public static function CHIINV($probability, $degrees) { - $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability); - $degrees = floor(PHPExcel_Calculation_Functions::flattenSingleValue($degrees)); + public static function CHIINV($probability, $degrees) + { + $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability); + $degrees = floor(PHPExcel_Calculation_Functions::flattenSingleValue($degrees)); if ((is_numeric($probability)) && (is_numeric($degrees))) { - $xLo = 100; $xHi = 0; @@ -1084,10 +1114,10 @@ class PHPExcel_Calculation_Statistical { if ($i == MAX_ITERATIONS) { return PHPExcel_Calculation_Functions::NA(); } - return round($x,12); + return round($x, 12); } return PHPExcel_Calculation_Functions::VALUE(); - } // function CHIINV() + } /** @@ -1101,10 +1131,11 @@ class PHPExcel_Calculation_Statistical { * @return float * */ - public static function CONFIDENCE($alpha,$stdDev,$size) { - $alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha); - $stdDev = PHPExcel_Calculation_Functions::flattenSingleValue($stdDev); - $size = floor(PHPExcel_Calculation_Functions::flattenSingleValue($size)); + public static function CONFIDENCE($alpha, $stdDev, $size) + { + $alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha); + $stdDev = PHPExcel_Calculation_Functions::flattenSingleValue($stdDev); + $size = floor(PHPExcel_Calculation_Functions::flattenSingleValue($size)); if ((is_numeric($alpha)) && (is_numeric($stdDev)) && (is_numeric($size))) { if (($alpha <= 0) || ($alpha >= 1)) { @@ -1116,7 +1147,7 @@ class PHPExcel_Calculation_Statistical { return self::NORMSINV(1 - $alpha / 2) * $stdDev / sqrt($size); } return PHPExcel_Calculation_Functions::VALUE(); - } // function CONFIDENCE() + } /** @@ -1128,11 +1159,12 @@ class PHPExcel_Calculation_Statistical { * @param array of mixed Data Series X * @return float */ - public static function CORREL($yValues,$xValues=null) { + public static function CORREL($yValues, $xValues = null) + { if ((is_null($xValues)) || (!is_array($yValues)) || (!is_array($xValues))) { return PHPExcel_Calculation_Functions::VALUE(); } - if (!self::_checkTrendArrays($yValues,$xValues)) { + if (!self::checkTrendArrays($yValues, $xValues)) { return PHPExcel_Calculation_Functions::VALUE(); } $yValueCount = count($yValues); @@ -1144,9 +1176,9 @@ class PHPExcel_Calculation_Statistical { return PHPExcel_Calculation_Functions::DIV0(); } - $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues); + $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR, $yValues, $xValues); return $bestFitLinear->getCorrelation(); - } // function CORREL() + } /** @@ -1162,8 +1194,8 @@ class PHPExcel_Calculation_Statistical { * @param mixed $arg,... Data values * @return int */ - public static function COUNT() { - // Return value + public static function COUNT() + { $returnValue = 0; // Loop through arguments @@ -1179,9 +1211,8 @@ class PHPExcel_Calculation_Statistical { } } - // Return return $returnValue; - } // function COUNT() + } /** @@ -1197,8 +1228,8 @@ class PHPExcel_Calculation_Statistical { * @param mixed $arg,... Data values * @return int */ - public static function COUNTA() { - // Return value + public static function COUNTA() + { $returnValue = 0; // Loop through arguments @@ -1210,9 +1241,8 @@ class PHPExcel_Calculation_Statistical { } } - // Return return $returnValue; - } // function COUNTA() + } /** @@ -1228,8 +1258,8 @@ class PHPExcel_Calculation_Statistical { * @param mixed $arg,... Data values * @return int */ - public static function COUNTBLANK() { - // Return value + public static function COUNTBLANK() + { $returnValue = 0; // Loop through arguments @@ -1241,9 +1271,8 @@ class PHPExcel_Calculation_Statistical { } } - // Return return $returnValue; - } // function COUNTBLANK() + } /** @@ -1260,15 +1289,17 @@ class PHPExcel_Calculation_Statistical { * @param string $condition The criteria that defines which cells will be counted. * @return int */ - public static function COUNTIF($aArgs,$condition) { - // Return value + public static function COUNTIF($aArgs, $condition) + { $returnValue = 0; $aArgs = PHPExcel_Calculation_Functions::flattenArray($aArgs); $condition = PHPExcel_Calculation_Functions::_ifCondition($condition); // Loop through arguments foreach ($aArgs as $arg) { - if (!is_numeric($arg)) { $arg = PHPExcel_Calculation::_wrapResult(strtoupper($arg)); } + if (!is_numeric($arg)) { + $arg = PHPExcel_Calculation::_wrapResult(strtoupper($arg)); + } $testCondition = '='.$arg.$condition; if (PHPExcel_Calculation::getInstance()->_calculateFormulaValue($testCondition)) { // Is it a value within our criteria @@ -1276,9 +1307,8 @@ class PHPExcel_Calculation_Statistical { } } - // Return return $returnValue; - } // function COUNTIF() + } /** @@ -1290,8 +1320,9 @@ class PHPExcel_Calculation_Statistical { * @param array of mixed Data Series X * @return float */ - public static function COVAR($yValues,$xValues) { - if (!self::_checkTrendArrays($yValues,$xValues)) { + public static function COVAR($yValues, $xValues) + { + if (!self::checkTrendArrays($yValues, $xValues)) { return PHPExcel_Calculation_Functions::VALUE(); } $yValueCount = count($yValues); @@ -1303,9 +1334,9 @@ class PHPExcel_Calculation_Statistical { return PHPExcel_Calculation_Functions::DIV0(); } - $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues); + $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR, $yValues, $xValues); return $bestFitLinear->getCovariance(); - } // function COVAR() + } /** @@ -1327,26 +1358,24 @@ class PHPExcel_Calculation_Statistical { * accuracy of the function (although all my tests have so far returned correct results). * */ - public static function CRITBINOM($trials, $probability, $alpha) { - $trials = floor(PHPExcel_Calculation_Functions::flattenSingleValue($trials)); - $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability); - $alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha); + public static function CRITBINOM($trials, $probability, $alpha) + { + $trials = floor(PHPExcel_Calculation_Functions::flattenSingleValue($trials)); + $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability); + $alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha); if ((is_numeric($trials)) && (is_numeric($probability)) && (is_numeric($alpha))) { if ($trials < 0) { return PHPExcel_Calculation_Functions::NaN(); - } - if (($probability < 0) || ($probability > 1)) { + } elseif (($probability < 0) || ($probability > 1)) { return PHPExcel_Calculation_Functions::NaN(); - } - if (($alpha < 0) || ($alpha > 1)) { + } elseif (($alpha < 0) || ($alpha > 1)) { return PHPExcel_Calculation_Functions::NaN(); - } - if ($alpha <= 0.5) { + } elseif ($alpha <= 0.5) { $t = sqrt(log(1 / ($alpha * $alpha))); $trialsApprox = 0 - ($t + (2.515517 + 0.802853 * $t + 0.010328 * $t * $t) / (1 + 1.432788 * $t + 0.189269 * $t * $t + 0.001308 * $t * $t * $t)); } else { - $t = sqrt(log(1 / pow(1 - $alpha,2))); + $t = sqrt(log(1 / pow(1 - $alpha, 2))); $trialsApprox = $t - (2.515517 + 0.802853 * $t + 0.010328 * $t * $t) / (1 + 1.432788 * $t + 0.189269 * $t * $t + 0.001308 * $t * $t * $t); } $Guess = floor($trials * $probability + $trialsApprox * sqrt($trials * $probability * (1 - $probability))); @@ -1361,31 +1390,47 @@ class PHPExcel_Calculation_Statistical { $m = floor($trials * $probability); ++$TotalUnscaledProbability; - if ($m == $Guess) { ++$UnscaledPGuess; } - if ($m <= $Guess) { ++$UnscaledCumPGuess; } + if ($m == $Guess) { + ++$UnscaledPGuess; + } + if ($m <= $Guess) { + ++$UnscaledCumPGuess; + } $PreviousValue = 1; - $Done = False; + $Done = false; $k = $m + 1; while ((!$Done) && ($k <= $trials)) { $CurrentValue = $PreviousValue * ($trials - $k + 1) * $probability / ($k * (1 - $probability)); $TotalUnscaledProbability += $CurrentValue; - if ($k == $Guess) { $UnscaledPGuess += $CurrentValue; } - if ($k <= $Guess) { $UnscaledCumPGuess += $CurrentValue; } - if ($CurrentValue <= $EssentiallyZero) { $Done = True; } + if ($k == $Guess) { + $UnscaledPGuess += $CurrentValue; + } + if ($k <= $Guess) { + $UnscaledCumPGuess += $CurrentValue; + } + if ($CurrentValue <= $EssentiallyZero) { + $Done = true; + } $PreviousValue = $CurrentValue; ++$k; } $PreviousValue = 1; - $Done = False; + $Done = false; $k = $m - 1; while ((!$Done) && ($k >= 0)) { $CurrentValue = $PreviousValue * $k + 1 * (1 - $probability) / (($trials - $k) * $probability); $TotalUnscaledProbability += $CurrentValue; - if ($k == $Guess) { $UnscaledPGuess += $CurrentValue; } - if ($k <= $Guess) { $UnscaledCumPGuess += $CurrentValue; } - if ($CurrentValue <= $EssentiallyZero) { $Done = True; } + if ($k == $Guess) { + $UnscaledPGuess += $CurrentValue; + } + if ($k <= $Guess) { + $UnscaledCumPGuess += $CurrentValue; + } + if ($CurrentValue <= $EssentiallyZero) { + $Done = true; + } $PreviousValue = $CurrentValue; --$k; } @@ -1396,7 +1441,7 @@ class PHPExcel_Calculation_Statistical { // $CumPGuessMinus1 = $CumPGuess - $PGuess; $CumPGuessMinus1 = $CumPGuess - 1; - while (True) { + while (true) { if (($CumPGuessMinus1 < $alpha) && ($CumPGuess >= $alpha)) { return $Guess; } elseif (($CumPGuessMinus1 < $alpha) && ($CumPGuess < $alpha)) { @@ -1415,7 +1460,7 @@ class PHPExcel_Calculation_Statistical { } } return PHPExcel_Calculation_Functions::VALUE(); - } // function CRITBINOM() + } /** @@ -1431,7 +1476,8 @@ class PHPExcel_Calculation_Statistical { * @param mixed $arg,... Data values * @return float */ - public static function DEVSQ() { + public static function DEVSQ() + { $aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args()); // Return value @@ -1443,14 +1489,15 @@ class PHPExcel_Calculation_Statistical { foreach ($aArgs as $k => $arg) { // Is it a numeric value? if ((is_bool($arg)) && - ((!PHPExcel_Calculation_Functions::isCellValue($k)) || (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE))) { + ((!PHPExcel_Calculation_Functions::isCellValue($k)) || + (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE))) { $arg = (integer) $arg; } if ((is_numeric($arg)) && (!is_string($arg))) { if (is_null($returnValue)) { - $returnValue = pow(($arg - $aMean),2); + $returnValue = pow(($arg - $aMean), 2); } else { - $returnValue += pow(($arg - $aMean),2); + $returnValue += pow(($arg - $aMean), 2); } ++$aCount; } @@ -1464,7 +1511,7 @@ class PHPExcel_Calculation_Statistical { } } return self::NA(); - } // function DEVSQ() + } /** @@ -1479,7 +1526,8 @@ class PHPExcel_Calculation_Statistical { * @param boolean $cumulative * @return float */ - public static function EXPONDIST($value, $lambda, $cumulative) { + public static function EXPONDIST($value, $lambda, $cumulative) + { $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); $lambda = PHPExcel_Calculation_Functions::flattenSingleValue($lambda); $cumulative = PHPExcel_Calculation_Functions::flattenSingleValue($cumulative); @@ -1497,7 +1545,7 @@ class PHPExcel_Calculation_Statistical { } } return PHPExcel_Calculation_Functions::VALUE(); - } // function EXPONDIST() + } /** @@ -1510,7 +1558,8 @@ class PHPExcel_Calculation_Statistical { * @param float $value * @return float */ - public static function FISHER($value) { + public static function FISHER($value) + { $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); if (is_numeric($value)) { @@ -1520,7 +1569,7 @@ class PHPExcel_Calculation_Statistical { return 0.5 * log((1+$value)/(1-$value)); } return PHPExcel_Calculation_Functions::VALUE(); - } // function FISHER() + } /** @@ -1533,14 +1582,15 @@ class PHPExcel_Calculation_Statistical { * @param float $value * @return float */ - public static function FISHERINV($value) { + public static function FISHERINV($value) + { $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); if (is_numeric($value)) { return (exp(2 * $value) - 1) / (exp(2 * $value) + 1); } return PHPExcel_Calculation_Functions::VALUE(); - } // function FISHERINV() + } /** @@ -1553,13 +1603,12 @@ class PHPExcel_Calculation_Statistical { * @param array of mixed Data Series X * @return float */ - public static function FORECAST($xValue,$yValues,$xValues) { + public static function FORECAST($xValue, $yValues, $xValues) + { $xValue = PHPExcel_Calculation_Functions::flattenSingleValue($xValue); if (!is_numeric($xValue)) { return PHPExcel_Calculation_Functions::VALUE(); - } - - if (!self::_checkTrendArrays($yValues,$xValues)) { + } elseif (!self::checkTrendArrays($yValues, $xValues)) { return PHPExcel_Calculation_Functions::VALUE(); } $yValueCount = count($yValues); @@ -1571,9 +1620,9 @@ class PHPExcel_Calculation_Statistical { return PHPExcel_Calculation_Functions::DIV0(); } - $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues); + $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR, $yValues, $xValues); return $bestFitLinear->getValueOfYForX($xValue); - } // function FORECAST() + } /** @@ -1588,10 +1637,11 @@ class PHPExcel_Calculation_Statistical { * @return float * */ - public static function GAMMADIST($value,$a,$b,$cumulative) { - $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); - $a = PHPExcel_Calculation_Functions::flattenSingleValue($a); - $b = PHPExcel_Calculation_Functions::flattenSingleValue($b); + public static function GAMMADIST($value, $a, $b, $cumulative) + { + $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); + $a = PHPExcel_Calculation_Functions::flattenSingleValue($a); + $b = PHPExcel_Calculation_Functions::flattenSingleValue($b); if ((is_numeric($value)) && (is_numeric($a)) && (is_numeric($b))) { if (($value < 0) || ($a <= 0) || ($b <= 0)) { @@ -1599,14 +1649,14 @@ class PHPExcel_Calculation_Statistical { } if ((is_numeric($cumulative)) || (is_bool($cumulative))) { if ($cumulative) { - return self::_incompleteGamma($a,$value / $b) / self::_gamma($a); + return self::_incompleteGamma($a, $value / $b) / self::_gamma($a); } else { - return (1 / (pow($b,$a) * self::_gamma($a))) * pow($value,$a-1) * exp(0-($value / $b)); + return (1 / (pow($b, $a) * self::_gamma($a))) * pow($value, $a-1) * exp(0-($value / $b)); } } } return PHPExcel_Calculation_Functions::VALUE(); - } // function GAMMADIST() + } /** @@ -1620,10 +1670,11 @@ class PHPExcel_Calculation_Statistical { * @return float * */ - public static function GAMMAINV($probability,$alpha,$beta) { - $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability); - $alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha); - $beta = PHPExcel_Calculation_Functions::flattenSingleValue($beta); + public static function GAMMAINV($probability, $alpha, $beta) + { + $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability); + $alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha); + $beta = PHPExcel_Calculation_Functions::flattenSingleValue($beta); if ((is_numeric($probability)) && (is_numeric($alpha)) && (is_numeric($beta))) { if (($alpha <= 0) || ($beta <= 0) || ($probability < 0) || ($probability > 1)) { @@ -1640,13 +1691,13 @@ class PHPExcel_Calculation_Statistical { while ((abs($dx) > PRECISION) && ($i++ < MAX_ITERATIONS)) { // Apply Newton-Raphson step - $error = self::GAMMADIST($x, $alpha, $beta, True) - $probability; + $error = self::GAMMADIST($x, $alpha, $beta, true) - $probability; if ($error < 0.0) { $xLo = $x; } else { $xHi = $x; } - $pdf = self::GAMMADIST($x, $alpha, $beta, False); + $pdf = self::GAMMADIST($x, $alpha, $beta, false); // Avoid division by zero if ($pdf != 0.0) { $dx = $error / $pdf; @@ -1667,7 +1718,7 @@ class PHPExcel_Calculation_Statistical { return $x; } return PHPExcel_Calculation_Functions::VALUE(); - } // function GAMMAINV() + } /** @@ -1678,7 +1729,8 @@ class PHPExcel_Calculation_Statistical { * @param float $value * @return float */ - public static function GAMMALN($value) { + public static function GAMMALN($value) + { $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); if (is_numeric($value)) { @@ -1688,7 +1740,7 @@ class PHPExcel_Calculation_Statistical { return log(self::_gamma($value)); } return PHPExcel_Calculation_Functions::VALUE(); - } // function GAMMALN() + } /** @@ -1706,7 +1758,8 @@ class PHPExcel_Calculation_Statistical { * @param mixed $arg,... Data values * @return float */ - public static function GEOMEAN() { + public static function GEOMEAN() + { $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args()); $aMean = PHPExcel_Calculation_MathTrig::PRODUCT($aArgs); @@ -1717,7 +1770,7 @@ class PHPExcel_Calculation_Statistical { } } return PHPExcel_Calculation_Functions::NaN(); - } // GEOMEAN() + } /** @@ -1731,24 +1784,25 @@ class PHPExcel_Calculation_Statistical { * @param boolean A logical value specifying whether to force the intersect to equal 0. * @return array of float */ - public static function GROWTH($yValues,$xValues=array(),$newValues=array(),$const=True) { + public static function GROWTH($yValues, $xValues = array(), $newValues = array(), $const = true) + { $yValues = PHPExcel_Calculation_Functions::flattenArray($yValues); $xValues = PHPExcel_Calculation_Functions::flattenArray($xValues); $newValues = PHPExcel_Calculation_Functions::flattenArray($newValues); - $const = (is_null($const)) ? True : (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($const); + $const = (is_null($const)) ? true : (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($const); - $bestFitExponential = trendClass::calculate(trendClass::TREND_EXPONENTIAL,$yValues,$xValues,$const); + $bestFitExponential = trendClass::calculate(trendClass::TREND_EXPONENTIAL, $yValues, $xValues, $const); if (empty($newValues)) { $newValues = $bestFitExponential->getXValues(); } $returnArray = array(); - foreach($newValues as $xValue) { + foreach ($newValues as $xValue) { $returnArray[0][] = $bestFitExponential->getValueOfYForX($xValue); } return $returnArray; - } // function GROWTH() + } /** @@ -1765,7 +1819,8 @@ class PHPExcel_Calculation_Statistical { * @param mixed $arg,... Data values * @return float */ - public static function HARMEAN() { + public static function HARMEAN() + { // Return value $returnValue = PHPExcel_Calculation_Functions::NA(); @@ -1796,7 +1851,7 @@ class PHPExcel_Calculation_Statistical { } else { return $returnValue; } - } // function HARMEAN() + } /** @@ -1812,11 +1867,12 @@ class PHPExcel_Calculation_Statistical { * @return float * */ - public static function HYPGEOMDIST($sampleSuccesses, $sampleNumber, $populationSuccesses, $populationNumber) { - $sampleSuccesses = floor(PHPExcel_Calculation_Functions::flattenSingleValue($sampleSuccesses)); - $sampleNumber = floor(PHPExcel_Calculation_Functions::flattenSingleValue($sampleNumber)); - $populationSuccesses = floor(PHPExcel_Calculation_Functions::flattenSingleValue($populationSuccesses)); - $populationNumber = floor(PHPExcel_Calculation_Functions::flattenSingleValue($populationNumber)); + public static function HYPGEOMDIST($sampleSuccesses, $sampleNumber, $populationSuccesses, $populationNumber) + { + $sampleSuccesses = floor(PHPExcel_Calculation_Functions::flattenSingleValue($sampleSuccesses)); + $sampleNumber = floor(PHPExcel_Calculation_Functions::flattenSingleValue($sampleNumber)); + $populationSuccesses = floor(PHPExcel_Calculation_Functions::flattenSingleValue($populationSuccesses)); + $populationNumber = floor(PHPExcel_Calculation_Functions::flattenSingleValue($populationNumber)); if ((is_numeric($sampleSuccesses)) && (is_numeric($sampleNumber)) && (is_numeric($populationSuccesses)) && (is_numeric($populationNumber))) { if (($sampleSuccesses < 0) || ($sampleSuccesses > $sampleNumber) || ($sampleSuccesses > $populationSuccesses)) { @@ -1828,12 +1884,12 @@ class PHPExcel_Calculation_Statistical { if (($populationSuccesses <= 0) || ($populationSuccesses > $populationNumber)) { return PHPExcel_Calculation_Functions::NaN(); } - return PHPExcel_Calculation_MathTrig::COMBIN($populationSuccesses,$sampleSuccesses) * - PHPExcel_Calculation_MathTrig::COMBIN($populationNumber - $populationSuccesses,$sampleNumber - $sampleSuccesses) / - PHPExcel_Calculation_MathTrig::COMBIN($populationNumber,$sampleNumber); + return PHPExcel_Calculation_MathTrig::COMBIN($populationSuccesses, $sampleSuccesses) * + PHPExcel_Calculation_MathTrig::COMBIN($populationNumber - $populationSuccesses, $sampleNumber - $sampleSuccesses) / + PHPExcel_Calculation_MathTrig::COMBIN($populationNumber, $sampleNumber); } return PHPExcel_Calculation_Functions::VALUE(); - } // function HYPGEOMDIST() + } /** @@ -1845,8 +1901,9 @@ class PHPExcel_Calculation_Statistical { * @param array of mixed Data Series X * @return float */ - public static function INTERCEPT($yValues,$xValues) { - if (!self::_checkTrendArrays($yValues,$xValues)) { + public static function INTERCEPT($yValues, $xValues) + { + if (!self::checkTrendArrays($yValues, $xValues)) { return PHPExcel_Calculation_Functions::VALUE(); } $yValueCount = count($yValues); @@ -1858,9 +1915,9 @@ class PHPExcel_Calculation_Statistical { return PHPExcel_Calculation_Functions::DIV0(); } - $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues); + $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR, $yValues, $xValues); return $bestFitLinear->getIntersect(); - } // function INTERCEPT() + } /** @@ -1874,7 +1931,8 @@ class PHPExcel_Calculation_Statistical { * @param array Data Series * @return float */ - public static function KURT() { + public static function KURT() + { $aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args()); $mean = self::AVERAGE($aArgs); $stdDev = self::STDEV($aArgs); @@ -1888,7 +1946,7 @@ class PHPExcel_Calculation_Statistical { } else { // Is it a numeric value? if ((is_numeric($arg)) && (!is_string($arg))) { - $summer += pow((($arg - $mean) / $stdDev),4) ; + $summer += pow((($arg - $mean) / $stdDev), 4); ++$count; } } @@ -1896,11 +1954,11 @@ class PHPExcel_Calculation_Statistical { // Return if ($count > 3) { - return $summer * ($count * ($count+1) / (($count-1) * ($count-2) * ($count-3))) - (3 * pow($count-1,2) / (($count-2) * ($count-3))); + return $summer * ($count * ($count+1) / (($count-1) * ($count-2) * ($count-3))) - (3 * pow($count-1, 2) / (($count-2) * ($count-3))); } } return PHPExcel_Calculation_Functions::DIV0(); - } // function KURT() + } /** @@ -1919,7 +1977,8 @@ class PHPExcel_Calculation_Statistical { * @return float * */ - public static function LARGE() { + public static function LARGE() + { $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args()); // Calculate @@ -1942,7 +2001,7 @@ class PHPExcel_Calculation_Statistical { return $mArgs[$entry]; } return PHPExcel_Calculation_Functions::VALUE(); - } // function LARGE() + } /** @@ -1957,12 +2016,15 @@ class PHPExcel_Calculation_Statistical { * @param boolean A logical value specifying whether to return additional regression statistics. * @return array */ - public static function LINEST($yValues, $xValues = NULL, $const = TRUE, $stats = FALSE) { - $const = (is_null($const)) ? TRUE : (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($const); - $stats = (is_null($stats)) ? FALSE : (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($stats); - if (is_null($xValues)) $xValues = range(1,count(PHPExcel_Calculation_Functions::flattenArray($yValues))); + public static function LINEST($yValues, $xValues = null, $const = true, $stats = false) + { + $const = (is_null($const)) ? true : (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($const); + $stats = (is_null($stats)) ? false : (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($stats); + if (is_null($xValues)) { + $xValues = range(1, count(PHPExcel_Calculation_Functions::flattenArray($yValues))); + } - if (!self::_checkTrendArrays($yValues,$xValues)) { + if (!self::checkTrendArrays($yValues, $xValues)) { return PHPExcel_Calculation_Functions::VALUE(); } $yValueCount = count($yValues); @@ -1975,27 +2037,31 @@ class PHPExcel_Calculation_Statistical { return 0; } - $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues,$const); + $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR, $yValues, $xValues, $const); if ($stats) { - return array( array( $bestFitLinear->getSlope(), - $bestFitLinear->getSlopeSE(), - $bestFitLinear->getGoodnessOfFit(), - $bestFitLinear->getF(), - $bestFitLinear->getSSRegression(), - ), - array( $bestFitLinear->getIntersect(), - $bestFitLinear->getIntersectSE(), - $bestFitLinear->getStdevOfResiduals(), - $bestFitLinear->getDFResiduals(), - $bestFitLinear->getSSResiduals() - ) - ); + return array( + array( + $bestFitLinear->getSlope(), + $bestFitLinear->getSlopeSE(), + $bestFitLinear->getGoodnessOfFit(), + $bestFitLinear->getF(), + $bestFitLinear->getSSRegression(), + ), + array( + $bestFitLinear->getIntersect(), + $bestFitLinear->getIntersectSE(), + $bestFitLinear->getStdevOfResiduals(), + $bestFitLinear->getDFResiduals(), + $bestFitLinear->getSSResiduals() + ) + ); } else { - return array( $bestFitLinear->getSlope(), - $bestFitLinear->getIntersect() - ); + return array( + $bestFitLinear->getSlope(), + $bestFitLinear->getIntersect() + ); } - } // function LINEST() + } /** @@ -2010,18 +2076,21 @@ class PHPExcel_Calculation_Statistical { * @param boolean A logical value specifying whether to return additional regression statistics. * @return array */ - public static function LOGEST($yValues,$xValues=null,$const=True,$stats=False) { - $const = (is_null($const)) ? True : (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($const); - $stats = (is_null($stats)) ? False : (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($stats); - if (is_null($xValues)) $xValues = range(1,count(PHPExcel_Calculation_Functions::flattenArray($yValues))); + public static function LOGEST($yValues, $xValues = null, $const = true, $stats = false) + { + $const = (is_null($const)) ? true : (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($const); + $stats = (is_null($stats)) ? false : (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($stats); + if (is_null($xValues)) { + $xValues = range(1, count(PHPExcel_Calculation_Functions::flattenArray($yValues))); + } - if (!self::_checkTrendArrays($yValues,$xValues)) { + if (!self::checkTrendArrays($yValues, $xValues)) { return PHPExcel_Calculation_Functions::VALUE(); } $yValueCount = count($yValues); $xValueCount = count($xValues); - foreach($yValues as $value) { + foreach ($yValues as $value) { if ($value <= 0.0) { return PHPExcel_Calculation_Functions::NaN(); } @@ -2034,27 +2103,31 @@ class PHPExcel_Calculation_Statistical { return 1; } - $bestFitExponential = trendClass::calculate(trendClass::TREND_EXPONENTIAL,$yValues,$xValues,$const); + $bestFitExponential = trendClass::calculate(trendClass::TREND_EXPONENTIAL, $yValues, $xValues, $const); if ($stats) { - return array( array( $bestFitExponential->getSlope(), - $bestFitExponential->getSlopeSE(), - $bestFitExponential->getGoodnessOfFit(), - $bestFitExponential->getF(), - $bestFitExponential->getSSRegression(), - ), - array( $bestFitExponential->getIntersect(), - $bestFitExponential->getIntersectSE(), - $bestFitExponential->getStdevOfResiduals(), - $bestFitExponential->getDFResiduals(), - $bestFitExponential->getSSResiduals() - ) - ); + return array( + array( + $bestFitExponential->getSlope(), + $bestFitExponential->getSlopeSE(), + $bestFitExponential->getGoodnessOfFit(), + $bestFitExponential->getF(), + $bestFitExponential->getSSRegression(), + ), + array( + $bestFitExponential->getIntersect(), + $bestFitExponential->getIntersectSE(), + $bestFitExponential->getStdevOfResiduals(), + $bestFitExponential->getDFResiduals(), + $bestFitExponential->getSSResiduals() + ) + ); } else { - return array( $bestFitExponential->getSlope(), - $bestFitExponential->getIntersect() - ); + return array( + $bestFitExponential->getSlope(), + $bestFitExponential->getIntersect() + ); } - } // function LOGEST() + } /** @@ -2071,10 +2144,11 @@ class PHPExcel_Calculation_Statistical { * accuracy if I can get my head round the mathematics * (as described at) http://home.online.no/~pjacklam/notes/invnorm/ */ - public static function LOGINV($probability, $mean, $stdDev) { - $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability); - $mean = PHPExcel_Calculation_Functions::flattenSingleValue($mean); - $stdDev = PHPExcel_Calculation_Functions::flattenSingleValue($stdDev); + public static function LOGINV($probability, $mean, $stdDev) + { + $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability); + $mean = PHPExcel_Calculation_Functions::flattenSingleValue($mean); + $stdDev = PHPExcel_Calculation_Functions::flattenSingleValue($stdDev); if ((is_numeric($probability)) && (is_numeric($mean)) && (is_numeric($stdDev))) { if (($probability < 0) || ($probability > 1) || ($stdDev <= 0)) { @@ -2083,7 +2157,7 @@ class PHPExcel_Calculation_Statistical { return exp($mean + $stdDev * self::NORMSINV($probability)); } return PHPExcel_Calculation_Functions::VALUE(); - } // function LOGINV() + } /** @@ -2097,10 +2171,11 @@ class PHPExcel_Calculation_Statistical { * @param float $stdDev * @return float */ - public static function LOGNORMDIST($value, $mean, $stdDev) { - $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); - $mean = PHPExcel_Calculation_Functions::flattenSingleValue($mean); - $stdDev = PHPExcel_Calculation_Functions::flattenSingleValue($stdDev); + public static function LOGNORMDIST($value, $mean, $stdDev) + { + $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); + $mean = PHPExcel_Calculation_Functions::flattenSingleValue($mean); + $stdDev = PHPExcel_Calculation_Functions::flattenSingleValue($stdDev); if ((is_numeric($value)) && (is_numeric($mean)) && (is_numeric($stdDev))) { if (($value <= 0) || ($stdDev <= 0)) { @@ -2109,7 +2184,7 @@ class PHPExcel_Calculation_Statistical { return self::NORMSDIST((log($value) - $mean) / $stdDev); } return PHPExcel_Calculation_Functions::VALUE(); - } // function LOGNORMDIST() + } /** @@ -2126,8 +2201,8 @@ class PHPExcel_Calculation_Statistical { * @param mixed $arg,... Data values * @return float */ - public static function MAX() { - // Return value + public static function MAX() + { $returnValue = null; // Loop through arguments @@ -2141,12 +2216,11 @@ class PHPExcel_Calculation_Statistical { } } - // Return - if(is_null($returnValue)) { + if (is_null($returnValue)) { return 0; } return $returnValue; - } // function MAX() + } /** @@ -2162,8 +2236,8 @@ class PHPExcel_Calculation_Statistical { * @param mixed $arg,... Data values * @return float */ - public static function MAXA() { - // Return value + public static function MAXA() + { $returnValue = null; // Loop through arguments @@ -2182,12 +2256,11 @@ class PHPExcel_Calculation_Statistical { } } - // Return - if(is_null($returnValue)) { + if (is_null($returnValue)) { return 0; } return $returnValue; - } // function MAXA() + } /** @@ -2204,8 +2277,8 @@ class PHPExcel_Calculation_Statistical { * @param string $condition The criteria that defines which cells will be checked. * @return float */ - public static function MAXIF($aArgs,$condition,$sumArgs = array()) { - // Return value + public static function MAXIF($aArgs, $condition, $sumArgs = array()) + { $returnValue = null; $aArgs = PHPExcel_Calculation_Functions::flattenArray($aArgs); @@ -2216,7 +2289,9 @@ class PHPExcel_Calculation_Statistical { $condition = PHPExcel_Calculation_Functions::_ifCondition($condition); // Loop through arguments foreach ($aArgs as $key => $arg) { - if (!is_numeric($arg)) { $arg = PHPExcel_Calculation::_wrapResult(strtoupper($arg)); } + if (!is_numeric($arg)) { + $arg = PHPExcel_Calculation::_wrapResult(strtoupper($arg)); + } $testCondition = '='.$arg.$condition; if (PHPExcel_Calculation::getInstance()->_calculateFormulaValue($testCondition)) { if ((is_null($returnValue)) || ($arg > $returnValue)) { @@ -2225,10 +2300,8 @@ class PHPExcel_Calculation_Statistical { } } - // Return return $returnValue; - } // function MAXIF() - + } /** * MEDIAN @@ -2243,8 +2316,8 @@ class PHPExcel_Calculation_Statistical { * @param mixed $arg,... Data values * @return float */ - public static function MEDIAN() { - // Return value + public static function MEDIAN() + { $returnValue = PHPExcel_Calculation_Functions::NaN(); $mArgs = array(); @@ -2259,7 +2332,7 @@ class PHPExcel_Calculation_Statistical { $mValueCount = count($mArgs); if ($mValueCount > 0) { - sort($mArgs,SORT_NUMERIC); + sort($mArgs, SORT_NUMERIC); $mValueCount = $mValueCount / 2; if ($mValueCount == floor($mValueCount)) { $returnValue = ($mArgs[$mValueCount--] + $mArgs[$mValueCount]) / 2; @@ -2269,9 +2342,8 @@ class PHPExcel_Calculation_Statistical { } } - // Return return $returnValue; - } // function MEDIAN() + } /** @@ -2288,8 +2360,8 @@ class PHPExcel_Calculation_Statistical { * @param mixed $arg,... Data values * @return float */ - public static function MIN() { - // Return value + public static function MIN() + { $returnValue = null; // Loop through arguments @@ -2303,12 +2375,11 @@ class PHPExcel_Calculation_Statistical { } } - // Return - if(is_null($returnValue)) { + if (is_null($returnValue)) { return 0; } return $returnValue; - } // function MIN() + } /** @@ -2324,8 +2395,8 @@ class PHPExcel_Calculation_Statistical { * @param mixed $arg,... Data values * @return float */ - public static function MINA() { - // Return value + public static function MINA() + { $returnValue = null; // Loop through arguments @@ -2344,12 +2415,11 @@ class PHPExcel_Calculation_Statistical { } } - // Return - if(is_null($returnValue)) { + if (is_null($returnValue)) { return 0; } return $returnValue; - } // function MINA() + } /** @@ -2366,8 +2436,8 @@ class PHPExcel_Calculation_Statistical { * @param string $condition The criteria that defines which cells will be checked. * @return float */ - public static function MINIF($aArgs,$condition,$sumArgs = array()) { - // Return value + public static function MINIF($aArgs, $condition, $sumArgs = array()) + { $returnValue = null; $aArgs = PHPExcel_Calculation_Functions::flattenArray($aArgs); @@ -2378,7 +2448,9 @@ class PHPExcel_Calculation_Statistical { $condition = PHPExcel_Calculation_Functions::_ifCondition($condition); // Loop through arguments foreach ($aArgs as $key => $arg) { - if (!is_numeric($arg)) { $arg = PHPExcel_Calculation::_wrapResult(strtoupper($arg)); } + if (!is_numeric($arg)) { + $arg = PHPExcel_Calculation::_wrapResult(strtoupper($arg)); + } $testCondition = '='.$arg.$condition; if (PHPExcel_Calculation::getInstance()->_calculateFormulaValue($testCondition)) { if ((is_null($returnValue)) || ($arg < $returnValue)) { @@ -2387,33 +2459,35 @@ class PHPExcel_Calculation_Statistical { } } - // Return return $returnValue; - } // function MINIF() + } // // Special variant of array_count_values that isn't limited to strings and integers, // but can work with floating point numbers as values // - private static function _modeCalc($data) { + private static function modeCalc($data) + { $frequencyArray = array(); - foreach($data as $datum) { - $found = False; - foreach($frequencyArray as $key => $value) { + foreach ($data as $datum) { + $found = false; + foreach ($frequencyArray as $key => $value) { if ((string) $value['value'] == (string) $datum) { ++$frequencyArray[$key]['frequency']; - $found = True; + $found = true; break; } } if (!$found) { - $frequencyArray[] = array('value' => $datum, - 'frequency' => 1 ); + $frequencyArray[] = array( + 'value' => $datum, + 'frequency' => 1 + ); } } - foreach($frequencyArray as $key => $value) { + foreach ($frequencyArray as $key => $value) { $frequencyList[$key] = $value['frequency']; $valueList[$key] = $value['value']; } @@ -2423,7 +2497,7 @@ class PHPExcel_Calculation_Statistical { return PHPExcel_Calculation_Functions::NA(); } return $frequencyArray[0]['value']; - } // function _modeCalc() + } /** @@ -2439,8 +2513,8 @@ class PHPExcel_Calculation_Statistical { * @param mixed $arg,... Data values * @return float */ - public static function MODE() { - // Return value + public static function MODE() + { $returnValue = PHPExcel_Calculation_Functions::NA(); // Loop through arguments @@ -2455,12 +2529,11 @@ class PHPExcel_Calculation_Statistical { } if (!empty($mArgs)) { - return self::_modeCalc($mArgs); + return self::modeCalc($mArgs); } - // Return return $returnValue; - } // function MODE() + } /** @@ -2478,16 +2551,16 @@ class PHPExcel_Calculation_Statistical { * @return float * */ - public static function NEGBINOMDIST($failures, $successes, $probability) { - $failures = floor(PHPExcel_Calculation_Functions::flattenSingleValue($failures)); - $successes = floor(PHPExcel_Calculation_Functions::flattenSingleValue($successes)); - $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability); + public static function NEGBINOMDIST($failures, $successes, $probability) + { + $failures = floor(PHPExcel_Calculation_Functions::flattenSingleValue($failures)); + $successes = floor(PHPExcel_Calculation_Functions::flattenSingleValue($successes)); + $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability); if ((is_numeric($failures)) && (is_numeric($successes)) && (is_numeric($probability))) { if (($failures < 0) || ($successes < 1)) { return PHPExcel_Calculation_Functions::NaN(); - } - if (($probability < 0) || ($probability > 1)) { + } elseif (($probability < 0) || ($probability > 1)) { return PHPExcel_Calculation_Functions::NaN(); } if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC) { @@ -2495,10 +2568,10 @@ class PHPExcel_Calculation_Statistical { return PHPExcel_Calculation_Functions::NaN(); } } - return (PHPExcel_Calculation_MathTrig::COMBIN($failures + $successes - 1,$successes - 1)) * (pow($probability,$successes)) * (pow(1 - $probability,$failures)) ; + return (PHPExcel_Calculation_MathTrig::COMBIN($failures + $successes - 1, $successes - 1)) * (pow($probability, $successes)) * (pow(1 - $probability, $failures)); } return PHPExcel_Calculation_Functions::VALUE(); - } // function NEGBINOMDIST() + } /** @@ -2515,10 +2588,11 @@ class PHPExcel_Calculation_Statistical { * @return float * */ - public static function NORMDIST($value, $mean, $stdDev, $cumulative) { - $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); - $mean = PHPExcel_Calculation_Functions::flattenSingleValue($mean); - $stdDev = PHPExcel_Calculation_Functions::flattenSingleValue($stdDev); + public static function NORMDIST($value, $mean, $stdDev, $cumulative) + { + $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); + $mean = PHPExcel_Calculation_Functions::flattenSingleValue($mean); + $stdDev = PHPExcel_Calculation_Functions::flattenSingleValue($stdDev); if ((is_numeric($value)) && (is_numeric($mean)) && (is_numeric($stdDev))) { if ($stdDev < 0) { @@ -2528,12 +2602,12 @@ class PHPExcel_Calculation_Statistical { if ($cumulative) { return 0.5 * (1 + PHPExcel_Calculation_Engineering::erfVal(($value - $mean) / ($stdDev * sqrt(2)))); } else { - return (1 / (SQRT2PI * $stdDev)) * exp(0 - (pow($value - $mean,2) / (2 * ($stdDev * $stdDev)))); + return (1 / (SQRT2PI * $stdDev)) * exp(0 - (pow($value - $mean, 2) / (2 * ($stdDev * $stdDev)))); } } } return PHPExcel_Calculation_Functions::VALUE(); - } // function NORMDIST() + } /** @@ -2547,10 +2621,11 @@ class PHPExcel_Calculation_Statistical { * @return float * */ - public static function NORMINV($probability,$mean,$stdDev) { - $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability); - $mean = PHPExcel_Calculation_Functions::flattenSingleValue($mean); - $stdDev = PHPExcel_Calculation_Functions::flattenSingleValue($stdDev); + public static function NORMINV($probability, $mean, $stdDev) + { + $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability); + $mean = PHPExcel_Calculation_Functions::flattenSingleValue($mean); + $stdDev = PHPExcel_Calculation_Functions::flattenSingleValue($stdDev); if ((is_numeric($probability)) && (is_numeric($mean)) && (is_numeric($stdDev))) { if (($probability < 0) || ($probability > 1)) { @@ -2559,10 +2634,10 @@ class PHPExcel_Calculation_Statistical { if ($stdDev < 0) { return PHPExcel_Calculation_Functions::NaN(); } - return (self::_inverse_ncdf($probability) * $stdDev) + $mean; + return (self::inverseNcdf($probability) * $stdDev) + $mean; } return PHPExcel_Calculation_Functions::VALUE(); - } // function NORMINV() + } /** @@ -2575,11 +2650,12 @@ class PHPExcel_Calculation_Statistical { * @param float $value * @return float */ - public static function NORMSDIST($value) { - $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); + public static function NORMSDIST($value) + { + $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); - return self::NORMDIST($value, 0, 1, True); - } // function NORMSDIST() + return self::NORMDIST($value, 0, 1, true); + } /** @@ -2590,9 +2666,10 @@ class PHPExcel_Calculation_Statistical { * @param float $value * @return float */ - public static function NORMSINV($value) { + public static function NORMSINV($value) + { return self::NORMINV($value, 0, 1); - } // function NORMSINV() + } /** @@ -2609,7 +2686,8 @@ class PHPExcel_Calculation_Statistical { * @param float $entry Percentile value in the range 0..1, inclusive. * @return float */ - public static function PERCENTILE() { + public static function PERCENTILE() + { $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args()); // Calculate @@ -2642,7 +2720,7 @@ class PHPExcel_Calculation_Statistical { } } return PHPExcel_Calculation_Functions::VALUE(); - } // function PERCENTILE() + } /** @@ -2655,17 +2733,18 @@ class PHPExcel_Calculation_Statistical { * @param number The number of significant digits for the returned percentage value. * @return float */ - public static function PERCENTRANK($valueSet,$value,$significance=3) { - $valueSet = PHPExcel_Calculation_Functions::flattenArray($valueSet); + public static function PERCENTRANK($valueSet, $value, $significance = 3) + { + $valueSet = PHPExcel_Calculation_Functions::flattenArray($valueSet); $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); - $significance = (is_null($significance)) ? 3 : (integer) PHPExcel_Calculation_Functions::flattenSingleValue($significance); + $significance = (is_null($significance)) ? 3 : (integer) PHPExcel_Calculation_Functions::flattenSingleValue($significance); - foreach($valueSet as $key => $valueEntry) { + foreach ($valueSet as $key => $valueEntry) { if (!is_numeric($valueEntry)) { unset($valueSet[$key]); } } - sort($valueSet,SORT_NUMERIC); + sort($valueSet, SORT_NUMERIC); $valueCount = count($valueSet); if ($valueCount == 0) { return PHPExcel_Calculation_Functions::NaN(); @@ -2676,8 +2755,8 @@ class PHPExcel_Calculation_Statistical { return PHPExcel_Calculation_Functions::NA(); } - $pos = array_search($value,$valueSet); - if ($pos === False) { + $pos = array_search($value, $valueSet); + if ($pos === false) { $pos = 0; $testValue = $valueSet[0]; while ($testValue < $value) { @@ -2687,8 +2766,8 @@ class PHPExcel_Calculation_Statistical { $pos += (($value - $valueSet[$pos]) / ($testValue - $valueSet[$pos])); } - return round($pos / $valueAdjustor,$significance); - } // function PERCENTRANK() + return round($pos / $valueAdjustor, $significance); + } /** @@ -2704,9 +2783,10 @@ class PHPExcel_Calculation_Statistical { * @param int $numInSet Number of objects in each permutation * @return int Number of permutations */ - public static function PERMUT($numObjs,$numInSet) { - $numObjs = PHPExcel_Calculation_Functions::flattenSingleValue($numObjs); - $numInSet = PHPExcel_Calculation_Functions::flattenSingleValue($numInSet); + public static function PERMUT($numObjs, $numInSet) + { + $numObjs = PHPExcel_Calculation_Functions::flattenSingleValue($numObjs); + $numInSet = PHPExcel_Calculation_Functions::flattenSingleValue($numInSet); if ((is_numeric($numObjs)) && (is_numeric($numInSet))) { $numInSet = floor($numInSet); @@ -2716,7 +2796,7 @@ class PHPExcel_Calculation_Statistical { return round(PHPExcel_Calculation_MathTrig::FACT($numObjs) / PHPExcel_Calculation_MathTrig::FACT($numObjs - $numInSet)); } return PHPExcel_Calculation_Functions::VALUE(); - } // function PERMUT() + } /** @@ -2732,9 +2812,10 @@ class PHPExcel_Calculation_Statistical { * @return float * */ - public static function POISSON($value, $mean, $cumulative) { - $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); - $mean = PHPExcel_Calculation_Functions::flattenSingleValue($mean); + public static function POISSON($value, $mean, $cumulative) + { + $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); + $mean = PHPExcel_Calculation_Functions::flattenSingleValue($mean); if ((is_numeric($value)) && (is_numeric($mean))) { if (($value < 0) || ($mean <= 0)) { @@ -2744,16 +2825,16 @@ class PHPExcel_Calculation_Statistical { if ($cumulative) { $summer = 0; for ($i = 0; $i <= floor($value); ++$i) { - $summer += pow($mean,$i) / PHPExcel_Calculation_MathTrig::FACT($i); + $summer += pow($mean, $i) / PHPExcel_Calculation_MathTrig::FACT($i); } return exp(0-$mean) * $summer; } else { - return (exp(0-$mean) * pow($mean,$value)) / PHPExcel_Calculation_MathTrig::FACT($value); + return (exp(0-$mean) * pow($mean, $value)) / PHPExcel_Calculation_MathTrig::FACT($value); } } } return PHPExcel_Calculation_Functions::VALUE(); - } // function POISSON() + } /** @@ -2770,7 +2851,8 @@ class PHPExcel_Calculation_Statistical { * @param int $entry Quartile value in the range 1..3, inclusive. * @return float */ - public static function QUARTILE() { + public static function QUARTILE() + { $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args()); // Calculate @@ -2781,10 +2863,10 @@ class PHPExcel_Calculation_Statistical { if (($entry < 0) || ($entry > 1)) { return PHPExcel_Calculation_Functions::NaN(); } - return self::PERCENTILE($aArgs,$entry); + return self::PERCENTILE($aArgs, $entry); } return PHPExcel_Calculation_Functions::VALUE(); - } // function QUARTILE() + } /** @@ -2797,29 +2879,30 @@ class PHPExcel_Calculation_Statistical { * @param mixed Order to sort the values in the value set * @return float */ - public static function RANK($value,$valueSet,$order=0) { + public static function RANK($value, $valueSet, $order = 0) + { $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); $valueSet = PHPExcel_Calculation_Functions::flattenArray($valueSet); - $order = (is_null($order)) ? 0 : (integer) PHPExcel_Calculation_Functions::flattenSingleValue($order); + $order = (is_null($order)) ? 0 : (integer) PHPExcel_Calculation_Functions::flattenSingleValue($order); - foreach($valueSet as $key => $valueEntry) { + foreach ($valueSet as $key => $valueEntry) { if (!is_numeric($valueEntry)) { unset($valueSet[$key]); } } if ($order == 0) { - rsort($valueSet,SORT_NUMERIC); + rsort($valueSet, SORT_NUMERIC); } else { - sort($valueSet,SORT_NUMERIC); + sort($valueSet, SORT_NUMERIC); } - $pos = array_search($value,$valueSet); - if ($pos === False) { + $pos = array_search($value, $valueSet); + if ($pos === false) { return PHPExcel_Calculation_Functions::NA(); } return ++$pos; - } // function RANK() + } /** @@ -2831,8 +2914,9 @@ class PHPExcel_Calculation_Statistical { * @param array of mixed Data Series X * @return float */ - public static function RSQ($yValues,$xValues) { - if (!self::_checkTrendArrays($yValues,$xValues)) { + public static function RSQ($yValues, $xValues) + { + if (!self::checkTrendArrays($yValues, $xValues)) { return PHPExcel_Calculation_Functions::VALUE(); } $yValueCount = count($yValues); @@ -2844,9 +2928,9 @@ class PHPExcel_Calculation_Statistical { return PHPExcel_Calculation_Functions::DIV0(); } - $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues); + $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR, $yValues, $xValues); return $bestFitLinear->getGoodnessOfFit(); - } // function RSQ() + } /** @@ -2860,7 +2944,8 @@ class PHPExcel_Calculation_Statistical { * @param array Data Series * @return float */ - public static function SKEW() { + public static function SKEW() + { $aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args()); $mean = self::AVERAGE($aArgs); $stdDev = self::STDEV($aArgs); @@ -2873,18 +2958,17 @@ class PHPExcel_Calculation_Statistical { } else { // Is it a numeric value? if ((is_numeric($arg)) && (!is_string($arg))) { - $summer += pow((($arg - $mean) / $stdDev),3) ; + $summer += pow((($arg - $mean) / $stdDev), 3); ++$count; } } } - // Return if ($count > 2) { return $summer * ($count / (($count-1) * ($count-2))); } return PHPExcel_Calculation_Functions::DIV0(); - } // function SKEW() + } /** @@ -2896,8 +2980,9 @@ class PHPExcel_Calculation_Statistical { * @param array of mixed Data Series X * @return float */ - public static function SLOPE($yValues,$xValues) { - if (!self::_checkTrendArrays($yValues,$xValues)) { + public static function SLOPE($yValues, $xValues) + { + if (!self::checkTrendArrays($yValues, $xValues)) { return PHPExcel_Calculation_Functions::VALUE(); } $yValueCount = count($yValues); @@ -2909,9 +2994,9 @@ class PHPExcel_Calculation_Statistical { return PHPExcel_Calculation_Functions::DIV0(); } - $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues); + $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR, $yValues, $xValues); return $bestFitLinear->getSlope(); - } // function SLOPE() + } /** @@ -2929,7 +3014,8 @@ class PHPExcel_Calculation_Statistical { * @param int $entry Position (ordered from the smallest) in the array or range of data to return * @return float */ - public static function SMALL() { + public static function SMALL() + { $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args()); // Calculate @@ -2952,7 +3038,7 @@ class PHPExcel_Calculation_Statistical { return $mArgs[$entry]; } return PHPExcel_Calculation_Functions::VALUE(); - } // function SMALL() + } /** @@ -2965,10 +3051,11 @@ class PHPExcel_Calculation_Statistical { * @param float $stdDev Standard Deviation * @return float Standardized value */ - public static function STANDARDIZE($value,$mean,$stdDev) { - $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); - $mean = PHPExcel_Calculation_Functions::flattenSingleValue($mean); - $stdDev = PHPExcel_Calculation_Functions::flattenSingleValue($stdDev); + public static function STANDARDIZE($value, $mean, $stdDev) + { + $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); + $mean = PHPExcel_Calculation_Functions::flattenSingleValue($mean); + $stdDev = PHPExcel_Calculation_Functions::flattenSingleValue($stdDev); if ((is_numeric($value)) && (is_numeric($mean)) && (is_numeric($stdDev))) { if ($stdDev <= 0) { @@ -2977,7 +3064,7 @@ class PHPExcel_Calculation_Statistical { return ($value - $mean) / $stdDev ; } return PHPExcel_Calculation_Functions::VALUE(); - } // function STANDARDIZE() + } /** @@ -2994,7 +3081,8 @@ class PHPExcel_Calculation_Statistical { * @param mixed $arg,... Data values * @return float */ - public static function STDEV() { + public static function STDEV() + { $aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args()); // Return value @@ -3011,9 +3099,9 @@ class PHPExcel_Calculation_Statistical { // Is it a numeric value? if ((is_numeric($arg)) && (!is_string($arg))) { if (is_null($returnValue)) { - $returnValue = pow(($arg - $aMean),2); + $returnValue = pow(($arg - $aMean), 2); } else { - $returnValue += pow(($arg - $aMean),2); + $returnValue += pow(($arg - $aMean), 2); } ++$aCount; } @@ -3025,7 +3113,7 @@ class PHPExcel_Calculation_Statistical { } } return PHPExcel_Calculation_Functions::DIV0(); - } // function STDEV() + } /** @@ -3041,10 +3129,10 @@ class PHPExcel_Calculation_Statistical { * @param mixed $arg,... Data values * @return float */ - public static function STDEVA() { + public static function STDEVA() + { $aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args()); - // Return value $returnValue = null; $aMean = self::AVERAGEA($aArgs); @@ -3062,22 +3150,21 @@ class PHPExcel_Calculation_Statistical { $arg = 0; } if (is_null($returnValue)) { - $returnValue = pow(($arg - $aMean),2); + $returnValue = pow(($arg - $aMean), 2); } else { - $returnValue += pow(($arg - $aMean),2); + $returnValue += pow(($arg - $aMean), 2); } ++$aCount; } } } - // Return if (($aCount > 0) && ($returnValue >= 0)) { return sqrt($returnValue / $aCount); } } return PHPExcel_Calculation_Functions::DIV0(); - } // function STDEVA() + } /** @@ -3093,10 +3180,10 @@ class PHPExcel_Calculation_Statistical { * @param mixed $arg,... Data values * @return float */ - public static function STDEVP() { + public static function STDEVP() + { $aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args()); - // Return value $returnValue = null; $aMean = self::AVERAGE($aArgs); @@ -3110,21 +3197,20 @@ class PHPExcel_Calculation_Statistical { // Is it a numeric value? if ((is_numeric($arg)) && (!is_string($arg))) { if (is_null($returnValue)) { - $returnValue = pow(($arg - $aMean),2); + $returnValue = pow(($arg - $aMean), 2); } else { - $returnValue += pow(($arg - $aMean),2); + $returnValue += pow(($arg - $aMean), 2); } ++$aCount; } } - // Return if (($aCount > 0) && ($returnValue >= 0)) { return sqrt($returnValue / $aCount); } } return PHPExcel_Calculation_Functions::DIV0(); - } // function STDEVP() + } /** @@ -3140,10 +3226,10 @@ class PHPExcel_Calculation_Statistical { * @param mixed $arg,... Data values * @return float */ - public static function STDEVPA() { + public static function STDEVPA() + { $aArgs = PHPExcel_Calculation_Functions::flattenArrayIndexed(func_get_args()); - // Return value $returnValue = null; $aMean = self::AVERAGEA($aArgs); @@ -3161,22 +3247,21 @@ class PHPExcel_Calculation_Statistical { $arg = 0; } if (is_null($returnValue)) { - $returnValue = pow(($arg - $aMean),2); + $returnValue = pow(($arg - $aMean), 2); } else { - $returnValue += pow(($arg - $aMean),2); + $returnValue += pow(($arg - $aMean), 2); } ++$aCount; } } } - // Return if (($aCount > 0) && ($returnValue >= 0)) { return sqrt($returnValue / $aCount); } } return PHPExcel_Calculation_Functions::DIV0(); - } // function STDEVPA() + } /** @@ -3188,8 +3273,9 @@ class PHPExcel_Calculation_Statistical { * @param array of mixed Data Series X * @return float */ - public static function STEYX($yValues,$xValues) { - if (!self::_checkTrendArrays($yValues,$xValues)) { + public static function STEYX($yValues, $xValues) + { + if (!self::checkTrendArrays($yValues, $xValues)) { return PHPExcel_Calculation_Functions::VALUE(); } $yValueCount = count($yValues); @@ -3201,9 +3287,9 @@ class PHPExcel_Calculation_Statistical { return PHPExcel_Calculation_Functions::DIV0(); } - $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues); + $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR, $yValues, $xValues); return $bestFitLinear->getStdevOfResiduals(); - } // function STEYX() + } /** @@ -3216,7 +3302,8 @@ class PHPExcel_Calculation_Statistical { * @param float $tails number of tails (1 or 2) * @return float */ - public static function TDIST($value, $degrees, $tails) { + public static function TDIST($value, $degrees, $tails) + { $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); $degrees = floor(PHPExcel_Calculation_Functions::flattenSingleValue($degrees)); $tails = floor(PHPExcel_Calculation_Functions::flattenSingleValue($tails)); @@ -3235,7 +3322,7 @@ class PHPExcel_Calculation_Statistical { // Algorithms", editied by P Griffiths and I D Hill (1985; Ellis // Horwood Ltd.; W. Sussex, England). $tterm = $degrees; - $ttheta = atan2($value,sqrt($tterm)); + $ttheta = atan2($value, sqrt($tterm)); $tc = cos($ttheta); $ts = sin($ttheta); $tsum = 0; @@ -3255,7 +3342,9 @@ class PHPExcel_Calculation_Statistical { $ti += 2; } $tsum *= $ts; - if (($degrees % 2) == 1) { $tsum = M_2DIVPI * ($tsum + $ttheta); } + if (($degrees % 2) == 1) { + $tsum = M_2DIVPI * ($tsum + $ttheta); + } $tValue = 0.5 * (1 + $tsum); if ($tails == 1) { return 1 - abs($tValue); @@ -3264,7 +3353,7 @@ class PHPExcel_Calculation_Statistical { } } return PHPExcel_Calculation_Functions::VALUE(); - } // function TDIST() + } /** @@ -3276,9 +3365,10 @@ class PHPExcel_Calculation_Statistical { * @param float $degrees degrees of freedom * @return float */ - public static function TINV($probability, $degrees) { - $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability); - $degrees = floor(PHPExcel_Calculation_Functions::flattenSingleValue($degrees)); + public static function TINV($probability, $degrees) + { + $probability = PHPExcel_Calculation_Functions::flattenSingleValue($probability); + $degrees = floor(PHPExcel_Calculation_Functions::flattenSingleValue($degrees)); if ((is_numeric($probability)) && (is_numeric($degrees))) { $xLo = 100; @@ -3316,10 +3406,10 @@ class PHPExcel_Calculation_Statistical { if ($i == MAX_ITERATIONS) { return PHPExcel_Calculation_Functions::NA(); } - return round($x,12); + return round($x, 12); } return PHPExcel_Calculation_Functions::VALUE(); - } // function TINV() + } /** @@ -3333,24 +3423,25 @@ class PHPExcel_Calculation_Statistical { * @param boolean A logical value specifying whether to force the intersect to equal 0. * @return array of float */ - public static function TREND($yValues,$xValues=array(),$newValues=array(),$const=True) { + public static function TREND($yValues, $xValues = array(), $newValues = array(), $const = true) + { $yValues = PHPExcel_Calculation_Functions::flattenArray($yValues); $xValues = PHPExcel_Calculation_Functions::flattenArray($xValues); $newValues = PHPExcel_Calculation_Functions::flattenArray($newValues); - $const = (is_null($const)) ? True : (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($const); + $const = (is_null($const)) ? true : (boolean) PHPExcel_Calculation_Functions::flattenSingleValue($const); - $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR,$yValues,$xValues,$const); + $bestFitLinear = trendClass::calculate(trendClass::TREND_LINEAR, $yValues, $xValues, $const); if (empty($newValues)) { $newValues = $bestFitLinear->getXValues(); } $returnArray = array(); - foreach($newValues as $xValue) { + foreach ($newValues as $xValue) { $returnArray[0][] = $bestFitLinear->getValueOfYForX($xValue); } return $returnArray; - } // function TREND() + } /** @@ -3369,7 +3460,8 @@ class PHPExcel_Calculation_Statistical { * @param float $discard Percentage to discard * @return float */ - public static function TRIMMEAN() { + public static function TRIMMEAN() + { $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args()); // Calculate @@ -3395,7 +3487,7 @@ class PHPExcel_Calculation_Statistical { return self::AVERAGE($mArgs); } return PHPExcel_Calculation_Functions::VALUE(); - } // function TRIMMEAN() + } /** @@ -3411,8 +3503,8 @@ class PHPExcel_Calculation_Statistical { * @param mixed $arg,... Data values * @return float */ - public static function VARFunc() { - // Return value + public static function VARFunc() + { $returnValue = PHPExcel_Calculation_Functions::DIV0(); $summerA = $summerB = 0; @@ -3421,7 +3513,9 @@ class PHPExcel_Calculation_Statistical { $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args()); $aCount = 0; foreach ($aArgs as $arg) { - if (is_bool($arg)) { $arg = (integer) $arg; } + if (is_bool($arg)) { + $arg = (integer) $arg; + } // Is it a numeric value? if ((is_numeric($arg)) && (!is_string($arg))) { $summerA += ($arg * $arg); @@ -3430,14 +3524,13 @@ class PHPExcel_Calculation_Statistical { } } - // Return if ($aCount > 1) { $summerA *= $aCount; $summerB *= $summerB; $returnValue = ($summerA - $summerB) / ($aCount * ($aCount - 1)); } return $returnValue; - } // function VARFunc() + } /** @@ -3453,8 +3546,8 @@ class PHPExcel_Calculation_Statistical { * @param mixed $arg,... Data values * @return float */ - public static function VARA() { - // Return value + public static function VARA() + { $returnValue = PHPExcel_Calculation_Functions::DIV0(); $summerA = $summerB = 0; @@ -3483,14 +3576,13 @@ class PHPExcel_Calculation_Statistical { } } - // Return if ($aCount > 1) { $summerA *= $aCount; $summerB *= $summerB; $returnValue = ($summerA - $summerB) / ($aCount * ($aCount - 1)); } return $returnValue; - } // function VARA() + } /** @@ -3506,7 +3598,8 @@ class PHPExcel_Calculation_Statistical { * @param mixed $arg,... Data values * @return float */ - public static function VARP() { + public static function VARP() + { // Return value $returnValue = PHPExcel_Calculation_Functions::DIV0(); @@ -3516,7 +3609,9 @@ class PHPExcel_Calculation_Statistical { $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args()); $aCount = 0; foreach ($aArgs as $arg) { - if (is_bool($arg)) { $arg = (integer) $arg; } + if (is_bool($arg)) { + $arg = (integer) $arg; + } // Is it a numeric value? if ((is_numeric($arg)) && (!is_string($arg))) { $summerA += ($arg * $arg); @@ -3525,14 +3620,13 @@ class PHPExcel_Calculation_Statistical { } } - // Return if ($aCount > 0) { $summerA *= $aCount; $summerB *= $summerB; $returnValue = ($summerA - $summerB) / ($aCount * $aCount); } return $returnValue; - } // function VARP() + } /** @@ -3548,8 +3642,8 @@ class PHPExcel_Calculation_Statistical { * @param mixed $arg,... Data values * @return float */ - public static function VARPA() { - // Return value + public static function VARPA() + { $returnValue = PHPExcel_Calculation_Functions::DIV0(); $summerA = $summerB = 0; @@ -3578,14 +3672,13 @@ class PHPExcel_Calculation_Statistical { } } - // Return if ($aCount > 0) { $summerA *= $aCount; $summerB *= $summerB; $returnValue = ($summerA - $summerB) / ($aCount * $aCount); } return $returnValue; - } // function VARPA() + } /** @@ -3601,10 +3694,11 @@ class PHPExcel_Calculation_Statistical { * @return float * */ - public static function WEIBULL($value, $alpha, $beta, $cumulative) { - $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); - $alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha); - $beta = PHPExcel_Calculation_Functions::flattenSingleValue($beta); + public static function WEIBULL($value, $alpha, $beta, $cumulative) + { + $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); + $alpha = PHPExcel_Calculation_Functions::flattenSingleValue($alpha); + $beta = PHPExcel_Calculation_Functions::flattenSingleValue($beta); if ((is_numeric($value)) && (is_numeric($alpha)) && (is_numeric($beta))) { if (($value < 0) || ($alpha <= 0) || ($beta <= 0)) { @@ -3612,14 +3706,14 @@ class PHPExcel_Calculation_Statistical { } if ((is_numeric($cumulative)) || (is_bool($cumulative))) { if ($cumulative) { - return 1 - exp(0 - pow($value / $beta,$alpha)); + return 1 - exp(0 - pow($value / $beta, $alpha)); } else { - return ($alpha / pow($beta,$alpha)) * pow($value,$alpha - 1) * exp(0 - pow($value / $beta,$alpha)); + return ($alpha / pow($beta, $alpha)) * pow($value, $alpha - 1) * exp(0 - pow($value / $beta, $alpha)); } } } return PHPExcel_Calculation_Functions::VALUE(); - } // function WEIBULL() + } /** @@ -3635,17 +3729,17 @@ class PHPExcel_Calculation_Statistical { * @return float * */ - public static function ZTEST($dataSet, $m0, $sigma = NULL) { - $dataSet = PHPExcel_Calculation_Functions::flattenArrayIndexed($dataSet); - $m0 = PHPExcel_Calculation_Functions::flattenSingleValue($m0); - $sigma = PHPExcel_Calculation_Functions::flattenSingleValue($sigma); + public static function ZTEST($dataSet, $m0, $sigma = null) + { + $dataSet = PHPExcel_Calculation_Functions::flattenArrayIndexed($dataSet); + $m0 = PHPExcel_Calculation_Functions::flattenSingleValue($m0); + $sigma = PHPExcel_Calculation_Functions::flattenSingleValue($sigma); if (is_null($sigma)) { $sigma = self::STDEV($dataSet); } $n = count($dataSet); - return 1 - self::NORMSDIST((self::AVERAGE($dataSet) - $m0)/($sigma/SQRT($n))); - } // function ZTEST() - -} // class PHPExcel_Calculation_Statistical + return 1 - self::NORMSDIST((self::AVERAGE($dataSet) - $m0) / ($sigma / SQRT($n))); + } +} diff --git a/Classes/PHPExcel/Calculation/TextData.php b/Classes/PHPExcel/Calculation/TextData.php index 0a5e9f9f..6461d060 100644 --- a/Classes/PHPExcel/Calculation/TextData.php +++ b/Classes/PHPExcel/Calculation/TextData.php @@ -1,6 +1,16 @@ =0 && ord($c{0}) <= 127) + private static function unicodeToOrd($c) + { + if (ord($c{0}) >=0 && ord($c{0}) <= 127) { return ord($c{0}); - if (ord($c{0}) >= 192 && ord($c{0}) <= 223) + } elseif (ord($c{0}) >= 192 && ord($c{0}) <= 223) { return (ord($c{0})-192)*64 + (ord($c{1})-128); - if (ord($c{0}) >= 224 && ord($c{0}) <= 239) + } elseif (ord($c{0}) >= 224 && ord($c{0}) <= 239) { return (ord($c{0})-224)*4096 + (ord($c{1})-128)*64 + (ord($c{2})-128); - if (ord($c{0}) >= 240 && ord($c{0}) <= 247) + } elseif (ord($c{0}) >= 240 && ord($c{0}) <= 247) { return (ord($c{0})-240)*262144 + (ord($c{1})-128)*4096 + (ord($c{2})-128)*64 + (ord($c{3})-128); - if (ord($c{0}) >= 248 && ord($c{0}) <= 251) + } elseif (ord($c{0}) >= 248 && ord($c{0}) <= 251) { return (ord($c{0})-248)*16777216 + (ord($c{1})-128)*262144 + (ord($c{2})-128)*4096 + (ord($c{3})-128)*64 + (ord($c{4})-128); - if (ord($c{0}) >= 252 && ord($c{0}) <= 253) + } elseif (ord($c{0}) >= 252 && ord($c{0}) <= 253) { return (ord($c{0})-252)*1073741824 + (ord($c{1})-128)*16777216 + (ord($c{2})-128)*262144 + (ord($c{3})-128)*4096 + (ord($c{4})-128)*64 + (ord($c{5})-128); - if (ord($c{0}) >= 254 && ord($c{0}) <= 255) //error + } elseif (ord($c{0}) >= 254 && ord($c{0}) <= 255) { + // error return PHPExcel_Calculation_Functions::VALUE(); + } return 0; - } // function _uniord() + } /** * CHARACTER @@ -71,8 +65,9 @@ class PHPExcel_Calculation_TextData { * @param string $character Value * @return int */ - public static function CHARACTER($character) { - $character = PHPExcel_Calculation_Functions::flattenSingleValue($character); + public static function CHARACTER($character) + { + $character = PHPExcel_Calculation_Functions::flattenSingleValue($character); if ((!is_numeric($character)) || ($character < 0)) { return PHPExcel_Calculation_Functions::VALUE(); @@ -92,22 +87,23 @@ class PHPExcel_Calculation_TextData { * @param mixed $stringValue Value to check * @return string */ - public static function TRIMNONPRINTABLE($stringValue = '') { + public static function TRIMNONPRINTABLE($stringValue = '') + { $stringValue = PHPExcel_Calculation_Functions::flattenSingleValue($stringValue); if (is_bool($stringValue)) { return ($stringValue) ? PHPExcel_Calculation::getTRUE() : PHPExcel_Calculation::getFALSE(); } - if (self::$_invalidChars == Null) { - self::$_invalidChars = range(chr(0),chr(31)); + if (self::$invalidChars == null) { + self::$invalidChars = range(chr(0), chr(31)); } if (is_string($stringValue) || is_numeric($stringValue)) { - return str_replace(self::$_invalidChars, '', trim($stringValue, "\x00..\x1F")); + return str_replace(self::$invalidChars, '', trim($stringValue, "\x00..\x1F")); } - return NULL; - } // function TRIMNONPRINTABLE() + return null; + } /** @@ -116,17 +112,18 @@ class PHPExcel_Calculation_TextData { * @param mixed $stringValue Value to check * @return string */ - public static function TRIMSPACES($stringValue = '') { - $stringValue = PHPExcel_Calculation_Functions::flattenSingleValue($stringValue); + public static function TRIMSPACES($stringValue = '') + { + $stringValue = PHPExcel_Calculation_Functions::flattenSingleValue($stringValue); if (is_bool($stringValue)) { return ($stringValue) ? PHPExcel_Calculation::getTRUE() : PHPExcel_Calculation::getFALSE(); } if (is_string($stringValue) || is_numeric($stringValue)) { - return trim(preg_replace('/ +/',' ',trim($stringValue, ' ')), ' '); + return trim(preg_replace('/ +/', ' ', trim($stringValue, ' ')), ' '); } - return NULL; - } // function TRIMSPACES() + return null; + } /** @@ -135,9 +132,11 @@ class PHPExcel_Calculation_TextData { * @param string $characters Value * @return int */ - public static function ASCIICODE($characters) { - if (($characters === NULL) || ($characters === '')) + public static function ASCIICODE($characters) + { + if (($characters === null) || ($characters === '')) { return PHPExcel_Calculation_Functions::VALUE(); + } $characters = PHPExcel_Calculation_Functions::flattenSingleValue($characters); if (is_bool($characters)) { if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE) { @@ -149,13 +148,17 @@ class PHPExcel_Calculation_TextData { $character = $characters; if ((function_exists('mb_strlen')) && (function_exists('mb_substr'))) { - if (mb_strlen($characters, 'UTF-8') > 1) { $character = mb_substr($characters, 0, 1, 'UTF-8'); } - return self::_uniord($character); + if (mb_strlen($characters, 'UTF-8') > 1) { + $character = mb_substr($characters, 0, 1, 'UTF-8'); + } + return self::unicodeToOrd($character); } else { - if (strlen($characters) > 0) { $character = substr($characters, 0, 1); } + if (strlen($characters) > 0) { + $character = substr($characters, 0, 1); + } return ord($character); } - } // function ASCIICODE() + } /** @@ -163,8 +166,8 @@ class PHPExcel_Calculation_TextData { * * @return string */ - public static function CONCATENATE() { - // Return value + public static function CONCATENATE() + { $returnValue = ''; // Loop through arguments @@ -180,9 +183,8 @@ class PHPExcel_Calculation_TextData { $returnValue .= $arg; } - // Return return $returnValue; - } // function CONCATENATE() + } /** @@ -197,7 +199,8 @@ class PHPExcel_Calculation_TextData { * If you omit decimals, it is assumed to be 2 * @return string */ - public static function DOLLAR($value = 0, $decimals = 2) { + public static function DOLLAR($value = 0, $decimals = 2) + { $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); $decimals = is_null($decimals) ? 0 : PHPExcel_Calculation_Functions::flattenSingleValue($decimals); @@ -209,16 +212,18 @@ class PHPExcel_Calculation_TextData { $mask = '$#,##0'; if ($decimals > 0) { - $mask .= '.' . str_repeat('0',$decimals); + $mask .= '.' . str_repeat('0', $decimals); } else { - $round = pow(10,abs($decimals)); - if ($value < 0) { $round = 0-$round; } + $round = pow(10, abs($decimals)); + if ($value < 0) { + $round = 0-$round; + } $value = PHPExcel_Calculation_MathTrig::MROUND($value, $round); } return PHPExcel_Style_NumberFormat::toFormattedString($value, $mask); - } // function DOLLAR() + } /** @@ -229,10 +234,11 @@ class PHPExcel_Calculation_TextData { * @param int $offset Offset within $haystack * @return string */ - public static function SEARCHSENSITIVE($needle,$haystack,$offset=1) { - $needle = PHPExcel_Calculation_Functions::flattenSingleValue($needle); - $haystack = PHPExcel_Calculation_Functions::flattenSingleValue($haystack); - $offset = PHPExcel_Calculation_Functions::flattenSingleValue($offset); + public static function SEARCHSENSITIVE($needle, $haystack, $offset = 1) + { + $needle = PHPExcel_Calculation_Functions::flattenSingleValue($needle); + $haystack = PHPExcel_Calculation_Functions::flattenSingleValue($haystack); + $offset = PHPExcel_Calculation_Functions::flattenSingleValue($offset); if (!is_bool($needle)) { if (is_bool($haystack)) { @@ -254,7 +260,7 @@ class PHPExcel_Calculation_TextData { } } return PHPExcel_Calculation_Functions::VALUE(); - } // function SEARCHSENSITIVE() + } /** @@ -265,10 +271,11 @@ class PHPExcel_Calculation_TextData { * @param int $offset Offset within $haystack * @return string */ - public static function SEARCHINSENSITIVE($needle,$haystack,$offset=1) { - $needle = PHPExcel_Calculation_Functions::flattenSingleValue($needle); - $haystack = PHPExcel_Calculation_Functions::flattenSingleValue($haystack); - $offset = PHPExcel_Calculation_Functions::flattenSingleValue($offset); + public static function SEARCHINSENSITIVE($needle, $haystack, $offset = 1) + { + $needle = PHPExcel_Calculation_Functions::flattenSingleValue($needle); + $haystack = PHPExcel_Calculation_Functions::flattenSingleValue($haystack); + $offset = PHPExcel_Calculation_Functions::flattenSingleValue($offset); if (!is_bool($needle)) { if (is_bool($haystack)) { @@ -280,7 +287,7 @@ class PHPExcel_Calculation_TextData { return $offset; } if (function_exists('mb_stripos')) { - $pos = mb_stripos($haystack, $needle, --$offset,'UTF-8'); + $pos = mb_stripos($haystack, $needle, --$offset, 'UTF-8'); } else { $pos = stripos($haystack, $needle, --$offset); } @@ -290,7 +297,7 @@ class PHPExcel_Calculation_TextData { } } return PHPExcel_Calculation_Functions::VALUE(); - } // function SEARCHINSENSITIVE() + } /** @@ -301,10 +308,11 @@ class PHPExcel_Calculation_TextData { * @param boolean $no_commas * @return boolean */ - public static function FIXEDFORMAT($value, $decimals = 2, $no_commas = FALSE) { - $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); - $decimals = PHPExcel_Calculation_Functions::flattenSingleValue($decimals); - $no_commas = PHPExcel_Calculation_Functions::flattenSingleValue($no_commas); + public static function FIXEDFORMAT($value, $decimals = 2, $no_commas = false) + { + $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); + $decimals = PHPExcel_Calculation_Functions::flattenSingleValue($decimals); + $no_commas = PHPExcel_Calculation_Functions::flattenSingleValue($no_commas); // Validate parameters if (!is_numeric($value) || !is_numeric($decimals)) { @@ -312,14 +320,16 @@ class PHPExcel_Calculation_TextData { } $decimals = floor($decimals); - $valueResult = round($value,$decimals); - if ($decimals < 0) { $decimals = 0; } + $valueResult = round($value, $decimals); + if ($decimals < 0) { + $decimals = 0; + } if (!$no_commas) { - $valueResult = number_format($valueResult,$decimals); + $valueResult = number_format($valueResult, $decimals); } return (string) $valueResult; - } // function FIXEDFORMAT() + } /** @@ -329,9 +339,10 @@ class PHPExcel_Calculation_TextData { * @param int $chars Number of characters * @return string */ - public static function LEFT($value = '', $chars = 1) { - $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); - $chars = PHPExcel_Calculation_Functions::flattenSingleValue($chars); + public static function LEFT($value = '', $chars = 1) + { + $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); + $chars = PHPExcel_Calculation_Functions::flattenSingleValue($chars); if ($chars < 0) { return PHPExcel_Calculation_Functions::VALUE(); @@ -346,7 +357,7 @@ class PHPExcel_Calculation_TextData { } else { return substr($value, 0, $chars); } - } // function LEFT() + } /** @@ -357,10 +368,11 @@ class PHPExcel_Calculation_TextData { * @param int $chars Number of characters * @return string */ - public static function MID($value = '', $start = 1, $chars = null) { - $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); - $start = PHPExcel_Calculation_Functions::flattenSingleValue($start); - $chars = PHPExcel_Calculation_Functions::flattenSingleValue($chars); + public static function MID($value = '', $start = 1, $chars = null) + { + $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); + $start = PHPExcel_Calculation_Functions::flattenSingleValue($start); + $chars = PHPExcel_Calculation_Functions::flattenSingleValue($chars); if (($start < 1) || ($chars < 0)) { return PHPExcel_Calculation_Functions::VALUE(); @@ -375,7 +387,7 @@ class PHPExcel_Calculation_TextData { } else { return substr($value, --$start, $chars); } - } // function MID() + } /** @@ -385,9 +397,10 @@ class PHPExcel_Calculation_TextData { * @param int $chars Number of characters * @return string */ - public static function RIGHT($value = '', $chars = 1) { - $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); - $chars = PHPExcel_Calculation_Functions::flattenSingleValue($chars); + public static function RIGHT($value = '', $chars = 1) + { + $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); + $chars = PHPExcel_Calculation_Functions::flattenSingleValue($chars); if ($chars < 0) { return PHPExcel_Calculation_Functions::VALUE(); @@ -402,7 +415,7 @@ class PHPExcel_Calculation_TextData { } else { return substr($value, strlen($value) - $chars); } - } // function RIGHT() + } /** @@ -411,8 +424,9 @@ class PHPExcel_Calculation_TextData { * @param string $value Value * @return string */ - public static function STRINGLENGTH($value = '') { - $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); + public static function STRINGLENGTH($value = '') + { + $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); if (is_bool($value)) { $value = ($value) ? PHPExcel_Calculation::getTRUE() : PHPExcel_Calculation::getFALSE(); @@ -423,7 +437,7 @@ class PHPExcel_Calculation_TextData { } else { return strlen($value); } - } // function STRINGLENGTH() + } /** @@ -434,15 +448,16 @@ class PHPExcel_Calculation_TextData { * @param string $mixedCaseString * @return string */ - public static function LOWERCASE($mixedCaseString) { - $mixedCaseString = PHPExcel_Calculation_Functions::flattenSingleValue($mixedCaseString); + public static function LOWERCASE($mixedCaseString) + { + $mixedCaseString = PHPExcel_Calculation_Functions::flattenSingleValue($mixedCaseString); if (is_bool($mixedCaseString)) { $mixedCaseString = ($mixedCaseString) ? PHPExcel_Calculation::getTRUE() : PHPExcel_Calculation::getFALSE(); } return PHPExcel_Shared_String::StrToLower($mixedCaseString); - } // function LOWERCASE() + } /** @@ -453,15 +468,16 @@ class PHPExcel_Calculation_TextData { * @param string $mixedCaseString * @return string */ - public static function UPPERCASE($mixedCaseString) { - $mixedCaseString = PHPExcel_Calculation_Functions::flattenSingleValue($mixedCaseString); + public static function UPPERCASE($mixedCaseString) + { + $mixedCaseString = PHPExcel_Calculation_Functions::flattenSingleValue($mixedCaseString); if (is_bool($mixedCaseString)) { $mixedCaseString = ($mixedCaseString) ? PHPExcel_Calculation::getTRUE() : PHPExcel_Calculation::getFALSE(); } return PHPExcel_Shared_String::StrToUpper($mixedCaseString); - } // function UPPERCASE() + } /** @@ -472,15 +488,16 @@ class PHPExcel_Calculation_TextData { * @param string $mixedCaseString * @return string */ - public static function PROPERCASE($mixedCaseString) { - $mixedCaseString = PHPExcel_Calculation_Functions::flattenSingleValue($mixedCaseString); + public static function PROPERCASE($mixedCaseString) + { + $mixedCaseString = PHPExcel_Calculation_Functions::flattenSingleValue($mixedCaseString); if (is_bool($mixedCaseString)) { $mixedCaseString = ($mixedCaseString) ? PHPExcel_Calculation::getTRUE() : PHPExcel_Calculation::getFALSE(); } return PHPExcel_Shared_String::StrToTitle($mixedCaseString); - } // function PROPERCASE() + } /** @@ -489,20 +506,21 @@ class PHPExcel_Calculation_TextData { * @param string $oldText String to modify * @param int $start Start character * @param int $chars Number of characters - * @param string $newText String to replace in defined position + * @param string $newText String to replace in defined position * @return string */ - public static function REPLACE($oldText = '', $start = 1, $chars = null, $newText) { - $oldText = PHPExcel_Calculation_Functions::flattenSingleValue($oldText); - $start = PHPExcel_Calculation_Functions::flattenSingleValue($start); - $chars = PHPExcel_Calculation_Functions::flattenSingleValue($chars); - $newText = PHPExcel_Calculation_Functions::flattenSingleValue($newText); + public static function REPLACE($oldText = '', $start = 1, $chars = null, $newText) + { + $oldText = PHPExcel_Calculation_Functions::flattenSingleValue($oldText); + $start = PHPExcel_Calculation_Functions::flattenSingleValue($start); + $chars = PHPExcel_Calculation_Functions::flattenSingleValue($chars); + $newText = PHPExcel_Calculation_Functions::flattenSingleValue($newText); - $left = self::LEFT($oldText,$start-1); - $right = self::RIGHT($oldText,self::STRINGLENGTH($oldText)-($start+$chars)+1); + $left = self::LEFT($oldText, $start-1); + $right = self::RIGHT($oldText, self::STRINGLENGTH($oldText)-($start+$chars)+1); return $left.$newText.$right; - } // function REPLACE() + } /** @@ -514,21 +532,22 @@ class PHPExcel_Calculation_TextData { * @param integer $instance Instance Number * @return string */ - public static function SUBSTITUTE($text = '', $fromText = '', $toText = '', $instance = 0) { - $text = PHPExcel_Calculation_Functions::flattenSingleValue($text); - $fromText = PHPExcel_Calculation_Functions::flattenSingleValue($fromText); - $toText = PHPExcel_Calculation_Functions::flattenSingleValue($toText); - $instance = floor(PHPExcel_Calculation_Functions::flattenSingleValue($instance)); + public static function SUBSTITUTE($text = '', $fromText = '', $toText = '', $instance = 0) + { + $text = PHPExcel_Calculation_Functions::flattenSingleValue($text); + $fromText = PHPExcel_Calculation_Functions::flattenSingleValue($fromText); + $toText = PHPExcel_Calculation_Functions::flattenSingleValue($toText); + $instance = floor(PHPExcel_Calculation_Functions::flattenSingleValue($instance)); if ($instance == 0) { - if(function_exists('mb_str_replace')) { - return mb_str_replace($fromText,$toText,$text); + if (function_exists('mb_str_replace')) { + return mb_str_replace($fromText, $toText, $text); } else { - return str_replace($fromText,$toText,$text); + return str_replace($fromText, $toText, $text); } } else { $pos = -1; - while($instance > 0) { + while ($instance > 0) { if (function_exists('mb_strpos')) { $pos = mb_strpos($text, $fromText, $pos+1, 'UTF-8'); } else { @@ -541,15 +560,15 @@ class PHPExcel_Calculation_TextData { } if ($pos !== false) { if (function_exists('mb_strlen')) { - return self::REPLACE($text,++$pos,mb_strlen($fromText, 'UTF-8'),$toText); + return self::REPLACE($text, ++$pos, mb_strlen($fromText, 'UTF-8'), $toText); } else { - return self::REPLACE($text,++$pos,strlen($fromText),$toText); + return self::REPLACE($text, ++$pos, strlen($fromText), $toText); } } } return $text; - } // function SUBSTITUTE() + } /** @@ -558,14 +577,15 @@ class PHPExcel_Calculation_TextData { * @param mixed $testValue Value to check * @return boolean */ - public static function RETURNSTRING($testValue = '') { - $testValue = PHPExcel_Calculation_Functions::flattenSingleValue($testValue); + public static function RETURNSTRING($testValue = '') + { + $testValue = PHPExcel_Calculation_Functions::flattenSingleValue($testValue); if (is_string($testValue)) { return $testValue; } - return Null; - } // function RETURNSTRING() + return null; + } /** @@ -575,16 +595,17 @@ class PHPExcel_Calculation_TextData { * @param string $format Format mask to use * @return boolean */ - public static function TEXTFORMAT($value,$format) { - $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); - $format = PHPExcel_Calculation_Functions::flattenSingleValue($format); + public static function TEXTFORMAT($value, $format) + { + $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); + $format = PHPExcel_Calculation_Functions::flattenSingleValue($format); if ((is_string($value)) && (!is_numeric($value)) && PHPExcel_Shared_Date::isDateTimeFormatCode($format)) { $value = PHPExcel_Calculation_DateTime::DATEVALUE($value); } - return (string) PHPExcel_Style_NumberFormat::toFormattedString($value,$format); - } // function TEXTFORMAT() + return (string) PHPExcel_Style_NumberFormat::toFormattedString($value, $format); + } /** * VALUE @@ -592,13 +613,14 @@ class PHPExcel_Calculation_TextData { * @param mixed $value Value to check * @return boolean */ - public static function VALUE($value = '') { - $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); + public static function VALUE($value = '') + { + $value = PHPExcel_Calculation_Functions::flattenSingleValue($value); if (!is_numeric($value)) { $numberValue = str_replace( - PHPExcel_Shared_String::getThousandsSeparator(), - '', + PHPExcel_Shared_String::getThousandsSeparator(), + '', trim($value, " \t\n\r\0\x0B" . PHPExcel_Shared_String::getCurrencyCode()) ); if (is_numeric($numberValue)) { @@ -626,5 +648,4 @@ class PHPExcel_Calculation_TextData { } return (float) $value; } - } diff --git a/Classes/PHPExcel/Calculation/Token/Stack.php b/Classes/PHPExcel/Calculation/Token/Stack.php index 7d561d4d..13f446ab 100644 --- a/Classes/PHPExcel/Calculation/Token/Stack.php +++ b/Classes/PHPExcel/Calculation/Token/Stack.php @@ -32,14 +32,14 @@ class PHPExcel_Calculation_Token_Stack * * @var mixed[] */ - private $_stack = array(); + private $stack = array(); /** * Count of entries in the parser stack * * @var integer */ - private $_count = 0; + private $count = 0; /** * Return the number of entries on the stack @@ -48,7 +48,7 @@ class PHPExcel_Calculation_Token_Stack */ public function count() { - return $this->_count; + return $this->count; } /** @@ -60,15 +60,15 @@ class PHPExcel_Calculation_Token_Stack */ public function push($type, $value, $reference = null) { - $this->_stack[$this->_count++] = array( - 'type' => $type, - 'value' => $value, - 'reference' => $reference + $this->stack[$this->count++] = array( + 'type' => $type, + 'value' => $value, + 'reference' => $reference ); if ($type == 'Function') { $localeFunction = PHPExcel_Calculation::_localeFunc($value); if ($localeFunction != $value) { - $this->_stack[($this->_count - 1)]['localeValue'] = $localeFunction; + $this->stack[($this->count - 1)]['localeValue'] = $localeFunction; } } } @@ -80,8 +80,8 @@ class PHPExcel_Calculation_Token_Stack */ public function pop() { - if ($this->_count > 0) { - return $this->_stack[--$this->_count]; + if ($this->count > 0) { + return $this->stack[--$this->count]; } return null; } @@ -94,18 +94,18 @@ class PHPExcel_Calculation_Token_Stack */ public function last($n = 1) { - if ($this->_count - $n < 0) { + if ($this->count - $n < 0) { return null; } - return $this->_stack[$this->_count - $n]; + return $this->stack[$this->count - $n]; } /** * Clear the stack */ - function clear() + public function clear() { - $this->_stack = array(); - $this->_count = 0; + $this->stack = array(); + $this->count = 0; } }