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:
<?php
namespace Chippyash\Math\Matrix\Special;
use Chippyash\Math\Matrix\NumericMatrix;
use Chippyash\Type\String\StringType;
use Chippyash\Validation\Common\Lambda;
use Chippyash\Validation\Exceptions\InvalidParameterException;
use Chippyash\Validation\Logical\LAnd;
use Chippyash\Validation\Pattern\HasTypeMap;
class Identity extends AbstractSpecial
{
const ERR1 = 'size parameter must be integer > 0';
protected $map = ['size'];
protected function validateArguments(array $args)
{
$valA1 = new HasTypeMap([
'size' => 'integer'
]
);
$valA2 = new Lambda(function($args) {
return $args['size'] > 0;
}, new StringType(self::ERR1));
$validator = new LAnd($valA1, $valA2);
if (!$validator->isValid($args)) {
throw new InvalidParameterException(implode(':', $validator->getMessages()));
}
}
protected function createMatrix(array $args)
{
$func = function($row, $col) {return ($row == $col ? 1 : 0);};
return (new Functional())->create([$args['size'], $args['size'], $func]);
}
}