Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added docstrings
  • Loading branch information
ad71 committed Jun 6, 2018
commit 5a79debe9c453f9ebdcc62f2ac3fdffb698f3e87
12 changes: 12 additions & 0 deletions mdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,12 @@ def max_difference(self, U1, U2):


class Matrix:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure ... maybe we should use numpy.matrix ?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are to use numpy here, we should consider using numpy on the rest of the repository as well. It would be an interesting undertaking and I am all for it, but adding another dependency is something that needs discussion.

Maybe an Issue can be opened to discuss this when this is more concrete?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we aren't supposed to use external libraries, but using numpy will make room for a lot of improvements in many algorithms. I think this can be discussed in a new issue as @MrDupin suggested.

"""Matrix operations class"""

@staticmethod
def add(A, B):
"""Add two matrices A and B"""

res = []
for i in range(len(A)):
row = []
Expand All @@ -386,13 +390,17 @@ def add(A, B):

@staticmethod
def scalar_multiply(a, B):
"""Multiply scalar a to matrix B"""

for i in range(len(B)):
for j in range(len(B[0])):
B[i][j] = a * B[i][j]
return B

@staticmethod
def multiply(A, B):
"""Multiply two matrices A and B element-wise"""

matrix = []
for i in range(len(B)):
row = []
Expand All @@ -404,10 +412,14 @@ def multiply(A, B):

@staticmethod
def matmul(A, B):
"""Inner-product of two matrices"""

return [[sum(ele_a*ele_b for ele_a, ele_b in zip(row_a, col_b)) for col_b in list(zip(*B))] for row_a in A]

@staticmethod
def transpose(A):
"""Transpose a matrix"""

return [list(i) for i in zip(*A)]


Expand Down