/** * Convert if possible a supplied argument to a strong numeric type * * @param int|float|string|NumericTypeInterface $numerator * @return Chippyash\Type\Interfaces\NumericTypeInterface * @throws UndefinedComputationException */ protected function convertNumberToNumeric($value) { switch (gettype($value)) { case 'integer': return TypeFactory::createInt($value); case 'double': return TypeFactory::createRational($value); case 'string': try { if (is_numeric($value)) { $value = floatval($value); } return TypeFactory::createRational($value); } catch (\Exception $e) { try { return TypeFactory::createComplex($value); } catch (\Exception $ex) { throw new MatrixException("The string representation of the number ('{$value}') is invalid for a complex"); } } case 'object': if ($value instanceof NumericTypeInterface) { return $value; } else { throw new MatrixException('NumberToNumeric expects int, float, string or Rational value'); } case 'NULL': return TypeFactory::createInt(0); case 'boolean': return TypeFactory::createInt($value ? 1 : 0); default: throw new MatrixException('NumberToNumeric expects int, float, string or Rational '); } }
public function setUp() { RequiredType::getInstance()->set(RequiredType::TYPE_NATIVE); $this->object = new AsciiNumeric(); $this->rationalOne = TypeFactory::createRational(TypeFactory::createInt(1), TypeFactory::createInt(1)); $this->rationalHalf = TypeFactory::createRational(TypeFactory::createInt(1), TypeFactory::createInt(2)); $this->complexTwo = TypeFactory::createComplex(TypeFactory::createRational(TypeFactory::createInt(2), TypeFactory::createInt(1)), TypeFactory::createRational(TypeFactory::createInt(0), TypeFactory::createInt(1))); $this->complexThree = TypeFactory::createComplex(TypeFactory::createRational(TypeFactory::createInt(3), TypeFactory::createInt(1)), TypeFactory::createRational(TypeFactory::createInt(-3), TypeFactory::createInt(2))); }
public function validNumerics() { return [[2], [2.5], ['12.45'], ['2/5'], [true], [false], ['2+3i'], [TypeFactory::createComplex(2)], [TypeFactory::createInt(2)], [TypeFactory::createFloat(2)], [TypeFactory::createRational(2)], [null]]; }
public function testCreateComplexReturnsComplexType() { $this->assertInstanceOf('\\Chippyash\\Type\\Number\\Complex\\ComplexType', Typefactory::createComplex(54, 2)); $this->assertEquals('54+2i', Typefactory::createComplex(54, 2)->get()); $this->assertInstanceOf('\\Chippyash\\Type\\Number\\Complex\\ComplexType', Typefactory::createComplex(54)); //real number 54+0i $this->assertEquals(54, Typefactory::createComplex(54)->get()); $this->assertInstanceOf('\\Chippyash\\Type\\Number\\Complex\\ComplexType', Typefactory::createComplex('54+2i')); $this->assertEquals('54+2i', Typefactory::createComplex('54+2i')->get()); $complexThree = TypeFactory::createComplex(TypeFactory::createRational(TypeFactory::createInt(3), TypeFactory::createInt(1)), TypeFactory::createRational(TypeFactory::createInt(-3), TypeFactory::createInt(2))); $this->assertEquals('3-3/2i', $complexThree->get()); }
public function invalidScalarCombinations() { return [[new RationalMatrix([2]), '1+0i', 'Rational\\RationalType'], [new RationalMatrix([2]), TypeFactory::createComplex(2, 1), 'Rational\\RationalType']]; }