matrix.py 5.86 KB
Newer Older
Bertrand  NÉRON's avatar
Bertrand NÉRON committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
"""
Implementation of simple matrix 
"""


def matrix_maker(row_num, col_num, val = None):
	"""
	: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


def _check_matrix_index(matrix, row_no, col_no):
	"""
	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
	""" 
	row_max, col_max = matrix_size(matrix)
	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")
	
	
def matrix_size(matrix):
	"""
	: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)


def matrix_get_cell(matrix, row_no, col_no):
	"""
	: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
	"""
	_check_matrix_index(matrix, row_no, col_no)
	return matrix[col_no][row_no]


def matrix_set_cell(matrix, row_no, col_no, val):
	"""
	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
	"""
	_check_matrix_index(matrix, row_no, col_no)
	matrix[col_no][row_no] = val


def matrix_to_str(matrix):
	"""
	: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


def matrix_mult(matrix, val):
	"""
	: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


def matrix_get_row(matrix, row_no):
	"""
	: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
	"""
	_check_matrix_index(matrix, row_no, 0)
	row_max, col_max = matrix_size(matrix)
	row = []
	for col_n in range(col_max):
		row.append(matrix_get_cell(matrix, row_no, col_n))
	return row
	
	
def matrix_set_row(matrix, row_no, val):
	"""
	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
	"""
	_check_matrix_index(matrix, row_no, 0)
	row_max, col_max = matrix_size(matrix)
	for col_n in range(col_max):
		matrix_set_cell(matrix, row_no, col_n, val)


def matrix_get_col(matrix, col_no):
	"""
	: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
	"""
	_check_matrix_index(matrix, 0, col_no)
	col = matrix[col_no][:]
	return col
	
	
def matrix_set_col(matrix, col_no, val):
	"""
	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
	"""
	_check_matrix_index(matrix, 0, col_no)
	row_max, col_max = matrix_size(matrix)
	for row_n in range(im):
		matrix_set_cell(matrix, row_n, col_no, val)


def matrix_replace_col(matrix, col_no, col):
	"""
	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
	"""
	row_max, col_max = matrix_size(matrix)
	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))
	_check_matrix_index(matrix, 0, col_no)
	matrix[col_no] = col


def matrix_replace_row(matrix, row_no, row):
	"""
	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
	"""
	row_max, col_max = matrix_size(matrix)
	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))
	_check_matrix_index(matrix, row_no, 0)
	for col_no, value in enumerate(row):
		matrix_set_cell(matrix, row_no, col_no, value)

		
	
if __name__ == '__main__':
	m = matrix_maker(5, 3)
	print m
	matrix_set_cell(m,0, 0, 1)
	matrix_set_cell(m,0, 2, 2)
	matrix_set_cell(m,4, 0, 12)
	matrix_set_cell(m,4, 2, 15)
	print matrix_to_str(m)
	print "get row 0",  matrix_get_row(m, 0)
	print "get col 0", matrix_get_col(m, 0)