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:
<?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 Functional extends AbstractSpecial
{
const ERR1 = 'rows and cols parameters must be integer > 0';
const ERR2 = 'f parameter must be a Closure';
protected $map = ['rows', 'cols', 'f'];
protected function validateArguments(array $args)
{
$valA1 = new HasTypeMap([
'rows' => 'integer',
'cols' => 'integer',
'f' => function($val) { return ($val instanceof \Closure);}
]
);
$valA2 = new Lambda(function($args) {
return ($args['rows'] > 0 && $args['cols'] > 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)
{
$data = [];
$func = $args['f'];
for ($row = 0; $row < $args['rows']; $row ++) {
for ($col = 0; $col < $args['cols']; $col ++) {
$data[$row][$col] = $func($row, $col);
}
}
return new NumericMatrix($data);
}
}