/** * Create a new scalar based on type of original matrix * * @param \Chippyash\Math\Matrix\NumericMatrix $originalMatrix * @param scalar $scalar * @return Chippyash\Type\Interfaces\NumericTypeInterface * */ protected function createCorrectScalarType(NumericMatrix $originalMatrix, $scalar) { if ($scalar instanceof NumericTypeInterface) { if ($originalMatrix instanceof RationalMatrix) { return $scalar->asRational(); } if ($originalMatrix instanceof ComplexMatrix) { return $scalar->asComplex(); } return $scalar; } if ($originalMatrix instanceof ComplexMatrix) { if (is_numeric($scalar)) { return ComplexTypeFactory::create($scalar, 0); } if (is_string($scalar)) { try { return RationalTypeFactory::create($scalar)->asComplex(); } catch (\Exception $e) { //do nothing } } if (is_bool($scalar)) { return ComplexTypeFactory::create($scalar ? 1 : 0, 0); } return ComplexTypeFactory::create($scalar); } if ($originalMatrix instanceof RationalMatrix) { if (is_bool($scalar)) { $scalar = $scalar ? 1 : 0; } return RationalTypeFactory::create($scalar); } //handling for NumericMatrix if (is_int($scalar)) { return TypeFactory::createInt($scalar); } elseif (is_float($scalar)) { return TypeFactory::createRational($scalar); } elseif (is_bool($scalar)) { return TypeFactory::createInt($scalar ? 1 : 0); } elseif (is_string($scalar)) { try { return TypeFactory::createRational($scalar); } catch (\InvalidArgumentException $e) { try { return ComplexTypeFactory::create($scalar); } catch (\InvalidArgumentException $e) { //do nothing } } } throw new ComputationException('Scalar parameter is not a supported type for numeric matrices: ' . getType($scalar)); }
/** * Writes input to stdin. * * @throws InvalidArgumentException When an input iterator yields a non supported value */ protected function write() { if (!isset($this->pipes[0])) { return; } $input = $this->input; if ($input instanceof \Iterator) { if (!$input->valid()) { $input = null; } elseif (is_resource($input = $input->current())) { stream_set_blocking($input, 0); } elseif (!isset($this->inputBuffer[0])) { if (!is_string($input)) { if (!is_scalar($input)) { throw new InvalidArgumentException(sprintf('%s yielded a value of type "%s", but only scalars and stream resources are supported', get_class($this->input), gettype($input))); } $input = (string) $input; } $this->inputBuffer = $input; $this->input->next(); $input = null; } else { $input = null; } } $r = $e = array(); $w = array($this->pipes[0]); // let's have a look if something changed in streams if (false === ($n = @stream_select($r, $w, $e, 0, 0))) { return; } foreach ($w as $stdin) { if (isset($this->inputBuffer[0])) { $written = fwrite($stdin, $this->inputBuffer); $this->inputBuffer = substr($this->inputBuffer, $written); if (isset($this->inputBuffer[0])) { return array($this->pipes[0]); } } if ($input) { for (;;) { $data = fread($input, self::CHUNK_SIZE); if (!isset($data[0])) { break; } $written = fwrite($stdin, $data); $data = substr($data, $written); if (isset($data[0])) { $this->inputBuffer = $data; return array($this->pipes[0]); } } if (feof($input)) { if ($this->input instanceof \Iterator) { $this->input->next(); } else { $this->input = null; } } } } // no input to read on resource, buffer is empty if (!isset($this->inputBuffer[0]) && !($this->input instanceof \Iterator ? $this->input->valid() : $this->input)) { $this->input = null; fclose($this->pipes[0]); unset($this->pipes[0]); } elseif (!$w) { return array($this->pipes[0]); } }