Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
11
Task/Identity-matrix/Python/identity-matrix-1.py
Normal file
11
Task/Identity-matrix/Python/identity-matrix-1.py
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
def identity(size):
|
||||
matrix = [[0]*size for i in range(size)]
|
||||
#matrix = [[0] * size] * size #Has a flaw. See http://stackoverflow.com/questions/240178/unexpected-feature-in-a-python-list-of-lists
|
||||
|
||||
for i in range(size):
|
||||
matrix[i][i] = 1
|
||||
|
||||
for rows in matrix:
|
||||
for elements in rows:
|
||||
print elements,
|
||||
print ""
|
||||
61
Task/Identity-matrix/Python/identity-matrix-2.py
Normal file
61
Task/Identity-matrix/Python/identity-matrix-2.py
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
'''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()
|
||||
11
Task/Identity-matrix/Python/identity-matrix-3.py
Normal file
11
Task/Identity-matrix/Python/identity-matrix-3.py
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
>>> 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
|
||||
>>>
|
||||
1
Task/Identity-matrix/Python/identity-matrix-4.py
Normal file
1
Task/Identity-matrix/Python/identity-matrix-4.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
np.mat(np.eye(size))
|
||||
Loading…
Add table
Add a link
Reference in a new issue