Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
python_one_week_4_biologists_solutions
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
hub-courses
python_one_week_4_biologists_solutions
Commits
fb1db00b
Commit
fb1db00b
authored
10 years ago
by
Bertrand NÉRON
Browse files
Options
Downloads
Patches
Plain Diff
rename functions
parent
2e3acbb0
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
source/_static/code/matrix.py
+39
-39
39 additions, 39 deletions
source/_static/code/matrix.py
with
39 additions
and
39 deletions
source/_static/code/matrix.py
+
39
−
39
View file @
fb1db00b
...
@@ -3,7 +3,7 @@ Implementation of simple matrix
...
@@ -3,7 +3,7 @@ Implementation of simple matrix
"""
"""
def
matrix_maker
(
row_num
,
col_num
,
val
=
None
):
def
create
(
row_num
,
col_num
,
val
=
None
):
"""
"""
:param row_num: the number of rows
:param row_num: the number of rows
:type row_num: int
:type row_num: int
...
@@ -21,7 +21,7 @@ def matrix_maker(row_num, col_num, val = None):
...
@@ -21,7 +21,7 @@ def matrix_maker(row_num, col_num, val = None):
return
matrix
return
matrix
def
_check_
matrix_
index
(
matrix
,
row_no
,
col_no
):
def
_check_index
(
matrix
,
row_no
,
col_no
):
"""
"""
check if row_no and col_no are in matrix bound
check if row_no and col_no are in matrix bound
...
@@ -33,12 +33,12 @@ def _check_matrix_index(matrix, row_no, col_no):
...
@@ -33,12 +33,12 @@ def _check_matrix_index(matrix, row_no, col_no):
:type col_no: int
:type col_no: int
:raise: IndexError if row_no or col_no are out of matrix bounds
:raise: IndexError if row_no or col_no are out of matrix bounds
"""
"""
row_max
,
col_max
=
matrix_
size
(
matrix
)
row_max
,
col_max
=
size
(
matrix
)
if
(
row_no
<
0
or
row_no
>=
row_max
)
or
(
col_no
<
0
or
col_no
>=
col_max
):
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
"
)
raise
IndexError
(
"
matrix index out of range
"
)
def
matrix_
size
(
matrix
):
def
size
(
matrix
):
"""
"""
:param matrix: the matrix to compute the size
:param matrix: the matrix to compute the size
:type matrix: matrix
:type matrix: matrix
...
@@ -48,7 +48,7 @@ def matrix_size(matrix):
...
@@ -48,7 +48,7 @@ def matrix_size(matrix):
return
len
(
matrix
[
0
]),
len
(
matrix
)
return
len
(
matrix
[
0
]),
len
(
matrix
)
def
matrix_
get_cell
(
matrix
,
row_no
,
col_no
):
def
get_cell
(
matrix
,
row_no
,
col_no
):
"""
"""
:param matrix: the matrix
:param matrix: the matrix
:type matrix: matrix
:type matrix: matrix
...
@@ -59,11 +59,11 @@ def matrix_get_cell(matrix, row_no, col_no):
...
@@ -59,11 +59,11 @@ def matrix_get_cell(matrix, row_no, col_no):
:retrun: the content of cell corresponding to row_no x col_no
:retrun: the content of cell corresponding to row_no x col_no
:rtype: any
:rtype: any
"""
"""
_check_
matrix_
index
(
matrix
,
row_no
,
col_no
)
_check_index
(
matrix
,
row_no
,
col_no
)
return
matrix
[
col_no
][
row_no
]
return
matrix
[
col_no
][
row_no
]
def
matrix_
set_cell
(
matrix
,
row_no
,
col_no
,
val
):
def
set_cell
(
matrix
,
row_no
,
col_no
,
val
):
"""
"""
set the value val in cell specified by row_no x col_no
set the value val in cell specified by row_no x col_no
...
@@ -76,11 +76,11 @@ def matrix_set_cell(matrix, row_no, col_no, val):
...
@@ -76,11 +76,11 @@ def matrix_set_cell(matrix, row_no, col_no, val):
:param val: the value to set in cell
:param val: the value to set in cell
:type val: int
:type val: int
"""
"""
_check_
matrix_
index
(
matrix
,
row_no
,
col_no
)
_check_index
(
matrix
,
row_no
,
col_no
)
matrix
[
col_no
][
row_no
]
=
val
matrix
[
col_no
][
row_no
]
=
val
def
matrix_
to_str
(
matrix
):
def
to_str
(
matrix
):
"""
"""
:param matrix: the matrix to compute the size
:param matrix: the matrix to compute the size
:type matrix: matrix
:type matrix: matrix
...
@@ -95,7 +95,7 @@ def matrix_to_str(matrix):
...
@@ -95,7 +95,7 @@ def matrix_to_str(matrix):
return
s
return
s
def
matrix_
mult
(
matrix
,
val
):
def
mult
(
matrix
,
val
):
"""
"""
:param matrix: the matrix to compute the size
:param matrix: the matrix to compute the size
:type matrix: matrix
:type matrix: matrix
...
@@ -115,7 +115,7 @@ def matrix_mult(matrix, val):
...
@@ -115,7 +115,7 @@ def matrix_mult(matrix, val):
return
new_matrix
return
new_matrix
def
matrix_
get_row
(
matrix
,
row_no
):
def
get_row
(
matrix
,
row_no
):
"""
"""
:param matrix: the matrix to compute the size
:param matrix: the matrix to compute the size
:type matrix: matrix
:type matrix: matrix
...
@@ -125,15 +125,15 @@ def matrix_get_row(matrix, row_no):
...
@@ -125,15 +125,15 @@ def matrix_get_row(matrix, row_no):
a shallow copy of the row
a shallow copy of the row
:rtype: list
:rtype: list
"""
"""
_check_
matrix_
index
(
matrix
,
row_no
,
0
)
_check_index
(
matrix
,
row_no
,
0
)
row_max
,
col_max
=
matrix_
size
(
matrix
)
row_max
,
col_max
=
size
(
matrix
)
row
=
[]
row
=
[]
for
col_n
in
range
(
col_max
):
for
col_n
in
range
(
col_max
):
row
.
append
(
matrix_
get_cell
(
matrix
,
row_no
,
col_n
))
row
.
append
(
get_cell
(
matrix
,
row_no
,
col_n
))
return
row
return
row
def
matrix_
set_row
(
matrix
,
row_no
,
val
):
def
set_row
(
matrix
,
row_no
,
val
):
"""
"""
set all cells of row row_no with val
set all cells of row row_no with val
...
@@ -144,13 +144,13 @@ def matrix_set_row(matrix, row_no, val):
...
@@ -144,13 +144,13 @@ def matrix_set_row(matrix, row_no, val):
:param val: the value to put in cells
:param val: the value to put in cells
:type val: any
:type val: any
"""
"""
_check_
matrix_
index
(
matrix
,
row_no
,
0
)
_check_index
(
matrix
,
row_no
,
0
)
row_max
,
col_max
=
matrix_
size
(
matrix
)
row_max
,
col_max
=
size
(
matrix
)
for
col_n
in
range
(
col_max
):
for
col_n
in
range
(
col_max
):
matrix_
set_cell
(
matrix
,
row_no
,
col_n
,
val
)
set_cell
(
matrix
,
row_no
,
col_n
,
val
)
def
matrix_
get_col
(
matrix
,
col_no
):
def
get_col
(
matrix
,
col_no
):
"""
"""
:param matrix: the matrix get row
:param matrix: the matrix get row
:type matrix: matrix
:type matrix: matrix
...
@@ -160,12 +160,12 @@ def matrix_get_col(matrix, col_no):
...
@@ -160,12 +160,12 @@ def matrix_get_col(matrix, col_no):
a shallow copy of the col
a shallow copy of the col
:rtype: list
:rtype: list
"""
"""
_check_
matrix_
index
(
matrix
,
0
,
col_no
)
_check_index
(
matrix
,
0
,
col_no
)
col
=
matrix
[
col_no
][:]
col
=
matrix
[
col_no
][:]
return
col
return
col
def
matrix_
set_col
(
matrix
,
col_no
,
val
):
def
set_col
(
matrix
,
col_no
,
val
):
"""
"""
set all cells of col col_no with val
set all cells of col col_no with val
...
@@ -176,13 +176,13 @@ def matrix_set_col(matrix, col_no, val):
...
@@ -176,13 +176,13 @@ def matrix_set_col(matrix, col_no, val):
:param val: the value to put in cells
:param val: the value to put in cells
:type val: any
:type val: any
"""
"""
_check_
matrix_
index
(
matrix
,
0
,
col_no
)
_check_index
(
matrix
,
0
,
col_no
)
row_max
,
col_max
=
matrix_
size
(
matrix
)
row_max
,
col_max
=
size
(
matrix
)
for
row_n
in
range
(
im
):
for
row_n
in
range
(
im
):
matrix_
set_cell
(
matrix
,
row_n
,
col_no
,
val
)
set_cell
(
matrix
,
row_n
,
col_no
,
val
)
def
matrix_
replace_col
(
matrix
,
col_no
,
col
):
def
replace_col
(
matrix
,
col_no
,
col
):
"""
"""
replace column col_no with col
replace column col_no with col
...
@@ -193,16 +193,16 @@ def matrix_replace_col(matrix, col_no, col):
...
@@ -193,16 +193,16 @@ def matrix_replace_col(matrix, col_no, col):
:param col: the list of values to use as replacement of column
:param col: the list of values to use as replacement of column
:type col: list
:type col: list
"""
"""
row_max
,
col_max
=
matrix_
size
(
matrix
)
row_max
,
col_max
=
size
(
matrix
)
if
len
(
col
)
!=
col_max
:
if
len
(
col
)
!=
col_max
:
raise
RuntimeError
(
"
the size of col {0} does not fit to matrix size {1}x{2}
"
.
format
(
len
(
col
),
raise
RuntimeError
(
"
the size of col {0} does not fit to matrix size {1}x{2}
"
.
format
(
len
(
col
),
row_max
,
row_max
,
col_max
))
col_max
))
_check_
matrix_
index
(
matrix
,
0
,
col_no
)
_check_index
(
matrix
,
0
,
col_no
)
matrix
[
col_no
]
=
col
matrix
[
col_no
]
=
col
def
matrix_
replace_row
(
matrix
,
row_no
,
row
):
def
replace_row
(
matrix
,
row_no
,
row
):
"""
"""
replace row row_no with row
replace row row_no with row
...
@@ -213,25 +213,25 @@ def matrix_replace_row(matrix, row_no, row):
...
@@ -213,25 +213,25 @@ def matrix_replace_row(matrix, row_no, row):
:param row: the list of value to use as replacement of row
:param row: the list of value to use as replacement of row
:type row: list
:type row: list
"""
"""
row_max
,
col_max
=
matrix_
size
(
matrix
)
row_max
,
col_max
=
size
(
matrix
)
if
len
(
row
)
!=
row_max
:
if
len
(
row
)
!=
row_max
:
raise
RuntimeError
(
"
the size of row {0} does not fit to matrix size {1}x{2}
"
.
format
(
len
(
row
),
raise
RuntimeError
(
"
the size of row {0} does not fit to matrix size {1}x{2}
"
.
format
(
len
(
row
),
row_max
,
row_max
,
col_max
))
col_max
))
_check_
matrix_
index
(
matrix
,
row_no
,
0
)
_check_index
(
matrix
,
row_no
,
0
)
for
col_no
,
value
in
enumerate
(
row
):
for
col_no
,
value
in
enumerate
(
row
):
matrix_
set_cell
(
matrix
,
row_no
,
col_no
,
value
)
set_cell
(
matrix
,
row_no
,
col_no
,
value
)
if
__name__
==
'
__main__
'
:
if
__name__
==
'
__main__
'
:
m
=
matrix_maker
(
5
,
3
)
m
=
create
(
5
,
3
)
print
m
print
m
matrix_
set_cell
(
m
,
0
,
0
,
1
)
set_cell
(
m
,
0
,
0
,
1
)
matrix_
set_cell
(
m
,
0
,
2
,
2
)
set_cell
(
m
,
0
,
2
,
2
)
matrix_
set_cell
(
m
,
4
,
0
,
12
)
set_cell
(
m
,
4
,
0
,
12
)
matrix_
set_cell
(
m
,
4
,
2
,
15
)
set_cell
(
m
,
4
,
2
,
15
)
print
matrix_
to_str
(
m
)
print
to_str
(
m
)
print
"
get row 0
"
,
matrix_
get_row
(
m
,
0
)
print
"
get row 0
"
,
get_row
(
m
,
0
)
print
"
get col 0
"
,
matrix_
get_col
(
m
,
0
)
print
"
get col 0
"
,
get_col
(
m
,
0
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment