Exemplo n.º 1
0
 /**
  *	power
  *
  *	A = A ^ B
  *	@param mixed $B Matrix/Array
  *	@return Matrix Sum
  */
 public function power()
 {
     if (func_num_args() > 0) {
         $args = func_get_args();
         $match = implode(",", array_map('gettype', $args));
         switch ($match) {
             case 'object':
                 if ($args[0] instanceof Matrix) {
                     $M = $args[0];
                 } else {
                     throw new Exception(JAMAError(ArgumentTypeException));
                 }
                 break;
             case 'array':
                 $M = new Matrix($args[0]);
                 break;
             default:
                 throw new Exception(JAMAError(PolymorphicArgumentException));
                 break;
         }
         $this->checkMatrixDimensions($M);
         for ($i = 0; $i < $this->m; ++$i) {
             for ($j = 0; $j < $this->n; ++$j) {
                 $validValues = True;
                 $value = $M->get($i, $j);
                 if (is_string($this->A[$i][$j]) && strlen($this->A[$i][$j]) > 0 && !is_numeric($this->A[$i][$j])) {
                     $this->A[$i][$j] = trim($this->A[$i][$j], '"');
                     $validValues &= Shared_String::convertToNumberIfFraction($this->A[$i][$j]);
                 }
                 if (is_string($value) && strlen($value) > 0 && !is_numeric($value)) {
                     $value = trim($value, '"');
                     $validValues &= Shared_String::convertToNumberIfFraction($value);
                 }
                 if ($validValues) {
                     $this->A[$i][$j] = pow($this->A[$i][$j], $value);
                 } else {
                     $this->A[$i][$j] = Calculation_Functions::NaN();
                 }
             }
         }
         return $this;
     } else {
         throw new Exception(JAMAError(PolymorphicArgumentException));
     }
 }
Exemplo n.º 2
0
 /**
  *	Calculate the value of a cell formula
  *
  *	@access	public
  *	@param	Cell	$pCell		Cell to calculate
  *	@param	Boolean			$resetLog	Flag indicating whether the debug log should be reset or not
  *	@return	mixed
  *	@throws	Exception
  */
 public function calculateCellValue(Cell $pCell = null, $resetLog = true)
 {
     if ($resetLog) {
         //	Initialise the logging settings if requested
         $this->formulaError = null;
         $this->debugLog = $this->debugLogStack = array();
         $this->_cyclicFormulaCount = 1;
         $returnArrayAsType = self::$returnArrayAsType;
         self::$returnArrayAsType = self::RETURN_ARRAY_AS_ARRAY;
     }
     //	Read the formula from the cell
     if (is_null($pCell)) {
         return null;
     }
     if ($resetLog) {
         self::$returnArrayAsType = $returnArrayAsType;
     }
     //	Execute the calculation for the cell formula
     try {
         $result = self::_unwrapResult($this->_calculateFormulaValue($pCell->getValue(), $pCell->getCoordinate(), $pCell));
     } catch (Exception $e) {
         throw new Exception($e->getMessage());
     }
     if (is_array($result) && self::$returnArrayAsType != self::RETURN_ARRAY_AS_ARRAY) {
         $testResult = Calculation_Functions::flattenArray($result);
         if (self::$returnArrayAsType == self::RETURN_ARRAY_AS_ERROR) {
             return Calculation_Functions::VALUE();
         }
         //	If there's only a single cell in the array, then we allow it
         if (count($testResult) != 1) {
             //	If keys are numeric, then it's a matrix result rather than a cell range result, so we permit it
             $r = array_keys($result);
             $r = array_shift($r);
             if (!is_numeric($r)) {
                 return Calculation_Functions::VALUE();
             }
             if (is_array($result[$r])) {
                 $c = array_keys($result[$r]);
                 $c = array_shift($c);
                 if (!is_numeric($c)) {
                     return Calculation_Functions::VALUE();
                 }
             }
         }
         $result = array_shift($testResult);
     }
     if (is_null($result)) {
         return 0;
     } elseif (is_float($result) && (is_nan($result) || is_infinite($result))) {
         return Calculation_Functions::NaN();
     }
     return $result;
 }