This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

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

View file

@ -0,0 +1,3 @@
---
category:
- Matrices

View file

@ -0,0 +1,4 @@
.=/¨3 3
1 0 0
0 1 0
0 0 1

View 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

View file

@ -0,0 +1 @@
ID{ ρ 1, ρ0}

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

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

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

View 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

View 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

View file

@ -0,0 +1 @@
6hd0bx#a.*\[#a.*0#a?dr@{(D!)\/1\/^^bx\/[+}m[e!

View 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();
}
}
}

View 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");
}
}

View file

@ -0,0 +1 @@
'( (0 1) (2 3) )

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

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

View file

@ -0,0 +1,4 @@
(defn identity-matrix [n]
(take n
(partition n (dec n)
(cycle (conj (repeat (dec n) 0) 1)))))

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

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

View file

@ -0,0 +1,7 @@
function IdentityMatrix(n)
$ X:=zeros(n,n);
$ for i=1 to n
$ X[i,i]:=1;
$ end;
$ return X;
$endfunction

View file

@ -0,0 +1,3 @@
>function IdentityMatrix (n:index)
$ return setdiag(zeros(n,n),0,1);
$endfunction

View file

@ -0,0 +1 @@
>id(5)

View 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

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

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

View file

@ -0,0 +1,11 @@
package main
import (
"fmt"
mat "github.com/skelterjohn/go.matrix"
)
func main() {
fmt.Println(mat.Eye(3))
}

View file

@ -0,0 +1,3 @@
def makeIdentityMatrix = { n ->
(0..<n).collect { i -> (0..<n).collect { j -> (i == j) ? 1 : 0 } }
}

View file

@ -0,0 +1,5 @@
(2..6).each { order ->
def iMatrix = makeIdentityMatrix(order)
iMatrix.each { println it }
println()
}

View file

@ -0,0 +1 @@
matI n = [ [fromEnum $ i == j | i <- [1..n]] | j <- [1..n]]

View file

@ -0,0 +1,2 @@
showMat :: [[Int]] -> String
showMat = unlines . map (unwords . map show)

View 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

View 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

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

View 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))+"]");
}
}
}

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

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

View file

@ -0,0 +1,8 @@
> LinearAlgebra:-IdentityMatrix( 4 );
[1 0 0 0]
[ ]
[0 1 0 0]
[ ]
[0 0 1 0]
[ ]
[0 0 0 1]

View 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.]

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

View file

@ -0,0 +1 @@
IdentityMatrix[4]

View file

@ -0,0 +1,5 @@
ident(4);
/* matrix([1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]) */

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

View 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";
}

View file

@ -0,0 +1,5 @@
(de identity (Size)
(let L (need Size (1) 0)
(make
(do Size
(link (copy (rot L))) ) ) ) )

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

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

View file

@ -0,0 +1 @@
np.mat(np.eye(size))

View file

@ -0,0 +1 @@
diag(3)

View file

@ -0,0 +1,4 @@
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1

View 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

View 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

View file

@ -0,0 +1,3 @@
m.=0
m.0=1000 /* the matrix' size */
m.4.17.333='Walter'

View file

@ -0,0 +1,3 @@
#lang racket
(require math)
(identity-matrix 5)

View 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

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

View 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 '())))

View file

@ -0,0 +1 @@
(display (identity 4))

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

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

View file

@ -0,0 +1,2 @@
set m [I 5 0 1] ;# Integer 0/1 for clarity of presentation
puts [$m format 2string]