matrix.py 5.82 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
	"""
	: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]
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
94
		s += "\t".join(cells) + "\n"
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
95
96
97
	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
	"""
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
100
	:param matrix: the matrix
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
101
102
103
104
105
106
107
108
109
110
111
112
113
	:type matrix: matrix
	: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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
def mult_inplace(matrix, val):
	"""
	compute the scalar product of a matrix and a value
	do this operation in place

	:param matrix: the matrix
	:type matrix: matrix
	:param val: the value to mult the matrix with
	:type val: int
	"""
	for col in matrix:
		for row_nb, cell in enumerate(col):
			col[row_nb] = cell * val


Bertrand  NÉRON's avatar
Bertrand NÉRON committed
129
def get_row(matrix, row_no):
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
130
131
132
133
134
135
136
137
138
	"""
	: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
139
140
	_check_index(matrix, row_no, 0)
	row_max, col_max = size(matrix)
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
141
142
	row = []
	for col_n in range(col_max):
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
143
		row.append(get_cell(matrix, row_no, col_n))
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
144
145
146
	return row
	
	
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
147
def set_row(matrix, row_no, val):
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
148
149
150
151
152
153
154
155
156
157
	"""
	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
158
159
	_check_index(matrix, row_no, 0)
	row_max, col_max = size(matrix)
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
160
	for col_n in range(col_max):
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
161
		set_cell(matrix, row_no, col_n, val)
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
162
163


Bertrand  NÉRON's avatar
Bertrand NÉRON committed
164
def get_col(matrix, col_no):
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
165
166
167
168
169
170
171
172
173
	"""
	: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
174
	_check_index(matrix, 0, col_no)
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
175
176
177
178
	col = matrix[col_no][:]
	return col
	
	
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
179
def set_col(matrix, col_no, val):
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
180
181
182
183
184
185
186
187
188
189
	"""
	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
190
	_check_index(matrix, 0, col_no)
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
191
	for row_n in range(matrix):
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
192
		set_cell(matrix, row_n, col_no, val)
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
193
194


Bertrand  NÉRON's avatar
Bertrand NÉRON committed
195
def replace_col(matrix, col_no, col):
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
196
197
198
199
200
201
202
203
204
205
	"""
	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
206
	row_max, col_max = size(matrix)
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
207
208
209
210
	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
211
	_check_index(matrix, 0, col_no)
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
212
213
214
	matrix[col_no] = col


Bertrand  NÉRON's avatar
Bertrand NÉRON committed
215
def replace_row(matrix, row_no, row):
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
216
217
218
219
220
221
222
223
224
225
	"""
	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
226
	row_max, col_max = size(matrix)
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
227
228
	if len(row) != row_max:
		raise RuntimeError("the size of row {0} does not fit to matrix size {1}x{2}".format(len(row),
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
229
230
																							row_max,
																							col_max))
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
231
	_check_index(matrix, row_no, 0)
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
232
	for col_no, value in enumerate(row):
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
233
		set_cell(matrix, row_no, col_no, value)
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
234

Bertrand  NÉRON's avatar
Bertrand NÉRON committed
235

Bertrand  NÉRON's avatar
Bertrand NÉRON committed
236
if __name__ == '__main__':
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
237
	m = create(5, 3)
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
238
239
240
241
242
243
244
245
	print(m)
	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
246
247
248
249

	m2 = create(3, 2, 4)
	mult_inplace(m2, 2)
	print(to_str(m2))