Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,4 +1,7 @@
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.
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 \\

View file

@ -0,0 +1,33 @@
(* ****** ****** *)
//
// How to compile:
//
// patscc -DATS_MEMALLOC_LIBC -o idmatrix idmatrix.dats
//
(* ****** ****** *)
//
#include
"share/atspre_staload.hats"
//
(* ****** ****** *)
extern
fun
idmatrix{n:nat}(n: size_t(n)): matrixref(int, n, n)
implement
idmatrix(n) =
matrixref_tabulate_cloref<int> (n, n, lam(i, j) => bool2int0(i = j))
(* ****** ****** *)
implement
main0 () =
{
//
val N = 5
//
val M = idmatrix(i2sz(N))
val () = fprint_matrixref_sep (stdout_ref, M, i2sz(N), i2sz(N), " ", "\n")
val () = fprint_newline (stdout_ref)
//
} (* end of [main0] *)

View file

@ -0,0 +1,61 @@
class
APPLICATION
inherit
ARGUMENTS
create
make
feature {NONE} -- Initialization
make
-- Run application.
local
dim : INTEGER -- Dimension of the identity matrix
do
from dim := 1 until dim > 10 loop
print_matrix( identity_matrix(dim) )
dim := dim + 1
io.new_line
end
end
feature -- Access
identity_matrix(dim : INTEGER) : ARRAY2[REAL_64]
require
dim > 0
local
matrix : ARRAY2[REAL_64]
i : INTEGER
do
create matrix.make_filled (0.0, dim, dim)
from i := 1 until i > dim loop
matrix.put(1.0, i, i)
i := i + 1
end
Result := matrix
end
print_matrix(matrix : ARRAY2[REAL_64])
local
i, j : INTEGER
do
from i := 1 until i > matrix.height loop
print("[ ")
from j := 1 until j > matrix.width loop
print(matrix.item (i, j))
print(" ")
j := j + 1
end
print("]%N")
i := i + 1
end
end
end

View file

@ -0,0 +1,3 @@
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; }) });
}

View file

@ -0,0 +1,3 @@
sub identity-matrix($n) {
[1, 0 xx $n-1], *.rotate(-1).item ... *[*-1] == 1
}

View file

@ -1,28 +1,33 @@
/*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.*/
/*REXX program creates and displays any sized identity matrix. */
do k=3 to 6 /* [↓] build & display a matrix.*/
call identity_matrix k /*build and display a kxk matrix.*/
end /*k*/ /* [↑] use general─purpose disp.*/
exit /*stick a fork in it, we're done.*/
/*─────────────────────────────────────IDENTITY_MATRIX subroutine───────*/
/*──────────────────────────────────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*/
do r=1 for n /*build identity matrix, by row,*/
do c=1 for n /* ··· and by col.*/
$=$ (r==c) /*append zero or one (if on diag)*/
end /*c*/
end /*r*/
call showMatrix 'identity matrix of size' n,$,n
call showMatrix 'identity matrix of size' 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*/
/*──────────────────────────────────SHOWMATRIX subroutine───────────────*/
showMatrix: procedure; parse arg hdr,x; #=words(x) /*#: # of elements*/
dp=0 /*DP: dec fraction width.*/
w=0 /*W: integer part width.*/
do n=1 until n*n>=#; _=word(x,n) /*find matrix order (size).*/
parse var _ y '.' f; w=max(w, length(y)); dp=max(dp, length(f))
end /*n*/ /* [↑] idiomatically find widths to align output*/
w=w+1
say; say center(hdr, max(length(hdr)+6, (w+1)*#%n), ''); say
#=0 /*#: element #*/
do row=1 for n; _=left('',n+w) /*indentation.*/
do col=1 for n; #=#+1 /*bump element*/
_=_ right(format(word(x, #), , dp)/1, w)
end /*col*/ /* [↑] division by 1 normalizes #*/
say _ /*display one line of the matrix. */
end /*row*/
return

View file

@ -0,0 +1,4 @@
2.1.1 :001 > require "matrix"
=> true
2.1.1 :002 > Matrix.identity(5)
=> Matrix[[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]]