matrix.py 5.6 KB
Newer Older
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
1
2
3
4
5
"""
Implementation of simple matrix 
"""


Bertrand  NÉRON's avatar
Bertrand NÉRON committed
6
def create(row_num, col_num, val = None):
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
	"""
	:param row_num: the number of rows
	:type row_num: int
	:param col_num: the number of columns
	:type col_num: int
	:param val: the default value to fill the matrix
	:type val: any (None by default)
	:return: matrix of rows_num x col_num
	:rtype: matrix
	"""
	matrix = []
	for i in range(col_num):
		col = [val] * row_num
		matrix.append(col)
	return matrix


Bertrand  NÉRON's avatar
Bertrand NÉRON committed
24
def _check_index(matrix, row_no, col_no):
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
25
26
27
28
29
30
31
32
33
34
35
	"""
	check if row_no and col_no are in matrix bound
	
	:param matrix: the matrix to compute the size
	:type matrix: matrix
	:param rows_no: the index of row to check
	:type rows_no: int
	:param col_no: the index of column to check
	:type col_no: int
	:raise: IndexError if row_no or col_no are out of matrix bounds
	""" 
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
36
	row_max, col_max = size(matrix)
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
37
38
39
40
	if (row_no < 0 or row_no >= row_max) or (col_no < 0 or col_no >= col_max):
		raise IndexError("matrix index out of range")
	
	
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
41
def size(matrix):
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
42
43
44
45
46
47
48
49
50
	"""
	:param matrix: the matrix to compute the size
	:type matrix: matrix
	:return: the size of matrix (number of rows, number of cols)
	:rtype: typle of 2 int
	"""
	return len(matrix[0]), len(matrix)


Bertrand  NÉRON's avatar
Bertrand NÉRON committed
51
def get_cell(matrix, row_no, col_no):
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
52
53
54
55
56
57
58
59
60
61
	"""
	:param matrix: the matrix 
	:type matrix: matrix
	:param rows_no: the row number
	:type rows_no: int
	:param col_no: the column number
	:type col_no: int
	:retrun: the content of cell corresponding to row_no x col_no
	:rtype: any
	"""
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
62
	_check_index(matrix, row_no, col_no)
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
63
64
65
	return matrix[col_no][row_no]


Bertrand  NÉRON's avatar
Bertrand NÉRON committed
66
def set_cell(matrix, row_no, col_no, val):
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
67
68
69
70
71
72
73
74
75
76
77
78
	"""
	set the value val in cell specified by row_no x col_no
	
	:param matrix: the matrix to modify
	:type matrix: matrix
	:param row_no: the row number of cell to set
	:type rows_no: int
	:param col_no: the column number of cell to set
	:type col_no: int
	:param val: the value to set in cell 
	:type val: int
	"""
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
79
	_check_index(matrix, row_no, col_no)
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
80
81
82
	matrix[col_no][row_no] = val


Bertrand  NÉRON's avatar
Bertrand NÉRON committed
83
def to_str(matrix):
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
	"""
	:param matrix: the matrix to compute the size
	:type matrix: matrix
	:return: a string representation of the matrix
	:rtype: str
	"""
	s = ""
	# by design all matrix cols have same size
	for row in zip(*matrix):
		cells = [str(cell) for cell in row]
		s += " ".join(cells) + "\n"
	return s


Bertrand  NÉRON's avatar
Bertrand NÉRON committed
98
def mult(matrix, val):
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
	"""
	:param matrix: the matrix to compute the size
	:type matrix: matrix
	:param rows_no: the number of rows
	:type rows_no: int
	:param col_no: the number of columns
	:type col_no: int
	:param val: the value to mult the matrix with
	:type val: int
	:return: a new matrix corresponding the scalar product of matrix * val
	:rtype: matrix
	"""
	new_matrix = []
	for col in matrix:
		new_col = [cell * val for cell in col]
		new_matrix.append(new_col)
	return new_matrix


