Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
hub-courses
python_one_week_4_biologists_solutions
Commits
911e2ccf
Unverified
Commit
911e2ccf
authored
Jun 15, 2021
by
Bertrand NÉRON
Browse files
fix doc in matrix
parent
dba0c7b4
Changes
1
Hide whitespace changes
Inline
Side-by-side
source/_static/code/matrix.py
View file @
911e2ccf
...
...
@@ -91,18 +91,14 @@ def to_str(matrix):
# by design all matrix cols have same size
for
row
in
zip
(
*
matrix
):
cells
=
[
str
(
cell
)
for
cell
in
row
]
s
+=
"
"
.
join
(
cells
)
+
"
\n
"
s
+=
"
\t
"
.
join
(
cells
)
+
"
\n
"
return
s
def
mult
(
matrix
,
val
):
"""
:param matrix: the matrix
to compute the size
:param matrix: the matrix
: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
...
...
@@ -115,6 +111,21 @@ def mult(matrix, val):
return
new_matrix
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
def
get_row
(
matrix
,
row_no
):
"""
:param matrix: the matrix to compute the size
...
...
@@ -221,8 +232,7 @@ def replace_row(matrix, row_no, row):
for
col_no
,
value
in
enumerate
(
row
):
set_cell
(
matrix
,
row_no
,
col_no
,
value
)
if
__name__
==
'__main__'
:
m
=
create
(
5
,
3
)
print
(
m
)
...
...
@@ -233,4 +243,7 @@ if __name__ == '__main__':
print
(
to_str
(
m
))
print
(
"get row 0"
,
get_row
(
m
,
0
))
print
(
"get col 0"
,
get_col
(
m
,
0
))
m2
=
create
(
3
,
2
,
4
)
mult_inplace
(
m2
,
2
)
print
(
to_str
(
m2
))
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment