Matrix with two diagonals: Difference between revisions

Content deleted Content added
imported>Maxima enthusiast
No edit summary
m [Uiua] ⇌ works on the rows of the array by default, so ≡ isn't needed.
(8 intermediate revisions by 5 users not shown)
Line 1:
{{Draft task|Matrices}}
 
;Task:
Line 460:
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">drawSquare: function [side][
loop 1..side 'x ->
Line 642 ⟶ 641:
0 1 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 0 1</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">D2 ← ∨⟜⌽∾˜⥊+⟜1↑×
 
D2 7</syntaxhighlight>
{{out}}
<pre>┌─
╵ 1 0 0 0 0 0 1
0 1 0 0 0 1 0
0 0 1 0 1 0 0
0 0 0 1 0 0 0
0 0 1 0 1 0 0
0 1 0 0 0 1 0
1 0 0 0 0 0 1
┘</pre>
 
=={{header|C}}==
Line 1,009 ⟶ 1,023:
0 1 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 0 1</pre>
 
=={{header|EasyLang}}==
{{trans|Nim}}
<syntaxhighlight>
proc matrix side . .
for i to side
for j to side
if i = j or i = side - j + 1
write "1 "
else
write "0 "
.
.
print ""
.
.
matrix 6
</syntaxhighlight>
{{out}}
<pre>
1 0 0 0 0 1
0 1 0 0 1 0
0 0 1 1 0 0
0 0 1 1 0 0
0 1 0 0 1 0
1 0 0 0 0 1
</pre>
 
=={{header|Excel}}==
Line 1,509 ⟶ 1,550:
1 0 0 0 0 0 1
</pre>
 
 
=={{header|FreeBASIC}}==
Line 1,567 ⟶ 1,607:
{{out}}
https://www.dropbox.com/s/ph9r28gpkp8ao8n/twoDiagonalMatrix.bmp?dl=0
 
 
=={{header|Fortran}}==
Line 1,769 ⟶ 1,808:
 
=={{header|J}}==
 
Implementation:
 
Line 2,088 ⟶ 2,126:
</syntaxhighlight>
 
=={{header|MatlabK}}==
K6
<syntaxhighlight lang="k">diag2: {x||x}@=:
 
diag2 5</syntaxhighlight>
{{out}}
<pre>(1 0 0 0 1
0 1 0 1 0
0 0 1 0 0
0 1 0 1 0
1 0 0 0 1)</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="Mathematica">
ClearAll[CreateMatrixWithTwoDiagonals];
CreateMatrixWithTwoDiagonals[n_Integer] :=
IdentityMatrix[n] + Reverse[IdentityMatrix[n]] -
If[OddQ[n], SparseArray[{{(n + 1)/2, (n + 1)/2} -> 1}, {n, n}], 0];
CreateMatrixWithTwoDiagonals[7] // MatrixForm
</syntaxhighlight>
{{out}}
 
<math>
\left(
\begin{array}{ccccccc}
1 & 0 & 0 & 0 & 0 & 0 & 1 \\
0 & 1 & 0 & 0 & 0 & 1 & 0 \\
0 & 0 & 1 & 0 & 1 & 0 & 0 \\
0 & 0 & 0 & 1 & 0 & 0 & 0 \\
0 & 0 & 1 & 0 & 1 & 0 & 0 \\
0 & 1 & 0 & 0 & 0 & 1 & 0 \\
1 & 0 & 0 & 0 & 0 & 0 & 1 \\
\end{array}
\right)
</math>
 
 
=={{header|MATLAB}}==
<syntaxhighlight lang="matlab">function A = diagdiag(N, sparse)
% Create an diagonal-diagonal square matrix.
Line 2,117 ⟶ 2,192:
A(1:N+1:end) = 1;
end</syntaxhighlight>
{{Outputout}}
<pre>
>> diagdiag(7)
Line 2,608 ⟶ 2,683:
}
</syntaxhighlight>
 
=={{header|Python}}==
===Pure Python===
Line 2,714 ⟶ 2,790:
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery"> [ [] swap dup times
[ 0 over of
Line 2,929 ⟶ 3,004:
1 0 0 0 1
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func dual_diagonal(n) {
Line 2,954 ⟶ 3,030:
1 0 0 0 0 1
</pre>
=={{header|Uiua}}==
 
<syntaxhighlight lang="Uiua">
DoubleMatrix ← ↥⇌.⊞=.⇡
DoubleMatrix 4
</syntaxhighlight>
{{out}}
<pre>
╭─
╷ 1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1
</pre>
=={{header|V (Vlang)}}==
{{trans|go}}
Line 2,992 ⟶ 3,081:
=={{header|Wren}}==
A terminal based solution as I don't like asking people to view external images.
<syntaxhighlight lang="ecmascriptwren">var specialMatrix = Fn.new { |n|
for (i in 0...n) {
for (j in 0...n) {