Matrix with two diagonals: Difference between revisions
Content added Content deleted
Alpha bravo (talk | contribs) (Added AutoHotkey) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 7: | Line 7: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_Io; use Ada.Text_Io; |
||
with Ada.Command_Line; |
with Ada.Command_Line; |
||
Line 46: | Line 46: | ||
Put_Line ("Usage: ./matrix_with_diagonals <side-length>"); |
Put_Line ("Usage: ./matrix_with_diagonals <side-length>"); |
||
end Matrix_With_Diagonals;</ |
end Matrix_With_Diagonals;</syntaxhighlight> |
||
=={{header|ALGOL 68}}== |
=={{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 # |
# draws a matrix with height and width = n with 1s on the diagonals # |
||
PROC draw 2 diagonals = ( INT n )VOID: |
PROC draw 2 diagonals = ( INT n )VOID: |
||
Line 68: | Line 68: | ||
print( ( newline ) ); |
print( ( newline ) ); |
||
draw 2 diagonals( 11 ) |
draw 2 diagonals( 11 ) |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 97: | Line 97: | ||
=={{header|APL}}== |
=={{header|APL}}== |
||
{{works with|Dyalog APL}} |
{{works with|Dyalog APL}} |
||
< |
<syntaxhighlight lang="apl">twoDiagonals ← (⌽∨⊢)∘.=⍨∘⍳ |
||
twoDiagonals¨ 10 11</ |
twoDiagonals¨ 10 11</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 |
<pre> 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 |
||
Line 113: | Line 113: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
===Procedural=== |
===Procedural=== |
||
< |
<syntaxhighlight lang="applescript">on twoDiagonalMatrix(n) |
||
if (n < 2) then error "twoDiagonalMatrix() handler: parameter must be > 1." |
if (n < 2) then error "twoDiagonalMatrix() handler: parameter must be > 1." |
||
Line 151: | Line 151: | ||
return linefeed & matrixToText(twoDiagonalMatrix(7), space) & ¬ |
return linefeed & matrixToText(twoDiagonalMatrix(7), space) & ¬ |
||
(linefeed & linefeed & matrixToText(twoDiagonalMatrix(8), space))</ |
(linefeed & linefeed & matrixToText(twoDiagonalMatrix(8), space))</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
< |
<syntaxhighlight lang="applescript">" |
||
1 0 0 0 0 0 1 |
1 0 0 0 0 0 1 |
||
0 1 0 0 0 1 0 |
0 1 0 0 0 1 0 |
||
Line 170: | Line 170: | ||
0 0 1 0 0 1 0 0 |
0 0 1 0 0 1 0 0 |
||
0 1 0 0 0 0 1 0 |
0 1 0 0 0 0 1 0 |
||
1 0 0 0 0 0 0 1"</ |
1 0 0 0 0 0 0 1"</syntaxhighlight> |
||
===Functional=== |
===Functional=== |
||
< |
<syntaxhighlight lang="applescript">---------------- MATRIX WITH TWO DIAGONALS --------------- |
||
-- bothDiagonals :: Int -> [[Int]] |
-- bothDiagonals :: Int -> [[Int]] |
||
Line 312: | Line 312: | ||
set my text item delimiters to dlm |
set my text item delimiters to dlm |
||
return s |
return s |
||
end unwords</ |
end unwords</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>1 0 0 0 0 0 1 |
<pre>1 0 0 0 0 0 1 |
||
Line 333: | Line 333: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">drawSquare: function [side][ |
||
loop 1..side 'x -> |
loop 1..side 'x -> |
||
print map 1..side 'y [ |
print map 1..side 'y [ |
||
Line 342: | Line 342: | ||
drawSquare 6 |
drawSquare 6 |
||
print "" |
print "" |
||
drawSquare 9</ |
drawSquare 9</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 364: | Line 364: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">for i, v in [8, 9] |
||
result .= "Matrix Size: " v "*" v "`n" matrix2txt(diagonalMatrix(v)) "`n" |
result .= "Matrix Size: " v "*" v "`n" matrix2txt(diagonalMatrix(v)) "`n" |
||
MsgBox % result |
MsgBox % result |
||
Line 386: | Line 386: | ||
} |
} |
||
return result |
return result |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Matrix Size: 8*8 |
<pre>Matrix Size: 8*8 |
||
Line 410: | Line 410: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f MATRIX_WITH_TWO_DIAGONALS.AWK |
# syntax: GAWK -f MATRIX_WITH_TWO_DIAGONALS.AWK |
||
BEGIN { |
BEGIN { |
||
Line 425: | Line 425: | ||
exit(0) |
exit(0) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 445: | Line 445: | ||
=={{header|Basic}}== |
=={{header|Basic}}== |
||
< |
<syntaxhighlight lang="qbasic">100 REM |
||
110 REM DIAGONAL-DIAGONAL MATRIX |
110 REM DIAGONAL-DIAGONAL MATRIX |
||
120 REM |
120 REM |
||
Line 467: | Line 467: | ||
290 NEXT I |
290 NEXT I |
||
300 END |
300 END |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Output}} |
{{Output}} |
||
<pre> 1 0 0 0 0 0 1 |
<pre> 1 0 0 0 0 0 1 |
||
Line 479: | Line 479: | ||
=={{header|BCPL}}== |
=={{header|BCPL}}== |
||
< |
<syntaxhighlight lang="bcpl">get "libhdr" |
||
let diagonals(size) be |
let diagonals(size) be |
||
Line 492: | Line 492: | ||
wrch('*N') |
wrch('*N') |
||
diagonals(10) |
diagonals(10) |
||
$)</ |
$)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 0 0 0 0 0 0 0 1 |
<pre>1 0 0 0 0 0 0 0 1 |
||
Line 517: | Line 517: | ||
=={{header|C}}== |
=={{header|C}}== |
||
{{trans|Wren}} |
{{trans|Wren}} |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
void specialMatrix(unsigned int n) { |
void specialMatrix(unsigned int n) { |
||
Line 538: | Line 538: | ||
specialMatrix(11); // odd n |
specialMatrix(11); // odd n |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 567: | Line 567: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <concepts> |
||
#include <iostream> |
#include <iostream> |
||
Line 597: | Line 597: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> 1 0 0 0 0 0 0 1 |
<pre> 1 0 0 0 0 0 0 1 |
||
Line 620: | Line 620: | ||
=={{header|CLU}}== |
=={{header|CLU}}== |
||
< |
<syntaxhighlight lang="clu">matrix = array[array[int]] |
||
diagonals = proc (size: int) returns (matrix) |
diagonals = proc (size: int) returns (matrix) |
||
Line 653: | Line 653: | ||
stream$putl(po, "") |
stream$putl(po, "") |
||
print_matrix(po, diagonals(10)) |
print_matrix(po, diagonals(10)) |
||
end start_up</ |
end start_up</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 1 0 0 0 0 0 0 0 1 |
<pre> 1 0 0 0 0 0 0 0 1 |
||
Line 677: | Line 677: | ||
=={{header|Draco}}== |
=={{header|Draco}}== |
||
< |
<syntaxhighlight lang="draco">proc setDiagonals([*,*] byte matrix) void: |
||
word x, y, width, height; |
word x, y, width, height; |
||
width := dim(matrix, 1); |
width := dim(matrix, 1); |
||
Line 714: | Line 714: | ||
setDiagonals(m_even); |
setDiagonals(m_even); |
||
printMatrix(m_even) |
printMatrix(m_even) |
||
corp</ |
corp</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 1 0 0 0 0 0 0 0 1 |
<pre> 1 0 0 0 0 0 0 0 1 |
||
Line 747: | Line 747: | ||
{{Works with|Office 365 betas 2021}} |
{{Works with|Office 365 betas 2021}} |
||
< |
<syntaxhighlight lang="lisp">TwoDiagonalMatrix |
||
=LAMBDA(n, |
=LAMBDA(n, |
||
LET( |
LET( |
||
Line 759: | Line 759: | ||
) |
) |
||
) |
) |
||
)</ |
)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
The formulae in cells A2 and B2 populate the whole of each adjacent matrix. |
The formulae in cells A2 and B2 populate the whole of each adjacent matrix. |
||
Line 970: | Line 970: | ||
Binding the name ''bothDiagonalMatrix'' in the Excel Name Manager: |
Binding the name ''bothDiagonalMatrix'' in the Excel Name Manager: |
||
< |
<syntaxhighlight lang="lisp">bothDiagonalMatrix |
||
=LAMBDA(n, |
=LAMBDA(n, |
||
MAKEARRAY( |
MAKEARRAY( |
||
Line 982: | Line 982: | ||
) |
) |
||
) |
) |
||
)</ |
)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
{| class="wikitable" |
{| class="wikitable" |
||
Line 1,189: | Line 1,189: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Matrix with two diagonals. Nigel Galloway: February 17th., 2022 |
// 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) |
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) |
printfn "%A\n\n%A" (m11 5) (m11 6) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,212: | Line 1,212: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.99 2021-06-02}} |
{{works with|Factor|0.99 2021-06-02}} |
||
< |
<syntaxhighlight lang="factor">USING: io kernel math math.matrices prettyprint ; |
||
: <x-matrix> ( n -- matrix ) |
: <x-matrix> ( n -- matrix ) |
||
Line 1,218: | Line 1,218: | ||
6 <x-matrix> simple-table. nl |
6 <x-matrix> simple-table. nl |
||
7 <x-matrix> simple-table.</ |
7 <x-matrix> simple-table.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,240: | Line 1,240: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
===Text based=== |
===Text based=== |
||
< |
<syntaxhighlight lang="freebasic">Sub twoDiagonalMatrix(n As Integer) |
||
For i As Integer = 1 To n |
For i As Integer = 1 To n |
||
For j As Integer = 1 To n |
For j As Integer = 1 To n |
||
Line 1,252: | Line 1,252: | ||
Print |
Print |
||
twoDiagonalMatrix(7) |
twoDiagonalMatrix(7) |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 0 0 0 0 1 |
<pre>1 0 0 0 0 1 |
||
Line 1,270: | Line 1,270: | ||
===Graphical=== |
===Graphical=== |
||
< |
<syntaxhighlight lang="freebasic">Dim As Integer n = 8, size = 60 * n + 70 |
||
Screenres size, size, 24 |
Screenres size, size, 24 |
||
Cls |
Cls |
||
Line 1,291: | Line 1,291: | ||
Next x |
Next x |
||
Bsave "twoDiagonalMatrix.bmp",0 |
Bsave "twoDiagonalMatrix.bmp",0 |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
https://www.dropbox.com/s/ph9r28gpkp8ao8n/twoDiagonalMatrix.bmp?dl=0 |
https://www.dropbox.com/s/ph9r28gpkp8ao8n/twoDiagonalMatrix.bmp?dl=0 |
||
Line 1,300: | Line 1,300: | ||
{{works with|Fortran 95}} |
{{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. |
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) |
dimension a(100, 100) |
||
Line 1,323: | Line 1,323: | ||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Output}} |
{{Output}} |
||
<pre> |
<pre> |
||
Line 1,337: | Line 1,337: | ||
{{Works with|Fortran 77}} |
{{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. |
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 |
PROGRAM PROG |
||
Line 1,353: | Line 1,353: | ||
20 PRINT *, (A(I, J), J=1,N) |
20 PRINT *, (A(I, J), J=1,N) |
||
END</ |
END</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|Wren}} |
{{trans|Wren}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 1,378: | Line 1,378: | ||
fmt.Println() |
fmt.Println() |
||
specialMatrix(9) // odd n |
specialMatrix(9) // odd n |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,403: | Line 1,403: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">---------------- MATRIX WITH TWO DIAGONALS --------------- |
||
twoDiagonalMatrix :: Int -> [[Int]] |
twoDiagonalMatrix :: Int -> [[Int]] |
||
Line 1,420: | Line 1,420: | ||
unlines . fmap (((' ' :) . show) =<<) |
unlines . fmap (((' ' :) . show) =<<) |
||
. twoDiagonalMatrix |
. twoDiagonalMatrix |
||
<$> [7, 8]</ |
<$> [7, 8]</syntaxhighlight> |
||
Or, in the form of a list comprehension: |
Or, in the form of a list comprehension: |
||
< |
<syntaxhighlight lang="haskell">-------------- MATRIX WITH TWO DIAGONALS --------------- |
||
twoDiagonalMatrix :: Int -> [[Int]] |
twoDiagonalMatrix :: Int -> [[Int]] |
||
Line 1,441: | Line 1,441: | ||
unlines . fmap (((' ' :) . show) =<<) |
unlines . fmap (((' ' :) . show) =<<) |
||
. twoDiagonalMatrix |
. twoDiagonalMatrix |
||
<$> [7, 8]</ |
<$> [7, 8]</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre> 1 0 0 0 0 0 1 |
<pre> 1 0 0 0 0 0 1 |
||
Line 1,462: | Line 1,462: | ||
and in terms of the Data.Matrix library: |
and in terms of the Data.Matrix library: |
||
< |
<syntaxhighlight lang="haskell">import Data.Matrix |
||
twoDiagonals :: Int -> Matrix Int |
twoDiagonals :: Int -> Matrix Int |
||
Line 1,473: | Line 1,473: | ||
main :: IO () |
main :: IO () |
||
main = |
main = |
||
mapM_ print $ twoDiagonals <$> [7, 8]</ |
mapM_ print $ twoDiagonals <$> [7, 8]</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>┌ ┐ |
<pre>┌ ┐ |
||
Line 1,499: | Line 1,499: | ||
Implementation: |
Implementation: |
||
< |
<syntaxhighlight lang="j">task=: {{(+.|.)=i.y}}</syntaxhighlight> |
||
In other words, generate an order n identity matrix, flip it and (treating it as a bit matrix) OR the two matrices. |
In other words, generate an order n identity matrix, flip it and (treating it as a bit matrix) OR the two matrices. |
||
Line 1,505: | Line 1,505: | ||
Some examples: |
Some examples: |
||
< |
<syntaxhighlight lang="j"> task 2 |
||
1 1 |
1 1 |
||
1 1 |
1 1 |
||
Line 1,529: | Line 1,529: | ||
0 0 1 1 0 0 |
0 0 1 1 0 0 |
||
0 1 0 0 1 0 |
0 1 0 0 1 0 |
||
1 0 0 0 0 1</ |
1 0 0 0 0 1</syntaxhighlight> |
||
=={{header|Java}}== |
=={{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. |
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 class Program { |
||
Line 1,588: | Line 1,588: | ||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{Output}} |
{{Output}} |
||
<pre> 1.0 0.0 0.0 0.0 0.0 0.0 1.0 |
<pre> 1.0 0.0 0.0 0.0 0.0 0.0 1.0 |
||
Line 1,599: | Line 1,599: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
"use strict"; |
"use strict"; |
||
Line 1,647: | Line 1,647: | ||
// MAIN -- |
// MAIN -- |
||
return main(); |
return main(); |
||
})();</ |
})();</syntaxhighlight> |
||
Or, in terms of a more general matrix function: |
Or, in terms of a more general matrix function: |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
"use strict"; |
"use strict"; |
||
Line 1,707: | Line 1,707: | ||
// MAIN --- |
// MAIN --- |
||
return main(); |
return main(); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>1 0 0 0 0 0 1 |
<pre>1 0 0 0 0 0 1 |
||
Line 1,729: | Line 1,729: | ||
{{works with|jq}} |
{{works with|jq}} |
||
'''Works with gojq, the Go implementation of jq''' |
'''Works with gojq, the Go implementation of jq''' |
||
< |
<syntaxhighlight lang="jq">def bidiagonal_matrix: |
||
. as $n |
. as $n |
||
| [range(0; $n) | 0] as $z |
| [range(0; $n) | 0] as $z |
||
Line 1,736: | Line 1,736: | ||
def display: |
def display: |
||
map(join(" ")) | join("\n");</ |
map(join(" ")) | join("\n");</syntaxhighlight> |
||
'''Example''' |
'''Example''' |
||
<lang |
<syntaxhighlight lang="jq">9|bidiagonal_matrix|display</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,753: | Line 1,753: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia"> |
||
julia> twodiagonalmat(n) = [Int(i == j || i == n - j + 1) for j in 1:n, i in 1:n] |
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) |
twodiagonalmat (generic function with 1 method) |
||
Line 1,786: | Line 1,786: | ||
0 1 0 1 0 |
0 1 0 1 0 |
||
1 0 0 0 1 |
1 0 0 0 1 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=== Sparse matrix version === |
=== Sparse matrix version === |
||
< |
<syntaxhighlight lang="julia">julia> using LinearAlgebra |
||
julia> twodiagonalsparse(n) = I(n) .| rotl90(I(n)) |
julia> twodiagonalsparse(n) = I(n) .| rotl90(I(n)) |
||
Line 1,813: | Line 1,813: | ||
⋅ 1 ⋅ ⋅ ⋅ ⋅ 1 ⋅ |
⋅ 1 ⋅ ⋅ ⋅ ⋅ 1 ⋅ |
||
1 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1 |
1 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Matlab}}== |
=={{header|Matlab}}== |
||
< |
<syntaxhighlight lang="matlab">function A = diagdiag(N, sparse) |
||
% Create an diagonal-diagonal square matrix. |
% Create an diagonal-diagonal square matrix. |
||
% |
% |
||
Line 1,843: | Line 1,843: | ||
A = fliplr(A); |
A = fliplr(A); |
||
A(1:N+1:end) = 1; |
A(1:N+1:end) = 1; |
||
end</ |
end</syntaxhighlight> |
||
{{Output}} |
{{Output}} |
||
<pre> |
<pre> |
||
Line 1,880: | Line 1,880: | ||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
< |
<syntaxhighlight lang="pascal">program diagonaldiagonal; |
||
const N = 7; |
const N = 7; |
||
type |
type |
||
Line 1,905: | Line 1,905: | ||
end |
end |
||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Output}} |
{{Output}} |
||
<pre> 1 0 0 0 0 0 1 |
<pre> 1 0 0 0 0 0 1 |
||
Line 1,918: | Line 1,918: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
===Strings=== |
===Strings=== |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use strict; #https://rosettacode.org/wiki/Matrix_with_two_diagonals |
use strict; #https://rosettacode.org/wiki/Matrix_with_two_diagonals |
||
Line 1,931: | Line 1,931: | ||
1 while s/(?<=1...{$n})0/1/s or s/(?<=2.{$n})[01]/2/s; |
1 while s/(?<=1...{$n})0/1/s or s/(?<=2.{$n})[01]/2/s; |
||
return tr/2/1/r =~ s/\B/ /gr; |
return tr/2/1/r =~ s/\B/ /gr; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 0 0 0 0 0 0 0 0 1 |
<pre>1 0 0 0 0 0 0 0 0 1 |
||
Line 1,957: | Line 1,957: | ||
===Numbers=== |
===Numbers=== |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use feature 'say'; |
use feature 'say'; |
||
Line 1,973: | Line 1,973: | ||
say join ' ', @$_ for dual_diagonal(4); say ''; |
say join ' ', @$_ for dual_diagonal(4); say ''; |
||
say join ' ', @$_ for dual_diagonal(5); |
say join ' ', @$_ for dual_diagonal(5); |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>1 0 0 1 |
<pre>1 0 0 1 |
||
Line 1,989: | Line 1,989: | ||
===numbers=== |
===numbers=== |
||
Tee hee, pick the bones out of this one. ''Lovely.'' |
Tee hee, pick the bones out of this one. ''Lovely.'' |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<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: #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: #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> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,012: | Line 2,012: | ||
</pre> |
</pre> |
||
Slightly saner, shows sackly same stuff: |
Slightly saner, shows sackly same stuff: |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<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: #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 2,022: | Line 2,022: | ||
<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: #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> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
===strings=== |
===strings=== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<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: #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 2,035: | Line 2,035: | ||
<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: #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> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,058: | Line 2,058: | ||
Based on [[2048#Phix]], and uses the same colour and font choices. |
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]. |
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;">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;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> |
||
Line 2,188: | Line 2,188: | ||
<span style="color: #7060A8;">IupClose</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> |
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
< |
<syntaxhighlight lang="php"><?php |
||
$n = 9; // the number of rows |
$n = 9; // the number of rows |
||
Line 2,201: | Line 2,201: | ||
echo "\n"; |
echo "\n"; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Output}} |
{{Output}} |
||
<pre> 1 0 0 0 0 0 0 0 1 |
<pre> 1 0 0 0 0 0 0 0 1 |
||
Line 2,215: | Line 2,215: | ||
=={{header|Processing}}== |
=={{header|Processing}}== |
||
< |
<syntaxhighlight lang="java"> |
||
//Aamrun, 27th June 2022 |
//Aamrun, 27th June 2022 |
||
Line 2,235: | Line 2,235: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Pure Python=== |
===Pure Python=== |
||
< |
<syntaxhighlight lang="python">'''Matrix with two diagonals''' |
||
Line 2,289: | Line 2,289: | ||
# MAIN --- |
# MAIN --- |
||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
main()</ |
main()</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>1 0 0 0 0 0 1 |
<pre>1 0 0 0 0 0 1 |
||
Line 2,310: | Line 2,310: | ||
===NumPy=== |
===NumPy=== |
||
{{libheader|numpy}} |
{{libheader|numpy}} |
||
< |
<syntaxhighlight lang="python">import numpy as np |
||
def diagdiag(n): |
def diagdiag(n): |
||
Line 2,329: | Line 2,329: | ||
return a |
return a |
||
print(diagdiag(7))</ |
print(diagdiag(7))</syntaxhighlight> |
||
{{Output}} |
{{Output}} |
||
<pre> |
<pre> |
||
Line 2,343: | Line 2,343: | ||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
< |
<syntaxhighlight lang="quackery"> [ [] swap dup times |
||
[ 0 over of |
[ 0 over of |
||
1 swap i poke |
1 swap i poke |
||
Line 2,357: | Line 2,357: | ||
witheach |
witheach |
||
[ witheach [ echo sp ] cr ] |
[ witheach [ echo sp ] cr ] |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,382: | Line 2,382: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
<lang |
<syntaxhighlight lang="raku" line>sub dual-diagonal($n) { ([1, |(0 xx $n-1)], *.rotate(-1) … *[*-1]).map: { [$_ Z|| .reverse] } } |
||
.say for dual-diagonal(6); |
.say for dual-diagonal(6); |
||
say ''; |
say ''; |
||
.say for dual-diagonal(7);</ |
.say for dual-diagonal(7);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[1 0 0 0 0 1] |
<pre>[1 0 0 0 0 1] |
||
Line 2,404: | Line 2,404: | ||
=={{header|Red}}== |
=={{header|Red}}== |
||
< |
<syntaxhighlight lang="rebol">Red[] |
||
x-matrix: function [size][ |
x-matrix: function [size][ |
||
Line 2,418: | Line 2,418: | ||
x-matrix 6 |
x-matrix 6 |
||
prin newline |
prin newline |
||
x-matrix 7</ |
x-matrix 7</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,438: | Line 2,438: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
# Project : Identity Matrix |
# Project : Identity Matrix |
||
# Date : 2022/16/02 |
# Date : 2022/16/02 |
||
Line 2,505: | Line 2,505: | ||
next |
next |
||
score = 0 |
score = 0 |
||
</syntaxhighlight> |
|||
</lang> |
|||
Outpu image: |
Outpu image: |
||
<br> |
<br> |
||
Line 2,511: | Line 2,511: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">func dual_diagonal(n) { |
||
n.of {|k| |
n.of {|k| |
||
var r = (k.of(0) + [1] + (n - k - 1).of(0)) |
var r = (k.of(0) + [1] + (n - k - 1).of(0)) |
||
Line 2,519: | Line 2,519: | ||
dual_diagonal(5).each{.join(' ').say}; say '' |
dual_diagonal(5).each{.join(' ').say}; say '' |
||
dual_diagonal(6).each{.join(' ').say}</ |
dual_diagonal(6).each{.join(' ').say}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,538: | Line 2,538: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
{{trans|go}} |
{{trans|go}} |
||
< |
<syntaxhighlight lang="vlang">fn special_matrix(n int) { |
||
for i in 0..n { |
for i in 0..n { |
||
for j in 0..n { |
for j in 0..n { |
||
Line 2,555: | Line 2,555: | ||
println('') |
println('') |
||
special_matrix(5) // odd n |
special_matrix(5) // odd n |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,573: | Line 2,573: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
A terminal based solution as I don't like asking people to view external images. |
A terminal based solution as I don't like asking people to view external images. |
||
< |
<syntaxhighlight lang="ecmascript">var specialMatrix = Fn.new { |n| |
||
for (i in 0...n) { |
for (i in 0...n) { |
||
for (j in 0...n) { |
for (j in 0...n) { |
||
Line 2,584: | Line 2,584: | ||
specialMatrix.call(6) // even n |
specialMatrix.call(6) // even n |
||
System.print() |
System.print() |
||
specialMatrix.call(7) // odd n</ |
specialMatrix.call(7) // odd n</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,605: | Line 2,605: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">proc DrawMat(S); |
||
int S, I, J; |
int S, I, J; |
||
[for I:= 0 to S-1 do |
[for I:= 0 to S-1 do |
||
Line 2,615: | Line 2,615: | ||
[DrawMat(6); CrLf(0); |
[DrawMat(6); CrLf(0); |
||
DrawMat(7); CrLf(0); |
DrawMat(7); CrLf(0); |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |