Matrix with two diagonals: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎Excel LAMBDA: Added a variant simplified by the newer MAKEARRAY function.)
Line 285: Line 285:
|||style="text-align:right; font-family:serif; font-style:italic; font-size:120%;"|fx
|||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)
! 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:
<lang lisp>bothDiagonalMatrix
=LAMBDA(n,
MAKEARRAY(
n, n,
LAMBDA(
x, y,
INT(OR(
x=y,
x=(1 + n - y)
))
)
)
)</lang>
{{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;"
|- style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff;"
|
|

Revision as of 12:37, 18 February 2022

Matrix with two diagonals is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task

Draw a square matrix which has 1's on both diagonals but 0's elsewhere.
If you can please use GUI

AppleScript

Procedural

<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))</lang>
Output:

<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"</lang>

Functional

<lang applescript>---------------- MATRIX WITH TWO DIAGONALS ---------------

-- doubleDiagonal :: Int -> Int on doubleDiagonal(n)

   -- A square matrix of dimension n with ones
   -- along both diagonals, and zeros elsewhere.
   
   set xs to enumFromTo(1, n)
   
   script idAndReflection
       on |λ|(y)
           script
               on |λ|(x)
                   ({y, 1 + n - y} contains x) as integer
               end |λ|
           end script
           
           map(result, xs)
       end |λ|
   end script
   
   map(idAndReflection, xs)

end doubleDiagonal



TEST -------------------------

on run

   script test
       on |λ|(n)
           showMatrix(doubleDiagonal(n)) & linefeed
       end |λ|
   end script
   
   unlines(map(test, {7, 8}))

end run



GENERIC ------------------------

-- 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


-- 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))

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</lang>

Output:
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

C

Translation of: Wren

<lang c>#include <stdio.h>

void specialMatrix(unsigned int n) {

   int i, j;
   for (i = 0; i < n; ++i) {
       for (j = 0; j < n; ++j) {
           if (i == j || i + j == n - 1) {
               printf("%d ", 1);
           } else {
               printf("%d ", 0);
           }
       }
       printf("\n");
   }

}

int main() {

   specialMatrix(10); // even n
   printf("\n");
   specialMatrix(11); // odd n
   return 0;

}</lang>

Output:
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 

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 LAMBDA: The ultimate Excel worksheet function)

<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)
       )
   )

)</lang>

Output:

The formulae in cells A2 and B2 populate the whole of each adjacent matrix.

fx =TwoDiagonalMatrix(A2)
A B C D E F G H I
1 N Matrix
2 7 1 0 0 0 0 0 1
3 0 1 0 0 0 1 0
4 0 0 1 0 1 0 0
5 0 0 0 1 0 0 0
6 0 0 1 0 1 0 0
7 0 1 0 0 0 1 0
8 1 0 0 0 0 0 1
9
10 8 1 0 0 0 0 0 0 1
11 0 1 0 0 0 0 1 0
12 0 0 1 0 0 1 0 0
13 0 0 0 1 1 0 0 0
14 0 0 0 1 1 0 0 0
15 0 0 1 0 0 1 0 0
16 0 1 0 0 0 0 1 0
17 1 0 0 0 0 0 0 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: <lang lisp>bothDiagonalMatrix =LAMBDA(n,

   MAKEARRAY(
       n, n, 
       LAMBDA(
           x, y, 
           INT(OR(
               x=y, 
               x=(1 + n - y)
           ))
       )
   )

)</lang>

Output:
fx =bothDiagonalMatrix(A2)
A B C D E F G H I
1 N Matrix
2 7 1 0 0 0 0 0 1
3 0 1 0 0 0 1 0
4 0 0 1 0 1 0 0
5 0 0 0 1 0 0 0
6 0 0 1 0 1 0 0
7 0 1 0 0 0 1 0
8 1 0 0 0 0 0 1
9
10 8 1 0 0 0 0 0 0 1
11 0 1 0 0 0 0 1 0
12 0 0 1 0 0 1 0 0
13 0 0 0 1 1 0 0 0
14 0 0 0 1 1 0 0 0
15 0 0 1 0 0 1 0 0
16 0 1 0 0 0 0 1 0
17 1 0 0 0 0 0 0 1

F#

<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) </lang>

Output:
[[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]]

Factor

Works with: Factor version 0.99 2021-06-02

<lang factor>USING: io kernel math math.matrices prettyprint ;

<x-matrix> ( n -- matrix )
   dup dup 1 - '[ 2dup = -rot + _ = or 1 0 ? ] <matrix-by-indices> ;

6 <x-matrix> simple-table. nl 7 <x-matrix> simple-table.</lang>

