Matrix with two diagonals: Difference between revisions

Add Mathematica/Wolfram Language implementation
m (Corrected a typo.)
(Add Mathematica/Wolfram Language implementation)
(6 intermediate revisions by 4 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,509 ⟶ 1,523:
1 0 0 0 0 0 1
</pre>
 
 
=={{header|FreeBASIC}}==
Line 1,567 ⟶ 1,580:
{{out}}
https://www.dropbox.com/s/ph9r28gpkp8ao8n/twoDiagonalMatrix.bmp?dl=0
 
 
=={{header|Fortran}}==
Line 1,769 ⟶ 1,781:
 
=={{header|J}}==
 
Implementation:
 
Line 2,088 ⟶ 2,099:
</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,165:
A(1:N+1:end) = 1;
end</syntaxhighlight>
{{Outputout}}
<pre>
>> diagdiag(7)
Line 2,150 ⟶ 2,198:
(1,7) 1
(7,7) 1
</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
/* Function that returns a square matrix with a diagonal and antidiagonal pattern in their entries */
diags(n):=genmatrix(lambda([x,y],if x=y or x+y=n+1 then 1 else 0),n,n)$
 
/* Example */
diags(6);
</syntaxhighlight>
{{out}}
<pre>
matrix(
[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>
 
Line 2,588 ⟶ 2,656:
}
</syntaxhighlight>
 
=={{header|Python}}==
===Pure Python===
Line 2,694 ⟶ 2,763:
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery"> [ [] swap dup times
[ 0 over of
Line 2,909 ⟶ 2,977:
1 0 0 0 1
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func dual_diagonal(n) {
Line 2,972 ⟶ 3,041:
=={{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) {
337

edits