-
Notifications
You must be signed in to change notification settings - Fork 0
Dz11 #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
karmusha
wants to merge
3
commits into
master
Choose a base branch
from
dz11
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Dz11 #11
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| # Добавьте ко всем задачам с семинара строки документации и методы вывода информации на печать. | ||
|
|
||
| # Создайте класс Матрица. Добавьте методы для: | ||
| # - вывода на печать, | ||
| # - сравнения, (если равны: одинаковый размер, количество элементов) | ||
| # - сложения, (если они равны) | ||
| # - *умножения матриц (если кол-во столбцов одной мартицы равно кол-ву строк другой матрицы) | ||
|
|
||
| class Matrix: | ||
| """Класс Матрица. Создаётся из списка списков.""" | ||
|
|
||
|
|
||
| def __init__(self, matrix): | ||
| self.matrix = matrix | ||
|
|
||
| def __str__(self): | ||
| """Вывод матрицы на печать для пользователя.""" | ||
|
|
||
| return '[' + ']\n['.join('\t'.join(map(str, row)) for row in self.matrix) + ']' | ||
|
|
||
|
|
||
| def __repr__(self) -> str: | ||
| return '\n' + self.__str__() | ||
|
|
||
| def __getitem__(self, index): | ||
| return self.matrix[index] | ||
|
|
||
| def matrix_size(self): | ||
| rows = len(self.matrix) | ||
| columns = len(self.matrix[0]) | ||
| return rows, columns | ||
|
|
||
| def get_high(self): | ||
| return len(self.matrix[0]) | ||
|
|
||
| def get_weight(self): | ||
| return len(self.matrix) | ||
|
|
||
| def check_matrix_equality(self, other): | ||
| """ | ||
| Проверяет мартицы одинаковость размерностей | ||
| :param self: Первая матрица | ||
| :param other: Вторая матрица | ||
|
|
||
| :return: True or False | ||
| """ | ||
|
|
||
| if self.get_high() == other.get_high() and self.get_weight() == other.get_weight(): | ||
| return True | ||
|
|
||
| return False | ||
|
|
||
| def __eq__(self, other): | ||
| """Сравнение мартиц одинавовой размерности на равенство""" | ||
|
|
||
| if self.check_matrix_equality(other): | ||
| res = zip(self.matrix, other) | ||
| res = map(lambda x: x[0] == x[1], res) | ||
| return all(res) | ||
|
|
||
| return False | ||
|
karmusha marked this conversation as resolved.
|
||
|
|
||
| def __add__(self, other): | ||
| """Сложение мартиц одинавовой размерности""" | ||
|
|
||
| if not self.check_matrix_equality(other): | ||
| return 'Нельзя сложить матрицы разных размерностей' | ||
|
karmusha marked this conversation as resolved.
|
||
|
|
||
| result = [] | ||
| numbers = [] | ||
| for i in range(self.get_weight()): | ||
| for j in range(self.get_high()): | ||
| summa = other[i][j] + self.matrix[i][j] | ||
| numbers.append(summa) | ||
| if len(numbers) == len(self.matrix): | ||
| result.append(numbers) | ||
| numbers = [] | ||
|
karmusha marked this conversation as resolved.
|
||
|
|
||
| return Matrix(result) | ||
|
|
||
| def __mul__(self, other): | ||
| """Умножение мартиц (длина одной матрицы должна быть равна ширине другой матрицы)""" | ||
|
|
||
| if self.get_high() != other.get_weight(): | ||
| return 'Нельзя перемножить такие матрицы' | ||
|
|
||
| result = [] | ||
| for i in range(self.get_weight()): | ||
| res = [] | ||
| for j in range(other.get_weight()): | ||
| el, m = 0, 0 | ||
| for k in range(self.get_high()): | ||
| m = self[i][k] * other[k][j] | ||
| el += m | ||
| res.append(el) | ||
| result.append(res) | ||
|
karmusha marked this conversation as resolved.
|
||
| return Matrix(result) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| m1 = Matrix([ | ||
| [1, 1, 1], | ||
| [2, 2, 2], | ||
| [3, 3, 3] | ||
| ]) | ||
|
|
||
| m2 = Matrix([ | ||
| [10, 10, 10], | ||
| [20, 20, 20], | ||
| [30, 30, 30] | ||
| ]) | ||
|
|
||
| m3 = Matrix([ | ||
| [1, 1, 1], | ||
| [2, 2, 2], | ||
| [3, 3, 3] | ||
| ]) | ||
|
|
||
| print(f'{m1 = }') | ||
| print(f'{m2 = }') | ||
| print(f'{m3 = }') | ||
|
|
||
| print(f'{m1 == m2 = }') | ||
| print(f'{m1 == m3 = }') | ||
|
|
||
| print(f'{m1 + m2 = }') | ||
| print(f'{m1 * m2 = }') | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.