Matrix with two diagonals: Difference between revisions

m
Added Easylang
No edit summary
m (Added Easylang)
 
(98 intermediate revisions by 30 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}}==
<syntaxhighlight lang="ada">with Ada.Text_Io; use Ada.Text_Io;
with Ada.Command_Line;
 
procedure Matrix_With_Diagonals is
 
type Matrix_Type is array (Positive range <>, Positive range <>) of Character;
 
function Matrix_X (Length : Natural) return Matrix_Type is
begin
return M : Matrix_Type (1 .. Length, 1 .. Length) do
for Row in M'Range (1) loop
for Col in M'Range (2) loop
M (Row, Col) := (if
Row = Col or else
Row = M'Last (2) - (Col - M'First (1))
then '1' else '0');
end loop;
end loop;
end return;
end Matrix_X;
 
procedure Put (M : Matrix_Type) is
begin
for Row in M'Range (1) loop
for Col in M'Range (2) loop
Put (' ');
Put (M (Row, Col));
end loop;
New_Line;
end loop;
end Put;
 
begin
Put (Matrix_X (Length => Natural'Value (Ada.Command_Line.Argument (1))));
 
exception
when others =>
Put_Line ("Usage: ./matrix_with_diagonals <side-length>");
 
end Matrix_With_Diagonals;</syntaxhighlight>
 
=={{header|ALGOL 68}}==
<syntaxhighlight 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:
BEGIN
INT l pos := 1;
INT r pos := n;
FOR i TO n DO
FOR j TO n DO
print( ( " ", whole( ABS ( j = l pos OR j = rpos ), 0 ) ) )
OD;
print( ( newline ) );
l pos +:= 1;
r pos -:= 1
OD
END # draw 2 diagonals # ;
# test the draw 2 diagonals procedure #
draw 2 diagonals( 10 );
print( ( newline ) );
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>
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|APL}}==
{{works with|Dyalog APL}}
<syntaxhighlight lang="apl">twoDiagonals ← (⌽∨⊢)∘.=⍨∘⍳
twoDiagonals¨ 10 11</syntaxhighlight>
{{out}}
<pre> 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 1 0 0 1 0 0 0 0 0 0 0 1 0
0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0
0 0 0 1 0 0 1 0 0 0 0 0 0 1 0 0 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 0 0 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0
0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0
0 1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0
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===
<syntaxhighlight lang="applescript">on twoDiagonalMatrix(n)
if (n < 2) then error "twoDiagonalMatrix() handler: parameter must be > 1."
set digits to {}
set oddness to n mod 2
repeat (n - 1 + oddness) times
set end of digits to 0
end repeat
set m to n div 2 + oddness -- Middle index of digit source list.
set item m of digits to 1
set matrix to {}
set leftLen to m - 1 -- Length of left end of each row - 1.
set rightLen to leftLen - oddness -- Length of right end ditto.
-- Assemble the first m rows from the relevant sections of 'digits'.
repeat with i from m to 1 by -1
set end of matrix to items i thru (i + leftLen) of digits & items -(i + rightLen) thru -i of digits
end repeat
-- The remaining rows are the reverse of these, not repeating the mth where n is odd.
return matrix & reverse of items 1 thru (m - oddness) of matrix
end twoDiagonalMatrix
 
-- Task code.
on matrixToText(matrix, separator)
copy matrix to matrix
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to separator
repeat with thisLine in matrix
set thisLine's contents to thisLine as text
end repeat
set AppleScript's text item delimiters to linefeed
set matrix to matrix as text
set AppleScript's text item delimiters to astid
return matrix
end matrixToText
 
return linefeed & matrixToText(twoDiagonalMatrix(7), space) & ¬
(linefeed & linefeed & matrixToText(twoDiagonalMatrix(8), space))</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">"
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
 
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"</syntaxhighlight>
 
===Functional===
<syntaxhighlight lang="applescript">---------------- MATRIX WITH TWO DIAGONALS ---------------
 
