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: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113:
<?php
namespace Chippyash\Math\Matrix;
use Chippyash\Type\Number\IntType;
use Chippyash\Type\Number\Rational\RationalTypeFactory;
use Chippyash\Type\Number\Complex\ComplexTypeFactory;
use Chippyash\Type\TypeFactory;
class IdentityMatrix extends FunctionMatrix
{
const IDM_TYPE_INT = 0;
const IDM_TYPE_RATIONAL = 1;
const IDM_TYPE_COMPLEX = 2;
private $availableTypes = [
self::IDM_TYPE_INT,
self::IDM_TYPE_RATIONAL,
self::IDM_TYPE_COMPLEX ];
public function __construct(IntType $size, IntType $identityType = null)
{
if (is_null($identityType)) {
$idt = self::IDM_TYPE_INT;
} else {
$idt = $identityType();
}
if (!in_array($idt, $this->availableTypes)) {
throw new \InvalidArgumentException('Identity type invalid');
}
if ($size() < 1) {
throw new \InvalidArgumentException('size must be >= 1');
}
$f = function($row, $col) use ($idt) {
if ($idt == self::IDM_TYPE_RATIONAL) {
return RationalTypeFactory::create($row == $col ? 1 : 0, 1);
} elseif ($idt == self::IDM_TYPE_COMPLEX) {
return ComplexTypeFactory::create(
RationalTypeFactory::create($row == $col ? 1 : 0, 1),
RationalTypeFactory::create(0, 1));
} else {
return TypeFactory::createInt($row == $col ? 1 : 0);
}
};
parent::__construct($f, $size, $size);
}
public static function rationalIdentity(IntType $size)
{
$mA = new self($size, TypeFactory::createInt(self::IDM_TYPE_RATIONAL));
return new RationalMatrix($mA);
}
public static function complexIdentity(IntType $size)
{
$mA = new self($size, TypeFactory::createInt(self::IDM_TYPE_COMPLEX));
return new ComplexMatrix($mA);
}
public static function numericIdentity(IntType $size)
{
return new self($size, TypeFactory::createInt(self::IDM_TYPE_INT));
}
}