Output:
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

Go

Translation of: Wren

<lang go>package main

import "fmt"

func specialMatrix(n uint) {

   for i := uint(0); i < n; i++ {
       for j := uint(0); j < n; j++ {
           if i == j || i+j == n-1 {
               fmt.Printf("%d ", 1)
           } else {
               fmt.Printf("%d ", 0)
           }
       }
       fmt.Println()
   }

}

func main() {

   specialMatrix(8) // even n
   fmt.Println()
   specialMatrix(9) // odd n

}</lang>

Output:
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 

Haskell

<lang haskell>---------------- MATRIX WITH TWO DIAGONALS ---------------

twoDiagonalMatrix :: Int -> Int twoDiagonalMatrix n = flip (fmap . go) xs <$> xs

 where
   xs = [1 .. n]
   go x y
     | y == x = 1
     | y == succ (subtract x n) = 1
     | otherwise = 0

TEST -------------------------

main :: IO () main =

 mapM_ putStrLn $
   unlines . fmap (((' ' :) . show) =<<)
     . twoDiagonalMatrix
     <$> [7, 8]</lang>


Or, in the form of a list comprehension: <lang haskell>-------------- MATRIX WITH TWO DIAGONALS ---------------

twoDiagonalMatrix :: Int -> Int twoDiagonalMatrix n =

 let xs = [1 .. n]
  in [ [ fromEnum $ x `elem` [y, succ (n - y)]
         | x <- xs
       ]
       | y <- xs
     ]

TEST -------------------------

main :: IO () main =

 mapM_ putStrLn $
   unlines . fmap (((' ' :) . show) =<<)
     . twoDiagonalMatrix
     <$> [7, 8]</lang>
Output:
 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

J

Implementation:

<lang J>task=: Template:(+.</lang>

In other words, generate an order n identity matrix, flip it and (treating it as a bit matrix) OR the two matrices.

Some examples:

<lang J> task 2 1 1 1 1

  task 3

1 0 1 0 1 0 1 0 1

  task 4

1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1

  task 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

  task 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</lang>

JavaScript

<lang javascript>(() => {

   "use strict";
   // ------------ MATRIX WITH TWO DIAGONALS ------------
   // doubleDiagonal :: Int -> Int
   const doubleDiagonal = n => {
       // A square matrix of dimension n with ones
       // along both diagonals, and zeros elsewhere.
       const xs = enumFromTo(1)(n);
       return xs.map(
           y => xs.map(
               x => Number(
                   [y, 1 + n - y].includes(x)
               )
           )
       );
   };


   // ---------------------- TEST -----------------------
   const main = () => [7, 8].map(
       n => showMatrix(
           doubleDiagonal(n)
       )
   ).join("\n\n");


   // --------------------- GENERIC ---------------------
   // enumFromTo :: Int -> Int -> [Int]
   const enumFromTo = m =>
       n => Array.from({
           length: 1 + n - m
       }, (_, i) => m + i);


   // showMatrix :: a -> String
   const showMatrix = rows =>
       // String representation of a matrix.
       rows.map(
           row => row.map(String).join(" ")
       ).join("\n");


   // MAIN --
   return main();

})();</lang>

Output:
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

Perl

Strings

<lang perl>#!/usr/bin/perl

use strict; #https://rosettacode.org/wiki/Matrix_with_two_diagonals use warnings;

print diagonal($_), "\n" for 10, 11;

sub diagonal

 {
 my $n =  shift() - 1;
 local $_ = 1 . 0 x ($n - 1) . 2 . "\n" . (0 . 0 x $n . "\n") x $n;
 1 while s/(?<=1...{$n})0/1/s or s/(?<=2.{$n})[01]/2/s;
 return tr/2/1/r =~ s/\B/ /gr;
 }</lang>
Output:
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

Numbers

<lang perl>use strict; use warnings; use feature 'say';

sub dual_diagonal {

   my($n) = shift() - 1;
   my @m;
   for (0..$n) {
       my @rr = reverse my @r = ( (0) x $_, 1, (0) x ($n-$_) );
       push @m, [ map { $r[$_] or $rr[$_] } 0..$n ]
   }
   @m

}

say join ' ', @$_ for dual_diagonal(4); say ; say join ' ', @$_ for dual_diagonal(5); </lang>

Output:
1 0 0 1
0 1 1 0
0 1 1 0
1 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

Phix

Tee hee, pick the bones out of this one. Lovely.

with javascript_semantics
for n=6 to 7 do
    pp(apply(true,reinstate,{repeat(repeat(0,n),n),apply(true,sq_mul,{tagset(n),{{1,-1}}}),{{1,1}}}),{pp_Nest,1})
end for
Output:
{{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}}

Slightly saner, shows sackly same stuff:

with javascript_semantics
for n=6 to 7 do
    sequence s = repeat(repeat(0,n),n)
    for i=1 to n do
        s[i][i] = 1
        s[i][-i] = 1
    end for
    pp(s,{pp_Nest,1})
end for

Python

<lang python>Matrix with two diagonals


  1. twoDiagonalMatrix :: Int -> Int

def twoDiagonalMatrix(n):

   A square matrix of dimension n with ones
      along both diagonals, and zeros elsewhere.
   
   xs = range(1, 1 + n)
   return [
       [
           int(x in (y, 1 + (n - y)))
           for x in xs
       ]
       for y in xs
   ]


  1. ------------------------- TEST -------------------------
  2. main :: IO ()

def main():

   Matrices of dimension 7 and 8
   for n in [7, 8]:
       print(
           showMatrix(
               twoDiagonalMatrix(n)
           ) + '\n'
       )


  1. ----------------------- GENERIC ------------------------
  2. showMatrix :: Int -> String

def showMatrix(rows):

   String representation of a matrix
   return '\n'.join([
       ' '.join([str(x) for x in y]) for y in rows
   ])


  1. MAIN ---

if __name__ == '__main__':

   main()</lang>
Output:
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

Raku

<lang perl6>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);</lang>

