A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
9
Task/Identity-matrix/0DESCRIPTION
Normal file
9
Task/Identity-matrix/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
Build an [[wp:identity matrix|identity matrix]] of a size known at runtime. 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 \\
|
||||
0 & 1 & 0 & \cdots & 0 \\
|
||||
0 & 0 & 1 & \cdots & 0 \\
|
||||
\vdots & \vdots & \vdots & \ddots & \vdots \\
|
||||
0 & 0 & 0 & \cdots & 1 \\
|
||||
\end{bmatrix}</math>
|
||||
3
Task/Identity-matrix/1META.yaml
Normal file
3
Task/Identity-matrix/1META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
category:
|
||||
- Matrices
|
||||
4
Task/Identity-matrix/APL/identity-matrix-1.apl
Normal file
4
Task/Identity-matrix/APL/identity-matrix-1.apl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
∘.=/⍳¨3 3
|
||||
1 0 0
|
||||
0 1 0
|
||||
0 0 1
|
||||
7
Task/Identity-matrix/APL/identity-matrix-2.apl
Normal file
7
Task/Identity-matrix/APL/identity-matrix-2.apl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
ID←{∘.=/⍳¨ ⍵ ⍵}
|
||||
ID 5
|
||||
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
Task/Identity-matrix/APL/identity-matrix-3.apl
Normal file
1
Task/Identity-matrix/APL/identity-matrix-3.apl
Normal file
|
|
@ -0,0 +1 @@
|
|||
ID←{⍵ ⍵ ρ 1, ⍵ρ0}
|
||||
17
Task/Identity-matrix/AWK/identity-matrix.awk
Normal file
17
Task/Identity-matrix/AWK/identity-matrix.awk
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
# syntax: GAWK -f IDENTITY_MATRIX.AWK size
|
||||
BEGIN {
|
||||
size = ARGV[1]
|
||||
if (size !~ /^[0-9]+$/) {
|
||||
print("size invalid or missing from command line")
|
||||
exit(1)
|
||||
}
|
||||
for (i=1; i<=size; i++) {
|
||||
for (j=1; j<=size; j++) {
|
||||
x = (i == j) ? 1 : 0
|
||||
printf("%2d",x) # print
|
||||
arr[i,j] = x # build
|
||||
}
|
||||
printf("\n")
|
||||
}
|
||||
exit(0)
|
||||
}
|
||||
4
Task/Identity-matrix/Ada/identity-matrix-1.ada
Normal file
4
Task/Identity-matrix/Ada/identity-matrix-1.ada
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
-- As prototyped in the Generic_Real_Arrays specification:
|
||||
-- function Unit_Matrix (Order : Positive; First_1, First_2 : Integer := 1) return Real_Matrix;
|
||||
-- For the task:
|
||||
mat : Real_Matrix := Unit_Matrix(5);
|
||||
4
Task/Identity-matrix/Ada/identity-matrix-2.ada
Normal file
4
Task/Identity-matrix/Ada/identity-matrix-2.ada
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
type Matrix is array(Positive Range <>, Positive Range <>) of Integer;
|
||||
mat : Matrix(1..5,1..5) := (others => (others => 0));
|
||||
-- then after the declarative section:
|
||||
for i in mat'Range(1) loop mat(i,i) := 1; end loop;
|
||||
17
Task/Identity-matrix/BBC-BASIC/identity-matrix.bbc
Normal file
17
Task/Identity-matrix/BBC-BASIC/identity-matrix.bbc
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
INPUT "Enter size of matrix: " size%
|
||||
PROCidentitymatrix(size%, im())
|
||||
FOR r% = 0 TO size%-1
|
||||
FOR c% = 0 TO size%-1
|
||||
PRINT im(r%, c%),;
|
||||
NEXT
|
||||
PRINT
|
||||
NEXT r%
|
||||
END
|
||||
|
||||
DEF PROCidentitymatrix(s%, RETURN m())
|
||||
LOCAL i%
|
||||
DIM m(s%-1, s%-1)
|
||||
FOR i% = 0 TO s%-1
|
||||
m(i%,i%) = 1
|
||||
NEXT
|
||||
ENDPROC
|
||||
7
Task/Identity-matrix/Burlesque/identity-matrix-1.blq
Normal file
7
Task/Identity-matrix/Burlesque/identity-matrix-1.blq
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
blsq ) 6 -.^^0\/r@\/'0\/.*'1+]\/{\/{rt}\/E!XX}x/+]m[sp
|
||||
1 0 0 0 0 0
|
||||
0 1 0 0 0 0
|
||||
0 0 1 0 0 0
|
||||
0 0 0 1 0 0
|
||||
0 0 0 0 1 0
|
||||
0 0 0 0 0 1
|
||||
1
Task/Identity-matrix/Burlesque/identity-matrix-2.blq
Normal file
1
Task/Identity-matrix/Burlesque/identity-matrix-2.blq
Normal file
|
|
@ -0,0 +1 @@
|
|||
6hd0bx#a.*\[#a.*0#a?dr@{(D!)\/1\/^^bx\/[+}m[e!
|
||||
35
Task/Identity-matrix/C-sharp/identity-matrix.cs
Normal file
35
Task/Identity-matrix/C-sharp/identity-matrix.cs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace IdentityMatrix
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
if (args.Length != 1)
|
||||
{
|
||||
Console.WriteLine("Requires exactly one argument");
|
||||
return;
|
||||
}
|
||||
int n;
|
||||
if (!int.TryParse(args[0], out n))
|
||||
{
|
||||
Console.WriteLine("Requires integer parameter");
|
||||
return;
|
||||
}
|
||||
|
||||
var identity =
|
||||
Enumerable.Range(0, n).Select(i => Enumerable.Repeat(0, n).Select((z,j) => j == i ? 1 : 0).ToList()).ToList();
|
||||
foreach (var row in identity)
|
||||
{
|
||||
foreach (var elem in row)
|
||||
{
|
||||
Console.Write(" " + elem);
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
Console.ReadKey();
|
||||
}
|
||||
}
|
||||
}
|
||||
38
Task/Identity-matrix/C/identity-matrix.c
Normal file
38
Task/Identity-matrix/C/identity-matrix.c
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
int main(int argc, char** argv) {
|
||||
if (argc < 2) {
|
||||
printf("usage: identitymatrix <number of rows>\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
signed int rowsize = atoi(argv[1]);
|
||||
if (rowsize < 0) {
|
||||
printf("Dimensions of matrix cannot be negative\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
volatile int numElements = rowsize * rowsize;
|
||||
if (numElements < rowsize) {
|
||||
printf("Squaring %d caused result to overflow to %d.\n", rowsize, numElements);
|
||||
abort();
|
||||
}
|
||||
int** matrix = calloc(numElements, sizeof(int*));
|
||||
if (!matrix) {
|
||||
printf("Failed to allocate %d elements of %d bytes each\n", numElements, sizeof(int*));
|
||||
abort();
|
||||
}
|
||||
for (unsigned int row = 0;row < rowsize;row++) {
|
||||
matrix[row] = calloc(numElements, sizeof(int));
|
||||
if (!matrix[row]) {
|
||||
printf("Failed to allocate %d elements of %d bytes each\n", numElements, sizeof(int));
|
||||
abort();
|
||||
}
|
||||
matrix[row][row] = 1;
|
||||
}
|
||||
printf("Matrix is: \n");
|
||||
for (unsigned int row = 0;row < rowsize;row++) {
|
||||
for (unsigned int column = 0;column < rowsize;column++) {
|
||||
printf("%d ", matrix[row][column]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
1
Task/Identity-matrix/Clojure/identity-matrix-1.clj
Normal file
1
Task/Identity-matrix/Clojure/identity-matrix-1.clj
Normal file
|
|
@ -0,0 +1 @@
|
|||
'( (0 1) (2 3) )
|
||||
6
Task/Identity-matrix/Clojure/identity-matrix-2.clj
Normal file
6
Task/Identity-matrix/Clojure/identity-matrix-2.clj
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
(defn identity-matrix [n]
|
||||
(let [row (conj (repeat (dec n) 0) 1)]
|
||||
(vec
|
||||
(for [i (range 1 (inc n))]
|
||||
(vec
|
||||
(reduce conj (drop i row ) (take i row)))))))
|
||||
2
Task/Identity-matrix/Clojure/identity-matrix-3.clj
Normal file
2
Task/Identity-matrix/Clojure/identity-matrix-3.clj
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
=> (identity-matrix 5)
|
||||
[[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]]
|
||||
4
Task/Identity-matrix/Clojure/identity-matrix-4.clj
Normal file
4
Task/Identity-matrix/Clojure/identity-matrix-4.clj
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
(defn identity-matrix [n]
|
||||
(take n
|
||||
(partition n (dec n)
|
||||
(cycle (conj (repeat (dec n) 0) 1)))))
|
||||
4
Task/Identity-matrix/Common-Lisp/identity-matrix.lisp
Normal file
4
Task/Identity-matrix/Common-Lisp/identity-matrix.lisp
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
(defun make-identity-matrix (n)
|
||||
(let ((array (make-array (list n n) :initial-element 0)))
|
||||
(loop for i below n do (setf (aref array i i) 1))
|
||||
array))
|
||||
30
Task/Identity-matrix/D/identity-matrix.d
Normal file
30
Task/Identity-matrix/D/identity-matrix.d
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import std.traits;
|
||||
|
||||
T[][] matId(T)(in size_t n) pure nothrow if (isAssignable!(T, T)) {
|
||||
auto Id = new T[][](n, n);
|
||||
|
||||
foreach (r, row; Id) {
|
||||
static if (__traits(compiles, {row[] = 0;})) {
|
||||
row[] = 0; // vector op doesn't work with T = BigInt
|
||||
row[r] = 1;
|
||||
} else {
|
||||
foreach (c; 0 .. n)
|
||||
row[c] = (c == r) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
return Id;
|
||||
}
|
||||
|
||||
void main() {
|
||||
import std.stdio, std.bigint;
|
||||
enum form = "[%([%(%s, %)],\n %)]]";
|
||||
|
||||
immutable id1 = matId!real(5);
|
||||
writefln(form ~ "\n", id1);
|
||||
|
||||
immutable id2 = matId!BigInt(3);
|
||||
writefln(form ~ "\n", id2);
|
||||
|
||||
// auto id3 = matId!(const int)(2); // cant't compile
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
function IdentityMatrix(n)
|
||||
$ X:=zeros(n,n);
|
||||
$ for i=1 to n
|
||||
$ X[i,i]:=1;
|
||||
$ end;
|
||||
$ return X;
|
||||
$endfunction
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
>function IdentityMatrix (n:index)
|
||||
$ return setdiag(zeros(n,n),0,1);
|
||||
$endfunction
|
||||
|
|
@ -0,0 +1 @@
|
|||
>id(5)
|
||||
23
Task/Identity-matrix/Fortran/identity-matrix.f
Normal file
23
Task/Identity-matrix/Fortran/identity-matrix.f
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
program identitymatrix
|
||||
|
||||
real, dimension(:, :), allocatable :: I
|
||||
character(len=8) :: fmt
|
||||
integer :: ms, j
|
||||
|
||||
ms = 10 ! the desired size
|
||||
|
||||
allocate(I(ms,ms))
|
||||
I = 0 ! Initialize the array.
|
||||
forall(j = 1:ms) I(j,j) = 1 ! Set the diagonal.
|
||||
|
||||
! I is the identity matrix, let's show it:
|
||||
|
||||
write (fmt, '(A,I2,A)') '(', ms, 'F6.2)'
|
||||
! if you consider to have used the (row, col) convention,
|
||||
! the following will print the transposed matrix (col, row)
|
||||
! but I' = I, so it's not important here
|
||||
write (*, fmt) I(:,:)
|
||||
|
||||
deallocate(I)
|
||||
|
||||
end program identitymatrix
|
||||
18
Task/Identity-matrix/Go/identity-matrix-1.go
Normal file
18
Task/Identity-matrix/Go/identity-matrix-1.go
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.Println(I(3))
|
||||
}
|
||||
|
||||
func I(n int) [][]float64 {
|
||||
m := make([][]float64, n)
|
||||
a := make([]float64, n*n)
|
||||
for i := 0; i < n; i++ {
|
||||
a[i] = 1
|
||||
m[i] = a[:n]
|
||||
a = a[n:]
|
||||
}
|
||||
return m
|
||||
}
|
||||
18
Task/Identity-matrix/Go/identity-matrix-2.go
Normal file
18
Task/Identity-matrix/Go/identity-matrix-2.go
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type matrix []float64
|
||||
|
||||
func main() {
|
||||
fmt.Println(I(3))
|
||||
}
|
||||
|
||||
func I(n int) matrix {
|
||||
m := make(matrix, n*n)
|
||||
n++
|
||||
for i := 0; i < len(m); i += n {
|
||||
m[i] = 1
|
||||
}
|
||||
return m
|
||||
}
|
||||
11
Task/Identity-matrix/Go/identity-matrix-3.go
Normal file
11
Task/Identity-matrix/Go/identity-matrix-3.go
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
mat "github.com/skelterjohn/go.matrix"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(mat.Eye(3))
|
||||
}
|
||||
3
Task/Identity-matrix/Groovy/identity-matrix-1.groovy
Normal file
3
Task/Identity-matrix/Groovy/identity-matrix-1.groovy
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
def makeIdentityMatrix = { n ->
|
||||
(0..<n).collect { i -> (0..<n).collect { j -> (i == j) ? 1 : 0 } }
|
||||
}
|
||||
5
Task/Identity-matrix/Groovy/identity-matrix-2.groovy
Normal file
5
Task/Identity-matrix/Groovy/identity-matrix-2.groovy
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(2..6).each { order ->
|
||||
def iMatrix = makeIdentityMatrix(order)
|
||||
iMatrix.each { println it }
|
||||
println()
|
||||
}
|
||||
1
Task/Identity-matrix/Haskell/identity-matrix-1.hs
Normal file
1
Task/Identity-matrix/Haskell/identity-matrix-1.hs
Normal file
|
|
@ -0,0 +1 @@
|
|||
matI n = [ [fromEnum $ i == j | i <- [1..n]] | j <- [1..n]]
|
||||
2
Task/Identity-matrix/Haskell/identity-matrix-2.hs
Normal file
2
Task/Identity-matrix/Haskell/identity-matrix-2.hs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
showMat :: [[Int]] -> String
|
||||
showMat = unlines . map (unwords . map show)
|
||||
10
Task/Identity-matrix/Haskell/identity-matrix-3.hs
Normal file
10
Task/Identity-matrix/Haskell/identity-matrix-3.hs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
*Main> putStr $ showMat $ matId 9
|
||||
1 0 0 0 0 0 0 0 0
|
||||
0 1 0 0 0 0 0 0 0
|
||||
0 0 1 0 0 0 0 0 0
|
||||
0 0 0 1 0 0 0 0 0
|
||||
0 0 0 0 1 0 0 0 0
|
||||
0 0 0 0 0 1 0 0 0
|
||||
0 0 0 0 0 0 1 0 0
|
||||
0 0 0 0 0 0 0 1 0
|
||||
0 0 0 0 0 0 0 0 1
|
||||
12
Task/Identity-matrix/J/identity-matrix.j
Normal file
12
Task/Identity-matrix/J/identity-matrix.j
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
= i. 4 NB. create an Identity matrix of size 4
|
||||
1 0 0 0
|
||||
0 1 0 0
|
||||
0 0 1 0
|
||||
0 0 0 1
|
||||
makeI=: =@i. NB. define as a verb with a user-defined name
|
||||
makeI 5 NB. create an Identity matrix of size 5
|
||||
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
|
||||
30
Task/Identity-matrix/Java/identity-matrix.java
Normal file
30
Task/Identity-matrix/Java/identity-matrix.java
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
public class IdentityMatrix {
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
By Sani Yusuf @saniyusuf.
|
||||
19
Task/Identity-matrix/LSL/identity-matrix.lsl
Normal file
19
Task/Identity-matrix/LSL/identity-matrix.lsl
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
default {
|
||||
state_entry() {
|
||||
llListen(PUBLIC_CHANNEL, "", llGetOwner(), "");
|
||||
llOwnerSay("Please Enter a Dimension for an Identity Matrix.");
|
||||
}
|
||||
listen(integer iChannel, string sName, key kId, string sMessage) {
|
||||
llOwnerSay("You entered "+sMessage+".");
|
||||
list lMatrix = [];
|
||||
integer x = 0;
|
||||
integer n = (integer)sMessage;
|
||||
for(x=0 ; x<n*n ; x++) {
|
||||
lMatrix += [(integer)(((x+1)%(n+1))==1)];
|
||||
}
|
||||
//llOwnerSay("["+llList2CSV(lMatrix)+"]");
|
||||
for(x=0 ; x<n ; x++) {
|
||||
llOwnerSay("["+llList2CSV(llList2ListStrided(lMatrix, x*n, (x+1)*n-1, 1))+"]");
|
||||
}
|
||||
}
|
||||
}
|
||||
8
Task/Identity-matrix/Lang5/identity-matrix.lang5
Normal file
8
Task/Identity-matrix/Lang5/identity-matrix.lang5
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
: identity-matrix
|
||||
dup iota 'A set
|
||||
|
||||
: i.(*) A in ;
|
||||
[1] swap append reverse A swap reshape 'i. apply
|
||||
;
|
||||
|
||||
5 identity-matrix .
|
||||
18
Task/Identity-matrix/Lua/identity-matrix.lua
Normal file
18
Task/Identity-matrix/Lua/identity-matrix.lua
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
function identity_matrix (size)
|
||||
local m = {}
|
||||
for i = 1, size do
|
||||
m[i] = {}
|
||||
for j = 1, size do
|
||||
m[i][j] = i == j and 1 or 0
|
||||
end
|
||||
end
|
||||
return m
|
||||
end
|
||||
|
||||
function print_matrix (m)
|
||||
for i = 1, #m do
|
||||
print(table.concat(m[i], " "))
|
||||
end
|
||||
end
|
||||
|
||||
print_matrix(identity_matrix(5))
|
||||
8
Task/Identity-matrix/Maple/identity-matrix-1.maple
Normal file
8
Task/Identity-matrix/Maple/identity-matrix-1.maple
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
> LinearAlgebra:-IdentityMatrix( 4 );
|
||||
[1 0 0 0]
|
||||
[ ]
|
||||
[0 1 0 0]
|
||||
[ ]
|
||||
[0 0 1 0]
|
||||
[ ]
|
||||
[0 0 0 1]
|
||||
8
Task/Identity-matrix/Maple/identity-matrix-2.maple
Normal file
8
Task/Identity-matrix/Maple/identity-matrix-2.maple
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
> Matrix( 4, shape = scalar[1], datatype = float[4] );
|
||||
[1. 0. 0. 0.]
|
||||
[ ]
|
||||
[0. 1. 0. 0.]
|
||||
[ ]
|
||||
[0. 0. 1. 0.]
|
||||
[ ]
|
||||
[0. 0. 0. 1.]
|
||||
8
Task/Identity-matrix/Maple/identity-matrix-3.maple
Normal file
8
Task/Identity-matrix/Maple/identity-matrix-3.maple
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
> Matrix( 4, shape = identity, datatype = integer[ 2 ] );
|
||||
[1 0 0 0]
|
||||
[ ]
|
||||
[0 1 0 0]
|
||||
[ ]
|
||||
[0 0 1 0]
|
||||
[ ]
|
||||
[0 0 0 1]
|
||||
|
|
@ -0,0 +1 @@
|
|||
IdentityMatrix[4]
|
||||
5
Task/Identity-matrix/Maxima/identity-matrix.maxima
Normal file
5
Task/Identity-matrix/Maxima/identity-matrix.maxima
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
ident(4);
|
||||
/* matrix([1, 0, 0, 0],
|
||||
[0, 1, 0, 0],
|
||||
[0, 0, 1, 0],
|
||||
[0, 0, 0, 1]) */
|
||||
25
Task/Identity-matrix/PHP/identity-matrix.php
Normal file
25
Task/Identity-matrix/PHP/identity-matrix.php
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
function createMatrix($size)
|
||||
{
|
||||
$result = array();
|
||||
|
||||
for ($i = 0; $i < $size; $i++) {
|
||||
$row = array_fill(0, $size, 0);
|
||||
$row[$i] = 1;
|
||||
$result[] = $row;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
function printMatrix(array $matrix)
|
||||
{
|
||||
foreach ($matrix as $row) {
|
||||
foreach ($row as $column) {
|
||||
echo $column . " ";
|
||||
}
|
||||
echo PHP_EOL;
|
||||
}
|
||||
echo PHP_EOL;
|
||||
}
|
||||
|
||||
printMatrix(createMatrix(5));
|
||||
18
Task/Identity-matrix/Perl/identity-matrix.pl
Normal file
18
Task/Identity-matrix/Perl/identity-matrix.pl
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
sub identity_matrix {
|
||||
my $n = shift;
|
||||
map {
|
||||
my $i = $_;
|
||||
[ map { ($_ == $i) - 0 } 1 .. $n ]
|
||||
} 1 .. $n;
|
||||
}
|
||||
|
||||
@ARGV = (4, 5, 6) unless @ARGV;
|
||||
|
||||
for (@ARGV) {
|
||||
my @id = identity_matrix $_;
|
||||
print "$_:\n";
|
||||
for (my $i=0; $i<@id; ++$i) {
|
||||
print join ' ', @{$id[$i]}, "\n";
|
||||
}
|
||||
print "\n";
|
||||
}
|
||||
5
Task/Identity-matrix/PicoLisp/identity-matrix-1.l
Normal file
5
Task/Identity-matrix/PicoLisp/identity-matrix-1.l
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(de identity (Size)
|
||||
(let L (need Size (1) 0)
|
||||
(make
|
||||
(do Size
|
||||
(link (copy (rot L))) ) ) ) )
|
||||
9
Task/Identity-matrix/PicoLisp/identity-matrix-2.l
Normal file
9
Task/Identity-matrix/PicoLisp/identity-matrix-2.l
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
: (identity 3)
|
||||
-> ((1 0 0) (0 1 0) (0 0 1))
|
||||
|
||||
: (mapc println (identity 5))
|
||||
(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)
|
||||
10
Task/Identity-matrix/Python/identity-matrix-1.py
Normal file
10
Task/Identity-matrix/Python/identity-matrix-1.py
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
def identity(size):
|
||||
matrix = [[0] * size] * size
|
||||
|
||||
for i in range(size):
|
||||
matrix[i][i] = 1
|
||||
|
||||
for rows in matrix:
|
||||
for elements in rows:
|
||||
print elements,
|
||||
print ""
|
||||
1
Task/Identity-matrix/Python/identity-matrix-2.py
Normal file
1
Task/Identity-matrix/Python/identity-matrix-2.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
np.mat(np.eye(size))
|
||||
1
Task/Identity-matrix/R/identity-matrix-1.r
Normal file
1
Task/Identity-matrix/R/identity-matrix-1.r
Normal file
|
|
@ -0,0 +1 @@
|
|||
diag(3)
|
||||
4
Task/Identity-matrix/R/identity-matrix-2.r
Normal file
4
Task/Identity-matrix/R/identity-matrix-2.r
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
[,1] [,2] [,3]
|
||||
[1,] 1 0 0
|
||||
[2,] 0 1 0
|
||||
[3,] 0 0 1
|
||||
28
Task/Identity-matrix/REXX/identity-matrix-1.rexx
Normal file
28
Task/Identity-matrix/REXX/identity-matrix-1.rexx
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/*REXX program to create and display an identity matrix. */
|
||||
call identity_matrix 4 /*build and display a 4x4 matrix.*/
|
||||
call identity_matrix 5 /*build and display a 5x5 matrix.*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*─────────────────────────────────────IDENTITY_MATRIX subroutine───────*/
|
||||
identity_matrix: procedure; parse arg n; $=
|
||||
|
||||
do r=1 for n /*build indentity matrix, by row,*/
|
||||
do c=1 for n /* and by cow.*/
|
||||
$=$ (r==c) /*append zero or one (if on diag)*/
|
||||
end /*c*/
|
||||
end /*r*/
|
||||
|
||||
call showMatrix 'identity matrix of size' n,$,n
|
||||
return
|
||||
/*─────────────────────────────────────TELL subroutine───&find the order*/
|
||||
showMatrix: procedure; parse arg hdr,x,order,sep; if sep=='' then sep='═'
|
||||
width=2 /*width of field to be used to display the elements*/
|
||||
decPlaces=1 /*# decimal places to the right of decimal point. */
|
||||
say; say center(hdr,max(length(hdr)+6,(width+1)*words(x)%order),sep); say
|
||||
#=0
|
||||
do row=1 for order; aLine=
|
||||
do col=1 for order; #=#+1
|
||||
aLine=aLine right(format(word(x,#),,decPlaces)/1, width)
|
||||
end /*col*/ /*dividing by 1 normalizes the #.*/
|
||||
say aLine
|
||||
end /*row*/
|
||||
return
|
||||
16
Task/Identity-matrix/REXX/identity-matrix-2.rexx
Normal file
16
Task/Identity-matrix/REXX/identity-matrix-2.rexx
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
/* REXX ***************************************************************
|
||||
* show identity matrix of size n
|
||||
* I consider m.i.j to represent the matrix (not needed for showing)
|
||||
* 06.07.2012 Walter Pachl
|
||||
**********************************************************************/
|
||||
Parse Arg n
|
||||
Say 'Identity Matrix of size' n '(m.i.j IS the Matrix)'
|
||||
m.=0
|
||||
Do i=1 To n
|
||||
ol=''
|
||||
Do j=1 To n
|
||||
m.i.j=(i=j)
|
||||
ol=ol''format(m.i.j,2) /* or ol=ol (i=j) */
|
||||
End
|
||||
Say ol
|
||||
End
|
||||
3
Task/Identity-matrix/REXX/identity-matrix-3.rexx
Normal file
3
Task/Identity-matrix/REXX/identity-matrix-3.rexx
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
m.=0
|
||||
m.0=1000 /* the matrix' size */
|
||||
m.4.17.333='Walter'
|
||||
3
Task/Identity-matrix/Racket/identity-matrix.rkt
Normal file
3
Task/Identity-matrix/Racket/identity-matrix.rkt
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
#lang racket
|
||||
(require math)
|
||||
(identity-matrix 5)
|
||||
7
Task/Identity-matrix/Ruby/identity-matrix.rb
Normal file
7
Task/Identity-matrix/Ruby/identity-matrix.rb
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
def identity(size)
|
||||
size.times.map { |i| size.times.map { |j| i == j ? 1 : 0 } }
|
||||
end
|
||||
|
||||
[4,5,6].each do |size|
|
||||
puts size, identity(size.to_i).map {|r| r.to_s}, ""
|
||||
end
|
||||
4
Task/Identity-matrix/Scala/identity-matrix.scala
Normal file
4
Task/Identity-matrix/Scala/identity-matrix.scala
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
def identityMatrix(n:Int)=Array.tabulate(n,n)((x,y) => if(x==y) 1 else 0)
|
||||
def printMatrix[T](m:Array[Array[T]])=m map (_.mkString("[", ", ", "]")) mkString "\n"
|
||||
|
||||
printMatrix(identityMatrix(5))
|
||||
15
Task/Identity-matrix/Scheme/identity-matrix-1.ss
Normal file
15
Task/Identity-matrix/Scheme/identity-matrix-1.ss
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
(define (identity n)
|
||||
(letrec
|
||||
((uvec
|
||||
(lambda (m i acc)
|
||||
(if (= i n)
|
||||
acc
|
||||
(uvec m (+ i 1)
|
||||
(cons (if (= i m) 1 0) acc)))))
|
||||
(idgen
|
||||
(lambda (i acc)
|
||||
(if (= i n)
|
||||
acc
|
||||
(idgen (+ i 1)
|
||||
(cons (uvec i 0 '()) acc))))))
|
||||
(idgen 0 '())))
|
||||
1
Task/Identity-matrix/Scheme/identity-matrix-2.ss
Normal file
1
Task/Identity-matrix/Scheme/identity-matrix-2.ss
Normal file
|
|
@ -0,0 +1 @@
|
|||
(display (identity 4))
|
||||
7
Task/Identity-matrix/Tcl/identity-matrix-1.tcl
Normal file
7
Task/Identity-matrix/Tcl/identity-matrix-1.tcl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
proc I {rank {zero 0.0} {one 1.0}} {
|
||||
set m [lrepeat $rank [lrepeat $rank $zero]]
|
||||
for {set i 0} {$i < $rank} {incr i} {
|
||||
lset m $i $i $one
|
||||
}
|
||||
return $m
|
||||
}
|
||||
13
Task/Identity-matrix/Tcl/identity-matrix-2.tcl
Normal file
13
Task/Identity-matrix/Tcl/identity-matrix-2.tcl
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
package require struct::matrix
|
||||
|
||||
proc I {rank {zero 0.0} {one 1.0}} {
|
||||
set m [struct::matrix]
|
||||
$m add columns $rank
|
||||
$m add rows $rank
|
||||
for {set i 0} {$i < $rank} {incr i} {
|
||||
for {set j 0} {$j < $rank} {incr j} {
|
||||
$m set cell $i $j [expr {$i==$j ? $one : $zero}]
|
||||
}
|
||||
}
|
||||
return $m
|
||||
}
|
||||
2
Task/Identity-matrix/Tcl/identity-matrix-3.tcl
Normal file
2
Task/Identity-matrix/Tcl/identity-matrix-3.tcl
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
set m [I 5 0 1] ;# Integer 0/1 for clarity of presentation
|
||||
puts [$m format 2string]
|
||||
Loading…
Add table
Add a link
Reference in a new issue