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: 114: 115: 116: 117: 118: 119: 120:
<?php
namespace Chippyash\Math\Matrix\Special;
use Chippyash\Math\Matrix\Exceptions\MathMatrixException;
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 Cauchy extends AbstractSpecial
{
const ERR1 = 'x and y must be vectors of same length for cauchy matrix';
const ERR2 = 'x and y must be vectors';
protected $map = ['x', 'y'];
protected function validateArguments(array $args)
{
$valA = new HasTypeMap([
'x' => 'integer'
]
);
$valB = new HasTypeMap([
'x' => 'Chippyash\Math\Matrix\NumericMatrix',
'y' => 'Chippyash\Math\Matrix\NumericMatrix'
]
);
$valB1 = new Lambda(function($args) {
return $args['x']->is('Vector') && $args['y']->is('Vector');
},
new StringType(self::ERR2));
$valB2 = new Lambda(function($args) {
return $args['x']->vertices() == $args['y']->vertices();
},
new StringType(self::ERR1)
);
$validator = new LOr(
$valA,
new LAnd(
$valB,
new LAnd(
$valB1, $valB2
)
)
);
if (!$validator->isValid($args)) {
throw new InvalidParameterException(implode(':', $validator->getMessages()));
}
}
protected function createMatrix(array $args)
{
if (is_int($args['x'])) {
return $this->createFromInt($args['x']);
}
return $this->createFromMatrices($args['x'], $args['y']);
}
protected function createFromInt($val)
{
$mX = new NumericMatrix(range(1, $val));
$mY = clone $mX;
return $this->createFromMatrices($mX, $mY);
}
protected function createFromMatrices(NumericMatrix $mX, NumericMatrix $mY)
{
$mX = ($mX->is('columnvector') ? $mX : $mX = $mX('Transpose'));
$mY = ($mY->is('rowvector') ? $mY : $mY = $mY('Transpose'));
$ones = new Ones();
$mVertices = $mX->vertices();
$onesRow = $ones->create([1, $mVertices]);
$onesCol = $ones->create([$mVertices, 1]);
$m1 = $mX('Mul\Matrix', $onesRow);
$m2 = $onesCol('Mul\Matrix', $mY);
$mC = $m1('Add\Matrix', $m2);
$onesSquare = $ones->create([$mVertices, $mVertices]);
return $onesSquare('Div\Entrywise', $mC);
}
}