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: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153:
<?php
namespace Chippyash\Math\Matrix\Formatter;
use Chippyash\Matrix\Formatter\Ascii as BaseAscii;
use Chippyash\Matrix\Matrix;
use Chippyash\Type\Interfaces\NumericTypeInterface;
use Chippyash\Type\Number\Rational\RationalTypeFactory;
use Chippyash\Type\Number\Complex\ComplexTypeFactory;
class AsciiNumeric extends BaseAscii
{
const TP_NONE = 0;
const TP_INT = 1;
const TP_FLOAT = 2;
const TP_RATIONAL = 3;
const TP_COMPLEX = 4;
protected $extendedOptions = array(
'outputType' => self::TP_NONE,
);
public function format(Matrix $mA, array $options = array())
{
$this->setOptions($options);
return parent::format($this->convertEntries($mA), $options);
}
protected function setOptions(array $options = array())
{
$this->options = array_merge($this->options, $this->extendedOptions, $options);
}
protected function convertEntries(Matrix $mA)
{
switch ($this->options['outputType']) {
case self::TP_INT:
return $this->toInt($mA->toArray());
case self::TP_FLOAT:
return $this->toFloat($mA->toArray());
case self::TP_RATIONAL:
return $this->toRational($mA->toArray());
case self::TP_COMPLEX:
return $this->toComplex($mA->toArray());
case self::TP_NONE:
default :
return $mA;
}
}
protected function toInt(array $dA)
{
foreach ($dA as &$row) {
foreach ($row as &$entry) {
if ($entry instanceof NumericTypeInterface) {
$entry = $entry->asIntType()->get();
} elseif (is_numeric($entry)) {
$entry = intval($entry);
} else {
$entry = $entry;
}
}
}
return new Matrix($dA);
}
protected function toFloat(array $dA)
{
foreach ($dA as &$row) {
foreach ($row as &$entry) {
if ($entry instanceof NumericTypeInterface) {
$entry = $entry->asFloatType()->get();
} elseif (is_numeric($entry)){
$entry = floatval($entry);
} else {
$entry = $entry;
}
}
}
return new Matrix($dA);
}
protected function toRational(array $dA)
{
foreach ($dA as &$row) {
foreach ($row as &$entry) {
if ($entry instanceof NumericTypeInterface) {
$entry = $entry->asRational();
} elseif (is_numeric($entry)) {
$entry = RationalTypeFactory::fromFloat(floatval($entry));
} else {
$entry = $entry;
}
}
}
return new Matrix($dA);
}
protected function toComplex(array $dA)
{
foreach ($dA as &$row) {
foreach ($row as &$entry) {
if ($entry instanceof NumericTypeInterface) {
$entry = $entry->asComplex();
} elseif (is_numeric($entry)) {
$entry = ComplexTypeFactory::create($entry);
} else {
$entry = $entry;
}
}
}
return new Matrix($dA);
}
}