Output:
[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]

Red

<lang rebol>Red[]

x-matrix: function [size][

   repeat i size [
       repeat j size [
           prin either any [i = j i + j = (size + 1)] [1] [0]
           prin sp
       ]
       prin newline
   ]

]

x-matrix 6 prin newline x-matrix 7</lang>

Output:
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 

Ring

<lang ring>

  1. Project : Identity Matrix
  2. Date  : 2022/16/02
  3. Author  : Gal Zsolt (~ CalmoSoft ~)
  4. Email  : <calmosoft@gmail.com>

load "stdlib.ring" load "guilib.ring"

size = 8 C_Spacing = 1

C_ButtonBlueStyle = 'border-radius:6px;color:black; background-color: blue' C_ButtonOrangeStyle = 'border-radius:6px;color:black; background-color: orange'

Button = newlist(size,size) LayoutButtonRow = list(size)

app = new qApp {

     win = new qWidget() {

setWindowTitle('Identity Matrix') move(500,100) reSize(600,600) winheight = win.height() fontSize = 18 + (winheight / 100)

	    LayoutButtonMain = new QVBoxLayout()			

LayoutButtonMain.setSpacing(C_Spacing) LayoutButtonMain.setContentsmargins(0,0,0,0)

for Row = 1 to size LayoutButtonRow[Row] = new QHBoxLayout() { setSpacing(C_Spacing) setContentsmargins(0,0,0,0) }

        	 for Col = 1 to size

Button[Row][Col] = new QPushButton(win) {

                                       setSizePolicy(1,1)                                                

}

LayoutButtonRow[Row].AddWidget(Button[Row][Col]) next LayoutButtonMain.AddLayout(LayoutButtonRow[Row]) next

             LayoutDataRow1 = new QHBoxLayout() { setSpacing(C_Spacing) setContentsMargins(0,0,0,0) }
             LayoutButtonMain.AddLayout(LayoutDataRow1)
             setLayout(LayoutButtonMain)
             show()
  }
  pBegin()
  exec()
  }

func pBegin()

    for Row = 1 to size
        for Col = 1 to size 
            if Row = Col or Row + Col = 9
               Button[Row][Col].setStyleSheet(C_ButtonOrangeStyle)
               Button[Row][Col].settext("1")
            else
               Button[Row][Col].setStyleSheet(C_ButtonBlueStyle)
               Button[Row][Col].settext("0")
            ok

next

    next
    score = 0

</lang> Outpu image:
Special identity matrix with two diagonals

Wren

A terminal based solution as I don't like asking people to view external images. <lang ecmascript>var specialMatrix = Fn.new { |n|

   for (i in 0...n) {
       for (j in 0...n) {
           System.write((i == j || i + j == n - 1) ? "1 " : "0 ")
       }
       System.print()
   }

}

specialMatrix.call(6) // even n System.print() specialMatrix.call(7) // odd n</lang>

Output:
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 

XPL0

<lang XPL0>proc DrawMat(S); int S, I, J; [for I:= 0 to S-1 do

   [for J:= 0 to S-1 do
       Text(0, if J=I or J=S-1-I then "1 " else "0 ");
   CrLf(0);
   ];

]; [DrawMat(6); CrLf(0);

DrawMat(7);  CrLf(0);

]</lang>

Output:
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