2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,7 +1,11 @@
|
|||
Build an [[wp:identity matrix|identity matrix]] of a size known at runtime.
|
||||
;Task:
|
||||
Build an [[wp:identity matrix|identity matrix]] of a size known at run-time.
|
||||
|
||||
|
||||
An ''identity matrix'' is a square matrix of size '''''n'' × ''n''''',
|
||||
<br>where the diagonal elements are all '''1'''s (ones),
|
||||
<br>and all the other elements are all '''0'''s (zeroes).
|
||||
|
||||
An identity matrix is a square matrix, of size ''n'' × ''n'',
|
||||
where the diagonal elements are all 1s, and the other elements are all 0s.
|
||||
|
||||
<math>I_n = \begin{bmatrix}
|
||||
1 & 0 & 0 & \cdots & 0 \\
|
||||
|
|
@ -10,3 +14,10 @@ where the diagonal elements are all 1s, and the other elements are all 0s.
|
|||
\vdots & \vdots & \vdots & \ddots & \vdots \\
|
||||
0 & 0 & 0 & \cdots & 1 \\
|
||||
\end{bmatrix}</math>
|
||||
|
||||
|
||||
;Related tasks:
|
||||
* [[Spiral matrix]]
|
||||
* [[Zig-zag matrix]]
|
||||
* [[Ulam_spiral_(for_primes)]]
|
||||
<br><br>
|
||||
|
|
|
|||
22
Task/Identity-matrix/ALGOL-W/identity-matrix.alg
Normal file
22
Task/Identity-matrix/ALGOL-W/identity-matrix.alg
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
begin
|
||||
% set m to an identity matrix of size s %
|
||||
procedure makeIdentity( real array m ( *, * )
|
||||
; integer value s
|
||||
) ;
|
||||
for i := 1 until s do begin
|
||||
for j := 1 until s do m( i, j ) := 0.0;
|
||||
m( i, i ) := 1.0
|
||||
end makeIdentity ;
|
||||
|
||||
% test the makeIdentity procedure %
|
||||
begin
|
||||
real array id5( 1 :: 5, 1 :: 5 );
|
||||
makeIdentity( id5, 5 );
|
||||
r_format := "A"; r_w := 6; r_d := 1; % set output format for reals %
|
||||
for i := 1 until 5 do begin
|
||||
write();
|
||||
for j := 1 until 5 do writeon( id5( i, j ) )
|
||||
end for_i ;
|
||||
end text
|
||||
|
||||
end.
|
||||
77
Task/Identity-matrix/AppleScript/identity-matrix.applescript
Normal file
77
Task/Identity-matrix/AppleScript/identity-matrix.applescript
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
-- idMatrix :: Int -> [(0|1)]
|
||||
on idMatrix(n)
|
||||
set xs to range(1, n)
|
||||
|
||||
script row
|
||||
on lambda(x)
|
||||
script zeroOrOne
|
||||
on lambda(i)
|
||||
cond(i = x, 1, 0)
|
||||
end lambda
|
||||
end script
|
||||
|
||||
map(zeroOrOne, xs)
|
||||
end lambda
|
||||
end script
|
||||
|
||||
map(row, xs)
|
||||
end idMatrix
|
||||
|
||||
|
||||
-- TEST
|
||||
on run
|
||||
|
||||
idMatrix(5)
|
||||
|
||||
end run
|
||||
|
||||
|
||||
|
||||
-- GENERIC FUNCTIONS ------------------------------------------
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, 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 lambda(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property lambda : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
-- cond :: Bool -> a -> a -> a
|
||||
on cond(bool, f, g)
|
||||
if bool then
|
||||
f
|
||||
else
|
||||
g
|
||||
end if
|
||||
end cond
|
||||
|
||||
-- range :: Int -> Int -> [Int]
|
||||
on range(m, n)
|
||||
if n < m then
|
||||
set d to -1
|
||||
else
|
||||
set d to 1
|
||||
end if
|
||||
set lst to {}
|
||||
repeat with i from m to n by d
|
||||
set end of lst to i
|
||||
end repeat
|
||||
return lst
|
||||
end range
|
||||
14
Task/Identity-matrix/Elena/identity-matrix.elena
Normal file
14
Task/Identity-matrix/Elena/identity-matrix.elena
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#import system.
|
||||
#import extensions.
|
||||
#import system'routines.
|
||||
#import system'collections.
|
||||
|
||||
#symbol program=
|
||||
[
|
||||
#var n := console write:"Enter the matrix size:" readLine toInt.
|
||||
|
||||
#var identity := n repeat &each: i = [ n repeat &each: j = [ (i == j)iif:1:0 ] summarize:(ArrayList new) ] summarize:(ArrayList new).
|
||||
|
||||
identity run &each:
|
||||
row = [ console writeLine:row ].
|
||||
].
|
||||
3
Task/Identity-matrix/Fortran/identity-matrix-3.f
Normal file
3
Task/Identity-matrix/Fortran/identity-matrix-3.f
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
DO 1 I = 1,N
|
||||
DO 1 J = 1,N
|
||||
1 A(I,J) = (I/J)*(J/I)
|
||||
|
|
@ -1,30 +1,13 @@
|
|||
public class IdentityMatrix {
|
||||
public class PrintIdentityMatrix {
|
||||
|
||||
public static int[][] matrix(int n){
|
||||
int[][] array = new int[n][n];
|
||||
|
||||
for(int row=0; row<n; row++){
|
||||
for(int col=0; col<n; col++){
|
||||
if(row == col){
|
||||
array[row][col] = 1;
|
||||
}
|
||||
else{
|
||||
array[row][col] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return array;
|
||||
}
|
||||
public static void printMatrix(int[][] array){
|
||||
for(int row=0; row<array.length; row++){
|
||||
for(int col=0; col<array[row].length; col++){
|
||||
System.out.print(array[row][col] + "\t");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
public static void main (String []args){
|
||||
printMatrix(matrix(5));
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
int n = 5;
|
||||
int[][] array = new int[n][n];
|
||||
|
||||
IntStream.range(0, n).forEach(i -> array[i][i] = 1);
|
||||
|
||||
Arrays.stream(array)
|
||||
.map((int[] a) -> Arrays.toString(a))
|
||||
.forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
By Sani Yusuf @saniyusuf.
|
||||
|
|
|
|||
8
Task/Identity-matrix/JavaScript/identity-matrix-1.js
Normal file
8
Task/Identity-matrix/JavaScript/identity-matrix-1.js
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
function idMatrix(n) {
|
||||
return Array.apply(null, new Array(n))
|
||||
.map(function (x, i, xs) {
|
||||
return xs.map(function (_, k) {
|
||||
return i === k ? 1 : 0;
|
||||
})
|
||||
});
|
||||
}
|
||||
13
Task/Identity-matrix/JavaScript/identity-matrix-2.js
Normal file
13
Task/Identity-matrix/JavaScript/identity-matrix-2.js
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
(() => {
|
||||
|
||||
// idMatrix :: Int -> [[0 | 1]]
|
||||
const idMatrix = n => Array.from({
|
||||
length: n
|
||||
}, (_, i) => Array.from({
|
||||
length: n
|
||||
}, (_, j) => i !== j ? 0 : 1));
|
||||
|
||||
|
||||
// TEST
|
||||
return idMatrix(5);
|
||||
})();
|
||||
2
Task/Identity-matrix/JavaScript/identity-matrix-3.js
Normal file
2
Task/Identity-matrix/JavaScript/identity-matrix-3.js
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
[[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0],
|
||||
[0, 0, 0, 1, 0], [0, 0, 0, 0, 1]]
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
function im(n) {
|
||||
return Array.apply(null, new Array(n)).map(function(x, i, a) { return a.map(function(y, k) { return i === k ? 1 : 0; }) });
|
||||
}
|
||||
|
|
@ -1,3 +1,3 @@
|
|||
sub identity-matrix($n) {
|
||||
([1, |(0 xx $n-1)].item, *.rotate(-1).item ... *)[^$n]
|
||||
[1, |(0 xx $n-1)], *.rotate(-1) ... *[*-1]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,21 +1,17 @@
|
|||
function id($n) {
|
||||
if($n -gt 0) {
|
||||
$array = @(1..$n | foreach{ @(0) })
|
||||
0..($n-1) | foreach{
|
||||
$i = $_
|
||||
$array[$i] = @(switch(0..($n-1)){
|
||||
$i {1}
|
||||
default {0}
|
||||
})
|
||||
function identity($n) {
|
||||
if(0 -lt $n) {
|
||||
$array = @(0) * $n
|
||||
foreach ($i in 0..($n-1)) {
|
||||
$array[$i] = @(0) * $n
|
||||
$array[$i][$i] = 1
|
||||
}
|
||||
$array
|
||||
} else { @() }
|
||||
}
|
||||
function show($a) {
|
||||
if($a.Count -gt 0) {
|
||||
$n = $a.Count - 1
|
||||
0..$n | foreach{ "$($a[$_][0..$n])" }
|
||||
if($a) {
|
||||
0..($a.Count - 1) | foreach{ if($a[$_]){"$($a[$_])"}else{""} }
|
||||
}
|
||||
}
|
||||
$array = id 4
|
||||
$array = identity 4
|
||||
show $array
|
||||
|
|
|
|||
|
|
@ -1,4 +1,7 @@
|
|||
[,1] [,2] [,3]
|
||||
[1,] 1 0 0
|
||||
[2,] 0 1 0
|
||||
[3,] 0 0 1
|
||||
Identity_matrix=function(size){
|
||||
x=matrix(0,size,size)
|
||||
for (i in 1:size) {
|
||||
x[i,i]=1
|
||||
}
|
||||
return(x)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1 +0,0 @@
|
|||
diag(3)
|
||||
15
Task/Identity-matrix/VBScript/identity-matrix-2.vb
Normal file
15
Task/Identity-matrix/VBScript/identity-matrix-2.vb
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
n = 8
|
||||
|
||||
arr = Identity(n)
|
||||
|
||||
for i = 0 to n-1
|
||||
for j = 0 to n-1
|
||||
wscript.stdout.Write arr(i,j) & " "
|
||||
next
|
||||
wscript.stdout.writeline
|
||||
next
|
||||
|
||||
Function Identity (size)
|
||||
Execute Replace("dim a(#,#):for i=0 to #:for j=0 to #:a(i,j)=0:next:a(i,i)=1:next","#",size-1)
|
||||
Identity = a
|
||||
End Function
|
||||
25
Task/Identity-matrix/Vala/identity-matrix.vala
Normal file
25
Task/Identity-matrix/Vala/identity-matrix.vala
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
int main (string[] args) {
|
||||
if (args.length < 2) {
|
||||
print ("Please, input an integer > 0.\n");
|
||||
return 0;
|
||||
}
|
||||
var n = int.parse (args[1]);
|
||||
if (n <= 0) {
|
||||
print ("Please, input an integer > 0.\n");
|
||||
return 0;
|
||||
}
|
||||
int[,] array = new int[n, n];
|
||||
for (var i = 0; i < n; i ++) {
|
||||
for (var j = 0; j < n; j ++) {
|
||||
if (i == j) array[i,j] = 1;
|
||||
else array[i,j] = 0;
|
||||
}
|
||||
}
|
||||
for (var i = 0; i < n; i ++) {
|
||||
for (var j = 0; j < n; j ++) {
|
||||
print ("%d ", array[i,j]);
|
||||
}
|
||||
print ("\b\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue