Matrix with two diagonals: Difference between revisions
Add Mathematica/Wolfram Language implementation
(Added Algol W) |
(Add Mathematica/Wolfram Language implementation) |
||
(13 intermediate revisions by 7 users not shown) | |||
Line 1:
{{Draft task|Matrices}}
;Task:
Line 5:
<br> If you can please use GUI
<br><br>
===See also===
* [[Four sides of square]]
* [[Mosaic matrix]]
=={{header|11l}}==
Line 37 ⟶ 41:
0 1 0 0 0 1 0
1 0 0 0 0 0 1
</pre>
=={{header|Action!}}==
<syntaxhighlight lang="action!">
;;; draw a matrix with 1s on the diagonals and 0s elsewhere
;;; draws a matrix with height and width = n with 1s on the diagonals
PROC drawDiagonals( INT n )
CARD i, j, r
r = n
FOR i = 1 TO n DO
FOR j = 1 TO n DO
Put(' )
IF j = i OR j = r THEN Put('1) ELSE Put('0) FI
OD
PutE()
r ==- 1
OD
RETURN
PROC Main()
drawDiagonals( 6 )
PutE()
drawDiagonals( 7 )
RETURN
</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
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>
Line 414 ⟶ 460:
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">drawSquare: function [side][
loop 1..side 'x ->
Line 596 ⟶ 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,463 ⟶ 1,523:
1 0 0 0 0 0 1
</pre>
=={{header|FreeBASIC}}==
Line 1,521 ⟶ 1,580:
{{out}}
https://www.dropbox.com/s/ph9r28gpkp8ao8n/twoDiagonalMatrix.bmp?dl=0
=={{header|Fortran}}==
Line 1,723 ⟶ 1,781:
=={{header|J}}==
Implementation:
Line 2,042 ⟶ 2,099:
</syntaxhighlight>
=={{header|
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,071 ⟶ 2,165:
A(1:N+1:end) = 1;
end</syntaxhighlight>
{{
<pre>
>> diagdiag(7)
Line 2,104 ⟶ 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>
=={{header|Nim}}==
<syntaxhighlight lang="Nim">proc drawMatrix(side: Positive) =
let last = side - 1
for i in 0..<side:
for j in 0..<side:
stdout.write if i == j or i == last - j: "1 " else: "0 "
echo()
drawMatrix(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>
Line 2,439 ⟶ 2,573:
0 1 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 1
</pre>
=={{header|PL/M}}==
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
<syntaxhighlight lang="plm">
100H: /* DRAW SOME MATRICES WITH 1S ON THE DIAGONALS and 0S ELSEWHERE */
/* CP/M SYSTEM CALL AND I/O ROUTINES */
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PR$NL: PROCEDURE; CALL PR$CHAR( 0DH ); CALL PR$CHAR( 0AH ); END;
/* TASK */
DRAW$DIAGONALS: PROCEDURE( N );
DECLARE N BYTE;
DECLARE ( I, J, R ) BYTE;
R = N;
DO I = 1 TO N;
DO J = 1 TO N;
CALL PR$CHAR( ' ' );
IF J = I OR J = R THEN CALL PR$CHAR( '1' );
ELSE CALL PR$CHAR( '0' );
END;
CALL PR$NL;
R = R - 1;
END;
END DRAW$DIAGONALS ;
CALL DRAW$DIAGONALS( 10 );
CALL PR$NL;
CALL DRAW$DIAGONALS( 11 );
EOF
</syntaxhighlight>
{{out}}
<pre>
1 0 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0 1 0
0 0 1 0 0 0 0 1 0 0
0 0 0 1 0 0 1 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 1 0 0 1 0 0 0
0 0 1 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0 0 1 0
0 0 1 0 0 0 0 0 1 0 0
0 0 0 1 0 0 0 1 0 0 0
0 0 0 0 1 0 1 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 1 0 1 0 0 0 0
0 0 0 1 0 0 0 1 0 0 0
0 0 1 0 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 0 0 1
</pre>
Line 2,463 ⟶ 2,656:
}
</syntaxhighlight>
=={{header|Python}}==
===Pure Python===
Line 2,569 ⟶ 2,763:
=={{header|Quackery}}==
<syntaxhighlight lang="quackery"> [ [] swap dup times
[ 0 over of
Line 2,738 ⟶ 2,931:
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
≪ IDN LAST
1 OVER '''FOR''' line
line OVER 2 →LIST ROT SWAP 1 PUT
SWAP 1 -
'''NEXT''' DROP
≫ ‘XDIAG’ STO
5 XDIAG
Line 2,754 ⟶ 2,945:
<pre>
2: [[ 1 0 0 0 1 ]
1: [[ 1 0 0 0 0 1 ]
</pre>
Line 2,786 ⟶ 2,977:
1 0 0 0 1
</pre>
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func dual_diagonal(n) {
Line 2,849 ⟶ 3,041:
=={{header|Wren}}==
A terminal based solution as I don't like asking people to view external images.
<syntaxhighlight lang="
for (i in 0...n) {
for (j in 0...n) {
|