Matrix with two diagonals: Difference between revisions
(→{{header|JavaScript}}: Added a version in JavaScript) |
|||
Line 191: | Line 191: | ||
0 1 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 0 0 0 1</pre> |
1 0 0 0 0 0 0 0 0 0 1</pre> |
||
=={{header|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 => [y, n - y + 1].includes(x) ? ( |
|||
1 |
|||
) : 0 |
|||
) |
|||
); |
|||
}; |
|||
// ---------------------- 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(cell => `${cell}`).join(" ") |
|||
).join("\n"); |
|||
// MAIN -- |
|||
return main(); |
|||
})();</lang> |
|||
{{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|Python}}== |
=={{header|Python}}== |
Revision as of 17:23, 17 February 2022
- Task
Draw a square matrix which has 1's on both diagonals but 0's elsewhere.
C
<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
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
<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
<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 || (y == succ (subtract x n)) = 1 | otherwise = 0
TEST -------------------------
main :: IO () main =
mapM_ putStrLn $ unlines . fmap (((' ' :) . show) =<<) . twoDiagonalMatrix <$> [10, 11]</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
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 => [y, n - y + 1].includes(x) ? ( 1 ) : 0 ) ); };
// ---------------------- 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(cell => `${cell}`).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
Python
<lang python>Matrix with two diagonals
- 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 [ [ 1 if x in (y, (n - y) + 1) else 0 for x in xs ] for y in xs ]
- ------------------------- TEST -------------------------
- main :: IO ()
def main():
Matrices of dimension 7 and 8 for n in [7, 8]: print( showMatrix( twoDiagonalMatrix(n) ) + '\n' )
- ----------------------- GENERIC ------------------------
- 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 ])
- 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
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>
- Project : Identity Matrix
- Date : 2022/16/02
- Author : Gal Zsolt (~ CalmoSoft ~)
- 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