-- bothDiagonals :: Int -> [[Int]]
on bothDiagonals(n)
-- A square matrix of dimension n with ones
-- along both diagonals, and zeros elsewhere.
script idOrReflection
on |λ|(x, y)
({y, 1 + n - y} contains x) as integer
end |λ|
end script
matrix(n, n, idOrReflection)
end bothDiagonals
 
 
--------------------------- TEST -------------------------
on run
-- Two diagonal matrices of dimensions 7 and 8
unlines(map(compose(showMatrix, bothDiagonals), ¬
{7, 8}))
end run
 
 
------------------------- GENERIC ------------------------
 
-- compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
on compose(f, g)
script
property mf : mReturn(f)
property mg : mReturn(g)
on |λ|(x)
mf's |λ|(mg's |λ|(x))
end |λ|
end script
end compose
 
 
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
if m ≤ n then
set xs to {}
repeat with i from m to n
set end of xs to i
end repeat
xs
else
{}
end if
end enumFromTo
 
 
-- matrix :: Int -> Int -> ((Int, Int) -> a) -> [[a]]
on matrix(nRows, nCols, f)
-- A matrix of a given number of columns and rows,
-- in which each value is a given function of its
-- (zero-based) column and row indices.
script go
property g : mReturn(f)'s |λ|
on |λ|(iRow)
set xs to {}
repeat with iCol from 1 to nCols
set end of xs to g(iRow, iCol)
end repeat
xs
end |λ|
end script
map(go, enumFromTo(1, nRows))
end matrix
 
 
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
-- The list obtained by applying f
-- to each element of xs.
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
 
 
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
-- 2nd class handler function lifted into 1st class script wrapper.
if script is class of f then
f
else
script
property |λ| : f
end script
end if
end mReturn
 
 
-- showMatrix :: [[a]] -> String
on showMatrix(rows)
-- String representation of a matrix.
script
on |λ|(cells)
unwords(map(my str, cells))
end |λ|
end script
unlines(map(result, rows)) & linefeed
end showMatrix
 
 
-- str :: a -> String
on str(x)
x as string
end str
 
 
-- unlines :: [String] -> String
on unlines(xs)
-- A single string formed by the intercalation
-- of a list of strings with the newline character.
set {dlm, my text item delimiters} to ¬
{my text item delimiters, linefeed}
set s to xs as text
set my text item delimiters to dlm
s
end unlines
 
 
-- unwords :: [String] -> String
on unwords(xs)
set {dlm, my text item delimiters} to ¬
{my text item delimiters, space}
set s to xs as text
set my text item delimiters to dlm
return s
end unwords</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
 
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</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">
# syntax: GAWK -f MATRIX_WITH_TWO_DIAGONALS.AWK
BEGIN {
for (n=6; n<=7; n++) {
for (i=1; i<=n; i++) {
for (j=1; j<=n; j++) {
tmp = (i==j || i+j==n+1) ? 1 : 0
printf("%2d",tmp)
}
printf("\n")
}
print("")
}
exit(0)
}
</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|Basic}}==
<syntaxhighlight lang="qbasic">100 REM
110 REM DIAGONAL-DIAGONAL MATRIX
120 REM
130 LET N = 7
140 DIM A(N,N)
150 FOR I=1 TO N
150 FOR J=1 TO N
160 LET A(I,J) = 0
170 NEXT J
180 LET J1 = I
190 LET J2 = N - I + 1
200 LET A(I,J1) = 1
210 LET A(I,J2) = 1
220 NEXT I
230 REM
240 FOR I=1 TO N
250 FOR J=1 TO N
260 PRINT " ";A(I,J);
270 NEXT J
280 PRINT
290 NEXT I
300 END
</syntaxhighlight>
{{Output}}
<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|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 28 ⟶ 680:
specialMatrix(11); // odd n
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 55 ⟶ 707:
1 0 0 0 0 0 0 0 0 0 1
</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <concepts>
#include <iostream>
 
// Print each element of a matrix according to a predicate. It
// will print a '1' if the predicate function is true, otherwise '0'.
void PrintMatrix(std::predicate<int, int, int> auto f, int size)
{
for(int y = 0; y < size; y++)
{
for(int x = 0; x < size; x++)
{
std::cout << " " << f(x, y, size);
}
std::cout << "\n";
}
std::cout << "\n";
}
 
