/** * Combines all elements of an NumArray at a given axis with the `$callback` * function recursive * * @param mixed $data given data * @param callback $callback callback fucntion * @param int $axis given axis * * @return mixed * * @since 1.0.0 */ protected static function reduceRecursive($data, $callback, $axis = null) { if (is_array($data)) { if (!is_null($axis) && $axis > 0) { foreach ($data as $key => $value) { $data[$key] = self::reduceRecursive($value, $callback, $axis - 1); } return $data; } $sum = array_shift($data); foreach ($data as $value) { $sum = Map::mapArray($sum, $value, $callback); } if ($axis === 0) { return $sum; } return self::reduceRecursive($sum, $callback); } return $data; }
/** * Divides the NumArray with a divisor * * @param mixed $divisor divisor * * @return $this * * @throws DivideByZeroException will be thrown, when dividing by zero * * @api * @since 1.0.4 */ public function div($divisor) { if ($divisor instanceof NumArray) { $divisor = $divisor->getData(); } $this->data = Map::mapArray($this->data, $divisor, function ($data1, $data2) { if ($data2) { return $data1 / $data2; } throw new DivideByZeroException("Dividing by zero is forbidden"); }); $this->shape = Shape::getShape($this->data); $this->flushCache(); return $this; }