Matrix with two diagonals: Difference between revisions

Add Mathematica/Wolfram Language implementation
No edit summary
(Add Mathematica/Wolfram Language implementation)
(15 intermediate revisions by 8 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 102 ⟶ 148:
draw 2 diagonals( 11 )
END</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>
 
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">
begin % draw a matrix with 1s on the diagonals and 0s elsewhere %
% draws a matrix with height and width = n with 1s on the diagonals %
procedure draw2Diagonals ( integer value n ) ;
begin
integer lPos, rPos;
lPos := 1;
rPos := n;
for i := 1 until n do begin
for j := 1 until n do writeon( s_w := 0, if j = lPos or j = rPos then " 1" else " 0" );
write();
lPos := lPos + 1;
rPos := rPos - 1
end for_i
end draw2Diagonals ;
% test the draw 2 diagonals procedure %
draw2Diagonals( 10 );
write();
draw2Diagonals( 11 )
end.
</syntaxhighlight>
{{out}}
<pre>
Line 144 ⟶ 238:
1 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 0 0 1 </pre>
 
=={{header|AppleScript}}==
===Procedural===
Line 365 ⟶ 460:
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">drawSquare: function [side][
loop 1..side 'x ->
Line 547 ⟶ 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 712 ⟶ 821:
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
It seems to me you should actually build the matricies, not just draw a bunch of numbers on the screen. Consequently, this code actually builds general purpose matrices to solve the problem. Once again, notice how the code is modular, breaking the operations down into separate subroutines that can be reused in other situations. This is how you build libraryslibraries and simplify your code when working on larger problems.
 
<syntaxhighlight lang="Delphi">
Line 853 ⟶ 962:
[1 0 0 0 0 0 0 0 0 1]
</pre>
 
 
=={{header|Draco}}==
Line 1,415 ⟶ 1,523:
1 0 0 0 0 0 1
</pre>
 
 
=={{header|FreeBASIC}}==
Line 1,473 ⟶ 1,580:
{{out}}
https://www.dropbox.com/s/ph9r28gpkp8ao8n/twoDiagonalMatrix.bmp?dl=0
 
 
=={{header|Fortran}}==
Line 1,675 ⟶ 1,781:
 
=={{header|J}}==
 
Implementation:
 
Line 1,994 ⟶ 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,023 ⟶ 2,165:
A(1:N+1:end) = 1;
end</syntaxhighlight>
{{Outputout}}
<pre>
>> diagdiag(7)
Line 2,056 ⟶ 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,391 ⟶ 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,415 ⟶ 2,656:
}
</syntaxhighlight>
 
=={{header|Python}}==
===Pure Python===
Line 2,521 ⟶ 2,763:
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery"> [ [] swap dup times
[ 0 over of
Line 2,690 ⟶ 2,931:
 
=={{header|RPL}}==
ApproachThe approach here is to fill the second diagonal of an identity matrix generated by the <code>IDN</code> instruction, thanks to a classical <code>FOR.. NEXT</code> loop.
{{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
DROP
‘XDIAG’ STO
 
5 XDIAG
Line 2,706 ⟶ 2,945:
<pre>
2: [[ 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 ]]
1: [[ 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,738 ⟶ 2,977:
1 0 0 0 1
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func dual_diagonal(n) {
Line 2,801 ⟶ 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) {
338

edits