Identity matrix: Difference between revisions
Content added Content deleted
m (→{{header|AppleScript}}: tidied, updated output.) |
|||
Line 2,409: | Line 2,409: | ||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
<lang php> |
<lang php> |
||
function |
function identity($length) { |
||
return array_map(function($key, $value) {$value[$key] = 1; return $value;}, range(0, $length-1), |
|||
{ |
|||
array_fill(0, $length, array_fill(0,$length, 0))); |
|||
$result = array(); |
|||
for ($i = 0; $i < $size; $i++) { |
|||
$row = array_fill(0, $size, 0); |
|||
$row[$i] = 1; |
|||
$result[] = $row; |
|||
} |
|||
return $result; |
|||
} |
} |
||
function print_identity($identity) { |
|||
echo implode(PHP_EOL, array_map(function ($value) {return implode(' ', $value);}, $identity)); |
|||
function printMatrix(array $matrix) |
|||
{ |
|||
foreach ($matrix as $row) { |
|||
foreach ($row as $column) { |
|||
echo $column . " "; |
|||
⚫ | |||
echo PHP_EOL; |
|||
} |
|||
echo PHP_EOL; |
|||
} |
} |
||
print_identity(identity(10)); |
|||
printMatrix(createMatrix(5)); |
|||
</lang> |
</lang> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
1 0 0 0 0 |
1 0 0 0 0 0 0 0 0 0 |
||
0 1 0 0 0 |
0 1 0 0 0 0 0 0 0 0 |
||
0 0 1 0 0 |
0 0 1 0 0 0 0 0 0 0 |
||
0 0 0 1 0 |
0 0 0 1 0 0 0 0 0 0 |
||
0 0 0 0 1 |
0 0 0 0 1 0 0 0 0 0 |
||
⚫ | |||
0 0 0 0 0 0 1 0 0 0 |
|||
0 0 0 0 0 0 0 1 0 0 |
|||
0 0 0 0 0 0 0 0 1 0 |
|||
0 0 0 0 0 0 0 0 0 1 |
|||
</pre> |
</pre> |
||