1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91:
<?php
namespace Chippyash\Math\Matrix\Traits;
use Chippyash\Math\Matrix\NumericMatrix;
use Chippyash\Math\Matrix\RationalMatrix;
use Chippyash\Math\Matrix\ComplexMatrix;
use Chippyash\Type\Number\Complex\ComplexTypeFactory;
use Chippyash\Type\Number\Rational\RationalTypeFactory;
use Chippyash\Type\Interfaces\NumericTypeInterface;
use Chippyash\Type\TypeFactory;
use Chippyash\Math\Matrix\Exceptions\ComputationException;
Trait CreateCorrectScalarType
{
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) {
}
}
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);
}
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) {
}
}
}
throw new ComputationException('Scalar parameter is not a supported type for numeric matrices: ' . getType($scalar));
}
}