Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
cs1-24fa
lecture-code
Commits
16138f7c
Commit
16138f7c
authored
5 months ago
by
Adam Blank
Browse files
Options
Download
Email Patches
Plain Diff
Add new file
parent
b76ae6f0
master
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
15/main.py
+79
-0
15/main.py
with
79 additions
and
0 deletions
+79
-0
15/main.py
0 → 100644
View file @
16138f7c
import
random
from
matrix
import
MutableRationalMatrix2D
,
MutableRationalVector
from
fractions
import
Fraction
"""
v = MutableRationalVector(dimensions=5)
v[2] = Fraction(100, 1)
print(Fraction(10, 1) * v)
"""
"""
M = MutableRationalMatrix2D((3, 3))
M[0][0] = Fraction(100, 1)
N = MutableRationalMatrix2D((3, 3))
N[0] = M[0]
N[1][1] = Fraction(1, 2)
print(M | N)
exit(0)
"""
###########
def
create_random_square_matrix
(
n
:
int
)
->
MutableRationalMatrix2D
:
m
=
MutableRationalMatrix2D
(
dimensions
=
(
n
,
n
))
for
i
in
range
(
m
.
dimensions
[
0
]):
for
j
in
range
(
n
):
m
[
i
][
j
]
=
Fraction
(
random
.
randint
(
-
100
,
100
),
random
.
randint
(
-
100
,
100
))
return
m
def
negate_matrix
(
M
:
MutableRationalMatrix2D
)
->
MutableRationalMatrix2D
:
m
=
MutableRationalMatrix2D
(
dimensions
=
M
.
dimensions
)
for
i
in
range
(
m
.
dimensions
[
0
]):
m
[
i
]
=
Fraction
(
-
1
,
1
)
*
M
[
i
]
# for j in range(m.dimensions[1]):
# m[i][j] = -1 * M[i][j]
return
m
random
.
seed
(
10000
)
A
=
create_random_square_matrix
(
5
)
print
(
A
)
assert
negate_matrix
(
negate_matrix
(
A
))
==
A
def
matrix_column_to_row
(
A
:
MutableRationalMatrix2D
,
i
:
int
)
->
MutableRationalVector
:
v
=
MutableRationalVector
(
dimensions
=
A
.
dimensions
[
0
])
for
j
in
range
(
A
.
dimensions
[
0
]):
v
[
j
]
=
A
[
j
][
i
]
return
v
def
multiply_matrices
(
A
:
MutableRationalMatrix2D
,
B
:
MutableRationalMatrix2D
)
->
MutableRationalMatrix2D
:
m
=
MutableRationalMatrix2D
(
dimensions
=
(
A
.
dimensions
[
0
],
B
.
dimensions
[
1
]))
for
i
in
range
(
A
.
dimensions
[
0
]):
for
j
in
range
(
B
.
dimensions
[
1
]):
m
[
i
][
j
]
=
A
[
i
]
*
matrix_column_to_row
(
B
,
j
)
return
m
x
:
list
[
list
[
int
]]
=
[[
1
,
0
,
1
],
[
2
,
1
,
1
],
[
0
,
1
,
1
],
[
1
,
1
,
2
]]
y
:
list
[
list
[
int
]]
=
[[
1
,
2
,
1
],
[
2
,
3
,
1
],
[
4
,
2
,
2
]]
z
:
list
[
list
[
int
]]
=
[[
5
,
4
,
3
],
[
8
,
9
,
5
],
[
6
,
5
,
3
],
[
11
,
9
,
6
]]
def
matrix_create
(
m
:
list
[
list
[
int
]])
->
MutableRationalMatrix2D
:
out
=
MutableRationalMatrix2D
(
dimensions
=
(
len
(
m
),
len
(
m
[
0
])))
for
i
in
range
(
out
.
dimensions
[
0
]):
for
j
in
range
(
out
.
dimensions
[
1
]):
out
[
i
][
j
]
=
Fraction
(
m
[
i
][
j
],
1
)
return
out
assert
(
matrix_create
(
z
)
==
multiply_matrices
(
matrix_create
(
x
),
matrix_create
(
y
)))
This diff is collapsed.
Click to expand it.
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
Menu
Projects
Groups
Snippets
Help