September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -0,0 +1,5 @@
(defun identity-matrix (n)
(loop for a from 1 to n
collect (loop for e from 1 to n
if (= a e) collect 1
else collect 0)))

View file

@ -1,14 +1,14 @@
import extensions.
import system'routines.
import system'collections.
import extensions;
import system'routines;
import system'collections;
program =
[
var n := console write:"Enter the matrix size:"; readLine; toInt.
public program()
{
var n := console.write:"Enter the matrix size:".readLine().toInt();
var identity := 0 till:n repeat(:i)( 0 till:n repeat(:j)( (i == j)iif(1,0) ); summarize(ArrayList new) );
summarize(ArrayList new).
var identity := new Range(0, n).selectBy:(i => new Range(0,n).selectBy:(j => (i == j).iif(1,0) ).summarize(new ArrayList()))
.summarize(new ArrayList());
identity forEach
(:row) [ console printLine:row ].
].
identity.forEach:
(row) { console.printLine(row.asEnumerable()) }
}

View file

@ -1,17 +1,6 @@
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) {
0..($a.Count - 1) | foreach{ if($a[$_]){"$($a[$_])"}else{""} }
}
0..($n-1) | foreach{$row = @(0) * $n; $row[$_] = 1; ,$row}
}
function show($a) { $a | foreach{ "$_"} }
$array = identity 4
show $array

View file

@ -1,11 +1,61 @@
>>> def identity(size):
... return {(x, y):int(x == y) for x in range(size) for y in range(size)}
...
>>> size = 4
>>> matrix = identity(size)
>>> print('\n'.join(' '.join(str(matrix[(x, y)]) for x in range(size)) for y in range(size)))
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
>>>
'''Identity matrices by maps and equivalent list comprehensions'''
import operator
# idMatrix :: Int -> [[Int]]
def idMatrix(n):
'''Identity matrix of order n,
expressed as a nested map.
'''
eq = curry(operator.eq)
xs = range(0, n)
return list(map(
lambda x: list(map(
compose(int)(eq(x)),
xs
)),
xs
))
# idMatrix3 :: Int -> [[Int]]
def idMatrix2(n):
'''Identity matrix of order n,
expressed as a nested comprehension.
'''
xs = range(0, n)
return ([int(x == y) for x in xs] for y in xs)
# TEST ----------------------------------------------------
def main():
'''
Identity matrix of dimension five,
by two different routes.
'''
for f in [idMatrix, idMatrix2]:
print(
'\n' + f.__name__ + ':',
'\n\n' + '\n'.join(map(str, f(5))),
)
# GENERIC -------------------------------------------------
# compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
def compose(g):
'''Right to left function composition.'''
return lambda f: lambda x: g(f(x))
# curry :: ((a, b) -> c) -> a -> b -> c
def curry(f):
'''A curried function derived
from an uncurried function.'''
return lambda a: lambda b: f(a, b)
# MAIN ---
if __name__ == '__main__':
main()

View file

@ -1 +1,11 @@
np.mat(np.eye(size))
>>> def identity(size):
... return {(x, y):int(x == y) for x in range(size) for y in range(size)}
...
>>> size = 4
>>> matrix = identity(size)
>>> print('\n'.join(' '.join(str(matrix[(x, y)]) for x in range(size)) for y in range(size)))
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
>>>

View file

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

View file

@ -1,33 +1,30 @@
/*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: procedure; parse arg n; $=
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, $
return
/*──────────────────────────────────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
/*REXX program creates and displays any sized identity matrix (centered, with title).*/
do k=3 to 6 /* [↓] build and display a sq. matrix.*/
call ident_mat k /*build & display a KxK square matrix. */
end /*k*/ /* [↑] use general─purpose display sub*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
ident_mat: procedure; parse arg n; $=
do r=1 for n /*build identity matrix, by row and col*/
do c=1 for n; $= $ (r==c) /*append zero or one (if on diag). */
end /*c*/
end /*r*/
call showMat 'identity matrix of size' n, $
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
showMat: procedure; parse arg hdr,x; #=words(x) /*# is the number of matrix elements. */
dp= 0 /*DP: max width of decimal fractions. */
w= 0 /*W: max width of integer part. */
do n=1 until n*n>=#; _= word(x,n) /*determine the matrix order. */
parse var _ y '.' f; w= max(w, length(y)); dp= max(dp, length(f) )
end /*n*/ /* [↑] idiomatically find the widths. */
w= w +1
say; say center(hdr, max(length(hdr)+8, (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 unity normalizes #.*/
say _ /*display a single line of the matrix. */
end /*row*/
return

View file

@ -0,0 +1 @@
(Array2D identity: (UIManager default request: 'Enter size of the matrix:') asInteger) asString

View file

@ -0,0 +1,7 @@
func identityMatrix(size: Int) -> [[Int]] {
return (0..<size).map({i in
return (0..<size).map({ $0 == i ? 1 : 0})
})
}
print(identityMatrix(size: 5))

View file

@ -0,0 +1,8 @@
Private Function Identity(n As Integer) As Variant
Dim I() As Integer
ReDim I(n - 1, n - 1)
For j = 0 To n - 1
I(j, j) = 1
Next j
Identity = I
End Function

View file

@ -0,0 +1,38 @@
Option Explicit
'------------
Public Function BuildIdentityMatrix(ByVal Size As Long) As Byte()
Dim i As Long
Dim b() As Byte
Size = Size - 1
ReDim b(0 To Size, 0 To Size)
'at this point, the matrix is allocated and
'all elements are initialized to 0 (zero)
For i = 0 To Size
b(i, i) = 1 'set diagonal elements to 1
Next i
BuildIdentityMatrix = b
End Function
'------------
Sub IdentityMatrixDemo(ByVal Size As Long)
Dim b() As Byte
Dim i As Long, j As Long
b() = BuildIdentityMatrix(Size)
For i = LBound(b(), 1) To UBound(b(), 1)
For j = LBound(b(), 2) To UBound(b(), 2)
Debug.Print CStr(b(i, j));
Next j
Debug.Print
Next i
End Sub
'------------
Sub Main()
IdentityMatrixDemo 5
Debug.Print
IdentityMatrixDemo 10
End Sub