Bertrand  NÉRON's avatar
Bertrand NÉRON committed
118
def get_row(matrix, row_no):
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
119
120
121
122
123
124
125
126
127
	"""
	:param matrix: the matrix to compute the size
	:type matrix: matrix
	:param rows_no: row number
	:type rows_no: int
	:return: the row of matrix corresponding to row_no
	         a shallow copy of the row
	:rtype: list
	"""
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
128
129
	_check_index(matrix, row_no, 0)
	row_max, col_max = size(matrix)
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
130
131
	row = []
	for col_n in range(col_max):
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
132
		row.append(get_cell(matrix, row_no, col_n))
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
133
134
135
	return row
	
	
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
136
def set_row(matrix, row_no, val):
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
137
138
139
140
141
142
143
144
145
146
	"""
	set all cells of row row_no with val
	
	:param matrix: the matrix to modify
	:type matrix: matrix
	:param row_no: the row number
	:type row_no: int
	:param val: the value to put in cells
	:type val: any
	"""
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
147
148
	_check_index(matrix, row_no, 0)
	row_max, col_max = size(matrix)
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
149
	for col_n in range(col_max):
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
150
		set_cell(matrix, row_no, col_n, val)
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
151
152


Bertrand  NÉRON's avatar
Bertrand NÉRON committed
153
def get_col(matrix, col_no):
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
154
155
156
157
158
159
160
161
162
	"""
	:param matrix: the matrix get row
	:type matrix: matrix
	:param col_no: the column number
	:type col_no: int
	:return: the column corresponding to col_no of matrix
	         a shallow copy of the col
	:rtype: list
	"""
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
163
	_check_index(matrix, 0, col_no)
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
164
165
166
167
	col = matrix[col_no][:]
	return col
	
	
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
168
def set_col(matrix, col_no, val):
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
169
170
171
172
173
174
175
176
177
178
	"""
	set all cells of col col_no with val
	
	:param matrix: the matrix to compute the size
	:type matrix: matrix
	:param col_no: the column number
	:type col_no: int
	:param val: the value to put in cells
	:type val: any
	"""
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
179
180
	_check_index(matrix, 0, col_no)
	row_max, col_max = size(matrix)
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
181
	for row_n in range(im):
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
182
		set_cell(matrix, row_n, col_no, val)
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
183
184


Bertrand  NÉRON's avatar
Bertrand NÉRON committed
185
def replace_col(matrix, col_no, col):
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
186
187
188
189
190
191
192
193
194
195
	"""
	replace column col_no with col
	
	:param matrix: the matrix to compute the size
	:type matrix: matrix
	:param col_no: the column number to replace
	:type col_no: int
	:param col: the list of values to use as replacement of column 
	:type col: list
	"""
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
196
	row_max, col_max = size(matrix)
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
197
198
199
200
	if len(col) != col_max:
		raise RuntimeError("the size of col {0} does not fit to matrix size {1}x{2}".format(len(col),
																						row_max,
																						col_max))
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
201
	_check_index(matrix, 0, col_no)
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
202
203
204
	matrix[col_no] = col


Bertrand  NÉRON's avatar
Bertrand NÉRON committed
205
def replace_row(matrix, row_no, row):
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
206
207
208
209
210
211
212
213
214
215
	"""
	replace row row_no with row
	
	:param matrix: the matrix to compute the size
	:type matrix: matrix
	:param row_no: the column number
	:type row_no: int
	:param row: the list of value to use as replacement of row 
	:type row: list
	"""
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
216
	row_max, col_max = size(matrix)
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
217
218
219
220
	if len(row) != row_max:
		raise RuntimeError("the size of row {0} does not fit to matrix size {1}x{2}".format(len(row),
																						row_max,
																						col_max))
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
221
	_check_index(matrix, row_no, 0)
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
222
	for col_no, value in enumerate(row):
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
223
		set_cell(matrix, row_no, col_no, value)
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
224
225
226
227

		
	
if __name__ == '__main__':
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
228
	m = create(5, 3)
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
229
	print m
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
230
231
232
233
234
235
236
	set_cell(m,0, 0, 1)
	set_cell(m,0, 2, 2)
	set_cell(m,4, 0, 12)
	set_cell(m,4, 2, 15)
	print to_str(m)
	print "get row 0",  get_row(m, 0)
	print "get col 0", get_col(m, 0)
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
237