How to resolve the algorithm Identity matrix step by step in the Python programming language
How to resolve the algorithm Identity matrix step by step in the Python programming language
Table of Contents
Problem Statement
Build an identity matrix of a size known at run-time.
An identity matrix is a square matrix of size n × n, where the diagonal elements are all 1s (ones), and all the other elements are all 0s (zeroes).
I
n
=
[
1
0
0
⋯
0
0
1
0
⋯
0
0
0
1
⋯
0
⋮
⋮
⋮
⋱
⋮
0
0
0
⋯
1
]
{\displaystyle 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}}}
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Identity matrix step by step in the Python programming language
The provided code defines several functions for creating identity matrices in Python:
-
identity(size)
: This function creates an identity matrix of the specified size using nested lists. It initializes a square matrix with size * size dimensions, where each row and column has the same size. It then sets the diagonal elements (i.e., matrix[i][i]) to 1, indicating the identity matrix property. Finally, it prints the matrix. -
idMatrix(n)
: This function creates an identity matrix of order n using maps (functions that take one argument and return another). It utilizes thecurry
function to create a curried version of the equality operator (operator.eq
). It then applies a nested map to create a matrix where each element is the result of applying the curried equality operator to the row and column indices. The resulting matrix is an identity matrix. -
idMatrix2(n)
: This function is similar toidMatrix
but uses nested list comprehensions to create the identity matrix. It creates a list of lists where each element is determined by comparing the row and column indices using theint(x == y)
expression. The result is an identity matrix. -
main()
: This function tests theidMatrix
andidMatrix2
functions by creating a 5x5 identity matrix and printing it for both functions. -
compose(g)
: This function performs right-to-left function composition. It takes a functiong
and returns a function that takes another functionf
and returns a new function that appliesg
to the result of applyingf
to an argument. -
curry(f)
: This function curries a functionf
that takes two arguments. It returns a new function that takes the first argument off
and returns a function that takes the second argument and appliesf
to both arguments.
In summary, the code provides multiple ways to create identity matrices in Python, including using nested lists, maps, and list comprehensions.
Source code in the python programming language
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 ""
'''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()
>>> 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
>>>
np.mat(np.eye(size))
You may also check:How to resolve the algorithm Additive primes step by step in the Wren programming language
You may also check:How to resolve the algorithm Guess the number step by step in the Quackery programming language
You may also check:How to resolve the algorithm Bitmap/Bresenham's line algorithm step by step in the 11l programming language
You may also check:How to resolve the algorithm Variable-length quantity step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Barnsley fern step by step in the PicoLisp programming language