int main()
{
// a lambda to create diagonals
auto diagonals = [](int x, int y, int size)
{
return x == y || ((size - x - 1) == y);
};
PrintMatrix(diagonals, 8);
PrintMatrix(diagonals, 9);
}
 
</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|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|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}}==
===LAMBDA===
 
Excel can lift functions over scalar values to functions over two-dimensional arrays.
 
Here we bind the name ''TwoDiagonalMatrix'' to a lambda expression in the Name Manager of the Excel WorkBook:
 
(See [https://www.microsoft.com/en-us/research/blog/lambda-the-ultimatae-excel-worksheet-function/ LAMBDA: The ultimate Excel worksheet function])
 
{{Works with|Office 365 betas 2021}}
<syntaxhighlight lang="lisp">TwoDiagonalMatrix
=LAMBDA(n,
LET(
ixs, SEQUENCE(n, n, 0, 1),
x, MOD(ixs, n),
y, QUOTIENT(ixs, n),
IF(x = y,
1,
IF(x = (n - y) - 1, 1, 0)
)
)
)</syntaxhighlight>
{{Out}}
The formulae in cells A2 and B2 populate the whole of each adjacent matrix.
{| class="wikitable"
|-
|||style="text-align:right; font-family:serif; font-style:italic; font-size:120%;"|fx
! colspan="9" style="text-align:left; vertical-align: bottom; font-family:Arial, Helvetica, sans-serif !important;"|=TwoDiagonalMatrix(A2)
|- style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff;"
|
| A
| B
| C
| D
| E
| F
| G
| H
| I
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 1
| N
| Matrix
|
|
|
|
|
|
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 2
| style="text-align:right" | 7
| style="text-align:right; background-color:#cbcefb" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 3
|
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 4
|
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 5
|
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 6
|
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 7
|
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 8
|
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 9
|
|
|
|
|
|
|
|
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 10
| style="text-align:right" | 8
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 11
|
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 12
|
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 13
|
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 14
|
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 15
|
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 16
|
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 17
|
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
|}
 
 
Recent builds of Excel have also introduced a '''MAKEARRAY''' function, which takes a lambda expression as an argument.
 
Binding the name ''bothDiagonalMatrix'' in the Excel Name Manager:
<syntaxhighlight lang="lisp">bothDiagonalMatrix
=LAMBDA(n,
MAKEARRAY(
n, n,
LAMBDA(
x, y,
INT(OR(
x = y,
x = (1 + n - y)
))
)
)
)</syntaxhighlight>
{{Out}}
{| class="wikitable"
|-
|||style="text-align:right; font-family:serif; font-style:italic; font-size:120%;"|fx
! colspan="9" style="text-align:left; vertical-align: bottom; font-family:Arial, Helvetica, sans-serif !important;"|=bothDiagonalMatrix(A2)
|- style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff;"
|
| A
| B
| C
| D
| E
| F
| G
| H
| I
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 1
| N
| Matrix
|
|
|
|
|
|
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 2
| style="text-align:right" | 7
| style="text-align:right; background-color:#cbcefb" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 3
|
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 4
|
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 5
|
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 6
|
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 7
|
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 8
|
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 9
|
|
|
|
|
|
|
|
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 10
| style="text-align:right" | 8
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 11
|
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 12
|
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 13
|
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 14
|
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 15
|
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 16
|
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
| style="text-align:right" | 0
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 17
|
| style="text-align:right" | 1
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 0
| style="text-align:right" | 1
|}
 
=={{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 77 ⟶ 1,523:
[1; 0; 0; 0; 0; 1]]
</pre>
 
=={{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 85 ⟶ 1,532:
 
6 <x-matrix> simple-table. nl
7 <x-matrix> simple-table.</langsyntaxhighlight>
{{out}}
<pre>
Line 103 ⟶ 1,550:
1 0 0 0 0 0 1
</pre>
 
=={{header|FreeBASIC}}==
===Text based===
<syntaxhighlight lang="freebasic">Sub twoDiagonalMatrix(n As Integer)
For i As Integer = 1 To n
For j As Integer = 1 To n
Print Iif((i = j) Or (i + j = n + 1), "1 ", "0 ");
Next j
Print
Next i
End Sub
 
twoDiagonalMatrix(6)
Print
twoDiagonalMatrix(7)
Sleep</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>
 
===Graphical===
<syntaxhighlight lang="freebasic">Dim As Integer n = 8, size = 60 * n + 70
Screenres size, size, 24
Cls
Windowtitle "Matrix with two diagonals"
 
Dim As Integer beige = Rgb(245, 245, 220), brown = Rgb(171, 82, 54)
 
For x As Integer = 0 To n
For y As Integer = 0 To n
Dim As Integer cx = x*60 + 10
Dim As Integer cy = y*60 + 10
'If (x + y) Mod 2 = 0 Then
If (x = y) Or (x + y = n) Then
Line (cx,cy) - (cx+50, cy+50), brown, BF
Draw String (cx + 20, cy + 20), "1", beige
Else
Line (cx,cy) - (cx+50, cy+50), beige, BF
End If
Next y
Next x
Bsave "twoDiagonalMatrix.bmp",0
Sleep</syntaxhighlight>
{{out}}
https://www.dropbox.com/s/ph9r28gpkp8ao8n/twoDiagonalMatrix.bmp?dl=0
 
=={{header|Fortran}}==
===Free form===
{{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.
<syntaxhighlight lang="fortran">program prog
 
dimension a(100, 100)
 
n = 7
j1 = 1
j2 = n
do i = 1, n
do j = 1, n
a(i, j) = 0.
end do
a(i, j1) = 1
a(i, j2) = 1
j1 = j1 + 1
j2 = j2 - 1
end do
 
do i = 1, n
print *, (a(i, j), j=1,n)
end do
 
end
</syntaxhighlight>
{{Output}}
<pre>
1.00000000 0.00000000E+00 0.00000000E+00 0.00000000E+00 0.00000000E+00 0.00000000E+00 1.00000000
0.00000000E+00 1.00000000 0.00000000E+00 0.00000000E+00 0.00000000E+00 1.00000000 0.00000000E+00
0.00000000E+00 0.00000000E+00 1.00000000 0.00000000E+00 1.00000000 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 0.00000000E+00 1.00000000 0.00000000E+00 0.00000000E+00 0.00000000E+00
0.00000000E+00 0.00000000E+00 1.00000000 0.00000000E+00 1.00000000 0.00000000E+00 0.00000000E+00
0.00000000E+00 1.00000000 0.00000000E+00 0.00000000E+00 0.00000000E+00 1.00000000 0.00000000E+00
1.00000000 0.00000000E+00 0.00000000E+00 0.00000000E+00 0.00000000E+00 0.00000000E+00 1.00000000
</pre>
===Fixed form===
{{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.
<syntaxhighlight lang="fortran">C DIAGONAL-DIAGONAL MATRIX IN FORTRAN 77
 
PROGRAM PROG
DIMENSION A(100, 100)
N = 7
 
A = 0.
DO 10 I = 1, N
A(I, I) = 1.
10 A(I, N - I + 1) = 1.
DO 20 I = 1, N
20 PRINT *, (A(I, J), J=1,N)
END</syntaxhighlight>
 
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 127 ⟶ 1,690:
fmt.Println()
specialMatrix(9) // odd n
}</langsyntaxhighlight>
 
{{out}}
Line 152 ⟶ 1,715:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">---------------- MATRIX WITH TWO DIAGONALS ---------------
 
twoDiagonalMatrix :: Int -> [[Int]]
Line 169 ⟶ 1,732:
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 190 ⟶ 1,753:
unlines . fmap (((' ' :) . show) =<<)
. twoDiagonalMatrix
<$> [7, 8]</langsyntaxhighlight>
{{Out}}
<pre> 1 0 0 0 0 0 1
Line 209 ⟶ 1,772:
1 0 0 0 0 0 0 1</pre>
 
=={{header|J}}==
 
and in terms of the Data.Matrix library:
<syntaxhighlight lang="haskell">import Data.Matrix
 
twoDiagonals :: Int -> Matrix Int
twoDiagonals n =
matrix
n
n
(\(a, b) -> fromEnum $ a `elem` [b, succ (n - b)])
 
main :: IO ()
main =
mapM_ print $ twoDiagonals <$> [7, 8]</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 │
└ ┘
┌ ┐
│ 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 │
└ ┘</pre>
 
=={{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 219 ⟶ 1,816:
Some examples:
 
<langsyntaxhighlight Jlang="j"> task 2
1 1
1 1
Line 243 ⟶ 1,840:
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.
<syntaxhighlight lang="java">package example.diagdiag;
 
public class Program {
 
public static void main(String[] args) {
DiagonalDiagonalMatrix A = new DiagonalDiagonalMatrix(7);
System.out.println(A);
}
 
}
 
class DiagonalDiagonalMatrix {
 
final int n;
private double[][] a = null;
 
public Matrix(int n) {
this.n = n;
}
 
public double get(int i, int j) {
if (a == null) {
return (i == j || i == n - j + 1) ? 1.0 : 0.0;
} else {
return a[i - 1][j - 1];
}
}
 
// Not necessary for the task: a lazy creation of the dense matrix.
//
// public void put(int i, int j, double value) {
// if (a == null) {
// a = new double[n][n];
// for (int p = 1; p <= n; i++) {
// for (int q = 1; q <= n; j++) {
// a[p - 1][q - 1] = get(p, q);
// }
// }
// }
// a[i - 1][j - 1] = value;
// }
 
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
sb.append('\t');
sb.append(get(i, j));
}
sb.append('\n');
}
return sb.toString();
}
 
}</syntaxhighlight>
{{Output}}
<pre> 1.0 0.0 0.0 0.0 0.0 0.0 1.0
0.0 1.0 0.0 0.0 0.0 1.0 0.0
0.0 0.0 1.0 0.0 1.0 0.0 0.0
0.0 0.0 0.0 1.0 0.0 0.0 0.0
0.0 0.0 1.0 0.0 1.0 0.0 0.0
0.0 1.0 0.0 0.0 0.0 1.0 0.0
1.0 0.0 0.0 0.0 0.0 0.0 1.0</pre>
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">(() => {
"use strict";
 
Line 259 ⟶ 1,923:
return xs.map(
y => xs.map(
x => [y, n - y + 1].includes(x) ? Number(
[y, 1 + n - y].includes(x)
) : 0
)
);
Line 288 ⟶ 1,952:
// String representation of a matrix.
rows.map(
row => row.map(cell => `${cell}`String).join(" ")
).join("\n");
 
Line 294 ⟶ 1,958:
// MAIN --
return main();
})();</langsyntaxhighlight>
 
 
Or, in terms of a more general matrix function:
<syntaxhighlight lang="javascript">(() => {
"use strict";
 
// ------------ MATRIX WITH TWO DIAGONALS ------------
 
// bothDiagonals :: Int -> [[Int]]
const bothDiagonals = n =>
matrix(n)(n)(
y => x => Number(
[y, (n - y) - 1].includes(x)
)
);
 
 
// ---------------------- TEST -----------------------
const main = () => [7, 8].map(
compose(
showMatrix,
bothDiagonals
)
).join("\n\n");
 
 
// --------------------- GENERIC ---------------------
 
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
const compose = (...fs) =>
// A function defined by the right-to-left
// composition of all the functions in fs.
fs.reduce(
(f, g) => x => f(g(x)),
x => x
);
 
 
// matrix Int -> Int -> (Int -> Int -> a) -> [[a]]
const matrix = nRows => nCols =>
// A matrix of a given number of columns and rows,
// in which each value is a given function of its
// (zero-based) column and row indices.
f => Array.from({
length: nRows
}, (_, iRow) => Array.from({
length: nCols
}, (__, iCol) => f(iRow)(iCol)));
 
 
// showMatrix :: [[a]] -> String
const showMatrix = rows =>
// String representation of a matrix.
rows.map(
row => row.map(String).join(" ")
).join("\n");
 
// MAIN ---
return main();
})();</syntaxhighlight>
{{Out}}
<pre>1 0 0 0 0 0 1
Line 312 ⟶ 2,036:
0 1 0 0 0 0 1 0
1 0 0 0 0 0 0 1</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq">def bidiagonal_matrix:
. as $n
| [range(0; $n) | 0] as $z
| reduce range(0; $n) as $i ([];
. + [$z | .[$i] = 1 | .[$n-$i-1] = 1] );
 
def display:
map(join(" ")) | join("\n");</syntaxhighlight>
'''Example'''
<syntaxhighlight lang="jq">9|bidiagonal_matrix|display</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
</pre>
 
=={{header|Julia}}==
<syntaxhighlight 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)
 
julia> twodiagonalmat(1)
1×1 Matrix{Int64}:
1
 
julia> twodiagonalmat(2)
2×2 Matrix{Int64}:
1 1
1 1
 
julia> twodiagonalmat(3)
3×3 Matrix{Int64}:
1 0 1
0 1 0
1 0 1
 
julia> twodiagonalmat(4)
4×4 Matrix{Int64}:
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1
 
julia> twodiagonalmat(5)
5×5 Matrix{Int64}:
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
</syntaxhighlight>
=== Sparse matrix version ===
<syntaxhighlight lang="julia">julia> using LinearAlgebra
 
julia> twodiagonalsparse(n) = I(n) .| rotl90(I(n))
twodiagonalsparse (generic function with 1 method)
 
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.
%
% Parameters:
%
% N -- number of rows (columns);
% sparse -- should be true to create a sparse matrix,
% default false (dense matrix).
%
% Return:
%
% A matrix where all elements A(i, j) are zero except
% elements on the diagonal or on the back-diagonal.
% The diagonal (and back-diagonal) elements are equal 1.
if nargin < 2
sparse = false;
end
if sparse
A = speye(N);
else
A = eye(N);
end
A = fliplr(A);
A(1:N+1:end) = 1;
end</syntaxhighlight>
{{out}}
<pre>
>> diagdiag(7)
 
ans =
 
Columns 1 through 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
 
>> diagdiag(7, true)
 
ans =
 
(1,1) 1
(7,1) 1
(2,2) 1
(6,2) 1
(3,3) 1
(5,3) 1
(4,4) 1
(3,5) 1
(5,5) 1
(2,6) 1
(6,6) 1
(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}}==
<syntaxhighlight lang="pascal">program diagonaldiagonal;
const N = 7;
type
index = 1..N;
var
a : array[index, index] of real;
i, j, j1, j2 : index;
begin
for i := 1 to N do
begin
for j := 1 to N do
a[i, j] := 0.0;
j1 := i;
j2 := N - i + 1;
a[i, j1] := 1.0;
a[i, j2] := 1.0;
end;
for i := 1 to N do
begin
for j := 1 to N do
write(a[i, j]:2:0);
writeln();
end
end.
</syntaxhighlight>
{{Output}}
<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|Perl}}==
===Strings===
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; #https://rosettacode.org/wiki/Matrix_with_two_diagonals
Line 328 ⟶ 2,319:
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 354 ⟶ 2,345:
 
===Numbers===
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 370 ⟶ 2,361:
say join ' ', @$_ for dual_diagonal(4); say '';
say join ' ', @$_ for dual_diagonal(5);
</syntaxhighlight>
</lang>
{{out}}
<pre>1 0 0 1
Line 384 ⟶ 2,375:
 
=={{header|Phix}}==
===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 408 ⟶ 2,400:
</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 418 ⟶ 2,410:
<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===
<!--<syntaxhighlight lang="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: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">),</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">j</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'1'</span>
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][-</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'1'</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<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>
<!--</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>
===GUI/online===
{{libheader|Phix/pGUI}}
{{libheader|Phix/online}}
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].
<!--<syntaxhighlight lang="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>
<span style="color: #008080;">constant</span> <span style="color: #000000;">TITLE</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"Strange Identity Matrices"</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">HELPTXT</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
Press F1 to see this help text.
Press +/- to increase or decrease the matrix size
Press 'M' for a mosaic pattern
Press 'O' for outer or four sides of a square
Press 'X' for two diagonals
The window can be fully resized
"""</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_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>
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">dlg</span>
<span style="color: #004080;">cdCanvas</span> <span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cdcanvas</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">board</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">mtype</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'X'</span> <span style="color: #000080;font-style:italic;">-- (two diags) or 'O'uter or 'M'osaic</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">mosaic</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">+</span><span style="color: #7060A8;">odd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">y</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">by</span> <span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">y</span><span style="color: #0000FF;">][</span><span style="color: #000000;">x</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"1"</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #008000;">"mosaic"</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">outer</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">step</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">or</span> <span style="color: #000000;">y</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;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">by</span> <span style="color: #000000;">step</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">y</span><span style="color: #0000FF;">][</span><span style="color: #000000;">x</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"1"</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #008000;">"four sides"</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">diag</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"1"</span>
<span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][-</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"1"</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #008000;">"two diagonals"</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">redraw_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;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">dw</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dh</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGetIntInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"DRAWSIZE"</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">ms</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">*</span><span style="color: #000000;">5</span><span style="color: #0000FF;">+</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- margin space</span>
<span style="color: #000000;">mx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dw</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dh</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">ms</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- max size</span>
<span style="color: #000000;">ts</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mx</span><span style="color: #0000FF;">/</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span> <span style="color: #000080;font-style:italic;">-- max tile size</span>
<span style="color: #000000;">th</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ts</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span> <span style="color: #000080;font-style:italic;">-- for text posn</span>
<span style="color: #000000;">os</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ts</span><span style="color: #0000FF;">*</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">ms</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- overall size</span>
<span style="color: #000000;">ox</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">((</span><span style="color: #000000;">dw</span><span style="color: #0000FF;">-</span><span style="color: #000000;">os</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span> <span style="color: #000080;font-style:italic;">-- top right x</span>
<span style="color: #000000;">oy</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">((</span><span style="color: #000000;">dh</span><span style="color: #0000FF;">-</span><span style="color: #000000;">os</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span> <span style="color: #000080;font-style:italic;">-- top right y</span>
<span style="color: #000000;">font_size</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">((</span><span style="color: #000000;">ts</span><span style="color: #0000FF;">+</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">board</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: #008000;">"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: #004080;">string</span> <span style="color: #000000;">title</span>
<span style="color: #008080;">switch</span> <span style="color: #000000;">mtype</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">case</span> <span style="color: #008000;">'M'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">title</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">mosaic</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">case</span> <span style="color: #008000;">'O'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">title</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">outer</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">case</span> <span style="color: #008000;">'X'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">title</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">diag</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">switch</span>
<span style="color: #7060A8;">IupSetStrAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"TITLE"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s (%dx%d, %s)"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">TITLE</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: #000000;">title</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">cdCanvasActivate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasSetBackground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">#FAF8EF</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasClear</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasSetForeground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">#BBADA0</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasRoundedBox</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ox</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ox</span><span style="color: #0000FF;">+</span><span style="color: #000000;">os</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">oy</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">oy</span><span style="color: #0000FF;">+</span><span style="color: #000000;">os</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasFont</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Calibri"</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">CD_BOLD</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">font_size</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">tx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ox</span><span style="color: #0000FF;">+</span><span style="color: #000000;">5</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">ty</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">oy</span><span style="color: #0000FF;">+</span><span style="color: #000000;">5</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">bxy</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">x</span><span style="color: #0000FF;">][</span><span style="color: #000000;">y</span><span style="color: #0000FF;">]</span>
<span style="color: #7060A8;">cdCanvasSetForeground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">#EEE4DA</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasRoundedBox</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tx</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tx</span><span style="color: #0000FF;">+</span><span style="color: #000000;">ts</span><span style="color: #0000FF;">-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ty</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ty</span><span style="color: #0000FF;">+</span><span style="color: #000000;">ts</span><span style="color: #0000FF;">-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasSetForeground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">#776E65</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasText</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tx</span><span style="color: #0000FF;">+</span><span style="color: #000000;">th</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ty</span><span style="color: #0000FF;">+</span><span style="color: #000000;">th</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">bxy</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">ty</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">ts</span><span style="color: #0000FF;">+</span><span style="color: #000000;">5</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">tx</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">ts</span><span style="color: #0000FF;">+</span><span style="color: #000000;">5</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">cdCanvasFlush</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">map_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000000;">ih</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">cdcanvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_IUP</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ih</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">cddbuffer</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_DBUFFER</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cdcanvas</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasSetTextAlignment</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">CD_CENTER</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<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>
<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: #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: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">upper</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
<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_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;">IupSetCallbacks</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: #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>
<span style="color: #7060A8;">IupSetAttributeHandle</span><span style="color: #0000FF;">(</span><span style="color: #004600;">NULL</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"PARENTDIALOG"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"RASTERSIZE"</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">NULL</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</syntaxhighlight>-->
 
=={{header|PHP}}==
<syntaxhighlight lang="php"><?php
 
$n = 9; // the number of rows
 
for ($i = 1; $i <= $n; $i++) {
for ($j = 1; $j <= $n; $j++) {
echo ($i == $j || $i == $n - $j + 1) ? ' 1' : ' 0';
}
echo "\n";
}
</syntaxhighlight>
{{Output}}
<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
</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===
<lang python>'''Matrix with two diagonals'''
<syntaxhighlight lang="python">'''Matrix with two diagonals'''
 
 
Line 429 ⟶ 2,694:
along both diagonals, and zeros elsewhere.
'''
xsreturn = rangematrix(1, 1 + n)
n, n, lambda row, col: int(
return [
[ row in (col, 1 + (n - col))
)
1 if x in (y, (n - y) + 1) else 0
)
for x in xs
]
for y in xs
]
 
 
Line 452 ⟶ 2,714:
 
# ----------------------- GENERIC ------------------------
 
# matrix :: Int -> Int -> ((Int, Int) -> a) -> [[a]]
def matrix(nRows, nCols, f):
'''A matrix of a given number of columns and rows,
in which each value is a given function over the
tuple of its (one-based) row and column indices.
'''
return [
[f(y, x) for x in range(1, 1 + nCols)]
for y in range(1, 1 + nRows)
]
 
 
# showMatrix :: [[Int]] -> String
def showMatrix(rows):
Line 462 ⟶ 2,737:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>1 0 0 0 0 0 1
Line 480 ⟶ 2,755:
0 1 0 0 0 0 1 0
1 0 0 0 0 0 0 1</pre>
 
===NumPy===
{{libheader|numpy}}
<syntaxhighlight lang="python">import numpy as np
 
def diagdiag(n):
"""
Create a diagonal-diagonal matrix
 
Args:
n (int): number of rows.
 
Returns:
a (numpy matrix): double diagonal matrix.
"""
d = np.eye(n)
a = d + np.fliplr(d)
if n % 2:
k = (n - 1) // 2
a[k, k] = 1
return a
 
print(diagdiag(7))</syntaxhighlight>
{{Output}}
<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|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 504 ⟶ 2,851:
 
=={{header|Red}}==
<langsyntaxhighlight lang="rebol">Red[]
 
x-matrix: function [size][
Line 518 ⟶ 2,865:
x-matrix 6
prin newline
x-matrix 7</langsyntaxhighlight>
{{out}}
<pre>
Line 538 ⟶ 2,885:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Identity Matrix
# Date : 2022/16/02
Line 605 ⟶ 2,952:
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 623 ⟶ 3,079:
specialMatrix.call(6) // even n
System.print()
specialMatrix.call(7) // odd n</langsyntaxhighlight>
 
{{out}}
Line 644 ⟶ 3,100:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">proc DrawMat(S);
int S, I, J;
[for I:= 0 to S-1 do
Line 654 ⟶ 3,110:
[DrawMat(6); CrLf(0);
DrawMat(7); CrLf(0);
]</langsyntaxhighlight>
 
{{out}}
2,041

edits