/** * 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 testCreateRationalIdentityReturnsRationalMatrix() { $rA = IdentityMatrix::rationalIdentity(TypeFactory::createInt(2)); $this->assertInstanceOf('Chippyash\\Math\\Matrix\\RationalMatrix', $rA); $one = TypeFactory::createRational(TypeFactory::createInt(1), TypeFactory::createInt(1)); $zero = TypeFactory::createRational(TypeFactory::createInt(0), TypeFactory::createInt(1)); $this->assertEquals([[$one, $zero], [$zero, $one]], $rA->toArray()); }
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))); }
/** * 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)); }
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()); }
/** * * @return array [[testArray, expectedArray], ...] */ public function differentNumberTypeArrays() { return [[[1], [[TypeFactory::createInt(1)]]], [[2.5], [[TypeFactory::createRational(TypeFactory::createInt(5), TypeFactory::createInt(2))]]], [[TypeFactory::createInt(1)], [[TypeFactory::createInt(1)]]], [[new FloatType(2.3)], [[new FloatType(2.3)]]], [[new WholeIntType(1)], [[new WholeIntType(1)]]], [[new NaturalIntType(1)], [[new NaturalIntType(1)]]], [[TypeFactory::createRational(TypeFactory::createInt(5), TypeFactory::createInt(2))], [[TypeFactory::createRational(TypeFactory::createInt(5), TypeFactory::createInt(2))]]]]; }
public function validScalarCombinations() { return [[new NumericMatrix([2]), 2, 'IntType'], [new NumericMatrix([2]), true, 'IntType'], [new NumericMatrix([2]), false, 'IntType'], [new NumericMatrix([2]), 2.5, 'Rational\\RationalType'], [new NumericMatrix([2]), '2/5', 'Rational\\RationalType'], [new NumericMatrix([2]), '1+2i', 'Complex\\ComplexType'], [new NumericMatrix([2]), TypeFactory::createComplex(2), 'Complex\\ComplexType'], [new NumericMatrix([2]), TypeFactory::createInt(2), 'IntType'], [new NumericMatrix([2]), TypeFactory::createFloat(2), 'FloatType'], [new NumericMatrix([2]), TypeFactory::createRational(2), 'Rational\\RationalType'], [new RationalMatrix([2]), 2, 'Rational\\RationalType'], [new RationalMatrix([2]), 2.5, 'Rational\\RationalType'], [new RationalMatrix([2]), '2/5', 'Rational\\RationalType'], [new RationalMatrix([2]), true, 'Rational\\RationalType'], [new RationalMatrix([2]), false, 'Rational\\RationalType'], [new RationalMatrix([2]), TypeFactory::createComplex(2), 'Rational\\RationalType'], [new RationalMatrix([2]), TypeFactory::createInt(2), 'Rational\\RationalType'], [new RationalMatrix([2]), TypeFactory::createFloat(2), 'Rational\\RationalType'], [new RationalMatrix([2]), TypeFactory::createRational(2), 'Rational\\RationalType'], [new ComplexMatrix(['2+1i']), 2, 'Complex\\ComplexType'], [new ComplexMatrix(['2+1i']), 2.5, 'Complex\\ComplexType'], [new ComplexMatrix(['2+1i']), true, 'Complex\\ComplexType'], [new ComplexMatrix(['2+1i']), false, 'Complex\\ComplexType'], [new ComplexMatrix(['2+1i']), '2/5', 'Complex\\ComplexType'], [new ComplexMatrix(['2+1i']), '1+2i', 'Complex\\ComplexType'], [new ComplexMatrix(['2+1i']), TypeFactory::createComplex(2), 'Complex\\ComplexType'], [new ComplexMatrix(['2+1i']), TypeFactory::createInt(2), 'Complex\\ComplexType'], [new ComplexMatrix(['2+1i']), TypeFactory::createFloat(2), 'Complex\\ComplexType'], [new ComplexMatrix(['2+1i']), TypeFactory::createRational(2), 'Complex\\ComplexType']]; }
public function testTestingARationalMatrixReturnsClass() { $this->assertInstanceOf('Chippyash\\Test\\Math\\Matrix\\Traits\\stubTraitAssertParameterIsRationalNumber', $this->object->test(TypeFactory::createRational(2))); }