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:
<?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\Logical\LOr;
use Chippyash\Validation\Pattern\HasTypeMap;
class Zeros extends AbstractSpecial
{
const ERR1 = 'rows parameter must be integer > 0';
const ERR2 = 'rows and cols parameter must be integer > 0';
protected $map = ['rows', 'cols'];
protected function validateArguments(array $args)
{
$valA1 = new HasTypeMap([
'rows' => 'integer'
]
);
$valA2 = new Lambda(function($args) {
return $args['rows'] > 0 && !array_key_exists('cols', $args);
}, new StringType(self::ERR1));
$valA = new LAnd($valA1, $valA2);
$valB1 = new HasTypeMap([
'rows' => 'integer',
'cols' => 'integer'
]
);
$valB2 = new Lambda(function($args) {
return $args['rows'] > 0 && $args['cols'] > 0;
},
new StringType(self::ERR2)
);
$valB = new LAnd($valB1, $valB2);
$validator = new LOr($valA,$valB);
if (!$validator->isValid($args)) {
throw new InvalidParameterException(implode(':', $validator->getMessages()));
}
}
protected function createMatrix(array $args)
{
$rows = $args['rows'];
$cols = array_key_exists('cols', $args) ? $args['cols'] : 1;
return $this->zeroes($rows, $cols);
}
private function zeroes($rows, $cols)
{
$ret = array_fill(0, $rows, array_fill(0, $cols, 0));
return new NumericMatrix($ret);
}
}