Matrix with two diagonals: Difference between revisions

Add Mathematica/Wolfram Language implementation
(Add Mathematica/Wolfram Language implementation)
(38 intermediate revisions by 23 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}}==
{{trans|Wren}}
 
<syntaxhighlight lang="11l">
F two_diagonal_matrix(n)
L(i) 0 .< n
L(j) 0 .< n
print(I i == j | i + j == n - 1 {1} E 0, end' ‘ ’)
print()
 
two_diagonal_matrix(6)
print()
two_diagonal_matrix(7)
</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>
 
=={{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>
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io; use Ada.Text_Io;
with Ada.Command_Line;
 
Line 46 ⟶ 125:
Put_Line ("Usage: ./matrix_with_diagonals <side-length>");
 
end Matrix_With_Diagonals;</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">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 #
PROC draw 2 diagonals = ( INT n )VOID:
Line 68 ⟶ 147:
print( ( newline ) );
draw 2 diagonals( 11 )
END</langsyntaxhighlight>
{{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 97 ⟶ 224:
=={{header|APL}}==
{{works with|Dyalog APL}}
<langsyntaxhighlight lang="apl">twoDiagonals ← (⌽∨⊢)∘.=⍨∘⍳
twoDiagonals¨ 10 11</langsyntaxhighlight>
{{out}}
<pre> 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1
Line 111 ⟶ 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===
<langsyntaxhighlight lang="applescript">on twoDiagonalMatrix(n)
if (n < 2) then error "twoDiagonalMatrix() handler: parameter must be > 1."
Line 151 ⟶ 279:
 
return linefeed & matrixToText(twoDiagonalMatrix(7), space) & ¬
(linefeed & linefeed & matrixToText(twoDiagonalMatrix(8), space))</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">"
1 0 0 0 0 0 1
0 1 0 0 0 1 0
Line 170 ⟶ 298:
0 0 1 0 0 1 0 0
0 1 0 0 0 0 1 0
1 0 0 0 0 0 0 1"</langsyntaxhighlight>
 
===Functional===
<langsyntaxhighlight lang="applescript">---------------- MATRIX WITH TWO DIAGONALS ---------------
 
-- bothDiagonals :: Int -> [[Int]]
Line 312 ⟶ 440:
set my text item delimiters to dlm
return s
end unwords</langsyntaxhighlight>
{{Out}}
<pre>1 0 0 0 0 0 1
Line 330 ⟶ 458:
0 1 0 0 0 0 1 0
1 0 0 0 0 0 0 1</pre>
 
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">drawSquare: function [side][
loop 1..side 'x ->
print map 1..side 'y [
(any? @[x=y side=x+y-1])? -> 1 -> 0
]
]
 
drawSquare 6
print ""
drawSquare 9</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 0 0 1
0 1 0 0 0 0 0 1 0
0 0 1 0 0 0 1 0 0
0 0 0 1 0 1 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 1 0 1 0 0 0
0 0 1 0 0 0 1 0 0
0 1 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 1</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">for i, v in [8, 9]
result .= "Matrix Size: " v "*" v "`n" matrix2txt(diagonalMatrix(v)) "`n"
MsgBox % result
return
 
diagonalMatrix(size){
M := []
loop % size {
row := A_Index
loop % size
M[row, A_Index] := (row = A_Index || row = size-A_Index+1) ? 1 : 0
}
return M
}
 
matrix2txt(M){
for row , obj in M {
for col, v in obj
result .= M[row, col] " "
result .= "`n"
}
return result
}</syntaxhighlight>
{{out}}
<pre>Matrix Size: 8*8
1 0 0 0 0 0 0 1
0 1 0 0 0 0 1 0
0 0 1 0 0 1 0 0
0 0 0 1 1 0 0 0
0 0 0 1 1 0 0 0
0 0 1 0 0 1 0 0
0 1 0 0 0 0 1 0
1 0 0 0 0 0 0 1
 
Matrix Size: 9*9
1 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 1 0
0 0 1 0 0 0 1 0 0
0 0 0 1 0 1 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 1 0 1 0 0 0
0 0 1 0 0 0 1 0 0
0 1 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 1 </pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f MATRIX_WITH_TWO_DIAGONALS.AWK
BEGIN {
Line 347 ⟶ 552:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 365 ⟶ 570:
1 0 0 0 0 0 1
</pre>
 
=={{header|Basic}}==
<langsyntaxhighlight lang="qbasic">100 REM
110 REM DIAGONAL-DIAGONAL MATRIX
120 REM
Line 388 ⟶ 594:
290 NEXT I
300 END
</syntaxhighlight>
</lang>
{{Output}}
<pre> 1 0 0 0 0 0 1
Line 398 ⟶ 604:
1 0 0 0 0 0 1
</pre>
 
=={{header|BCPL}}==
<syntaxhighlight lang="bcpl">get "libhdr"
 
let diagonals(size) be
for y = 1 to size
for x = 1 to size
do writef("%C%C",
x=y | size-x=y-1 -> '1', '0',
x=size -> '*N', ' ')
let start() be
$( diagonals(9)
wrch('*N')
diagonals(10)
$)</syntaxhighlight>
{{out}}
<pre>1 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 1 0
0 0 1 0 0 0 1 0 0
0 0 0 1 0 1 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 1 0 1 0 0 0
0 0 1 0 0 0 1 0 0
0 1 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 1
 
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</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}}==
{{trans|Wren}}
<langsyntaxhighlight lang="c">#include <stdio.h>
 
void specialMatrix(unsigned int n) {
Line 422 ⟶ 680:
specialMatrix(11); // odd n
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 451 ⟶ 709:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <concepts>
#include <iostream>
 
Line 481 ⟶ 739:
}
 
</syntaxhighlight>
</lang>
{{out}}
<pre> 1 0 0 0 0 0 0 1
Line 502 ⟶ 760:
1 0 0 0 0 0 0 0 1
</pre>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">matrix = array[array[int]]
 
diagonals = proc (size: int) returns (matrix)
mat: matrix := matrix$new()
for y: int in int$from_to(1, size) do
line: array[int] := array[int]$new()
for x: int in int$from_to(1, size) do
n: int
if x=y cor size-x=y-1
then n := 1
else n := 0
end
array[int]$addh(line, n)
end
matrix$addh(mat, line)
end
return(mat)
end diagonals
 
print_matrix = proc (s: stream, mat: matrix)
for line: array[int] in matrix$elements(mat) do
for elem: int in array[int]$elements(line) do
stream$puts(s, " " || int$unparse(elem))
end
stream$putl(s, "")
end
end print_matrix
 
start_up = proc ()
po: stream := stream$primary_output()
print_matrix(po, diagonals(9))
stream$putl(po, "")
print_matrix(po, diagonals(10))
end start_up</syntaxhighlight>
{{out}}
<pre> 1 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 1 0
0 0 1 0 0 0 1 0 0
0 0 0 1 0 1 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 1 0 1 0 0 0
0 0 1 0 0 0 1 0 0
0 1 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 1
 
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</pre>
 
=={{header|Delphi}}==
{{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 libraries and simplify your code when working on larger problems.
 
<syntaxhighlight lang="Delphi">
{Define a matrix}
 
type TMatrix = array of array of double;
 
procedure DisplayMatrix(Memo: TMemo; Mat: TMatrix);
{Display specified matrix}
var X,Y: integer;
var S: string;
begin
S:='';
for Y:=0 to High(Mat) do
begin
S:=S+'[';
for X:=0 to High(Mat[0]) do
if X=0 then S:=S+Format('%0.0f',[Mat[X,Y]])
else S:=S+Format('%2.0f',[Mat[X,Y]]);
S:=S+']'+#$0D#$0A;
end;
Memo.Lines.Add(S);
end;
 
 
procedure ClearMatrix(var Mat: TMatrix; Value: double);
{Set all elements of the matrix to specified value}
var X,Y: integer;
begin
for Y:=0 to High(Mat) do
for X:=0 to High(Mat[0]) do Mat[X,Y]:=0;
end;
 
procedure SetMatrixDiagonals(var Mat: TMatrix);
{Set both diagonals to one}
var X,Y: integer;
begin
{Set diagonals to one}
for X:=0 to High(Mat) do Mat[X,X]:=1;
for X:=0 to High(Mat) do Mat[X,High(Mat)-X]:=1
end;
 
 
 
procedure BuildDiagionalMatrix(var Mat: TMatrix; Size: integer);
{Build a matrix with diagonals of the specified size }
var X,Y: integer;
begin
SetLength(Mat,Size,Size);
ClearMatrix(Mat,0);
SetMatrixDiagonals(Mat);
end;
 
procedure BuildAndShowMatrix(Memo: TMemo; var Mat: TMatrix; Size: integer);
{Build and show a matrix of specific size}
begin
Memo.Lines.Add(Format('Matrix = %2d X %2d',[Size,Size]));
BuildDiagionalMatrix(Mat,Size);
DisplayMatrix(Memo,Mat);
end;
 
 
procedure DisplayDiagonalMatrices(Memo: TMemo);
{Build and display matrices of various sizes}
var Mat: TMatrix;
var I: integer;
begin
for I:=3 to 10 do BuildAndShowMatrix(Memo,Mat,I);
end;
 
</syntaxhighlight>
{{out}}
<pre>
Matrix = 3 X 3
[1 0 1]
[0 1 0]
[1 0 1]
 
Matrix = 4 X 4
[1 0 0 1]
[0 1 1 0]
[0 1 1 0]
[1 0 0 1]
 
Matrix = 5 X 5
[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]
 
Matrix = 6 X 6
[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]
 
Matrix = 7 X 7
[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]
 
Matrix = 8 X 8
[1 0 0 0 0 0 0 1]
[0 1 0 0 0 0 1 0]
[0 0 1 0 0 1 0 0]
[0 0 0 1 1 0 0 0]
[0 0 0 1 1 0 0 0]
[0 0 1 0 0 1 0 0]
[0 1 0 0 0 0 1 0]
[1 0 0 0 0 0 0 1]
 
Matrix = 9 X 9
[1 0 0 0 0 0 0 0 1]
[0 1 0 0 0 0 0 1 0]
[0 0 1 0 0 0 1 0 0]
[0 0 0 1 0 1 0 0 0]
[0 0 0 0 1 0 0 0 0]
[0 0 0 1 0 1 0 0 0]
[0 0 1 0 0 0 1 0 0]
[0 1 0 0 0 0 0 1 0]
[1 0 0 0 0 0 0 0 1]
 
Matrix = 10 X 10
[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]
</pre>
 
=={{header|Draco}}==
<syntaxhighlight lang="draco">proc setDiagonals([*,*] byte matrix) void:
word x, y, width, height;
width := dim(matrix, 1);
height := dim(matrix, 2);
for x from 0 upto width-1 do
for y from 0 upto height-1 do
matrix[x,y] :=
if x = y or width - x - 1 = y
then 1
else 0
fi
od
od
corp
 
proc printMatrix([*,*] byte matrix) void:
word x, y, width, height;
width := dim(matrix, 1);
height := dim(matrix, 2);
for y from 0 upto height-1 do
for x from 0 upto width-1 do
write(' ', matrix[x,y]:1)
od;
writeln()
od
corp
 
proc main() void:
[9,9] byte m_odd;
[10,10] byte m_even;
setDiagonals(m_odd);
printMatrix(m_odd);
writeln();
setDiagonals(m_even);
printMatrix(m_even)
corp</syntaxhighlight>
{{out}}
<pre> 1 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 1 0
0 0 1 0 0 0 1 0 0
0 0 0 1 0 1 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 1 0 1 0 0 0
0 0 1 0 0 0 1 0 0
0 1 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 1
 
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</pre>
 
=={{header|Excel}}==
Line 513 ⟶ 1,034:
 
{{Works with|Office 365 betas 2021}}
<langsyntaxhighlight lang="lisp">TwoDiagonalMatrix
=LAMBDA(n,
LET(
Line 525 ⟶ 1,046:
)
)
)</langsyntaxhighlight>
{{Out}}
The formulae in cells A2 and B2 populate the whole of each adjacent matrix.
Line 736 ⟶ 1,257:
 
Binding the name ''bothDiagonalMatrix'' in the Excel Name Manager:
<langsyntaxhighlight lang="lisp">bothDiagonalMatrix
=LAMBDA(n,
MAKEARRAY(
Line 748 ⟶ 1,269:
)
)
)</langsyntaxhighlight>
{{Out}}
{| class="wikitable"
Line 955 ⟶ 1,476:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Matrix with two diagonals. Nigel Galloway: February 17th., 2022
let m11 m=Array2D.init m m (fun n g->if n=g || n+g=m-1 then 1 else 0)
printfn "%A\n\n%A" (m11 5) (m11 6)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 978 ⟶ 1,499:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: io kernel math math.matrices prettyprint ;
 
: <x-matrix> ( n -- matrix )
Line 984 ⟶ 1,505:
 
6 <x-matrix> simple-table. nl
7 <x-matrix> simple-table.</langsyntaxhighlight>
{{out}}
<pre>
Line 1,002 ⟶ 1,523:
1 0 0 0 0 0 1
</pre>
 
 
=={{header|FreeBASIC}}==
===Text based===
<langsyntaxhighlight lang="freebasic">Sub twoDiagonalMatrix(n As Integer)
For i As Integer = 1 To n
For j As Integer = 1 To n
Line 1,018 ⟶ 1,538:
Print
twoDiagonalMatrix(7)
Sleep</langsyntaxhighlight>
{{out}}
<pre>1 0 0 0 0 1
Line 1,036 ⟶ 1,556:
 
===Graphical===
<langsyntaxhighlight lang="freebasic">Dim As Integer n = 8, size = 60 * n + 70
Screenres size, size, 24
Cls
Line 1,057 ⟶ 1,577:
Next x
Bsave "twoDiagonalMatrix.bmp",0
Sleep</langsyntaxhighlight>
{{out}}
https://www.dropbox.com/s/ph9r28gpkp8ao8n/twoDiagonalMatrix.bmp?dl=0
 
 
=={{header|Fortran}}==
Line 1,066 ⟶ 1,585:
{{works with|Fortran 95}}
Fortran stores data in columns. Therefore, it should be checked whether instead of filling the matrix row by row (as below) it would be better to do it column by column. The profit would be cache hit optimization, better communication between CPU and RAM. Fortran allows you to zero the entire array, such as the a array below, simply by substituting a = 0. Unfortunately, an array 100x100 (that is, the choosen maximum size) would be filled with zeros even if, as in the example, we would need a much smaller array. You can also eliminate the variables j1 and j2 by replacing them with i and n - i + 1 respectively, but the source code would be slightly less readable.
<langsyntaxhighlight Fortranlang="fortran">program prog
 
dimension a(100, 100)
Line 1,089 ⟶ 1,608:
 
end
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 1,103 ⟶ 1,622:
{{Works with|Fortran 77}}
Fortran is the oldest high-level programming language. It is constantly evolving and unfortunately there are no longer (or at least I do not have) computers on which to test whether the program works in the old Fortran IV or an even older dialect. The example below should be in Fortran IV, but unfortunately it was tested with a modern Fortran 2018 compiler, so even in legacy mode it is not Fortran IV but Fortran 77. However, it seems that it should work on CDC6000 mainframe ... if someone obviously has CDC6000.
<langsyntaxhighlight lang="fortran">C DIAGONAL-DIAGONAL MATRIX IN FORTRAN 77
 
PROGRAM PROG
Line 1,113 ⟶ 1,632:
A = 0.
DO 10 I = 1, N
A(I, I) = 1.
10 A(I, N - I + 1) = 1.
Line 1,119 ⟶ 1,638:
20 PRINT *, (A(I, J), J=1,N)
END</langsyntaxhighlight>
 
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,144 ⟶ 1,663:
fmt.Println()
specialMatrix(9) // odd n
}</langsyntaxhighlight>
 
{{out}}
Line 1,169 ⟶ 1,688:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">---------------- MATRIX WITH TWO DIAGONALS ---------------
 
twoDiagonalMatrix :: Int -> [[Int]]
Line 1,186 ⟶ 1,705:
unlines . fmap (((' ' :) . show) =<<)
. twoDiagonalMatrix
<$> [7, 8]</langsyntaxhighlight>
 
 
Or, in the form of a list comprehension:
<langsyntaxhighlight lang="haskell">-------------- MATRIX WITH TWO DIAGONALS ---------------
 
twoDiagonalMatrix :: Int -> [[Int]]
Line 1,207 ⟶ 1,726:
unlines . fmap (((' ' :) . show) =<<)
. twoDiagonalMatrix
<$> [7, 8]</langsyntaxhighlight>
{{Out}}
<pre> 1 0 0 0 0 0 1
Line 1,228 ⟶ 1,747:
 
and in terms of the Data.Matrix library:
<langsyntaxhighlight lang="haskell">import Data.Matrix
 
twoDiagonals :: Int -> Matrix Int
Line 1,239 ⟶ 1,758:
main :: IO ()
main =
mapM_ print $ twoDiagonals <$> [7, 8]</langsyntaxhighlight>
{{Out}}
<pre>┌ ┐
Line 1,262 ⟶ 1,781:
 
=={{header|J}}==
 
Implementation:
 
<langsyntaxhighlight Jlang="j">task=: {{(+.|.)=i.y}}</langsyntaxhighlight>
 
In other words, generate an order n identity matrix, flip it and (treating it as a bit matrix) OR the two matrices.
Line 1,271 ⟶ 1,789:
Some examples:
 
<langsyntaxhighlight Jlang="j"> task 2
1 1
1 1
Line 1,295 ⟶ 1,813:
0 0 1 1 0 0
0 1 0 0 1 0
1 0 0 0 0 1</langsyntaxhighlight>
 
=={{header|Java}}==
The "Java philosophy" is the object-oriented paradigm. The example solution given below is therefore somewhat incomplete. We should first declare the matrix as an interface (or abstract class), then create a whole hierarchy of subclasses where DiagonalDiagonalMatrix would be a subclass of SquareMatrix or something like that. Of course, it's a gigantic job, but as a result we would have a library competing with Matlab / Octave etc., compliant with SOLID principles.
<lang java>package example.diagdiag;
<syntaxhighlight lang="java">package example.diagdiag;
 
public class Program {
Line 1,353 ⟶ 1,872:
}
 
}</langsyntaxhighlight>
{{Output}}
<pre> 1.0 0.0 0.0 0.0 0.0 0.0 1.0
Line 1,364 ⟶ 1,883:
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">(() => {
"use strict";
 
Line 1,412 ⟶ 1,931:
// MAIN --
return main();
})();</langsyntaxhighlight>
 
 
Or, in terms of a more general matrix function:
<langsyntaxhighlight lang="javascript">(() => {
"use strict";
 
Line 1,472 ⟶ 1,991:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>1 0 0 0 0 0 1
Line 1,494 ⟶ 2,013:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<langsyntaxhighlight lang="jq">def bidiagonal_matrix:
. as $n
| [range(0; $n) | 0] as $z
Line 1,501 ⟶ 2,020:
 
def display:
map(join(" ")) | join("\n");</langsyntaxhighlight>
'''Example'''
<syntaxhighlight lang ="jq">9|bidiagonal_matrix|display</langsyntaxhighlight>
{{out}}
<pre>
Line 1,518 ⟶ 2,037:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">
julia> twodiagonalmat(n) = [Int(i == j || i == n - j + 1) for j in 1:n, i in 1:n]
twodiagonalmat (generic function with 1 method)
Line 1,551 ⟶ 2,070:
0 1 0 1 0
1 0 0 0 1
</syntaxhighlight>
</lang>
=== Sparse matrix version ===
<syntaxhighlight lang="julia">julia> using LinearAlgebra
 
julia> twodiagonalsparse(n) = I(n) .| rotl90(I(n))
=={{header|Matlab}}==
twodiagonalsparse (generic function with 1 method)
<lang Matlab>function A = diagdiag(N, sparse)
 
julia> twodiagonalsparse(7)
7×7 SparseArrays.SparseMatrixCSC{Bool, Int64} with 13 stored entries:
1 ⋅ ⋅ ⋅ ⋅ ⋅ 1
⋅ 1 ⋅ ⋅ ⋅ 1 ⋅
⋅ ⋅ 1 ⋅ 1 ⋅ ⋅
⋅ ⋅ ⋅ 1 ⋅ ⋅ ⋅
⋅ ⋅ 1 ⋅ 1 ⋅ ⋅
⋅ 1 ⋅ ⋅ ⋅ 1 ⋅
1 ⋅ ⋅ ⋅ ⋅ ⋅ 1
 
julia> twodiagonalsparse(8)
8×8 SparseArrays.SparseMatrixCSC{Bool, Int64} with 16 stored entries:
1 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1
⋅ 1 ⋅ ⋅ ⋅ ⋅ 1 ⋅
⋅ ⋅ 1 ⋅ ⋅ 1 ⋅ ⋅
⋅ ⋅ ⋅ 1 1 ⋅ ⋅ ⋅
⋅ ⋅ ⋅ 1 1 ⋅ ⋅ ⋅
⋅ ⋅ 1 ⋅ ⋅ 1 ⋅ ⋅
⋅ 1 ⋅ ⋅ ⋅ ⋅ 1 ⋅
1 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1
</syntaxhighlight>
 
=={{header|K}}==
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 1,581 ⟶ 2,164:
A = fliplr(A);
A(1:N+1:end) = 1;
end</langsyntaxhighlight>
{{Outputout}}
<pre>
>> diagdiag(7)
Line 1,615 ⟶ 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>
 
=={{header|Pascal}}==
<langsyntaxhighlight lang="pascal">program bidiagonaldiagonaldiagonal;
const N = 7;
type
Line 1,643 ⟶ 2,266:
end
end.
</syntaxhighlight>
</lang>
{{Output}}
<pre> 1 0 0 0 0 0 1
Line 1,656 ⟶ 2,279:
=={{header|Perl}}==
===Strings===
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; #https://rosettacode.org/wiki/Matrix_with_two_diagonals
Line 1,669 ⟶ 2,292:
1 while s/(?<=1...{$n})0/1/s or s/(?<=2.{$n})[01]/2/s;
return tr/2/1/r =~ s/\B/ /gr;
}</langsyntaxhighlight>
{{out}}
<pre>1 0 0 0 0 0 0 0 0 1
Line 1,695 ⟶ 2,318:
 
===Numbers===
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,711 ⟶ 2,334:
say join ' ', @$_ for dual_diagonal(4); say '';
say join ' ', @$_ for dual_diagonal(5);
</syntaxhighlight>
</lang>
{{out}}
<pre>1 0 0 1
Line 1,727 ⟶ 2,350:
===numbers===
Tee hee, pick the bones out of this one. ''Lovely.''
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">6</span> <span style="color: #008080;">to</span> <span style="color: #000000;">7</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">reinstate</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sq_mul</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),{{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}}}),{{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}}}),{</span><span style="color: #004600;">pp_Nest</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,750 ⟶ 2,373:
</pre>
Slightly saner, shows sackly same stuff:
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">6</span> <span style="color: #008080;">to</span> <span style="color: #000000;">7</span> <span style="color: #008080;">do</span>
Line 1,760 ⟶ 2,383:
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,{</span><span style="color: #004600;">pp_Nest</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
===strings===
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">6</span> <span style="color: #008080;">to</span> <span style="color: #000000;">7</span> <span style="color: #008080;">do</span>
Line 1,773 ⟶ 2,396:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,796 ⟶ 2,419:
Based on [[2048#Phix]], and uses the same colour and font choices.
You can run this online [http://phix.x10.mx/p2js/strange_identity_matrices.htm here].
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 1,808 ⟶ 2,431:
Press 'X' for two diagonals
The window can be fully resized
(MINSIZE does not yet work in a browser)
"""</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">help</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupMessage</span><span style="color: #0000FF;">(</span><span style="color: #000000;">TITLE</span><span style="color: #0000FF;">,</span><span style="color: #000000;">HELPTXT</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bWrap</span><span style="color: #0000FF;">:=</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULTIUP_IGNORE</span> <span style="color: #000080;font-style:italic;">-- (don't open browser help!)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
Line 1,903 ⟶ 2,525:
<span style="color: #008080;">function</span> <span style="color: #000000;">key_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #004600;">K_ESC</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">IUP_CLOSE</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #000080;font-style:italic;">-- (standard practice for me)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #004600;">K_F5</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #000080;font-style:italic;">-- (let browser reload work)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #004600;">K_F1</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">help</span><span style="color: #0000FF;">()</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'+'</span> <span style="color: #008080;">then</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
Line 1,910 ⟶ 2,533:
<span style="color: #008080;">if</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"MOX"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #000000;">mtype</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">c</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">IupUpdate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_CONTINUEIUP_IGNORE</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">canvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupCanvas</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"RASTERSIZE=532x532"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetCallbackIupSetCallbacks</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"MAP_CB"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"map_cb"</span><span style="color: #0000FF;">)),</span>
<span style="color: #7060A8;">IupSetCallback</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"ACTION"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"redraw_cb"</span><span style="color: #0000FF;">)})</span>
<span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"MINSIZE=440x450"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetCallback</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"K_ANY"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"key_cb"</span><span style="color: #0000FF;">))</span>
Line 1,926 ⟶ 2,549:
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php"><?php
 
$n = 9; // the number of rows
Line 1,939 ⟶ 2,562:
echo "\n";
}
</syntaxhighlight>
</lang>
{{Output}}
<pre> 1 0 0 0 0 0 0 0 1
Line 1,951 ⟶ 2,574:
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>
 
=={{header|Processing}}==
<syntaxhighlight lang="java">
//Aamrun, 27th June 2022
 
size(1000,1000);
 
textSize(50);
 
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
noFill();
square(i*100,j*100,100);
fill(#000000);
if(i==j||i+j==9){
text("1",i*100+50,j*100+50);
}
else{
text("0",i*100+50,j*100+50);
}
}
}
</syntaxhighlight>
 
=={{header|Python}}==
===Pure Python===
<langsyntaxhighlight lang="python">'''Matrix with two diagonals'''
 
 
Line 2,005 ⟶ 2,710:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>1 0 0 0 0 0 1
Line 2,026 ⟶ 2,731:
===NumPy===
{{libheader|numpy}}
<langsyntaxhighlight Pythonlang="python">import numpy as np
 
def diagdiag(n):
Line 2,037 ⟶ 2,742:
Returns:
a (numpy matrix): double diagonal matrix.
 
"""
d = np.eye(n)
Line 2,046 ⟶ 2,750:
return a
 
print(diagdiag(7))</langsyntaxhighlight>
{{Output}}
<pre>
Line 2,056 ⟶ 2,760:
[0. 1. 0. 0. 0. 1. 0.]
[1. 0. 0. 0. 0. 0. 1.]]
</pre>
 
=={{header|Quackery}}==
<syntaxhighlight lang="quackery"> [ [] swap dup times
[ 0 over of
1 swap i poke
1 swap i^ poke
nested rot join swap ]
drop ] is two-diagonals ( n --> [ )
 
8 two-diagonals
witheach
[ witheach [ echo sp ] cr ]
cr
9 two-diagonals
witheach
[ witheach [ echo sp ] cr ]
</syntaxhighlight>
 
{{out}}
 
<pre>1 0 0 0 0 0 0 1
0 1 0 0 0 0 1 0
0 0 1 0 0 1 0 0
0 0 0 1 1 0 0 0
0 0 0 1 1 0 0 0
0 0 1 0 0 1 0 0
0 1 0 0 0 0 1 0
1 0 0 0 0 0 0 1
 
1 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 1 0
0 0 1 0 0 0 1 0 0
0 0 0 1 0 1 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 1 0 1 0 0 0
0 0 1 0 0 0 1 0 0
0 1 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 1
</pre>
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>sub dual-diagonal($n) { ([1, |(0 xx $n-1)], *.rotate(-1) … *[*-1]).map: { [$_ Z|| .reverse] } }
 
.say for dual-diagonal(6);
say '';
.say for dual-diagonal(7);</langsyntaxhighlight>
{{out}}
<pre>[1 0 0 0 0 1]
Line 2,081 ⟶ 2,824:
 
=={{header|Red}}==
<langsyntaxhighlight lang="rebol">Red[]
 
x-matrix: function [size][
Line 2,095 ⟶ 2,838:
x-matrix 6
prin newline
x-matrix 7</langsyntaxhighlight>
{{out}}
<pre>
Line 2,115 ⟶ 2,858:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Identity Matrix
# Date : 2022/16/02
Line 2,182 ⟶ 2,925:
next
score = 0
</syntaxhighlight>
</lang>
Outpu image:
<br>
[http://keptarhely.eu/view.php?file=20220217v00xdle18.jpeg Special identity matrix with two diagonals]
 
=={{header|RPL}}==
The 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
 
5 XDIAG
6 XDIAG
{{out}}
<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>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'matrix'
 
class Matrix
def self.two_diagonals(n)
Matrix.build(n, n) do |row, col|
row == col || row == n-col-1 ? 1 : 0
end
end
end
 
Matrix.two_diagonals(5).row_vectors.each{|row| puts row.to_a.join(" ") }</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|Sidef}}==
<syntaxhighlight lang="ruby">func dual_diagonal(n) {
n.of {|k|
var r = (k.of(0) + [1] + (n - k - 1).of(0))
r ~Z| r.reverse
}
}
 
dual_diagonal(5).each{.join(' ').say}; say ''
dual_diagonal(6).each{.join(' ').say}</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
 
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|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">fn special_matrix(n int) {
for i in 0..n {
for j in 0..n {
if i == j || i+j == n-1 {
print("1 ")
} else {
print("0 ")
}
}
println('')
}
}
fn main() {
special_matrix(6) // even n
println('')
special_matrix(5) // odd n
}</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 1
0 1 0 1 0
0 0 1 0 0
0 1 0 1 0
1 0 0 0 1</pre>
 
=={{header|Wren}}==
A terminal based solution as I don't like asking people to view external images.
<langsyntaxhighlight ecmascriptlang="wren">var specialMatrix = Fn.new { |n|
for (i in 0...n) {
for (j in 0...n) {
Line 2,200 ⟶ 3,052:
specialMatrix.call(6) // even n
System.print()
specialMatrix.call(7) // odd n</langsyntaxhighlight>
 
{{out}}
Line 2,221 ⟶ 3,073:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">proc DrawMat(S);
int S, I, J;
[for I:= 0 to S-1 do
Line 2,231 ⟶ 3,083:
[DrawMat(6); CrLf(0);
DrawMat(7); CrLf(0);
]</langsyntaxhighlight>
 
{{out}}
337

edits